Making WordPress.org

Changeset 12751


Ignore:
Timestamp:
07/21/2023 09:49:35 AM (3 years ago)
Author:
spiraltee
Message:

Translate: Refactor code to log suggestion source for translations

Location:
sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions
Files:
4 edited

Legend:

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

    r12528 r12751  
    55use GP;
    66use GP_Locales;
     7use WordPressdotorg\GlotPress\TranslationSuggestions\Routes\Translation_Memory;
    78
    89class Plugin {
    … …  
    5657
    5758                add_action( self::TM_UPDATE_EVENT, array( Translation_Memory_Client::class, 'update' ) );
     59                add_action( 'gp_translation_created', array( Translation_Memory::class, 'update_external_translations' ) );
    5860        }
    5961
    … …  
    101103                GP::$router->prepend( "/$set/-get-tm-openai-suggestions", array( __NAMESPACE__ . '\Routes\Translation_Memory', 'get_openai_suggestions' ) );
    102104                GP::$router->prepend( "/$set/-get-tm-deepl-suggestions", array( __NAMESPACE__ . '\Routes\Translation_Memory', 'get_deepl_suggestions' ) );
    103                 GP::$router->prepend( '/-save-external-suggestions', array( __NAMESPACE__ . '\Routes\Translation_Memory', 'update_external_translations' ), 'post' );
    104105        }
    105106
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/inc/routes/class-translation-memory.php

    r12579 r12751  
    483483
    484484        /**
    485          * Updates the external translations used by each user.
     485         * Update the number of external translations used.
    486486         *
    487487         * @return void
    488488         */
    489         public function update_external_translations() {
    490                 if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'wporg-editor-settings' ) ) {
    491                         wp_send_json_error( array( 'message' => esc_html__( 'Invalid nonce.', 'glotpress' ) ), 403 );
    492                 }
    493                 if ( ! isset( $_POST['translation'] ) ) {
    494                         wp_send_json_error( array( 'message' => esc_html__( 'Translation parameter is not present.', 'glotpress' ) ), 400 );
    495                 }
    496                 if ( ! isset( $_POST['openAITranslationsUsed'] ) && ! isset( $_POST['deeplTranslationsUsed'] ) ) {
    497                         wp_send_json_error( array( 'message' => esc_html__( 'Translation suggested parameter is not present.', 'glotpress' ) ), 400 );
    498                 }
    499                 if ( isset( $_POST['openAITranslationsUsed'] ) ) {
    500                         $this->update_one_external_translation(
    501                                 $_POST['translation'],
    502                                 $_POST['openAITranslationsUsed'],
    503                                 'openai_translations_used',
    504                                 'openai_same_translations_used'
    505                         );
    506                 }
    507                 if ( isset( $_POST['deeplTranslationsUsed'] ) ) {
    508                         $this->update_one_external_translation(
    509                                 $_POST['translation'],
    510                                 $_POST['deeplTranslationsUsed'],
    511                                 'deepl_translations_used',
    512                                 'deepl_same_translations_used'
    513                         );
    514                 }
    515                 wp_send_json_success();
     489        public function update_external_translations( $translation ) {
     490                $is_source_set    = isset( $_POST['externalTranslationSource'] ) && isset( $_POST['externalTranslationUsed'] );
     491                $is_request_valid = 'GP_Route_Translation' === GP::$current_route->class_name && 'translations_post' === GP::$current_route->last_method_called;
     492                if ( ! $is_request_valid || ! $is_source_set || ! $translation ) {
     493                        return;
     494                }
     495                self::update_one_external_translation(
     496                        $translation->translation_0,
     497                        sanitize_text_field( $_POST['externalTranslationSource'] ),
     498                        sanitize_text_field( $_POST['externalTranslationUsed'] ),
     499                );
    516500        }
    517501
    … …  
    520504         *
    521505         * @param string $translation                     The translation.
     506         * @param string $suggestion_source               The suggestion_source.
    522507         * @param string $suggestion                      The suggestion.
    523          * @param string $external_translations_used      The external translations used.
    524          * @param string $external_same_translations_used The external same translations used.
    525508         *
    526509         * @return void
    527510         */
    528         private function update_one_external_translation( string $translation, string $suggestion, string $external_translations_used, string $external_same_translations_used ) {
     511        private static function update_one_external_translation( string $translation, string $suggestion_source, string $suggestion ) {
     512                $external_translations_used      = $suggestion_source . '_translations_used';
     513                $external_same_translations_used = $suggestion_source . '_same_translations_used';
     514
    529515                $is_the_same_translation  = $translation == $suggestion;
    530516                $gp_external_translations = get_user_option( 'gp_external_translations' );
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/js/translation-suggestions.js

    r12742 r12751  
    2525        var DeeplTMSuggestionRequested = [];
    2626
    27         /**
    28         * Stores the OpenAI translations used.
    29         * @type {array}
    30         */
    31    var $openAITranslationsUsed = [];
    32 
    3327   /**
    34         * Stores the DeepL translations used.
    35         * @type {array}
    36         * */
    37    var $deeplTranslationsUsed = [];
     28        * Stores the external translations used.
     29        * @type {object}
     30    */
     31   var externalSuggestion = {};
    3832
    3933        /**
    … …  
    467461                        $( $gp.editor.table )
    468462                                .on( 'click', '.translation-suggestion', copySuggestion )
    469                                 .on( 'click', 'button.translation-actions__save', saveExternalSuggestions )
    470                                 .on( 'click', '.translation-suggestion.with-tooltip.openai', addSuggestion )
    471                                 .on( 'click', '.translation-suggestion.with-tooltip.deepl', addSuggestion );
     463                                .on( 'click', '.translation-suggestion', addSuggestion );
    472464                        $( document ).ready( function() {
    473465                                getSuggestionsForTheFirstRow();
    … …  
    487479                        return;
    488480                }
    489                 var $originalId = $row.closest( 'tr' ).attr( 'id' ).substring( 7 );
    490                 var $CSSclass = $row.attr( 'class' );
    491                 if ( $CSSclass.indexOf( 'openai' ) > -1 ) {
    492                         $openAITranslationsUsed[ $originalId ] = $row.find( '.translation-suggestion__translation' ).text();
    493                         delete $deeplTranslationsUsed[ $originalId ];
    494                 } else if ( $CSSclass.indexOf( 'deepl' ) > -1 ) {
    495                         $deeplTranslationsUsed[ $originalId ] = $row.find( '.translation-suggestion__translation' ).text();
    496                         delete $openAITranslationsUsed[ $originalId ];
    497                 }
    498         }
    499 
    500         /**
    501          * Saves the number of external suggestions used and used without modification.
    502          *
    503          * @return {void}
    504          **/
    505         function saveExternalSuggestions() {
    506 
    507                 var $button = $( this );
    508                 var $row = $button.closest( 'tr.editor' );
    509                 var $originalId = $row.attr( 'id' ).substring( 7 );
    510                 if ( ! $openAITranslationsUsed[$originalId] && ! $deeplTranslationsUsed[$originalId] ) {
    511                         return;
    512                 }
    513 
    514                 var $translation = $row.find( 'textarea' ).val();
    515                 var $data = {
    516                         nonce: wporgEditorSettings.nonce,
    517                         translation: $translation,
    518                         openAITranslationsUsed: $openAITranslationsUsed[$originalId],
    519                         deeplTranslationsUsed: $deeplTranslationsUsed[$originalId]
    520                 };
    521 
    522                 $.ajax({
    523                         url: '/-save-external-suggestions',
    524                         type: 'POST',
    525                         data: $data,
    526                 });
    527         }
    528 
     481                externalSuggestion.suggestion_source = $row.data( 'suggestion-source' ) == 'translation' ? 'tm' : $row.data( 'suggestion-source' );
     482                externalSuggestion.translation = $row.find( '.translation-suggestion__translation' ).text();
     483
     484        }
     485
     486        //Prefilter ajax requests to add external translations used to the request.
     487        $.ajaxPrefilter( function ( options ) {
     488                const isSuggestionUsed = Object.keys( externalSuggestion ).length  > 0 ? true : false;
     489
     490                if ( ! externalSuggestion || ! isSuggestionUsed ) {
     491                        return;
     492                }
     493                if ( 'POST' === options.type && $gp_editor_options.url === options.url ) {
     494                                options.data += '&externalTranslationSource=' + externalSuggestion.suggestion_source;
     495                                options.data += '&externalTranslationUsed=' + externalSuggestion.translation;
     496                                externalSuggestion = {};
     497                }
     498        });
    529499})( jQuery );
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/templates/translation-memory-suggestions.php

    r12527 r12751  
    66        foreach ( $suggestions as $suggestion ) {
    77                echo '<li>';
    8                 echo '<div class="translation-suggestion with-tooltip ' . esc_html( strtolower( $type ) ) . '" tabindex="0" role="button" aria-pressed="false" aria-label="Copy translation">';
     8                echo '<div class="translation-suggestion with-tooltip ' . esc_html( strtolower( $type ) ) . '" tabindex="0" data-suggestion-source="' . esc_html( strtolower( $type ) ) . '" role="button" aria-pressed="false" aria-label="Copy translation">';
    99                        echo '<span class="' . esc_html( strtolower( $type ) ) . '-suggestion__score">';
    1010                if ( 'Translation' == $type ) {
Note: See TracChangeset for help on using the changeset viewer.