Skip to content

Add the wp_script_module_attributes filter.#10324

Closed
samueljseay wants to merge 1 commit intoWordPress:trunkfrom
samueljseay:dev/add_wp_script_module_attributes_filter
Closed

Add the wp_script_module_attributes filter.#10324
samueljseay wants to merge 1 commit intoWordPress:trunkfrom
samueljseay:dev/add_wp_script_module_attributes_filter

Conversation

@samueljseay
Copy link
Copy Markdown

Trac ticket: https://core.trac.wordpress.org/ticket/64111


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

* @param string $id The script module identifier.
* @param array $script_module The script module data.
*/
$args = apply_filters( 'wp_script_module_attributes', $args, $id, $script_module );
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't the existing filter be used? I don't think we really need a new filter.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't that have unexpected consequences? Someone who is using the wp_script_attributes filter today only expects to be modifying the scripts, not the script modules.

For example, if someone is using that filter to add defer to some scripts with a conditional that is also true for some script modules, it would generate invalid HTML for them:

Image

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, because as you see below, it is using wp_print_script_tag(). This function is used for printing any script, including classic scripts, script modules, speculationrules. This function calls wp_get_script_tag() and that function is what applies the wp_script_attributes filters.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohhh, it's already there. We didn't notice that!

Thanks, Weston. Closing this then, as it's not needed. We'll update GB as well 🙂

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've drafted a PR as a starting point: WordPress/gutenberg#72449

I'm guessing lib/compat/wordpress-6.9/script-modules.php can be removed as well.

* @param string $id The script module identifier.
* @param array $script_module The script module data.
*/
$args = apply_filters( 'wp_script_module_attributes', $args, $id, $script_module );
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd make the arguments match the existing filter for scripts (wp_script_attributes):

  • Rename $args to $attributes
  • Remove $id, which can be obtained from the $attributes['id'] anyway
  • Eliminate $script_module, whose schema is not publicly exposed for now, and this exposure would mean that backward compatibility would have to be maintained from now on.
Suggested change
$args = apply_filters( 'wp_script_module_attributes', $args, $id, $script_module );
$args = apply_filters( 'wp_script_module_attributes', $attributes );

@luisherranz
Copy link
Copy Markdown
Member

Closing as the attributes of the script modules can already be filtered using wp_script_attributes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants