From 20917f9a45b577c5e7f064820e73c64f9c03f20f Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Wed, 22 Feb 2023 13:54:19 +0000 Subject: [PATCH 01/43] get_eligible_loading_strategy logic --- src/wp-includes/class-wp-scripts.php | 52 ++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 459eeb81cde58..566c20a25c41f 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -757,6 +757,58 @@ private function get_normalized_script_args( $handle, $args = array() ) { return wp_parse_args( $args, $default_args ); } + /** + * Helper function to check if a script has an `after` inline dependency. + */ + private function has_after_inline_dependency( $handle ) { + return false !== $this->get_data( $handle, 'after' ); + } + + /** + * Helper function to check if a script has an `before` inline dependency. + */ + private function has_before_inline_dependency( $handle ) { + return false !== $this->get_data( $handle, 'before' ); + } + + /** + * Get the correct loading strategy for the script. + * + * @param string $handle Handle name of the script. + * @return string $strategy return the final strategy. + */ + private function get_eligible_loading_strategy( $handle = '' ) { + if ( ! isset( $this->registered[ $handle ] ) ) { + return 'blocking'; + } + + $script_args = $this->get_data( $handle, 'script_args' ); + + if ( empty( $this->registered[ $handle ]->deps ) ) { + if ( ! isset( $script_args['strategy'] ) ) { + return 'async'; + } + + // Script eligible for async but user has specific strategy requirement. + return $script_args['strategy']; + } + + $deps_has_async = false; + foreach ( $this->registered[ $handle ]->deps as $dep_handle ) { + $dep_strategy = $this->get_eligible_loading_strategy( $dep_handle ); + if ( 'async' === $dep_strategy ) { + $deps_has_async = true; + break; + } + } + + if ( ! $deps_has_async && ! $this->has_before_inline_dependency( $handle ) ) { + return 'defer'; + } + + return 'blocking'; + } + /** * Resets class properties. * From db02a89e1e370abcde2a6f95636b3f7abac3eccd Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Thu, 23 Feb 2023 14:50:21 +0000 Subject: [PATCH 02/43] Deferring strategy refined --- src/wp-includes/class-wp-scripts.php | 111 +++++++++++++++++++++------ 1 file changed, 86 insertions(+), 25 deletions(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 566c20a25c41f..76fabc7df22ec 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -746,10 +746,8 @@ public function add_data( $handle, $key, $value ) { * @return array Normalized $args array. */ private function get_normalized_script_args( $handle, $args = array() ) { - $default_args = array( - 'in_footer' => false, - 'strategy' => 'blocking', - ); + $default_args = array( 'in_footer' => false ); + // Handle backward compatibility for $in_footer. if ( true === $args ) { $args = array( 'in_footer' => true ); @@ -757,18 +755,82 @@ private function get_normalized_script_args( $handle, $args = array() ) { return wp_parse_args( $args, $default_args ); } + /** + * Get all of the scripts that depend on a script. + * + * @param string $handle The script handle. + * @return array Array of script handles. + */ + private function get_dependents( $handle ) { + $dependents = array(); + + // Iterate over all registered scripts, finding ones that depend on the script. + foreach ( $this->registered as $registered_handle => $args ) { + if ( in_array( $handle, $args->deps, true ) ) { + $dependents[] = $registered_handle; + } + } + return $dependents; + } + + /** + * Get the strategy mentioned during script registration. + * + * @param string $handle The script handle. + * @return string|bool Strategy in script registration, False if not strategy is mentioned.. + */ + private function get_intended_strategy( $handle ) { + $script_args = $this->get_data( $handle, 'script_args' ); + return isset( $script_args['strategy'] ) ? $script_args['strategy'] : false; + } + /** * Helper function to check if a script has an `after` inline dependency. + * + * @param string $handle Name of the item. Should be unique. + * @return bool True on success, false on failure. */ private function has_after_inline_dependency( $handle ) { return false !== $this->get_data( $handle, 'after' ); } + /** - * Helper function to check if a script has an `before` inline dependency. + * Check if all of a scripts dependents are deferrable which is required to maintain execution order. + * + * @param string $handle The script handle. + * @param array $visited An array of already visited script handles used to avoid looping recursion. + * @return bool True if all dependents are deferrable, false otherwise. */ - private function has_before_inline_dependency( $handle ) { - return false !== $this->get_data( $handle, 'before' ); + private function all_dependents_are_deferrable( $handle, $visited = array() ) { + // If this node was already visited, this script can be deferred and the branch ends. + if ( in_array( $handle, $visited, true ) ) { + return true; + } + $visited[] = $handle; + $dependents = $this->get_dependents( $handle ); + // If there are no dependents remaining to consider, the script can be deferred and the branch ends. + if ( empty( $dependents ) ) { + return true; + } + + // Consider each dependent and check if it is deferrable. + foreach ( $dependents as $dependent ) { + // If the dependent script is not using the defer strategy, no script in the chain is deferrable. + $script_args = $this->get_data( $handle, 'script_args' ); + if ( empty( $script_args['strategy'] ) ) { + return false; + } + if ( 'defer' !== $script_args['strategy'] ) { + return false; + } + + // Recursively check all dependent. + if ( ! $this->all_dependents_are_deferrable( $dependent, $visited ) ) { + return false; + } + } + return true; } /** @@ -778,31 +840,30 @@ private function has_before_inline_dependency( $handle ) { * @return string $strategy return the final strategy. */ private function get_eligible_loading_strategy( $handle = '' ) { - if ( ! isset( $this->registered[ $handle ] ) ) { + + $intended_strategy = $this->get_intended_strategy( $handle ); + /** + * Handle known blocking strategy scenarios. + * + * Blocking if not a registered handle. + * If the script has an 'after' inline dependency, then it can't be eligible for async or defer. + */ + if ( ! isset( $this->registered[ $handle ] ) || + $this->has_after_inline_dependency( $handle ) || + 'blocking' === $intended_strategy + ) { return 'blocking'; } - $script_args = $this->get_data( $handle, 'script_args' ); - - if ( empty( $this->registered[ $handle ]->deps ) ) { - if ( ! isset( $script_args['strategy'] ) ) { + // Handling async strategy scenarios. + if ( empty( $this->registered[ $handle ]->deps ) && empty( $this->get_dependents( $handle ) ) ) { + if ( false !== $intended_strategy || 'async' === $intended_strategy ) { return 'async'; } - - // Script eligible for async but user has specific strategy requirement. - return $script_args['strategy']; - } - - $deps_has_async = false; - foreach ( $this->registered[ $handle ]->deps as $dep_handle ) { - $dep_strategy = $this->get_eligible_loading_strategy( $dep_handle ); - if ( 'async' === $dep_strategy ) { - $deps_has_async = true; - break; - } } - if ( ! $deps_has_async && ! $this->has_before_inline_dependency( $handle ) ) { + // Handling defer strategy scenarios. Dependency will never be async. So only checking dependent. + if ( ! $this->all_dependents_are_deferrable( $handle ) ) { return 'defer'; } From af30f17fe9deb510b1d6af16de786dfdfc6bc526 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Thu, 23 Feb 2023 21:55:27 +0000 Subject: [PATCH 03/43] Bug fix --- src/wp-includes/class-wp-scripts.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 76fabc7df22ec..3d31ac673cc3e 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -862,8 +862,8 @@ private function get_eligible_loading_strategy( $handle = '' ) { } } - // Handling defer strategy scenarios. Dependency will never be async. So only checking dependent. - if ( ! $this->all_dependents_are_deferrable( $handle ) ) { + // Handling defer strategy scenarios. Dependency will never be set async. So only checking dependent. + if ( $this->all_dependents_are_deferrable( $handle ) ) { return 'defer'; } From 2ed92d31c157f9d291db92802cf8b5fc6a1bd703 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 24 Feb 2023 01:44:00 +0000 Subject: [PATCH 04/43] removing a few lines with function --- src/wp-includes/class-wp-scripts.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 3d31ac673cc3e..7c8e294afcb8a 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -817,11 +817,7 @@ private function all_dependents_are_deferrable( $handle, $visited = array() ) { // Consider each dependent and check if it is deferrable. foreach ( $dependents as $dependent ) { // If the dependent script is not using the defer strategy, no script in the chain is deferrable. - $script_args = $this->get_data( $handle, 'script_args' ); - if ( empty( $script_args['strategy'] ) ) { - return false; - } - if ( 'defer' !== $script_args['strategy'] ) { + if ( 'defer' !== get_intended_strategy( $dependent ) ) { return false; } From 552d91aafd6651f7b240d200d0dbbf5856e3390d Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 24 Feb 2023 01:49:39 +0000 Subject: [PATCH 05/43] removing default script strategy --- tests/phpunit/tests/dependencies/scripts.php | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 6e810588b6c0a..ae5f51939268b 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -102,20 +102,12 @@ public function test_old_and_new_in_footer_scripts() { public function test_get_normalized_script_args() { global $wp_scripts; $args = array( - 'in_footer' => true, - 'strategy' => 'async', + 'in_footer' => true ); wp_enqueue_script( 'footer-async', '/footer-async.js', array(), null, $args ); $this->assertSame( $args, $wp_scripts->get_data( 'footer-async', 'script_args' ) ); // Test defaults. - $expected_args = array( - 'in_footer' => true, - 'strategy' => 'blocking', - ); - wp_register_script( 'defaults-strategy', '/defaults.js', array(), null, array( 'in_footer' => true ) ); - $this->assertSame( $expected_args, $wp_scripts->get_data( 'defaults-strategy', 'script_args' ) ); - $expected_args = array( 'in_footer' => false, 'strategy' => 'async', @@ -131,12 +123,9 @@ public function test_get_normalized_script_args() { $this->assertSame( false, $wp_scripts->get_data( 'defaults-no-args', 'script_args' ) ); // Test backward compatibility. - $args = array( - 'in_footer' => true, - 'strategy' => 'blocking', - ); + $expected_args = array( 'in_footer' => true ); wp_enqueue_script( 'footer-old', '/footer-async.js', array(), null, true ); - $this->assertSame( $args, $wp_scripts->get_data( 'footer-old', 'script_args' ) ); + $this->assertSame( $expected_args, $wp_scripts->get_data( 'footer-old', 'script_args' ) ); } /** From 1eacd055657d35d1f5b2c655821c2e2a04ecf82c Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 24 Feb 2023 02:35:59 +0000 Subject: [PATCH 06/43] do_item strategy support --- src/wp-includes/class-wp-scripts.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 7c8e294afcb8a..317c1a8ffc002 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -390,8 +390,12 @@ public function do_item( $handle, $group = false ) { return true; } + $strategy = $this->get_eligible_loading_strategy( $handle ); + if( '' !== $strategy ) { + $strategy = ' '.$strategy; + } $tag = $translations . $cond_before . $before_handle; - $tag .= sprintf( "\n", $this->type_attr, $src, esc_attr( $handle ) ); + $tag .= sprintf( "\n", $this->type_attr, $src, esc_attr( $handle ), $strategy ); $tag .= $after_handle . $cond_after; /** @@ -817,7 +821,7 @@ private function all_dependents_are_deferrable( $handle, $visited = array() ) { // Consider each dependent and check if it is deferrable. foreach ( $dependents as $dependent ) { // If the dependent script is not using the defer strategy, no script in the chain is deferrable. - if ( 'defer' !== get_intended_strategy( $dependent ) ) { + if ( 'defer' !== $this->get_intended_strategy( $dependent ) ) { return false; } From 4542775c566f9170e850e98b2e7e052026c069db Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 24 Feb 2023 09:56:21 +0000 Subject: [PATCH 07/43] bug fix --- src/wp-includes/class-wp-scripts.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 7c8e294afcb8a..c89a55235d64e 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -817,7 +817,7 @@ private function all_dependents_are_deferrable( $handle, $visited = array() ) { // Consider each dependent and check if it is deferrable. foreach ( $dependents as $dependent ) { // If the dependent script is not using the defer strategy, no script in the chain is deferrable. - if ( 'defer' !== get_intended_strategy( $dependent ) ) { + if ( 'defer' !== $this->get_intended_strategy( $dependent ) ) { return false; } From d89ab9af812b049a1292837cf0ae050b8c9de946 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 24 Feb 2023 19:05:19 +0000 Subject: [PATCH 08/43] Strategy name update. --- src/wp-includes/class-wp-scripts.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 317c1a8ffc002..e24ef77ea7d7c 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -852,7 +852,7 @@ private function get_eligible_loading_strategy( $handle = '' ) { $this->has_after_inline_dependency( $handle ) || 'blocking' === $intended_strategy ) { - return 'blocking'; + return ''; } // Handling async strategy scenarios. @@ -867,7 +867,7 @@ private function get_eligible_loading_strategy( $handle = '' ) { return 'defer'; } - return 'blocking'; + return ''; } /** From 4105504074ced86b7115d512aaa11a1bd2ba4314 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 24 Feb 2023 19:31:27 +0000 Subject: [PATCH 09/43] fixing bug --- src/wp-includes/class-wp-scripts.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index c89a55235d64e..3109ecfd5ed4c 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -848,12 +848,12 @@ private function get_eligible_loading_strategy( $handle = '' ) { $this->has_after_inline_dependency( $handle ) || 'blocking' === $intended_strategy ) { - return 'blocking'; + return ''; } // Handling async strategy scenarios. if ( empty( $this->registered[ $handle ]->deps ) && empty( $this->get_dependents( $handle ) ) ) { - if ( false !== $intended_strategy || 'async' === $intended_strategy ) { + if ( false === $intended_strategy || 'async' === $intended_strategy ) { return 'async'; } } @@ -863,7 +863,7 @@ private function get_eligible_loading_strategy( $handle = '' ) { return 'defer'; } - return 'blocking'; + return ''; } /** From a9eef9ff2b3745ed130913d33a55c3b190914fcc Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Sun, 26 Feb 2023 04:13:01 +0000 Subject: [PATCH 10/43] strategy test cases --- tests/phpunit/tests/dependencies/scripts.php | 98 ++++++++++++++++++++ 1 file changed, 98 insertions(+) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index ae5f51939268b..68a009ec5d8cf 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -63,8 +63,106 @@ public function test_wp_enqueue_script() { $this->assertSame( '', get_echo( 'wp_print_scripts' ) ); } + + /** + * Test loading strategy. + * + * @ticket 99999 + */ + public function test_loading_strategy() { + global $wp_scripts; + + /** + * Async strategy testing. + */ + // Valid Async cases. + // No dependents, No dependencies then async. + wp_enqueue_script( 'main-script-a1', '/main-script-a1.js', array(), null, array( 'strategy' => 'async' ) ); + $output = get_echo( 'wp_print_scripts' ); + $expected = "\n"; + $this->assertSame( $expected, $output ); + + // Invalid async cases. + // If any dependencies then it's not async. Since dependency is blocking(/defer) final strategy will be defer. + wp_enqueue_script( 'dependency-script-a2', '/dependency-script-a2.js', array(), null ); + wp_enqueue_script( 'main-script-a2', '/main-script-a2.js', array( 'dependency-script-a2' ), null, array( 'strategy' => 'async' ) ); + $output = get_echo( 'wp_print_scripts' ); + $not_expected = ""; + $expected = ""; + $this->assertStringNotContainsString( $not_expected, $output ); + $this->assertStringContainsString( $expected, $output ); + + // If any dependent then it's not async. Since dependent is not set to defer the final strategy will be blocking. + wp_enqueue_script( 'main-script-a3', '/main-script-a3.js', array(), null, array( 'strategy' => 'async' ) ); + wp_enqueue_script( 'dependent-script-a3', '/dependent-script-a3.js', array( 'main-script-a3' ), null ); + $output = get_echo( 'wp_print_scripts' ); + $not_expected = ""; + $expected = ""; + $this->assertStringNotContainsString( $not_expected, $output ); + $this->assertStringContainsString( $expected, $output ); + + /** + * Defer strategy testing. + */ + // Valid Defer cases. + // No dependents, No dependencies and defer strategy. + wp_enqueue_script( 'main-script-d1', '/main-script-d1.js', array(), null, array( 'strategy' => 'defer' ) ); + $output = get_echo( 'wp_print_scripts' ); + $expected = "\n"; + $this->assertSame( $expected, $output ); + + // Main script is defer and all dependencies are either defer/blocking. + wp_enqueue_script( 'dependency-script-d2-1', '/dependency-script-d2-1.js', array(), null, array( 'strategy' => 'defer' ) ); + wp_enqueue_script( 'dependency-script-d2-2', '/dependency-script-d2-2.js', array(), null, array( 'strategy' => 'blocking' ) ); + wp_enqueue_script( 'dependency-script-d2-3', '/dependency-script-d2-3.js', array( 'dependency-script-d2-2' ), null, array( 'strategy' => 'defer' ) ); + wp_enqueue_script( 'main-script-d2', '/main-script-d2.js', array( 'dependency-script-d2-1', 'dependency-script-d2-3' ), null, array( 'strategy' => 'defer' ) ); + $output = get_echo( 'wp_print_scripts' ); + $expected = "\n"; + $this->assertStringContainsString( $expected, $output ); + + // Main script is defer and all dependent are defer. + wp_enqueue_script( 'main-script-d3', '/main-script-d3.js', array(), null, array( 'strategy' => 'defer' ) ); + wp_enqueue_script( 'dependent-script-d3-1', '/dependent-script-d3-1.js', array( 'main-script-d3' ), null, array( 'strategy' => 'defer' ) ); + wp_enqueue_script( 'dependent-script-d3-2', '/dependent-script-d3-2.js', array( 'dependent-script-d3-1' ), null, array( 'strategy' => 'defer' ) ); + wp_enqueue_script( 'dependent-script-d3-3', '/dependent-script-d3-3.js', array( 'dependent-script-d3-2' ), null, array( 'strategy' => 'defer' ) ); + $output = get_echo( 'wp_print_scripts' ); + $expected = "\n"; + $this->assertStringContainsString( $expected, $output ); + + // Invalid defer. + // Main script is defer and all dependent are not defer. Then main script will have blocking(or no) strategy. + wp_enqueue_script( 'main-script-d4', '/main-script-d4.js', array(), null, array( 'strategy' => 'defer' ) ); + wp_enqueue_script( 'dependent-script-d4-1', '/dependent-script-d4-1.js', array( 'main-script-d4' ), null, array( 'strategy' => 'defer' ) ); + wp_enqueue_script( 'dependent-script-d4-2', '/dependent-script-d4-2.js', array( 'dependent-script-d4-1' ), null, array( 'strategy' => 'blocking' ) ); + wp_enqueue_script( 'dependent-script-d4-3', '/dependent-script-d4-3.js', array( 'dependent-script-d4-2' ), null, array( 'strategy' => 'defer' ) ); + $output = get_echo( 'wp_print_scripts' ); + $not_expected = "\n"; + $expected = "\n"; + $this->assertStringNotContainsString( $not_expected, $output ); + $this->assertStringContainsString( $expected, $output ); + + /** + * Blocking(or no) strategy testing. + */ + // Valid Defer cases. + // args set as blocking. + wp_enqueue_script( 'main-script-b1', '/main-script-b1.js', array(), null, array( 'strategy' => 'blocking' ) ); + $output = get_echo( 'wp_print_scripts' ); + $expected = "\n"; + $this->assertSame( $expected, $output ); + + // strategy args not set. + wp_enqueue_script( 'main-script-b2', '/main-script-b2.js', array(), null, array() ); + $output = get_echo( 'wp_print_scripts' ); + $expected = "\n"; + $this->assertSame( $expected, $output ); + + } + /** * Test old and new in_footer logic. + * + * @ticket 99991 */ public function test_old_and_new_in_footer_scripts() { // Scripts in head. From c2c1d0722e74ad7e1db8d1ced181f4693c0a7609 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Sun, 26 Feb 2023 04:14:42 +0000 Subject: [PATCH 11/43] cs fixes --- tests/phpunit/tests/dependencies/scripts.php | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 68a009ec5d8cf..31f4f15f35881 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -66,12 +66,10 @@ public function test_wp_enqueue_script() { /** * Test loading strategy. - * + * * @ticket 99999 */ public function test_loading_strategy() { - global $wp_scripts; - /** * Async strategy testing. */ @@ -150,19 +148,18 @@ public function test_loading_strategy() { $output = get_echo( 'wp_print_scripts' ); $expected = "\n"; $this->assertSame( $expected, $output ); - + // strategy args not set. wp_enqueue_script( 'main-script-b2', '/main-script-b2.js', array(), null, array() ); $output = get_echo( 'wp_print_scripts' ); $expected = "\n"; $this->assertSame( $expected, $output ); - } /** * Test old and new in_footer logic. - * - * @ticket 99991 + * + * @ticket 99999 */ public function test_old_and_new_in_footer_scripts() { // Scripts in head. @@ -200,7 +197,7 @@ public function test_old_and_new_in_footer_scripts() { public function test_get_normalized_script_args() { global $wp_scripts; $args = array( - 'in_footer' => true + 'in_footer' => true, ); wp_enqueue_script( 'footer-async', '/footer-async.js', array(), null, $args ); $this->assertSame( $args, $wp_scripts->get_data( 'footer-async', 'script_args' ) ); From 9eb3f891c804ff951aa57a0ed8cdfc01418be5bf Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Sun, 26 Feb 2023 04:23:19 +0000 Subject: [PATCH 12/43] opt-in + remove blocking after dep --- src/wp-includes/class-wp-scripts.php | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 3109ecfd5ed4c..17e95af679d8f 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -746,8 +746,10 @@ public function add_data( $handle, $key, $value ) { * @return array Normalized $args array. */ private function get_normalized_script_args( $handle, $args = array() ) { - $default_args = array( 'in_footer' => false ); - + $default_args = array( + 'in_footer' => false, + 'strategy' => 'blocking', + ); // Handle backward compatibility for $in_footer. if ( true === $args ) { $args = array( 'in_footer' => true ); @@ -844,18 +846,13 @@ private function get_eligible_loading_strategy( $handle = '' ) { * Blocking if not a registered handle. * If the script has an 'after' inline dependency, then it can't be eligible for async or defer. */ - if ( ! isset( $this->registered[ $handle ] ) || - $this->has_after_inline_dependency( $handle ) || - 'blocking' === $intended_strategy - ) { + if ( ! isset( $this->registered[ $handle ] ) || 'blocking' === $intended_strategy ) { return ''; } // Handling async strategy scenarios. - if ( empty( $this->registered[ $handle ]->deps ) && empty( $this->get_dependents( $handle ) ) ) { - if ( false === $intended_strategy || 'async' === $intended_strategy ) { - return 'async'; - } + if ( empty( $this->registered[ $handle ]->deps ) && empty( $this->get_dependents( $handle ) ) && 'async' === $intended_strategy ) { + return 'async'; } // Handling defer strategy scenarios. Dependency will never be set async. So only checking dependent. From 35279fc922ba4022ae6215cb3dd19ed7f6630211 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Sun, 26 Feb 2023 04:26:22 +0000 Subject: [PATCH 13/43] complex condition towards end --- src/wp-includes/class-wp-scripts.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 17e95af679d8f..7b5d83e87c037 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -851,7 +851,7 @@ private function get_eligible_loading_strategy( $handle = '' ) { } // Handling async strategy scenarios. - if ( empty( $this->registered[ $handle ]->deps ) && empty( $this->get_dependents( $handle ) ) && 'async' === $intended_strategy ) { + if ( 'async' === $intended_strategy && empty( $this->registered[ $handle ]->deps ) && empty( $this->get_dependents( $handle ) ) ) { return 'async'; } From 1de0f319040beb873a75f22758f1cdf273800984 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Sun, 26 Feb 2023 04:35:53 +0000 Subject: [PATCH 14/43] if user not entered --- src/wp-includes/class-wp-scripts.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 7b5d83e87c037..b813cf1eff48f 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -843,10 +843,11 @@ private function get_eligible_loading_strategy( $handle = '' ) { /** * Handle known blocking strategy scenarios. * - * Blocking if not a registered handle. - * If the script has an 'after' inline dependency, then it can't be eligible for async or defer. + * blocking if explicitly set. + * blocking if strategy not set. + * blocking if not a registered handle. */ - if ( ! isset( $this->registered[ $handle ] ) || 'blocking' === $intended_strategy ) { + if ( ! isset( $this->registered[ $handle ] ) || 'blocking' === $intended_strategy || ! $intended_strategy ) { return ''; } From d5ebbc314825fcf8866cba154964c2df08b1ca02 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Sun, 26 Feb 2023 04:36:46 +0000 Subject: [PATCH 15/43] place comment in order --- src/wp-includes/class-wp-scripts.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index b813cf1eff48f..ff4d87a674b1a 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -843,9 +843,9 @@ private function get_eligible_loading_strategy( $handle = '' ) { /** * Handle known blocking strategy scenarios. * - * blocking if explicitly set. - * blocking if strategy not set. * blocking if not a registered handle. + * blocking if explicitly set. + * blocking if script args not set. */ if ( ! isset( $this->registered[ $handle ] ) || 'blocking' === $intended_strategy || ! $intended_strategy ) { return ''; From 5795b4c44bc17b3d86ae039cd4935399a8a43825 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Sun, 26 Feb 2023 04:43:03 +0000 Subject: [PATCH 16/43] restore default values --- tests/phpunit/tests/dependencies/scripts.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 31f4f15f35881..601665e50167e 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -199,8 +199,12 @@ public function test_get_normalized_script_args() { $args = array( 'in_footer' => true, ); + $expected_args = array( + 'in_footer' => true, + 'strategy' => 'blocking' + ); wp_enqueue_script( 'footer-async', '/footer-async.js', array(), null, $args ); - $this->assertSame( $args, $wp_scripts->get_data( 'footer-async', 'script_args' ) ); + $this->assertSame( $expected_args, $wp_scripts->get_data( 'footer-async', 'script_args' ) ); // Test defaults. $expected_args = array( @@ -218,7 +222,10 @@ public function test_get_normalized_script_args() { $this->assertSame( false, $wp_scripts->get_data( 'defaults-no-args', 'script_args' ) ); // Test backward compatibility. - $expected_args = array( 'in_footer' => true ); + $expected_args = array( + 'in_footer' => true, + 'strategy' => 'blocking' + ); wp_enqueue_script( 'footer-old', '/footer-async.js', array(), null, true ); $this->assertSame( $expected_args, $wp_scripts->get_data( 'footer-old', 'script_args' ) ); } From 0ebfaa531bbd1857f394c55243e6823dc0867b2f Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Sun, 26 Feb 2023 04:43:58 +0000 Subject: [PATCH 17/43] phpcs fixes --- tests/phpunit/tests/dependencies/scripts.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 601665e50167e..0d9766caf3a01 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -196,12 +196,12 @@ public function test_old_and_new_in_footer_scripts() { */ public function test_get_normalized_script_args() { global $wp_scripts; - $args = array( + $args = array( 'in_footer' => true, ); $expected_args = array( 'in_footer' => true, - 'strategy' => 'blocking' + 'strategy' => 'blocking', ); wp_enqueue_script( 'footer-async', '/footer-async.js', array(), null, $args ); $this->assertSame( $expected_args, $wp_scripts->get_data( 'footer-async', 'script_args' ) ); @@ -224,7 +224,7 @@ public function test_get_normalized_script_args() { // Test backward compatibility. $expected_args = array( 'in_footer' => true, - 'strategy' => 'blocking' + 'strategy' => 'blocking', ); wp_enqueue_script( 'footer-old', '/footer-async.js', array(), null, true ); $this->assertSame( $expected_args, $wp_scripts->get_data( 'footer-old', 'script_args' ) ); From f6b04839af0f099c3db37f1e5fc436ad42d673ca Mon Sep 17 00:00:00 2001 From: Karthik Thayyil <30643833+kt-12@users.noreply.github.com> Date: Sun, 26 Feb 2023 04:46:02 +0000 Subject: [PATCH 18/43] Update text Co-authored-by: Joe McGill <801097+joemcgill@users.noreply.github.com> --- src/wp-includes/class-wp-scripts.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index ff4d87a674b1a..b3651f8879dd6 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -779,7 +779,7 @@ private function get_dependents( $handle ) { * Get the strategy mentioned during script registration. * * @param string $handle The script handle. - * @return string|bool Strategy in script registration, False if not strategy is mentioned.. + * @return string|bool Strategy set during script registration. False if none was set. */ private function get_intended_strategy( $handle ) { $script_args = $this->get_data( $handle, 'script_args' ); From 7b36d211adb7c1bd41317fb276b0f84943fe6354 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil <30643833+kt-12@users.noreply.github.com> Date: Sun, 26 Feb 2023 04:47:54 +0000 Subject: [PATCH 19/43] Readability Co-authored-by: Joe McGill <801097+joemcgill@users.noreply.github.com> --- src/wp-includes/class-wp-scripts.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index b3651f8879dd6..1cc310d7114b0 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -811,6 +811,7 @@ private function all_dependents_are_deferrable( $handle, $visited = array() ) { } $visited[] = $handle; $dependents = $this->get_dependents( $handle ); + // If there are no dependents remaining to consider, the script can be deferred and the branch ends. if ( empty( $dependents ) ) { return true; From f71319c71a93b0c43214a4e7451bd46e50ce058f Mon Sep 17 00:00:00 2001 From: Karthik Thayyil <30643833+kt-12@users.noreply.github.com> Date: Sun, 26 Feb 2023 04:48:15 +0000 Subject: [PATCH 20/43] consistent with other docblock descriptions Co-authored-by: Joe McGill <801097+joemcgill@users.noreply.github.com> --- src/wp-includes/class-wp-scripts.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 1cc310d7114b0..8d540110676e3 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -835,7 +835,7 @@ private function all_dependents_are_deferrable( $handle, $visited = array() ) { /** * Get the correct loading strategy for the script. * - * @param string $handle Handle name of the script. + * @param string $handle Name of the script. * @return string $strategy return the final strategy. */ private function get_eligible_loading_strategy( $handle = '' ) { From 624e797159224a2b8316974c4457317acaec8c94 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil <30643833+kt-12@users.noreply.github.com> Date: Sun, 26 Feb 2023 04:53:54 +0000 Subject: [PATCH 21/43] casting the result Co-authored-by: Joe McGill <801097+joemcgill@users.noreply.github.com> --- src/wp-includes/class-wp-scripts.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 8d540110676e3..57e830127c668 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -793,7 +793,7 @@ private function get_intended_strategy( $handle ) { * @return bool True on success, false on failure. */ private function has_after_inline_dependency( $handle ) { - return false !== $this->get_data( $handle, 'after' ); + return (bool) $this->get_data( $handle, 'after' ); } From b7334b7b94680dff7f073607cd6e8050b90aa523 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Sun, 26 Feb 2023 04:57:52 +0000 Subject: [PATCH 22/43] wordpress coding standards --- src/wp-includes/class-wp-scripts.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index ff4d87a674b1a..65dd51bcede80 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -840,7 +840,7 @@ private function all_dependents_are_deferrable( $handle, $visited = array() ) { private function get_eligible_loading_strategy( $handle = '' ) { $intended_strategy = $this->get_intended_strategy( $handle ); - /** + /* * Handle known blocking strategy scenarios. * * blocking if not a registered handle. From 5305f1caf67e488d889069fe295c0cfe6d18525a Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Mon, 27 Feb 2023 09:10:56 +0000 Subject: [PATCH 23/43] restore test case --- tests/phpunit/tests/dependencies/scripts.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index ae5f51939268b..be014dff0237f 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -102,7 +102,8 @@ public function test_old_and_new_in_footer_scripts() { public function test_get_normalized_script_args() { global $wp_scripts; $args = array( - 'in_footer' => true + 'in_footer' => true, + 'strategy' => 'async', ); wp_enqueue_script( 'footer-async', '/footer-async.js', array(), null, $args ); $this->assertSame( $args, $wp_scripts->get_data( 'footer-async', 'script_args' ) ); @@ -123,7 +124,10 @@ public function test_get_normalized_script_args() { $this->assertSame( false, $wp_scripts->get_data( 'defaults-no-args', 'script_args' ) ); // Test backward compatibility. - $expected_args = array( 'in_footer' => true ); + $expected_args = array( + 'in_footer' => true, + 'strategy' => 'blocking', + ); wp_enqueue_script( 'footer-old', '/footer-async.js', array(), null, true ); $this->assertSame( $expected_args, $wp_scripts->get_data( 'footer-old', 'script_args' ) ); } From 073684a5d93b3869640f268fcc8f5de1d330841f Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Mon, 27 Feb 2023 09:13:29 +0000 Subject: [PATCH 24/43] add temporary ticket id --- tests/phpunit/tests/dependencies/scripts.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index be014dff0237f..f9c51de6198a9 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -98,6 +98,8 @@ public function test_old_and_new_in_footer_scripts() { /** * Test normalized script args. + * + * @ticket 99999 */ public function test_get_normalized_script_args() { global $wp_scripts; From 5e343b87c54b26ebffb3b89235919343072fa312 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Mon, 27 Feb 2023 09:14:46 +0000 Subject: [PATCH 25/43] fix test bug --- tests/phpunit/tests/dependencies/scripts.php | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 0d9766caf3a01..6298edf224304 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -193,18 +193,17 @@ public function test_old_and_new_in_footer_scripts() { /** * Test normalized script args. + * + * @ticket 99999 */ public function test_get_normalized_script_args() { global $wp_scripts; - $args = array( + $args = array( 'in_footer' => true, - ); - $expected_args = array( - 'in_footer' => true, - 'strategy' => 'blocking', + 'strategy' => 'async', ); wp_enqueue_script( 'footer-async', '/footer-async.js', array(), null, $args ); - $this->assertSame( $expected_args, $wp_scripts->get_data( 'footer-async', 'script_args' ) ); + $this->assertSame( $args, $wp_scripts->get_data( 'footer-async', 'script_args' ) ); // Test defaults. $expected_args = array( From b56c0e1af3eb5c8ac595d5b0b7e91e91f7083bf8 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Mon, 27 Feb 2023 09:18:47 +0000 Subject: [PATCH 26/43] restore test case --- tests/phpunit/tests/dependencies/scripts.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index f9c51de6198a9..c9a09b4a4f195 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -111,6 +111,13 @@ public function test_get_normalized_script_args() { $this->assertSame( $args, $wp_scripts->get_data( 'footer-async', 'script_args' ) ); // Test defaults. + $expected_args = array( + 'in_footer' => true, + 'strategy' => 'blocking', + ); + wp_register_script( 'defaults-strategy', '/defaults.js', array(), null, array( 'in_footer' => true ) ); + $this->assertSame( $expected_args, $wp_scripts->get_data( 'defaults-strategy', 'script_args' ) ); + $expected_args = array( 'in_footer' => false, 'strategy' => 'async', From b70ee6c18282b3db2ef343507748d100604c7991 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Mon, 27 Feb 2023 09:30:09 +0000 Subject: [PATCH 27/43] temporary ticket added for testing --- tests/phpunit/tests/dependencies/scripts.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index c9a09b4a4f195..7fc36739fa81b 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -65,6 +65,8 @@ public function test_wp_enqueue_script() { /** * Test old and new in_footer logic. + * + * @ticket 99999 */ public function test_old_and_new_in_footer_scripts() { // Scripts in head. From 2deadcf156a1d01eb8acf7bbbe205fa744a5dc7d Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Mon, 27 Feb 2023 09:31:36 +0000 Subject: [PATCH 28/43] remove extra line --- tests/phpunit/tests/dependencies/scripts.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 40358e416b6c2..ec0cc0a2fa45a 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -63,7 +63,6 @@ public function test_wp_enqueue_script() { $this->assertSame( '', get_echo( 'wp_print_scripts' ) ); } - /** * Test loading strategy. * From fcaa62e27d13405710155ff3ffdbc60179063788 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Mon, 27 Feb 2023 09:57:35 +0000 Subject: [PATCH 29/43] remove after dependency function --- src/wp-includes/class-wp-scripts.php | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 951658fe001ee..16d3c6356d7a0 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -786,17 +786,6 @@ private function get_intended_strategy( $handle ) { return isset( $script_args['strategy'] ) ? $script_args['strategy'] : false; } - /** - * Helper function to check if a script has an `after` inline dependency. - * - * @param string $handle Name of the item. Should be unique. - * @return bool True on success, false on failure. - */ - private function has_after_inline_dependency( $handle ) { - return (bool) $this->get_data( $handle, 'after' ); - } - - /** * Check if all of a scripts dependents are deferrable which is required to maintain execution order. * From d578e50a511c6d5fdf3ed8dcf4b159ded10c2d79 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil <30643833+kt-12@users.noreply.github.com> Date: Mon, 27 Feb 2023 18:31:41 +0000 Subject: [PATCH 30/43] fix doc block text Co-authored-by: Simon Dowdles <72872375+10upsimon@users.noreply.github.com> --- src/wp-includes/class-wp-scripts.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 16d3c6356d7a0..c724615a2266e 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -822,7 +822,7 @@ private function all_dependents_are_deferrable( $handle, $visited = array() ) { } /** - * Get the correct loading strategy for the script. + * Get the most eligible loading strategy for a script. * * @param string $handle Name of the script. * @return string $strategy return the final strategy. From 5262bf78f6a25f373ef1b5670d7ec1f98811f00e Mon Sep 17 00:00:00 2001 From: Karthik Thayyil <30643833+kt-12@users.noreply.github.com> Date: Mon, 27 Feb 2023 18:34:04 +0000 Subject: [PATCH 31/43] Correct grammar Co-authored-by: Simon Dowdles <72872375+10upsimon@users.noreply.github.com> --- src/wp-includes/class-wp-scripts.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index c724615a2266e..6f2a7bc0e3a19 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -813,7 +813,7 @@ private function all_dependents_are_deferrable( $handle, $visited = array() ) { return false; } - // Recursively check all dependent. + // Recursively check all dependents. if ( ! $this->all_dependents_are_deferrable( $dependent, $visited ) ) { return false; } From 2970f1a195b8e39ba4cb2e4b31d66b4ca2a858f1 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil <30643833+kt-12@users.noreply.github.com> Date: Mon, 27 Feb 2023 18:34:39 +0000 Subject: [PATCH 32/43] Fix doc block string. Co-authored-by: Simon Dowdles <72872375+10upsimon@users.noreply.github.com> --- src/wp-includes/class-wp-scripts.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 6f2a7bc0e3a19..70b44ab21ba62 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -824,7 +824,7 @@ private function all_dependents_are_deferrable( $handle, $visited = array() ) { /** * Get the most eligible loading strategy for a script. * - * @param string $handle Name of the script. + * @param string $handle The registered handle of the script. * @return string $strategy return the final strategy. */ private function get_eligible_loading_strategy( $handle = '' ) { From 28af60db70410081bf8220eb6ca4149c889d146e Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Mon, 27 Feb 2023 18:40:25 +0000 Subject: [PATCH 33/43] change variable name --- src/wp-includes/class-wp-scripts.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 70b44ab21ba62..d8bad6b4b2914 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -790,15 +790,15 @@ private function get_intended_strategy( $handle ) { * Check if all of a scripts dependents are deferrable which is required to maintain execution order. * * @param string $handle The script handle. - * @param array $visited An array of already visited script handles used to avoid looping recursion. + * @param array $checked An array of already checked script handles, used to avoid looping recursion. * @return bool True if all dependents are deferrable, false otherwise. */ - private function all_dependents_are_deferrable( $handle, $visited = array() ) { - // If this node was already visited, this script can be deferred and the branch ends. - if ( in_array( $handle, $visited, true ) ) { + private function all_dependents_are_deferrable( $handle, $checked = array() ) { + // If this node was already checked, this script can be deferred and the branch ends. + if ( in_array( $handle, $checked, true ) ) { return true; } - $visited[] = $handle; + $checked[] = $handle; $dependents = $this->get_dependents( $handle ); // If there are no dependents remaining to consider, the script can be deferred and the branch ends. @@ -814,7 +814,7 @@ private function all_dependents_are_deferrable( $handle, $visited = array() ) { } // Recursively check all dependents. - if ( ! $this->all_dependents_are_deferrable( $dependent, $visited ) ) { + if ( ! $this->all_dependents_are_deferrable( $dependent, $checked ) ) { return false; } } From f746dfdb8a8d404cab0a2f5c963a4a44874e23df Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Mon, 27 Feb 2023 18:52:52 +0000 Subject: [PATCH 34/43] doc block changes, better word --- src/wp-includes/class-wp-scripts.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index d8bad6b4b2914..8ca9498494538 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -776,7 +776,7 @@ private function get_dependents( $handle ) { } /** - * Get the strategy mentioned during script registration. + * Get the strategy assigned during script registration. * * @param string $handle The script handle. * @return string|bool Strategy set during script registration. False if none was set. From d4bbaff684e5019007ec1c09f14aee1947ace851 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil <30643833+kt-12@users.noreply.github.com> Date: Mon, 27 Feb 2023 19:11:34 +0000 Subject: [PATCH 35/43] spacing issue. Co-authored-by: Simon Dowdles <72872375+10upsimon@users.noreply.github.com> --- src/wp-includes/class-wp-scripts.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index e73ee878cf9c7..6623adc4d4fa3 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -392,7 +392,7 @@ public function do_item( $handle, $group = false ) { $strategy = $this->get_eligible_loading_strategy( $handle ); if( '' !== $strategy ) { - $strategy = ' '.$strategy; + $strategy = ' ' . $strategy; } $tag = $translations . $cond_before . $before_handle; $tag .= sprintf( "\n", $this->type_attr, $src, esc_attr( $handle ), $strategy ); From 01e1d4fd15139c5926c6515991fda6e933e80588 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil <30643833+kt-12@users.noreply.github.com> Date: Tue, 28 Feb 2023 12:55:58 +0000 Subject: [PATCH 36/43] update placeholder with numbered placeholder Co-authored-by: Joe McGill <801097+joemcgill@users.noreply.github.com> --- src/wp-includes/class-wp-scripts.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 6623adc4d4fa3..28af51c80c720 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -395,7 +395,13 @@ public function do_item( $handle, $group = false ) { $strategy = ' ' . $strategy; } $tag = $translations . $cond_before . $before_handle; - $tag .= sprintf( "\n", $this->type_attr, $src, esc_attr( $handle ), $strategy ); + $tag .= sprintf( + "\n", + $this->type_attr, + esc_url($src), + esc_attr( $handle ), + $strategy + ); $tag .= $after_handle . $cond_after; /** From a36795164d4c4b9b0d0de091fdcc26a531b9f5d3 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Tue, 28 Feb 2023 13:57:18 +0000 Subject: [PATCH 37/43] Change ticket id to trac id --- tests/phpunit/tests/dependencies/scripts.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index ec0cc0a2fa45a..2ea26a67f4b29 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -66,7 +66,7 @@ public function test_wp_enqueue_script() { /** * Test loading strategy. * - * @ticket 99999 + * @ticket 12009 */ public function test_loading_strategy() { /** @@ -158,7 +158,7 @@ public function test_loading_strategy() { /** * Test old and new in_footer logic. * - * @ticket 99999 + * @ticket 12009 */ public function test_old_and_new_in_footer_scripts() { // Scripts in head. @@ -193,7 +193,7 @@ public function test_old_and_new_in_footer_scripts() { /** * Test normalized script args. * - * @ticket 99999 + * @ticket 12009 */ public function test_get_normalized_script_args() { global $wp_scripts; From 51e3e416214bde1084692c2197c5e4bfe762934d Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Tue, 28 Feb 2023 13:59:06 +0000 Subject: [PATCH 38/43] Switching from numbered to normal substitution --- src/wp-includes/class-wp-scripts.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 28af51c80c720..0d30a3bac1b51 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -392,15 +392,15 @@ public function do_item( $handle, $group = false ) { $strategy = $this->get_eligible_loading_strategy( $handle ); if( '' !== $strategy ) { - $strategy = ' ' . $strategy; + $strategy = ' '.$strategy; } $tag = $translations . $cond_before . $before_handle; $tag .= sprintf( - "\n", - $this->type_attr, - esc_url($src), - esc_attr( $handle ), - $strategy + "\n", + $this->type_attr, + esc_url( $src ), + esc_attr( $handle ), + $strategy ); $tag .= $after_handle . $cond_after; From 62cbf6079be3d9aa44d7e69ef597a51d49c9ec05 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Tue, 28 Feb 2023 14:51:05 +0000 Subject: [PATCH 39/43] Breaking test into multiple test functions --- tests/phpunit/tests/dependencies/scripts.php | 53 +++++++++++--------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 2ea26a67f4b29..dd9bd4aff8a83 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -64,44 +64,45 @@ public function test_wp_enqueue_script() { } /** - * Test loading strategy. + * Test valid async loading strategy case. * * @ticket 12009 */ - public function test_loading_strategy() { - /** - * Async strategy testing. - */ - // Valid Async cases. + public function test_loading_strategy_with_valid_async_registration() { // No dependents, No dependencies then async. wp_enqueue_script( 'main-script-a1', '/main-script-a1.js', array(), null, array( 'strategy' => 'async' ) ); $output = get_echo( 'wp_print_scripts' ); $expected = "\n"; $this->assertSame( $expected, $output ); + } - // Invalid async cases. + /** + * Test invalid async loading strategy cases. + * + * @ticket 12009 + */ + public function test_loading_strategy_with_invalid_async_registration() { // If any dependencies then it's not async. Since dependency is blocking(/defer) final strategy will be defer. wp_enqueue_script( 'dependency-script-a2', '/dependency-script-a2.js', array(), null ); wp_enqueue_script( 'main-script-a2', '/main-script-a2.js', array( 'dependency-script-a2' ), null, array( 'strategy' => 'async' ) ); $output = get_echo( 'wp_print_scripts' ); - $not_expected = ""; $expected = ""; - $this->assertStringNotContainsString( $not_expected, $output ); $this->assertStringContainsString( $expected, $output ); // If any dependent then it's not async. Since dependent is not set to defer the final strategy will be blocking. wp_enqueue_script( 'main-script-a3', '/main-script-a3.js', array(), null, array( 'strategy' => 'async' ) ); wp_enqueue_script( 'dependent-script-a3', '/dependent-script-a3.js', array( 'main-script-a3' ), null ); $output = get_echo( 'wp_print_scripts' ); - $not_expected = ""; $expected = ""; - $this->assertStringNotContainsString( $not_expected, $output ); $this->assertStringContainsString( $expected, $output ); + } - /** - * Defer strategy testing. - */ - // Valid Defer cases. + /** + * Test valid defer loading strategy cases. + * + * @ticket 12009 + */ + public function test_loading_strategy_with_valid_defer_registration() { // No dependents, No dependencies and defer strategy. wp_enqueue_script( 'main-script-d1', '/main-script-d1.js', array(), null, array( 'strategy' => 'defer' ) ); $output = get_echo( 'wp_print_scripts' ); @@ -125,24 +126,30 @@ public function test_loading_strategy() { $output = get_echo( 'wp_print_scripts' ); $expected = "\n"; $this->assertStringContainsString( $expected, $output ); + } - // Invalid defer. + /** + * Test invalid defer loading strategy case. + * + * @ticket 12009 + */ + public function test_loading_strategy_with_invalid_defer_registration() { // Main script is defer and all dependent are not defer. Then main script will have blocking(or no) strategy. wp_enqueue_script( 'main-script-d4', '/main-script-d4.js', array(), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'dependent-script-d4-1', '/dependent-script-d4-1.js', array( 'main-script-d4' ), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'dependent-script-d4-2', '/dependent-script-d4-2.js', array( 'dependent-script-d4-1' ), null, array( 'strategy' => 'blocking' ) ); wp_enqueue_script( 'dependent-script-d4-3', '/dependent-script-d4-3.js', array( 'dependent-script-d4-2' ), null, array( 'strategy' => 'defer' ) ); $output = get_echo( 'wp_print_scripts' ); - $not_expected = "\n"; $expected = "\n"; - $this->assertStringNotContainsString( $not_expected, $output ); $this->assertStringContainsString( $expected, $output ); + } - /** - * Blocking(or no) strategy testing. - */ - // Valid Defer cases. - // args set as blocking. + /** + * Test valid blocking loading strategy cases. + * + * @ticket 12009 + */ + public function test_loading_strategy_with_valid_blocking_registration() { wp_enqueue_script( 'main-script-b1', '/main-script-b1.js', array(), null, array( 'strategy' => 'blocking' ) ); $output = get_echo( 'wp_print_scripts' ); $expected = "\n"; From 0f80794456215b3a3559d42989e331dca659fddc Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Tue, 28 Feb 2023 15:24:32 +0000 Subject: [PATCH 40/43] breaking test to dataprovider --- tests/phpunit/tests/dependencies/scripts.php | 57 +++++++++++--------- 1 file changed, 33 insertions(+), 24 deletions(-) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index dd9bd4aff8a83..49104d6b68eba 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -85,47 +85,56 @@ public function test_loading_strategy_with_invalid_async_registration() { // If any dependencies then it's not async. Since dependency is blocking(/defer) final strategy will be defer. wp_enqueue_script( 'dependency-script-a2', '/dependency-script-a2.js', array(), null ); wp_enqueue_script( 'main-script-a2', '/main-script-a2.js', array( 'dependency-script-a2' ), null, array( 'strategy' => 'async' ) ); - $output = get_echo( 'wp_print_scripts' ); - $expected = ""; - $this->assertStringContainsString( $expected, $output ); + $output = get_echo( 'wp_print_scripts' ); + $expected = ""; + $this->assertStringContainsString( $expected, $output, 'Expected defer.' ); // If any dependent then it's not async. Since dependent is not set to defer the final strategy will be blocking. wp_enqueue_script( 'main-script-a3', '/main-script-a3.js', array(), null, array( 'strategy' => 'async' ) ); wp_enqueue_script( 'dependent-script-a3', '/dependent-script-a3.js', array( 'main-script-a3' ), null ); - $output = get_echo( 'wp_print_scripts' ); - $expected = ""; - $this->assertStringContainsString( $expected, $output ); + $output = get_echo( 'wp_print_scripts' ); + $expected = ""; + $this->assertStringContainsString( $expected, $output, 'Expected blocking.' ); } /** * Test valid defer loading strategy cases. * * @ticket 12009 + * @dataProvider data_loading_strategy_with_valid_defer_registration */ - public function test_loading_strategy_with_valid_defer_registration() { + public function test_loading_strategy_with_valid_defer_registration( $expected, $output ) { + $this->assertStringContainsString( $expected, $output ); + } + + public function data_loading_strategy_with_valid_defer_registration() { + $data = array(); + // No dependents, No dependencies and defer strategy. - wp_enqueue_script( 'main-script-d1', '/main-script-d1.js', array(), null, array( 'strategy' => 'defer' ) ); + wp_enqueue_script( 'main-script-d1', 'http://example.com/main-script-d1.js', array(), null, array( 'strategy' => 'defer' ) ); $output = get_echo( 'wp_print_scripts' ); - $expected = "\n"; - $this->assertSame( $expected, $output ); + $expected = "\n"; + array_push( $data, array( $expected, $output ) ); // Main script is defer and all dependencies are either defer/blocking. - wp_enqueue_script( 'dependency-script-d2-1', '/dependency-script-d2-1.js', array(), null, array( 'strategy' => 'defer' ) ); - wp_enqueue_script( 'dependency-script-d2-2', '/dependency-script-d2-2.js', array(), null, array( 'strategy' => 'blocking' ) ); - wp_enqueue_script( 'dependency-script-d2-3', '/dependency-script-d2-3.js', array( 'dependency-script-d2-2' ), null, array( 'strategy' => 'defer' ) ); - wp_enqueue_script( 'main-script-d2', '/main-script-d2.js', array( 'dependency-script-d2-1', 'dependency-script-d2-3' ), null, array( 'strategy' => 'defer' ) ); + wp_enqueue_script( 'dependency-script-d2-1', 'http://example.com/dependency-script-d2-1.js', array(), null, array( 'strategy' => 'defer' ) ); + wp_enqueue_script( 'dependency-script-d2-2', 'http://example.com/dependency-script-d2-2.js', array(), null, array( 'strategy' => 'blocking' ) ); + wp_enqueue_script( 'dependency-script-d2-3', 'http://example.com/dependency-script-d2-3.js', array( 'dependency-script-d2-2' ), null, array( 'strategy' => 'defer' ) ); + wp_enqueue_script( 'main-script-d2', 'http://example.com/main-script-d2.js', array( 'dependency-script-d2-1', 'dependency-script-d2-3' ), null, array( 'strategy' => 'defer' ) ); $output = get_echo( 'wp_print_scripts' ); - $expected = "\n"; - $this->assertStringContainsString( $expected, $output ); + $expected = "\n"; + array_push( $data, array( $expected, $output ) ); // Main script is defer and all dependent are defer. - wp_enqueue_script( 'main-script-d3', '/main-script-d3.js', array(), null, array( 'strategy' => 'defer' ) ); - wp_enqueue_script( 'dependent-script-d3-1', '/dependent-script-d3-1.js', array( 'main-script-d3' ), null, array( 'strategy' => 'defer' ) ); - wp_enqueue_script( 'dependent-script-d3-2', '/dependent-script-d3-2.js', array( 'dependent-script-d3-1' ), null, array( 'strategy' => 'defer' ) ); - wp_enqueue_script( 'dependent-script-d3-3', '/dependent-script-d3-3.js', array( 'dependent-script-d3-2' ), null, array( 'strategy' => 'defer' ) ); + wp_enqueue_script( 'main-script-d3', 'http://example.com/main-script-d3.js', array(), null, array( 'strategy' => 'defer' ) ); + wp_enqueue_script( 'dependent-script-d3-1', 'http://example.com/dependent-script-d3-1.js', array( 'main-script-d3' ), null, array( 'strategy' => 'defer' ) ); + wp_enqueue_script( 'dependent-script-d3-2', 'http://example.com/dependent-script-d3-2.js', array( 'dependent-script-d3-1' ), null, array( 'strategy' => 'defer' ) ); + wp_enqueue_script( 'dependent-script-d3-3', 'http://example.com/dependent-script-d3-3.js', array( 'dependent-script-d3-2' ), null, array( 'strategy' => 'defer' ) ); $output = get_echo( 'wp_print_scripts' ); - $expected = "\n"; - $this->assertStringContainsString( $expected, $output ); + $expected = "\n"; + array_push( $data, array( $expected, $output ) ); + + return $data; } /** @@ -139,8 +148,8 @@ public function test_loading_strategy_with_invalid_defer_registration() { wp_enqueue_script( 'dependent-script-d4-1', '/dependent-script-d4-1.js', array( 'main-script-d4' ), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'dependent-script-d4-2', '/dependent-script-d4-2.js', array( 'dependent-script-d4-1' ), null, array( 'strategy' => 'blocking' ) ); wp_enqueue_script( 'dependent-script-d4-3', '/dependent-script-d4-3.js', array( 'dependent-script-d4-2' ), null, array( 'strategy' => 'defer' ) ); - $output = get_echo( 'wp_print_scripts' ); - $expected = "\n"; + $output = get_echo( 'wp_print_scripts' ); + $expected = "\n"; $this->assertStringContainsString( $expected, $output ); } From f20cfeae472f53d69c30090a488fc925bc8ce3df Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Tue, 28 Feb 2023 15:36:24 +0000 Subject: [PATCH 41/43] add async and defer check in dependent check --- src/wp-includes/class-wp-scripts.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 8ca9498494538..ffd4c426a504e 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -808,8 +808,8 @@ private function all_dependents_are_deferrable( $handle, $checked = array() ) { // Consider each dependent and check if it is deferrable. foreach ( $dependents as $dependent ) { - // If the dependent script is not using the defer strategy, no script in the chain is deferrable. - if ( 'defer' !== $this->get_intended_strategy( $dependent ) ) { + // If the dependent script is not using the defer or async strategy, no script in the chain is deferrable. + if ( ! in_array( $this->get_intended_strategy( $dependent ), array( 'defer', 'async' ), true ) ) { return false; } @@ -828,16 +828,18 @@ private function all_dependents_are_deferrable( $handle, $checked = array() ) { * @return string $strategy return the final strategy. */ private function get_eligible_loading_strategy( $handle = '' ) { + if ( ! isset( $this->registered[ $handle ] ) ) { + return ''; + } $intended_strategy = $this->get_intended_strategy( $handle ); /* * Handle known blocking strategy scenarios. * - * blocking if not a registered handle. - * blocking if explicitly set. * blocking if script args not set. + * blocking if explicitly set. */ - if ( ! isset( $this->registered[ $handle ] ) || 'blocking' === $intended_strategy || ! $intended_strategy ) { + if ( ! $intended_strategy || 'blocking' === $intended_strategy ) { return ''; } @@ -846,7 +848,7 @@ private function get_eligible_loading_strategy( $handle = '' ) { return 'async'; } - // Handling defer strategy scenarios. Dependency will never be set async. So only checking dependent. + // Handling defer strategy scenarios. if ( $this->all_dependents_are_deferrable( $handle ) ) { return 'defer'; } From b961108c3561ef3d28f79971c6dbd32c2f12ac13 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Tue, 28 Feb 2023 15:58:28 +0000 Subject: [PATCH 42/43] defer with async dependent --- tests/phpunit/tests/dependencies/scripts.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 49104d6b68eba..0ba0b31fcac58 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -137,6 +137,26 @@ public function data_loading_strategy_with_valid_defer_registration() { return $data; } + /** + * Test valid defer loading with async dependent. + * + * @ticket 12009 + */ + public function test_defer_with_async_dependent() { + // case with one async dependent. + wp_enqueue_script( 'main-script-d4', '/main-script-d4.js', array(), null, array( 'strategy' => 'defer' ) ); + wp_enqueue_script( 'dependent-script-d4-1', '/dependent-script-d4-1.js', array( 'main-script-d4' ), null, array( 'strategy' => 'defer' ) ); + wp_enqueue_script( 'dependent-script-d4-2', '/dependent-script-d4-2.js', array( 'dependent-script-d4-1' ), null, array( 'strategy' => 'async' ) ); + wp_enqueue_script( 'dependent-script-d4-3', '/dependent-script-d4-3.js', array( 'dependent-script-d4-2' ), null, array( 'strategy' => 'defer' ) ); + $output = get_echo( 'wp_print_scripts' ); + $expected = "\n"; + $expected .= "\n"; + $expected .= "\n"; + $expected .= "\n"; + + $this->assertSame( $expected, $output ); + } + /** * Test invalid defer loading strategy case. * From 70fb6c6e5ad81f2f1db661c6fbbfad3a86ad633f Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Tue, 28 Feb 2023 18:39:37 +0000 Subject: [PATCH 43/43] Add failure messages --- tests/phpunit/tests/dependencies/scripts.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 0ba0b31fcac58..cd1582cc97c39 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -103,8 +103,8 @@ public function test_loading_strategy_with_invalid_async_registration() { * @ticket 12009 * @dataProvider data_loading_strategy_with_valid_defer_registration */ - public function test_loading_strategy_with_valid_defer_registration( $expected, $output ) { - $this->assertStringContainsString( $expected, $output ); + public function test_loading_strategy_with_valid_defer_registration( $expected, $output, $message ) { + $this->assertStringContainsString( $expected, $output, $message ); } public function data_loading_strategy_with_valid_defer_registration() { @@ -114,7 +114,7 @@ public function data_loading_strategy_with_valid_defer_registration() { wp_enqueue_script( 'main-script-d1', 'http://example.com/main-script-d1.js', array(), null, array( 'strategy' => 'defer' ) ); $output = get_echo( 'wp_print_scripts' ); $expected = "\n"; - array_push( $data, array( $expected, $output ) ); + array_push( $data, array( $expected, $output, 'Expected defer, as there is no dependent or dependency' ) ); // Main script is defer and all dependencies are either defer/blocking. wp_enqueue_script( 'dependency-script-d2-1', 'http://example.com/dependency-script-d2-1.js', array(), null, array( 'strategy' => 'defer' ) ); @@ -123,7 +123,7 @@ public function data_loading_strategy_with_valid_defer_registration() { wp_enqueue_script( 'main-script-d2', 'http://example.com/main-script-d2.js', array( 'dependency-script-d2-1', 'dependency-script-d2-3' ), null, array( 'strategy' => 'defer' ) ); $output = get_echo( 'wp_print_scripts' ); $expected = "\n"; - array_push( $data, array( $expected, $output ) ); + array_push( $data, array( $expected, $output, 'Expected defer, as all dependencies are either deferred or blocking' ) ); // Main script is defer and all dependent are defer. wp_enqueue_script( 'main-script-d3', 'http://example.com/main-script-d3.js', array(), null, array( 'strategy' => 'defer' ) ); @@ -132,7 +132,7 @@ public function data_loading_strategy_with_valid_defer_registration() { wp_enqueue_script( 'dependent-script-d3-3', 'http://example.com/dependent-script-d3-3.js', array( 'dependent-script-d3-2' ), null, array( 'strategy' => 'defer' ) ); $output = get_echo( 'wp_print_scripts' ); $expected = "\n"; - array_push( $data, array( $expected, $output ) ); + array_push( $data, array( $expected, $output, 'Expected defer, as all dependents have defer loading strategy' ) ); return $data; } @@ -148,7 +148,7 @@ public function test_defer_with_async_dependent() { wp_enqueue_script( 'dependent-script-d4-1', '/dependent-script-d4-1.js', array( 'main-script-d4' ), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'dependent-script-d4-2', '/dependent-script-d4-2.js', array( 'dependent-script-d4-1' ), null, array( 'strategy' => 'async' ) ); wp_enqueue_script( 'dependent-script-d4-3', '/dependent-script-d4-3.js', array( 'dependent-script-d4-2' ), null, array( 'strategy' => 'defer' ) ); - $output = get_echo( 'wp_print_scripts' ); + $output = get_echo( 'wp_print_scripts' ); $expected = "\n"; $expected .= "\n"; $expected .= "\n";