Make WordPress Core

Ticket #32607: 32607.patch

File 32607.patch, 3.2 KB (added by tyxla, 10 years ago)

Introducing wp_get_registered_script() and wp_get_registered_style(). Including unit tests.

  • src/wp-includes/script-loader.php

     
    11171117                        $compress_css = false;
    11181118        }
    11191119}
     1120
     1121/**
     1122 * Get a registered style object.
     1123 * Returns `false` if the style has not been registered.
     1124 *
     1125 * @param string $handle Name of the style.
     1126 *
     1127 * @global WP_Styles $wp_styles
     1128 *
     1129 * @return _WP_Dependency|false
     1130 */
     1131function wp_get_registered_style( $handle ) {
     1132        global $wp_styles;
     1133
     1134        if ( isset( $wp_styles->registered[ $handle ] ) ) {
     1135                return $wp_styles->registered[ $handle ];
     1136        }
     1137
     1138        return false;
     1139}
     1140
     1141/**
     1142 * Get a registered script object.
     1143 * Returns `false` if the script has not been registered.
     1144 *
     1145 * @param string $handle Name of the script.
     1146 *
     1147 * @global WP_Scripts $wp_scripts
     1148 *
     1149 * @return _WP_Dependency|false
     1150 */
     1151function wp_get_registered_script( $handle ) {
     1152        global $wp_scripts;
     1153
     1154        if ( isset( $wp_scripts->registered[$handle] ) ) {
     1155                return $wp_scripts->registered[$handle];
     1156        }
     1157
     1158        return false;
     1159}
     1160 No newline at end of file
  • tests/phpunit/tests/dependencies/scripts.php

     
    166166        $this->assertFalse( wp_register_script( 'duplicate-handler', 'http://example.com' ) );
    167167    }
    168168
     169    /**
     170     * Testing 'wp_get_registered_script' return value.
     171     *
     172     * @ticket 32607
     173     */
     174    function test_wp_get_registered_script() {
     175        $this->assertFalse( wp_get_registered_script( 'handle' ) );
     176
     177        wp_register_script( 'handle', 'http://example.com', array( 'test-dep' ), 1 );
     178
     179        $object = wp_get_registered_script( 'handle' );
     180        $this->assertInstanceOf( '_WP_Dependency', $object );
     181        $this->assertEquals( 'handle', $object->handle );
     182        $this->assertEquals( 'http://example.com', $object->src );
     183        $this->assertEquals( array( 'test-dep' ), $object->deps );
     184        $this->assertEquals( 1, $object->ver );
     185    }
     186
    169187}
  • tests/phpunit/tests/dependencies/styles.php

     
    239239        $this->assertFalse( wp_register_style( 'duplicate-handler', 'http://example.com' ) );
    240240    }
    241241
     242    /**
     243     * Testing 'wp_get_registered_style' return value.
     244     *
     245     * @ticket 32607
     246     */
     247    function test_wp_get_registered_style() {
     248        $this->assertFalse( wp_get_registered_style( 'handle' ) );
     249
     250        wp_register_style( 'handle', 'http://example.com', array( 'test-dep' ), 1 );
     251
     252        $object = wp_get_registered_style( 'handle' );
     253        $this->assertInstanceOf( '_WP_Dependency', $object );
     254        $this->assertEquals( 'handle', $object->handle );
     255        $this->assertEquals( 'http://example.com', $object->src );
     256        $this->assertEquals( array( 'test-dep' ), $object->deps );
     257        $this->assertEquals( 1, $object->ver );
     258    }
     259
    242260}