| 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 | } |
| 703 | |
| 704 | /** |
| 705 | * @ticket 36392 |
| 706 | */ |
| 707 | public function test_wp_add_inline_script_before_third_core_script_prints_two_concat_scripts() { |
| 708 | global $wp_scripts; |
| 709 | |
| 710 | $wp_scripts->do_concat = true; |
| 711 | $wp_scripts->default_dirs = array( '/wp-admin/js/', '/wp-includes/js/' ); // Default dirs as in wp-includes/script-loader.php |
| 712 | |
| 713 | wp_enqueue_script( 'one', '/wp-includes/js/script.js' ); |
| 714 | wp_enqueue_script( 'two', '/wp-includes/js/script2.js', array( 'one' ) ); |
| 715 | wp_enqueue_script( 'three', '/wp-includes/js/script3.js' ); |
| 716 | wp_add_inline_script( 'three', 'console.log("before three");', 'before' ); |
| 717 | wp_enqueue_script( 'four', '/wp-includes/js/script4.js' ); |
| 718 | |
| 719 | $ver = get_bloginfo( 'version' ); |
| 720 | $expected = "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&load%5B%5D=one,two&ver={$ver}'></script>\n"; |
| 721 | $expected .= "<script type='text/javascript'>\nconsole.log(\"before three\");\n</script>\n"; |
| 722 | $expected .= "<script type='text/javascript' src='/wp-includes/js/script3.js?ver={$ver}'></script>\n"; |
| 723 | $expected .= "<script type='text/javascript' src='/wp-includes/js/script4.js?ver={$ver}'></script>\n"; |
| 724 | |
| 725 | $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); |
| 726 | } |