Changeset 3511 for sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-plugin-i18n.php
- Timestamp:
- 06/20/2016 05:34:18 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-plugin-i18n.php
r3468 r3511 14 14 public $i18n_cache_group = 'plugins-i18n'; 15 15 16 /** 17 * @var string 18 */ 16 19 public $master_project; 17 20 21 /** 22 * @static 23 * 24 * @var bool 25 */ 18 26 public static $use_cache = true; 27 28 /** 29 * @static 30 * 31 * @var bool 32 */ 19 33 public static $set_cache = true; 20 34 21 35 /** 36 * @access protected 37 * 22 38 * @var \wpdb WordPress database abstraction object. 23 39 */ … … 26 42 /** 27 43 * Fetch the instance of the Plugin_I18n class. 44 * 45 * @static 28 46 */ 29 47 public static function instance() { … … 31 49 32 50 global $wpdb; 51 33 52 return ! is_null( $instance ) ? $instance : $instance = new Plugin_I18n( $wpdb ); 34 53 } 35 54 36 function __construct( $db, $tracker = null ) { 37 if ( !empty( $db ) && is_object( $db ) ) { 55 /** 56 * Plugin_I18n constructor. 57 * 58 * @access private 59 * 60 * @param \wpdb $db WordPress database abstraction object. 61 * @param null $tracker 62 */ 63 private function __construct( $db, $tracker = null ) { 64 if ( ! empty( $db ) && is_object( $db ) ) { 38 65 $this->db = $db; 39 66 } … … 43 70 44 71 /** 45 * Generates and returns a standard cache key format, for consistency 46 * 47 * @param string $slug Plugin slug 48 * @param string $branch dev|stable 49 * @param string $suffix Arbitrary cache key suffix, if needed for uniqueness 50 * 72 * Generates and returns a standard cache key format, for consistency. 73 * 74 * @param string $slug Plugin slug 75 * @param string $branch dev|stable 76 * @param string $suffix Optional. Arbitrary cache key suffix, if needed for uniqueness. 51 77 * @return string Cache key 52 78 */ 53 function cache_key( $slug, $branch, $suffix = null ) { 54 // EG keys 55 // plugin:press-this:stable-readme:originals 56 // plugin:press-this:stable-readme:original:title 57 // plugin:press-this:stable-readme:fr:title 79 public function cache_key( $slug, $branch, $suffix = '' ) { 80 81 /* 82 * EG keys 83 * plugin:press-this:stable-readme:originals 84 * plugin:press-this:stable-readme:original:title 85 * plugin:press-this:stable-readme:fr:title 86 */ 58 87 $key = "{$this->master_project}:{$slug}:{$branch}"; 59 if ( ! empty( $suffix ) ) {88 if ( ! empty( $suffix ) ) { 60 89 $key .= ":{$suffix}"; 61 90 } 91 62 92 return $key; 63 93 } 64 94 65 95 /** 66 * Cache getting, with proper global cache group 67 * 68 * @param string $slug Plugin slug 69 * @param string $branch dev|stable 70 * @param string $suffix Arbitrary cache key suffix, if needed for uniqueness 71 * 96 * Cache getting, with proper global cache group. 97 * 98 * @param string $slug Plugin slug 99 * @param string $branch dev|stable 100 * @param string $suffix Optional. Arbitrary cache key suffix, if needed for uniqueness. 72 101 * @return bool|mixed As returned by wp_cache_set() 73 102 */ 74 function cache_get( $slug, $branch, $suffix = null) {103 public function cache_get( $slug, $branch, $suffix = '' ) { 75 104 if ( ! self::$use_cache ) { 76 105 return false; … … 78 107 79 108 $key = $this->cache_key( $slug, $branch, $suffix ); 109 80 110 return wp_cache_get( $key, $this->i18n_cache_group ); 81 111 } … … 88 118 * @param mixed $content Content to be cached. 89 119 * @param string $suffix Optional. Arbitrary cache key suffix, if needed for uniqueness. 90 *91 120 * @return bool As returned by wp_cache_set() 92 121 */ 93 function cache_set( $slug, $branch, $content, $suffix = null) {122 public function cache_set( $slug, $branch, $content, $suffix = '' ) { 94 123 if ( ! self::$set_cache ) { 95 124 return false; … … 97 126 98 127 $key = $this->cache_key( $slug, $branch, $suffix ); 128 99 129 return wp_cache_set( $key, $content, $this->i18n_cache_group ); 100 130 } 101 131 102 132 /** 103 * Gets a GlotPress branch ID 104 * 105 * @param string $slug Plugin slug133 * Gets a GlotPress branch ID. 134 * 135 * @param string $slug Plugin slug 106 136 * @param string $branch dev|stable 107 137 * 108 138 * @return bool|int|mixed 109 139 */ 110 function get_gp_branch_id( $slug, $branch ) {140 public function get_gp_branch_id( $slug, $branch ) { 111 141 $cache_suffix = "branch_id"; 112 142 … … 130 160 131 161 /** 132 * Gets GlotPress "originals" based on passed parameters 133 * 134 * @param string $slug Plugin slug 135 * @param string $branch dev|stable 136 * @param string $key Unique key 137 * @param string $str String to match in GP 138 * 162 * Gets GlotPress "originals" based on passed parameters. 163 * 164 * @param string $slug Plugin slug 165 * @param string $branch dev|stable 166 * @param string $key Unique key 167 * @param string $str String to match in GP 139 168 * @return array|bool|mixed|null 140 169 */ 141 function get_gp_originals( $slug, $branch, $key, $str ) { 170 public function get_gp_originals( $slug, $branch, $key, $str ) { 171 142 172 // Try to get a single original with the whole content first (title, etc), if passed, or get them all otherwise. 143 if ( ! empty( $key ) && !empty( $str ) ) {173 if ( ! empty( $key ) && ! empty( $str ) ) { 144 174 $originals = $this->search_gp_original( $slug, $branch, $key, $str ); 145 if ( !empty( $originals ) ) { 175 176 // Do not cache this as originals, search_gp_original() does its own caching. 177 if ( ! empty( $originals ) ) { 146 178 return array( $originals ); 147 179 } 148 // Do not cache this as originals, search_gp_original() does its own caching149 180 } 150 181 … … 167 198 168 199 if ( empty( $originals ) ) { 169 $originals = array(); // still cache if empty, but as array, never false 200 201 // Still cache if empty, but as array, never false. 202 $originals = array(); 170 203 } 171 204 … … 176 209 177 210 /** 178 * Get GlotPress translation set ID based on passed params 179 * 180 * @param string $slug Plugin slug211 * Get GlotPress translation set ID based on passed params. 212 * 213 * @param string $slug Plugin slug 181 214 * @param string $branch dev|stable 182 215 * @param string $locale EG: fr 183 *184 216 * @return bool|int|mixed 185 217 */ 186 function get_gp_translation_set_id( $slug, $branch, $locale ) {218 public function get_gp_translation_set_id( $slug, $branch, $locale ) { 187 219 $cache_suffix = "{$locale}:translation_set_id"; 188 220 … … 202 234 203 235 if ( empty( $translation_set_id ) ) { 236 204 237 // Don't give up yet. Might be given fr_FR, which actually exists as locale=fr in GP. 205 238 $translation_set_id = $this->db->get_var( $this->db->prepare( 206 239 'SELECT id FROM ' . GLOTPRESS_TABLE_PREFIX . 'translation_sets WHERE project_id = %d AND locale = %s', 207 $branch_id, preg_replace( '/^([^-]+)(-.+)?$/', '\1', $locale ) ) ); 240 $branch_id, preg_replace( '/^([^-]+)(-.+)?$/', '\1', $locale ) 241 ) ); 208 242 } 209 243 … … 218 252 219 253 /** 220 * Searches GlotPress "originals" for the passed string 221 * 222 * @param string $slug Plugin slug 223 * @param string $branch dev|stable 224 * @param string $key Unique key 225 * @param string $str String to be searched for 226 * 254 * Searches GlotPress "originals" for the passed string. 255 * 256 * @param string $slug Plugin slug 257 * @param string $branch dev|stable 258 * @param string $key Unique key 259 * @param string $str String to be searched for 227 260 * @return bool|mixed|null 228 261 */ 229 function search_gp_original( $slug, $branch, $key, $str ) {262 public function search_gp_original( $slug, $branch, $key, $str ) { 230 263 $cache_suffix = "original:{$key}"; 231 264 … … 255 288 256 289 /** 257 * Somewhat emulated equivalent of __() for content translation drawn directly from the GlotPress DB 258 * 259 * @param string $key Unique key, used for caching290 * Somewhat emulated equivalent of __() for content translation drawn directly from the GlotPress DB. 291 * 292 * @param string $key Unique key, used for caching 260 293 * @param string $content Content to be translated 261 * @param array $args Misc arguments, such as BBPress topic id (otherwise acquired from global $topic_id)262 * 263 * @return mixed264 */ 265 function translate( $key, $content, $args = array() ) {294 * @param array $args Optional. Misc arguments, such as BBPress topic id 295 * (otherwise acquired from global $topic_id). 296 * @return string 297 */ 298 public function translate( $key, $content, $args = array() ) { 266 299 if ( empty( $key ) || empty( $content ) ) { 267 300 return $content; … … 285 318 } 286 319 287 $server_name = strtolower( $_SERVER['SERVER_NAME'] );320 $server_name = strtolower( $_SERVER['SERVER_NAME'] ); 288 321 if ( 'api.wordpress.org' == $server_name ) { 289 // Support formats like fr, haz, and en_GB 322 323 // Support formats like fr, haz, and en_GB. 290 324 if ( ! empty( $_REQUEST['locale'] ) ) { 291 325 $wp_locale = preg_replace( '/[^a-zA-Z_]/', '', $_REQUEST['locale'] ); … … 309 343 } 310 344 311 $locale = $gp_locale->slug; // The slug is the locale of a translation set. 312 $slug = $post->post_name; 345 // The slug is the locale of a translation set. 346 $locale = $gp_locale->slug; 347 $slug = $post->post_name; 313 348 314 349 $post->stable_tag = get_post_meta( $post->ID, 'stable_tag', true ); … … 326 361 $cache_suffix = "{$locale}:{$key}"; 327 362 328 // Try the cache 363 // Try the cache. 329 364 if ( false !== ( $cache = $this->cache_get( $slug, $branch, $cache_suffix ) ) ) { 330 365 // DEBUG … … 368 403 369 404 /** 370 * Takes content, searches for $original, and replaces it by $translation 371 * 372 * @param string $original English string 373 * @param string $translation Translation 374 * @param string $content Content to be searched 375 * 405 * Takes content, searches for $original, and replaces it by $translation. 406 * 407 * @param string $original English string. 408 * @param string $translation Translation. 409 * @param string $content Content to be searched. 376 410 * @return mixed 377 411 */ 378 function translate_gp_original( $original, $translation, $content ) {412 public function translate_gp_original( $original, $translation, $content ) { 379 413 if ( false === strpos( $content, '<' ) ) { 380 414 $content = str_replace( $original, $translation, $content ); 381 415 } else { 382 416 $original = preg_quote( $original, '/' ); 383 $content = preg_replace( "/(<([a-z0-9]*)\b[^>]*>){$original}(<\/\\2>)/m", "\\1{$translation}\\3", $content );417 $content = preg_replace( "/(<([a-z0-9]*)\b[^>]*>){$original}(<\/\\2>)/m", "\\1{$translation}\\3", $content ); 384 418 } 385 419
Note: See TracChangeset
for help on using the changeset viewer.