Making WordPress.org


Ignore:
Timestamp:
04/05/2023 07:33:49 PM (3 years ago)
Author:
amieiro
Message:

Traslate: Make all the requests async in the Translation Memory

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/inc/class-plugin.php

    r12510 r12527  
    1818     * @var array
    1919     */
    20     private $queue = [];
     20    private $queue = array();
    2121
    2222    /**
     
    3636     */
    3737    private function __construct() {
    38         add_action( 'plugins_loaded', [ $this, 'plugins_loaded' ] );
     38        add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ) );
    3939    }
    4040
     
    4343     */
    4444    public function plugins_loaded() {
    45         add_action( 'template_redirect', [ $this, 'register_routes' ], 5 );
    46         add_action( 'gp_pre_tmpl_load', [ $this, 'pre_tmpl_load' ], 10, 2 );
    47         add_action( 'wporg_translate_suggestions', [ $this, 'extend_translation_suggestions' ] );
     45        add_action( 'template_redirect', array( $this, 'register_routes' ), 5 );
     46        add_action( 'gp_pre_tmpl_load', array( $this, 'pre_tmpl_load' ), 10, 2 );
     47        add_action( 'wporg_translate_suggestions', array( $this, 'extend_translation_suggestions' ) );
    4848
    4949        if ( 'cli' !== PHP_SAPI ) {
    50             add_action( 'gp_translation_created', [ $this, 'translation_updated' ], 3 );
    51             add_action( 'gp_translation_saved', [ $this, 'translation_updated' ], 3 );
     50            add_action( 'gp_translation_created', array( $this, 'translation_updated' ), 3 );
     51            add_action( 'gp_translation_saved', array( $this, 'translation_updated' ), 3 );
    5252
    5353            // DB Writes are delayed until shutdown to bulk-update the stats during imports.
    54             add_action( 'shutdown', [ $this, 'schedule_tm_update' ], 3 );
     54            add_action( 'shutdown', array( $this, 'schedule_tm_update' ), 3 );
    5555        }
    5656
    57         add_action( self::TM_UPDATE_EVENT, [ Translation_Memory_Client::class, 'update' ] );
     57        add_action( self::TM_UPDATE_EVENT, array( Translation_Memory_Client::class, 'update' ) );
    5858    }
    5959
     
    7676     */
    7777    public function schedule_tm_update() {
    78         remove_action( 'gp_translation_created', [ $this, 'translation_updated' ], 3 );
    79         remove_action( 'gp_translation_saved', [ $this, 'translation_updated' ], 3 );
     78        remove_action( 'gp_translation_created', array( $this, 'translation_updated' ), 3 );
     79        remove_action( 'gp_translation_saved', array( $this, 'translation_updated' ), 3 );
    8080
    8181        if ( ! $this->queue ) {
     
    8383        }
    8484
    85         wp_schedule_single_event( time() + 60, self::TM_UPDATE_EVENT, [ 'translations' => $this->queue ] );
     85        wp_schedule_single_event( time() + 60, self::TM_UPDATE_EVENT, array( 'translations' => $this->queue ) );
    8686    }
    8787
     
    9090     */
    9191    public function register_routes() {
    92         $dir = '([^_/][^/]*)';
    93         $path = '(.+?)';
     92        $dir      = '([^_/][^/]*)';
     93        $path     = '(.+?)';
    9494        $projects = 'projects';
    95         $project = $projects . '/' . $path;
    96         $locale = '(' . implode( '|', wp_list_pluck( GP_Locales::locales(), 'slug' ) ) . ')';
    97         $set = "$project/$locale/$dir";
     95        $project  = $projects . '/' . $path;
     96        $locale   = '(' . implode( '|', wp_list_pluck( GP_Locales::locales(), 'slug' ) ) . ')';
     97        $set      = "$project/$locale/$dir";
    9898
    99         GP::$router->prepend( "/$set/-get-tm-suggestions", [ __NAMESPACE__ . '\Routes\Translation_Memory', 'get_suggestions' ] );
    100         GP::$router->prepend( "/$set/-get-other-language-suggestions", [ __NAMESPACE__ . '\Routes\Other_Languages', 'get_suggestions' ] );
    101         GP::$router->prepend( "/-save-external-suggestions", [ __NAMESPACE__ . '\Routes\Translation_Memory', 'update_external_translations' ], 'post' );
     99        GP::$router->prepend( "/$set/-get-tm-suggestions", array( __NAMESPACE__ . '\Routes\Translation_Memory', 'get_suggestions' ) );
     100        GP::$router->prepend( "/$set/-get-other-language-suggestions", array( __NAMESPACE__ . '\Routes\Other_Languages', 'get_suggestions' ) );
     101        GP::$router->prepend( "/$set/-get-tm-openai-suggestions", array( __NAMESPACE__ . '\Routes\Translation_Memory', 'get_openai_suggestions' ) );
     102        GP::$router->prepend( "/$set/-get-tm-deepl-suggestions", array( __NAMESPACE__ . '\Routes\Translation_Memory', 'get_deepl_suggestions' ) );
     103        GP::$router->prepend( '/-save-external-suggestions', array( __NAMESPACE__ . '\Routes\Translation_Memory', 'update_external_translations' ), 'post' );
    102104    }
    103105
     
    113115            'gp-translation-suggestions',
    114116            plugins_url( 'css/translation-suggestions.css', PLUGIN_FILE ),
    115             [],
     117            array(),
    116118            '20220401'
    117119        );
     
    121123            'gp-translation-suggestions',
    122124            plugins_url( './js/translation-suggestions.js', PLUGIN_FILE ),
    123             [ 'gp-editor' ],
     125            array( 'gp-editor' ),
    124126            '20190510'
     127        );
     128
     129        $gp_default_sort         = get_user_option( 'gp_default_sort' );
     130        $get_openai_translations = ! empty( trim( gp_array_get( $gp_default_sort, 'openai_api_key' ) ) );
     131        $get_deepl_translations  = ! empty( trim( gp_array_get( $gp_default_sort, 'deepl_api_key' ) ) );
     132
     133        wp_localize_script(
     134            'gp-translation-suggestions',
     135            'gpTranslationSuggestions',
     136            array(
     137                'nonce'                     => wp_create_nonce( 'gp-translation-suggestions' ),
     138                'get_external_translations' => array(
     139                    'get_openai_translations' => $get_openai_translations,
     140                    'get_deepl_translations'  => $get_deepl_translations,
     141                ),
     142            )
    125143        );
    126144
     
    130148            'gp-translation-suggestions',
    131149            sprintf(
    132                 "window.WPORG_TRANSLATION_MEMORY_API_URL = %s;\nwindow.WPORG_OTHER_LANGUAGES_API_URL = %s;",
     150                "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;",
    133151                wp_json_encode( gp_url_project( $args['project'], gp_url_join( $args['locale_slug'], $args['translation_set_slug'], '-get-tm-suggestions' ) ) ),
     152                wp_json_encode( gp_url_project( $args['project'], gp_url_join( $args['locale_slug'], $args['translation_set_slug'], '-get-tm-openai-suggestions' ) ) ),
     153                wp_json_encode( gp_url_project( $args['project'], gp_url_join( $args['locale_slug'], $args['translation_set_slug'], '-get-tm-deepl-suggestions' ) ) ),
    134154                wp_json_encode( gp_url_project( $args['project'], gp_url_join( $args['locale_slug'], $args['translation_set_slug'], '-get-other-language-suggestions' ) ) )
    135155            )
     
    147167            return;
    148168        }
    149 
    150169
    151170        // Prevent querying the TM for long strings which usually time out
Note: See TracChangeset for help on using the changeset viewer.