Make WordPress Core

Changeset 18480


Ignore:
Timestamp:
07/28/2011 06:24:00 PM (13 years ago)
Author:
azaozz
Message:

Introduce WP_Dependencies::get_data() method, change scripts and styles priority to follow the "natural" order in HTML, i.e. the last one wins, props scribu, see #11520

Location:
trunk/wp-includes
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/class.wp-dependencies.php

    r18446 r18480  
    135135     * Adds extra data
    136136     *
    137      * Adds data only if script has already been added
     137     * Adds data only if script has already been added.
    138138     *
    139139     * @param string $handle Script name
    140      * @param string $data_name Name of object in which to store extra data
    141      * @param array $data Array of extra data
     140     * @param string $key
     141     * @param mixed $value
    142142     * @return bool success
    143143     */
    144     function add_data( $handle, $data_name, $data ) {
    145         if ( !isset($this->registered[$handle]) )
    146             return false;
    147         return $this->registered[$handle]->add_data( $data_name, $data );
     144    function add_data( $handle, $key, $value ) {
     145        if ( !isset( $this->registered[$handle] ) )
     146            return false;
     147
     148        return $this->registered[$handle]->add_data( $key, $value );
     149    }
     150
     151    /**
     152     * Get extra data
     153     *
     154     * Gets data associated with a certain handle.
     155     *
     156     * @since WP 3.3
     157     *
     158     * @param string $handle Script name
     159     * @param string $key
     160     * @return mixed
     161     */
     162    function get_data( $handle, $key ) {
     163        if ( !isset( $this->registered[$handle] ) )
     164            return false;
     165
     166        if ( !isset( $this->registered[$handle]->extra[$key] ) )
     167            return false;
     168
     169        return $this->registered[$handle]->extra[$key];
    148170    }
    149171
  • trunk/wp-includes/class.wp-scripts.php

    r18464 r18480  
    5555
    5656    function print_script_data( $handle, $echo = true, $_l10n = false ) {
    57         if ( empty($this->registered[$handle]->extra['data']) )
    58             return false;
    59 
    6057        if ( $_l10n ) {
    61             $name = $this->registered[$handle]->extra['l10n'][0];
    62             $data = $this->registered[$handle]->extra['l10n'][1];
     58            list( $name, $data ) = $this->get_data( $handle, 'l10n' );
    6359            $after = '';
    6460
     
    6965            $output = "var $name = " . json_encode($data) . "; $after\n";
    7066        } else {
    71             foreach ( (array) $this->registered[$handle]->extra['data'] as $name => $data ) {
     67            $data = $this->get_data( $handle, 'data' );
     68
     69            if ( empty( $data ) )
     70                return false;
     71
     72            foreach ( (array) $data as $name => $data ) {
    7273                $output = "var $name = " . json_encode($data) . ";\n";
    7374            }
     
    143144     * Localizes only if script has already been added
    144145     *
    145      * @since
    146146     * @deprecated WP 3.3
    147147     */
     
    158158     * @param string $handle Script name
    159159     * @param string $name Name of JS object to hold the data
    160      * @param array $data Associative array of JS name => value
     160     * @param array $args Associative array of JS object attributes
    161161     * @return bool Successful or not
    162162     */
    163     function add_script_data( $handle, $name, $data ) {
    164         if ( !$name || !is_array($data) )
    165             return false;
    166 
    167         if ( !empty( $this->registered[$handle]->extra['data'][$name] ) )
    168             $data = array_merge( $data, (array) $this->registered[$handle]->extra['data'][$name] );
    169 
    170         return $this->add_data( $handle, 'data', array( $name => $data ) );
     163    function add_script_data( $handle, $name, $args ) {
     164        if ( !$name || !is_array( $args ) )
     165            return false;
     166
     167        $data = $this->get_data( $handle, 'data' );
     168
     169        if ( !empty( $data[$name] ) )
     170            $args = array_merge( $data[$name], $args );
     171
     172        return $this->add_data( $handle, 'data', array( $name => $args ) );
    171173    }
    172174
    173175    function set_group( $handle, $recursion, $group = false ) {
    174         $grp = isset($this->registered[$handle]->extra['group']) ? (int) $this->registered[$handle]->extra['group'] : 0;
     176        $grp = (int) $this->get_data( $handle, 'group' );
     177
    175178        if ( false !== $group && $grp > $group )
    176179            $grp = $group;
  • trunk/wp-includes/class.wp-styles.php

    r18464 r18480  
    4747
    4848        if ( $this->do_concat ) {
    49             if ( $this->in_default_dir($obj->src) && !isset($obj->extra['conditional']) && !isset($obj->extra['alt']) ) {
     49            if ( $this->in_default_dir($obj->src) && !isset($obj->extra['conditional']) && !isset($obj->extra['alt']) ) {
    5050                $this->concat .= "$handle,";
    5151                $this->concat_version .= "$handle$ver";
    5252
    53                 if ( !empty($this->registered[$handle]->extra['data']) )
    54                     $this->print_code .= $this->registered[$handle]->extra['data'];
     53                $this->print_code .= $this->get_data( $handle, 'after' );
    5554
    5655                return true;
     
    9897    }
    9998
    100     function add_inline_style( $handle, $data ) {
    101         if ( !$data )
     99    function add_inline_style( $handle, $code ) {
     100        if ( !$code )
    102101            return false;
    103102
    104         if ( !empty( $this->registered[$handle]->extra['data'] ) )
    105             $data .= "\n" . $this->registered[$handle]->extra['data'];
     103        $after = $this->get_data( $handle, 'after' );
     104        if ( !$after )
     105            $after = array();
    106106
    107         return $this->add_data( $handle, 'data', $data );
     107        $after[] = $code;
     108
     109        return $this->add_data( $handle, 'after', $after );
    108110    }
    109111
    110112    function print_inline_style( $handle, $echo = true ) {
    111         if ( empty($this->registered[$handle]->extra['data']) )
     113        $output = $this->get_data( $handle, 'after' );
     114
     115        if ( empty( $output ) )
    112116            return false;
    113117
    114         $output = $this->registered[$handle]->extra['data'];
     118        $output = implode( "\n", $output );
    115119
    116120        if ( !$echo )
     
    152156        return false;
    153157    }
    154    
     158
    155159    function do_footer_items() { // HTML 5 allows styles in the body, grab late enqueued items and output them in the footer.
    156160        $this->do_items(false, 1);
  • trunk/wp-includes/functions.wp-scripts.php

    r18464 r18480  
    6767 * }
    6868 * The $name is passed directly so it should be qualified JS variable /[a-zA-Z0-9_]+/
    69  * The $data array is JSON encoded. If called more than once for the same $handle, with the same $name,
     69 * The $data array is JSON encoded. If called more than once for the same $handle with the same $name,
    7070 * the object would contain all values. In that case if two or more keys are the same,
    71  * the first value is kept and subsequent values are ignored.
     71 * the last value overwrites the previous.
    7272 *
    7373 * @since 3.3
  • trunk/wp-includes/functions.wp-styles.php

    r18464 r18480  
    3838 *
    3939 * Works only if the stylesheet has already been added.
    40  * Accepts a string $data containing the CSS.
     40 * Accepts a string $data containing the CSS. If two or more CSS code blocks are
     41 * added to the same stylesheet $handle, they will be printed in the order
     42 * they were added, i.e. the latter added styles can redeclare the previous.
    4143 *
    4244 * @since 3.3
Note: See TracChangeset for help on using the changeset viewer.