Ticket #14853: 14853.3.diff
File 14853.3.diff, 3.8 KB (added by , 11 years ago) |
---|
-
src/wp-includes/class.wp-scripts.php
58 58 return $this->print_extra_script( $handle, $echo ); 59 59 } 60 60 61 function print_extra_script( $handle, $echo = true ) {62 if ( ! $output = $this->get_data( $handle, 'data') )61 function print_extra_script( $handle, $echo = true, $data = 'data' ) { 62 if ( ! $output = $this->get_data( $handle, $data ) ) 63 63 return; 64 64 65 if ( !$echo ) 65 $output = implode( "\n", $output ); 66 67 if ( ! $echo ) 66 68 return $output; 67 69 68 70 echo "<script type='text/javascript'>\n"; // CDATA and type='text/javascript' is not needed for HTML 5 … … 119 121 120 122 $src = esc_url( apply_filters( 'script_loader_src', $src, $handle ) ); 121 123 122 if ( $this->do_concat ) 124 if ( $this->do_concat ) { 123 125 $this->print_html .= "<script type='text/javascript' src='$src'></script>\n"; 124 else 126 $this->print_html .= $this->print_extra_script( $handle, false, 'after' ); 127 } else { 125 128 echo "<script type='text/javascript' src='$src'></script>\n"; 129 $this->print_extra_script( $handle, true, 'after' ); 130 } 126 131 127 132 return true; 128 133 } 129 134 135 function add_inline_script( $handle, $code ) { 136 if ( ! $code ) 137 return false; 138 139 $after = $this->get_data( $handle, 'after' ); 140 if ( ! $after ) 141 $after = array(); 142 143 $after[] = $code; 144 145 return $this->add_data( $handle, 'after', $after ); 146 } 147 130 148 /** 131 149 * Localizes a script 132 150 * -
src/wp-includes/functions.wp-scripts.php
39 39 } 40 40 41 /** 42 * Adds extra javascript. 43 * 44 * Works only if the javascript has already been added. 45 * Accepts a string $data containing the javascript. If two or more javascript code blocks are 46 * added to the same stylesheet $handle, they will be printed in the order 47 * they were added, i.e. the latter added styles can redeclare the previous. 48 * 49 * Uses $wp_scripts 50 * 51 * @since 3.7.0 52 * @see WP_Scripts::add_inline_script() 53 * @param $handle string Handle of the script to put this inline script after 54 * @param $data string Script to put after the script of the $handle 55 */ 56 function wp_add_inline_script( $handle, $data ) { 57 global $wp_scripts; 58 if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) { 59 if ( ! did_action( 'init' ) ) 60 _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ), '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.7' ); 61 $wp_scripts = new WP_Scripts(); 62 } 63 64 return $wp_scripts->add_inline_script( $handle, $data ); 65 } 66 41 67 /** 42 68 * Register new Javascript file. 43 69 * 44 70 * @since r16 -
tests/tests/dependencies/scripts.php
42 42 } 43 43 44 /** 45 * Tests wp_add_inline_scripts 46 * @ticket 14853 47 */ 48 public function test_wp_add_inline_script() { 49 $expected = ''; 50 $ver = get_bloginfo( 'version' ); 51 52 wp_enqueue_script( 'handle', '//url' ); 53 wp_add_inline_script( 'handle', 'sometestdata' ); 54 $expected .= "<script type='text/javascript' src='//url?ver=$ver'></script>\n"; 55 $expected .= "<script type='text/javascript'>\n/* <![CDATA[ */\nsometestdata\n/* ]]> */\n</script>\n"; 56 57 $returned = get_echo( 'wp_print_scripts' ); 58 $this->assertEquals( $expected, $returned ); 59 } 60 44 61 /** 45 62 * Test the different protocol references in wp_enqueue_script 46 63 * @global WP_Scripts $wp_scripts 47 64 * @ticket 16560