Make WordPress Core


Ignore:
Timestamp:
07/16/2024 09:42:38 AM (2 years ago)
Author:
Bernhard Reiter
Message:

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 ​https://github.com/WordPress/wordpress-develop/pull/6683.

Props jonsurrell, gziolo, luisherranz, cbravobernal.
Fixes #61512.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/interactivity-api/class-wp-interactivity-api.php

    r58327 r58729  
    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;
    199                 }
    200 
    201                 $interactivity_data = array();
     215                        return $data;
     216                }
    202217
    203218                $config = array();
    … …  
    208223                }
    209224                if ( ! empty( $config ) ) {
    210                         $interactivity_data['config'] = $config;
     225                        $data['config'] = $config;
    211226                }
    212227
    … …  
    218233                }
    219234                if ( ! empty( $state ) ) {
    220                         $interactivity_data['state'] = $state;
    221                 }
    222 
    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                 }
     235                        $data['state'] = $state;
     236                }
     237
     238                return $data;
    266239        }
    267240
    … …  
    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
Note: See TracChangeset for help on using the changeset viewer.