Making WordPress.org

Changeset 12624


Ignore:
Timestamp:
06/06/2023 09:10:32 AM (2 years ago)
Author:
spiraltee
Message:

Translate: Update from remote repo (add ChatGPT auto-review)

Location:
sites/trunk/wordpress.org/public_html/wp-content/plugins/gp-translation-helpers
Files:
1 added
6 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/gp-translation-helpers/css/discussion.css

    r12435 r12624  
    326326    text-decoration: none;
    327327}
     328.openai-review {
     329    background: #eee;
     330    border-radius: 3px;
     331    padding: 1em;
     332    color: #4b4c4c;
     333    font-size: .9em;
     334    margin-top: 1em;
     335}
     336.openai-review h4 {
     337    margin-top: 0.2em;
     338    margin-bottom: 0.3em;
     339    font-size: 1em;
     340}
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/gp-translation-helpers/gp-translation-helpers.php

    r12405 r12624  
    3838require_once __DIR__ . '/includes/class-wporg-customizations.php';
    3939require_once __DIR__ . '/includes/class-gp-custom-locale-reasons.php';
     40require_once __DIR__ . '/includes/class-gp-openai-review.php';
    4041
    41 add_action( 'gp_init', array( 'GP_Translation_Helpers', 'init' ) ); // todo: remove this when this plugin will be merged in the GlotPress core.
    42 add_action( 'gp_init', array( 'GP_Sidebar', 'init' ) );    // todo: remove this when this plugin will be merged in the GlotPress core.
    43 add_action( 'gp_init', array( 'WPorg_GlotPress_Notifications', 'init' ) );    // todo: include this class in a different plugin.
     42if ( ! wp_doing_ajax() ) {
     43    add_action( 'gp_init', array( 'GP_Translation_Helpers', 'init' ) ); // todo: remove this when this plugin will be merged in the GlotPress core.
     44    add_action( 'gp_init', array( 'GP_Sidebar', 'init' ) );    // todo: remove this when this plugin will be merged in the GlotPress core.
     45    add_action( 'gp_init', array( 'WPorg_GlotPress_Notifications', 'init' ) );    // todo: include this class in a different plugin.
     46    add_filter( 'gp_enable_changesrequested_status', '__return_true' ); // todo: remove this filter when this plugin will be merged in the GlotPress core.
     47}
     48
    4449add_action( 'gp_init', array( 'WPorg_GlotPress_Customizations', 'init' ) );    // todo: include this class in a different plugin.
    45 add_filter( 'gp_enable_changesrequested_status', '__return_true' ); // todo: remove this filter when this plugin will be merged in the GlotPress core.
     50add_action( 'wp_ajax_fetch_openai_review', array( 'GP_Translation_Helpers', 'fetch_openai_review' ) );
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/gp-translation-helpers/includes/class-gp-translation-helpers.php

    r12435 r12624  
    435435            )
    436436        );
     437    }
     438
     439    /**
     440     * Is called from the AJAX request in reject-feedback.js to use ChatGPT to review a translation.
     441     *
     442     * @since 0.0.2
     443     *
     444     * @return void
     445     */
     446    public static function fetch_openai_review() {
     447        check_ajax_referer( 'gp_comment_feedback', 'nonce' );
     448        $translation_id = sanitize_text_field( $_POST['data']['translation_id'] );
     449        $locale_slug    = sanitize_text_field( $_POST['data']['locale_slug'] );
     450        $glossary_query = sanitize_text_field( $_POST['data']['glossary_query'] );
     451        $is_retry       = filter_var( $_POST['data']['is_retry'], FILTER_VALIDATE_BOOLEAN );
     452
     453        $translation = GP::$translation->get( $translation_id );
     454        $original    = GP::$original->get( $translation->original_id );
     455
     456        $openai_response = GP_OpenAI_Review::get_openai_review( $original->singular, $translation->translation_0, $locale_slug, $glossary_query, $is_retry );
     457
     458        wp_send_json_success( $openai_response['openai'] );
    437459    }
    438460
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/gp-translation-helpers/includes/class-wporg-customizations.php

    r12405 r12624  
    8282                }
    8383            );
     84            add_filter(
     85                'gp_get_openai_key',
     86                function () {
     87                    $default_sort = get_user_option( 'gp_default_sort' );
     88                    if ( is_array( $default_sort ) && ! empty( $default_sort['openai_api_key'] ) ) {
     89                        $gp_openai_key = $default_sort['openai_api_key'];
     90                        return $gp_openai_key;
     91                    }
     92                    return;
     93                }
     94            );
    8495        }
    8596    }
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/gp-translation-helpers/js/editor.js

    r12408 r12624  
    1 /* global $gp, $gp_translation_helpers_editor, wpApiSettings  */
     1/* global $gp, $gp_translation_helpers_editor, wpApiSettings, $gp_comment_feedback_settings, console, $gp_editor_options  */
    22/* eslint camelcase: "off" */
    33jQuery( function( $ ) {
     4    var focusedRowId = '';
    45    // When a user clicks on a sidebar tab, the visible tab and div changes.
    56    $gp.editor.table.on( 'click', '.sidebar-tabs li', function() {
     
    1516    // or with the hotkeys), the translation textarea is focused, so the tabs (header tabs and
    1617    // divs with the content) for the right sidebar are updated.
    17     $gp.editor.table.on( 'focus input', 'tr.editor textarea.foreign-text', function() {
     18    $gp.editor.table.on( 'focus', 'tr.editor textarea.foreign-text', function() {
    1819        var tr = $( this ).closest( 'tr.editor' );
     20        var rowId = tr.attr( 'row' );
     21        var translation_status = tr.find( '.panel-header' ).find( 'span' ).html();
     22
     23        if ( focusedRowId === rowId ) {
     24            return;
     25        }
     26        focusedRowId = rowId;
    1927        loadTabsAndDivs( tr );
     28        if ( $gp_editor_options.can_approve && ( 'waiting' === translation_status || 'fuzzy' === translation_status ) ) {
     29            fetchOpenAIReviewResponse( rowId, tr, false );
     30        }
     31    } );
     32
     33    $gp.editor.table.on( 'click', 'a.retry-auto-review', function( event ) {
     34        var tr = $( this ).closest( 'tr.editor' );
     35        var rowId = tr.attr( 'row' );
     36        event.preventDefault();
     37        tr.find( '.openai-review .auto-review-result' ).html( '' );
     38        tr.find( '.openai-review .suggestions__loading-indicator' ).show();
     39        fetchOpenAIReviewResponse( rowId, tr, true );
    2040    } );
    2141
     
    211231        } );
    212232    }
     233
     234    /**
     235     * Fetch translation review from OpenAI.
     236     *
     237     * @param {string}  rowId      The row-id attribute of the current row.
     238     * @param {string}  currentRow The current row.
     239     * @param {boolean} isRetry    The current row.
     240     */
     241    function fetchOpenAIReviewResponse( rowId, currentRow, isRetry ) {
     242        var payload = {};
     243        var data = {};
     244        var original_str = currentRow.find( '.original' );
     245        var glossary_prompt = '';
     246        var translationId = $gp.editor.translation_id_from_row_id( rowId );
     247
     248        $.each( $( original_str ).find( '.glossary-word' ), function( k, word ) {
     249            $.each( $( word ).data( 'translations' ), function( i, e ) {
     250                glossary_prompt += 'where "' + word.textContent + '" is translated as "' + e.translation + '" when it is a ' + e.pos;
     251                if ( e.comment ) {
     252                    glossary_prompt += ' (' + e.comment + ')';
     253                }
     254                glossary_prompt += ', ';
     255            } );
     256        } );
     257
     258        if ( '' !== glossary_prompt ) {
     259            glossary_prompt = 'You are required to follow these rules, ' + glossary_prompt + 'for words found in the English text you are translating.';
     260        }
     261        payload.locale_slug = $gp_comment_feedback_settings.locale_slug;
     262        payload.translation_id = translationId;
     263        payload.glossary_query = glossary_prompt;
     264        payload.is_retry = isRetry;
     265
     266        data = {
     267            action: 'fetch_openai_review',
     268            data: payload,
     269            _ajax_nonce: $gp_comment_feedback_settings.nonce,
     270        };
     271
     272        $.ajax(
     273            {
     274                type: 'POST',
     275                url: $gp_comment_feedback_settings.url,
     276                data: data,
     277            }
     278        ).done(
     279            function( response ) {
     280                currentRow.find( '.openai-review .suggestions__loading-indicator' ).hide();
     281                if ( 200 === response.data.status ) {
     282                    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)' ) );
     283                } else {
     284                    currentRow.find( '.openai-review .auto-review-result' ).text( 'Error ' + response.data.status + ' : ' + response.data.error );
     285                }
     286                currentRow.find( '.openai-review .auto-review-result' ).append( ' <a href="#" class="retry-auto-review">Retry</a>' );
     287            }
     288        ).fail(
     289            function( xhr, msg ) {
     290                /* eslint no-console: ["error", { allow: ["error"] }] */
     291                console.error( data );
     292                msg = 'An error has occurred';
     293                if ( xhr.responseText ) {
     294                    msg += ': ' + xhr.responseText;
     295                }
     296                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.';
     297                $gp.notices.error( msg );
     298            }
     299        );
     300    }
    213301} );
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/gp-translation-helpers/templates/translation-row-editor-meta-feedback.php

    r12435 r12624  
    11<?php if ( ! $can_approve_translation || ! $translation->translation_status ) {
    22    return;
    3 }  ?>
     3}
     4
     5if ( 'waiting' === $translation->translation_status || 'fuzzy' === $translation->translation_status ) :
     6    ?>
     7<div>
     8    <div class="openai-review">
     9        <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>
     10        <div class="auto-review-result"></div>
     11    </div>
     12</div>
     13<?php endif; ?>
    414<details open>
    515    <summary class="feedback-summary"><?php esc_html_e( 'Give feedback', 'glotpress' ); ?></summary>
Note: See TracChangeset for help on using the changeset viewer.