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