<!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>[12624] sites/trunk/wordpress.org/public_html/wp-content/plugins/gp-translation-helpers: Translate: Update from remote repo (add ChatGPT auto-review)</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/12624">12624</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/12624","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-06-06 09:10:32 +0000 (Tue, 06 Jun 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: Update from remote repo (add ChatGPT auto-review)</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginsgptranslationhelperscssdiscussioncss">sites/trunk/wordpress.org/public_html/wp-content/plugins/gp-translation-helpers/css/discussion.css</a></li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginsgptranslationhelpersgptranslationhelpersphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/gp-translation-helpers/gp-translation-helpers.php</a></li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginsgptranslationhelpersincludesclassgptranslationhelpersphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/gp-translation-helpers/includes/class-gp-translation-helpers.php</a></li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginsgptranslationhelpersincludesclasswporgcustomizationsphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/gp-translation-helpers/includes/class-wporg-customizations.php</a></li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginsgptranslationhelpersjseditorjs">sites/trunk/wordpress.org/public_html/wp-content/plugins/gp-translation-helpers/js/editor.js</a></li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginsgptranslationhelperstemplatestranslationroweditormetafeedbackphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/gp-translation-helpers/templates/translation-row-editor-meta-feedback.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginsgptranslationhelpersincludesclassgpopenaireviewphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/gp-translation-helpers/includes/class-gp-openai-review.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginsgptranslationhelperscssdiscussioncss"></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/gp-translation-helpers/css/discussion.css</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/gp-translation-helpers/css/discussion.css        2023-06-06 05:31:31 UTC (rev 12623)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/gp-translation-helpers/css/discussion.css  2023-06-06 09:10:32 UTC (rev 12624)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -325,3 +325,16 @@
</span><span class="cx" style="display: block; padding: 0 10px"> td.gtes-involved a{
</span><span class="cx" style="display: block; padding: 0 10px">        text-decoration: none;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+.openai-review {
+    background: #eee;
+    border-radius: 3px;
+    padding: 1em;
+    color: #4b4c4c;
+    font-size: .9em;
+    margin-top: 1em;
+}
+.openai-review h4 {
+    margin-top: 0.2em;
+    margin-bottom: 0.3em;
+    font-size: 1em;
+}
</ins></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginsgptranslationhelpersgptranslationhelpersphp"></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/gp-translation-helpers/gp-translation-helpers.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/gp-translation-helpers/gp-translation-helpers.php        2023-06-06 05:31:31 UTC (rev 12623)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/gp-translation-helpers/gp-translation-helpers.php  2023-06-06 09:10:32 UTC (rev 12624)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -37,9 +37,14 @@
</span><span class="cx" style="display: block; padding: 0 10px"> require_once __DIR__ . '/includes/class-wporg-notifications.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require_once __DIR__ . '/includes/class-wporg-customizations.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require_once __DIR__ . '/includes/class-gp-custom-locale-reasons.php';
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+require_once __DIR__ . '/includes/class-gp-openai-review.php';
</ins><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( 'gp_init', array( 'GP_Translation_Helpers', 'init' ) ); // todo: remove this when this plugin will be merged in the GlotPress core.
-add_action( 'gp_init', array( 'GP_Sidebar', 'init' ) );    // todo: remove this when this plugin will be merged in the GlotPress core.
-add_action( 'gp_init', array( 'WPorg_GlotPress_Notifications', 'init' ) );    // todo: include this class in a different plugin.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+if ( ! wp_doing_ajax() ) {
+       add_action( 'gp_init', array( 'GP_Translation_Helpers', 'init' ) ); // todo: remove this when this plugin will be merged in the GlotPress core.
+       add_action( 'gp_init', array( 'GP_Sidebar', 'init' ) );    // todo: remove this when this plugin will be merged in the GlotPress core.
+       add_action( 'gp_init', array( 'WPorg_GlotPress_Notifications', 'init' ) );    // todo: include this class in a different plugin.
+       add_filter( 'gp_enable_changesrequested_status', '__return_true' ); // todo: remove this filter when this plugin will be merged in the GlotPress core.
+}
+
</ins><span class="cx" style="display: block; padding: 0 10px"> add_action( 'gp_init', array( 'WPorg_GlotPress_Customizations', 'init' ) );    // todo: include this class in a different plugin.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-add_filter( 'gp_enable_changesrequested_status', '__return_true' ); // todo: remove this filter when this plugin will be merged in the GlotPress core.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+add_action( 'wp_ajax_fetch_openai_review', array( 'GP_Translation_Helpers', 'fetch_openai_review' ) );
</ins></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginsgptranslationhelpersincludesclassgpopenaireviewphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: sites/trunk/wordpress.org/public_html/wp-content/plugins/gp-translation-helpers/includes/class-gp-openai-review.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/gp-translation-helpers/includes/class-gp-openai-review.php                               (rev 0)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/gp-translation-helpers/includes/class-gp-openai-review.php 2023-06-06 09:10:32 UTC (rev 12624)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,82 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+class GP_OpenAI_Review {
+       /**
+        * The OpenAI key.
+        *
+        * @var string
+        */
+       private static $gp_openai_key = '';
+
+       /**
+        * Get suggestions from OpenAI (ChatGPT).
+        *
+        * @param string  $original_singular The singular from the original string.
+        * @param string  $translation       The translation.
+        * @param string  $locale            The locale.
+        * @param string  $glossary_query   The prompt generated to include glossary for the locale.
+        * @param boolean $is_retry   Flag to check if the request is a retry.
+        *
+        * @return array
+        */
+       public static function get_openai_review( $original_singular, $translation, $locale, $glossary_query, $is_retry ): array {
+               $openai_query = '';
+               $openai_key   = apply_filters( 'gp_get_openai_key', self::$gp_openai_key );
+
+               if ( empty( trim( $openai_key ) ) ) {
+                       return array();
+               }
+               $openai_temperature = 0;
+
+               $gp_locale     = GP_Locales::by_field( 'slug', $locale );
+               $openai_query .= 'For the english text  "' . $original_singular . '", is "' . $translation . '" a correct translation in ' . $gp_locale->english_name . '?';
+               if ( $glossary_query ) {
+                       $messages[] = array(
+                               'role'    => 'system',
+                               'content' => $glossary_query,
+                       );
+               }
+               $messages[]      = array(
+                       'role'    => 'user',
+                       'content' => $openai_query,
+               );
+               $start_time      = microtime( true );
+               $openai_response = wp_remote_post(
+                       'https://api.openai.com/v1/chat/completions',
+                       array(
+                               'timeout' => 20,
+                               'headers' => array(
+                                       'Content-Type'  => 'application/json',
+                                       'Authorization' => 'Bearer ' . $openai_key,
+                               ),
+                               'body'    => wp_json_encode(
+                                       array(
+                                               'model'       => 'gpt-3.5-turbo',
+                                               'max_tokens'  => 1000,
+                                               'n'           => 1,
+                                               'messages'    => $messages,
+                                               'temperature' => $openai_temperature,
+                                       )
+                               ),
+                       )
+               );
+               $end_time        = microtime( true );
+               $time_taken      = $end_time - $start_time;
+
+               $response_status = wp_remote_retrieve_response_code( $openai_response );
+               $output          = json_decode( wp_remote_retrieve_body( $openai_response ), true );
+
+               if ( 200 !== $response_status || is_wp_error( $openai_response ) ) {
+                       $response['openai']['status'] = $response_status;
+                       $response['openai']['error']  = wp_remote_retrieve_response_message( $openai_response );
+                       return $response;
+               }
+
+               $message                          = $output['choices'][0]['message'];
+               $response['openai']['status']     = $response_status;
+               $response['openai']['review']     = trim( trim( $message['content'] ), '"' );
+               $response['openai']['time_taken'] = $time_taken;
+
+               return $response;
+       }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: sites/trunk/wordpress.org/public_html/wp-content/plugins/gp-translation-helpers/includes/class-gp-openai-review.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginsgptranslationhelpersincludesclassgptranslationhelpersphp"></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/gp-translation-helpers/includes/class-gp-translation-helpers.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/gp-translation-helpers/includes/class-gp-translation-helpers.php 2023-06-06 05:31:31 UTC (rev 12623)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/gp-translation-helpers/includes/class-gp-translation-helpers.php   2023-06-06 09:10:32 UTC (rev 12624)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -437,6 +437,28 @@
</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">+         * Is called from the AJAX request in reject-feedback.js to use ChatGPT to review a translation.
+        *
+        * @since 0.0.2
+        *
+        * @return void
+        */
+       public static function fetch_openai_review() {
+               check_ajax_referer( 'gp_comment_feedback', 'nonce' );
+               $translation_id = sanitize_text_field( $_POST['data']['translation_id'] );
+               $locale_slug    = sanitize_text_field( $_POST['data']['locale_slug'] );
+               $glossary_query = sanitize_text_field( $_POST['data']['glossary_query'] );
+               $is_retry       = filter_var( $_POST['data']['is_retry'], FILTER_VALIDATE_BOOLEAN );
+
+               $translation = GP::$translation->get( $translation_id );
+               $original    = GP::$original->get( $translation->original_id );
+
+               $openai_response = GP_OpenAI_Review::get_openai_review( $original->singular, $translation->translation_0, $locale_slug, $glossary_query, $is_retry );
+
+               wp_send_json_success( $openai_response['openai'] );
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Is called from the AJAX request in reject-feedback.js to submit an comment feedback.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 0.0.2
</span></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginsgptranslationhelpersincludesclasswporgcustomizationsphp"></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/gp-translation-helpers/includes/class-wporg-customizations.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/gp-translation-helpers/includes/class-wporg-customizations.php   2023-06-06 05:31:31 UTC (rev 12623)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/gp-translation-helpers/includes/class-wporg-customizations.php     2023-06-06 09:10:32 UTC (rev 12624)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -81,6 +81,17 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                        return __( 'GTEs/PTEs/CLPTEs Involved' );
</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">+                        add_filter(
+                               'gp_get_openai_key',
+                               function () {
+                                       $default_sort = get_user_option( 'gp_default_sort' );
+                                       if ( is_array( $default_sort ) && ! empty( $default_sort['openai_api_key'] ) ) {
+                                               $gp_openai_key = $default_sort['openai_api_key'];
+                                               return $gp_openai_key;
+                                       }
+                                       return;
+                               }
+                       );
</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></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginsgptranslationhelpersjseditorjs"></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/gp-translation-helpers/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/gp-translation-helpers/js/editor.js      2023-06-06 05:31:31 UTC (rev 12623)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/gp-translation-helpers/js/editor.js        2023-06-06 09:10:32 UTC (rev 12624)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1,6 +1,7 @@
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-/* global $gp, $gp_translation_helpers_editor, wpApiSettings  */
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+/* global $gp, $gp_translation_helpers_editor, wpApiSettings, $gp_comment_feedback_settings, console, $gp_editor_options  */
</ins><span class="cx" style="display: block; padding: 0 10px"> /* eslint camelcase: "off" */
</span><span class="cx" style="display: block; padding: 0 10px"> jQuery( function( $ ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        var focusedRowId = '';
</ins><span class="cx" style="display: block; padding: 0 10px">         // When a user clicks on a sidebar tab, the visible tab and div changes.
</span><span class="cx" style="display: block; padding: 0 10px">        $gp.editor.table.on( 'click', '.sidebar-tabs li', function() {
</span><span class="cx" style="display: block; padding: 0 10px">                var tab = $( this );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -14,11 +15,30 @@
</span><span class="cx" style="display: block; padding: 0 10px">        // When a new translation row is opened (with double click, clicking in the "Details" button,
</span><span class="cx" style="display: block; padding: 0 10px">        // or with the hotkeys), the translation textarea is focused, so the tabs (header tabs and
</span><span class="cx" style="display: block; padding: 0 10px">        // divs with the content) for the right sidebar are updated.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $gp.editor.table.on( 'focus input', 'tr.editor textarea.foreign-text', function() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $gp.editor.table.on( 'focus', 'tr.editor textarea.foreign-text', function() {
</ins><span class="cx" style="display: block; padding: 0 10px">                 var tr = $( this ).closest( 'tr.editor' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                var rowId = tr.attr( 'row' );
+               var translation_status = tr.find( '.panel-header' ).find( 'span' ).html();
+
+               if ( focusedRowId === rowId ) {
+                       return;
+               }
+               focusedRowId = rowId;
</ins><span class="cx" style="display: block; padding: 0 10px">                 loadTabsAndDivs( tr );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                if ( $gp_editor_options.can_approve && ( 'waiting' === translation_status || 'fuzzy' === translation_status ) ) {
+                       fetchOpenAIReviewResponse( rowId, tr, false );
+               }
</ins><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.editor.table.on( 'click', 'a.retry-auto-review', function( event ) {
+               var tr = $( this ).closest( 'tr.editor' );
+               var rowId = tr.attr( 'row' );
+               event.preventDefault();
+               tr.find( '.openai-review .auto-review-result' ).html( '' );
+               tr.find( '.openai-review .suggestions__loading-indicator' ).show();
+               fetchOpenAIReviewResponse( rowId, tr, true );
+       } );
+
</ins><span class="cx" style="display: block; padding: 0 10px">         // Shows/hides the reply form for a comment in the discussion.
</span><span class="cx" style="display: block; padding: 0 10px">        $gp.editor.table.on( 'click', 'a.comment-reply-link', function( event ) {
</span><span class="cx" style="display: block; padding: 0 10px">                var commentId = $( this ).attr( 'data-commentid' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -210,4 +230,72 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        add_copy_button( '#sidebar-div-other-locales-' + originalId );
</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">+
+       /**
+        * Fetch translation review from OpenAI.
+        *
+        * @param {string}  rowId      The row-id attribute of the current row.
+        * @param {string}  currentRow The current row.
+        * @param {boolean} isRetry    The current row.
+        */
+       function fetchOpenAIReviewResponse( rowId, currentRow, isRetry ) {
+               var payload = {};
+               var data = {};
+               var original_str = currentRow.find( '.original' );
+               var glossary_prompt = '';
+               var translationId = $gp.editor.translation_id_from_row_id( rowId );
+
+               $.each( $( original_str ).find( '.glossary-word' ), function( k, word ) {
+                       $.each( $( word ).data( 'translations' ), function( i, e ) {
+                               glossary_prompt += 'where "' + word.textContent + '" is translated as "' + e.translation + '" when it is a ' + e.pos;
+                               if ( e.comment ) {
+                                       glossary_prompt += ' (' + e.comment + ')';
+                               }
+                               glossary_prompt += ', ';
+                       } );
+               } );
+
+               if ( '' !== glossary_prompt ) {
+                       glossary_prompt = 'You are required to follow these rules, ' + glossary_prompt + 'for words found in the English text you are translating.';
+               }
+               payload.locale_slug = $gp_comment_feedback_settings.locale_slug;
+               payload.translation_id = translationId;
+               payload.glossary_query = glossary_prompt;
+               payload.is_retry = isRetry;
+
+               data = {
+                       action: 'fetch_openai_review',
+                       data: payload,
+                       _ajax_nonce: $gp_comment_feedback_settings.nonce,
+               };
+
+               $.ajax(
+                       {
+                               type: 'POST',
+                               url: $gp_comment_feedback_settings.url,
+                               data: data,
+                       }
+               ).done(
+                       function( response ) {
+                               currentRow.find( '.openai-review .suggestions__loading-indicator' ).hide();
+                               if ( 200 === response.data.status ) {
+                                       currentRow.find( '.openai-review .auto-review-result' ).html( '<h4>Auto-review by ChatGPT' ).append( $( '<span/>' ).text( response.data.review + ' (' + response.data.time_taken.toFixed( 2 ) + 's)' ) );
+                               } else {
+                                       currentRow.find( '.openai-review .auto-review-result' ).text( 'Error ' + response.data.status + ' : ' + response.data.error );
+                               }
+                               currentRow.find( '.openai-review .auto-review-result' ).append( ' <a href="#" class="retry-auto-review">Retry</a>' );
+                       }
+               ).fail(
+                       function( xhr, msg ) {
+                               /* eslint no-console: ["error", { allow: ["error"] }] */
+                               console.error( data );
+                               msg = 'An error has occurred';
+                               if ( xhr.responseText ) {
+                                       msg += ': ' + xhr.responseText;
+                               }
+                               msg += '. Please, take a screenshot of the output in the browser console, send it to the developers, and reload the page to see if it works.';
+                               $gp.notices.error( msg );
+                       }
+               );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> } );
</span></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginsgptranslationhelperstemplatestranslationroweditormetafeedbackphp"></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/gp-translation-helpers/templates/translation-row-editor-meta-feedback.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/gp-translation-helpers/templates/translation-row-editor-meta-feedback.php        2023-06-06 05:31:31 UTC (rev 12623)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/gp-translation-helpers/templates/translation-row-editor-meta-feedback.php  2023-06-06 09:10:32 UTC (rev 12624)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1,6 +1,16 @@
</span><span class="cx" style="display: block; padding: 0 10px"> <?php if ( ! $can_approve_translation || ! $translation->translation_status ) {
</span><span class="cx" style="display: block; padding: 0 10px">        return;
</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">+}
+
+if ( 'waiting' === $translation->translation_status || 'fuzzy' === $translation->translation_status ) :
+       ?>
+<div>
+       <div class="openai-review">
+               <p class="suggestions__loading-indicator">ChatGPT review in progress <span aria-hidden="true" class="suggestions__loading-indicator__icon"><span></span><span></span><span></span></span></p>
+               <div class="auto-review-result"></div>
+       </div>
+</div>
+<?php endif; ?>
</ins><span class="cx" style="display: block; padding: 0 10px"> <details open>
</span><span class="cx" style="display: block; padding: 0 10px">        <summary class="feedback-summary"><?php esc_html_e( 'Give feedback', 'glotpress' ); ?></summary>
</span><span class="cx" style="display: block; padding: 0 10px">        <div id="feedback-form">
</span></span></pre>
</div>
</div>

</body>
</html>