-
Notifications
You must be signed in to change notification settings - Fork 0
Add script_data_{$handle} filter for classic scripts #17
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 6 commits
661399e
2c0960c
f4afe13
d3cd233
4e490ee
14e4ff9
16c9b3f
3edd50e
0fbee29
d89d333
d9f018f
cf669ad
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 |
|---|---|---|
|
|
@@ -1182,4 +1182,116 @@ protected function get_dependency_warning_message( $handle, $missing_dependency_ | |
| implode( ', ', $missing_dependency_handles ) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Prints data associated with scripts. | ||
| * | ||
| * The data will be embedded in the page HTML and can be read by scripts on page load. | ||
| * | ||
| * Data can be associated with a script via the {@see "script_data_{$handle}"} filter. | ||
| * | ||
| * The data for a script will be serialized as JSON in a script tag with an ID of the | ||
| * form `wp-script-data-{$handle}`. | ||
| * | ||
| * @since 6.8.0 | ||
| */ | ||
| public function print_script_data() { | ||
|
||
| /* | ||
| * Determine JSON encoding flags once, outside the loop. | ||
| * The charset won't change during iteration. | ||
| * | ||
| * This data will be printed as JSON inside a script tag like this: | ||
| * <script type="application/json"></script> | ||
| * | ||
| * A script tag must be closed by a sequence beginning with `</`. It's impossible to | ||
| * close a script tag without using `<`. We ensure that `<` is escaped and `/` can | ||
| * remain unescaped, so `</script>` will be printed as `\u003C/script>`. | ||
| * | ||
| * - JSON_HEX_TAG: All < and > are converted to \u003C and \u003E. | ||
| * - JSON_UNESCAPED_SLASHES: Don't escape /. | ||
| * | ||
| * If the page will use UTF-8 encoding, it's safe to print unescaped unicode: | ||
| * | ||
| * - JSON_UNESCAPED_UNICODE: Encode multibyte Unicode characters literally (instead of as `\uXXXX`). | ||
| * - JSON_UNESCAPED_LINE_TERMINATORS: The line terminators are kept unescaped when | ||
| * JSON_UNESCAPED_UNICODE is supplied. It uses the same behaviour as it was | ||
| * before PHP 7.1 without this constant. Available as of PHP 7.1.0. | ||
| * | ||
| * The JSON specification requires encoding in UTF-8, so if the generated HTML page | ||
| * is not encoded in UTF-8 then it's not safe to include those literals. They must | ||
| * be escaped to avoid encoding issues. | ||
| * | ||
| * @see https://www.rfc-editor.org/rfc/rfc8259.html for details on encoding requirements. | ||
| * @see https://www.php.net/manual/en/json.constants.php for details on these constants. | ||
| * @see https://html.spec.whatwg.org/#script-data-state for details on script tag parsing. | ||
| */ | ||
| $json_encode_flags = JSON_HEX_TAG | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_LINE_TERMINATORS; | ||
| if ( ! is_utf8_charset() ) { | ||
| $json_encode_flags = JSON_HEX_TAG | JSON_UNESCAPED_SLASHES; | ||
| } | ||
|
|
||
| foreach ( array_unique( $this->queue ) as $handle ) { | ||
| /** | ||
| * Filters data associated with a given script. | ||
| * | ||
| * The dynamic portion of the hook name, `$handle`, refers to the script handle. | ||
| * | ||
| * Scripts may require data that is required for initialization or is essential | ||
| * to have immediately available on page load. These are suitable use cases for | ||
| * this data. | ||
| * | ||
| * This is best suited to pass essential data that must be available to the script for | ||
| * initialization or immediately on page load. It does not replace the REST API or | ||
| * fetching data from the client. | ||
| * | ||
| * Example: | ||
| * | ||
| * add_filter( | ||
| * 'script_data_my-script-handle', | ||
| * function ( array $data ): array { | ||
| * $data['myData'] = array( | ||
| * 'option' => get_option( 'my_option' ), | ||
| * ); | ||
| * return $data; | ||
| * } | ||
| * ); | ||
| * | ||
| * If the filter returns no data (an empty array), nothing will be embedded in the page. | ||
| * | ||
| * The data for a given script, if provided, will be JSON serialized in a script | ||
| * tag with an ID of the form `wp-script-data-{$handle}`. | ||
| * | ||
| * The data can be read on the client with a pattern like this: | ||
| * | ||
| * Example: | ||
| * | ||
| * const dataContainer = document.getElementById( 'wp-script-data-my-script-handle' ); | ||
| * let data = {}; | ||
| * if ( dataContainer ) { | ||
| * try { | ||
| * data = JSON.parse( dataContainer.textContent ); | ||
| * } catch {} | ||
| * } | ||
| * initMyScriptWithData( data ); | ||
| * | ||
| * @since 6.8.0 | ||
| * | ||
| * @param array $data The data associated with the script. | ||
| */ | ||
| $data = apply_filters( "script_data_{$handle}", array() ); | ||
|
|
||
| if ( ! empty( $data ) ) { | ||
| wp_print_inline_script_tag( | ||
| (string) wp_json_encode( | ||
| $data, | ||
| $json_encode_flags | ||
| ), | ||
| array( | ||
| 'type' => 'application/json', | ||
| 'id' => "wp-script-data-{$handle}", | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.
@copilot remove this empty line
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.
Fixed in 0fbee29