Making WordPress.org


Ignore:
Timestamp:
04/16/2024 12:41:37 PM (19 months ago)
Author:
amieiro
Message:

Translate: Sync "Translation Events" from GitHub

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-events/includes/routes/user/attend-event.php

    r13529 r13541  
    77use Wporg\TranslationEvents\Event\Event_Repository_Interface;
    88use Wporg\TranslationEvents\Routes\Route;
     9use Wporg\TranslationEvents\Stats\Stats_Importer;
    910use Wporg\TranslationEvents\Translation_Events;
    1011
     
    1314 * If the user is not currently marked as attending, they will be marked as attending.
    1415 * If the user is currently marked as attending, they will be marked as not attending.
     16 *
     17 * If the user is marked as attending, and the event is active at that moment, stats for the translations the user
     18 * created since the event started are imported.
    1519 */
    1620class Attend_Event_Route extends Route {
    1721    private Event_Repository_Interface $event_repository;
    1822    private Attendee_Repository $attendee_repository;
     23    private Stats_Importer $stats_importer;
    1924
    2025    public function __construct() {
     
    2227        $this->event_repository    = Translation_Events::get_event_repository();
    2328        $this->attendee_repository = Translation_Events::get_attendee_repository();
     29        $this->stats_importer      = new Stats_Importer();
    2430    }
    2531
     
    2935            $this->die_with_error( esc_html__( 'Only logged-in users can attend events', 'gp-translation-events' ), 403 );
    3036        }
     37        $user_id = $user->ID;
    3138
    3239        $event = $this->event_repository->get_event( $event_id );
     
    3542        }
    3643
    37         $attendee = $this->attendee_repository->get_attendee( $event->id(), $user->ID );
    38         if ( $attendee instanceof Attendee && $attendee->is_host() && ( 1 === count( $this->attendee_repository->get_hosts( $event_id ) ) ) ) {
    39             $this->die_with_error( esc_html__( 'The event needs a host. Add a new host before stopping to attend the event.', 'gp-translation-events' ), 403 );
     44        if ( $event->is_past() ) {
     45            $this->die_with_error( esc_html__( 'Cannot attend or un-attend a past event', 'gp-translation-events' ), 403 );
    4046        }
     47
     48        $attendee = $this->attendee_repository->get_attendee( $event->id(), $user_id );
    4149        if ( $attendee instanceof Attendee ) {
    42             $this->attendee_repository->remove_attendee( $event->id(), $user->ID );
     50            $this->attendee_repository->remove_attendee( $event->id(), $user_id );
    4351        } else {
    44             $attendee = new Attendee( $event->id(), $user->ID );
     52            $attendee = new Attendee( $event->id(), $user_id );
    4553            $this->attendee_repository->insert_attendee( $attendee );
     54
     55            // If the event is active right now,
     56            // import stats for translations the user created since the event started.
     57            if ( $event->is_active() ) {
     58                $this->stats_importer->import_for_user_and_event( $user_id, $event );
     59            }
    4660        }
    4761
Note: See TracChangeset for help on using the changeset viewer.