Ticket #25280: 25280.3.diff
File 25280.3.diff, 6.5 KB (added by , 8 years ago) |
---|
-
src/wp-includes/class.wp-scripts.php
diff --git src/wp-includes/class.wp-scripts.php src/wp-includes/class.wp-scripts.php index 81634ec..e718f82 100644
class WP_Scripts extends WP_Dependencies { 461 461 } 462 462 463 463 /** 464 * Pass data to a script if the script has already been added. 465 * 466 * @since 4.8.0 467 * @access public 468 * 469 * @param string $handle 470 * @param string $object_name 471 * @param array $data 472 * @return bool 473 */ 474 public function pass_data( $handle, $object_name, $data ) { 475 476 // Pass scalar values untouched; pass strings through html_entity_decode. 477 if ( is_scalar( $data ) ) { 478 479 // Pass strings through html_entity_decode, pass other scalar untouched. 480 if ( is_string( $data ) ) { 481 $data = html_entity_decode( $data, ENT_QUOTES, 'UTF-8'); 482 } 483 } else { 484 // Recurse over passed arrays. Cast objects to arrays. 485 foreach ( (array) $data as $key => $value ) { 486 487 // Pass scalar values untouched; pass strings through html_entity_decode. 488 if ( is_scalar( $value ) ) { 489 490 // Pass strings through html_entity_decode, pass other scalar values untouched. 491 if ( is_string( $value ) ) { 492 $data[ $key ] = html_entity_decode( $value, ENT_QUOTES, 'UTF-8'); 493 } else { 494 $data[$key] = $value; 495 } 496 } else { 497 498 $data = $this->entity_decode_array_values( $data ); 499 } 500 } 501 } 502 503 $script = "var $object_name = " . wp_json_encode( $data ) . ';'; 504 505 if ( ! empty( $after ) ) 506 $script .= "\n$after;"; 507 508 $data = $this->get_data( $handle, 'data' ); 509 510 if ( ! empty( $data ) ) 511 $script = "$data\n$script"; 512 513 return $this->add_data( $handle, 'data', $script ); 514 } 515 516 /** 517 * Handles recursively HTML entity decoding multi-dimensional array values. 518 * 519 * @since 4.8.0 520 * 521 * @param array $data_array Array of data which should be decoded. 522 * 523 * @return array Array with decoded values. 524 */ 525 public function entity_decode_array_values( $data_array ) { 526 527 // Begin a new array so that non-decoded values do not get added. 528 $data = array(); 529 530 foreach ( (array) $data_array as $key => $value ) { 531 if ( is_array( $value ) ) { 532 $data[ $key ] = $this->entity_decode_array_values( $value ); 533 } 534 535 // Non-array, non-scalar values should not be added. 536 if ( ! is_scalar( $value ) ) { 537 continue; 538 } 539 540 // Pass strings through html_entity_decode, pass other scalar untouched. 541 if ( is_string( $value ) ) { 542 $data[ $key ] = html_entity_decode( $value, ENT_QUOTES, 'UTF-8'); 543 } else { 544 $data[ $key ] = $value; 545 } 546 } 547 548 return $data; 549 } 550 551 /** 464 552 * Sets handle group. 465 553 * 466 554 * @since 2.8.0 -
src/wp-includes/functions.wp-scripts.php
diff --git src/wp-includes/functions.wp-scripts.php src/wp-includes/functions.wp-scripts.php index dccaaf2..50ae172 100644
function wp_localize_script( $handle, $object_name, $l10n ) { 193 193 } 194 194 195 195 /** 196 * Pass data from PHP to a registered JavaScript file. 197 * 198 * Accepts a scalar value or and associative array and creates a JavaScript object. 199 * 200 * @since 4.8.0 201 * 202 * @param string $handle Script handle the data will be attached to. 203 * @param string $object_name Name for the JavaScript object. Passed directly, so it should be qualified JS variable. 204 * @param array $data The data itself. The data can be either a scalar value, or a single or multi-dimensional array. 205 * 206 * @return bool True if the script was successfully localized, false otherwise. 207 */ 208 function wp_pass_data_to_script( $handle, $object_name, $data ) { 209 global $wp_scripts; 210 if ( ! ( $wp_scripts instanceof WP_Scripts ) ) { 211 _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ ); 212 return false; 213 } 214 215 return $wp_scripts->pass_data( $handle, $object_name, $data ); 216 } 217 218 /** 196 219 * Remove a registered script. 197 220 * 198 221 * Note: there are intentional safeguards in place to prevent critical admin scripts, -
tests/phpunit/tests/dependencies/scripts.php
diff --git tests/phpunit/tests/dependencies/scripts.php tests/phpunit/tests/dependencies/scripts.php index efe5a7b..6b21281 100644
class Tests_Dependencies_Scripts extends WP_UnitTestCase { 724 724 725 725 $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); 726 726 } 727 728 /** 729 * Testing `wp_pass_data_to_script` with scalar and array values. 730 * @ticket 25280 731 */ 732 function test_wp_pass_data_to_script_with_scalar_and_multidimensional_arrays() { 733 // Enqueue & localize variables 734 wp_enqueue_script( 'test-l10n-data', 'example.com', array(), null ); 735 wp_pass_data_to_script( 'test-l10n-data', 'data', true ); 736 737 $test_array = array( 'var' => 'string' ); 738 wp_pass_data_to_script( 'test-l10n-data', 'dataArray', $test_array ); 739 740 $test_multidimensional = array( 741 'first-level' => array( 742 'second-level' => array( 743 'index' => "I'll \"walk\" the <b>dog</b> now", 744 ), 745 ), 746 ); 747 wp_pass_data_to_script( 'test-l10n-data', 'dataMultiDimensionalArray', $test_multidimensional ); 748 749 750 // Boolean output. 751 $expected = "var data = true;\n"; 752 // One-dimensional array output. 753 $expected .= "var dataArray = {\"var\":\"string\"};\n"; 754 // Multi-dimensional array output with odd characters. 755 $string = '\"walk\"'; 756 $expected .= "var dataMultiDimensionalArray = {\"first-level\":{\"second-level\":{\"index\":\"I'll " . 757 $string . " the <b>dog<\/b> now\"}}};\n"; 758 // var script wrapper output 759 $expected = "<script type='text/javascript'>\n/* <![CDATA[ */\n$expected/* ]]> */\n</script>\n"; 760 // script link output 761 $expected .= "<script type='text/javascript' src='http://example.com'></script>\n"; 762 $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); 763 // No scripts left to print 764 $this->assertEquals( '', get_echo( 'wp_print_scripts' ) ); 765 } 766 767 /** 768 * Test passing data to scripts via `wp_pass_data_to_script`. 769 * 770 * @ticket 25280 771 */ 772 function test_wp_pass_data_to_script_doesnt_convert_types_unexpectedly() { 773 global $wp_scripts; 774 $array_to_pass = array( 775 'a_string' => __( 'Some String' ), 776 'a_number' => 10, 777 'a_boolean' => true, 778 ); 779 wp_enqueue_script( 'localize_test','localize_script.js', array(), '1.0.0', true ); 780 wp_pass_data_to_script( 'localize_test', 'localized_variable', $array_to_pass ); 781 $this->assertEquals( 782 'var localized_variable = {"a_string":"Some String","a_number":10,"a_boolean":true};', 783 $wp_scripts->print_extra_script( 'localize_test', false ) 784 ); 785 } 727 786 }