| 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 | /* |
| 337 | * DEBUG: Getting $language from request or host name, should use something like $GLOBALS['locale'] |
| 338 | * and/or $GLOBALS['i18n'] once/if set properly when on localized site and get it from that. |
| 339 | * |
| 340 | * TODO: find the proper way |
| 341 | * |
| 342 | * $GLOBALS['locale'] is currently en_US on localized sites (eg: fr.wordpress.org) |
| 343 | * $GLOBALS['i18n'] is currently false on localized sites (eg: fr.wordpress.org) |
| 344 | */ |
| 345 | $language = ''; |
| 346 | $server_name = strtolower( $_SERVER['SERVER_NAME'] ); |
| 347 | if ( 'api.wordpress.org' == $server_name && preg_match( '/[a-z]{2}_[A-Z]{2}/', $_REQUEST['locale'] ) ) { |
| 348 | $language = substr( $_REQUEST['locale'], 0, 2 );; |
| 349 | } else if ( preg_match( '/^[a-z]{2}\.wordpress\.org$/', $server_name ) ) { |
| 350 | $language = substr( $server_name, 0, 2 ); |
| 351 | } |
| 352 | |
| 353 | if ( empty( $language ) || 2 !== strlen( $language ) ) { |
| 354 | return $content; |
| 355 | } |
| 356 | |
| 357 | $slug = $topic->plugin_san; |
| 358 | |
| 359 | // DEBUG: in_array check is because we'll start the program with a finite list of plugins |
| 360 | // TODO: remove when we launch for all plugins. |
| 361 | if ( empty( $slug ) || ! in_array( $slug, $this->translated_plugins ) ) { |
| 362 | return $content; |
| 363 | } |
| 364 | |
| 365 | $branch = ( empty( $topic->stable_tag ) || 'trunk' === $topic->stable_tag ) ? 'dev' : 'stable'; |
| 366 | |
| 367 | if ( empty( $args['code_i18n'] ) || true !== $args['code_i18n'] ) { |
| 368 | $branch .= '-readme'; |
| 369 | } |
| 370 | |
| 371 | // Instantiate this before modifying $content |
| 372 | $cache_suffix = "{$language}:{$key}"; |
| 373 | |
| 374 | // Try the cache |
| 375 | if ( false !== ( $cache = $this->cache_get( $slug, $branch, $cache_suffix ) ) ) { |
| 376 | // DEBUG |
| 377 | // var_dump( array( $slug, $branch, $cache_suffix, $cache ) ); |
| 378 | return $cache; |
| 379 | } |
| 380 | |
| 381 | $originals = $this->get_gp_originals( $slug, $branch, $key, $content ); |
| 382 | |
| 383 | if ( empty( $originals ) ) |
| 384 | return $content; |
| 385 | |
| 386 | $translation_set_id = $this->get_gp_translation_set_id( $slug, $branch, $language ); |
| 387 | |
| 388 | if ( empty( $translation_set_id ) ) |
| 389 | return $content; |
| 390 | |
| 391 | foreach ( $originals as $original ) { |
| 392 | if ( empty( $original->id ) ) |
| 393 | continue; |
| 394 | |
| 395 | $translation = $this->db->get_var( $this->db->prepare( |
| 396 | 'SELECT translation_0 FROM translate_translations WHERE original_id = %d AND translation_set_id = %d AND status = %s', |
| 397 | $original->id, $translation_set_id, 'current' |
| 398 | ) ); |
| 399 | |
| 400 | if ( empty( $translation ) ) |
| 401 | continue; |
| 402 | |
| 403 | $content = $this->translate_gp_original( $original->singular, $translation, $content ); |
| 404 | } |
| 405 | |
| 406 | $this->cache_set( $slug, $branch, $content, $cache_suffix ); |
| 407 | |
| 408 | return $content; |
| 409 | } |
| 410 | |
| 411 | function cache_key( $slug, $branch, $suffix = null ) { |
| 412 | // EG keys |
| 413 | // plugin:press-this:stable-readme:originals |
| 414 | // plugin:press-this:stable-readme:original:title |
| 415 | // plugin:press-this:stable-readme:fr:title |
| 416 | $key = "plugin:{$slug}:{$branch}"; |
| 417 | if ( !empty( $suffix ) ) |
| 418 | $key .= ":{$suffix}"; |
| 419 | return $key; |
| 420 | } |
| 421 | |
| 422 | function cache_get( $slug, $branch, $suffix = null ) { |
| 423 | $key = $this->cache_key( $slug, $branch, $suffix ); |
| 424 | // DEBUG |
| 425 | // wp_cache_delete( $key, $this->i18n_cache_group ); |
| 426 | return wp_cache_get( $key, $this->i18n_cache_group ); |
| 427 | } |
| 428 | |
| 429 | function cache_set( $slug, $branch, $content, $suffix = null ) { |
| 430 | $key = $this->cache_key( $slug, $branch, $suffix ); |
| 431 | return wp_cache_set( $key, $content, $this->i18n_cache_group ); |
| 432 | } |
| 433 | |
| 434 | function get_gp_branch_id( $slug, $branch ) { |
| 435 | $cache_suffix = "branch_id"; |
| 436 | |
| 437 | if ( false !== ( $branch_id = $this->cache_get( $slug, $branch, $cache_suffix ) ) ) |
| 438 | return $branch_id; |
| 439 | |
| 440 | $branch_id = $this->db->get_var( $this->db->prepare( |
| 441 | 'SELECT id FROM translate_projects WHERE path = %s', |
| 442 | "wp-plugins/{$slug}/{$branch}" |
| 443 | ) ); |
| 444 | |
| 445 | if ( empty( $branch_id ) ) |
| 446 | $branch_id = 0; |
| 447 | |
| 448 | $this->cache_set( $slug, $branch, $branch_id, $cache_suffix ); |
| 449 | |
| 450 | return $branch_id; |
| 451 | } |
| 452 | |
| 453 | function get_gp_originals( $slug, $branch, $key, $str ) { |
| 454 | // Try to get a single original with the whole content first (title, etc), if passed, or get them all otherwise. |
| 455 | if ( !empty( $key ) && !empty( $str ) ) { |
| 456 | $originals = $this->search_gp_original( $slug, $branch, $key, $str ); |
| 457 | if ( !empty( $originals ) ) |
| 458 | return array( $originals ); |
| 459 | // Do not cache this as originals, search_gp_original() does its own caching |
| 460 | } |
| 461 | |
| 462 | $cache_suffix = 'originals'; |
| 463 | |
| 464 | if ( false !== ( $originals = $this->cache_get( $slug, $branch, $cache_suffix ) ) ) |
| 465 | return $originals; |
| 466 | |
| 467 | $branch_id = $this->get_gp_branch_id( $slug, $branch ); |
| 468 | |
| 469 | if ( empty( $branch_id ) ) |
| 470 | return array(); |
| 471 | |
| 472 | $originals = $this->db->get_results( $this->db->prepare( |
| 473 | 'SELECT id, singular, comment FROM translate_originals WHERE project_id = %d AND status = %s', |
| 474 | $branch_id, '+active' |
| 475 | ) ); |
| 476 | |
| 477 | if ( empty( $originals ) ) |
| 478 | $originals = array(); // still cache if empty, but as array, never false |
| 479 | |
| 480 | $this->cache_set( $slug, $branch, $originals, $cache_suffix ); |
| 481 | |
| 482 | return $originals; |
| 483 | } |
| 484 | |
| 485 | function get_gp_translation_set_id( $slug, $branch, $locale ) { |
| 486 | $cache_suffix = "{$locale}:translation_set_id"; |
| 487 | |
| 488 | if ( false !== ( $translation_set_id = $this->cache_get( $slug, $branch, $cache_suffix ) ) ) |
| 489 | return $translation_set_id; |
| 490 | |
| 491 | $branch_id = $this->get_gp_branch_id( $slug, $branch ); |
| 492 | |
| 493 | if ( empty( $branch_id ) ) |
| 494 | return 0; |
| 495 | |
| 496 | $translation_set_id = $this->db->get_var( $this->db->prepare( |
| 497 | 'SELECT id FROM translate_translation_sets WHERE project_id = %d AND locale = %s', |
| 498 | $branch_id, $locale ) ); |
| 499 | |
| 500 | if ( empty( $translation_set_id ) ) |
| 501 | $translation_set_id = 0; |
| 502 | |
| 503 | $this->cache_set( $slug, $branch, $translation_set_id, $cache_suffix ); |
| 504 | |
| 505 | return $translation_set_id; |
| 506 | } |
| 507 | |
| 508 | function search_gp_original( $slug, $branch, $key, $str ) { |
| 509 | $cache_suffix = "original:{$key}"; |
| 510 | |
| 511 | if ( false !== ( $original = $this->cache_get( $slug, $branch, $cache_suffix ) ) ) |
| 512 | return $original; |
| 513 | |
| 514 | $branch_id = $this->get_gp_branch_id( $slug, $branch ); |
| 515 | |
| 516 | if ( empty( $branch_id ) ) |
| 517 | return false; |
| 518 | |
| 519 | $original = $this->db->get_row( $this->db->prepare( |
| 520 | 'SELECT id, singular, comment FROM translate_originals WHERE project_id = %d AND status = %s AND singular = %s', |
| 521 | $branch_id, '+active', $str |
| 522 | ) ); |
| 523 | |
| 524 | if ( empty( $original ) ) |
| 525 | $original = null; |
| 526 | |
| 527 | $this->cache_set( $slug, $branch, $original, $cache_suffix ); |
| 528 | |
| 529 | return $original; |
| 530 | } |
| 531 | |
| 532 | function translate_gp_original( $original, $translation, $content) { |
| 533 | $content = str_replace( $original, $translation, $content ); |
| 534 | return $content; |
| 535 | } |
| 536 | } |
| 537 | No newline at end of file |