From 39639c79da854e9ab7860c50c3e5dec17dcabc0f Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Sun, 10 May 2015 19:56:15 +0000 Subject: [PATCH] Add a return value to `wp_register_script()` and `wp_register_style()` which matches the return value of `WP_Dependencies::add()`. Props katzwebdesign, pareshradadiya, DrewAPicture. Fixes #31126 git-svn-id: https://develop.svn.wordpress.org/trunk@32483 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class.wp-dependencies.php | 2 +- src/wp-includes/functions.wp-scripts.php | 6 +++++- src/wp-includes/functions.wp-styles.php | 4 +++- tests/phpunit/tests/dependencies/scripts.php | 11 +++++++++++ tests/phpunit/tests/dependencies/styles.php | 10 ++++++++++ 5 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/class.wp-dependencies.php b/src/wp-includes/class.wp-dependencies.php index 7995a908c8..239f8f260b 100644 --- a/src/wp-includes/class.wp-dependencies.php +++ b/src/wp-includes/class.wp-dependencies.php @@ -213,7 +213,7 @@ class WP_Dependencies { * @param array $deps Optional. An array of item handle strings on which this item depends. * @param string $ver Optional. Version (used for cache busting). * @param mixed $args Optional. Custom property of the item. NOT the class property $args. Examples: $media, $in_footer. - * @return bool True on success, false on failure. + * @return bool Whether the item has been registered. True on success, false on failure. */ public function add( $handle, $src, $deps = array(), $ver = false, $args = null ) { if ( isset($this->registered[$handle]) ) diff --git a/src/wp-includes/functions.wp-scripts.php b/src/wp-includes/functions.wp-scripts.php index c4be60c48a..f5328b09a3 100644 --- a/src/wp-includes/functions.wp-scripts.php +++ b/src/wp-includes/functions.wp-scripts.php @@ -94,6 +94,7 @@ function wp_print_scripts( $handles = false ) { * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts. * * @since 2.6.0 + * @since 4.3.0 A return value was added. * * @param string $handle Name of the script. Should be unique. * @param string $src Path to the script from the WordPress root directory. Example: '/js/myscript.js'. @@ -105,15 +106,18 @@ function wp_print_scripts( $handles = false ) { * If set to null, no version is added. Default 'false'. Accepts 'false', 'null', or 'string'. * @param bool $in_footer Optional. Whether to enqueue the script before or before . * Default 'false'. Accepts 'false' or 'true'. + * @return bool Whether the script has been registered. True on success, false on failure. */ function wp_register_script( $handle, $src, $deps = array(), $ver = false, $in_footer = false ) { $wp_scripts = wp_scripts(); _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ ); - $wp_scripts->add( $handle, $src, $deps, $ver ); + $registered = $wp_scripts->add( $handle, $src, $deps, $ver ); if ( $in_footer ) { $wp_scripts->add_data( $handle, 'group', 1 ); } + + return $registered; } /** diff --git a/src/wp-includes/functions.wp-styles.php b/src/wp-includes/functions.wp-styles.php index 76ca28fac0..8e38ce2ba0 100644 --- a/src/wp-includes/functions.wp-styles.php +++ b/src/wp-includes/functions.wp-styles.php @@ -98,6 +98,7 @@ function wp_add_inline_style( $handle, $data ) { * @link http://www.w3.org/TR/CSS2/media.html#media-types List of CSS media types. * * @since 2.6.0 + * @since 4.3.0 A return value was added. * * @param string $handle Name of the stylesheet. * @param string|bool $src Path to the stylesheet from the WordPress root directory. Example: '/css/mystyle.css'. @@ -107,11 +108,12 @@ function wp_add_inline_style( $handle, $data ) { * @param string $media Optional. The media for which this stylesheet has been defined. * Default 'all'. Accepts 'all', 'aural', 'braille', 'handheld', 'projection', 'print', * 'screen', 'tty', or 'tv'. + * @return bool Whether the style has been registered. True on success, false on failure. */ function wp_register_style( $handle, $src, $deps = array(), $ver = false, $media = 'all' ) { _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ ); - wp_styles()->add( $handle, $src, $deps, $ver, $media ); + return wp_styles()->add( $handle, $src, $deps, $ver, $media ); } /** diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index da46fc441b..73680df6c3 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -155,4 +155,15 @@ class Tests_Dependencies_Scripts extends WP_UnitTestCase { // No scripts left to print $this->assertEquals( '', get_echo( 'wp_print_scripts' ) ); } + + /** + * Testing 'wp_register_script' return boolean success/failure value. + * + * @ticket 31126 + */ + function test_wp_register_script() { + $this->assertTrue( wp_register_script( 'duplicate-handler', 'http://example.com' ) ); + $this->assertFalse( wp_register_script( 'duplicate-handler', 'http://example.com' ) ); + } + } diff --git a/tests/phpunit/tests/dependencies/styles.php b/tests/phpunit/tests/dependencies/styles.php index 27de285b3f..ebc23fd641 100644 --- a/tests/phpunit/tests/dependencies/styles.php +++ b/tests/phpunit/tests/dependencies/styles.php @@ -229,4 +229,14 @@ CSS; $this->assertEquals( $expected, get_echo( 'wp_print_styles' ) ); } + /** + * Testing 'wp_register_style' return boolean success/failure value. + * + * @ticket 31126 + */ + function test_wp_register_style(){ + $this->assertTrue( wp_register_style( 'duplicate-handler', 'http://example.com' ) ); + $this->assertFalse( wp_register_style( 'duplicate-handler', 'http://example.com' ) ); + } + }