Making WordPress.org

Changeset 11041


Ignore:
Timestamp:
06/20/2021 04:35:22 PM (4 years ago)
Author:
ocean90
Message:

Translate: When syncing plugin translations ensure the current status is also copied.

See #3828.

File:
1 edited

Legend:

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

    r8937 r11041  
    5353        $set_slug = isset( $assoc_args['set'] ) ? $assoc_args['set'] : 'default';
    5454
     55
    5556        $translation_set_src = GP::$translation_set->by_project_id_slug_and_locale( $project_src->id, $set_slug, $locale->slug );
    5657        if ( ! $translation_set_src ) {
     
    6061        $translation_set_dest = GP::$translation_set->by_project_id_slug_and_locale( $project_dest->id, $set_slug, $locale->slug );
    6162        if ( ! $translation_set_dest ) {
    62             WP_CLI::error( 'Source translation set not found!' );
     63            WP_CLI::error( 'Destination translation set not found!' );
    6364        }
    6465
     
    6970        }
    7071
    71         $current_translations = new Translations();
    72         $user_id_map = [];
     72        $current_translations     = new Translations();
     73        $current_translations_map = [];
    7374        foreach ( $current_translations_list as $translation ) {
    7475            $current_translations->add_entry( $translation );
    75             $user_id_map[ $translation->key() ] = $translation->user_id;
     76            $current_translations_map[ $translation->key() ] = [
     77                'user_id'  => $translation->user_id,
     78                'status'   => $translation->translation_status,
     79                'warnings' => $translation->warnings,
     80            ];
     81
    7682        }
    7783        unset( $current_translations_list );
    7884
    79         add_action( 'gp_translation_created', function( $translation ) use ( $user_id_map ) {
    80             $original = GP::$original->get( $translation->original_id );
     85        add_filter( 'gp_translation_prepare_for_save', function( $args, $translation ) use ( $current_translations_map ) {
     86            $original = GP::$original->get( $args['original_id'] );
    8187            if ( ! $original ) {
    82                 return;
     88                return $args;
    8389            }
    8490
     
    8995            ] );
    9096            $key = $translation_entry->key();
    91             if ( isset( $user_id_map[ $key ] ) && $user_id_map[ $key ] !== $translation->user_id ) {
    92                 $translation->update( [ 'user_id' => $user_id_map[ $key ] ] );
     97
     98            if ( ! isset( $current_translations_map[ $key ] ) ) {
     99                return $args;
    93100            }
    94         } );
     101
     102            $current_translation = $current_translations_map[ $key ];
     103
     104            $args['user_id']  = $current_translation['user_id'];
     105            $args['status']   = $current_translation['status'];
     106            $args['warnings'] = $current_translation['warnings'];
     107
     108            return $args;
     109        }, 50, 2 );
     110
     111        // GP_Translation_Set::import() calls `$translation->set_status()` which may reset the status set above.
     112        add_filter( 'gp_translation_set_import_status', function( $status, $entry ) use ( $current_translations_map ) {
     113            if ( ! isset( $current_translations_map[ $entry->key() ] ) ) {
     114                return $status;
     115            }
     116
     117            $current_translation = $current_translations_map[ $entry->key() ];
     118
     119            return $current_translation['status'];
     120        }, 50, 2 );
    95121
    96122        $synced = $translation_set_dest->import( $current_translations );
Note: See TracChangeset for help on using the changeset viewer.