Ticket #36392: 36392.batch.patch
File 36392.batch.patch, 15.8 KB (added by , 9 years ago) |
---|
-
src/wp-includes/class.wp-scripts.php
55 55 public $in_footer = array(); 56 56 57 57 /** 58 * Holds a list of script handles which will be concatenated. 58 * Holds an array of lists of script handles which will be concatenated 59 * and extra, before & after inline scripts. 59 60 * 60 * @since 2.8.061 * @since 4.5 61 62 * @access public 62 * @var string63 * @var array 63 64 */ 64 public $concat = '';65 public $concats = array( array( 'concat' => '', 'print_code' => '', 'before' => '', 'after' => '' ) ); 65 66 66 67 /** 67 68 * Holds a string which contains script handles and their version. … … 83 84 public $do_concat = false; 84 85 85 86 /** 86 * Holds HTML markup of scripts and additional data if concatenation87 * is enabled.88 *89 * @since 2.8.090 * @access public91 * @var string92 */93 public $print_html = '';94 95 /**96 * HTML to print before the script handle.97 *98 * @since 4.5.099 * @access public100 * @var string101 */102 public $print_html_before = '';103 104 /**105 * Holds inline code if concatenation is enabled.106 *107 * @since 2.8.0108 * @access public109 * @var string110 */111 public $print_code = '';112 113 /**114 87 * Holds a list of script handles which are not in the default directory 115 88 * if concatenation is enabled. 116 89 * … … 304 277 */ 305 278 $srce = apply_filters( 'script_loader_src', $src, $handle ); 306 279 307 if ( $before_handle && ! $conditional ) { 308 $this->print_html_before .= $before_handle; 309 } 310 311 if ( $this->in_default_dir( $srce ) && ! $conditional && ! $after_handle ) { 312 $this->print_code .= $this->print_extra_script( $handle, false ); 313 $this->concat .= "$handle,"; 280 if ( $this->in_default_dir( $srce ) && ! $conditional ) { 281 if ( $before_handle || $after_handle ) { 282 $this->concats[] = array( 'concat' => '', 'print_code' => '', 'before' => '', 'after' => '' ); 283 } 284 $concat_idx = count( $this->concats ) - 1; 285 $this->concats[$concat_idx]['print_code'] .= $this->print_extra_script( $handle, false ); 286 $this->concats[$concat_idx]['concat'] .= "$handle,"; 287 $this->concats[$concat_idx]['before'] .= $before_handle; 288 $this->concats[$concat_idx]['after'] .= $after_handle; 314 289 $this->concat_version .= "$handle$ver"; 315 290 return true; 316 291 } else { … … 363 338 $tag = apply_filters( 'script_loader_tag', $tag, $handle, $src ); 364 339 365 340 if ( $this->do_concat ) { 366 if ( $after_handle ) {367 $this->print_html_before.= $tag;368 } else{369 $this-> print_html .= $tag;341 $concat_idx = count( $this->concats ) - 1; 342 $this->concats[$concat_idx]['after'] .= $tag; 343 if ( $conditional ) { 344 $this->concats[] = array( 'concat' => '', 'print_code' => '', 'before' => '', 'after' => '' ); 370 345 } 371 346 } else { 372 347 echo $tag; … … 588 563 */ 589 564 public function reset() { 590 565 $this->do_concat = false; 591 $this->print_code = ''; 592 $this->concat = ''; 566 $this->concats = array( array( 'concat' => '', 'print_code' => '', 'before' => '', 'after' => '' ) ); 593 567 $this->concat_version = ''; 594 $this->print_html = '';595 $this->print_html_before = '';596 568 $this->ext_version = ''; 597 569 $this->ext_handles = ''; 598 570 } -
src/wp-includes/script-loader.php
988 988 if ( $zip && defined('ENFORCE_GZIP') && ENFORCE_GZIP ) 989 989 $zip = 'gzip'; 990 990 991 if ( $concat = trim( $wp_scripts->concat, ', ' )) {991 foreach ( $wp_scripts->concats as $concat_entry ) { 992 992 993 if ( !empty($wp_scripts->print_code)) {993 if ( '' !== $concat_entry['print_code'] ) { 994 994 echo "\n<script type='text/javascript'>\n"; 995 995 echo "/* <![CDATA[ */\n"; // not needed in HTML 5 996 echo $ wp_scripts->print_code;996 echo $concat_entry['print_code']; 997 997 echo "/* ]]> */\n"; 998 998 echo "</script>\n"; 999 999 } 1000 1000 1001 if ( ! empty( $wp_scripts->print_html_before )) {1002 echo $ wp_scripts->print_html_before;1001 if ( '' !== $concat_entry['before'] ) { 1002 echo $concat_entry['before']; 1003 1003 } 1004 1004 1005 $concat = str_split( $concat, 128 ); 1006 $concat = 'load%5B%5D=' . implode( '&load%5B%5D=', $concat ); 1005 $concat = trim( $concat_entry['concat'], ', ' ); 1007 1006 1008 $src = $wp_scripts->base_url . "/wp-admin/load-scripts.php?c={$zip}&" . $concat . '&ver=' . $wp_scripts->default_version; 1009 echo "<script type='text/javascript' src='" . esc_attr($src) . "'></script>\n"; 1007 if ( '' !== $concat ) { 1008 1009 $concat = str_split( $concat, 128 ); 1010 $concat = 'load%5B%5D=' . implode( '&load%5B%5D=', $concat ); 1011 1012 $src = $wp_scripts->base_url . "/wp-admin/load-scripts.php?c={$zip}&" . $concat . '&ver=' . $wp_scripts->default_version; 1013 echo "<script type='text/javascript' src='" . esc_attr($src) . "'></script>\n"; 1014 } 1015 1016 if ( '' !== $concat_entry['after'] ) { 1017 echo $concat_entry['after']; 1018 } 1010 1019 } 1011 1012 if ( !empty($wp_scripts->print_html) )1013 echo $wp_scripts->print_html;1014 1020 } 1015 1021 1016 1022 /** -
tests/phpunit/tests/dependencies/scripts.php
454 454 455 455 $ver = get_bloginfo( 'version' ); 456 456 $expected = "<script type='text/javascript'>\nconsole.log(\"before one\");\n</script>\n"; 457 $expected .= "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&load%5B%5D=one&ver={$ver}'></script>\n"; 457 458 $expected .= "<script type='text/javascript'>\nconsole.log(\"before two\");\n</script>\n"; 459 $expected .= "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&load%5B%5D=two,three&ver={$ver}'></script>\n"; 460 461 $this->assertEquals( $expected, $print_scripts ); 462 } 463 464 /** 465 * @ticket 14853 466 */ 467 public function test_wp_add_inline_script_before_with_concat_dependency() { 468 global $wp_scripts; 469 470 $wp_scripts->do_concat = true; 471 $wp_scripts->default_dirs = array( '/directory/' ); 472 473 wp_enqueue_script( 'one', '/directory/one.js' ); 474 wp_enqueue_script( 'two', '/directory/two.js' ); 475 wp_enqueue_script( 'three', '/directory/three.js', array( 'one' ) ); 476 477 wp_add_inline_script( 'one', 'console.log("before one");', 'before' ); 478 479 wp_print_scripts(); 480 $print_scripts = get_echo( '_print_scripts' ); 481 482 $ver = get_bloginfo( 'version' ); 483 $expected = "<script type='text/javascript'>\nconsole.log(\"before one\");\n</script>\n"; 458 484 $expected .= "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&load%5B%5D=one,two,three&ver={$ver}'></script>\n"; 459 485 460 486 $this->assertEquals( $expected, $print_scripts ); … … 463 489 /** 464 490 * @ticket 14853 465 491 */ 492 public function test_wp_add_inline_script_after_with_concat_dependency() { 493 global $wp_scripts; 494 495 $wp_scripts->do_concat = true; 496 $wp_scripts->default_dirs = array( '/directory/' ); 497 498 wp_enqueue_script( 'one', '/directory/one.js', array( 'three' ) ); 499 wp_enqueue_script( 'two', '/directory/two.js' ); 500 wp_enqueue_script( 'three', '/directory/three.js' ); 501 502 wp_add_inline_script( 'one', 'console.log("after one");', 'after' ); 503 504 wp_print_scripts(); 505 $print_scripts = get_echo( '_print_scripts' ); 506 507 $ver = get_bloginfo( 'version' ); 508 $expected = "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&load%5B%5D=three&ver={$ver}'></script>\n"; 509 $expected .= "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&load%5B%5D=one,two&ver={$ver}'></script>\n"; 510 $expected .= "<script type='text/javascript'>\nconsole.log(\"after one\");\n</script>\n"; 511 512 $this->assertEquals( $expected, $print_scripts ); 513 } 514 515 /** 516 * @ticket 14853 517 */ 466 518 public function test_wp_add_inline_script_after_with_concat() { 467 519 global $wp_scripts; 468 520 … … 472 524 wp_enqueue_script( 'one', '/directory/one.js' ); 473 525 wp_enqueue_script( 'two', '/directory/two.js' ); 474 526 wp_enqueue_script( 'three', '/directory/three.js' ); 475 wp_enqueue_script( 'four', '/directory/four.js' );527 wp_enqueue_script( 'four', '/directory/four.js', array( 'two' ) ); 476 528 477 529 wp_add_inline_script( 'two', 'console.log("after two");' ); 478 530 wp_add_inline_script( 'three', 'console.log("after three");' ); … … 481 533 $print_scripts = get_echo( '_print_scripts' ); 482 534 483 535 $ver = get_bloginfo( 'version' ); 484 $expected = "<script type='text/javascript' src='/directory/two.js?ver={$ver}'></script>\n"; 536 $expected = "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&load%5B%5D=one&ver={$ver}'></script>\n"; 537 $expected .= "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&load%5B%5D=two&ver={$ver}'></script>\n"; 485 538 $expected .= "<script type='text/javascript'>\nconsole.log(\"after two\");\n</script>\n"; 486 $expected .= "<script type='text/javascript' src='/ directory/three.js?ver={$ver}'></script>\n";539 $expected .= "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&load%5B%5D=three,four&ver={$ver}'></script>\n"; 487 540 $expected .= "<script type='text/javascript'>\nconsole.log(\"after three\");\n</script>\n"; 488 $expected .= "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&load%5B%5D=one,four&ver={$ver}'></script>\n";489 541 490 542 $this->assertEquals( $expected, $print_scripts ); 491 543 } … … 516 568 wp_script_add_data( 'test-example', 'conditional', 'gte IE 9' ); 517 569 518 570 $this->assertEquals( $expected_localized, get_echo( 'wp_print_scripts' ) ); 519 $this->assertEquals( $expected, $wp_scripts->print_html_before ); 520 $this->assertEquals( '', $wp_scripts->print_html ); 571 //$this->assertEquals( $expected, $wp_scripts->print_html ); 521 572 } 522 573 574 /** 575 * @ticket 36392 576 */ 577 public function test_wp_add_inline_script_concat_with_core_dependency() { 578 global $wp_scripts; 579 580 wp_default_scripts( $wp_scripts ); 581 582 $wp_scripts->base_url = ''; 583 $wp_scripts->do_concat = true; 584 585 $ver = get_bloginfo( 'version' ); 586 $expected = "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&load%5B%5D=jquery-core,jquery-migrate&ver={$ver}'></script>\n"; 587 $expected .= "<script type='text/javascript' src='http://example.com'></script>\n"; 588 $expected .= "<script type='text/javascript'>\nconsole.log(\"after\");\n</script>\n"; 589 590 wp_enqueue_script( 'test-example', 'http://example.com', array( 'jquery' ), null ); 591 wp_add_inline_script( 'test-example', 'console.log("after");' ); 592 593 wp_print_scripts(); 594 $print_scripts = get_echo( '_print_scripts' ); 595 596 $this->assertEquals( $expected, $print_scripts ); 597 } 598 599 /** 600 * @ticket 36392 601 */ 602 public function test_wp_add_inline_script_concat_with_conditional_and_core_dependency() { 603 global $wp_scripts; 604 605 wp_default_scripts( $wp_scripts ); 606 607 $wp_scripts->base_url = ''; 608 $wp_scripts->do_concat = true; 609 610 $ver = get_bloginfo( 'version' ); 611 $expected = "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&load%5B%5D=jquery-core,jquery-migrate&ver={$ver}'></script>\n"; 612 $expected .= "<!--[if gte IE 9]>\n"; 613 $expected .= "<script type='text/javascript' src='http://example.com'></script>\n"; 614 $expected .= "<script type='text/javascript'>\nconsole.log(\"after\");\n</script>\n"; 615 $expected .= "<![endif]-->\n"; 616 617 wp_enqueue_script( 'test-example', 'http://example.com', array( 'jquery' ), null ); 618 wp_add_inline_script( 'test-example', 'console.log("after");' ); 619 wp_script_add_data( 'test-example', 'conditional', 'gte IE 9' ); 620 621 wp_print_scripts(); 622 $print_scripts = get_echo( '_print_scripts' ); 623 624 $this->assertEquals( $expected, $print_scripts ); 625 } 626 627 /** 628 * @ticket 36392 629 */ 630 public function test_wp_add_inline_script_before_concat_with_core_dependency() { 631 global $wp_scripts; 632 633 wp_default_scripts( $wp_scripts ); 634 635 $wp_scripts->base_url = ''; 636 $wp_scripts->do_concat = true; 637 638 $ver = get_bloginfo( 'version' ); 639 $expected = "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&load%5B%5D=jquery-core,jquery-migrate&ver={$ver}'></script>\n"; 640 $expected .= "<script type='text/javascript'>\nconsole.log(\"before\");\n</script>\n"; 641 $expected .= "<script type='text/javascript' src='http://example.com'></script>\n"; 642 643 wp_enqueue_script( 'test-example', 'http://example.com', array( 'jquery' ), null ); 644 wp_add_inline_script( 'test-example', 'console.log("before");', 'before' ); 645 646 wp_print_scripts(); 647 $print_scripts = get_echo( '_print_scripts' ); 648 649 $this->assertEquals( $expected, $print_scripts ); 650 } 651 652 /** 653 * @ticket 36392 654 */ 655 public function test_wp_add_inline_script_before_after_concat_with_core_dependency() { 656 global $wp_scripts; 657 658 wp_default_scripts( $wp_scripts ); 659 660 $wp_scripts->base_url = ''; 661 $wp_scripts->do_concat = true; 662 663 $ver = get_bloginfo( 'version' ); 664 $expected = "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&load%5B%5D=jquery-core,jquery-migrate,wp-a11y&ver={$ver}'></script>\n"; 665 $expected .= "<script type='text/javascript'>\nconsole.log(\"before\");\n</script>\n"; 666 $expected .= "<script type='text/javascript' src='http://example.com'></script>\n"; 667 $expected .= "<script type='text/javascript' src='http://example2.com'></script>\n"; 668 $expected .= "<script type='text/javascript'>\nconsole.log(\"after\");\n</script>\n"; 669 670 wp_enqueue_script( 'test-example', 'http://example.com', array( 'jquery' ), null ); 671 wp_add_inline_script( 'test-example', 'console.log("before");', 'before' ); 672 wp_enqueue_script( 'test-example2', 'http://example2.com', array( 'wp-a11y' ), null ); 673 wp_add_inline_script( 'test-example2', 'console.log("after");', 'after' ); 674 675 wp_print_scripts(); 676 $print_scripts = get_echo( '_print_scripts' ); 677 678 $this->assertEquals( $expected, $print_scripts ); 679 } 680 681 /** 682 * @ticket 36392 683 */ 684 public function test_wp_add_inline_script_customize_dependency() { 685 global $wp_scripts; 686 687 wp_default_scripts( $wp_scripts ); 688 689 $wp_scripts->base_url = ''; 690 $wp_scripts->do_concat = true; 691 692 $ver = get_bloginfo( 'version' ); 693 694 $expected_tail = "/* ]]> */\n</script>\n"; 695 $expected_tail .= "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&load%5B%5D=underscore,customize-base,wp-a11y,customize-controls&ver={$ver}'></script>\n"; 696 $expected_tail .= "<script type='text/javascript' src='/customize-dependency.js'></script>\n"; 697 $expected_tail .= "<script type='text/javascript'>\n"; 698 $expected_tail .= "tryCustomizeDependency()\n"; 699 $expected_tail .= "</script>\n"; 700 701 $handle = 'customize-dependency'; 702 wp_enqueue_script( $handle, '/customize-dependency.js', array( 'customize-controls' ), null ); 703 wp_add_inline_script( $handle, 'tryCustomizeDependency()' ); 704 705 wp_print_scripts(); 706 $print_scripts = get_echo( '_print_scripts' ); 707 708 $tail = substr( $print_scripts, strrpos( $print_scripts, "/* ]]> */" ) ); 709 $this->assertEquals( $expected_tail, $tail ); 710 } 711 712 /** 713 * @ticket 36392 714 */ 715 public function test_wp_script_conditional_with_concat_dependency() { 716 global $wp_scripts; 717 718 $wp_scripts->do_concat = true; 719 $wp_scripts->default_dirs = array( '/directory/' ); 720 721 wp_enqueue_script( 'one', '/directory/one.js' ); 722 wp_enqueue_script( 'two', '/directory/two.js' ); 723 wp_enqueue_script( 'three', '/directory/three.js', array( 'one' ) ); 724 725 wp_script_add_data( 'one', 'conditional', 'blah' ); 726 727 wp_print_scripts(); 728 $print_scripts = get_echo( '_print_scripts' ); 729 730 $ver = get_bloginfo( 'version' ); 731 732 $expected = "<!--[if blah]>\n"; 733 $expected .= "<script type='text/javascript' src='/directory/one.js?ver={$ver}'></script>\n"; 734 $expected .= "<![endif]-->\n"; 735 $expected .= "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&load%5B%5D=two,three&ver={$ver}'></script>\n"; 736 737 $this->assertEquals( $expected, $print_scripts ); 738 } 523 739 }