diff --git a/assets/css/translation-events.css b/assets/css/translation-events.css index 3a2f25de..83185108 100644 --- a/assets/css/translation-events.css +++ b/assets/css/translation-events.css @@ -138,10 +138,10 @@ h2.event_page_title { a.event-link-draft { color:#80807f; } -span.event-label-draft, span.event-details-join-expired { +span.event-label-draft, span.event-details-join-expired, span.active-events-before-translation-table { font-weight: 500; - color:#c05621; - border: 1px solid #c05621; + color: var( --gp-color-bubble-inactive-project-text ); + border: 1px solid var( --gp-color-bubble-inactive-project-text ); font-size: .7em; margin-right: 0.3em; width: 6em; @@ -150,6 +150,17 @@ span.event-label-draft, span.event-details-join-expired { border-radius: 1em; text-transform: capitalize; } +span.active-events-before-translation-table a { + color: var( --gp-color-bubble-inactive-project-text ); + text-decoration: none; +} +.active-events-before-translation-table { + width: 100%; + border: 1px solid var( --gp-color-border-default ); + background: var( --gp-color-status-waiting-subtle ); + margin: 1rem 0; + padding: 0.3rem 0.8rem; +} .event-page-wrapper { margin: 0 auto; width: 80%; diff --git a/wporg-gp-translation-events.php b/wporg-gp-translation-events.php index a39e3859..8f1cef3c 100644 --- a/wporg-gp-translation-events.php +++ b/wporg-gp-translation-events.php @@ -23,6 +23,7 @@ use Exception; use GP; use WP_Post; +use WP_Query; /** * Check if a slug is being used by another post type. @@ -404,3 +405,87 @@ function () { $wporg_gp_translation_events_listener->start(); } ); + +/** + * Add the active events for the current user before the translation table. + * + * @return void + */ +function add_active_events_current_user(): void { + $user_attending_events = get_user_meta( get_current_user_id(), Route::USER_META_KEY_ATTENDING, true ) ?: array(); + $current_datetime_utc = ( new DateTime( 'now', new DateTimeZone( 'UTC' ) ) )->format( 'Y-m-d H:i:s' ); + $user_attending_events_args = array( + 'post_type' => 'event', + 'post__in' => array_keys( $user_attending_events ), + 'post_status' => 'publish', + // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query + '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', + ), + ), + // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key + 'meta_key' => '_event_start', + 'orderby' => 'meta_value', + 'order' => 'ASC', + ); + $user_attending_events_query = new WP_Query( $user_attending_events_args ); + $number_of_events = $user_attending_events_query->post_count; + if ( 0 === $number_of_events ) { + return; + } + + $content = '
'; + /* translators: %d: Number of events */ + $content .= sprintf( _n( 'Contributing to %d event:', 'Contributing to %d events:', $number_of_events, 'gp-translation-events' ), $number_of_events ); + $content .= '  '; + if ( $number_of_events > 3 ) { + $counter = 0; + while ( $user_attending_events_query->have_posts() && $counter < 2 ) { + $user_attending_events_query->the_post(); + $url = esc_url( gp_url( '/events/' . get_post_field( 'post_name', get_post() ) ) ); + $content .= '' . get_the_title() . ''; + ++$counter; + } + + $remaining_events = $number_of_events - 2; + $url = esc_url( gp_url( '/events/' ) ); + /* translators: %d: Number of remaining events */ + $content .= '' . sprintf( esc_html__( ' and %d more events.', 'gp-translation-events' ), $remaining_events ) . ''; + + } else { + while ( $user_attending_events_query->have_posts() ) { + $user_attending_events_query->the_post(); + $url = esc_url( gp_url( '/events/' . get_post_field( 'post_name', get_post() ) ) ); + $content .= '' . get_the_title() . ''; + } + } + $content .= '
'; + + echo wp_kses( + $content, + array( + 'div' => array( + 'id' => array(), + 'class' => array(), + ), + 'span' => array( + 'class' => array(), + ), + 'a' => array( + 'href' => array(), + 'target' => array(), + ), + ) + ); +} +add_action( 'gp_before_translation_table', 'Wporg\TranslationEvents\add_active_events_current_user' );