Changeset 12527 for sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/inc/class-plugin.php
- Timestamp:
- 04/05/2023 07:33:49 PM (3 years ago)
- 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 18 18 * @var array 19 19 */ 20 private $queue = [];20 private $queue = array(); 21 21 22 22 /** … … 36 36 */ 37 37 private function __construct() { 38 add_action( 'plugins_loaded', [ $this, 'plugins_loaded' ]);38 add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ) ); 39 39 } 40 40 … … 43 43 */ 44 44 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' ) ); 48 48 49 49 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 ); 52 52 53 53 // 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 ); 55 55 } 56 56 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' ) ); 58 58 } 59 59 … … 76 76 */ 77 77 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 ); 80 80 81 81 if ( ! $this->queue ) { … … 83 83 } 84 84 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 ) ); 86 86 } 87 87 … … 90 90 */ 91 91 public function register_routes() { 92 $dir = '([^_/][^/]*)';93 $path = '(.+?)';92 $dir = '([^_/][^/]*)'; 93 $path = '(.+?)'; 94 94 $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"; 98 98 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' ); 102 104 } 103 105 … … 113 115 'gp-translation-suggestions', 114 116 plugins_url( 'css/translation-suggestions.css', PLUGIN_FILE ), 115 [],117 array(), 116 118 '20220401' 117 119 ); … … 121 123 'gp-translation-suggestions', 122 124 plugins_url( './js/translation-suggestions.js', PLUGIN_FILE ), 123 [ 'gp-editor' ],125 array( 'gp-editor' ), 124 126 '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 ) 125 143 ); 126 144 … … 130 148 'gp-translation-suggestions', 131 149 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;", 133 151 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' ) ) ), 134 154 wp_json_encode( gp_url_project( $args['project'], gp_url_join( $args['locale_slug'], $args['translation_set_slug'], '-get-other-language-suggestions' ) ) ) 135 155 ) … … 147 167 return; 148 168 } 149 150 169 151 170 // Prevent querying the TM for long strings which usually time out
Note: See TracChangeset
for help on using the changeset viewer.