Changeset 12751
- Timestamp:
- 07/21/2023 09:49:35 AM (3 years ago)
- Location:
- sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions
- Files:
-
- 4 edited
-
inc/class-plugin.php (modified) (3 diffs)
-
inc/routes/class-translation-memory.php (modified) (2 diffs)
-
js/translation-suggestions.js (modified) (3 diffs)
-
templates/translation-memory-suggestions.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/inc/class-plugin.php
r12528 r12751 5 5 use GP; 6 6 use GP_Locales; 7 use WordPressdotorg\GlotPress\TranslationSuggestions\Routes\Translation_Memory; 7 8 8 9 class Plugin { … … 56 57 57 58 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' ) ); 58 60 } 59 61 … … 101 103 GP::$router->prepend( "/$set/-get-tm-openai-suggestions", array( __NAMESPACE__ . '\Routes\Translation_Memory', 'get_openai_suggestions' ) ); 102 104 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' );104 105 } 105 106 -
sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/inc/routes/class-translation-memory.php
r12579 r12751 483 483 484 484 /** 485 * Update s the external translations used by each user.485 * Update the number of external translations used. 486 486 * 487 487 * @return void 488 488 */ 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 ); 516 500 } 517 501 … … 520 504 * 521 505 * @param string $translation The translation. 506 * @param string $suggestion_source The suggestion_source. 522 507 * @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.525 508 * 526 509 * @return void 527 510 */ 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 529 515 $is_the_same_translation = $translation == $suggestion; 530 516 $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 25 25 var DeeplTMSuggestionRequested = []; 26 26 27 /**28 * Stores the OpenAI translations used.29 * @type {array}30 */31 var $openAITranslationsUsed = [];32 33 27 /** 34 * Stores the DeepLtranslations used.35 * @type { array}36 **/37 var $deeplTranslationsUsed = [];28 * Stores the external translations used. 29 * @type {object} 30 */ 31 var externalSuggestion = {}; 38 32 39 33 /** … … 467 461 $( $gp.editor.table ) 468 462 .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 ); 472 464 $( document ).ready( function() { 473 465 getSuggestionsForTheFirstRow(); … … 487 479 return; 488 480 } 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 }); 529 499 })( jQuery ); -
sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/templates/translation-memory-suggestions.php
r12527 r12751 6 6 foreach ( $suggestions as $suggestion ) { 7 7 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">'; 9 9 echo '<span class="' . esc_html( strtolower( $type ) ) . '-suggestion__score">'; 10 10 if ( 'Translation' == $type ) {
Note: See TracChangeset
for help on using the changeset viewer.