Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show a bar with the events to which the user contributes #114

Merged
merged 4 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions assets/css/translation-events.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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%;
Expand Down
85 changes: 85 additions & 0 deletions wporg-gp-translation-events.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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();
amieiro marked this conversation as resolved.
Show resolved Hide resolved
$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 = '<div id="active-events-before-translation-table" class="active-events-before-translation-table">';
/* 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 .= '&nbsp;&nbsp;';
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 .= '<span class="active-events-before-translation-table"><a href="' . $url . '" target="_blank">' . get_the_title() . '</a></span>';
++$counter;
}

$remaining_events = $number_of_events - 2;
$url = esc_url( gp_url( '/events/' ) );
/* translators: %d: Number of remaining events */
$content .= '<span class="remaining-events"><a href="' . $url . '" target="_blank">' . sprintf( esc_html__( ' and %d more events.', 'gp-translation-events' ), $remaining_events ) . '</a></span>';

} 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 .= '<span class="active-events-before-translation-table"><a href="' . $url . '" target="_blank">' . get_the_title() . '</a></span>';
}
}
$content .= '</div>';

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' );
Loading