Making WordPress.org

Changeset 6856


Ignore:
Timestamp:
03/10/2018 04:36:32 PM (7 years ago)
Author:
ocean90
Message:

Translate: Add cron schedule to update profile badges for translation contributors.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-customizations/inc/class-plugin.php

    r6741 r6856  
    4747        add_filter( 'gp_translation_prepare_for_save', array( $this, 'apply_capital_P_dangit' ), 10, 2 );
    4848
     49        // Cron.
     50        add_filter( 'cron_schedules', [ $this, 'register_cron_schedules' ] );
     51        add_action( 'init', [ $this, 'register_cron_events' ] );
     52        add_action( 'wporg_translate_update_contributor_profile_badges', [ $this, 'update_contributor_profile_badges' ] );
     53
    4954        // Toolbar.
    5055        add_action( 'admin_bar_menu', array( $this, 'add_profile_settings_to_admin_bar' ) );
     
    6166            $this->register_cli_commands();
    6267        }
     68    }
     69
     70    /**
     71     * Filters the non-default cron schedules.
     72     *
     73     * @param array $schedules An array of non-default cron schedules.
     74     * @return array  An array of non-default cron schedules.
     75     */
     76    public function register_cron_schedules( $schedules ) {
     77        $schedules['15_minutes'] = array(
     78            'interval' => 15 * MINUTE_IN_SECONDS,
     79            'display'  => 'Every 15 minutes',
     80        );
     81
     82        return $schedules;
     83    }
     84
     85    /**
     86     * Registers cron events.
     87     */
     88    public function register_cron_events() {
     89        if ( ! wp_next_scheduled( 'wporg_translate_update_contributor_profile_badges' ) ) {
     90            wp_schedule_event( time(), '15_minutes', 'wporg_translate_update_contributor_profile_badges' );
     91        }
     92    }
     93
     94    /**
     95     * Calls the profile.w.org activity handler to assign contributors to the
     96     * Translation Contributor group.
     97     */
     98    public function update_contributor_profile_badges() {
     99        global $wpdb;
     100
     101        if ( ! isset( $wpdb->user_translations_count ) ) {
     102            return;
     103        }
     104
     105        $now       = time();
     106        $last_sync = get_option( 'wporg_translate_last_badges_sync' );
     107        if ( ! $last_sync ) {
     108            return;
     109        }
     110
     111        $user_ids = $wpdb->get_col( $wpdb->prepare( "
     112            SELECT user_id
     113            FROM {$wpdb->user_translations_count}
     114            WHERE accepted > 0 AND date_modified > %s
     115            GROUP BY user_id
     116        ", gmdate( 'Y-m-d H:i:s', $last_sync ) ) );
     117
     118        if ( ! $user_ids ) {
     119            update_option( 'wporg_translate_last_badges_sync', $now );
     120            return;
     121        }
     122
     123        $request_body = [
     124            'action'      => 'wporg_handle_association',
     125            'source'      => 'polyglots',
     126            'command'     => 'add',
     127            'association' => 'translation-contributor',
     128        ];
     129
     130        foreach( $user_ids as $user_id ) {
     131            $request_body['user_id'] = $user_id;
     132
     133            wp_remote_post( 'https://profiles.wordpress.org/wp-admin/admin-ajax.php', [
     134                'body'       => $request_body,
     135                'user-agent' => 'WordPress.org Translate',
     136            ] );
     137        }
     138
     139        update_option( 'wporg_translate_last_badges_sync', $now );
    63140    }
    64141
Note: See TracChangeset for help on using the changeset viewer.