Skip to content

Commit a87bac2

Browse files
committed
Interactivity API: Use Script Modules filter for store & config data.
A dedicated API exists for passing data to Script Modules implemented in changeset [58579]. Use this Core API instead of a custom implementation for Interactivity API to pass data to the client. Developed in WordPress#6683. Props jonsurrell, gziolo, luisherranz, cbravobernal. Fixes #61512. git-svn-id: https://develop.svn.wordpress.org/trunk@58729 602fd350-edb4-49c9-b593-d223f7449a82
1 parent da99a2b commit a87bac2

2 files changed

Lines changed: 111 additions & 240 deletions

File tree

src/wp-includes/interactivity-api/class-wp-interactivity-api.php

Lines changed: 24 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -192,22 +192,37 @@ public function config( string $store_namespace, array $config = array() ): arra
192192
* configuration will be available using a `getConfig` utility.
193193
*
194194
* @since 6.5.0
195+
*
196+
* @deprecated 6.7.0 Client data passing is handled by the {@see "script_module_data_{$module_id}"} filter.
195197
*/
196198
public function print_client_interactivity_data() {
199+
_deprecated_function( __METHOD__, '6.7.0' );
200+
}
201+
202+
/**
203+
* Set client-side interactivity data.
204+
*
205+
* Once in the browser, the state will be parsed and used to hydrate the client-side
206+
* interactivity stores and the configuration will be available using a `getConfig` utility.
207+
*
208+
* @since 6.7.0
209+
*
210+
* @param array $data Data to filter.
211+
* @return array Data for the Interactivity API script module.
212+
*/
213+
public function filter_script_module_interactivity_data( array $data ): array {
197214
if ( empty( $this->state_data ) && empty( $this->config_data ) ) {
198-
return;
215+
return $data;
199216
}
200217

201-
$interactivity_data = array();
202-
203218
$config = array();
204219
foreach ( $this->config_data as $key => $value ) {
205220
if ( ! empty( $value ) ) {
206221
$config[ $key ] = $value;
207222
}
208223
}
209224
if ( ! empty( $config ) ) {
210-
$interactivity_data['config'] = $config;
225+
$data['config'] = $config;
211226
}
212227

213228
$state = array();
@@ -217,52 +232,10 @@ public function print_client_interactivity_data() {
217232
}
218233
}
219234
if ( ! empty( $state ) ) {
220-
$interactivity_data['state'] = $state;
235+
$data['state'] = $state;
221236
}
222237

223-
if ( ! empty( $interactivity_data ) ) {
224-
/*
225-
* This data will be printed as JSON inside a script tag like this:
226-
* <script type="application/json"></script>
227-
*
228-
* A script tag must be closed by a sequence beginning with `</`. It's impossible to
229-
* close a script tag without using `<`. We ensure that `<` is escaped and `/` can
230-
* remain unescaped, so `</script>` will be printed as `\u003C/script\u00E3`.
231-
*
232-
* - JSON_HEX_TAG: All < and > are converted to \u003C and \u003E.
233-
* - JSON_UNESCAPED_SLASHES: Don't escape /.
234-
*
235-
* If the page will use UTF-8 encoding, it's safe to print unescaped unicode:
236-
*
237-
* - JSON_UNESCAPED_UNICODE: Encode multibyte Unicode characters literally (instead of as `\uXXXX`).
238-
* - JSON_UNESCAPED_LINE_TERMINATORS: The line terminators are kept unescaped when
239-
* JSON_UNESCAPED_UNICODE is supplied. It uses the same behaviour as it was
240-
* before PHP 7.1 without this constant. Available as of PHP 7.1.0.
241-
*
242-
* The JSON specification requires encoding in UTF-8, so if the generated HTML page
243-
* is not encoded in UTF-8 then it's not safe to include those literals. They must
244-
* be escaped to avoid encoding issues.
245-
*
246-
* @see https://www.rfc-editor.org/rfc/rfc8259.html for details on encoding requirements.
247-
* @see https://www.php.net/manual/en/json.constants.php for details on these constants.
248-
* @see https://html.spec.whatwg.org/#script-data-state for details on script tag parsing.
249-
*/
250-
$json_encode_flags = JSON_HEX_TAG | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_LINE_TERMINATORS;
251-
if ( ! is_utf8_charset() ) {
252-
$json_encode_flags = JSON_HEX_TAG | JSON_UNESCAPED_SLASHES;
253-
}
254-
255-
wp_print_inline_script_tag(
256-
wp_json_encode(
257-
$interactivity_data,
258-
$json_encode_flags
259-
),
260-
array(
261-
'type' => 'application/json',
262-
'id' => 'wp-interactivity-data',
263-
)
264-
);
265-
}
238+
return $data;
266239
}
267240

268241
/**
@@ -329,13 +302,13 @@ public function register_script_modules() {
329302
* Adds the necessary hooks for the Interactivity API.
330303
*
331304
* @since 6.5.0
305+
* @since 6.7.0 Use the {@see "script_module_data_{$module_id}"} filter to pass client-side data.
332306
*/
333307
public function add_hooks() {
334308
add_action( 'wp_enqueue_scripts', array( $this, 'register_script_modules' ) );
335-
add_action( 'wp_footer', array( $this, 'print_client_interactivity_data' ) );
336-
337309
add_action( 'admin_enqueue_scripts', array( $this, 'register_script_modules' ) );
338-
add_action( 'admin_print_footer_scripts', array( $this, 'print_client_interactivity_data' ) );
310+
311+
add_filter( 'script_module_data_@wordpress/interactivity', array( $this, 'filter_script_module_interactivity_data' ) );
339312
}
340313

341314
/**

0 commit comments

Comments
 (0)