-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Allow classic scripts to depend on modules #8024
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Changes from all commits
39a11a8
65fd1df
843e1e0
2e180cf
7fc42d2
5144a90
d83ea4b
9125683
5ddc2dd
9e976bb
591e3b5
d1d8c43
10c3a02
a444ffa
b0f579b
8366f45
45cacbb
14ae089
6122621
b77dbc7
1d12174
64e67b8
fda5f17
f2f72a6
d926e8f
1a1df99
6b56f5f
cf8f813
34867df
babe954
b450826
269c4c7
3c2c84b
354f42d
ae642f4
8a782ac
6a3141a
07ab8d6
907cb0d
23e2a33
7f53642
7ddcdd7
0cb95e0
ce5124f
083287a
9acd7f0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -68,6 +68,47 @@ function _wp_scripts_maybe_doing_it_wrong( $function_name, $handle = '' ) { | |
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Adds the data for the recognized args and warns for unrecognized args. | ||
| * | ||
| * @ignore | ||
| * @since 7.0.0 | ||
| * | ||
| * @param string $function_name Function name. | ||
| * @param WP_Scripts $wp_scripts WP_Scripts instance. | ||
| * @param string $handle Script handle. | ||
| * @param array $args Array of extra args for the script. | ||
| */ | ||
| function _wp_scripts_add_args_data( string $function_name, WP_Scripts $wp_scripts, string $handle, array $args ) { | ||
| $allowed_keys = array( 'strategy', 'in_footer', 'fetchpriority', 'module_dependencies' ); | ||
| $unknown_keys = array_diff( array_keys( $args ), $allowed_keys ); | ||
| if ( ! empty( $unknown_keys ) ) { | ||
| _doing_it_wrong( | ||
| $function_name, | ||
| sprintf( | ||
| /* translators: 1: $args, 2: List of unrecognized keys. */ | ||
| __( 'Unrecognized keys in the %1$s array: %2$s.' ), | ||
| '$args', | ||
| implode( wp_get_list_item_separator(), $unknown_keys ) | ||
| ), | ||
| '7.0.0' | ||
| ); | ||
|
Comment on lines
+86
to
+95
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Importantly, this will catch cases where someone “mispells” the arg |
||
| } | ||
|
|
||
| if ( ! empty( $args['in_footer'] ) ) { | ||
| $wp_scripts->add_data( $handle, 'group', 1 ); | ||
| } | ||
| if ( ! empty( $args['strategy'] ) ) { | ||
| $wp_scripts->add_data( $handle, 'strategy', $args['strategy'] ); | ||
| } | ||
| if ( ! empty( $args['fetchpriority'] ) ) { | ||
| $wp_scripts->add_data( $handle, 'fetchpriority', $args['fetchpriority'] ); | ||
| } | ||
| if ( ! empty( $args['module_dependencies'] ) ) { | ||
| $wp_scripts->add_data( $handle, 'module_dependencies', $args['module_dependencies'] ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Prints scripts in document head that are in the $handles queue. | ||
| * | ||
|
|
@@ -159,22 +200,24 @@ function wp_add_inline_script( $handle, $data, $position = 'after' ) { | |
| * @since 4.3.0 A return value was added. | ||
| * @since 6.3.0 The $in_footer parameter of type boolean was overloaded to be an $args parameter of type array. | ||
| * @since 6.9.0 The $fetchpriority parameter of type string was added to the $args parameter of type array. | ||
| * | ||
| * @param string $handle Name of the script. Should be unique. | ||
| * @param string|false $src Full URL of the script, or path of the script relative to the WordPress root directory. | ||
| * If source is set to false, script is an alias of other scripts it depends on. | ||
| * @param string[] $deps Optional. An array of registered script handles this script depends on. Default empty array. | ||
| * @param string|bool|null $ver Optional. String specifying script version number, if it has one, which is added to the URL | ||
| * 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<string, string|bool>|bool $args { | ||
| * Optional. An array of additional script loading strategies. Default empty array. | ||
| * @since 7.0.0 The $module_dependencies parameter of type string[] was added to the $args parameter of type array. | ||
| * | ||
| * @param string $handle Name of the script. Should be unique. | ||
westonruter marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * @param string|false $src Full URL of the script, or path of the script relative to the WordPress root directory. | ||
| * If source is set to false, script is an alias of other scripts it depends on. | ||
| * @param string[] $deps Optional. An array of registered script handles this script depends on. Default empty array. | ||
| * @param string|bool|null $ver Optional. String specifying script version number, if it has one, which is added to the URL | ||
| * 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<string, string|bool|string[]>|bool $args { | ||
| * Optional. An array of extra args for the script. Default empty array. | ||
| * Otherwise, it may be a boolean in which case it determines whether the script is printed in the footer. Default false. | ||
| * | ||
| * @type string $strategy Optional. If provided, may be either 'defer' or 'async'. | ||
| * @type bool $in_footer Optional. Whether to print the script in the footer. Default 'false'. | ||
| * @type string $fetchpriority Optional. The fetch priority for the script. Default 'auto'. | ||
| * @type string $strategy Optional. If provided, may be either 'defer' or 'async'. | ||
| * @type bool $in_footer Optional. Whether to print the script in the footer. Default 'false'. | ||
| * @type string $fetchpriority Optional. The fetch priority for the script. Default 'auto'. | ||
| * @type string[] $module_dependencies Optional. IDs for module dependencies loaded via dynamic import. Default empty array. | ||
| * } | ||
| * @return bool Whether the script has been registered. True on success, false on failure. | ||
| */ | ||
|
|
@@ -189,15 +232,8 @@ 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['in_footer'] ) ) { | ||
| $wp_scripts->add_data( $handle, 'group', 1 ); | ||
| } | ||
| if ( ! empty( $args['strategy'] ) ) { | ||
| $wp_scripts->add_data( $handle, 'strategy', $args['strategy'] ); | ||
| } | ||
| if ( ! empty( $args['fetchpriority'] ) ) { | ||
| $wp_scripts->add_data( $handle, 'fetchpriority', $args['fetchpriority'] ); | ||
| } | ||
| _wp_scripts_add_args_data( __FUNCTION__, $wp_scripts, $handle, $args ); | ||
|
|
||
| return $registered; | ||
| } | ||
|
|
||
|
|
@@ -345,22 +381,24 @@ function wp_deregister_script( $handle ) { | |
| * @since 2.1.0 | ||
| * @since 6.3.0 The $in_footer parameter of type boolean was overloaded to be an $args parameter of type array. | ||
| * @since 6.9.0 The $fetchpriority parameter of type string was added to the $args parameter of type array. | ||
| * | ||
| * @param string $handle Name of the script. Should be unique. | ||
| * @param string $src Full URL of the script, or path of the script relative to the WordPress root directory. | ||
| * Default empty. | ||
| * @param string[] $deps Optional. An array of registered script handles this script depends on. Default empty array. | ||
| * @param string|bool|null $ver Optional. String specifying script version number, if it has one, which is added to the URL | ||
| * 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<string, string|bool>|bool $args { | ||
| * Optional. An array of additional script loading strategies. Default empty array. | ||
| * @since 7.0.0 The $module_dependencies parameter of type string[] was added to the $args parameter of type array. | ||
| * | ||
| * @param string $handle Name of the script. Should be unique. | ||
| * @param string $src Full URL of the script, or path of the script relative to the WordPress root directory. | ||
| * Default empty. | ||
| * @param string[] $deps Optional. An array of registered script handles this script depends on. Default empty array. | ||
| * @param string|bool|null $ver Optional. String specifying script version number, if it has one, which is added to the URL | ||
| * 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<string, string|bool|string[]>|bool $args { | ||
| * Optional. An array of extra args for the script. Default empty array. | ||
| * Otherwise, it may be a boolean in which case it determines whether the script is printed in the footer. Default false. | ||
| * | ||
| * @type string $strategy Optional. If provided, may be either 'defer' or 'async'. | ||
| * @type bool $in_footer Optional. Whether to print the script in the footer. Default 'false'. | ||
| * @type string $fetchpriority Optional. The fetch priority for the script. Default 'auto'. | ||
| * @type string $strategy Optional. If provided, may be either 'defer' or 'async'. | ||
| * @type bool $in_footer Optional. Whether to print the script in the footer. Default 'false'. | ||
| * @type string $fetchpriority Optional. The fetch priority for the script. Default 'auto'. | ||
| * @type string[] $module_dependencies Optional. IDs for module dependencies loaded via dynamic import. Default empty array. | ||
| * } | ||
| */ | ||
| function wp_enqueue_script( $handle, $src = '', $deps = array(), $ver = false, $args = array() ) { | ||
|
|
@@ -379,14 +417,8 @@ function wp_enqueue_script( $handle, $src = '', $deps = array(), $ver = false, $ | |
| if ( $src ) { | ||
| $wp_scripts->add( $_handle[0], $src, $deps, $ver ); | ||
| } | ||
| if ( ! empty( $args['in_footer'] ) ) { | ||
| $wp_scripts->add_data( $_handle[0], 'group', 1 ); | ||
| } | ||
| if ( ! empty( $args['strategy'] ) ) { | ||
| $wp_scripts->add_data( $_handle[0], 'strategy', $args['strategy'] ); | ||
| } | ||
| if ( ! empty( $args['fetchpriority'] ) ) { | ||
| $wp_scripts->add_data( $_handle[0], 'fetchpriority', $args['fetchpriority'] ); | ||
| if ( ! empty( $args ) ) { | ||
| _wp_scripts_add_args_data( __FUNCTION__, $wp_scripts, $_handle[0], $args ); | ||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like a good place for
!empty(). I know that function is avoided sometimes, but this is an array and it seems appropriate.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I went with
count()because it is being usedclass-wp-dependencies.php:wordpress-develop/src/wp-includes/class-wp-dependencies.php
Lines 222 to 230 in dc62ecb
Personally I prefer to not use
empty()when possible because if it so happens that you have a typo in the variable name, the mistake goes silently under the radar.There's also
disallowedEmptyin PHPStan strict rules:We eliminated
empty()in Performance Lab: WordPress/performance#1219