Make WordPress Core

Ticket #11520: 11520-6.patch

File 11520-6.patch, 18.1 KB (added by azaozz, 13 years ago)
  • wp-admin/js/wp-fullscreen.dev.js

     
    138138                        return;
    139139
    140140                // Settings can be added or changed by defining "wp_fullscreen_settings" JS object.
    141                 // This can be done by defining it as PHP array and passing it to JS with:
    142                 // add_script_data( 'wp-fullscreen', 'wp_fullscreen_settings', $settings_array )
     141                // This can be done by defining it as PHP associative array, json encoding it and passing it to JS with:
     142                // add_script_extra( 'wp-fullscreen', 'wp_fullscreen_settings = ' . $json_encoded_array . ';' );
    143143                if ( typeof(wp_fullscreen_settings) == 'object' )
    144144                        $.extend( s, wp_fullscreen_settings );
    145145
  • wp-includes/class.wp-scripts.php

     
    4747                return $this->do_items( $handles, $group );
    4848        }
    4949
    50         // Deprecated since 3.3, see print_script_data()
     50        // Deprecated since 3.3, see print_script_extra()
    5151        function print_scripts_l10n( $handle, $echo = true ) {
    52                 _deprecated_function( __FUNCTION__, '3.3', 'print_script_data()' );
    53                 return $this->print_script_data( $handle, $echo, true );
     52                _deprecated_function( __FUNCTION__, '3.3', 'print_script_extra()' );
     53                return $this->print_script_extra( $handle, $echo );
    5454        }
    5555
    56         function print_script_data( $handle, $echo = true, $_l10n = false ) {
    57                 if ( $_l10n ) {
    58                         list( $name, $data ) = $this->get_data( $handle, 'l10n' );
    59                         $after = '';
     56        function print_script_extra( $handle, $echo = true ) {
     57                if ( !$output = $this->get_data( $handle, 'data' ) )
     58                        return;
    6059
    61                         if ( is_array($data) && isset($data['l10n_print_after']) ) {
    62                                 $after = $data['l10n_print_after'];
    63                                 unset($data['l10n_print_after']);
    64                         }
    65 
    66                         $data = $this->decode_html_entities($data);
    67                         $output = "var $name = " . json_encode( $data ) . "; $after\n";
    68                 } else {
    69                         $data = $this->get_data( $handle, 'data' );
    70 
    71                         if ( empty( $data ) )
    72                                 return false;
    73 
    74                         foreach ( (array) $data as $name => $value ) {
    75                                 $value = $this->decode_html_entities($value);
    76                                 $output = "var $name = " . json_encode( $value ) . ";\n";
    77                         }
    78                 }
    79 
    8060                if ( !$echo )
    8161                        return $output;
    8262
    83                 echo "<script type='text/javascript'>\n";
    84                 echo "/* <![CDATA[ */\n"; // CDATA is not needed for HTML 5
    85                 echo $output;
     63                echo "<script type='text/javascript'>\n"; // CDATA and type='text/javascript' is not needed for HTML 5
     64                echo "/* <![CDATA[ */\n";
     65                echo "$output\n";
    8666                echo "/* ]]> */\n";
    8767                echo "</script>\n";
    8868
     
    11494                if ( $this->do_concat ) {
    11595                        $srce = apply_filters( 'script_loader_src', $src, $handle );
    11696                        if ( $this->in_default_dir($srce) ) {
    117                                 $this->print_code .= $this->print_script_data( $handle, false );
     97                                $this->print_code .= $this->print_script_extra( $handle, false );
    11898                                $this->concat .= "$handle,";
    11999                                $this->concat_version .= "$handle$ver";
    120100                                return true;
     
    124104                        }
    125105                }
    126106
    127                 $this->print_script_data( $handle );
     107                $this->print_script_extra( $handle );
    128108                if ( !preg_match('|^https?://|', $src) && ! ( $this->content_url && 0 === strpos($src, $this->content_url) ) ) {
    129109                        $src = $this->base_url . $src;
    130110                }
    131111
    132112                if ( !empty($ver) )
    133113                        $src = add_query_arg('ver', $ver, $src);
    134                 $src = esc_url(apply_filters( 'script_loader_src', $src, $handle ));
    135114
     115                $src = esc_url( apply_filters( 'script_loader_src', $src, $handle ) );
     116
    136117                if ( $this->do_concat )
    137118                        $this->print_html .= "<script type='text/javascript' src='$src'></script>\n";
    138119                else
     
    142123        }
    143124
    144125        /**
    145          * Localizes a script (Deprecated)
     126         * Localizes a script
    146127         *
    147128         * Localizes only if script has already been added
    148          *
    149          * @deprecated WP 3.3
    150129         */
    151130        function localize( $handle, $object_name, $l10n ) {
    152                 _deprecated_function( __FUNCTION__, '3.3', 'add_script_data()' );
    153                 return $this->add_script_data( $handle, $object_name, $l10n );
     131                if ( is_array($l10n) && isset($l10n['l10n_print_after']) ) { // back compat, preserve the code in 'l10n_print_after' if present
     132                        $after = $l10n['l10n_print_after'];
     133                        unset($l10n['l10n_print_after']);
     134                }
     135
     136                foreach ( (array) $l10n as $key => $value ) {
     137                        if ( !is_scalar($value) )
     138                                continue;
     139
     140                        $l10n[$key] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8');
     141                }
     142
     143                $data = "var $object_name = " . json_encode($l10n) . ';';
     144
     145                if ( !empty($after) )
     146                        $data .= "\n$after";
     147
     148                return $this->add_script_extra( $handle, $data );
    154149        }
    155150
    156151        /**
     
    159154         * Only if script has already been added.
    160155         *
    161156         * @param string $handle Script name
    162          * @param string $name Name of JS object to hold the data
    163          * @param array $args Associative array of JS object attributes
     157         * @param string $extra Extra JS to add before the script
    164158         * @return bool Successful or not
    165159         */
    166         function add_script_data( $handle, $name, $args ) {
    167                 if ( !$name || !is_array( $args ) )
     160        function add_script_extra( $handle, $extra ) {
     161                if ( !is_string( $extra ) )
    168162                        return false;
    169163
    170164                $data = $this->get_data( $handle, 'data' );
    171165
    172                 if ( !empty( $data[$name] ) )
    173                         $args = array_merge( $data[$name], $args );
     166                if ( !empty( $data ) )
     167                        $extra = "$data;\n$extra";
    174168
    175                 return $this->add_data( $handle, 'data', array( $name => $args ) );
     169                return $this->add_data( $handle, 'data', $extra );
    176170        }
    177171
    178172        function set_group( $handle, $recursion, $group = false ) {
     
    219213                return false;
    220214        }
    221215
    222         function decode_html_entities($data) {
    223                 foreach ( (array) $data as $key => $value ) {
    224                         if ( is_array($value) )
    225                                 $data[$key] = $this->decode_html_entities($value);
    226                         elseif ( is_string($value) )
    227                                 $data[$key] = html_entity_decode($value, ENT_QUOTES, 'UTF-8');
    228                 }
    229                 return $data;
    230         }
    231 
    232216        function reset() {
    233217                $this->do_concat = false;
    234218                $this->print_code = '';
  • wp-includes/functions.wp-scripts.php

     
    6464}
    6565
    6666/**
    67  * Adds extra Javascript data.
     67 * Wrapper for $wp_scripts->localize().
    6868 *
     69 * Used to localizes a script.
    6970 * Works only if the script has already been added.
    70  * Accepts an associative array $data and creates JS object:
    71  * "$name" = {
     71 * Accepts an associative array $l10n and creates JS object:
     72 * "$object_name" = {
    7273 *   key: value,
    7374 *   key: value,
    7475 *   ...
    7576 * }
    76  * The $name is passed directly so it should be qualified JS variable /[a-zA-Z0-9_]+/
    77  * The $data array is JSON encoded. If called more than once for the same $handle with the same $name,
    78  * the object would contain all values. In that case if two or more keys are the same,
    79  * the last value overwrites the previous. The function is named "localize_script" because of historical reasons.
     77 * See http://core.trac.wordpress.org/ticket/11520 for more information.
    8078 *
    8179 * @since r16
    82  * @see WP_Scripts::add_script_data()
     80 *
     81 * @param string $handle The script handle that was registered or used in script-loader
     82 * @param string $object_name Name for the created JS object. This is passed directly so it should be qualified JS variable /[a-zA-Z0-9_]+/
     83 * @param array $l10n Associative PHP array containing the translated strings. HTML entities will be converted and the array will be JSON encoded.
     84 * @return bool Whether the localization was added successfully.
    8385 */
    84 function wp_localize_script( $handle, $name, $data ) {
     86function wp_localize_script( $handle, $object_name, $l10n ) {
    8587        global $wp_scripts;
    8688        if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
    8789                if ( ! did_action( 'init' ) )
    8890                        _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
    8991                                '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
     92
    9093                return false;
    9194        }
    9295
    93         return $wp_scripts->add_script_data( $handle, $name, $data );
     96        return $wp_scripts->localize( $handle, $object_name, $l10n );
     97}
     98
     99/**
     100 * Adds extra Javascript.
     101 *
     102 * Works only if the script referenced by $handle has already been added.
     103 * Accepts string $extra that will be printed as script before the main script tag.
     104 *
     105 * @since 3.3
     106 * @see WP_Scripts::add_script_extra()
     107 */
     108function wp_add_script_extra( $handle, $extra ) {
     109        global $wp_scripts;
     110        if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
     111                if ( ! did_action( 'init' ) )
     112                        _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
     113                                '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
     114                return false;
     115        }
     116
     117        return $wp_scripts->add_script_extra( $handle, $extra );
    94118}
    95119
    96120/**
  • wp-includes/script-loader.php

     
    6262        $scripts->add( 'utils', "/wp-admin/js/utils$suffix.js", false, '20101110' );
    6363
    6464        $scripts->add( 'common', "/wp-admin/js/common$suffix.js", array('jquery', 'hoverIntent', 'utils'), '20111106a', 1 );
    65         $scripts->add_script_data( 'common', 'commonL10n', array(
     65        $scripts->localize( 'common', 'commonL10n', array(
    6666                'warnDelete' => __("You are about to permanently delete the selected items.\n  'Cancel' to stop, 'OK' to delete.")
    6767        ) );
    6868
    6969        $scripts->add( 'sack', "/wp-includes/js/tw-sack$suffix.js", false, '1.6.1', 1 );
    7070
    7171        $scripts->add( 'quicktags', "/wp-includes/js/quicktags$suffix.js", false, '20111105', 1 );
    72         $scripts->add_script_data( 'quicktags', 'quicktagsL10n', array(
     72        $scripts->localize( 'quicktags', 'quicktagsL10n', array(
    7373                'wordLookup' => __('Enter a word to look up:'),
    7474                'dictionaryLookup' => esc_attr(__('Dictionary lookup')),
    7575                'lookup' => esc_attr(__('lookup')),
     
    9191        $scripts->add( 'prototype', '/wp-includes/js/prototype.js', false, '1.6.1');
    9292
    9393        $scripts->add( 'wp-ajax-response', "/wp-includes/js/wp-ajax-response$suffix.js", array('jquery'), '20091119', 1 );
    94         $scripts->add_script_data( 'wp-ajax-response', 'wpAjax', array(
     94        $scripts->localize( 'wp-ajax-response', 'wpAjax', array(
    9595                'noPerm' => __('You do not have permission to do that.'),
    9696                'broken' => __('An unidentified error has occurred.')
    9797        ) );
    9898
    9999        $scripts->add( 'wp-pointer', "/wp-includes/js/wp-pointer$suffix.js", array( 'jquery-ui-widget', 'jquery-ui-position' ), '20111017', 1 );
    100         $scripts->add_script_data( 'wp-pointer', 'wpPointerL10n', array(
     100        $scripts->localize( 'wp-pointer', 'wpPointerL10n', array(
    101101                'close' => __('Close'),
    102102        ) );
    103103
     
    166166        $scripts->add( 'jquery-table-hotkeys', "/wp-includes/js/jquery/jquery.table-hotkeys$suffix.js", array('jquery', 'jquery-hotkeys'), '20090102', 1 );
    167167
    168168        $scripts->add( 'thickbox', "/wp-includes/js/thickbox/thickbox.js", array('jquery'), '3.1-20110930', 1 );
    169         $scripts->add_script_data( 'thickbox', 'thickboxL10n', array(
     169        $scripts->localize( 'thickbox', 'thickboxL10n', array(
    170170                        'next' => __('Next &gt;'),
    171171                        'prev' => __('&lt; Prev'),
    172172                        'image' => __('Image'),
     
    221221        $scripts->add( 'plupload-all', false, array('plupload', 'plupload-html5', 'plupload-flash', 'plupload-silverlight', 'plupload-html4'), '1511');
    222222
    223223        $scripts->add( 'plupload-handlers', "/wp-includes/js/plupload/handlers$suffix.js", array('plupload-all', 'jquery'), '20111102');
    224         $scripts->add_script_data( 'plupload-handlers', 'pluploadL10n', $uploader_l10n );
     224        $scripts->localize( 'plupload-handlers', 'pluploadL10n', $uploader_l10n );
    225225
    226226        // keep 'swfupload' for back-compat.
    227227        $scripts->add( 'swfupload', '/wp-includes/js/swfupload/swfupload.js', false, '2201-20110113');
     
    237237        }
    238238
    239239        $scripts->add( 'swfupload-handlers', "/wp-includes/js/swfupload/handlers$suffix.js", array('swfupload-all', 'jquery'), '2201-20110524');
    240         $scripts->add_script_data( 'swfupload-handlers', 'swfuploadL10n', $uploader_l10n );
     240        $scripts->localize( 'swfupload-handlers', 'swfuploadL10n', $uploader_l10n );
    241241
    242242        $scripts->add( 'comment-reply', "/wp-includes/js/comment-reply$suffix.js", false, '20090102');
    243243
     
    246246        $scripts->add( 'imgareaselect', "/wp-includes/js/imgareaselect/jquery.imgareaselect$suffix.js", array('jquery'), '0.9.6-20110515', 1 );
    247247
    248248        $scripts->add( 'password-strength-meter', "/wp-admin/js/password-strength-meter$suffix.js", array('jquery'), '20101027', 1 );
    249         $scripts->add_script_data( 'password-strength-meter', 'pwsL10n', array(
     249        $scripts->localize( 'password-strength-meter', 'pwsL10n', array(
    250250                'empty' => __('Strength indicator'),
    251251                'short' => __('Very weak'),
    252252                'bad' => __('Weak'),
     
    261261        $scripts->add( 'admin-bar', "/wp-includes/js/admin-bar$suffix.js", false, '20111107', 1 );
    262262
    263263        $scripts->add( 'wplink', "/wp-includes/js/wplink$suffix.js", array( 'jquery', 'wpdialogs' ), '20110929', 1 );
    264         $scripts->add_script_data( 'wplink', 'wpLinkL10n', array(
     264        $scripts->localize( 'wplink', 'wpLinkL10n', array(
    265265                'title' => __('Insert/edit link'),
    266266                'update' => __('Update'),
    267267                'save' => __('Add Link'),
     
    280280        if ( is_admin() ) {
    281281                $scripts->add( 'ajaxcat', "/wp-admin/js/cat$suffix.js", array( 'wp-lists' ), '20090102' );
    282282                $scripts->add_data( 'ajaxcat', 'group', 1 );
    283                 $scripts->add_script_data( 'ajaxcat', 'catL10n', array(
     283                $scripts->localize( 'ajaxcat', 'catL10n', array(
    284284                        'add' => esc_attr(__('Add')),
    285285                        'how' => __('Separate multiple categories with commas.')
    286286                ) );
     
    288288                $scripts->add( 'admin-categories', "/wp-admin/js/categories$suffix.js", array('wp-lists'), '20091201', 1 );
    289289
    290290                $scripts->add( 'admin-tags', "/wp-admin/js/tags$suffix.js", array('jquery', 'wp-ajax-response'), '20110429', 1 );
    291                 $scripts->add_script_data( 'admin-tags', 'tagsl10n', array(
     291                $scripts->localize( 'admin-tags', 'tagsl10n', array(
    292292                        'noPerm' => __('You do not have permission to do that.'),
    293293                        'broken' => __('An unidentified error has occurred.')
    294294                ));
     
    296296                $scripts->add( 'admin-custom-fields', "/wp-admin/js/custom-fields$suffix.js", array('wp-lists'), '20110429', 1 );
    297297
    298298                $scripts->add( 'admin-comments', "/wp-admin/js/edit-comments$suffix.js", array('wp-lists', 'quicktags', 'jquery-query'), '20111026', 1 );
    299                 $scripts->add_script_data( 'admin-comments', 'adminCommentsL10n', array(
     299                $scripts->localize( 'admin-comments', 'adminCommentsL10n', array(
    300300                        'hotkeys_highlight_first' => isset($_GET['hotkeys_highlight_first']),
    301301                        'hotkeys_highlight_last' => isset($_GET['hotkeys_highlight_last']),
    302302                        'replyApprove' => __( 'Approve and Reply' ),
     
    308308                $scripts->add( 'postbox', "/wp-admin/js/postbox$suffix.js", array('jquery-ui-sortable'), '20111009a', 1 );
    309309
    310310                $scripts->add( 'post', "/wp-admin/js/post$suffix.js", array('suggest', 'wp-lists', 'postbox'), '20110524', 1 );
    311                 $scripts->add_script_data( 'post', 'postL10n', array(
     311                $scripts->localize( 'post', 'postL10n', array(
    312312                        'ok' => __('OK'),
    313313                        'cancel' => __('Cancel'),
    314314                        'publishOn' => __('Publish on:'),
     
    333333
    334334                $scripts->add( 'comment', "/wp-admin/js/comment$suffix.js", array('jquery'), '20110429' );
    335335                $scripts->add_data( 'comment', 'group', 1 );
    336                 $scripts->add_script_data( 'comment', 'commentL10n', array(
     336                $scripts->localize( 'comment', 'commentL10n', array(
    337337                        'submittedOn' => __('Submitted on:')
    338338                ) );
    339339
     
    346346                $scripts->add( 'theme-preview', "/wp-admin/js/theme-preview$suffix.js", array( 'thickbox', 'jquery' ), '20100407', 1 );
    347347
    348348                $scripts->add( 'inline-edit-post', "/wp-admin/js/inline-edit-post$suffix.js", array( 'jquery', 'suggest' ), '20111107', 1 );
    349                 $scripts->add_script_data( 'inline-edit-post', 'inlineEditL10n', array(
     349                $scripts->localize( 'inline-edit-post', 'inlineEditL10n', array(
    350350                        'error' => __('Error while saving the changes.'),
    351351                        'ntdeltitle' => __('Remove From Bulk Edit'),
    352352                        'notitle' => __('(no title)')
    353353                ) );
    354354
    355355                $scripts->add( 'inline-edit-tax', "/wp-admin/js/inline-edit-tax$suffix.js", array( 'jquery' ), '20110609', 1 );
    356                 $scripts->add_script_data( 'inline-edit-tax', 'inlineEditL10n', array(
     356                $scripts->localize( 'inline-edit-tax', 'inlineEditL10n', array(
    357357                        'error' => __('Error while saving the changes.')
    358358                ) );
    359359
    360360                $scripts->add( 'plugin-install', "/wp-admin/js/plugin-install$suffix.js", array( 'jquery', 'thickbox' ), '20110113', 1 );
    361                 $scripts->add_script_data( 'plugin-install', 'plugininstallL10n', array(
     361                $scripts->localize( 'plugin-install', 'plugininstallL10n', array(
    362362                        'plugin_information' => __('Plugin Information:'),
    363363                        'ays' => __('Are you sure you want to install this plugin?')
    364364                ) );
     
    374374                $scripts->add( 'media', "/wp-admin/js/media$suffix.js", array( 'jquery-ui-draggable' ), '20101022', 1 );
    375375
    376376                $scripts->add( 'image-edit', "/wp-admin/js/image-edit$suffix.js", array('jquery', 'json2', 'imgareaselect'), '20110927', 1 );
    377                 $scripts->add_script_data( 'image-edit', 'imageEditL10n', array(
     377                $scripts->localize( 'image-edit', 'imageEditL10n', array(
    378378                        'error' => __( 'Could not load the preview image. Please reload the page and try again.' )
    379379                ));
    380380
    381381                $scripts->add( 'set-post-thumbnail', "/wp-admin/js/set-post-thumbnail$suffix.js", array( 'jquery' ), '20100518', 1 );
    382                 $scripts->add_script_data( 'set-post-thumbnail', 'setPostThumbnailL10n', array(
     382                $scripts->localize( 'set-post-thumbnail', 'setPostThumbnailL10n', array(
    383383                        'setThumbnail' => __( 'Use as featured image' ),
    384384                        'saving' => __( 'Saving...' ),
    385385                        'error' => __( 'Could not set that as the thumbnail image. Try a different attachment.' ),
     
    388388
    389389                // Navigation Menus
    390390                $scripts->add( 'nav-menu', "/wp-admin/js/nav-menu$suffix.js", array('jquery-ui-sortable'), '20110524' );
    391                 $scripts->add_script_data( 'nav-menu', 'navMenuL10n', array(
     391                $scripts->localize( 'nav-menu', 'navMenuL10n', array(
    392392                        'noResultsFound' => _x('No results found.', 'search results'),
    393393                        'warnDeleteMenu' => __( "You are about to permanently delete this menu. \n 'Cancel' to stop, 'OK' to delete." ),
    394394                        'saveAlert' => __('The changes you made will be lost if you navigate away from this page.')