Skip to content

Commit

Permalink
Merge pull request #52 from WordPress/add-current-events
Browse files Browse the repository at this point in the history
Add the current events in the event list
  • Loading branch information
amieiro authored Feb 14, 2024
2 parents 1c1eda6 + 6ec6d61 commit e808b29
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 27 deletions.
50 changes: 39 additions & 11 deletions includes/class-wporg-gp-translation-events-route.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,53 @@ public function __construct() {
* @return void
*/
public function events_list() {
$current_datetime_utc = ( new DateTime( 'now', new DateTimeZone( 'UTC' ) ) )->format( 'Y-m-d H:i:s' );
$_paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'event',
'posts_per_page' => 10,
'paged' => $_paged,
'post_status' => 'publish',
'meta_query' => array(
$current_datetime_utc = ( new DateTime( 'now', new DateTimeZone( 'UTC' ) ) )->format( 'Y-m-d H:i:s' );
$_current_events_paged = isset( $_GET[ 'current_events_paged' ] ) && is_numeric( $_GET[ 'current_events_paged' ] ) ? $_GET[ 'current_events_paged' ] : 1;
$_upcoming_events_paged = isset( $_GET[ 'upcoming_events_paged' ] ) && is_numeric( $_GET[ 'upcoming_events_paged' ] ) ? $_GET[ 'upcoming_events_paged' ] : 1;

$current_events_args = array(
'post_type' => 'event',
'posts_per_page' => 10,
'current_events_paged' => $_current_events_paged,
'paged' => $_current_events_paged,
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => '_event_start',
'value' => $current_datetime_utc,
'compare' => '<=',
'type' => 'DATETIME',
),
array(
'key' => '_event_end',
'value' => $current_datetime_utc,
'compare' => '>=',
'type' => 'DATETIME',
),
),
'orderby' => 'meta_value',
'order' => 'ASC',
);
$current_events_query = new WP_Query( $current_events_args );

$upcoming_events_args = array(
'post_type' => 'event',
'posts_per_page' => 10,
'upcoming_events_paged' => $_upcoming_events_paged,
'paged' => $_upcoming_events_paged,
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => '_event_start',
'value' => $current_datetime_utc,
'compare' => '>=',
'type' => 'DATETIME',
),
),
'orderby' => 'meta_value',
'order' => 'ASC',
'orderby' => 'meta_value',
'order' => 'ASC',
);
$query = new WP_Query( $args );
$upcoming_events_query = new WP_Query( $upcoming_events_args );
$this->tmpl( 'events-list', get_defined_vars() );
}

Expand Down
66 changes: 50 additions & 16 deletions templates/events-list.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,75 @@



<h2 class="event_page_title">Upcoming Translation Events</h2>
<h2 class="event_page_title">Translation Events</h2>
<div class="event-left-col">
<?php
if ( $query->have_posts() ) :
if ( $current_events_query->have_posts() ) :
?>
<h3>Current events</h3>
<ul>
<?php
while ( $query->have_posts() ) :
$query->the_post();
$event_start = get_post_meta( get_the_ID(), '_event_start', true );
while ( $current_events_query->have_posts() ) :
$current_events_query->the_post();
$event_start = ( new DateTime( get_post_meta( get_the_ID(), '_event_start', true ) ) )->format('l, F j, Y');
?>
<li class="event-list-item">
<span class="event-list-date"><time class="event-utc-time" datetime="<?php echo esc_attr( $event_start ); ?>"></span>
<span class="event-list-date"><?php echo esc_html( $event_start ); ?></span>
<a href="<?php echo esc_url( gp_url( wp_make_link_relative( get_the_permalink() ) ) ) ?>"><?php the_title(); ?></a> by <span><?php the_author(); ?></span>
<p><?php the_excerpt(); ?></p>
</li>
<?php
<?php
endwhile;
?>
</ul>

<?php
echo paginate_links(
array(
'total' => $current_events_query->max_num_pages,
'current' => max( 1, $current_events_query->query_vars['current_events_paged'] ),
'format' => '?current_events_paged=%#%',
'prev_text' => '&laquo; Previous',
'next_text' => 'Next &raquo;',
)
);

wp_reset_postdata();
endif;
if ( $upcoming_events_query->have_posts() ) :
?>
<h3>Upcoming events</h3>
<ul>
<?php
while ( $upcoming_events_query->have_posts() ) :
$upcoming_events_query->the_post();
$event_start = ( new DateTime( get_post_meta( get_the_ID(), '_event_start', true ) ) )->format('l, F j, Y');
?>
<li class="event-list-item">
<span class="event-list-date"><?php echo esc_html( $event_start ); ?></span>
<a href="<?php echo esc_url( gp_url( wp_make_link_relative( get_the_permalink() ) ) ) ?>"><?php the_title(); ?></a> by <span><?php the_author(); ?></span>
<p><?php the_excerpt(); ?></p>
</li>
<?php
endwhile;
?>
</ul>

<?php
echo esc_html(
paginate_links(
array(
'total' => $query->max_num_pages,
'current' => max( 1, get_query_var( 'paged' ) ),
'prev_text' => '&laquo; Previous',
'next_text' => 'Next &raquo;',
)
echo paginate_links(
array(
'total' => $upcoming_events_query->max_num_pages,
'current' => max( 1, $upcoming_events_query->query_vars['upcoming_events_paged'] ),
'format' => '?upcoming_events_paged=%#%',
'prev_text' => '&laquo; Previous',
'next_text' => 'Next &raquo;',
)
);

wp_reset_postdata();
else :
endif;

if ( 0 === $current_events_query->post_count && 0 === $upcoming_events_query->post_count ) :
echo 'No events found.';
endif;
?>
Expand Down

0 comments on commit e808b29

Please sign in to comment.