Ticket #25247: 25247.diff
| File 25247.diff, 7.9 KB (added by , 10 years ago) |
|---|
-
src/wp-includes/class.wp-dependencies.php
100 100 if ( !in_array($handle, $this->done, true) && isset($this->registered[$handle]) ) { 101 101 /* 102 102 * Attempt to process the item. If successful, 103 * add the handle to the done array. 104 * 105 * Unset the item from the to_do array. 103 * add the handle to the done array and unset the item from the to_do array. 106 104 */ 107 if ( $this->do_item( $handle, $group ) ) 105 if ( $this->do_item( $handle, $group ) ) { 108 106 $this->done[] = $handle; 109 110 unset( $this->to_do[$key] );107 unset( $this->to_do[$key] ); 108 } 111 109 } 112 110 } 113 111 … … 385 383 $group = (int) $group; 386 384 387 385 if ( $recursion ) 388 $ group = min($this->group, $group);386 $this->group = $group = min($this->group, $group); 389 387 else 390 388 $this->group = $group; 391 389 -
src/wp-includes/class.wp-scripts.php
20 20 public $base_url; // Full URL with trailing slash 21 21 public $content_url; 22 22 public $default_version; 23 public $in_footer = array();24 23 public $concat = ''; 25 24 public $concat_version = ''; 26 25 public $do_concat = false; … … 117 116 return false; 118 117 119 118 if ( 0 === $group && $this->groups[$handle] > 0 ) { 120 $this->in_footer[] = $handle;121 119 return false; 122 120 } 123 121 124 if ( false === $group && in_array($handle, $this->in_footer, true) )125 $this->in_footer = array_diff( $this->in_footer, (array) $handle );126 127 122 $obj = $this->registered[$handle]; 128 123 129 124 if ( null === $obj->ver ) { … … 218 213 } 219 214 220 215 /** 216 * Register a script. 217 * 218 * Registers the script if no script of that name already exists. 219 * 220 * @access public 221 * @since 4.5 222 * 223 * @param string $handle Unique script name. 224 * @param string $src The script url. 225 * @param array $deps Optional. An array of script handle strings on which this script depends. 226 * @param string $ver Optional. Version (used for cache busting). 227 * @param mixed $args Optional. Custom property of the script. NOT the class property $args. Examples: 1 for in_footer. 228 * @return bool True on success, false on failure. 229 */ 230 public function add( $handle, $src, $deps = array(), $ver = false, $args = null ) { 231 $in_footer = ( $args === 1 ); 232 233 $result = parent::add( $handle, $src, $deps, $ver, $in_footer ? null : $args ); 234 235 if ( $in_footer && $result ) { 236 $this->add_data( $handle, 'group', 1 ); 237 } 238 239 return $result; 240 } 241 242 /** 221 243 * Localizes a script, only if the script has already been added 222 244 * 223 245 * @param string $handle … … 261 283 * @return bool Not already in the group or a lower group 262 284 */ 263 285 public function set_group( $handle, $recursion, $group = false ) { 264 if ( isset( $this->registered[$handle]->args ) && $this->registered[$handle]->args === 1 ) 265 $grp = 1; 266 else 267 $grp = (int) $this->get_data( $handle, 'group' ); 286 $grp = (int) $this->get_data( $handle, 'group' ); 268 287 269 288 if ( false !== $group && $grp > $group ) 270 289 $grp = $group; -
tests/phpunit/tests/dependencies/jquery.php
81 81 82 82 unset( $GLOBALS['wp_scripts'] ); 83 83 } 84 85 /** 86 * Test placing of jquery in footer. 87 * @ticket 25247 88 */ 89 function test_jquery_in_footer() { 90 $scripts = new WP_Scripts; 91 $scripts->add( 'jquery', false, array( 'jquery-core', 'jquery-migrate' ) ); 92 $scripts->add( 'jquery-core', '/jquery.js', array() ); 93 $scripts->add( 'jquery-migrate', "/jquery-migrate.js", array() ); 94 95 $scripts->enqueue( 'jquery' ); 96 97 $object = $scripts->query( 'jquery' ); 98 $object->add_data( 'group', 1 ); 99 foreach( $object->deps as $dep ) { 100 $scripts->add_data( $dep, 'group', 1 ); 101 } 102 103 $this->expectOutputRegex( '/^(?:<script[^>]+><\/script>\\n){2}$/' ); 104 105 $scripts->do_items( false, 0 ); 106 // Could do $this->assertContains( 'jquery', $scripts->done ); 107 $this->assertNotContains( 'jquery-core', $scripts->done, 'jquery-core should be in footer but is in head' ); 108 $this->assertNotContains( 'jquery-migrate', $scripts->done, 'jquery-migrate should be in footer but is in head' ); 109 110 $scripts->do_items( false, 1 ); 111 $this->assertContains( 'jquery', $scripts->done ); 112 $this->assertContains( 'jquery-core', $scripts->done, 'jquery-core in footer' ); 113 $this->assertContains( 'jquery-migrate', $scripts->done, 'jquery-migrate in footer' ); 114 } 84 115 } -
tests/phpunit/tests/dependencies/scripts.php
157 157 } 158 158 159 159 /** 160 * Test mismatch of groups in dependencies outputs all scripts in right order. 161 * @ticket 25247 162 */ 163 public function test_group_mismatch_in_deps() { 164 $scripts = new WP_Scripts; 165 $scripts->add( 'one', 'one', array(), 'v1', 1 ); 166 $scripts->add( 'two', 'two', array( 'one' ) ); 167 $scripts->add( 'three', 'three', array( 'two' ), 'v1', 1 ); 168 169 $scripts->enqueue( array( 'three' ) ); 170 171 $this->expectOutputRegex( '/^(?:<script[^>]+><\/script>\\n){7}$/' ); 172 173 $scripts->do_items( false, 0 ); 174 $this->assertContains( 'one', $scripts->done ); 175 $this->assertContains( 'two', $scripts->done ); 176 $this->assertNotContains( 'three', $scripts->done ); 177 178 $scripts->do_items( false, 1 ); 179 $this->assertContains( 'one', $scripts->done ); 180 $this->assertContains( 'two', $scripts->done ); 181 $this->assertContains( 'three', $scripts->done ); 182 183 $scripts = new WP_Scripts; 184 $scripts->add( 'one', 'one', array(), 'v1', 1 ); 185 $scripts->add( 'two', 'two', array( 'one' ), 'v1', 1 ); 186 $scripts->add( 'three', 'three', array( 'one' ) ); 187 $scripts->add( 'four', 'four', array( 'two', 'three' ), 'v1', 1 ); 188 189 $scripts->enqueue( array( 'four' ) ); 190 191 $scripts->do_items( false, 0 ); 192 $this->assertContains( 'one', $scripts->done ); 193 $this->assertNotContains( 'two', $scripts->done ); 194 $this->assertContains( 'three', $scripts->done ); 195 $this->assertNotContains( 'four', $scripts->done ); 196 197 $scripts->do_items( false, 1 ); 198 $this->assertContains( 'one', $scripts->done ); 199 $this->assertContains( 'two', $scripts->done ); 200 $this->assertContains( 'three', $scripts->done ); 201 $this->assertContains( 'four', $scripts->done ); 202 } 203 204 /** 160 205 * Testing 'wp_register_script' return boolean success/failure value. 161 206 * 162 207 * @ticket 31126 … … 199 244 $this->assertContains( home_url( 'bar.js' ), $footer ); 200 245 $this->assertContains( home_url( 'baz.js' ), $footer ); 201 246 } 247 248 /** 249 * @ticket 35873 250 */ 251 function test_wp_register_script_with_dependencies_in_head_and_footer() { 252 wp_register_script( 'parent', home_url( 'parent.js' ), array( 'child' ), '1', true ); // in footer 253 wp_register_script( 'child', home_url( 'child.js' ), array( 'grandchild' ), '1', false ); // in head 254 wp_register_script( 'grandchild', home_url( 'grandchild.js' ), array(), '1', true ); // in footer 255 256 wp_enqueue_script( 'parent' ); 257 258 $header = get_echo( 'wp_print_head_scripts' ); 259 $footer = get_echo( 'wp_print_footer_scripts' ); 260 261 $expected_header = "<script type='text/javascript' src='http://example.org/grandchild.js?ver=1'></script>\n"; 262 $expected_header .= "<script type='text/javascript' src='http://example.org/child.js?ver=1'></script>\n"; 263 $expected_footer = "<script type='text/javascript' src='http://example.org/parent.js?ver=1'></script>\n"; 264 265 $this->assertEquals( $expected_header, $header ); 266 $this->assertEquals( $expected_footer, $footer ); 267 } 202 268 }