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

git-svn-id: http://svn.automattic.com/wordpress/trunk@18480 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
azaozz 2011-07-28 18:24:00 +00:00
parent d18937d7d9
commit 23b9d7759a
5 changed files with 65 additions and 34 deletions

View File

@ -134,17 +134,39 @@ class WP_Dependencies {
/**
* Adds extra data
*
* Adds data only if script has already been added
* Adds data only if script has already been added.
*
* @param string $handle Script name
* @param string $data_name Name of object in which to store extra data
* @param array $data Array of extra data
* @param string $key
* @param mixed $value
* @return bool success
*/
function add_data( $handle, $data_name, $data ) {
if ( !isset($this->registered[$handle]) )
function add_data( $handle, $key, $value ) {
if ( !isset( $this->registered[$handle] ) )
return false;
return $this->registered[$handle]->add_data( $data_name, $data );
return $this->registered[$handle]->add_data( $key, $value );
}
/**
* Get extra data
*
* Gets data associated with a certain handle.
*
* @since WP 3.3
*
* @param string $handle Script name
* @param string $key
* @return mixed
*/
function get_data( $handle, $key ) {
if ( !isset( $this->registered[$handle] ) )
return false;
if ( !isset( $this->registered[$handle]->extra[$key] ) )
return false;
return $this->registered[$handle]->extra[$key];
}
function remove( $handles ) {

View File

@ -54,12 +54,8 @@ class WP_Scripts extends WP_Dependencies {
}
function print_script_data( $handle, $echo = true, $_l10n = false ) {
if ( empty($this->registered[$handle]->extra['data']) )
return false;
if ( $_l10n ) {
$name = $this->registered[$handle]->extra['l10n'][0];
$data = $this->registered[$handle]->extra['l10n'][1];
list( $name, $data ) = $this->get_data( $handle, 'l10n' );
$after = '';
if ( is_array($data) && isset($data['l10n_print_after']) ) {
@ -68,7 +64,12 @@ class WP_Scripts extends WP_Dependencies {
}
$output = "var $name = " . json_encode($data) . "; $after\n";
} else {
foreach ( (array) $this->registered[$handle]->extra['data'] as $name => $data ) {
$data = $this->get_data( $handle, 'data' );
if ( empty( $data ) )
return false;
foreach ( (array) $data as $name => $data ) {
$output = "var $name = " . json_encode($data) . ";\n";
}
}
@ -142,7 +143,6 @@ class WP_Scripts extends WP_Dependencies {
*
* Localizes only if script has already been added
*
* @since
* @deprecated WP 3.3
*/
function localize( $handle, $object_name, $l10n ) {
@ -157,21 +157,24 @@ class WP_Scripts extends WP_Dependencies {
*
* @param string $handle Script name
* @param string $name Name of JS object to hold the data
* @param array $data Associative array of JS name => value
* @param array $args Associative array of JS object attributes
* @return bool Successful or not
*/
function add_script_data( $handle, $name, $data ) {
if ( !$name || !is_array($data) )
function add_script_data( $handle, $name, $args ) {
if ( !$name || !is_array( $args ) )
return false;
if ( !empty( $this->registered[$handle]->extra['data'][$name] ) )
$data = array_merge( $data, (array) $this->registered[$handle]->extra['data'][$name] );
$data = $this->get_data( $handle, 'data' );
return $this->add_data( $handle, 'data', array( $name => $data ) );
if ( !empty( $data[$name] ) )
$args = array_merge( $data[$name], $args );
return $this->add_data( $handle, 'data', array( $name => $args ) );
}
function set_group( $handle, $recursion, $group = false ) {
$grp = isset($this->registered[$handle]->extra['group']) ? (int) $this->registered[$handle]->extra['group'] : 0;
$grp = (int) $this->get_data( $handle, 'group' );
if ( false !== $group && $grp > $group )
$grp = $group;

View File

@ -50,8 +50,7 @@ class WP_Styles extends WP_Dependencies {
$this->concat .= "$handle,";
$this->concat_version .= "$handle$ver";
if ( !empty($this->registered[$handle]->extra['data']) )
$this->print_code .= $this->registered[$handle]->extra['data'];
$this->print_code .= $this->get_data( $handle, 'after' );
return true;
}
@ -97,21 +96,26 @@ class WP_Styles extends WP_Dependencies {
return true;
}
function add_inline_style( $handle, $data ) {
if ( !$data )
function add_inline_style( $handle, $code ) {
if ( !$code )
return false;
if ( !empty( $this->registered[$handle]->extra['data'] ) )
$data .= "\n" . $this->registered[$handle]->extra['data'];
$after = $this->get_data( $handle, 'after' );
if ( !$after )
$after = array();
return $this->add_data( $handle, 'data', $data );
$after[] = $code;
return $this->add_data( $handle, 'after', $after );
}
function print_inline_style( $handle, $echo = true ) {
if ( empty($this->registered[$handle]->extra['data']) )
$output = $this->get_data( $handle, 'after' );
if ( empty( $output ) )
return false;
$output = $this->registered[$handle]->extra['data'];
$output = implode( "\n", $output );
if ( !$echo )
return $output;

View File

@ -66,9 +66,9 @@ function wp_register_script( $handle, $src, $deps = array(), $ver = false, $in_f
* ...
* }
* The $name is passed directly so it should be qualified JS variable /[a-zA-Z0-9_]+/
* The $data array is JSON encoded. If called more than once for the same $handle, with the same $name,
* The $data array is JSON encoded. If called more than once for the same $handle with the same $name,
* the object would contain all values. In that case if two or more keys are the same,
* the first value is kept and subsequent values are ignored.
* the last value overwrites the previous.
*
* @since 3.3
* @see WP_Scripts::add_script_data()

View File

@ -37,7 +37,9 @@ function wp_print_styles( $handles = false ) {
* Adds extra CSS.
*
* Works only if the stylesheet has already been added.
* Accepts a string $data containing the CSS.
* Accepts a string $data containing the CSS. If two or more CSS code blocks are
* added to the same stylesheet $handle, they will be printed in the order
* they were added, i.e. the latter added styles can redeclare the previous.
*
* @since 3.3
* @see WP_Scripts::add_inline_style()