From 23b9d7759ad58a956ba8c89c9ffd340c1e1248a7 Mon Sep 17 00:00:00 2001 From: azaozz Date: Thu, 28 Jul 2011 18:24:00 +0000 Subject: [PATCH] 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 --- wp-includes/class.wp-dependencies.php | 34 ++++++++++++++++++++++----- wp-includes/class.wp-scripts.php | 31 +++++++++++++----------- wp-includes/class.wp-styles.php | 26 +++++++++++--------- wp-includes/functions.wp-scripts.php | 4 ++-- wp-includes/functions.wp-styles.php | 4 +++- 5 files changed, 65 insertions(+), 34 deletions(-) diff --git a/wp-includes/class.wp-dependencies.php b/wp-includes/class.wp-dependencies.php index 6cba02d4a..3907082eb 100644 --- a/wp-includes/class.wp-dependencies.php +++ b/wp-includes/class.wp-dependencies.php @@ -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 ) { diff --git a/wp-includes/class.wp-scripts.php b/wp-includes/class.wp-scripts.php index 0648ce512..381f2d109 100644 --- a/wp-includes/class.wp-scripts.php +++ b/wp-includes/class.wp-scripts.php @@ -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; diff --git a/wp-includes/class.wp-styles.php b/wp-includes/class.wp-styles.php index 88dff8119..6455fe473 100644 --- a/wp-includes/class.wp-styles.php +++ b/wp-includes/class.wp-styles.php @@ -46,12 +46,11 @@ class WP_Styles extends WP_Dependencies { $ver = $ver ? $ver . '&' . $this->args[$handle] : $this->args[$handle]; if ( $this->do_concat ) { - if ( $this->in_default_dir($obj->src) && !isset($obj->extra['conditional']) && !isset($obj->extra['alt']) ) { + if ( $this->in_default_dir($obj->src) && !isset($obj->extra['conditional']) && !isset($obj->extra['alt']) ) { $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; @@ -151,7 +155,7 @@ class WP_Styles extends WP_Dependencies { } return false; } - + function do_footer_items() { // HTML 5 allows styles in the body, grab late enqueued items and output them in the footer. $this->do_items(false, 1); return $this->done; diff --git a/wp-includes/functions.wp-scripts.php b/wp-includes/functions.wp-scripts.php index 6add0416e..d20d63745 100644 --- a/wp-includes/functions.wp-scripts.php +++ b/wp-includes/functions.wp-scripts.php @@ -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() diff --git a/wp-includes/functions.wp-styles.php b/wp-includes/functions.wp-styles.php index c5ae3c96c..3ce08679a 100644 --- a/wp-includes/functions.wp-styles.php +++ b/wp-includes/functions.wp-styles.php @@ -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()