Making WordPress.org


Ignore:
Timestamp:
05/21/2024 11:43:05 AM (20 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/upgrade.php

    r13683 r13739  
    33namespace Wporg\TranslationEvents;
    44
     5use Exception;
     6use WP_Query;
     7
    58class Upgrade {
    6     private const VERSION        = 2;
     9    private const VERSION        = 3;
    710    private const VERSION_OPTION = 'wporg_gp_translations_events_version';
    811
     
    2326        require_once ABSPATH . 'wp-admin/includes/upgrade.php';
    2427        dbDelta( self::get_database_schema_sql() );
     28
     29        // Run version-specific upgrades.
     30        $is_running_tests = 'yes' === getenv( 'WPORG_TRANSLATION_EVENTS_TESTS' );
     31        if ( $previous_version < 3 && ! $is_running_tests ) {
     32            try {
     33                self::v3_set_is_new_contributor();
     34            } catch ( Exception $e ) {
     35                // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
     36                error_log( $e );
     37            }
     38        }
    2539
    2640        update_option( self::VERSION_OPTION, self::VERSION );
     
    4862                `user_id` int(10) NOT NULL COMMENT 'ID of the user who is attending the event',
    4963                `is_host` tinyint(1) default 0 not null comment 'Whether the user is a host of the event',
     64                `is_new_contributor` tinyint(1) default 0 not null comment 'Whether the user is a new translation contributor',
    5065            PRIMARY KEY (`translate_event_attendees_id`),
    5166            UNIQUE KEY `event_per_user` (`event_id`,`user_id`),
     
    5469        ";
    5570    }
     71
     72    /**
     73     * Set is_new_contributor in attendees table for all events.
     74     */
     75    private static function v3_set_is_new_contributor(): void {
     76        global $wpdb, $gp_table_prefix;
     77
     78        $query = new WP_Query(
     79            array(
     80                'post_type'   => Translation_Events::CPT,
     81                'post_status' => 'publish',
     82            )
     83        );
     84
     85        $events              = $query->get_posts();
     86        $event_repository    = Translation_Events::get_event_repository();
     87        $attendee_repository = Translation_Events::get_attendee_repository();
     88
     89        foreach ( $events as $post ) {
     90            $event = $event_repository->get_event( $post->ID );
     91            if ( ! $event ) {
     92                continue;
     93            }
     94
     95            $attendees = $attendee_repository->get_attendees( $event->id() );
     96
     97            foreach ( $attendees as $attendee ) {
     98                // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
     99                // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery
     100                // phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching
     101                $translation_count = $wpdb->get_var(
     102                    $wpdb->prepare(
     103                        "
     104                            select count(*) as cnt
     105                            from {$gp_table_prefix}translations
     106                            where user_id = %d
     107                              and date_added < %s
     108                        ",
     109                        array(
     110                            $attendee->user_id(),
     111                            $event->start()->format( 'Y-m-d H:i:s' ),
     112                        ),
     113                    )
     114                );
     115
     116                if ( $translation_count > 10 ) {
     117                    // Not a new contributor.
     118                    continue;
     119                }
     120
     121                $wpdb->update(
     122                    "{$gp_table_prefix}event_attendees",
     123                    array( 'is_new_contributor' => 1 ),
     124                    array(
     125                        'event_id' => $attendee->event_id(),
     126                        'user_id'  => $attendee->user_id(),
     127                    )
     128                );
     129                // phpcs:enable
     130            }
     131        }
     132    }
    56133}
Note: See TracChangeset for help on using the changeset viewer.