| 570 | /** |
| 571 | * Original issue with concat. |
| 572 | * |
| 573 | * @ticket 36392 |
| 574 | */ |
| 575 | public function test_wp_add_inline_script_customize_dependency_concat() { |
| 576 | global $wp_scripts; |
| 577 | |
| 578 | wp_default_scripts( $wp_scripts ); |
| 579 | |
| 580 | $wp_scripts->base_url = ''; |
| 581 | $wp_scripts->do_concat = true; |
| 582 | |
| 583 | $ver = get_bloginfo( 'version' ); |
| 584 | |
| 585 | $expected_tail = "/* ]]> */\n</script>\n"; |
| 586 | $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"; |
| 587 | $expected_tail .= "<script type='text/javascript' src='/customize-dependency.js'></script>\n"; |
| 588 | $expected_tail .= "<script type='text/javascript'>\n"; |
| 589 | $expected_tail .= "tryCustomizeDependency()\n"; |
| 590 | $expected_tail .= "</script>\n"; |
| 591 | |
| 592 | $handle = 'customize-dependency'; |
| 593 | wp_enqueue_script( $handle, '/customize-dependency.js', array( 'customize-controls' ), null ); |
| 594 | wp_add_inline_script( $handle, 'tryCustomizeDependency()' ); |
| 595 | |
| 596 | wp_print_scripts(); |
| 597 | $print_scripts = get_echo( '_print_scripts' ); |
| 598 | |
| 599 | $tail = substr( $print_scripts, strrpos( $print_scripts, "/* ]]> */" ) ); |
| 600 | $this->assertEquals( $expected_tail, $tail ); |
| 601 | } |
| 602 | |
| 603 | /** |
| 604 | * Original issue without concat. |
| 605 | * |
| 606 | * @ticket 36392 |
| 607 | */ |
| 608 | public function test_wp_add_inline_script_customize_dependency_noconcat() { |
| 609 | global $wp_scripts; |
| 610 | |
| 611 | wp_default_scripts( $wp_scripts ); |
| 612 | |
| 613 | $wp_scripts->base_url = ''; |
| 614 | $wp_scripts->do_concat = false; |
| 615 | |
| 616 | $ver = get_bloginfo( 'version' ); |
| 617 | |
| 618 | $expected_tail = "/* ]]> */\n</script>\n"; |
| 619 | $expected_tail .= "<script type='text/javascript' src='/wp-admin/js/customize-controls.js?ver={$ver}'></script>\n"; |
| 620 | $expected_tail .= "<script type='text/javascript' src='/customize-dependency.js'></script>\n"; |
| 621 | $expected_tail .= "<script type='text/javascript'>\n"; |
| 622 | $expected_tail .= "tryCustomizeDependency()\n"; |
| 623 | $expected_tail .= "</script>\n"; |
| 624 | |
| 625 | $handle = 'customize-dependency'; |
| 626 | wp_enqueue_script( $handle, '/customize-dependency.js', array( 'customize-controls' ), null ); |
| 627 | wp_add_inline_script( $handle, 'tryCustomizeDependency()' ); |
| 628 | |
| 629 | $print_scripts = get_echo( 'wp_print_scripts' ); |
| 630 | |
| 631 | $tail = substr( $print_scripts, strrpos( $print_scripts, "/* ]]> */" ) ); |
| 632 | $this->assertEquals( $expected_tail, $tail ); |
| 633 | } |
| 634 | |
| 635 | /** |
| 636 | * @ticket 36392 |
| 637 | */ |
| 638 | public function test_wp_add_inline_script_concat_with_core_dependency() { |
| 639 | global $wp_scripts; |
| 640 | |
| 641 | wp_default_scripts( $wp_scripts ); |
| 642 | |
| 643 | $wp_scripts->base_url = ''; |
| 644 | $wp_scripts->do_concat = true; |
| 645 | |
| 646 | $ver = get_bloginfo( 'version' ); |
| 647 | $expected = "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&load%5B%5D=jquery-core,jquery-migrate&ver={$ver}'></script>\n"; |
| 648 | $expected .= "<script type='text/javascript' src='http://example.com'></script>\n"; |
| 649 | $expected .= "<script type='text/javascript'>\nconsole.log(\"after\");\n</script>\n"; |
| 650 | |
| 651 | wp_enqueue_script( 'test-example', 'http://example.com', array( 'jquery' ), null ); |
| 652 | wp_add_inline_script( 'test-example', 'console.log("after");' ); |
| 653 | |
| 654 | wp_print_scripts(); |
| 655 | $print_scripts = get_echo( '_print_scripts' ); |
| 656 | |
| 657 | $this->assertEquals( $expected, $print_scripts ); |
| 658 | } |
| 659 | |
| 660 | /** |
| 661 | * @ticket 36392 |
| 662 | */ |
| 663 | public function test_wp_add_inline_script_noconcat_with_core_dependency() { |
| 664 | global $wp_scripts; |
| 665 | |
| 666 | wp_default_scripts( $wp_scripts ); |
| 667 | |
| 668 | $wp_scripts->base_url = ''; |
| 669 | $wp_scripts->do_concat = false; |
| 670 | |
| 671 | $js_ver = $wp_scripts->registered['jquery']->ver; |
| 672 | $js_migrate_ver = $wp_scripts->registered['jquery-migrate']->ver; |
| 673 | |
| 674 | $expected = "<script type='text/javascript' src='/wp-includes/js/jquery/jquery.js?ver={$js_ver}'></script>\n"; |
| 675 | $expected .= "<script type='text/javascript' src='/wp-includes/js/jquery/jquery-migrate.js?ver={$js_migrate_ver}'></script>\n"; |
| 676 | $expected .= "<script type='text/javascript' src='http://example.com'></script>\n"; |
| 677 | $expected .= "<script type='text/javascript'>\nconsole.log(\"after\");\n</script>\n"; |
| 678 | |
| 679 | wp_enqueue_script( 'test-example', 'http://example.com', array( 'jquery' ), null ); |
| 680 | wp_add_inline_script( 'test-example', 'console.log("after");' ); |
| 681 | |
| 682 | $print_scripts = get_echo( 'wp_print_scripts' ); |
| 683 | |
| 684 | $this->assertEquals( $expected, $print_scripts ); |
| 685 | } |
| 686 | |
| 687 | /** |
| 688 | * @ticket 36392 |
| 689 | */ |
| 690 | public function test_wp_add_inline_script_concat_with_conditional_and_core_dependency() { |
| 691 | global $wp_scripts; |
| 692 | |
| 693 | wp_default_scripts( $wp_scripts ); |
| 694 | |
| 695 | $wp_scripts->base_url = ''; |
| 696 | $wp_scripts->do_concat = true; |
| 697 | |
| 698 | $ver = get_bloginfo( 'version' ); |
| 699 | $expected = "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&load%5B%5D=jquery-core,jquery-migrate&ver={$ver}'></script>\n"; |
| 700 | $expected .= "<!--[if gte IE 9]>\n"; |
| 701 | $expected .= "<script type='text/javascript' src='http://example.com'></script>\n"; |
| 702 | $expected .= "<script type='text/javascript'>\nconsole.log(\"after\");\n</script>\n"; |
| 703 | $expected .= "<![endif]-->\n"; |
| 704 | |
| 705 | wp_enqueue_script( 'test-example', 'http://example.com', array( 'jquery' ), null ); |
| 706 | wp_add_inline_script( 'test-example', 'console.log("after");' ); |
| 707 | wp_script_add_data( 'test-example', 'conditional', 'gte IE 9' ); |
| 708 | |
| 709 | wp_print_scripts(); |
| 710 | $print_scripts = get_echo( '_print_scripts' ); |
| 711 | |
| 712 | $this->assertEquals( $expected, $print_scripts ); |
| 713 | } |
| 714 | |
| 715 | /** |
| 716 | * @ticket 36392 |
| 717 | */ |
| 718 | public function test_wp_add_inline_script_before_concat_with_core_dependency() { |
| 719 | global $wp_scripts; |
| 720 | |
| 721 | wp_default_scripts( $wp_scripts ); |
| 722 | |
| 723 | $wp_scripts->base_url = ''; |
| 724 | $wp_scripts->do_concat = true; |
| 725 | |
| 726 | $ver = get_bloginfo( 'version' ); |
| 727 | $expected = "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&load%5B%5D=jquery-core,jquery-migrate&ver={$ver}'></script>\n"; |
| 728 | $expected .= "<script type='text/javascript'>\nconsole.log(\"before\");\n</script>\n"; |
| 729 | $expected .= "<script type='text/javascript' src='http://example.com'></script>\n"; |
| 730 | |
| 731 | wp_enqueue_script( 'test-example', 'http://example.com', array( 'jquery' ), null ); |
| 732 | wp_add_inline_script( 'test-example', 'console.log("before");', 'before' ); |
| 733 | |
| 734 | wp_print_scripts(); |
| 735 | $print_scripts = get_echo( '_print_scripts' ); |
| 736 | |
| 737 | $this->assertEquals( $expected, $print_scripts ); |
| 738 | } |
| 739 | |
| 740 | /** |
| 741 | * @ticket 36392 |
| 742 | */ |
| 743 | public function test_wp_add_inline_script_before_after_concat_with_core_dependency() { |
| 744 | global $wp_scripts; |
| 745 | |
| 746 | wp_default_scripts( $wp_scripts ); |
| 747 | |
| 748 | $wp_scripts->base_url = ''; |
| 749 | $wp_scripts->do_concat = true; |
| 750 | |
| 751 | $ver = get_bloginfo( 'version' ); |
| 752 | $expected = "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&load%5B%5D=jquery-core,jquery-migrate&ver={$ver}'></script>\n"; |
| 753 | $expected .= "<script type='text/javascript'>\nconsole.log(\"before\");\n</script>\n"; |
| 754 | $expected .= "<script type='text/javascript' src='http://example.com'></script>\n"; |
| 755 | $expected .= "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&load%5B%5D=wp-a11y&ver={$ver}'></script>\n"; |
| 756 | $expected .= "<script type='text/javascript' src='http://example2.com'></script>\n"; |
| 757 | $expected .= "<script type='text/javascript'>\nconsole.log(\"after\");\n</script>\n"; |
| 758 | |
| 759 | wp_enqueue_script( 'test-example', 'http://example.com', array( 'jquery' ), null ); |
| 760 | wp_add_inline_script( 'test-example', 'console.log("before");', 'before' ); |
| 761 | wp_enqueue_script( 'test-example2', 'http://example2.com', array( 'wp-a11y' ), null ); |
| 762 | wp_add_inline_script( 'test-example2', 'console.log("after");', 'after' ); |
| 763 | |
| 764 | wp_print_scripts(); |
| 765 | $print_scripts = get_echo( '_print_scripts' ); |
| 766 | |
| 767 | $this->assertEquals( $expected, $print_scripts ); |
| 768 | } |
| 769 | |
| 770 | /** |
| 771 | * @ticket 36392 |
| 772 | */ |
| 773 | public function test_wp_add_inline_script_before_after_concat_with_core_dependency_default_dirs() { |
| 774 | global $wp_scripts; |
| 775 | |
| 776 | wp_default_scripts( $wp_scripts ); |
| 777 | |
| 778 | $wp_scripts->base_url = ''; |
| 779 | $wp_scripts->do_concat = true; |
| 780 | $wp_scripts->default_dirs = array('/wp-admin/js/', '/wp-includes/js/'); // Default dirs as in wp-includes/script-loader.php |
| 781 | |
| 782 | $ver = get_bloginfo( 'version' ); |
| 783 | $expected = "<script type='text/javascript'>\nconsole.log(\"before\");\n</script>\n"; |
| 784 | $expected .= "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&load%5B%5D=jquery-core,jquery-migrate,test-example,wp-a11y,test-example2&ver={$ver}'></script>\n"; |
| 785 | $expected .= "<script type='text/javascript'>\nconsole.log(\"after\");\n</script>\n"; |
| 786 | |
| 787 | wp_enqueue_script( 'test-example', '/wp-admin/js/example.js', array( 'jquery' ), null ); |
| 788 | wp_add_inline_script( 'test-example', 'console.log("before");', 'before' ); |
| 789 | wp_enqueue_script( 'test-example2', '/wp-includes/js/example2.js', array( 'wp-a11y' ), null ); |
| 790 | wp_add_inline_script( 'test-example2', 'console.log("after");', 'after' ); |
| 791 | |
| 792 | wp_print_scripts(); |
| 793 | $print_scripts = get_echo( '_print_scripts' ); |
| 794 | |
| 795 | $this->assertEquals( $expected, $print_scripts ); |
| 796 | } |
| 797 | |
| 798 | /** |
| 799 | * @ticket 36392 |
| 800 | */ |
| 801 | public function test_wp_script_conditional_concat_dependency() { |
| 802 | global $wp_scripts; |
| 803 | |
| 804 | $wp_scripts->do_concat = true; |
| 805 | $wp_scripts->default_dirs = array( '/directory/' ); |
| 806 | |
| 807 | wp_enqueue_script( 'one', '/directory/one.js' ); |
| 808 | wp_enqueue_script( 'two', '/directory/two.js' ); |
| 809 | wp_enqueue_script( 'three', '/directory/three.js', array( 'one' ) ); |
| 810 | |
| 811 | wp_script_add_data( 'one', 'conditional', 'blah' ); |
| 812 | |
| 813 | wp_print_scripts(); |
| 814 | $print_scripts = get_echo( '_print_scripts' ); |
| 815 | |
| 816 | $ver = get_bloginfo( 'version' ); |
| 817 | |
| 818 | $expected = "<!--[if blah]>\n"; |
| 819 | $expected .= "<script type='text/javascript' src='/directory/one.js?ver={$ver}'></script>\n"; |
| 820 | $expected .= "<![endif]-->\n"; |
| 821 | $expected .= "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&load%5B%5D=two,three&ver={$ver}'></script>\n"; |
| 822 | |
| 823 | $this->assertEquals( $expected, $print_scripts ); |
| 824 | } |
| 825 | |
| 826 | /** |
| 827 | * @ticket 36392 |
| 828 | */ |
| 829 | public function test_wp_script_conditional_noconcat_dependency() { |
| 830 | global $wp_scripts; |
| 831 | |
| 832 | $wp_scripts->do_concat = false; |
| 833 | $wp_scripts->default_dirs = array( '/directory/' ); |
| 834 | |
| 835 | wp_enqueue_script( 'one', '/directory/one.js' ); |
| 836 | wp_enqueue_script( 'two', '/directory/two.js' ); |
| 837 | wp_enqueue_script( 'three', '/directory/three.js', array( 'one' ) ); |
| 838 | |
| 839 | wp_script_add_data( 'one', 'conditional', 'blah' ); |
| 840 | |
| 841 | $print_scripts = get_echo( 'wp_print_scripts' ); |
| 842 | |
| 843 | $ver = get_bloginfo( 'version' ); |
| 844 | |
| 845 | $expected = "<!--[if blah]>\n"; |
| 846 | $expected .= "<script type='text/javascript' src='/directory/one.js?ver={$ver}'></script>\n"; |
| 847 | $expected .= "<![endif]-->\n"; |
| 848 | $expected .= "<script type='text/javascript' src='/directory/two.js?ver={$ver}'></script>\n"; |
| 849 | $expected .= "<script type='text/javascript' src='/directory/three.js?ver={$ver}'></script>\n"; |
| 850 | |
| 851 | $this->assertEquals( $expected, $print_scripts ); |
| 852 | } |
| 853 | |
| 854 | /** |
| 855 | * @ticket 36392 |
| 856 | */ |
| 857 | public function test_wp_script_conditional_before_after_concat_dependency_diff_include_dirs() { |
| 858 | global $wp_scripts; |
| 859 | |
| 860 | $wp_scripts->do_concat = true; |
| 861 | $wp_scripts->default_dirs = array( '/directory/' ); |
| 862 | |
| 863 | wp_enqueue_script( 'one', '/directory/one.js' ); |
| 864 | wp_enqueue_script( 'two', '/directory2/two.js' ); // Not in_default_dirs(). |
| 865 | wp_enqueue_script( 'three', '/directory/three.js', array( 'one' ) ); |
| 866 | wp_enqueue_script( 'four', '/directory/four.js' ); |
| 867 | |
| 868 | wp_script_add_data( 'one', 'conditional', 'blah' ); |
| 869 | wp_add_inline_script( 'one', 'console.log("before1");', 'before' ); |
| 870 | |
| 871 | wp_add_inline_script( 'two', 'console.log("before2");', 'before' ); |
| 872 | wp_add_inline_script( 'two', 'console.log("after2");', 'after' ); |
| 873 | |
| 874 | wp_add_inline_script( 'three', 'console.log("before3");', 'before' ); |
| 875 | wp_add_inline_script( 'three', 'console.log("after3");', 'after' ); |
| 876 | |
| 877 | wp_add_inline_script( 'four', 'console.log("before4");', 'before' ); |
| 878 | wp_add_inline_script( 'four', 'console.log("after4");', 'after' ); |
| 879 | |
| 880 | wp_print_scripts(); |
| 881 | $print_scripts = get_echo( '_print_scripts' ); |
| 882 | |
| 883 | $ver = get_bloginfo( 'version' ); |
| 884 | |
| 885 | $expected = "<!--[if blah]>\n"; |
| 886 | $expected .= "<script type='text/javascript'>\nconsole.log(\"before1\");\n</script>\n"; |
| 887 | $expected .= "<script type='text/javascript' src='/directory/one.js?ver={$ver}'></script>\n"; |
| 888 | $expected .= "<![endif]-->\n"; |
| 889 | $expected .= "<script type='text/javascript'>\nconsole.log(\"before2\");\n</script>\n"; |
| 890 | $expected .= "<script type='text/javascript' src='/directory2/two.js?ver={$ver}'></script>\n"; |
| 891 | $expected .= "<script type='text/javascript'>\nconsole.log(\"after2\");\n</script>\n"; |
| 892 | $expected .= "<script type='text/javascript'>\nconsole.log(\"before3\");\n</script>\n"; |
| 893 | $expected .= "<script type='text/javascript'>\nconsole.log(\"before4\");\n</script>\n"; |
| 894 | $expected .= "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&load%5B%5D=three,four&ver={$ver}'></script>\n"; |
| 895 | $expected .= "<script type='text/javascript'>\nconsole.log(\"after3\");\n</script>\n"; |
| 896 | $expected .= "<script type='text/javascript'>\nconsole.log(\"after4\");\n</script>\n"; |
| 897 | |
| 898 | $this->assertEquals( $expected, $print_scripts ); |
| 899 | } |