<!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>[12527] sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions: Traslate: Make all the requests async in the Translation Memory</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/12527">12527</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/12527","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>amieiro</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2023-04-05 19:33:49 +0000 (Wed, 05 Apr 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'>Traslate: Make all the requests async in the Translation Memory

See https://github.com/WordPress/wordpress.org/pull/137</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggptranslationsuggestionsincclasspluginphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/inc/class-plugin.php</a></li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggptranslationsuggestionsincroutesclasstranslationmemoryphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/inc/routes/class-translation-memory.php</a></li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggptranslationsuggestionsjstranslationsuggestionsjs">sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/js/translation-suggestions.js</a></li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggptranslationsuggestionstemplatestranslationmemorysuggestionsphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/templates/translation-memory-suggestions.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggptranslationsuggestionsincclasspluginphp"></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-translation-suggestions/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-translation-suggestions/inc/class-plugin.php    2023-04-05 02:59:09 UTC (rev 12526)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/inc/class-plugin.php      2023-04-05 19:33:49 UTC (rev 12527)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -17,7 +17,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="cx" style="display: block; padding: 0 10px">         * @var array
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        private $queue = [];
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ private $queue = array();
</ins><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">         * Returns always the same instance of this plugin.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -35,7 +35,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * Instantiates a new Plugin object.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        private function __construct() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                add_action( 'plugins_loaded', [ $this, 'plugins_loaded' ] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ) );
</ins><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><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -42,19 +42,19 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * Initializes the plugin.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public function plugins_loaded() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                add_action( 'template_redirect', [ $this, 'register_routes' ], 5 );
-               add_action( 'gp_pre_tmpl_load', [ $this, 'pre_tmpl_load' ], 10, 2 );
-               add_action( 'wporg_translate_suggestions', [ $this, 'extend_translation_suggestions' ] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         add_action( 'template_redirect', array( $this, 'register_routes' ), 5 );
+               add_action( 'gp_pre_tmpl_load', array( $this, 'pre_tmpl_load' ), 10, 2 );
+               add_action( 'wporg_translate_suggestions', array( $this, 'extend_translation_suggestions' ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( 'cli' !== PHP_SAPI ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        add_action( 'gp_translation_created', [ $this, 'translation_updated' ], 3 );
-                       add_action( 'gp_translation_saved', [ $this, 'translation_updated' ], 3 );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 add_action( 'gp_translation_created', array( $this, 'translation_updated' ), 3 );
+                       add_action( 'gp_translation_saved', array( $this, 'translation_updated' ), 3 );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        // DB Writes are delayed until shutdown to bulk-update the stats during imports.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        add_action( 'shutdown', [ $this, 'schedule_tm_update' ], 3 );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 add_action( 'shutdown', array( $this, 'schedule_tm_update' ), 3 );
</ins><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">-                add_action( self::TM_UPDATE_EVENT, [ Translation_Memory_Client::class, 'update' ] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         add_action( self::TM_UPDATE_EVENT, array( Translation_Memory_Client::class, 'update' ) );
</ins><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><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -75,14 +75,14 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * Schedules a single event to update translation memory for new translations.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public function schedule_tm_update() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                remove_action( 'gp_translation_created', [ $this, 'translation_updated' ], 3 );
-               remove_action( 'gp_translation_saved', [ $this, 'translation_updated' ], 3 );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         remove_action( 'gp_translation_created', array( $this, 'translation_updated' ), 3 );
+               remove_action( 'gp_translation_saved', array( $this, 'translation_updated' ), 3 );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! $this->queue ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        return;
</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">-                wp_schedule_single_event( time() + 60, self::TM_UPDATE_EVENT, [ 'translations' => $this->queue ] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         wp_schedule_single_event( time() + 60, self::TM_UPDATE_EVENT, array( 'translations' => $this->queue ) );
</ins><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><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -89,16 +89,18 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * Registers custom routes.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public function register_routes() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $dir = '([^_/][^/]*)';
-               $path = '(.+?)';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $dir      = '([^_/][^/]*)';
+               $path     = '(.+?)';
</ins><span class="cx" style="display: block; padding: 0 10px">                 $projects = 'projects';
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $project = $projects . '/' . $path;
-               $locale = '(' . implode( '|', wp_list_pluck( GP_Locales::locales(), 'slug' ) ) . ')';
-               $set = "$project/$locale/$dir";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $project  = $projects . '/' . $path;
+               $locale   = '(' . implode( '|', wp_list_pluck( GP_Locales::locales(), 'slug' ) ) . ')';
+               $set      = "$project/$locale/$dir";
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                GP::$router->prepend( "/$set/-get-tm-suggestions", [ __NAMESPACE__ . '\Routes\Translation_Memory', 'get_suggestions' ] );
-               GP::$router->prepend( "/$set/-get-other-language-suggestions", [ __NAMESPACE__ . '\Routes\Other_Languages', 'get_suggestions' ] );
-               GP::$router->prepend( "/-save-external-suggestions", [ __NAMESPACE__ . '\Routes\Translation_Memory', 'update_external_translations' ], 'post' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         GP::$router->prepend( "/$set/-get-tm-suggestions", array( __NAMESPACE__ . '\Routes\Translation_Memory', 'get_suggestions' ) );
+               GP::$router->prepend( "/$set/-get-other-language-suggestions", array( __NAMESPACE__ . '\Routes\Other_Languages', 'get_suggestions' ) );
+               GP::$router->prepend( "/$set/-get-tm-openai-suggestions", array( __NAMESPACE__ . '\Routes\Translation_Memory', 'get_openai_suggestions' ) );
+               GP::$router->prepend( "/$set/-get-tm-deepl-suggestions", array( __NAMESPACE__ . '\Routes\Translation_Memory', 'get_deepl_suggestions' ) );
+               GP::$router->prepend( '/-save-external-suggestions', array( __NAMESPACE__ . '\Routes\Translation_Memory', 'update_external_translations' ), 'post' );
</ins><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><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -112,7 +114,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                wp_register_style(
</span><span class="cx" style="display: block; padding: 0 10px">                        'gp-translation-suggestions',
</span><span class="cx" style="display: block; padding: 0 10px">                        plugins_url( 'css/translation-suggestions.css', PLUGIN_FILE ),
</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">+                 array(),
</ins><span class="cx" style="display: block; padding: 0 10px">                         '20220401'
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span><span class="cx" style="display: block; padding: 0 10px">                gp_enqueue_style( 'gp-translation-suggestions' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -120,17 +122,35 @@
</span><span class="cx" style="display: block; padding: 0 10px">                wp_register_script(
</span><span class="cx" style="display: block; padding: 0 10px">                        'gp-translation-suggestions',
</span><span class="cx" style="display: block; padding: 0 10px">                        plugins_url( './js/translation-suggestions.js', PLUGIN_FILE ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        [ 'gp-editor' ],
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 array( 'gp-editor' ),
</ins><span class="cx" style="display: block; padding: 0 10px">                         '20190510'
</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">+                $gp_default_sort         = get_user_option( 'gp_default_sort' );
+               $get_openai_translations = ! empty( trim( gp_array_get( $gp_default_sort, 'openai_api_key' ) ) );
+               $get_deepl_translations  = ! empty( trim( gp_array_get( $gp_default_sort, 'deepl_api_key' ) ) );
+
+               wp_localize_script(
+                       'gp-translation-suggestions',
+                       'gpTranslationSuggestions',
+                       array(
+                               'nonce'                     => wp_create_nonce( 'gp-translation-suggestions' ),
+                               'get_external_translations' => array(
+                                       'get_openai_translations' => $get_openai_translations,
+                                       'get_deepl_translations'  => $get_deepl_translations,
+                               ),
+                       )
+               );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 gp_enqueue_script( 'gp-translation-suggestions' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                wp_add_inline_script(
</span><span class="cx" style="display: block; padding: 0 10px">                        'gp-translation-suggestions',
</span><span class="cx" style="display: block; padding: 0 10px">                        sprintf(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                "window.WPORG_TRANSLATION_MEMORY_API_URL = %s;\nwindow.WPORG_OTHER_LANGUAGES_API_URL = %s;",
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         "window.WPORG_TRANSLATION_MEMORY_API_URL = %s;\nwindow.WPORG_TRANSLATION_MEMORY_OPENAI_API_URL = %s;\nwindow.WPORG_TRANSLATION_MEMORY_DEEPL_API_URL = %s;\nwindow.WPORG_OTHER_LANGUAGES_API_URL = %s;",
</ins><span class="cx" style="display: block; padding: 0 10px">                                 wp_json_encode( gp_url_project( $args['project'], gp_url_join( $args['locale_slug'], $args['translation_set_slug'], '-get-tm-suggestions' ) ) ),
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                wp_json_encode( gp_url_project( $args['project'], gp_url_join( $args['locale_slug'], $args['translation_set_slug'], '-get-tm-openai-suggestions' ) ) ),
+                               wp_json_encode( gp_url_project( $args['project'], gp_url_join( $args['locale_slug'], $args['translation_set_slug'], '-get-tm-deepl-suggestions' ) ) ),
</ins><span class="cx" style="display: block; padding: 0 10px">                                 wp_json_encode( gp_url_project( $args['project'], gp_url_join( $args['locale_slug'], $args['translation_set_slug'], '-get-other-language-suggestions' ) ) )
</span><span class="cx" style="display: block; padding: 0 10px">                        )
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -147,7 +167,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        return;
</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">-
</del><span class="cx" style="display: block; padding: 0 10px">                 // Prevent querying the TM for long strings which usually time out
</span><span class="cx" style="display: block; padding: 0 10px">                // and have no results due to being too unique.
</span><span class="cx" style="display: block; padding: 0 10px">                $query_tm = mb_strlen( $entry->singular ) <= 420;
</span></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggptranslationsuggestionsincroutesclasstranslationmemoryphp"></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-translation-suggestions/inc/routes/class-translation-memory.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-translation-suggestions/inc/routes/class-translation-memory.php 2023-04-05 02:59:09 UTC (rev 12526)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/inc/routes/class-translation-memory.php   2023-04-05 19:33:49 UTC (rev 12527)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -11,6 +11,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> class Translation_Memory extends GP_Route {
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        public function get_suggestions( $project_path, $locale_slug, $set_slug ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $type                                  = 'Translation';
</ins><span class="cx" style="display: block; padding: 0 10px">                 $original_id                           = gp_get( 'original' );
</span><span class="cx" style="display: block; padding: 0 10px">                $translation_id                        = gp_get( 'translation', 0 );
</span><span class="cx" style="display: block; padding: 0 10px">                $nonce                                 = gp_get( 'nonce' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -38,7 +39,44 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        $locale .= '_' . $set_slug;
</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">-                $suggestions                     = Translation_Memory_Client::query( $original->singular, $locale );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $suggestions = Translation_Memory_Client::query( $original->singular, $locale );
+
+               if ( is_wp_error( $suggestions ) ) {
+                       wp_send_json_error( $suggestions->get_error_code() );
+               }
+
+               wp_send_json_success(
+                       gp_tmpl_get_output(
+                               'translation-memory-suggestions',
+                               compact( 'suggestions', 'type' ),
+                               PLUGIN_DIR . '/templates/'
+                       )
+               );
+       }
+
+       public function get_openai_suggestions( $project_path, $locale_slug, $set_slug ) {
+               $type                                  = 'OpenAI';
+               $original_id                           = gp_get( 'original' );
+               $translation_id                        = gp_get( 'translation', 0 );
+               $nonce                                 = gp_get( 'nonce' );
+               $gp_default_sort                       = get_user_option( 'gp_default_sort' );
+               $external_services_exclude_some_status = gp_array_get( $gp_default_sort, 'external_services_exclude_some_status', 0 );
+               $translation                           = null;
+               $suggestions                           = array();
+
+               if ( ! wp_verify_nonce( $nonce, 'translation-memory-suggestions-' . $original_id ) ) {
+                       wp_send_json_error( 'invalid_nonce' );
+               }
+
+               if ( empty( $original_id ) ) {
+                       wp_send_json_error( 'no_original' );
+               }
+
+               $original = GP::$original->get( $original_id );
+               if ( ! $original ) {
+                       wp_send_json_error( 'invalid_original' );
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 $current_set_slug                = 'default';
</span><span class="cx" style="display: block; padding: 0 10px">                $locale_glossary_translation_set = GP::$translation_set->by_project_id_slug_and_locale( 0, $current_set_slug, $locale_slug );
</span><span class="cx" style="display: block; padding: 0 10px">                $locale_glossary                 = GP::$glossary->by_set_id( $locale_glossary_translation_set->id );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -48,19 +86,67 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                $translation = GP::$translation->get( $translation_id );
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( ! $translation || ( 'current' != $translation->status && 'rejected' != $translation->status && 'old' != $translation->status ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $openai_suggestions = $this->get_openai_suggestion( $original->singular, $locale_slug, $locale_glossary );
-                               $deepl_suggestions  = $this->get_deepl_suggestion( $original->singular, $locale_slug, $set_slug );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         $suggestions = $this->get_openai_suggestion( $original->singular, $locale_slug, $locale_glossary );
</ins><span class="cx" style="display: block; padding: 0 10px">                         }
</span><span class="cx" style="display: block; padding: 0 10px">                } else {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $openai_suggestions = $this->get_openai_suggestion( $original->singular, $locale_slug, $locale_glossary );
-                       $deepl_suggestions  = $this->get_deepl_suggestion( $original->singular, $locale_slug, $set_slug );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $suggestions = $this->get_openai_suggestion( $original->singular, $locale_slug, $locale_glossary );
</ins><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">-                if ( is_wp_error( $suggestions ) ) {
-                       wp_send_json_error( $suggestions->get_error_code() );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         wp_send_json_success(
+                       gp_tmpl_get_output(
+                               'translation-memory-suggestions',
+                               compact( 'suggestions', 'type' ),
+                               PLUGIN_DIR . '/templates/'
+                       )
+               );
+       }
+
+       public function get_deepl_suggestions( $project_path, $locale_slug, $set_slug ) {
+               $type                                  = 'DeepL';
+               $original_id                           = gp_get( 'original' );
+               $translation_id                        = gp_get( 'translation', 0 );
+               $nonce                                 = gp_get( 'nonce' );
+               $gp_default_sort                       = get_user_option( 'gp_default_sort' );
+               $external_services_exclude_some_status = gp_array_get( $gp_default_sort, 'external_services_exclude_some_status', 0 );
+               $translation                           = null;
+               $suggestions                           = array();
+
+               if ( ! wp_verify_nonce( $nonce, 'translation-memory-suggestions-' . $original_id ) ) {
+                       wp_send_json_error( 'invalid_nonce' );
</ins><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">-                wp_send_json_success( gp_tmpl_get_output( 'translation-memory-suggestions', compact( 'suggestions', 'openai_suggestions', 'deepl_suggestions' ), PLUGIN_DIR . '/templates/' ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( empty( $original_id ) ) {
+                       wp_send_json_error( 'no_original' );
+               }
+
+               $original = GP::$original->get( $original_id );
+               if ( ! $original ) {
+                       wp_send_json_error( 'invalid_original' );
+               }
+
+               $locale = $locale_slug;
+               if ( 'default' !== $set_slug ) {
+                       $locale .= '_' . $set_slug;
+               }
+
+               if ( $external_services_exclude_some_status ) {
+                       if ( $translation_id > 0 ) {
+                               $translation = GP::$translation->get( $translation_id );
+                       }
+                       if ( ! $translation || ( 'current' != $translation->status && 'rejected' != $translation->status && 'old' != $translation->status ) ) {
+                               $suggestions = $this->get_deepl_suggestion( $original->singular, $locale_slug, $set_slug );
+                       }
+               } else {
+                       $suggestions = $this->get_deepl_suggestion( $original->singular, $locale_slug, $set_slug );
+               }
+
+               wp_send_json_success(
+                       gp_tmpl_get_output(
+                               'translation-memory-suggestions',
+                               compact( 'suggestions', 'type' ),
+                               PLUGIN_DIR . '/templates/'
+                       )
+               );
</ins><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><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -377,7 +463,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * @return void
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        private function update_one_external_translation( string $translation, string $suggestion, string $external_translations_used, string $external_same_translations_used ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $sameTranslationUsed      = $translation == $suggestion;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $is_the_same_translation  = $translation == $suggestion;
</ins><span class="cx" style="display: block; padding: 0 10px">                 $gp_external_translations = get_user_option( 'gp_external_translations' );
</span><span class="cx" style="display: block; padding: 0 10px">                $translations_used        = gp_array_get( $gp_external_translations, $external_translations_used, 0 );
</span><span class="cx" style="display: block; padding: 0 10px">                $same_translations_used   = gp_array_get( $gp_external_translations, $external_same_translations_used, 0 );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -386,7 +472,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px">                $translations_used++;
</span><span class="cx" style="display: block; padding: 0 10px">                $gp_external_translations[ $external_translations_used ] = $translations_used;
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( $sameTranslationUsed ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( $is_the_same_translation ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         if ( ! is_int( $same_translations_used ) || $same_translations_used < 0 ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                $same_translations_used = 0;
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggptranslationsuggestionsjstranslationsuggestionsjs"></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-translation-suggestions/js/translation-suggestions.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-translation-suggestions/js/translation-suggestions.js   2023-04-05 02:59:09 UTC (rev 12526)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/js/translation-suggestions.js     2023-04-05 19:33:49 UTC (rev 12527)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -29,6 +29,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                xhr.always( function() {
</span><span class="cx" style="display: block; padding: 0 10px">                        $container.removeClass( 'fetching' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        removeNoSuggestionsMessage( $container );
</ins><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><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -51,6 +52,46 @@
</span><span class="cx" style="display: block; padding: 0 10px">                fetchSuggestions( $container, window.WPORG_TRANSLATION_MEMORY_API_URL, originalId, translationId, nonce );
</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">+        /**
+        * Gets the suggestions from the OpenAI API.
+        *
+        * @return {void}
+        **/
+       function maybeFetchOpenAISuggestions() {
+               maybeFetchExternalSuggestions( gpTranslationSuggestions.get_external_translations.get_openai_translations, window.WPORG_TRANSLATION_MEMORY_OPENAI_API_URL );
+       }
+
+       /**
+        * Gets the suggestions from the DeepL API.
+        *
+        * @return {void}
+        **/
+       function maybeFetchDeeplSuggestions() {
+               maybeFetchExternalSuggestions( gpTranslationSuggestions.get_external_translations.get_deepl_translations, window.WPORG_TRANSLATION_MEMORY_DEEPL_API_URL );
+       }
+
+       /**
+        * Gets the suggestions from an external service.
+        *
+        * @param getExternalSuggestions
+        * @param apiUrl
+        */
+       function maybeFetchExternalSuggestions( getExternalSuggestions, apiUrl ) {
+               var $container = $gp.editor.current.find( '.suggestions__translation-memory' );
+               if ( !$container.length ) {
+                       return;
+               }
+               if ( true !== getExternalSuggestions ) {
+                       return;
+               }
+
+               var originalId = $gp.editor.current.original_id;
+               var translationId = $gp.editor.current.translation_id;
+               var nonce = $container.data( 'nonce' );
+
+               fetchSuggestions( $container, apiUrl, originalId, translationId, nonce );
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         function maybeFetchOtherLanguageSuggestions() {
</span><span class="cx" style="display: block; padding: 0 10px">                var $container = $gp.editor.current.find( '.suggestions__other-languages' );
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! $container.length ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -70,6 +111,45 @@
</span><span class="cx" style="display: block; padding: 0 10px">                fetchSuggestions( $container, window.WPORG_OTHER_LANGUAGES_API_URL, originalId , translationId,  nonce );
</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">+        /**
+        * Removes the "No suggestions" message if there are suggestions.
+        *
+        * This is needed because the suggestions are loaded asynchronously.
+        *
+        * @param $container
+        */
+       function removeNoSuggestionsMessage( $container ) {
+               var hasSuggestions = $container.find( '.translation-suggestion' ).length > 0;
+               if ( hasSuggestions ) {
+                       $container.find( '.no-suggestions' ).hide();
+               } else {
+                       $container = removeNoSuggestionsDuplicateMessage( $container );
+               }
+       }
+
+       /**
+        * Removes duplicate "No suggestions" messages.
+        *
+        * @param $container
+        * @returns {*|jQuery}
+        */
+       function removeNoSuggestionsDuplicateMessage( $container ) {
+               var $html = $($container);
+               var $paragraphs = $html.find('p');
+               var uniqueParagraphs = [];
+
+               $paragraphs.each(function() {
+                       var paragraphText = $(this).text().trim();
+
+                       if (uniqueParagraphs.indexOf(paragraphText) === -1) {
+                               uniqueParagraphs.push(paragraphText);
+                       } else {
+                               $(this).remove();
+                       }
+               });
+
+               return $html.prop('outerHTML');
+       }
</ins><span class="cx" style="display: block; padding: 0 10px">         function copySuggestion( event ) {
</span><span class="cx" style="display: block; padding: 0 10px">                if ( 'A' === event.target.tagName ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        return;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -96,8 +176,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">        $gp.editor.show = ( function( original ) {
</span><span class="cx" style="display: block; padding: 0 10px">                return function() {
</span><span class="cx" style="display: block; padding: 0 10px">                        original.apply( $gp.editor, arguments );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
</del><span class="cx" style="display: block; padding: 0 10px">                         maybeFetchTranslationMemorySuggestions();
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        maybeFetchOpenAISuggestions();
+                       maybeFetchDeeplSuggestions();
</ins><span class="cx" style="display: block; padding: 0 10px">                         maybeFetchOtherLanguageSuggestions();
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px">        })( $gp.editor.show );
</span></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggptranslationsuggestionstemplatestranslationmemorysuggestionsphp"></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-translation-suggestions/templates/translation-memory-suggestions.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-translation-suggestions/templates/translation-memory-suggestions.php    2023-04-05 02:59:09 UTC (rev 12526)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/templates/translation-memory-suggestions.php      2023-04-05 19:33:49 UTC (rev 12527)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1,38 +1,24 @@
</span><span class="cx" style="display: block; padding: 0 10px"> <?php
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-if ( empty( $suggestions ) && empty( $openai_suggestions ) && empty( $deepl_suggestions ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+if ( empty( $suggestions ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px">         echo '<p class="no-suggestions">No suggestions.</p>';
</span><span class="cx" style="display: block; padding: 0 10px"> } else {
</span><span class="cx" style="display: block; padding: 0 10px">        echo '<ul class="suggestions-list">';
</span><span class="cx" style="display: block; padding: 0 10px">        foreach ( $suggestions as $suggestion ) {
</span><span class="cx" style="display: block; padding: 0 10px">                echo '<li>';
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                echo '<div class="translation-suggestion with-tooltip" tabindex="0" role="button" aria-pressed="false" aria-label="Copy translation">';
-                       echo '<span class="translation-suggestion__score">' . number_format( 100 * $suggestion['similarity_score'] ) . '%</span>';
-
-                       echo '<span class="translation-suggestion__translation">';
-                               echo esc_translation( $suggestion['translation'] );
-
-                               if ( $suggestion['diff'] ) {
-                                       echo '<span class="translation-suggestion__original-diff">' . wp_kses_post( $suggestion['diff'] ) . '</span>';
-                               }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         echo '<div class="translation-suggestion with-tooltip ' . esc_html( strtolower( $type ) ) . '" tabindex="0" role="button" aria-pressed="false" aria-label="Copy translation">';
+                       echo '<span class="' . esc_html( strtolower( $type ) ) . '-suggestion__score">';
+               if ( 'Translation' == $type ) {
+                       echo number_format( 100 * $suggestion['similarity_score'] ) . '%';
+               } else {
+                       echo esc_html( $type );
+               }
</ins><span class="cx" style="display: block; padding: 0 10px">                         echo '</span>';
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
-                       echo '<span aria-hidden="true" class="translation-suggestion__translation-raw">' . esc_translation( $suggestion['translation'] ) . '</span>';
-
-                       echo '<button type="button" class="button is-small copy-suggestion">Copy</button>';
-               echo '</div>';
-               echo '</li>';
-       }
-       foreach ( $openai_suggestions as $suggestion ) {
-               echo '<li>';
-               echo '<div class="translation-suggestion with-tooltip openai" tabindex="0" role="button" aria-pressed="false" aria-label="Copy translation">';
-                       echo '<span class="openai-suggestion__score">OpenAI</span>';
-
</del><span class="cx" style="display: block; padding: 0 10px">                         echo '<span class="translation-suggestion__translation">';
</span><span class="cx" style="display: block; padding: 0 10px">                                echo esc_translation( $suggestion['translation'] );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                if ( $suggestion['diff'] ) {
-                                       echo '<span class="translation-suggestion__original-diff">' . wp_kses_post( $suggestion['diff'] ) . '</span>';
-                               }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( $suggestion['diff'] ) {
+                       echo '<span class="translation-suggestion__original-diff">' . wp_kses_post( $suggestion['diff'] ) . '</span>';
+               }
</ins><span class="cx" style="display: block; padding: 0 10px">                         echo '</span>';
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        echo '<span aria-hidden="true" class="translation-suggestion__translation-raw">' . esc_translation( $suggestion['translation'] ) . '</span>';
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -41,24 +27,5 @@
</span><span class="cx" style="display: block; padding: 0 10px">                echo '</div>';
</span><span class="cx" style="display: block; padding: 0 10px">                echo '</li>';
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        foreach ( $deepl_suggestions as $suggestion ) {
-               echo '<li>';
-               echo '<div class="translation-suggestion with-tooltip deepl" tabindex="0" role="button" aria-pressed="false" aria-label="Copy translation">';
-                       echo '<span class="deepl-suggestion__score">DeepL</span>';
-
-                       echo '<span class="translation-suggestion__translation">';
-                               echo esc_translation( $suggestion['translation'] );
-
-                               if ( $suggestion['diff'] ) {
-                                       echo '<span class="translation-suggestion__original-diff">' . wp_kses_post( $suggestion['diff'] ) . '</span>';
-                               }
-                       echo '</span>';
-
-                       echo '<span aria-hidden="true" class="translation-suggestion__translation-raw">' . esc_translation( $suggestion['translation'] ) . '</span>';
-
-                       echo '<button type="button" class="button is-small copy-suggestion">Copy</button>';
-               echo '</div>';
-               echo '</li>';
-       }
</del><span class="cx" style="display: block; padding: 0 10px">         echo '</ul>';
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>

</body>
</html>