Making WordPress.org


Ignore:
Timestamp:
01/13/2021 08:24:28 AM (6 years ago)
Author:
dd32
Message:

Translate: Prevent suggesting a string which has already been rejected for that original for that user.

This prevents a translator suggesting the same sting for an original after having their translation rejected once.

See https://wordpress.slack.com/archives/C02RP50LK/p1610520419455100

File:
1 edited

Legend:

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

    r10377 r10575  
    33namespace WordPressdotorg\GlotPress\Customizations;
    44
     5use GP;
     6use GP_Locales;
    57use GP_Translation;
    68use WP_CLI;
     
    4951                add_filter( 'wporg_translate_language_pack_theme_args', array( $this, 'set_version_for_default_themes_in_development' ), 10, 2 );
    5052
     53                add_filter( 'gp_translation_prepare_for_save', array( $this, 'auto_reject_already_rejected' ), 100, 2 );
     54
    5155                // Cron.
    5256                add_filter( 'cron_schedules', [ $this, 'register_cron_schedules' ] );
     
    185189                                $args[ "translation_$i" ] = capital_P_dangit( $args[ "translation_$i" ] );
    186190                        }
     191                }
     192
     193                return $args;
     194        }
     195
     196        /**
     197         * Rejects translation submissions where the suggested string has already been rejected for the user.
     198         *
     199         * @param array          $args        Translation arguments.
     200         * @param GP_Translation $translation Translation instance.
     201         * @return array Translation arguments.
     202         */
     203        public function auto_reject_already_rejected( $args, GP_Translation $translation ) {
     204                if (
     205                        ! $translation->id &&
     206                        'waiting' === $args['status'] &&
     207                        GP::$current_route->class_name === 'GP_Route_Translation' &&
     208                        GP::$current_route->last_method_called === 'translations_post'
     209                ) {
     210                        // New translation being added.
     211
     212                        $translation_set = GP::$translation_set->get( $args['translation_set_id'] );
     213                        $project = GP::$project->get( $translation_set->project_id );
     214
     215                        // If the current user can approve / write to the project, skip.
     216                        if (
     217                                GP::$permission->current_user_can( 'approve', 'translation-set', $translation_set->id )
     218                                ||
     219                                GP::$permission->current_user_can( 'write', 'project', $project->id )
     220                        ) {
     221                                return $args;
     222                        }
     223
     224                        $existing_rejected_translations = GP::$translation->for_translation(
     225                                $project,
     226                                $translation_set,
     227                                'no-limit',
     228                                array(
     229                                        'user_login'  => get_user_by( 'ID', $args['user_id'] )->user_login,
     230                                        'original_id' => $args['original_id'],
     231                                        'status'      => 'rejected',
     232                                ),
     233                                array()
     234                        );
     235
     236                        if ( $existing_rejected_translations ) {
     237                                $locale = GP_Locales::by_slug( $translation_set->locale );
     238
     239                                $translations = [];
     240                                foreach ( range( 0, GP::$translation->get_static( 'number_of_plural_translations' ) - 1 ) as $i ) {
     241                                        if ( isset( $args[ "translation_$i" ] ) ) {
     242                                                $translations[] = $args[ "translation_$i" ];
     243                                        }
     244                                }
     245
     246                                foreach ( $existing_rejected_translations as $e ) {
     247                                        if ( array_pad( $translations, $locale->nplurals, null ) == $e->translations ) {
     248                                                GP::$current_route->die_with_error( __( 'Identical rejected translation already exists.', 'wporg' ), 200 );
     249                                        }
     250                                }
     251                        }
     252
    187253                }
    188254
Note: See TracChangeset for help on using the changeset viewer.