mirrored from git://develop.git.wordpress.org/
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Script Modules: Add server to client data passing #6682
Closed
sirreal
wants to merge
11
commits into
WordPress:trunk
from
sirreal:add/server-client-script-modules-data
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3c2b902
Add print_script_module_data to WP_Script_Modules
sirreal a55bd76
Add tests
sirreal 507eb83
Fix typo and reword in filter documentation
sirreal 8a0f274
Avoid use of empty()
sirreal 64b0b5f
Update "bad data" test name to "invalid data"
sirreal de7e7ac
Update filter name script_module_data_{$module_id}
sirreal 208ad79
Update tests with updated filter name
sirreal fd27379
Change script tag ID to wp-script-module-data-{$module_id}
sirreal 9a06d99
Update tests for new ID
sirreal c4f122e
Update since tags to 6.7.0
sirreal 1af07a5
Update ticket references
ockham File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
My only remaining question is whether adding a filter is the most ergonomic way to add data to a module. Perhaps there should be a function to make this easier? For example:
Compare with a typical way that scripts expose data to non-module scripts via
wp_localize_script()
:Granted, this use of
wp_localize_script()
is not ideal since it is a hack of what is intended to be used for l10n. What I think is the better more modern alternative is to usewp_add_inline_script_tag()
(which is also worse in some ways since it requires manual serialization):There is also the
WP_Scripts::add_data()
method (andwp_script_add_data()
helper function) which are used to add "data" to script handles, wherewp_localize_script()
adds this to theextra
array key, andwp_add_inline_script()
adds to thebefore
andafter
array keys.What if there was a similar
add_data()
method onWP_Script_Modules
?This could still ultimately get filtered, but it seems maybe a bit strange for the filter to be the primary interface to add data to a module.
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.
For example, the pending data could be stored with the registered script module:
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.
@gziolo brought this up in the Gutenberg PR: WordPress/gutenberg#61658 (comment)
The motivation for using a filter is that it's a simple interface to implement and it seems sufficient. It doesn't require the class to store any additional data or to handle data merging, the filters handle that. As you note, the filter would be compatible with other methods for adding data if there's demand for them in the future.
The patch you shared is likely sufficient, although it doesn't consider data merging. Should data be merged on subsequent
::add_data()
calls? Maybe there's an argument to merge or replace, or maybe there are separate::set_data()
and::add_data()
methods. As the PR stands right now, it avoids having to answer those questions.I don't feel strongly. If you do, let me know and I can explore adding those methods.
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 don't feel strongly that they have to be added now, but as the API for the JS-side API is refined to access that data, the PHP-side API could also be refined to set the data.