| 1 | <?php |
| 2 | class Dotorg_Plugin_I18n { |
| 3 | var $db; |
| 4 | var $tracker; |
| 5 | |
| 6 | // TODO: remove when we launch for all plugins. |
| 7 | var $translated_plugins = array( |
| 8 | 'blogware-importer', |
| 9 | 'livejournal-importer', |
| 10 | 'dotclear-importer', |
| 11 | ); |
| 12 | |
| 13 | var $i18n_cache_group = 'plugin-i18n'; |
| 14 | |
| 15 | function __construct( $db, $tracker = null ) { |
| 16 | if ( !empty( $db ) && is_object( $db ) ) |
| 17 | $this->db = $db; |
| 18 | if ( !empty( $tracker ) && is_object( $tracker ) ) |
| 19 | $this->tracker = $tracker; |
| 20 | wp_cache_add_global_groups( $this->i18n_cache_group ); |
| 21 | } |
| 22 | |
| 23 | /* |
| 24 | * *********************** |
| 25 | * Processing |
| 26 | * *********************** |
| 27 | */ |
| 28 | |
| 29 | function process( $slug, $branch = 'dev', $type = 'all' ) { |
| 30 | if ( empty( $slug ) || empty( $this->tracker ) ) |
| 31 | return false; |
| 32 | |
| 33 | // DEBUG: in_array check is because we'll start the program with a finite list of plugins |
| 34 | // TODO: remove when we launch for all plugins. |
| 35 | if ( ! in_array( $slug, $this->translated_plugins ) ) |
| 36 | return false; |
| 37 | |
| 38 | if ( 'stable' !== $branch ) |
| 39 | $branch = 'dev'; |
| 40 | |
| 41 | if ( 'code' !== $type && 'readme' !== $type ) |
| 42 | $type = 'all'; |
| 43 | |
| 44 | $path_rel = "{$slug}/trunk/"; |
| 45 | |
| 46 | if ( 'stable' === $branch ) { |
| 47 | if ( false == ( $stable_tag = $this->tracker->get_stable_tag_dir_using( $path_rel ) ) ) { |
| 48 | // Can't get a stable tag, bail out |
| 49 | return false; |
| 50 | } else if ( 'trunk' == trim( $stable_tag['tag_dir'], '/' ) ) { |
| 51 | // If stable is trunk, then it's really same as dev, switch to that |
| 52 | $branch = 'dev'; |
| 53 | } else { |
| 54 | // We're dealing with an actual stable tag, go for it |
| 55 | $path_rel = "{$slug}/{$stable_tag['tag_dir']}"; // |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | $this->set_glotpress_for_plugin( $slug ); |
| 60 | |
| 61 | if ( 'code' === $type || 'all' === $type ) |
| 62 | $this->process_code( $path_rel, $branch ); |
| 63 | |
| 64 | if ( 'readme' === $type || 'all' === $type ) |
| 65 | $this->process_readme( $path_rel, $branch ); |
| 66 | |
| 67 | echo "Processed {$type} for {$path_rel}\n"; |
| 68 | return true; |
| 69 | } |
| 70 | |
| 71 | function process_code( $path_rel, $branch = 'dev' ) { |
| 72 | if ( empty( $this->tracker ) ) |
| 73 | return false; |
| 74 | |
| 75 | $slug = preg_replace( '|^/?([^/]+)/?.+?$|', '\1', $path_rel ); |
| 76 | |
| 77 | if ( empty( $slug ) || !preg_match( '/^[a-z0-9-]+$/i', $slug ) ) |
| 78 | return false; |
| 79 | |
| 80 | // DEBUG: in_array check is because we'll start the program with a finite list of plugins |
| 81 | // TODO: remove when we launch for all plugins. |
| 82 | if ( !in_array( $slug, $this->translated_plugins ) ) |
| 83 | return false; |
| 84 | |
| 85 | $export_path = $this->tracker->create_export( $path_rel ); |
| 86 | |
| 87 | if ( empty( $export_path ) || !is_dir( $export_path ) ) |
| 88 | return false; |
| 89 | |
| 90 | $old_cwd = getcwd(); |
| 91 | chdir( $export_path ); |
| 92 | |
| 93 | // Check for a plugin text domain declaration and loading, grep recursively, not necessarily in [slug].php |
| 94 | if ( ! shell_exec( 'grep -r --include "*.php" "Text Domain: ' . escapeshellarg( $slug ) . '" .' ) && ! shell_exec( 'grep -r --include "*.php" "\bload_plugin_textdomain\b" .' ) ) |
| 95 | return false; |
| 96 | |
| 97 | if ( !class_exists( 'PotExtMeta' ) ) |
| 98 | require_once( __DIR__ . '/i18n-tools/pot-ext-meta.php' ); |
| 99 | |
| 100 | // Create pot file from code |
| 101 | $pot_file = "./tmp-{$slug}.pot"; // Using tmp- prefix in case a plugin has $slug.pot committed |
| 102 | $makepot = new MakePOT; |
| 103 | |
| 104 | if ( ! $makepot->wp_plugin( '.', $pot_file, $slug ) || ! file_exists( $pot_file ) ) |
| 105 | return false; |
| 106 | |
| 107 | // DEBUG |
| 108 | // system( "cat {$pot_file}" ); |
| 109 | |
| 110 | $this->import_to_glotpress_project( $slug, $branch, $pot_file ); |
| 111 | |
| 112 | chdir( $old_cwd ); |
| 113 | return true; |
| 114 | } |
| 115 | |
| 116 | function process_readme( $path_rel, $branch = 'dev' ) { |
| 117 | if ( empty( $this->tracker ) ) |
| 118 | return false; |
| 119 | |
| 120 | $slug = preg_replace( '|^/?([^/]+)/?.+?$|', '\1', $path_rel ); |
| 121 | |
| 122 | if ( empty( $slug ) || !preg_match( '/^[a-z0-9-]+$/i', $slug ) ) |
| 123 | return false; |
| 124 | |
| 125 | // DEBUG: in_array as separate check because we'll start the program with a finite list of plugins |
| 126 | // TODO: remove when we launch for all plugins. |
| 127 | if ( !in_array( $slug, $this->translated_plugins ) ) |
| 128 | return false; |
| 129 | |
| 130 | $export_path = $this->tracker->create_export( $path_rel ); |
| 131 | |
| 132 | if ( empty( $export_path ) || !is_dir( $export_path ) ) |
| 133 | return false; |
| 134 | |
| 135 | $old_cwd = getcwd(); |
| 136 | chdir( $export_path ); |
| 137 | |
| 138 | $readme = $this->tracker->parse_readme_in( $path_rel ); |
| 139 | |
| 140 | $str_priorities = array(); |
| 141 | |
| 142 | if ( !class_exists( 'PO' ) ) |
| 143 | require_once( __DIR__ . '/i18n-tools/pomo/po.php' ); |
| 144 | |
| 145 | $pot = new PO; |
| 146 | |
| 147 | // No need for license, being in the directory implies GPLv2 or later. Add here otherwise. |
| 148 | foreach ( array( 'name', 'short_description' ) as $key ) { |
| 149 | $readme[ $key ] = trim( $readme[ $key ] ) ; |
| 150 | } |
| 151 | |
| 152 | // If empty or "sketchy", get the plugin name form the PHP file's headers |
| 153 | if ( empty( $readme['name'] ) || 'Plugin Name' == trim( $readme['name'] ) ) { |
| 154 | // -o in grep will make sure we don't get comments opening delimiters (//, /*) or spaces as part of string |
| 155 | $name_from_php = trim( shell_exec( 'grep -o "\bPlugin Name:.*" ' . escapeshellarg( $slug ) . '.php' ) ); |
| 156 | // Remove the header label |
| 157 | $name_from_php = str_replace( 'Plugin Name:', '', $name_from_php ); |
| 158 | // Do clean out potential comment closing delimiter (*/) out of string |
| 159 | $name_from_php = preg_replace( '|^(.+)[\s]+?\*/$|', '\1', $name_from_php ); |
| 160 | // Use trimmed results as plugin name |
| 161 | $readme['name'] = trim( $name_from_php ); |
| 162 | } |
| 163 | |
| 164 | if ( !empty( $readme['name'] ) ) { |
| 165 | $pot->add_entry( new Translation_Entry ( array( |
| 166 | 'singular' => $readme['name'], |
| 167 | 'extracted_comments' => 'Name.', |
| 168 | ) ) ); |
| 169 | |
| 170 | $str_priorities[ $readme['name'] ] = 1; |
| 171 | } |
| 172 | |
| 173 | if ( !empty( $readme['short_description'] ) ) { |
| 174 | $pot->add_entry( new Translation_Entry ( array( |
| 175 | 'singular' => $readme['short_description'], |
| 176 | 'extracted_comments' => 'Short description.', |
| 177 | ) ) ); |
| 178 | |
| 179 | $str_priorities[ $readme['short_description'] ] = 1; |
| 180 | } |
| 181 | |
| 182 | if ( !empty( $readme['screenshots'] ) ) { |
| 183 | foreach ( $readme['screenshots'] as $sshot_key => $sshot_desc ) { |
| 184 | $sshot_desc = trim( $sshot_desc ); |
| 185 | $pot->add_entry( new Translation_Entry ( array( |
| 186 | 'singular' => $sshot_desc, |
| 187 | 'extracted_comments' => 'Screenshot description.', |
| 188 | ) ) ); |
| 189 | } |
| 190 | |
| 191 | } |
| 192 | |
| 193 | if ( empty( $readme['sections'] ) ) |
| 194 | $readme['sections'] = array(); |
| 195 | |
| 196 | // Adding remaining content as a section so it's processed by the same loop below |
| 197 | if ( !empty( $readme['remaining_content'] ) ) |
| 198 | $readme['sections']['remaining_content'] = $readme['remaining_content']; |
| 199 | |
| 200 | $strings = array(); |
| 201 | |
| 202 | foreach ( $readme['sections'] as $section_key => $section_text ) { |
| 203 | if ( 'screenshots' == $section_key ) |
| 204 | continue; |
| 205 | |
| 206 | /* |
| 207 | * Scanned tags based on block elements found in Automattic_Readme::filter_text() $allowed. |
| 208 | * Scanning H3/4, li, p and blockquote. Other tags are ignored in strings (a, strong, cite, etc). |
| 209 | * Processing notes: |
| 210 | * * Don't normalize/modify original text, will be used as a search pattern in original doc in some use-cases. |
| 211 | * * Using regexes over XML parsing for performance reasons, could move to the latter for more accuracy. |
| 212 | */ |
| 213 | |
| 214 | if ( 'changelog' !== $section_key ) { // No need to scan non-translatable version headers in changelog |
| 215 | if ( preg_match_all( '|<h[3-4]+[^>]*>(.+)</h[3-4]+>|', $section_text, $matches ) ) { |
| 216 | if ( !empty( $matches[1] ) ) { |
| 217 | foreach ( $matches[1] as $text ) { |
| 218 | $strings = $this->handle_translator_comment( $strings, $text, "{$section_key} header" ); |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | if ( preg_match_all( '|<li>(.+)</li>|', $section_text, $matches ) ) { |
| 225 | if ( !empty( $matches[1] ) ) { |
| 226 | foreach ( $matches[1] as $text ) { |
| 227 | $strings = $this->handle_translator_comment( $strings, $text, "{$section_key} list item" ); |
| 228 | if ( 'changelog' === $section_key ) |
| 229 | $str_priorities[ $text ] = -1; |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | if ( preg_match_all( '|<p>(.+)</p>|', $section_text, $matches ) ) { |
| 235 | if ( !empty( $matches[1] ) ) { |
| 236 | foreach ( $matches[1] as $text ) { |
| 237 | $strings = $this->handle_translator_comment( $strings, $text, "{$section_key} paragraph" ); |
| 238 | if ( 'changelog' === $section_key ) |
| 239 | $str_priorities[ $text ] = -1; |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | if ( preg_match_all( '|<blockquote>(.+)</blockquote>|', $section_text, $matches ) ) { |
| 245 | if ( !empty( $matches[1] ) ) { |
| 246 | foreach ( $matches[1] as $text ) { |
| 247 | $strings = $this->handle_translator_comment( $strings, $text, "{$section_key} block quote" ); |
| 248 | if ( 'changelog' === $section_key ) |
| 249 | $str_priorities[ $text ] = -1; |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | foreach ( $strings as $text => $comments ) { |
| 256 | $pot->add_entry( new Translation_Entry ( array( |
| 257 | 'singular' => $text, |
| 258 | 'extracted_comments' => 'Found in ' . implode( $comments, ", " ) . '.', |
| 259 | ) ) ); |
| 260 | } |
| 261 | |
| 262 | $pot_file = "./tmp-{$slug}-readme.pot"; |
| 263 | $pot->export_to_file( $pot_file ); |
| 264 | |
| 265 | // DEBUG |
| 266 | // system( "cat {$pot_file}" ); |
| 267 | |
| 268 | // import to GlotPress, dev or stable |
| 269 | $this->import_to_glotpress_project( $slug, "{$branch}-readme", $pot_file, $str_priorities ); |
| 270 | |
| 271 | chdir( $old_cwd ); |
| 272 | return true; |
| 273 | } |
| 274 | |
| 275 | function handle_translator_comment( $array, $key, $val ) { |
| 276 | $val = trim( preg_replace( '/[^a-z0-9]/i', ' ', $val ) ); // cleanup key names for display |
| 277 | if ( empty( $array[ $key ] ) ) { |
| 278 | $array[ $key ] = array( $val ); |
| 279 | } else if ( !in_array( $val, $array[ $key ] ) ) { |
| 280 | $array[ $key ][] = $val; |
| 281 | } |
| 282 | return $array; |
| 283 | } |
| 284 | |
| 285 | function import_to_glotpress_project( $project, $branch, $file, $str_priorities = array() ) { |
| 286 | if ( empty( $project ) || empty( $branch ) || empty( $file ) ) |
| 287 | return; |
| 288 | // Note: this will only work if the GlotPress project/sub-projects exist. |
| 289 | $cmd = 'php ' . __DIR__ . '/../../../translate/glotpress/scripts/import-originals.php -o po -p ' . escapeshellarg( "wp-plugins/{$project }/{$branch}" ) . ' -f ' . escapeshellarg( $file ); |
| 290 | // DEBUG |
| 291 | // var_dump( $cmd ); |
| 292 | system( $cmd ); |
| 293 | if ( empty( $str_priorities ) ) |
| 294 | return; |
| 295 | $branch_id = $this->get_gp_branch_id( $project, $branch ); |
| 296 | // Set the string priorities in GP once the originals have been imported |
| 297 | if ( empty( $branch_id ) ) |
| 298 | return; |
| 299 | foreach ( (array) $str_priorities as $str => $prio ) { |
| 300 | if ( 1 !== $prio && -1 !== $prio ) |
| 301 | $prio = 0; |
| 302 | $res = $this->db->query( $this->db->prepare( |
| 303 | 'UPDATE translate_originals SET priority = %d WHERE project_id = %d AND status = %s AND singular = %s', |
| 304 | $prio, $branch_id, '+active', $str |
| 305 | ) ); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | function set_glotpress_for_plugin( $plugin_slug ) { |
| 310 | if ( empty( $plugin_slug ) ) |
| 311 | return; |
| 312 | $cmd = 'php ' . __DIR__ . '/../../../translate/bin/set-wp-plugin-project.php ' . escapeshellarg( $plugin_slug ); |
| 313 | // DEBUG |
| 314 | // var_dump( $cmd ); |
| 315 | system( $cmd ); |
| 316 | } |
| 317 | |
| 318 | /* |
| 319 | * *********************** |
| 320 | * Rendering |
| 321 | * *********************** |
| 322 | */ |
| 323 | |
| 324 | function translate( $key, $content, $args = array() ) { |
| 325 | if ( empty( $key ) || empty( $content ) ) |
| 326 | return $content; |
| 327 | |
| 328 | if ( !empty( $args['topic_id'] ) && is_numeric( $args['topic_id'] ) ) |
| 329 | $topic = get_topic( $args['topic_id'] ); |
| 330 | else |
| 331 | global $topic; |
| 332 | |
| 333 | if ( empty( $topic ) ) |
| 334 | return $content; |
| 335 | |
| 336 | $language = ''; |
| 337 | $server_name = strtolower( $_SERVER['SERVER_NAME'] ); |
| 338 | if ( 'api.wordpress.org' == $server_name ) { |
| 339 | if ( preg_match( '/^[a-z]{2}(_[A-Z]{2})?$/', trim( $_REQUEST['locale'] ) ) ) { |
| 340 | $language = substr( trim( $_REQUEST['locale'] ), 0, 5 ); |
| 341 | } else if ( !empty( $_REQUEST['request'] ) ) { |
| 342 | $request = maybe_unserialize( $_REQUEST['request'] ); |
| 343 | if ( !empty( $request ) && !empty( $request->locale ) && preg_match( '/^[a-z]{2}(_[A-Z]{2})?$/', trim( $request->locale ) ) ) { |
| 344 | $language = trim( $request->locale ); |
| 345 | } |
| 346 | } |
| 347 | } else if ( preg_match( '/^([^\.]+)\.wordpress\.org$/', $server_name, $matches ) ) { |
| 348 | $subdomain = $this->verify_subdomain( $matches[1] ); |
| 349 | if ( ! empty( $subdomain ) ) |
| 350 | $language = substr( $server_name, 0, 2 ); |
| 351 | } |
| 352 | |
| 353 | if ( empty( $language ) || 'en' === $language || 'en_US' === $language ) |
| 354 | return $content; |
| 355 | |
| 356 | $slug = $topic->plugin_san; |
| 357 | |
| 358 | // DEBUG: in_array check is because we'll start the program with a finite list of plugins |
| 359 | // TODO: remove when we launch for all plugins. |
| 360 | if ( empty( $slug ) || ! in_array( $slug, $this->translated_plugins ) ) |
| 361 | return $content; |
| 362 | |
| 363 | $branch = ( empty( $topic->stable_tag ) || 'trunk' === $topic->stable_tag ) ? 'dev' : 'stable'; |
| 364 | |
| 365 | if ( empty( $args['code_i18n'] ) || true !== $args['code_i18n'] ) |
| 366 | $branch .= '-readme'; |
| 367 | |
| 368 | $cache_suffix = "{$language}:{$key}"; |
| 369 | |
| 370 | // Try the cache |
| 371 | if ( false !== ( $cache = $this->cache_get( $slug, $branch, $cache_suffix ) ) ) { |
| 372 | // DEBUG |
| 373 | // var_dump( array( $slug, $branch, $cache_suffix, $cache ) ); |
| 374 | return $cache; |
| 375 | } |
| 376 | |
| 377 | $originals = $this->get_gp_originals( $slug, $branch, $key, $content ); |
| 378 | |
| 379 | if ( empty( $originals ) ) |
| 380 | return $content; |
| 381 | |
| 382 | $translation_set_id = $this->get_gp_translation_set_id( $slug, $branch, $language ); |
| 383 | |
| 384 | if ( empty( $translation_set_id ) ) |
| 385 | return $content; |
| 386 | |
| 387 | foreach ( $originals as $original ) { |
| 388 | if ( empty( $original->id ) ) |
| 389 | continue; |
| 390 | |
| 391 | $translation = $this->db->get_var( $this->db->prepare( |
| 392 | 'SELECT translation_0 FROM translate_translations WHERE original_id = %d AND translation_set_id = %d AND status = %s', |
| 393 | $original->id, $translation_set_id, 'current' |
| 394 | ) ); |
| 395 | |
| 396 | if ( empty( $translation ) ) |
| 397 | continue; |
| 398 | |
| 399 | $content = $this->translate_gp_original( $original->singular, $translation, $content ); |
| 400 | } |
| 401 | |
| 402 | $this->cache_set( $slug, $branch, $content, $cache_suffix ); |
| 403 | |
| 404 | return $content; |
| 405 | } |
| 406 | |
| 407 | function cache_key( $slug, $branch, $suffix = null ) { |
| 408 | // EG keys |
| 409 | // plugin:press-this:stable-readme:originals |
| 410 | // plugin:press-this:stable-readme:original:title |
| 411 | // plugin:press-this:stable-readme:fr:title |
| 412 | $key = "plugin:{$slug}:{$branch}"; |
| 413 | if ( !empty( $suffix ) ) |
| 414 | $key .= ":{$suffix}"; |
| 415 | return $key; |
| 416 | } |
| 417 | |
| 418 | function cache_get( $slug, $branch, $suffix = null ) { |
| 419 | $key = $this->cache_key( $slug, $branch, $suffix ); |
| 420 | // DEBUG |
| 421 | // wp_cache_delete( $key, $this->i18n_cache_group ); |
| 422 | return wp_cache_get( $key, $this->i18n_cache_group ); |
| 423 | } |
| 424 | |
| 425 | function cache_set( $slug, $branch, $content, $suffix = null ) { |
| 426 | $key = $this->cache_key( $slug, $branch, $suffix ); |
| 427 | return wp_cache_set( $key, $content, $this->i18n_cache_group ); |
| 428 | } |
| 429 | |
| 430 | function get_gp_branch_id( $slug, $branch ) { |
| 431 | $cache_suffix = "branch_id"; |
| 432 | |
| 433 | if ( false !== ( $branch_id = $this->cache_get( $slug, $branch, $cache_suffix ) ) ) |
| 434 | return $branch_id; |
| 435 | |
| 436 | $branch_id = $this->db->get_var( $this->db->prepare( |
| 437 | 'SELECT id FROM translate_projects WHERE path = %s', |
| 438 | "wp-plugins/{$slug}/{$branch}" |
| 439 | ) ); |
| 440 | |
| 441 | if ( empty( $branch_id ) ) |
| 442 | $branch_id = 0; |
| 443 | |
| 444 | $this->cache_set( $slug, $branch, $branch_id, $cache_suffix ); |
| 445 | |
| 446 | return $branch_id; |
| 447 | } |
| 448 | |
| 449 | function get_gp_originals( $slug, $branch, $key, $str ) { |
| 450 | // Try to get a single original with the whole content first (title, etc), if passed, or get them all otherwise. |
| 451 | if ( !empty( $key ) && !empty( $str ) ) { |
| 452 | $originals = $this->search_gp_original( $slug, $branch, $key, $str ); |
| 453 | if ( !empty( $originals ) ) |
| 454 | return array( $originals ); |
| 455 | // Do not cache this as originals, search_gp_original() does its own caching |
| 456 | } |
| 457 | |
| 458 | $cache_suffix = 'originals'; |
| 459 | |
| 460 | if ( false !== ( $originals = $this->cache_get( $slug, $branch, $cache_suffix ) ) ) |
| 461 | return $originals; |
| 462 | |
| 463 | $branch_id = $this->get_gp_branch_id( $slug, $branch ); |
| 464 | |
| 465 | if ( empty( $branch_id ) ) |
| 466 | return array(); |
| 467 | |
| 468 | $originals = $this->db->get_results( $this->db->prepare( |
| 469 | 'SELECT id, singular, comment FROM translate_originals WHERE project_id = %d AND status = %s', |
| 470 | $branch_id, '+active' |
| 471 | ) ); |
| 472 | |
| 473 | if ( empty( $originals ) ) |
| 474 | $originals = array(); // still cache if empty, but as array, never false |
| 475 | |
| 476 | $this->cache_set( $slug, $branch, $originals, $cache_suffix ); |
| 477 | |
| 478 | return $originals; |
| 479 | } |
| 480 | |
| 481 | function get_gp_translation_set_id( $slug, $branch, $locale ) { |
| 482 | $locale = strtolower( $locale ); |
| 483 | |
| 484 | if ( false !== strpos( $locale, '_' ) ) { |
| 485 | $locale = str_replace( '_', '-', $locale ); |
| 486 | } |
| 487 | |
| 488 | $cache_suffix = "{$locale}:translation_set_id"; |
| 489 | |
| 490 | if ( false !== ( $translation_set_id = $this->cache_get( $slug, $branch, $cache_suffix ) ) ) |
| 491 | return $translation_set_id; |
| 492 | |
| 493 | $branch_id = $this->get_gp_branch_id( $slug, $branch ); |
| 494 | |
| 495 | if ( empty( $branch_id ) ) |
| 496 | return 0; |
| 497 | |
| 498 | $translation_set_id = $this->db->get_var( $this->db->prepare( |
| 499 | 'SELECT id FROM translate_translation_sets WHERE project_id = %d AND locale = %s', |
| 500 | $branch_id, $locale ) ); |
| 501 | |
| 502 | if ( empty( $translation_set_id ) ) { |
| 503 | // Don't give up yet. Might be given fr_FR, which actually exists as locale=fr in GP. |
| 504 | $translation_set_id = $this->db->get_var( $this->db->prepare( |
| 505 | 'SELECT id FROM translate_translation_sets WHERE project_id = %d AND locale = %s', |
| 506 | $branch_id, substr( $locale, 0, 2 ) ) ); |
| 507 | } |
| 508 | |
| 509 | if ( empty( $translation_set_id ) ) |
| 510 | $translation_set_id = 0; |
| 511 | |
| 512 | $this->cache_set( $slug, $branch, $translation_set_id, $cache_suffix ); |
| 513 | |
| 514 | return $translation_set_id; |
| 515 | } |
| 516 | |
| 517 | function search_gp_original( $slug, $branch, $key, $str ) { |
| 518 | $cache_suffix = "original:{$key}"; |
| 519 | |
| 520 | if ( false !== ( $original = $this->cache_get( $slug, $branch, $cache_suffix ) ) ) |
| 521 | return $original; |
| 522 | |
| 523 | $branch_id = $this->get_gp_branch_id( $slug, $branch ); |
| 524 | |
| 525 | if ( empty( $branch_id ) ) |
| 526 | return false; |
| 527 | |
| 528 | $original = $this->db->get_row( $this->db->prepare( |
| 529 | 'SELECT id, singular, comment FROM translate_originals WHERE project_id = %d AND status = %s AND singular = %s', |
| 530 | $branch_id, '+active', $str |
| 531 | ) ); |
| 532 | |
| 533 | if ( empty( $original ) ) |
| 534 | $original = null; |
| 535 | |
| 536 | $this->cache_set( $slug, $branch, $original, $cache_suffix ); |
| 537 | |
| 538 | return $original; |
| 539 | } |
| 540 | |
| 541 | function translate_gp_original( $original, $translation, $content) { |
| 542 | $content = str_replace( $original, $translation, $content ); |
| 543 | return $content; |
| 544 | } |
| 545 | |
| 546 | function verify_subdomain( $locale ) { |
| 547 | if ( empty( $locale ) ) |
| 548 | return ''; |
| 549 | |
| 550 | $cache_key = "subdomains:{$locale}"; |
| 551 | |
| 552 | if ( false !== ( $subdomain = wp_cache_get( $cache_key, $this->i18n_cache_group ) ) ) { |
| 553 | // var_dump(array($cache_key, $subdomain)); |
| 554 | return $subdomain; |
| 555 | } |
| 556 | |
| 557 | $subdomain = ''; |
| 558 | |
| 559 | if ( 2 === strlen( $locale ) ) { |
| 560 | $subdomain = $this->db->get_var( $this->db->prepare( |
| 561 | 'SELECT subdomain FROM locales WHERE subdomain = %s LIMIT 1', |
| 562 | $locale |
| 563 | ) ); |
| 564 | } else if ( 5 === strlen( $locale ) ) { |
| 565 | $subdomain = $this->db->get_var( $this->db->prepare( |
| 566 | 'SELECT subdomain FROM locales WHERE locale = %s LIMIT 1', |
| 567 | $locale |
| 568 | ) ); |
| 569 | } |
| 570 | |
| 571 | wp_cache_set( $cache_key, $subdomain, $this->i18n_cache_group ); |
| 572 | |
| 573 | return $subdomain; |
| 574 | } |
| 575 | } |
| 576 | No newline at end of file |