Making WordPress.org

Changeset 11937


Ignore:
Timestamp:
07/05/2022 04:59:28 PM (23 months ago)
Author:
iandunn
Message:

GP Profiles: Process bulk translation activity.

Only approvals are done at the moment, but reject/fuzzy will be added when we start to credit editor review actions.

See https://github.com/WordPress/five-for-the-future/issues/196

Location:
sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-profiles
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-profiles/profiles.php

    r11935 r11937  
    1010
    1111use WordPressdotorg\Profiles as Profiles_API;
    12 use GP, GP_Translation;
     12use GP, GP_Translation, GP_Project, GP_Locale, GP_Translation_Set;
    1313
    1414defined( 'WPINC' ) || die();
     
    2323}
    2424
    25 add_action( 'gp_translation_created', __NAMESPACE__ . '\add_translation_activity' );
    26 add_action( 'gp_translation_saved', __NAMESPACE__ . '\add_translation_activity', 10, 2 );
     25add_action( 'gp_translation_created', __NAMESPACE__ . '\add_single_translation_activity' );
     26add_action( 'gp_translation_saved', __NAMESPACE__ . '\add_single_translation_activity', 10, 2 );
     27add_action( 'gp_translation_set_bulk_action_post', __NAMESPACE__ . '\add_bulk_translation_activity', 10, 4 );
    2728
    2829/**
    2930 * Add a activity when strings are suggested and approved.
    3031 */
    31 function add_translation_activity( GP_Translation $new_translation, GP_Translation $previous_translation = null ) : void {
     32function add_single_translation_activity( GP_Translation $new_translation, GP_Translation $previous_translation = null ) : void {
     33    $bulk_request = gp_post( 'bulk', null );
     34
     35    // Bulk actions are handled by `add_bulk_translation_activity()`.
     36    if ( $bulk_request ) {
     37        return;
     38    }
     39
    3240    $current_user_is_editor = GP::$permission->current_user_can(
    3341        'approve',
     
    5866    Profiles_API\api( $request_body );
    5967}
     68
     69/**
     70 * Add activity when bulk actions are performed.
     71 */
     72function add_bulk_translation_activity( GP_Project $project, GP_Locale $locale, GP_Translation_Set $translation_set, array $bulk ) : void {
     73    switch ( $bulk['action'] ) {
     74        case 'approve':
     75            $type = 'glotpress_translation_approved';
     76            break;
     77
     78        default:
     79            return;
     80    }
     81
     82    $translation_ids = array();
     83    foreach ( $bulk['row-ids'] as $row_id ) {
     84        $parts             = explode( '-', $row_id );
     85        $translation_ids[] = intval( $parts[1] ?? 0 );
     86    }
     87
     88    $translations = GP::$translation->find_many(
     89        sprintf( 'id IN ( %s )', implode( ',', $translation_ids ) )
     90    );
     91
     92    $user_type_counts = array();
     93    foreach ( $translations as $translation ) {
     94        // `GP_Route_Translation::_bulk_approve()` tracks which `set_status()` calls succeed, but that information
     95        // isn't passed to this callback. Check this just in case any of them failed.
     96        if ( 'current' !== $translation->status ) {
     97            continue;
     98        }
     99
     100        $user_id = $translation->user_id;
     101
     102        if ( isset( $user_type_counts[ $user_id ][ $type ] ) ) {
     103            $user_type_counts[ $user_id ][ $type ]++;
     104        } else {
     105            $user_type_counts[ $user_id ][ $type ] = 1;
     106        }
     107    }
     108
     109    $activities = array();
     110    foreach ( $user_type_counts as $user_id => $types ) {
     111        foreach ( $types as $type => $count ) {
     112            $activities[] = array(
     113                'component' => 'glotpress',
     114                'type'      => $type,
     115                'user_id'   => $user_id,
     116                'bump'      => $count,
     117            );
     118        }
     119    }
     120
     121    $request_body = array(
     122        'action'     => 'wporg_handle_activity',
     123        'component'  => 'glotpress',
     124        'activities' => $activities,
     125    );
     126
     127    Profiles_API\api( $request_body );
     128}
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-profiles/tests/e2e.php

    r11929 r11937  
    4545        'status'  => 'waiting',
    4646    ) );
    47     GlotPress_Profiles\add_translation_activity( $translation );
     47    GlotPress_Profiles\add_single_translation_activity( $translation );
    4848
    4949    $translation = new GP_Translation( array(
     
    5151        'status'  => 'current',
    5252    ) );
    53     GlotPress_Profiles\add_translation_activity( $translation );
     53    GlotPress_Profiles\add_single_translation_activity( $translation );
    5454
    5555    echo "\nThe daily digest count should have been bumped on https://profiles.wordpress.org/$user->user_nicename/ \n";
Note: See TracChangeset for help on using the changeset viewer.