Ticket #22229: 22229.3.diff
File 22229.3.diff, 11.2 KB (added by , 11 years ago) |
---|
-
src/wp-includes/class.wp-dependencies.php
201 201 } 202 202 203 203 /** 204 * Adds dependencies 205 * @param string $handle Script name 206 * @param array|string $deps Either an array of handles to become dependancies, or a single string dependancy 207 * @rerurn bool success 208 */ 209 function add_deps( $handle, $deps ) { 210 if ( !isset( $this->registered[$handle] ) ) { 211 return false; 212 } 213 return $this->registered[$handle]->add_deps( $deps ); 214 } 215 216 /** 204 217 * Register an item. 205 218 * 206 219 * Registers the item if no item of that name already exists. … … 478 491 return true; 479 492 } 480 493 494 function add_deps( $deps ) { 495 if ( is_string( $deps ) ) { 496 $deps = array( $deps ); 497 } 498 if ( !is_array( $deps ) ) { 499 return false; 500 } 501 $this->deps = array_merge( $this->deps, $deps ); 502 return true; 503 } 481 504 } // _WP_Dependencies -
src/wp-includes/class.wp-scripts.php
17 17 * @since r16 18 18 */ 19 19 class WP_Scripts extends WP_Dependencies { 20 20 21 var $base_url; // Full URL with trailing slash 21 22 var $content_url; 22 23 var $default_version; … … 97 98 $src = $this->registered[$handle]->src; 98 99 99 100 if ( $this->do_concat ) { 101 if ( 'inline' == $src ) { 102 $this->print_code .= $this->print_extra_script( $handle, false ); 103 return true; 104 } 100 105 $srce = apply_filters( 'script_loader_src', $src, $handle ); 101 106 if ( $this->in_default_dir($srce) ) { 102 107 $this->print_code .= $this->print_extra_script( $handle, false ); … … 110 115 } 111 116 112 117 $this->print_extra_script( $handle ); 118 119 if ( 'inline' == $src ) { 120 return true; 121 } 122 113 123 if ( !preg_match('|^(https?:)?//|', $src) && ! ( $this->content_url && 0 === strpos($src, $this->content_url) ) ) { 114 124 $src = $this->base_url . $src; 115 125 } … … 150 160 151 161 $l10n[$key] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8'); 152 162 } 163 $literals = WP_JS_Literal::find( $l10n ); 153 164 154 $ script = "var $object_name = " . json_encode($l10n) . ';';165 $this->add_deps_from_literals( $handle, $literals ); 155 166 167 $script = "var $object_name = " . WP_JS_Literal::json_encode( $l10n, $literals ) . ';'; 168 156 169 if ( !empty($after) ) 157 170 $script .= "\n$after;"; 158 171 … … 161 174 if ( !empty( $data ) ) 162 175 $script = "$data\n$script"; 163 176 177 $this->add_data( $handle, 'data', 'dudu' ); 178 164 179 return $this->add_data( $handle, 'data', $script ); 165 180 } 166 181 182 private function add_deps_from_literals( $handle, $literals ) { 183 foreach( $literals as $literal ) { 184 $this->add_deps( $handle, $literal->get_deps() ); 185 } 186 } 187 167 188 function set_group( $handle, $recursion, $group = false ) { 168 189 169 190 if ( $this->registered[$handle]->args === 1 ) … … 218 239 $this->ext_handles = ''; 219 240 } 220 241 } 242 243 /** 244 * Represents a literal JS text, which should not be JSON-encoded 245 * 246 * Used when localizing a script to tell the script loader not to encode 247 * this specific values and to load any additional dependancies this piece 248 * of code might have. 249 */ 250 class WP_JS_Literal { 251 private $text; 252 private $deps; 253 254 public function __construct( $js_text, $deps = array() ) { 255 $this->text = $js_text; 256 if ( is_string( $deps ) ) { 257 $this->deps = array( $deps ); 258 } elseif ( is_array( $deps ) ) { 259 $this->deps = $deps; 260 } else { 261 $deps = array(); 262 } 263 } 264 265 public function get_text() { 266 return $this->text; 267 } 268 269 public function get_deps() { 270 return $this->deps; 271 } 272 273 static public function json_encode( $value, $literals = null ) { 274 if ( !$literals ) { 275 $literals = self::find( $value ); 276 } 277 $encoded = json_encode( $value ); 278 return self::replace_literals_with_their_texts( $literals, $encoded ); 279 } 280 281 private static function replace_literals_with_their_texts( $literals, $json_encoded ) { 282 $replaces_from = array(); 283 $replaces_to = array(); 284 foreach( $literals as $literal ) { 285 $replaces_from[] = json_encode( $literal ); 286 $replaces_to[] = $literal->get_text(); 287 } 288 return str_replace( $replaces_from, $replaces_to, $json_encoded ); 289 } 290 291 public static function find( $value ) { 292 if ( !is_array( $value ) && !is_object( $value ) ) { 293 return array(); 294 } 295 if ( is_a( $value, 'WP_JS_Literal' ) ) { 296 return array( $value ); 297 } 298 $literals = array(); 299 foreach( $value as &$item ) { 300 $literals += self::find( $item ); 301 } 302 return $literals; 303 } 304 } -
src/wp-includes/functions.wp-scripts.php
251 251 252 252 return (bool) $wp_scripts->query( $handle, $list ); 253 253 } 254 255 function wp_js_make_literal( $js_text, $deps = array() ) { 256 $literal = new WP_JS_Literal( $js_text, $deps ); 257 return $literal; 258 } -
src/wp-includes/l10n.php
275 275 return esc_html( translate_with_gettext_context( $text, $context, $domain ) ); 276 276 } 277 277 278 function _n_js( $singular, $plural, $domain = 'default' ) { 279 return translate_plural_for_js( $singular, $plural, null, $domain ); 280 } 281 282 function _nx_js( $singular, $plural, $context, $domain = 'default' ) { 283 return translate_plural_for_js( $singular, $plural, $context, $domain ); 284 } 285 286 function translate_plural_for_js( $singular, $plural, $context, $domain ) { 287 $translations = get_translations_for_domain( $domain ); 288 $entry = new Translation_Entry( array( 'singular' => $singular, 'plural' => $plural, 'context' => $context ) ); 289 if ( $translated = $translations->translate_entry( $entry ) ) { 290 $js_translations = json_encode( $translated->translations ); 291 $js_domain = esc_js( $domain ); 292 return wp_js_make_literal( "wp.i18n.make_plural($js_translations, '$js_domain')", 'i18n-make-plural' ); 293 } else { 294 $js_originals = json_encode( array( $singular, $plural ) ); 295 return wp_js_make_literal( "wp.i18n.make_plural($js_originals)", 'i18n-make-plural' ); 296 } 297 } 298 299 function wp_i18n_js_make_plural_defintion() { 300 $json_encoded_plural_info = wp_i18n_get_json_encoded_domains_plural_info(); 301 return <<<JS 302 window.wp = window.wp || {}; 303 window.wp.i18n = window.wp.i18n || {}; 304 wp.i18n.domains_plural_info = $json_encoded_plural_info; 305 wp.i18n.english_plural_info = {nplurals: 2, choose: function(n) { return n != 1;}}; 306 wp.i18n.make_plural = function(translations, domain) { 307 var domain_plural_info = wp.i18n.domains_plural_info[domain] || wp.i18n.english_plural_info; 308 return function(n) { 309 var i = domain_plural_info['choose'](n); 310 if (typeof i === 'boolean') i = i ? 1 : 0; 311 return i >=0 && i < domain_plural_info['nplurals']? translations[i] : null; 312 } 313 } 314 JS; 315 } 316 317 function wp_i18n_get_json_encoded_domains_plural_info() { 318 global $l10n; 319 $domains_plural_info = array(); 320 foreach( $l10n as $domain => $translations ) { 321 list( $nplurals, $expression ) = $translations->nplurals_and_expression(); 322 $domains_plural_info[$domain] = array( 323 'nplurals' => $nplurals, 324 'choose' => wp_js_make_literal( "function(n) { return $expression; }" ), 325 ); 326 } 327 return WP_JS_Literal::json_encode( $domains_plural_info ); 328 } 329 278 330 /** 279 331 * Retrieve the plural or single form based on the supplied amount. 280 332 * -
src/wp-includes/pomo/translations.php
142 142 */ 143 143 function gettext_select_plural_form($count) { 144 144 if (!isset($this->_gettext_select_plural_form) || is_null($this->_gettext_select_plural_form)) { 145 list( $nplurals, $expression ) = $this->nplurals_and_expression _from_header($this->get_header('Plural-Forms'));145 list( $nplurals, $expression ) = $this->nplurals_and_expression(); 146 146 $this->_nplurals = $nplurals; 147 147 $this->_gettext_select_plural_form = $this->make_plural_form_function($nplurals, $expression); 148 148 } … … 159 159 } 160 160 } 161 161 162 function nplurals_and_expression() { 163 $header = $this->get_header( 'Plural-Forms' ); 164 return $this->nplurals_and_expression_from_header( $header ); 165 } 166 162 167 /** 163 168 * Makes a function, which will return the right translation index, according to the 164 169 * plural forms header … … 229 234 230 235 if ( !class_exists( 'NOOP_Translations' ) ): 231 236 /** 232 * Provides the same interface as Translations, but doesn't do anything 237 * Provides the same interface as Gettext_Translations, but doesn't do anything 238 * 239 * Used for domains without any translations to make sure we neither have special cases, 240 * nor look for translations on every translate call. 233 241 */ 234 class NOOP_Translations {242 class NOOP_Translations extends Gettext_Translations { 235 243 var $entries = array(); 236 244 var $headers = array(); 237 245 -
src/wp-includes/script-loader.php
72 72 'time' => (string) time(), 73 73 ) ); 74 74 75 $scripts->add( 'i18n-make-plural', 'inline' ); 76 $scripts->add_data( 'i18n-make-plural', 'data', wp_i18n_js_make_plural_defintion() ); 77 75 78 $scripts->add( 'common', "/wp-admin/js/common$suffix.js", array('jquery', 'hoverIntent', 'utils'), false, 1 ); 76 79 did_action( 'init' ) && $scripts->localize( 'common', 'commonL10n', array( 77 'warnDelete' => __("You are about to permanently delete the selected items.\n 'Cancel' to stop, 'OK' to delete.") 80 'warnDelete' => __("You are about to permanently delete the selected items.\n 'Cancel' to stop, 'OK' to delete."), 81 'baba' => _n_js( 'baba', 'babas' ), 82 'dudu' => _nx_js( 'dudu', 'dudus', 'cataxta' ), 78 83 ) ); 79 84 80 85 $scripts->add( 'sack', "/wp-includes/js/tw-sack$suffix.js", array(), '1.6.1', 1 ); -
tests/phpunit/tests/dependencies.php
136 136 $this->assertFalse( $dep->query( 'one' ) ); 137 137 138 138 } 139 140 function test_add_deps_should_add_one_string_dep() { 141 $dep = new WP_Dependencies; 142 $dep->add( 'baba', '', array( 'dep0' ) ); 143 $dep->add_deps( 'baba', 'new-dep' ); 144 $this->assertEquals( array( 'dep0', 'new-dep' ), $dep->query( 'baba' )->deps ); 145 } 146 147 function test_add_deps_should_merge_deps() { 148 $dep = new WP_Dependencies; 149 $dep->add( 'baba', '', array( 'dep0' ) ); 150 $dep->add_deps( 'baba', array( 'new-dep', 'another-dep' ) ); 151 $this->assertEquals( array( 'dep0', 'new-dep', 'another-dep' ), $dep->query( 'baba' )->deps ); 152 } 153 154 function test_add_deps_should_return_false_on_non_string_non_array_deps() { 155 $dep = new WP_Dependencies; 156 $dep->add( 'baba', '', array( 'dep0' ) ); 157 $this->assertFalse( $dep->add_deps( 'baba', 5 ) ); 158 } 159 160 function test_add_deps_should_return_false_on_non_registered_handle() { 161 $dep = new WP_Dependencies; 162 $this->assertFalse( $dep->add_deps( 'baba', 'dep0' ) ); 163 } 139 164 }