Make WordPress Core

Ticket #40485: 40485.diff

File 40485.diff, 2.6 KB (added by desrosj, 8 years ago)

Adds wp_script_get_data() with unit tests.

  • src/wp-includes/functions.wp-scripts.php

     
    334334function wp_script_add_data( $handle, $key, $value ){
    335335        return wp_scripts()->add_data( $handle, $key, $value );
    336336}
     337
     338/**
     339 * Get metadata for a script.
     340 *
     341 * Works only if the script has already been added.
     342 *
     343 * Possible values for $key and $value:
     344 * 'data' array Localized script data.
     345 *
     346 * @since 4.8.0
     347 *
     348 * @see WP_Dependency::get_data()
     349 *
     350 * @param string $handle     Name of the script.
     351 * @param string $key        Name of data point for which we're retrieving a value.
     352 * @return bool|string|array False when the script is not registered, or data is not
     353 *                           stored in $key, string or array of data when it exists.
     354 */
     355function wp_script_get_data( $handle, $key ){
     356        return wp_scripts()->get_data( $handle, $key );
     357}
  • tests/phpunit/tests/dependencies/scripts.php

     
    179179        }
    180180
    181181        /**
     182         * Testing `wp_script_get_data` with data added through `wp_localize_script`.
     183         *
     184         * @ticket 40485
     185         */
     186        function test_wp_script_get_data_with_localized_data() {
     187                $data = array(
     188                        'key' => 'value',
     189                        'key2' => 'value',
     190                );
     191                $l10n = array();
     192
     193                wp_enqueue_script( 'test-get-data', 'example.com', array(), null );
     194                wp_localize_script( 'test-get-data', 'localized_data', $data );
     195
     196                // Simulate preparing data for localization.
     197                foreach ( (array) $data as $key => $value ) {
     198                        if ( ! is_scalar( $value ) )
     199                                continue;
     200
     201                        $l10n[ $key ] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8' );
     202                }
     203
     204                $localized_data = 'var localized_data = ' . wp_json_encode( $data ) . ';';
     205
     206                $this->assertEquals( $localized_data, wp_script_get_data( 'test-get-data', 'data' ) );
     207        }
     208
     209        /**
     210         * Testing `wp_script_get_data` for retrieving the conditional data key.
     211         *
     212         * @ticket 40485
     213         */
     214        function test_wp_script_get_data_with_conditional_key() {
     215                // Enqueue & add an invalid key
     216                wp_enqueue_script( 'test-get-date-conditional', 'example.com', array(), null );
     217                wp_script_add_data( 'test-get-date-conditional', 'conditional', 'lt IE 9' );
     218
     219                $this->assertEquals( 'lt IE 9', wp_script_get_data( 'test-get-date-conditional', 'conditional' ) );
     220        }
     221
     222        /**
    182223         * Testing 'wp_register_script' return boolean success/failure value.
    183224         *
    184225         * @ticket 31126