Make WordPress Core

Ticket #22229: 22229.3.diff

File 22229.3.diff, 11.2 KB (added by nbachiyski, 11 years ago)
  • src/wp-includes/class.wp-dependencies.php

     
    201201        }
    202202
    203203        /**
     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        /**
    204217         * Register an item.
    205218         *
    206219         * Registers the item if no item of that name already exists.
     
    478491                return true;
    479492        }
    480493
     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        }
    481504} // _WP_Dependencies
  • src/wp-includes/class.wp-scripts.php

     
    1717 * @since r16
    1818 */
    1919class WP_Scripts extends WP_Dependencies {
     20
    2021        var $base_url; // Full URL with trailing slash
    2122        var $content_url;
    2223        var $default_version;
     
    9798                $src = $this->registered[$handle]->src;
    9899
    99100                if ( $this->do_concat ) {
     101                        if ( 'inline' == $src ) {
     102                                $this->print_code .= $this->print_extra_script( $handle, false );
     103                                return true;
     104                        }
    100105                        $srce = apply_filters( 'script_loader_src', $src, $handle );
    101106                        if ( $this->in_default_dir($srce) ) {
    102107                                $this->print_code .= $this->print_extra_script( $handle, false );
     
    110115                }
    111116
    112117                $this->print_extra_script( $handle );
     118
     119                if ( 'inline' == $src ) {
     120                        return true;
     121                }
     122
    113123                if ( !preg_match('|^(https?:)?//|', $src) && ! ( $this->content_url && 0 === strpos($src, $this->content_url) ) ) {
    114124                        $src = $this->base_url . $src;
    115125                }
     
    150160
    151161                        $l10n[$key] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8');
    152162                }
     163                $literals = WP_JS_Literal::find( $l10n );
    153164
    154                 $script = "var $object_name = " . json_encode($l10n) . ';';
     165                $this->add_deps_from_literals( $handle, $literals );
    155166
     167                $script = "var $object_name = " . WP_JS_Literal::json_encode( $l10n, $literals ) . ';';
     168
    156169                if ( !empty($after) )
    157170                        $script .= "\n$after;";
    158171
     
    161174                if ( !empty( $data ) )
    162175                        $script = "$data\n$script";
    163176
     177                $this->add_data( $handle, 'data', 'dudu' );
     178
    164179                return $this->add_data( $handle, 'data', $script );
    165180        }
    166181
     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
    167188        function set_group( $handle, $recursion, $group = false ) {
    168189
    169190                if ( $this->registered[$handle]->args === 1 )
     
    218239                $this->ext_handles = '';
    219240        }
    220241}
     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 */
     250class 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

     
    251251
    252252        return (bool) $wp_scripts->query( $handle, $list );
    253253}
     254
     255function 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

     
    275275        return esc_html( translate_with_gettext_context( $text, $context, $domain ) );
    276276}
    277277
     278function _n_js( $singular, $plural, $domain = 'default' ) {
     279        return translate_plural_for_js( $singular, $plural, null, $domain );
     280}
     281
     282function _nx_js( $singular, $plural, $context, $domain = 'default' ) {
     283        return translate_plural_for_js( $singular, $plural, $context, $domain );
     284}
     285
     286function 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
     299function wp_i18n_js_make_plural_defintion() {
     300        $json_encoded_plural_info = wp_i18n_get_json_encoded_domains_plural_info();
     301        return <<<JS
     302window.wp = window.wp || {};
     303window.wp.i18n = window.wp.i18n || {};
     304wp.i18n.domains_plural_info = $json_encoded_plural_info;
     305wp.i18n.english_plural_info = {nplurals: 2, choose: function(n) { return n != 1;}};
     306wp.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}
     314JS;
     315}
     316
     317function 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
    278330/**
    279331 * Retrieve the plural or single form based on the supplied amount.
    280332 *
  • src/wp-includes/pomo/translations.php

     
    142142         */
    143143        function gettext_select_plural_form($count) {
    144144                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();
    146146                        $this->_nplurals = $nplurals;
    147147                        $this->_gettext_select_plural_form = $this->make_plural_form_function($nplurals, $expression);
    148148                }
     
    159159                }
    160160        }
    161161
     162        function nplurals_and_expression() {
     163                $header = $this->get_header( 'Plural-Forms' );
     164                return $this->nplurals_and_expression_from_header( $header );
     165        }
     166
    162167        /**
    163168         * Makes a function, which will return the right translation index, according to the
    164169         * plural forms header
     
    229234
    230235if ( !class_exists( 'NOOP_Translations' ) ):
    231236/**
    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.
    233241 */
    234 class NOOP_Translations {
     242class NOOP_Translations extends Gettext_Translations {
    235243        var $entries = array();
    236244        var $headers = array();
    237245
  • src/wp-includes/script-loader.php

     
    7272                'time' => (string) time(),
    7373        ) );
    7474
     75        $scripts->add( 'i18n-make-plural', 'inline' );
     76        $scripts->add_data( 'i18n-make-plural', 'data', wp_i18n_js_make_plural_defintion() );
     77
    7578        $scripts->add( 'common', "/wp-admin/js/common$suffix.js", array('jquery', 'hoverIntent', 'utils'), false, 1 );
    7679        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' ),
    7883        ) );
    7984
    8085        $scripts->add( 'sack', "/wp-includes/js/tw-sack$suffix.js", array(), '1.6.1', 1 );
  • tests/phpunit/tests/dependencies.php

     
    136136                $this->assertFalse( $dep->query( 'one' ) );
    137137
    138138        }
     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        }
    139164}