| 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_inline_script_customize_dependency() { |
| 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 | $expected_tail = "<![endif]-->\n"; |
| 669 | $expected_tail .= "<script type='text/javascript' src='/customize-dependency.js'></script>\n"; |
| 670 | $expected_tail .= "<script type='text/javascript'>\n"; |
| 671 | $expected_tail .= "tryCustomizeDependency()\n"; |
| 672 | $expected_tail .= "</script>\n"; |
| 673 | |
| 674 | $handle = 'customize-dependency'; |
| 675 | wp_enqueue_script( $handle, '/customize-dependency.js', array( 'customize-controls' ), null ); |
| 676 | wp_add_inline_script( $handle, 'tryCustomizeDependency()' ); |
| 677 | |
| 678 | wp_print_scripts(); |
| 679 | $print_scripts = get_echo( '_print_scripts' ); |
| 680 | |
| 681 | $tail = substr( $print_scripts, strrpos( $print_scripts, "<![endif]-->" ) ); |
| 682 | $this->assertEquals( $expected_tail, $tail ); |
| 683 | } |
| 684 | |
| 685 | /** |
| 686 | * Limitation: One can't add inline script to scripts in the $default_dirs (which in standard use are '/wp-admin/js/' and '/wp-includes/js/') if those scripts have dependencies on each other. |
| 687 | * |
| 688 | * @ticket 36392 |
| 689 | */ |
| 690 | public function test_wp_add_inline_script_limitation() { |
| 691 | global $wp_scripts; |
| 692 | |
| 693 | $wp_scripts->do_concat = true; |
| 694 | $wp_scripts->default_dirs = array('/wp-admin/js/', '/wp-includes/js/'); // Default dirs as in wp-includes/script-loader.php |
| 695 | |
| 696 | wp_enqueue_script( 'one', '/wp-includes/js/script.js' ); |
| 697 | wp_enqueue_script( 'two', '/wp-includes/js/script2.js', array( 'one' ) ); |
| 698 | wp_add_inline_script( 'one', 'console.log("after one");', 'after' ); |
| 699 | |
| 700 | wp_print_scripts(); |
| 701 | $print_scripts = get_echo( '_print_scripts' ); |
| 702 | |
| 703 | $ver = get_bloginfo( 'version' ); |
| 704 | |
| 705 | $expected = ''; |
| 706 | |
| 707 | if ( false ) { |
| 708 | /* Should be */ |
| 709 | $expected .= "<script type='text/javascript' src='/wp-includes/js/script.js?ver={$ver}'></script>\n"; |
| 710 | $expected .= "<script type='text/javascript'>\nconsole.log(\"after one\");\n</script>\n"; |
| 711 | $expected .= "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&load%5B%5D=two&ver={$ver}'></script>\n"; |
| 712 | } else { |
| 713 | /* Actual */ |
| 714 | $expected .= "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&load%5B%5D=two&ver={$ver}'></script>\n"; // Wrong. |
| 715 | $expected .= "<script type='text/javascript' src='/wp-includes/js/script.js?ver={$ver}'></script>\n"; |
| 716 | $expected .= "<script type='text/javascript'>\nconsole.log(\"after one\");\n</script>\n"; |
| 717 | } |
| 718 | |
| 719 | $this->assertEquals( $expected, $print_scripts ); |
| 720 | } |