<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[12741] sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-customizations: Translate: Log translation source</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="http://meta.trac.wordpress.org/changeset/12741">12741</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"http://meta.trac.wordpress.org/changeset/12741","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>spiraltee</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2023-07-14 09:35:22 +0000 (Fri, 14 Jul 2023)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Translate: Log translation source</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggpcustomizationsincclasspluginphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-customizations/inc/class-plugin.php</a></li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggpcustomizationstemplatesjseditorjs">sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-customizations/templates/js/editor.js</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggpcustomizationsincclasspluginphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-customizations/inc/class-plugin.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-customizations/inc/class-plugin.php     2023-07-14 08:04:18 UTC (rev 12740)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-customizations/inc/class-plugin.php       2023-07-14 09:35:22 UTC (rev 12741)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -17,6 +17,16 @@
</span><span class="cx" style="display: block; padding: 0 10px">        private static $instance;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @var array The IDs of the translations that have been imported.
+        */
+       private array $imported_translation_ids = array();
+
+       /**
+        * @var string The source of translations that have been imported.
+        */
+       private string $imported_source;
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Returns always the same instance of this plugin.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @return Plugin
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -65,6 +75,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">                add_action( 'init', [ $this, 'register_cron_events' ] );
</span><span class="cx" style="display: block; padding: 0 10px">                add_action( 'wporg_translate_update_contributor_profile_badges', [ $this, 'update_contributor_profile_badges' ] );
</span><span class="cx" style="display: block; padding: 0 10px">                add_action( 'wporg_translate_update_polyglots_stats', [ $this, 'update_polyglots_stats' ] );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                add_action( 'gp_translation_created', array( $this, 'log_translation_source' ) );
+               add_action( 'gp_translation_saved', array( $this, 'log_translation_source' ) );
+               add_action( 'gp_translations_imported', array( $this, 'log_imported_translations' ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Toolbar.
</span><span class="cx" style="display: block; padding: 0 10px">                add_action( 'admin_bar_menu', array( $this, 'add_profile_settings_to_admin_bar' ) );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -275,6 +288,99 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Stores source of a translation in database.
+        *
+        * @param GP_Translation $translation Translation instance.
+        * @return void
+        */
+       public function log_translation_source( GP_Translation $translation ) {
+               static $already_logged = array();
+               $key                   = ! $translation->translation_0 ? null : $translation->translation_0;
+               if ( isset( $already_logged[ $key ] ) ) {
+                       return;
+               }
+               $already_logged[ $key ] = true;
+               $source                 = '';
+               if ( $translation && 'GP_Route_Translation' === GP::$current_route->class_name ) {
+                       if ( 'import_translations_post' === GP::$current_route->last_method_called ) {
+                               $this->imported_translation_ids[] = $translation->id;
+
+                               if ( isset( $_POST['source'] ) && 'translate-live' == $_POST['source'] ) {
+                                       $this->imported_source = 'playground';
+                               } elseif ( ! isset( $_POST['source'] ) && 'Import' == $_POST['submit'] ) {
+                                       $this->imported_source = 'import';
+                               } else {
+                                       return;
+                               }
+                       }
+                       if ( 'translations_post' === GP::$current_route->last_method_called ) {
+                               if ( isset( $_POST['translation_source'] ) && 'frontend' == $_POST['translation_source'] ) {
+                                       $source = 'frontend';
+                                       if ( isset( $_POST['openAITranslationsUsed'] ) ) {
+                                               $suggestion_source     = 'openai';
+                                               $suggested_translation = sanitize_text_field( $_POST['openAITranslationsUsed'] );
+                                               $this->save_translation_suggestion_source( $translation, $suggested_translation, $suggestion_source );
+                                       }
+                                       if ( isset( $_POST['deeplTranslationsUsed'] ) ) {
+                                               $suggestion_source     = 'deepl';
+                                               $suggested_translation = sanitize_text_field( $_POST['deeplTranslationsUsed'] );
+                                               $this->save_translation_suggestion_source( $translation, $suggested_translation, $suggestion_source );
+                                       }
+                               }
+                       }
+               }
+               if ( $source ) {
+                       gp_update_meta( $translation->id, 'source', $source, 'translation' );
+               }
+       }
+
+       /**
+        * Save the source of a translation suggestion.
+        *
+        * @param object $translation Translation object.
+        * @param string $suggested_translation Suggested translation string.
+        * @param string $suggestion_source Suggestion source.
+        *
+        * @return void
+        */
+       private function save_translation_suggestion_source( $translation, $suggested_translation, $suggestion_source ) {
+               $suggestion_used = $suggestion_source;
+               if ( $translation->translation_0 !== $suggested_translation ) {
+                       $suggestion_used .= '_modified';
+               }
+               if ( $suggestion_used ) {
+                       gp_update_meta( $translation->id, 'suggestion-used', $suggestion_used, 'translation' );
+               }
+
+       }
+
+       /**
+        * Logs imported translations.
+        *
+        * @return void
+        */
+       public function log_imported_translations() {
+               global $wpdb;
+               $source = $this->imported_source;
+               if ( ! $source && ! $this->imported_translation_ids ) {
+                       return;
+               }
+               $sql        = 'INSERT INTO ' . $wpdb->gp_meta . ' (object_type, object_id, meta_key, meta_value) VALUES ';
+               $sql_vars   = array();
+               $sql_values = array_map(
+                       function( $translation_id ) use ( $source, &$sql_vars ) {
+                               $sql_vars[] = $translation_id;
+                               $sql_vars[] = $source;
+                               return '( "translation", %d, "source", %s )';
+                       },
+                       $this->imported_translation_ids
+               );
+               $sql       .= implode( ', ', $sql_values );
+               $wpdb->query( $wpdb->prepare( $sql, $sql_vars ) );
+
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Auto-Rejects a translators submissions when the translator submits a replacement suggestion.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @see https://github.com/GlotPress/GlotPress-WP/issues/889
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -646,4 +752,4 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $reasons = isset( $locale_reasons[ $locale ] ) ? $locale_reasons[ $locale ] : array();
</span><span class="cx" style="display: block; padding: 0 10px">                return array_merge( $default_reasons, $reasons );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-}
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+}
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of file
</span></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggpcustomizationstemplatesjseditorjs"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-customizations/templates/js/editor.js</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-customizations/templates/js/editor.js   2023-07-14 08:04:18 UTC (rev 12740)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-customizations/templates/js/editor.js     2023-07-14 09:35:22 UTC (rev 12741)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2,8 +2,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> ( function( $ ){
</span><span class="cx" style="display: block; padding: 0 10px">        var $html = $( 'html' );
</span><span class="cx" style="display: block; padding: 0 10px">        var $document = $( document );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        var $openAITranslationsUsed = [];
-       var $deeplTranslationsUsed = [];
</del><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        function checkStorage() {
</span><span class="cx" style="display: block; padding: 0 10px">                var test = 'test',
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -72,53 +70,16 @@
</span><span class="cx" style="display: block; padding: 0 10px">                autosize( $textarea );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        /**
-        * Adds the suggestion to the translation in an array, and removes the previous suggestions, so
-        * we only store the last one in the database, using the saveExternalSuggestions() function.
-        *
-        * @return {void}
-        */
-       function addSuggestion() {
-               var $row = $( this );
-               if ( ! $row ) {
-                       return;
-               }
-               var $originalId = $row.closest( 'tr' ).attr( 'id' ).substring( 7 );
-               var $CSSclass = $row.attr( 'class' );
-               if ( $CSSclass.indexOf( 'openai' ) > -1 ) {
-                       $openAITranslationsUsed[ $originalId ] = $row.find( '.translation-suggestion__translation' ).text();
-                       delete $deeplTranslationsUsed[ $originalId ];
-               } else if ( $CSSclass.indexOf( 'deepl' ) > -1 ) {
-                       $deeplTranslationsUsed[ $originalId ] = $row.find( '.translation-suggestion__translation' ).text();
-                       delete $openAITranslationsUsed[ $originalId ];
-               }
-       }
</del><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        /**
-        * Saves the number of external suggestions used and used without modification.
-        *
-        * @return {void}
-        **/
-       function saveExternalSuggestions() {
-               var $button = $( this );
-               var $row = $button.closest( 'tr.editor' );
-               var $originalId = $row.attr( 'id' ).substring( 7 );
-               if ( ! $openAITranslationsUsed[$originalId] && ! $deeplTranslationsUsed[$originalId] ) {
-                       return;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ //Prefilter ajax requests to add translation_source to the request.
+       $.ajaxPrefilter( function ( options ) {
+               let data = Object.fromEntries( new URLSearchParams( options.data ) );
+
+               if ( 'POST' === options.type && $gp_editor_options.url === options.url ) {
+                       options.data += '&translation_source=frontend';
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                var $translation = $row.find( 'textarea' ).val();
-               var $data = {
-                       nonce: wporgEditorSettings.nonce,
-                       translation: $translation,
-                       openAITranslationsUsed: $openAITranslationsUsed[$originalId],
-                       deeplTranslationsUsed: $deeplTranslationsUsed[$originalId]
-               };
-               $.ajax({
-                       url: '/-save-external-suggestions',
-                       type: 'POST',
-                       data: $data,
-               });
-       }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ });
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        // Override functions to adopt custom markup.
</span><span class="cx" style="display: block; padding: 0 10px">        $gp.editor.copy = function() {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -372,7 +333,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                .on( 'click', 'button.panel-header-actions__cancel', $gp.editor.hooks.cancel )
</span><span class="cx" style="display: block; padding: 0 10px">                                .on( 'click', 'button.translation-actions__copy', $gp.editor.hooks.copy )
</span><span class="cx" style="display: block; padding: 0 10px">                                .on( 'click', 'button.translation-actions__insert-tab', $gp.editor.hooks.tab )
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                .on( 'click', 'button.translation-actions__save', saveExternalSuggestions )
</del><span class="cx" style="display: block; padding: 0 10px">                                 .on( 'click', 'button.translation-actions__save', $gp.editor.hooks.ok )
</span><span class="cx" style="display: block; padding: 0 10px">                                .on( 'click', 'button.translation-actions__help', openHelpModal )
</span><span class="cx" style="display: block; padding: 0 10px">                                .on( 'click', 'button.translation-actions__ltr', switchTextDirection )
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -380,8 +340,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                .on( 'focus', 'textarea', textareaAutosize )
</span><span class="cx" style="display: block; padding: 0 10px">                                .on( 'click', 'summary', toggleDetails )
</span><span class="cx" style="display: block; padding: 0 10px">                                .on( 'click', 'button.button-menu__toggle', toggleLinkMenu )
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                .on( 'click', '.translation-suggestion.with-tooltip.openai', addSuggestion )
-                               .on( 'click', '.translation-suggestion.with-tooltip.deepl', addSuggestion )
</del><span class="cx" style="display: block; padding: 0 10px">                                 .on( 'click', '.sidebar-tabs li', changeRightTab );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px">        })( $gp.editor.install_hooks );
</span></span></pre>
</div>
</div>

</body>
</html>