From 464ce169c4914becb279d993c31a026ef01997ae Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Thu, 9 Feb 2023 02:52:02 +0000 Subject: [PATCH 001/256] WP register --- src/wp-includes/functions.wp-scripts.php | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/wp-includes/functions.wp-scripts.php b/src/wp-includes/functions.wp-scripts.php index d6c8aaddd2ad5..b1467ee2ffb6b 100644 --- a/src/wp-includes/functions.wp-scripts.php +++ b/src/wp-includes/functions.wp-scripts.php @@ -166,18 +166,24 @@ function wp_add_inline_script( $handle, $data, $position = 'after' ) { * as a query string for cache busting purposes. If version is set to false, a version * number is automatically added equal to current installed WordPress version. * If set to null, no version is added. - * @param bool $in_footer Optional. Whether to enqueue the script before `` instead of in the ``. - * Default 'false'. + * @param array $args { + * + * @type boolean $in_footer Optional. Default true + * @type string $strategy Optional. Default 'blocking' + * + * } * @return bool Whether the script has been registered. True on success, false on failure. */ -function wp_register_script( $handle, $src, $deps = array(), $ver = false, $in_footer = false ) { +function wp_register_script( $handle, $src, $deps = array(), $ver = false, $args = array() ) { _wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle ); $wp_scripts = wp_scripts(); $registered = $wp_scripts->add( $handle, $src, $deps, $ver ); - if ( $in_footer ) { - $wp_scripts->add_data( $handle, 'group', 1 ); + + + if ( ! empty( $args ) ) { + $wp_scripts->add_data( $handle, 'args', $args ); } return $registered; From 54e6509337b84fe398ec1c6f0ebbeb6c4377c84e Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Thu, 9 Feb 2023 02:54:00 +0000 Subject: [PATCH 002/256] update doc type --- src/wp-includes/functions.wp-scripts.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/wp-includes/functions.wp-scripts.php b/src/wp-includes/functions.wp-scripts.php index b1467ee2ffb6b..6043134957354 100644 --- a/src/wp-includes/functions.wp-scripts.php +++ b/src/wp-includes/functions.wp-scripts.php @@ -167,6 +167,7 @@ function wp_add_inline_script( $handle, $data, $position = 'after' ) { * number is automatically added equal to current installed WordPress version. * If set to null, no version is added. * @param array $args { + * Optional. An array of additional script strategies. Default empty array. * * @type boolean $in_footer Optional. Default true * @type string $strategy Optional. Default 'blocking' From 1ecea4c212407ee7075b3c21355087d02af62cbf Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Thu, 9 Feb 2023 02:54:30 +0000 Subject: [PATCH 003/256] removing a extra line --- src/wp-includes/functions.wp-scripts.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/wp-includes/functions.wp-scripts.php b/src/wp-includes/functions.wp-scripts.php index 6043134957354..3dd3cb87d6a03 100644 --- a/src/wp-includes/functions.wp-scripts.php +++ b/src/wp-includes/functions.wp-scripts.php @@ -171,7 +171,6 @@ function wp_add_inline_script( $handle, $data, $position = 'after' ) { * * @type boolean $in_footer Optional. Default true * @type string $strategy Optional. Default 'blocking' - * * } * @return bool Whether the script has been registered. True on success, false on failure. */ From 72cd0475349fe3b8a07849619229a1384c889413 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Thu, 9 Feb 2023 03:01:09 +0000 Subject: [PATCH 004/256] style fixes --- src/wp-includes/functions.wp-scripts.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/functions.wp-scripts.php b/src/wp-includes/functions.wp-scripts.php index 3dd3cb87d6a03..5a541593d7a96 100644 --- a/src/wp-includes/functions.wp-scripts.php +++ b/src/wp-includes/functions.wp-scripts.php @@ -167,10 +167,10 @@ function wp_add_inline_script( $handle, $data, $position = 'after' ) { * number is automatically added equal to current installed WordPress version. * If set to null, no version is added. * @param array $args { - * Optional. An array of additional script strategies. Default empty array. + * Optional. An array of additional script strategies. Default empty array. * - * @type boolean $in_footer Optional. Default true - * @type string $strategy Optional. Default 'blocking' + * @type boolean $in_footer Optional. Default true. + * @type string $strategy Optional. Default 'blocking' * } * @return bool Whether the script has been registered. True on success, false on failure. */ @@ -181,7 +181,6 @@ function wp_register_script( $handle, $src, $deps = array(), $ver = false, $args $registered = $wp_scripts->add( $handle, $src, $deps, $ver ); - if ( ! empty( $args ) ) { $wp_scripts->add_data( $handle, 'args', $args ); } From e156264a11da6b59bfd526677b7b49522592d1a5 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Thu, 9 Feb 2023 22:22:01 +0000 Subject: [PATCH 005/256] args update --- src/wp-includes/functions.wp-scripts.php | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/wp-includes/functions.wp-scripts.php b/src/wp-includes/functions.wp-scripts.php index 5a541593d7a96..780feb9a4a5ea 100644 --- a/src/wp-includes/functions.wp-scripts.php +++ b/src/wp-includes/functions.wp-scripts.php @@ -166,11 +166,11 @@ function wp_add_inline_script( $handle, $data, $position = 'after' ) { * as a query string for cache busting purposes. If version is set to false, a version * number is automatically added equal to current installed WordPress version. * If set to null, no version is added. - * @param array $args { + * @param array $args { * Optional. An array of additional script strategies. Default empty array. * * @type boolean $in_footer Optional. Default true. - * @type string $strategy Optional. Default 'blocking' + * @type string $strategy Optional. Default 'blocking'. * } * @return bool Whether the script has been registered. True on success, false on failure. */ @@ -345,10 +345,17 @@ function wp_deregister_script( $handle ) { * as a query string for cache busting purposes. If version is set to false, a version * number is automatically added equal to current installed WordPress version. * If set to null, no version is added. + * @param array $args { + * Optional. An array of additional script strategies. Default empty array. + * + * @type boolean $in_footer Optional. Default true. + * @type string $strategy Optional. Default 'blocking'. + * } + * * @param bool $in_footer Optional. Whether to enqueue the script before `` instead of in the ``. * Default 'false'. */ -function wp_enqueue_script( $handle, $src = '', $deps = array(), $ver = false, $in_footer = false ) { +function wp_enqueue_script( $handle, $src = '', $deps = array(), $ver = false, $args = array() ) { _wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle ); $wp_scripts = wp_scripts(); @@ -360,8 +367,8 @@ function wp_enqueue_script( $handle, $src = '', $deps = array(), $ver = false, $ $wp_scripts->add( $_handle[0], $src, $deps, $ver ); } - if ( $in_footer ) { - $wp_scripts->add_data( $_handle[0], 'group', 1 ); + if ( ! empty( $args ) ) { + $wp_scripts->add_data( $_handle[0], 'args', $args ); } } From 7ce9b70599afbbbaf42a9801b3bc1f7aa7a31fa7 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 10 Feb 2023 07:34:27 +0000 Subject: [PATCH 006/256] Normalize script args --- src/wp-includes/class-wp-dependencies.php | 4 +++- src/wp-includes/class-wp-scripts.php | 26 +++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-dependencies.php b/src/wp-includes/class-wp-dependencies.php index 0bc42bb282b77..308522b65c776 100644 --- a/src/wp-includes/class-wp-dependencies.php +++ b/src/wp-includes/class-wp-dependencies.php @@ -289,7 +289,9 @@ public function add_data( $handle, $key, $value ) { if ( ! isset( $this->registered[ $handle ] ) ) { return false; } - + if( 'in_footer_args' === $key ) { + $this->normalize_script_in_footer_args( $handle, $value ); + } return $this->registered[ $handle ]->add_data( $key, $value ); } diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 5374aa912a757..6aa65675f159a 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -714,6 +714,32 @@ public function in_default_dir( $src ) { return false; } + /** + * Normalize in_footer data. + * + * + * @param string $handle Name of the script to register a translation domain to. + * @param bool|array $args boolean for $in_footer backward compatibility. + */ + private function normalize_script_in_footer_args( $handle, $args ) { + $in_footer = false; + $strategy = 'blocking'; + + if( 'boolean' === gettype( $args ) && $args ) { + $in_footer = true; + } else { + if( isset( $args["in_footer"] ) && $args["in_footer"] ) { + $in_footer = true; + } + } + + $this->add_data( $handle, 'strategy', $strategy ); + + if ( $in_footer ) { + $this->add_data( $handle, 'group', 1 ); + } + } + /** * Resets class properties. * From 5b4bdfe7fcce1f531b680ce6333f7993ef834e72 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 10 Feb 2023 07:35:06 +0000 Subject: [PATCH 007/256] support for in_footer args --- src/wp-includes/functions.wp-scripts.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/functions.wp-scripts.php b/src/wp-includes/functions.wp-scripts.php index 780feb9a4a5ea..f76868eb256b7 100644 --- a/src/wp-includes/functions.wp-scripts.php +++ b/src/wp-includes/functions.wp-scripts.php @@ -182,7 +182,7 @@ function wp_register_script( $handle, $src, $deps = array(), $ver = false, $args $registered = $wp_scripts->add( $handle, $src, $deps, $ver ); if ( ! empty( $args ) ) { - $wp_scripts->add_data( $handle, 'args', $args ); + $wp_scripts->add_data( $handle, 'in_footer_args', $args ); } return $registered; @@ -368,7 +368,7 @@ function wp_enqueue_script( $handle, $src = '', $deps = array(), $ver = false, $ } if ( ! empty( $args ) ) { - $wp_scripts->add_data( $_handle[0], 'args', $args ); + $wp_scripts->add_data( $_handle[0], 'in_footer_args', $args ); } } From 5675b6461eb62fdb46d40ec13b8526bc31916a22 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 10 Feb 2023 18:16:18 +0000 Subject: [PATCH 008/256] Fix private call to child from add_data issue --- src/wp-includes/class-wp-dependencies.php | 3 --- src/wp-includes/class-wp-scripts.php | 17 +++++++++++++++-- src/wp-includes/functions.wp-scripts.php | 2 +- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/class-wp-dependencies.php b/src/wp-includes/class-wp-dependencies.php index 308522b65c776..6e2754cf20881 100644 --- a/src/wp-includes/class-wp-dependencies.php +++ b/src/wp-includes/class-wp-dependencies.php @@ -289,9 +289,6 @@ public function add_data( $handle, $key, $value ) { if ( ! isset( $this->registered[ $handle ] ) ) { return false; } - if( 'in_footer_args' === $key ) { - $this->normalize_script_in_footer_args( $handle, $value ); - } return $this->registered[ $handle ]->add_data( $key, $value ); } diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 6aa65675f159a..121922647e317 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -714,6 +714,21 @@ public function in_default_dir( $src ) { return false; } + /** + * The overrides the add_data function from WP_Dependencies, to support normalizing of $args. + * + * @param string $handle Name of the item. Should be unique. + * @param string $key The data key. + * @param mixed $value The data value. + * @return bool True on success, false on failure. + */ + public function add_data( $handle, $key, $value ) { + if( 'in_footer_args' === $key ) { + return $this->normalize_script_in_footer_args( $handle, $value ); + } + return parent::add_data( $handle, $key, $value ); + } + /** * Normalize in_footer data. * @@ -732,9 +747,7 @@ private function normalize_script_in_footer_args( $handle, $args ) { $in_footer = true; } } - $this->add_data( $handle, 'strategy', $strategy ); - if ( $in_footer ) { $this->add_data( $handle, 'group', 1 ); } diff --git a/src/wp-includes/functions.wp-scripts.php b/src/wp-includes/functions.wp-scripts.php index f76868eb256b7..8556e14da315e 100644 --- a/src/wp-includes/functions.wp-scripts.php +++ b/src/wp-includes/functions.wp-scripts.php @@ -360,7 +360,7 @@ function wp_enqueue_script( $handle, $src = '', $deps = array(), $ver = false, $ $wp_scripts = wp_scripts(); - if ( $src || $in_footer ) { + if ( $src || ! empty( $args ) ) { $_handle = explode( '?', $handle ); if ( $src ) { From 158f9253554d29b472ae3d78e427e98bc95f9d40 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 10 Feb 2023 20:44:32 +0000 Subject: [PATCH 009/256] change get_normalized_scripts_args --- src/wp-includes/class-wp-scripts.php | 32 ++++++++++++++-------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 121922647e317..5743e1ed3d54c 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -724,33 +724,33 @@ public function in_default_dir( $src ) { */ public function add_data( $handle, $key, $value ) { if( 'in_footer_args' === $key ) { - return $this->normalize_script_in_footer_args( $handle, $value ); + $args = $this->get_normalized_script_args( $handle, $value ); + if( $args['in_footer'] ) { + parent::add_data( $handle, 'group', 1 ); + } + return parent::add_data( $handle, 'script_args', $args ); } return parent::add_data( $handle, $key, $value ); } /** - * Normalize in_footer data. - * + * Normalize the data inside $args parameter and handle backward compatibility. * * @param string $handle Name of the script to register a translation domain to. * @param bool|array $args boolean for $in_footer backward compatibility. */ - private function normalize_script_in_footer_args( $handle, $args ) { - $in_footer = false; - $strategy = 'blocking'; - + private function get_normalized_script_args( $handle, $args ) { + $default_args = array( + 'in_footer' => false, + 'strategy' => 'blocking' + ); + // handle backward compatibility for $in_footer if( 'boolean' === gettype( $args ) && $args ) { - $in_footer = true; - } else { - if( isset( $args["in_footer"] ) && $args["in_footer"] ) { - $in_footer = true; - } - } - $this->add_data( $handle, 'strategy', $strategy ); - if ( $in_footer ) { - $this->add_data( $handle, 'group', 1 ); + $args = array( + 'in_footer' => true + ); } + return wp_parse_args( $args, $default_args ); } /** From 35330dd182a0bd98ddeeb205bbb8471fc2836a01 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 10 Feb 2023 20:45:33 +0000 Subject: [PATCH 010/256] change text --- 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 5743e1ed3d54c..2d631c380df30 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -734,7 +734,7 @@ public function add_data( $handle, $key, $value ) { } /** - * Normalize the data inside $args parameter and handle backward compatibility. + * Normalize the data inside $args parameter and support backward compatibility. * * @param string $handle Name of the script to register a translation domain to. * @param bool|array $args boolean for $in_footer backward compatibility. From f88153750113ecb8e3c3f6663aa9343cb0b24e89 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 10 Feb 2023 22:05:52 +0000 Subject: [PATCH 011/256] key name change --- src/wp-includes/class-wp-scripts.php | 12 +++++++----- src/wp-includes/functions.wp-scripts.php | 6 +----- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 2d631c380df30..ed8719b354755 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -723,12 +723,12 @@ public function in_default_dir( $src ) { * @return bool True on success, false on failure. */ public function add_data( $handle, $key, $value ) { - if( 'in_footer_args' === $key ) { + if( 'script_args' === $key ) { $args = $this->get_normalized_script_args( $handle, $value ); if( $args['in_footer'] ) { parent::add_data( $handle, 'group', 1 ); } - return parent::add_data( $handle, 'script_args', $args ); + return parent::add_data( $handle, $key, $args ); } return parent::add_data( $handle, $key, $value ); } @@ -736,10 +736,12 @@ public function add_data( $handle, $key, $value ) { /** * Normalize the data inside $args parameter and support backward compatibility. * - * @param string $handle Name of the script to register a translation domain to. - * @param bool|array $args boolean for $in_footer backward compatibility. + * @param string $handle Name of the script. + * @param bool|array $args array of script arguments or boolean for old usage ($in_footer). + * Default empty array. + * @return array Normalized argument array. */ - private function get_normalized_script_args( $handle, $args ) { + private function get_normalized_script_args( $handle, $args = array() ) { $default_args = array( 'in_footer' => false, 'strategy' => 'blocking' diff --git a/src/wp-includes/functions.wp-scripts.php b/src/wp-includes/functions.wp-scripts.php index 8556e14da315e..177e84f34a7cb 100644 --- a/src/wp-includes/functions.wp-scripts.php +++ b/src/wp-includes/functions.wp-scripts.php @@ -180,11 +180,7 @@ function wp_register_script( $handle, $src, $deps = array(), $ver = false, $args $wp_scripts = wp_scripts(); $registered = $wp_scripts->add( $handle, $src, $deps, $ver ); - - if ( ! empty( $args ) ) { - $wp_scripts->add_data( $handle, 'in_footer_args', $args ); - } - + $wp_scripts->add_data( $handle, 'script_args', $args ); return $registered; } From 1620df796156cb8c97969d96988246195356a4c9 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 10 Feb 2023 22:06:49 +0000 Subject: [PATCH 012/256] update args if src is preset or is args not empty --- src/wp-includes/functions.wp-scripts.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/wp-includes/functions.wp-scripts.php b/src/wp-includes/functions.wp-scripts.php index 177e84f34a7cb..413fdd6621c3a 100644 --- a/src/wp-includes/functions.wp-scripts.php +++ b/src/wp-includes/functions.wp-scripts.php @@ -358,14 +358,11 @@ function wp_enqueue_script( $handle, $src = '', $deps = array(), $ver = false, $ if ( $src || ! empty( $args ) ) { $_handle = explode( '?', $handle ); - if ( $src ) { $wp_scripts->add( $_handle[0], $src, $deps, $ver ); } - if ( ! empty( $args ) ) { - $wp_scripts->add_data( $_handle[0], 'in_footer_args', $args ); - } + $wp_scripts->add_data( $_handle[0], 'script_args', $args ); } $wp_scripts->enqueue( $handle ); From 52077c0ddb0414bb592dab8705db709299f95ae7 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Sat, 11 Feb 2023 02:37:46 +0000 Subject: [PATCH 013/256] test compatibility of in_footer and new normalize --- tests/phpunit/tests/dependencies/scripts.php | 55 ++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 10e447728a53b..616221462b641 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -63,6 +63,61 @@ public function test_wp_enqueue_script() { $this->assertSame( '', get_echo( 'wp_print_scripts' ) ); } + /** + * + */ + public function test_old_and_new_in_footer_scripts() { + wp_register_script( 'header-old', '/header-old.js', array(), null, false ); // In head. + wp_enqueue_script( 'header-old' ); + wp_register_script( 'header-new', '/header-new.js', array(), null, array( 'in_footer' => false ) ); // In head. + wp_enqueue_script( 'header-new' ); + wp_enqueue_script( 'enqueue-header-old', '/enqueue-header-old.js', array(), null, false ); // In head. + wp_enqueue_script( 'enqueue-header-new', '/enqueue-header-new.js', array(), null, array( 'in_footer' => false ) ); // In head. + + wp_register_script( 'footer-old', '/footer-old.js', array(), null, true ); // In footer. + wp_enqueue_script( 'footer-old' ); + wp_register_script( 'footer-new', '/footer-new.js', array(), null, array( 'in_footer' => true ) ); // In footer. + wp_enqueue_script( 'footer-new' ); + wp_enqueue_script( 'enqueue-footer-old', '/enqueue-footer-old.js', array(), null, true ); // In footer. + wp_enqueue_script( 'enqueue-footer-new', '/enqueue-footer-new.js', array(), null, array( 'in_footer' => true ) ); // In footer. + + $header = get_echo( 'wp_print_head_scripts' ); + $footer = get_echo( 'wp_print_footer_scripts' ); + + $expected_header = "\n"; + $expected_header .= "\n"; + $expected_header .= "\n"; + $expected_header .= "\n"; + + $expected_footer = "\n"; + $expected_footer .= "\n"; + $expected_footer .= "\n"; + $expected_footer .= "\n"; + + $this->assertSame( $expected_header, $header ); + $this->assertSame( $expected_footer, $footer ); + } + + /** + * + */ + public function test_get_normalized_script_args() { + global $wp_scripts; + $args = array( + 'in_footer' => false, + 'strategy' => 'defer' + ); + wp_register_script( 'header-defer', '/header-defer.js', array(), null, $args ); + $this->assertSame( $args, $wp_scripts->get_data( 'header-defer', 'script_args' ) ); + + $args = array( + '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' ) ); + } + /** * @ticket 42804 */ From a46c8ff6f6d28e817c10434318b5252385471cae Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Sat, 11 Feb 2023 03:01:04 +0000 Subject: [PATCH 014/256] formating --- src/wp-includes/functions.wp-scripts.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/functions.wp-scripts.php b/src/wp-includes/functions.wp-scripts.php index 413fdd6621c3a..800b848f2ac51 100644 --- a/src/wp-includes/functions.wp-scripts.php +++ b/src/wp-includes/functions.wp-scripts.php @@ -362,7 +362,7 @@ function wp_enqueue_script( $handle, $src = '', $deps = array(), $ver = false, $ $wp_scripts->add( $_handle[0], $src, $deps, $ver ); } - $wp_scripts->add_data( $_handle[0], 'script_args', $args ); + $wp_scripts->add_data( $_handle[0], 'script_args', $args ); } $wp_scripts->enqueue( $handle ); From c7f163248faad2692957b0a6c6ff04058bec3526 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Sat, 11 Feb 2023 03:05:38 +0000 Subject: [PATCH 015/256] change wording in doc type --- src/wp-includes/class-wp-scripts.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index ed8719b354755..132491a64698f 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -737,9 +737,13 @@ public function add_data( $handle, $key, $value ) { * Normalize the data inside $args parameter and support backward compatibility. * * @param string $handle Name of the script. - * @param bool|array $args array of script arguments or boolean for old usage ($in_footer). - * Default empty array. - * @return array Normalized argument array. + * @param array $args { + * Optional. Additional script arguments. Default empty array. + * + * @type boolean $in_footer Optional. Default true. + * @type string $strategy Optional. Default 'blocking'. + * } + * @return array Normalized $args array. */ private function get_normalized_script_args( $handle, $args = array() ) { $default_args = array( From d198e1a81637dec51ec441cd9d2bbbf88dc8be8e Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Sat, 11 Feb 2023 03:10:11 +0000 Subject: [PATCH 016/256] update doctype --- src/wp-includes/class-wp-scripts.php | 2 +- src/wp-includes/functions.wp-scripts.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 132491a64698f..5a81dc41d753c 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -741,7 +741,7 @@ public function add_data( $handle, $key, $value ) { * Optional. Additional script arguments. Default empty array. * * @type boolean $in_footer Optional. Default true. - * @type string $strategy Optional. Default 'blocking'. + * @type string $strategy Optional. Values blocking|defer|async .Default 'blocking'. * } * @return array Normalized $args array. */ diff --git a/src/wp-includes/functions.wp-scripts.php b/src/wp-includes/functions.wp-scripts.php index 800b848f2ac51..66714b0e350f1 100644 --- a/src/wp-includes/functions.wp-scripts.php +++ b/src/wp-includes/functions.wp-scripts.php @@ -170,7 +170,7 @@ function wp_add_inline_script( $handle, $data, $position = 'after' ) { * Optional. An array of additional script strategies. Default empty array. * * @type boolean $in_footer Optional. Default true. - * @type string $strategy Optional. Default 'blocking'. + * @type string $strategy Optional. Values blocking|defer|async. Default 'blocking'. * } * @return bool Whether the script has been registered. True on success, false on failure. */ @@ -345,7 +345,7 @@ function wp_deregister_script( $handle ) { * Optional. An array of additional script strategies. Default empty array. * * @type boolean $in_footer Optional. Default true. - * @type string $strategy Optional. Default 'blocking'. + * @type string $strategy Optional. Values blocking|defer|async. Default 'blocking'. * } * * @param bool $in_footer Optional. Whether to enqueue the script before `` instead of in the ``. From 3dac78ebefc428453d061ad19583f4158c1f22ab Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Sat, 11 Feb 2023 03:29:07 +0000 Subject: [PATCH 017/256] add few more assertions --- tests/phpunit/tests/dependencies/scripts.php | 21 ++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 616221462b641..60692bba98ddb 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -104,18 +104,27 @@ 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' + ); + wp_enqueue_script( 'footer-async', '/footer-async.js', array(), null, $args ); + $this->assertSame( $args, $wp_scripts->get_data( 'footer-async', 'script_args' ) ); + + $expected_args = array( 'in_footer' => false, - 'strategy' => 'defer' + 'strategy' => 'blocking' ); - wp_register_script( 'header-defer', '/header-defer.js', array(), null, $args ); - $this->assertSame( $args, $wp_scripts->get_data( 'header-defer', 'script_args' ) ); + $args = array(); + wp_register_script( 'defaults', '/defaults.js', array(), null, $args ); + $this->assertSame( $expected_args, $wp_scripts->get_data( 'defaults', 'script_args' ) ); + //test backward compatibility. $args = array( 'in_footer' => true, - 'strategy' => 'async' + 'strategy' => 'blocking' ); - wp_enqueue_script( 'footer-async', '/footer-async.js', array(), null, $args ); - $this->assertSame( $args, $wp_scripts->get_data( 'footer-async', 'script_args' ) ); + wp_enqueue_script( 'footer-old', '/footer-async.js', array(), null, true ); + $this->assertSame( $args, $wp_scripts->get_data( 'footer-old', 'script_args' ) ); } /** From 1f5baf7d146f9d40d164e6e9850c0a2b42f639d1 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Sat, 11 Feb 2023 19:36:08 +0000 Subject: [PATCH 018/256] phpcsfixes --- tests/phpunit/tests/dependencies/scripts.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 60692bba98ddb..1c2b687292083 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -64,22 +64,22 @@ public function test_wp_enqueue_script() { } /** - * + * Test old and new in_footer logic. */ public function test_old_and_new_in_footer_scripts() { - wp_register_script( 'header-old', '/header-old.js', array(), null, false ); // In head. + wp_register_script( 'header-old', '/header-old.js', array(), null, false ); // In head. wp_enqueue_script( 'header-old' ); - wp_register_script( 'header-new', '/header-new.js', array(), null, array( 'in_footer' => false ) ); // In head. + wp_register_script( 'header-new', '/header-new.js', array(), null, array( 'in_footer' => false ) ); // In head. wp_enqueue_script( 'header-new' ); - wp_enqueue_script( 'enqueue-header-old', '/enqueue-header-old.js', array(), null, false ); // In head. - wp_enqueue_script( 'enqueue-header-new', '/enqueue-header-new.js', array(), null, array( 'in_footer' => false ) ); // In head. + wp_enqueue_script( 'enqueue-header-old', '/enqueue-header-old.js', array(), null, false ); // In head. + wp_enqueue_script( 'enqueue-header-new', '/enqueue-header-new.js', array(), null, array( 'in_footer' => false ) ); // In head. - wp_register_script( 'footer-old', '/footer-old.js', array(), null, true ); // In footer. + wp_register_script( 'footer-old', '/footer-old.js', array(), null, true ); // In footer. wp_enqueue_script( 'footer-old' ); - wp_register_script( 'footer-new', '/footer-new.js', array(), null, array( 'in_footer' => true ) ); // In footer. + wp_register_script( 'footer-new', '/footer-new.js', array(), null, array( 'in_footer' => true ) ); // In footer. wp_enqueue_script( 'footer-new' ); - wp_enqueue_script( 'enqueue-footer-old', '/enqueue-footer-old.js', array(), null, true ); // In footer. - wp_enqueue_script( 'enqueue-footer-new', '/enqueue-footer-new.js', array(), null, array( 'in_footer' => true ) ); // In footer. + wp_enqueue_script( 'enqueue-footer-old', '/enqueue-footer-old.js', array(), null, true ); // In footer. + wp_enqueue_script( 'enqueue-footer-new', '/enqueue-footer-new.js', array(), null, array( 'in_footer' => true ) ); // In footer. $header = get_echo( 'wp_print_head_scripts' ); $footer = get_echo( 'wp_print_footer_scripts' ); @@ -99,7 +99,7 @@ public function test_old_and_new_in_footer_scripts() { } /** - * + * Test normalized script args. */ public function test_get_normalized_script_args() { global $wp_scripts; From ca29ce80369797bf46fd2c27b8cef43832de047d Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Mon, 13 Feb 2023 12:21:30 +0000 Subject: [PATCH 019/256] coding style fixes in test --- tests/phpunit/tests/dependencies/scripts.php | 27 ++++++++++---------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 1c2b687292083..02c6dc18fc041 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -71,16 +71,16 @@ public function test_old_and_new_in_footer_scripts() { wp_enqueue_script( 'header-old' ); wp_register_script( 'header-new', '/header-new.js', array(), null, array( 'in_footer' => false ) ); // In head. wp_enqueue_script( 'header-new' ); - wp_enqueue_script( 'enqueue-header-old', '/enqueue-header-old.js', array(), null, false ); // In head. + wp_enqueue_script( 'enqueue-header-old', '/enqueue-header-old.js', array(), null, false ); // In head. wp_enqueue_script( 'enqueue-header-new', '/enqueue-header-new.js', array(), null, array( 'in_footer' => false ) ); // In head. - + wp_register_script( 'footer-old', '/footer-old.js', array(), null, true ); // In footer. wp_enqueue_script( 'footer-old' ); wp_register_script( 'footer-new', '/footer-new.js', array(), null, array( 'in_footer' => true ) ); // In footer. wp_enqueue_script( 'footer-new' ); wp_enqueue_script( 'enqueue-footer-old', '/enqueue-footer-old.js', array(), null, true ); // In footer. wp_enqueue_script( 'enqueue-footer-new', '/enqueue-footer-new.js', array(), null, array( 'in_footer' => true ) ); // In footer. - + $header = get_echo( 'wp_print_head_scripts' ); $footer = get_echo( 'wp_print_footer_scripts' ); @@ -103,25 +103,26 @@ 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' + $args = array( + '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' ) ); - - $expected_args = array( - 'in_footer' => false, - 'strategy' => 'blocking' + + $expected_args = array( + 'in_footer' => false, + 'strategy' => 'blocking', ); + $args = array(); wp_register_script( 'defaults', '/defaults.js', array(), null, $args ); $this->assertSame( $expected_args, $wp_scripts->get_data( 'defaults', 'script_args' ) ); //test backward compatibility. - $args = array( - 'in_footer' => true, - 'strategy' => 'blocking' + $args = array( + 'in_footer' => true, + 'strategy' => 'blocking', ); wp_enqueue_script( 'footer-old', '/footer-async.js', array(), null, true ); $this->assertSame( $args, $wp_scripts->get_data( 'footer-old', 'script_args' ) ); From bb1abece156333598c63b08a645c1bff68ebaa73 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Mon, 13 Feb 2023 12:30:44 +0000 Subject: [PATCH 020/256] style issues --- 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 5a81dc41d753c..6fc08557776f1 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -715,7 +715,7 @@ public function in_default_dir( $src ) { } /** - * The overrides the add_data function from WP_Dependencies, to support normalizing of $args. + * The overrides the add_data function from WP_Dependencies, to support normalizing of $args. * * @param string $handle Name of the item. Should be unique. * @param string $key The data key. @@ -723,9 +723,9 @@ public function in_default_dir( $src ) { * @return bool True on success, false on failure. */ public function add_data( $handle, $key, $value ) { - if( 'script_args' === $key ) { + if ( 'script_args' === $key ) { $args = $this->get_normalized_script_args( $handle, $value ); - if( $args['in_footer'] ) { + if ( $args['in_footer'] ) { parent::add_data( $handle, 'group', 1 ); } return parent::add_data( $handle, $key, $args ); From 05ceffded087c37e03ee8eed31b877ba744e9ab5 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Mon, 13 Feb 2023 12:42:33 +0000 Subject: [PATCH 021/256] phpcs style fixes --- src/wp-includes/class-wp-scripts.php | 18 ++++++++---------- src/wp-includes/functions.wp-scripts.php | 6 +++--- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 6fc08557776f1..5cc87f4f9de59 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -735,26 +735,24 @@ public function add_data( $handle, $key, $value ) { /** * Normalize the data inside $args parameter and support backward compatibility. - * + * * @param string $handle Name of the script. * @param array $args { * Optional. Additional script arguments. Default empty array. - * + * * @type boolean $in_footer Optional. Default true. * @type string $strategy Optional. Values blocking|defer|async .Default 'blocking'. * } - * @return array Normalized $args array. + * @return array Normalized $args array. */ private function get_normalized_script_args( $handle, $args = array() ) { $default_args = array( - 'in_footer' => false, - 'strategy' => 'blocking' - ); + 'in_footer' => false, + 'strategy' => 'blocking', + ); // handle backward compatibility for $in_footer - if( 'boolean' === gettype( $args ) && $args ) { - $args = array( - 'in_footer' => true - ); + if ( 'boolean' === gettype( $args ) && $args ) { + $args = array( 'in_footer' => true ); } return wp_parse_args( $args, $default_args ); } diff --git a/src/wp-includes/functions.wp-scripts.php b/src/wp-includes/functions.wp-scripts.php index 66714b0e350f1..72d33d9635abb 100644 --- a/src/wp-includes/functions.wp-scripts.php +++ b/src/wp-includes/functions.wp-scripts.php @@ -168,7 +168,7 @@ function wp_add_inline_script( $handle, $data, $position = 'after' ) { * If set to null, no version is added. * @param array $args { * Optional. An array of additional script strategies. Default empty array. - * + * * @type boolean $in_footer Optional. Default true. * @type string $strategy Optional. Values blocking|defer|async. Default 'blocking'. * } @@ -343,11 +343,11 @@ function wp_deregister_script( $handle ) { * If set to null, no version is added. * @param array $args { * Optional. An array of additional script strategies. Default empty array. - * + * * @type boolean $in_footer Optional. Default true. * @type string $strategy Optional. Values blocking|defer|async. Default 'blocking'. * } - * + * * @param bool $in_footer Optional. Whether to enqueue the script before `` instead of in the ``. * Default 'false'. */ From 758f8c686172e42a33433f9d4e3e1dbaa9a0c149 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Mon, 13 Feb 2023 16:27:36 +0000 Subject: [PATCH 022/256] print_footer_scripts pushing out