| 550 | /** |
| 551 | * @ticket 36392 |
| 552 | */ |
| 553 | public function test_wp_add_inline_script_concat_with_core_dependency() { |
| 554 | global $wp_scripts; |
| 555 | |
| 556 | wp_default_scripts( $wp_scripts ); |
| 557 | |
| 558 | $wp_scripts->base_url = ''; |
| 559 | $wp_scripts->do_concat = true; |
| 560 | |
| 561 | $ver = get_bloginfo( 'version' ); |
| 562 | $expected = "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&load%5B%5D=jquery-core,jquery-migrate&ver={$ver}'></script>\n"; |
| 563 | $expected .= "<script type='text/javascript' src='http://example.com'></script>\n"; |
| 564 | $expected .= "<script type='text/javascript'>\nconsole.log(\"after\");\n</script>\n"; |
| 565 | |
| 566 | wp_enqueue_script( 'test-example', 'http://example.com', array( 'jquery' ), null ); |
| 567 | wp_add_inline_script( 'test-example', 'console.log("after");' ); |
| 568 | |
| 569 | wp_print_scripts(); |
| 570 | $print_scripts = get_echo( '_print_scripts' ); |
| 571 | |
| 572 | $this->assertEquals( $expected, $print_scripts ); |
| 573 | } |
| 574 | |
| 575 | /** |
| 576 | * @ticket 36392 |
| 577 | */ |
| 578 | public function test_wp_add_inline_script_concat_with_conditional_and_core_dependency() { |
| 579 | global $wp_scripts; |
| 580 | |
| 581 | wp_default_scripts( $wp_scripts ); |
| 582 | |
| 583 | $wp_scripts->base_url = ''; |
| 584 | $wp_scripts->do_concat = true; |
| 585 | |
| 586 | $ver = get_bloginfo( 'version' ); |
| 587 | $expected = "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&load%5B%5D=jquery-core,jquery-migrate&ver={$ver}'></script>\n"; |
| 588 | $expected .= "<!--[if gte IE 9]>\n"; |
| 589 | $expected .= "<script type='text/javascript' src='http://example.com'></script>\n"; |
| 590 | $expected .= "<script type='text/javascript'>\nconsole.log(\"after\");\n</script>\n"; |
| 591 | $expected .= "<![endif]-->\n"; |
| 592 | |
| 593 | wp_enqueue_script( 'test-example', 'http://example.com', array( 'jquery' ), null ); |
| 594 | wp_add_inline_script( 'test-example', 'console.log("after");' ); |
| 595 | wp_script_add_data( 'test-example', 'conditional', 'gte IE 9' ); |
| 596 | |
| 597 | wp_print_scripts(); |
| 598 | $print_scripts = get_echo( '_print_scripts' ); |
| 599 | |
| 600 | $this->assertEquals( $expected, $print_scripts ); |
| 601 | } |
| 602 | |
| 603 | /** |
| 604 | * @ticket 36392 |
| 605 | */ |
| 606 | public function test_wp_add_inline_script_before_concat_with_core_dependency() { |
| 607 | global $wp_scripts; |
| 608 | |
| 609 | wp_default_scripts( $wp_scripts ); |
| 610 | |
| 611 | $wp_scripts->base_url = ''; |
| 612 | $wp_scripts->do_concat = true; |
| 613 | |
| 614 | $ver = get_bloginfo( 'version' ); |
| 615 | $expected = "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&load%5B%5D=jquery-core,jquery-migrate&ver={$ver}'></script>\n"; |
| 616 | $expected .= "<script type='text/javascript'>\nconsole.log(\"before\");\n</script>\n"; |
| 617 | $expected .= "<script type='text/javascript' src='http://example.com'></script>\n"; |
| 618 | |
| 619 | wp_enqueue_script( 'test-example', 'http://example.com', array( 'jquery' ), null ); |
| 620 | wp_add_inline_script( 'test-example', 'console.log("before");', 'before' ); |
| 621 | |
| 622 | wp_print_scripts(); |
| 623 | $print_scripts = get_echo( '_print_scripts' ); |
| 624 | |
| 625 | $this->assertEquals( $expected, $print_scripts ); |
| 626 | } |
| 627 | |
| 628 | /** |
| 629 | * @ticket 36392 |
| 630 | */ |
| 631 | public function test_wp_add_inline_script_before_after_concat_with_core_dependency() { |
| 632 | global $wp_scripts; |
| 633 | |
| 634 | wp_default_scripts( $wp_scripts ); |
| 635 | |
| 636 | $wp_scripts->base_url = ''; |
| 637 | $wp_scripts->do_concat = true; |
| 638 | |
| 639 | $ver = get_bloginfo( 'version' ); |
| 640 | $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"; |
| 641 | $expected .= "<script type='text/javascript'>\nconsole.log(\"before\");\n</script>\n"; |
| 642 | $expected .= "<script type='text/javascript' src='http://example.com'></script>\n"; |
| 643 | $expected .= "<script type='text/javascript' src='http://example2.com'></script>\n"; |
| 644 | $expected .= "<script type='text/javascript'>\nconsole.log(\"after\");\n</script>\n"; |
| 645 | |
| 646 | wp_enqueue_script( 'test-example', 'http://example.com', array( 'jquery' ), null ); |
| 647 | wp_add_inline_script( 'test-example', 'console.log("before");', 'before' ); |
| 648 | wp_enqueue_script( 'test-example2', 'http://example2.com', array( 'wp-a11y' ), null ); |
| 649 | wp_add_inline_script( 'test-example2', 'console.log("after");', 'after' ); |
| 650 | |
| 651 | wp_print_scripts(); |
| 652 | $print_scripts = get_echo( '_print_scripts' ); |
| 653 | |
| 654 | $this->assertEquals( $expected, $print_scripts ); |
| 655 | } |
| 656 | |
| 657 | /** |
| 658 | * @ticket 36392 |
| 659 | */ |
| 660 | public function test_wp_add_line_script_before_core_registered_script() { |
| 661 | global $wp_scripts; |
| 662 | |
| 663 | wp_default_scripts( $wp_scripts ); |
| 664 | |
| 665 | $wp_scripts->base_url = ''; |
| 666 | $wp_scripts->do_concat = true; |
| 667 | |
| 668 | $ver = get_bloginfo( 'version' ); |
| 669 | $expected = "<script type='text/javascript'>\nconsole.log(\"before jquery\");\n</script>\n"; |
| 670 | $expected .= "<script type='text/javascript' src='/wp-includes/js/jquery/jquery.js?ver=1.12.3'></script>\n"; |
| 671 | $expected .= "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&load%5B%5D=jquery-migrate&ver={$ver}'></script>\n"; |
| 672 | $expected .= "<script type='text/javascript' src='http://example.com'></script>\n"; |
| 673 | |
| 674 | wp_enqueue_script( 'test-example', 'http://example.com', array( 'jquery' ), null ); |
| 675 | wp_add_inline_script( 'jquery-core', 'console.log("before jquery");', 'before' ); |
| 676 | |
| 677 | wp_print_scripts(); |
| 678 | $print_scripts = get_echo( '_print_scripts' ); |
| 679 | |
| 680 | $this->assertEquals( $expected, $print_scripts ); |
| 681 | } |
| 682 | |
| 683 | /** |
| 684 | * @ticket 36392 |
| 685 | */ |
| 686 | public function test_wp_add_line_script_after_core_registered_script() { |
| 687 | global $wp_scripts; |
| 688 | |
| 689 | wp_default_scripts( $wp_scripts ); |
| 690 | |
| 691 | $wp_scripts->base_url = ''; |
| 692 | $wp_scripts->do_concat = true; |
| 693 | |
| 694 | $ver = get_bloginfo( 'version' ); |
| 695 | $expected = "<script type='text/javascript' src='/wp-includes/js/jquery/jquery.js?ver=1.12.3'></script>\n"; |
| 696 | $expected .= "<script type='text/javascript'>\nconsole.log(\"after jquery\");\n</script>\n"; |
| 697 | $expected .= "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&load%5B%5D=jquery-migrate&ver={$ver}'></script>\n"; |
| 698 | $expected .= "<script type='text/javascript' src='http://example.com'></script>\n"; |
| 699 | |
| 700 | wp_enqueue_script( 'test-example', 'http://example.com', array( 'jquery' ), null ); |
| 701 | wp_add_inline_script( 'jquery-core', 'console.log("after jquery");', 'after' ); |
| 702 | |
| 703 | wp_print_scripts(); |
| 704 | $print_scripts = get_echo( '_print_scripts' ); |
| 705 | |
| 706 | $this->assertEquals( $expected, $print_scripts ); |
| 707 | } |
| 708 | |
| 709 | /** |
| 710 | * @ticket 36392 |
| 711 | */ |
| 712 | public function test_wp_add_inline_script_customize_dependency() { |
| 713 | global $wp_scripts; |
| 714 | |
| 715 | wp_default_scripts( $wp_scripts ); |
| 716 | |
| 717 | $wp_scripts->base_url = ''; |
| 718 | $wp_scripts->do_concat = true; |
| 719 | |
| 720 | $expected_tail = "<![endif]-->\n"; |
| 721 | $expected_tail .= "<script type='text/javascript' src='/customize-dependency.js'></script>\n"; |
| 722 | $expected_tail .= "<script type='text/javascript'>\n"; |
| 723 | $expected_tail .= "tryCustomizeDependency()\n"; |
| 724 | $expected_tail .= "</script>\n"; |
| 725 | |
| 726 | $handle = 'customize-dependency'; |
| 727 | wp_enqueue_script( $handle, '/customize-dependency.js', array( 'customize-controls' ), null ); |
| 728 | wp_add_inline_script( $handle, 'tryCustomizeDependency()' ); |
| 729 | |
| 730 | wp_print_scripts(); |
| 731 | $print_scripts = get_echo( '_print_scripts' ); |
| 732 | |
| 733 | $tail = substr( $print_scripts, strrpos( $print_scripts, "<![endif]-->" ) ); |
| 734 | $this->assertEquals( $expected_tail, $tail ); |
| 735 | } |