Ticket #36392: 36392.2.diff
File 36392.2.diff, 12.7 KB (added by , 9 years ago) |
---|
-
src/wp-includes/class.wp-scripts.php
93 93 public $print_html = ''; 94 94 95 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 96 * Holds inline code if concatenation is enabled. 106 97 * 107 98 * @since 2.8.0 … … 304 295 */ 305 296 $srce = apply_filters( 'script_loader_src', $src, $handle ); 306 297 307 if ( $before_handle && ! $conditional ) { 308 $this->print_html_before .= $before_handle; 309 } 298 if ( $this->in_default_dir( $srce ) && ( $before_handle || $after_handle ) ) { 299 $this->do_concat = false; 310 300 311 if ( $this->in_default_dir( $srce ) && ! $conditional && ! $after_handle ) { 301 // Have to print the so-far concatenated scripts right away to maintain the right order. 302 _print_scripts(); 303 $this->reset(); 304 } elseif ( $this->in_default_dir( $srce ) && ! $conditional ) { 312 305 $this->print_code .= $this->print_extra_script( $handle, false ); 313 306 $this->concat .= "$handle,"; 314 307 $this->concat_version .= "$handle$ver"; … … 363 356 $tag = apply_filters( 'script_loader_tag', $tag, $handle, $src ); 364 357 365 358 if ( $this->do_concat ) { 366 if ( $after_handle ) { 367 $this->print_html_before .= $tag; 368 } else { 369 $this->print_html .= $tag; 370 } 359 $this->print_html .= $tag; 371 360 } else { 372 361 echo $tag; 373 362 } … … 592 581 $this->concat = ''; 593 582 $this->concat_version = ''; 594 583 $this->print_html = ''; 595 $this->print_html_before = '';596 584 $this->ext_version = ''; 597 585 $this->ext_handles = ''; 598 586 } -
src/wp-includes/script-loader.php
998 998 echo "</script>\n"; 999 999 } 1000 1000 1001 if ( ! empty( $wp_scripts->print_html_before ) ) {1002 echo $wp_scripts->print_html_before;1003 }1004 1005 1001 $concat = str_split( $concat, 128 ); 1006 1002 $concat = 'load%5B%5D=' . implode( '&load%5B%5D=', $concat ); 1007 1003 -
tests/phpunit/tests/dependencies/scripts.php
449 449 wp_add_inline_script( 'one', 'console.log("before one");', 'before' ); 450 450 wp_add_inline_script( 'two', 'console.log("before two");', 'before' ); 451 451 452 wp_print_scripts();453 $print_scripts = get_echo( '_print_scripts' );454 455 452 $ver = get_bloginfo( 'version' ); 456 $expected = "<script type='text/javascript'>\nconsole.log(\"before one\");\n</script>\n"; 453 $expected = "<script type='text/javascript'>\nconsole.log(\"before one\");\n</script>\n"; 454 $expected .= "<script type='text/javascript' src='/directory/one.js?ver={$ver}'></script>\n"; 457 455 $expected .= "<script type='text/javascript'>\nconsole.log(\"before two\");\n</script>\n"; 458 $expected .= "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&load%5B%5D=one,two,three&ver={$ver}'></script>\n"; 456 $expected .= "<script type='text/javascript' src='/directory/two.js?ver={$ver}'></script>\n"; 457 $expected .= "<script type='text/javascript' src='/directory/three.js?ver={$ver}'></script>\n"; 459 458 460 $this->assertEquals( $expected, $print_scripts);459 $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); 461 460 } 462 461 463 462 /** 464 463 * @ticket 14853 465 464 */ 465 public function test_wp_add_inline_script_before_with_concat2() { 466 global $wp_scripts; 467 468 $wp_scripts->do_concat = true; 469 $wp_scripts->default_dirs = array( '/directory/' ); 470 471 wp_enqueue_script( 'one', '/directory/one.js' ); 472 wp_enqueue_script( 'two', '/directory/two.js' ); 473 wp_enqueue_script( 'three', '/directory/three.js' ); 474 475 wp_add_inline_script( 'one', 'console.log("before one");', 'before' ); 476 477 $ver = get_bloginfo( 'version' ); 478 $expected = "<script type='text/javascript'>\nconsole.log(\"before one\");\n</script>\n"; 479 $expected .= "<script type='text/javascript' src='/directory/one.js?ver={$ver}'></script>\n"; 480 $expected .= "<script type='text/javascript' src='/directory/two.js?ver={$ver}'></script>\n"; 481 $expected .= "<script type='text/javascript' src='/directory/three.js?ver={$ver}'></script>\n"; 482 483 $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); 484 } 485 486 /** 487 * @ticket 14853 488 */ 466 489 public function test_wp_add_inline_script_after_with_concat() { 467 490 global $wp_scripts; 468 491 … … 477 500 wp_add_inline_script( 'two', 'console.log("after two");' ); 478 501 wp_add_inline_script( 'three', 'console.log("after three");' ); 479 502 480 wp_print_scripts();481 $print_scripts = get_echo( '_print_scripts' );482 483 503 $ver = get_bloginfo( 'version' ); 484 $expected = "<script type='text/javascript' src='/directory/two.js?ver={$ver}'></script>\n"; 504 $expected = "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&load%5B%5D=one&ver={$ver}'></script>\n"; 505 $expected .= "<script type='text/javascript' src='/directory/two.js?ver={$ver}'></script>\n"; 485 506 $expected .= "<script type='text/javascript'>\nconsole.log(\"after two\");\n</script>\n"; 486 507 $expected .= "<script type='text/javascript' src='/directory/three.js?ver={$ver}'></script>\n"; 487 508 $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";509 $expected .= "<script type='text/javascript' src='/directory/four.js?ver={$ver}'></script>\n"; 489 510 490 $this->assertEquals( $expected, $print_scripts);511 $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); 491 512 } 492 513 493 514 /** 494 515 * @ticket 14853 495 516 */ 496 public function test_wp_add_inline_script_ concat_with_conditional() {517 public function test_wp_add_inline_script_after_and_before_with_concat_and_conditional() { 497 518 global $wp_scripts; 498 519 499 520 $wp_scripts->do_concat = true; … … 516 537 wp_script_add_data( 'test-example', 'conditional', 'gte IE 9' ); 517 538 518 539 $this->assertEquals( $expected_localized, get_echo( 'wp_print_scripts' ) ); 519 $this->assertEquals( $expected, $wp_scripts->print_html _before);520 $this->assert Equals( '', $wp_scripts->print_html);540 $this->assertEquals( $expected, $wp_scripts->print_html ); 541 $this->assertTrue( $wp_scripts->do_concat ); 521 542 } 522 543 544 /** 545 * @ticket 36392 546 */ 547 public function test_wp_add_inline_script_after_with_concat_and_core_dependency() { 548 global $wp_scripts; 549 550 wp_default_scripts( $wp_scripts ); 551 552 $wp_scripts->base_url = ''; 553 $wp_scripts->do_concat = true; 554 555 $ver = get_bloginfo( 'version' ); 556 $expected = "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&load%5B%5D=jquery-core,jquery-migrate&ver={$ver}'></script>\n"; 557 $expected .= "<script type='text/javascript' src='http://example.com'></script>\n"; 558 $expected .= "<script type='text/javascript'>\nconsole.log(\"after\");\n</script>\n"; 559 560 wp_enqueue_script( 'test-example', 'http://example.com', array( 'jquery' ), null ); 561 wp_add_inline_script( 'test-example', 'console.log("after");' ); 562 563 wp_print_scripts(); 564 $print_scripts = get_echo( '_print_scripts' ); 565 566 $this->assertEquals( $expected, $print_scripts ); 567 } 568 569 /** 570 * @ticket 36392 571 */ 572 public function test_wp_add_inline_script_after_with_concat_and_conditional_and_core_dependency() { 573 global $wp_scripts; 574 575 wp_default_scripts( $wp_scripts ); 576 577 $wp_scripts->base_url = ''; 578 $wp_scripts->do_concat = true; 579 580 $ver = get_bloginfo( 'version' ); 581 $expected = "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&load%5B%5D=jquery-core,jquery-migrate&ver={$ver}'></script>\n"; 582 $expected .= "<!--[if gte IE 9]>\n"; 583 $expected .= "<script type='text/javascript' src='http://example.com'></script>\n"; 584 $expected .= "<script type='text/javascript'>\nconsole.log(\"after\");\n</script>\n"; 585 $expected .= "<![endif]-->\n"; 586 587 wp_enqueue_script( 'test-example', 'http://example.com', array( 'jquery' ), null ); 588 wp_add_inline_script( 'test-example', 'console.log("after");' ); 589 wp_script_add_data( 'test-example', 'conditional', 'gte IE 9' ); 590 591 wp_print_scripts(); 592 $print_scripts = get_echo( '_print_scripts' ); 593 594 $this->assertEquals( $expected, $print_scripts ); 595 } 596 597 /** 598 * @ticket 36392 599 */ 600 public function test_wp_add_inline_script_before_with_concat_and_core_dependency() { 601 global $wp_scripts; 602 603 wp_default_scripts( $wp_scripts ); 604 605 $wp_scripts->base_url = ''; 606 $wp_scripts->do_concat = true; 607 608 $ver = get_bloginfo( 'version' ); 609 $expected = "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&load%5B%5D=jquery-core,jquery-migrate&ver={$ver}'></script>\n"; 610 $expected .= "<script type='text/javascript'>\nconsole.log(\"before\");\n</script>\n"; 611 $expected .= "<script type='text/javascript' src='http://example.com'></script>\n"; 612 613 wp_enqueue_script( 'test-example', 'http://example.com', array( 'jquery' ), null ); 614 wp_add_inline_script( 'test-example', 'console.log("before");', 'before' ); 615 616 wp_print_scripts(); 617 $print_scripts = get_echo( '_print_scripts' ); 618 619 $this->assertEquals( $expected, $print_scripts ); 620 } 621 622 /** 623 * @ticket 36392 624 */ 625 public function test_wp_add_inline_script_before_after_concat_with_core_dependency() { 626 global $wp_scripts; 627 628 wp_default_scripts( $wp_scripts ); 629 630 $wp_scripts->base_url = ''; 631 $wp_scripts->do_concat = true; 632 633 $ver = get_bloginfo( 'version' ); 634 $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"; 635 $expected .= "<script type='text/javascript'>\nconsole.log(\"before\");\n</script>\n"; 636 $expected .= "<script type='text/javascript' src='http://example.com'></script>\n"; 637 $expected .= "<script type='text/javascript' src='http://example2.com'></script>\n"; 638 $expected .= "<script type='text/javascript'>\nconsole.log(\"after\");\n</script>\n"; 639 640 wp_enqueue_script( 'test-example', 'http://example.com', array( 'jquery' ), null ); 641 wp_add_inline_script( 'test-example', 'console.log("before");', 'before' ); 642 wp_enqueue_script( 'test-example2', 'http://example2.com', array( 'wp-a11y' ), null ); 643 wp_add_inline_script( 'test-example2', 'console.log("after");', 'after' ); 644 645 wp_print_scripts(); 646 $print_scripts = get_echo( '_print_scripts' ); 647 648 $this->assertEquals( $expected, $print_scripts ); 649 } 650 651 /** 652 * @ticket 36392 653 */ 654 public function test_wp_add_inline_script_customize_dependency() { 655 global $wp_scripts; 656 657 wp_default_scripts( $wp_scripts ); 658 659 $wp_scripts->base_url = ''; 660 $wp_scripts->do_concat = true; 661 662 $expected_tail = "<![endif]-->\n"; 663 $expected_tail .= "<script type='text/javascript' src='/customize-dependency.js'></script>\n"; 664 $expected_tail .= "<script type='text/javascript'>\n"; 665 $expected_tail .= "tryCustomizeDependency()\n"; 666 $expected_tail .= "</script>\n"; 667 668 $handle = 'customize-dependency'; 669 wp_enqueue_script( $handle, '/customize-dependency.js', array( 'customize-controls' ), null ); 670 wp_add_inline_script( $handle, 'tryCustomizeDependency()' ); 671 672 wp_print_scripts(); 673 $print_scripts = get_echo( '_print_scripts' ); 674 675 $tail = substr( $print_scripts, strrpos( $print_scripts, "<![endif]-->" ) ); 676 $this->assertEquals( $expected_tail, $tail ); 677 } 678 679 /** 680 * @ticket 36392 681 */ 682 public function test_wp_add_inline_script_after_for_core_scripts_with_concat_is_limited_and_falls_back_to_no_concat() { 683 global $wp_scripts; 684 685 $wp_scripts->do_concat = true; 686 $wp_scripts->default_dirs = array( '/wp-admin/js/', '/wp-includes/js/' ); // Default dirs as in wp-includes/script-loader.php 687 688 wp_enqueue_script( 'one', '/wp-includes/js/script.js' ); 689 wp_enqueue_script( 'two', '/wp-includes/js/script2.js', array( 'one' ) ); 690 wp_add_inline_script( 'one', 'console.log("after one");', 'after' ); 691 wp_enqueue_script( 'three', '/wp-includes/js/script3.js' ); 692 wp_enqueue_script( 'four', '/wp-includes/js/script4.js' ); 693 694 $ver = get_bloginfo( 'version' ); 695 $expected = "<script type='text/javascript' src='/wp-includes/js/script.js?ver={$ver}'></script>\n"; 696 $expected .= "<script type='text/javascript'>\nconsole.log(\"after one\");\n</script>\n"; 697 $expected .= "<script type='text/javascript' src='/wp-includes/js/script2.js?ver={$ver}'></script>\n"; 698 $expected .= "<script type='text/javascript' src='/wp-includes/js/script3.js?ver={$ver}'></script>\n"; 699 $expected .= "<script type='text/javascript' src='/wp-includes/js/script4.js?ver={$ver}'></script>\n"; 700 701 $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); 702 } 523 703 }