<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[58729] trunk: Interactivity API: Use Script Modules filter for store & config data.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/58729">58729</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/58729","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>Bernhard Reiter</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2024-07-16 09:42:38 +0000 (Tue, 16 Jul 2024)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Interactivity API: Use Script Modules filter for store & config data.

A dedicated API exists for passing data to Script Modules implemented in changeset <a href="https://core.trac.wordpress.org/changeset/58579">[58579]</a>.
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 <a href="https://core.trac.wordpress.org/ticket/61512">#61512</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesinteractivityapiclasswpinteractivityapiphp">trunk/src/wp-includes/interactivity-api/class-wp-interactivity-api.php</a></li>
<li><a href="#trunktestsphpunittestsinteractivityapiwpInteractivityAPIphp">trunk/tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesinteractivityapiclasswpinteractivityapiphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/interactivity-api/class-wp-interactivity-api.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/interactivity-api/class-wp-interactivity-api.php    2024-07-16 09:37:50 UTC (rev 58728)
+++ trunk/src/wp-includes/interactivity-api/class-wp-interactivity-api.php      2024-07-16 09:42:38 UTC (rev 58729)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -192,14 +192,29 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * configuration will be available using a `getConfig` utility.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 6.5.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         *
+        * @deprecated 6.7.0 Client data passing is handled by the {@see "script_module_data_{$module_id}"} filter.
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><span class="cx" style="display: block; padding: 0 10px">        public function print_client_interactivity_data() {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                _deprecated_function( __METHOD__, '6.7.0' );
+       }
+
+       /**
+        * Set client-side interactivity data.
+        *
+        * Once in the browser, the state will be parsed and used to hydrate the client-side
+        * interactivity stores and the configuration will be available using a `getConfig` utility.
+        *
+        * @since 6.7.0
+        *
+        * @param array $data Data to filter.
+        * @return array Data for the Interactivity API script module.
+        */
+       public function filter_script_module_interactivity_data( array $data ): array {
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( empty( $this->state_data ) && empty( $this->config_data ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        return;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 return $data;
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $interactivity_data = array();
-
</del><span class="cx" style="display: block; padding: 0 10px">                 $config = array();
</span><span class="cx" style="display: block; padding: 0 10px">                foreach ( $this->config_data as $key => $value ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( ! empty( $value ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -207,7 +222,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! empty( $config ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $interactivity_data['config'] = $config;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $data['config'] = $config;
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $state = array();
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -217,52 +232,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! empty( $state ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $interactivity_data['state'] = $state;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $data['state'] = $state;
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( ! empty( $interactivity_data ) ) {
-                       /*
-                        * 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\u00E3`.
-                        *
-                        *   - 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;
-                       }
-
-                       wp_print_inline_script_tag(
-                               wp_json_encode(
-                                       $interactivity_data,
-                                       $json_encode_flags
-                               ),
-                               array(
-                                       'type' => 'application/json',
-                                       'id'   => 'wp-interactivity-data',
-                               )
-                       );
-               }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         return $data;
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -329,13 +302,13 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * Adds the necessary hooks for the Interactivity API.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 6.5.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @since 6.7.0 Use the {@see "script_module_data_{$module_id}"} filter to pass client-side data.
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><span class="cx" style="display: block; padding: 0 10px">        public function add_hooks() {
</span><span class="cx" style="display: block; padding: 0 10px">                add_action( 'wp_enqueue_scripts', array( $this, 'register_script_modules' ) );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                add_action( 'wp_footer', array( $this, 'print_client_interactivity_data' ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         add_action( 'admin_enqueue_scripts', array( $this, 'register_script_modules' ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                add_action( 'admin_enqueue_scripts', array( $this, 'register_script_modules' ) );
-               add_action( 'admin_print_footer_scripts', array( $this, 'print_client_interactivity_data' ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         add_filter( 'script_module_data_@wordpress/interactivity', array( $this, 'filter_script_module_interactivity_data' ) );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span></span></pre></div>
<a id="trunktestsphpunittestsinteractivityapiwpInteractivityAPIphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php        2024-07-16 09:37:50 UTC (rev 58728)
+++ trunk/tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php  2024-07-16 09:42:38 UTC (rev 58729)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -184,88 +184,73 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * Invokes the private `print_client_interactivity` method of
-        * WP_Interactivity_API class.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * Tests that the wp-interactivity-data script is not printed if both state
+        * and config are empty.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @return array|null The content of the JSON object printed on the client-side or null if nothings was printed.
-        */
-       private function print_client_interactivity_data() {
-               $interactivity_data_markup = get_echo( array( $this->interactivity, 'print_client_interactivity_data' ) );
-               preg_match( '/<script type="application\/json" id="wp-interactivity-data">.*?(\{.*\}).*?<\/script>/s', $interactivity_data_markup, $interactivity_data_string );
-               return isset( $interactivity_data_string[1] ) ? json_decode( $interactivity_data_string[1], true ) : null;
-       }
-
-       /**
-        * Tests that the initial state and config are correctly printed on the
-        * client-side.
-        *
</del><span class="cx" style="display: block; padding: 0 10px">          * @ticket 60356
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         *
-        * @covers ::state
-        * @covers ::config
-        * @covers ::print_client_interactivity_data
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @ticket 61512
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        public function test_state_and_config_is_correctly_printed() {
-               $this->interactivity->state( 'myPlugin', array( 'a' => 1 ) );
-               $this->interactivity->state( 'otherPlugin', array( 'b' => 2 ) );
-               $this->interactivity->config( 'myPlugin', array( 'a' => 1 ) );
-               $this->interactivity->config( 'otherPlugin', array( 'b' => 2 ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_state_and_config_dont_print_when_empty() {
+               $filter = $this->get_script_data_filter_result();
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $result = $this->print_client_interactivity_data();
-
-               $data = array(
-                       'myPlugin'    => array( 'a' => 1 ),
-                       'otherPlugin' => array( 'b' => 2 ),
-               );
-
-               $this->assertSame(
-                       array(
-                               'config' => $data,
-                               'state'  => $data,
-                       ),
-                       $result
-               );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertSame( array(), $filter->get_args()[0][0] );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * Tests that the wp-interactivity-data script is not printed if both state
-        * and config are empty.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * Test that the print_client_interactivity_data is deprecated and produces no output.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @ticket 60356
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @ticket 61512
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @covers ::print_client_interactivity_data
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         *
+        * @expectedDeprecated WP_Interactivity_API::print_client_interactivity_data
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        public function test_state_and_config_dont_print_when_empty() {
-               $result = $this->print_client_interactivity_data();
-               $this->assertNull( $result );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_config_not_printed_when_empty() {
+               $this->interactivity->print_client_interactivity_data();
+               $this->expectOutputString( '' );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * Tests that the config is not printed if it's empty.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * Sets up an activity, runs an optional callback, and returns a MockAction for inspection.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @ticket 60356
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @since 6.7.0
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @covers ::state
-        * @covers ::print_client_interactivity_data
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @param  ?Closure $callback Optional. Callback to run to set up interactivity state and config.
+        * @return MockAction
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        public function test_config_not_printed_when_empty() {
-               $this->interactivity->state( 'myPlugin', array( 'a' => 1 ) );
-               $result = $this->print_client_interactivity_data();
-               $this->assertSame( array( 'state' => array( 'myPlugin' => array( 'a' => 1 ) ) ), $result );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ private function get_script_data_filter_result( ?Closure $callback = null ): MockAction {
+               $this->interactivity->add_hooks();
+               $this->interactivity->register_script_modules();
+               wp_enqueue_script_module( '@wordpress/interactivity' );
+               $filter = new MockAction();
+               add_filter( 'script_module_data_@wordpress/interactivity', array( $filter, 'filter' ) );
+
+               if ( $callback ) {
+                       $callback();
+               }
+
+               ob_start();
+               wp_script_modules()->print_script_module_data();
+               ob_end_clean();
+
+               return $filter;
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * Tests that the state is not printed if it's empty.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * Tests that the state is not included in client data if it's empty.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @ticket 60356
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         *
-        * @covers ::config
-        * @covers ::print_client_interactivity_data
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @ticket 61512
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><span class="cx" style="display: block; padding: 0 10px">        public function test_state_not_printed_when_empty() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->interactivity->config( 'myPlugin', array( 'a' => 1 ) );
-               $result = $this->print_client_interactivity_data();
-               $this->assertSame( array( 'config' => array( 'myPlugin' => array( 'a' => 1 ) ) ), $result );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $filter = $this->get_script_data_filter_result(
+                       function () {
+                               $this->interactivity->config( 'myPlugin', array( 'a' => 1 ) );
+                       }
+               );
+
+               $this->assertSame( array( 'config' => array( 'myPlugin' => array( 'a' => 1 ) ) ), $filter->get_args()[0][0] );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -272,21 +257,17 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * Tests that empty state objects are pruned from printed data.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @ticket 60761
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         *
-        * @covers ::print_client_interactivity_data
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @ticket 61512
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><span class="cx" style="display: block; padding: 0 10px">        public function test_state_not_printed_when_empty_array() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->interactivity->state( 'pluginWithEmptyState_prune', array() );
-               $this->interactivity->state( 'pluginWithState_include', array( 'value' => 'excellent' ) );
-               $printed_script = get_echo( array( $this->interactivity, 'print_client_interactivity_data' ) );
-               $expected       = <<<'SCRIPT_TAG'
-<script type="application/json" id="wp-interactivity-data">
-{"state":{"pluginWithState_include":{"value":"excellent"}}}
-</script>
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $filter = $this->get_script_data_filter_result(
+                       function () {
+                               $this->interactivity->state( 'pluginWithEmptyState_prune', array() );
+                               $this->interactivity->state( 'pluginWithState_include', array( 'value' => 'excellent' ) );
+                       }
+               );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-SCRIPT_TAG;
-
-               $this->assertSameIgnoreEOL( $expected, $printed_script );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertSame( array( 'state' => array( 'pluginWithState_include' => array( 'value' => 'excellent' ) ) ), $filter->get_args()[0][0] );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -293,13 +274,16 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * Tests that data consisting of only empty state objects is not printed.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @ticket 60761
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         *
-        * @covers ::print_client_interactivity_data
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @ticket 61512
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><span class="cx" style="display: block; padding: 0 10px">        public function test_state_not_printed_when_only_empty_arrays() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->interactivity->state( 'pluginWithEmptyState_prune', array() );
-               $printed_script = get_echo( array( $this->interactivity, 'print_client_interactivity_data' ) );
-               $this->assertSame( '', $printed_script );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $filter = $this->get_script_data_filter_result(
+                       function () {
+                               $this->interactivity->state( 'pluginWithEmptyState_prune', array() );
+                       }
+               );
+
+               $this->assertSame( array(), $filter->get_args()[0][0] );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -306,20 +290,16 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * Tests that nested empty state objects are printed correctly.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @ticket 60761
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         *
-        * @covers ::print_client_interactivity_data
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @ticket 61512
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><span class="cx" style="display: block; padding: 0 10px">        public function test_state_printed_correctly_with_nested_empty_array() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->interactivity->state( 'myPlugin', array( 'emptyArray' => array() ) );
-               $printed_script = get_echo( array( $this->interactivity, 'print_client_interactivity_data' ) );
-               $expected       = <<<'SCRIPT_TAG'
-<script type="application/json" id="wp-interactivity-data">
-{"state":{"myPlugin":{"emptyArray":[]}}}
-</script>
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $filter = $this->get_script_data_filter_result(
+                       function () {
+                               $this->interactivity->state( 'myPlugin', array( 'emptyArray' => array() ) );
+                       }
+               );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-SCRIPT_TAG;
-
-               $this->assertSameIgnoreEOL( $expected, $printed_script );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertSame( array( 'state' => array( 'myPlugin' => array( 'emptyArray' => array() ) ) ), $filter->get_args()[0][0] );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -326,21 +306,17 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * Tests that empty config objects are pruned from printed data.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @ticket 60761
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         *
-        * @covers ::print_client_interactivity_data
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @ticket 61512
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><span class="cx" style="display: block; padding: 0 10px">        public function test_config_not_printed_when_empty_array() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->interactivity->config( 'pluginWithEmptyConfig_prune', array() );
-               $this->interactivity->config( 'pluginWithConfig_include', array( 'value' => 'excellent' ) );
-               $printed_script = get_echo( array( $this->interactivity, 'print_client_interactivity_data' ) );
-               $expected       = <<<'SCRIPT_TAG'
-<script type="application/json" id="wp-interactivity-data">
-{"config":{"pluginWithConfig_include":{"value":"excellent"}}}
-</script>
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $filter = $this->get_script_data_filter_result(
+                       function () {
+                               $this->interactivity->config( 'pluginWithEmptyConfig_prune', array() );
+                               $this->interactivity->config( 'pluginWithConfig_include', array( 'value' => 'excellent' ) );
+                       }
+               );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-SCRIPT_TAG;
-
-               $this->assertSameIgnoreEOL( $expected, $printed_script );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertSame( array( 'config' => array( 'pluginWithConfig_include' => array( 'value' => 'excellent' ) ) ), $filter->get_args()[0][0] );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -347,13 +323,16 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * Tests that data consisting of only empty config objects is not printed.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @ticket 60761
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         *
-        * @covers ::print_client_interactivity_data
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @ticket 61512
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><span class="cx" style="display: block; padding: 0 10px">        public function test_config_not_printed_when_only_empty_arrays() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->interactivity->config( 'pluginWithEmptyConfig_prune', array() );
-               $printed_script = get_echo( array( $this->interactivity, 'print_client_interactivity_data' ) );
-               $this->assertSame( '', $printed_script );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $filter = $this->get_script_data_filter_result(
+                       function () {
+                               $this->interactivity->config( 'pluginWithEmptyConfig_prune', array() );
+                       }
+               );
+
+               $this->assertSame( array(), $filter->get_args()[0][0] );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -360,100 +339,19 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * Tests that nested empty config objects are printed correctly.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @ticket 60761
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         *
-        * @covers ::print_client_interactivity_data
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @ticket 61512
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><span class="cx" style="display: block; padding: 0 10px">        public function test_config_printed_correctly_with_nested_empty_array() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->interactivity->config( 'myPlugin', array( 'emptyArray' => array() ) );
-               $printed_script = get_echo( array( $this->interactivity, 'print_client_interactivity_data' ) );
-               $expected       = <<<'SCRIPT_TAG'
-<script type="application/json" id="wp-interactivity-data">
-{"config":{"myPlugin":{"emptyArray":[]}}}
-</script>
-
-SCRIPT_TAG;
-
-               $this->assertSameIgnoreEOL( $expected, $printed_script );
-       }
-
-       /**
-        * Tests that special characters in the initial state and configuration are
-        * properly escaped.
-        *
-        * @ticket 60356
-        * @ticket 61170
-        *
-        * @covers ::state
-        * @covers ::config
-        * @covers ::print_client_interactivity_data
-        */
-       public function test_state_and_config_escape_special_characters() {
-               $this->interactivity->state(
-                       'myPlugin',
-                       array(
-                               'ampersand'                              => '&',
-                               'less-than sign'                         => '<',
-                               'greater-than sign'                      => '>',
-                               'solidus'                                => '/',
-                               'line separator'                         => "\u{2028}",
-                               'paragraph separator'                    => "\u{2029}",
-                               'flag of england'                        => "\u{1F3F4}\u{E0067}\u{E0062}\u{E0065}\u{E006E}\u{E0067}\u{E007F}",
-                               'malicious script closer'                => '</script>',
-                               'entity-encoded malicious script closer' => '&lt;/script&gt;',
-                       )
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $filter = $this->get_script_data_filter_result(
+                       function () {
+                               $this->interactivity->config( 'myPlugin', array( 'emptyArray' => array() ) );
+                       }
</ins><span class="cx" style="display: block; padding: 0 10px">                 );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->interactivity->config( 'myPlugin', array( 'chars' => '&<>/' ) );
</del><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $interactivity_data_markup = get_echo( array( $this->interactivity, 'print_client_interactivity_data' ) );
-               preg_match( '~<script type="application/json" id="wp-interactivity-data">\s*(\{.*\})\s*</script>~s', $interactivity_data_markup, $interactivity_data_string );
-
-               $expected = <<<"JSON"
-{"config":{"myPlugin":{"chars":"&\\u003C\\u003E/"}},"state":{"myPlugin":{"ampersand":"&","less-than sign":"\\u003C","greater-than sign":"\\u003E","solidus":"/","line separator":"\u{2028}","paragraph separator":"\u{2029}","flag of england":"\u{1F3F4}\u{E0067}\u{E0062}\u{E0065}\u{E006E}\u{E0067}\u{E007F}","malicious script closer":"\\u003C/script\\u003E","entity-encoded malicious script closer":"&lt;/script&gt;"}}}
-JSON;
-               $this->assertSame( $expected, $interactivity_data_string[1] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertSame( array( 'config' => array( 'myPlugin' => array( 'emptyArray' => array() ) ) ), $filter->get_args()[0][0] );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * Tests that special characters in the initial state and configuration are
-        * properly escaped when the blog_charset is not UTF-8 (unicode compatible).
-        *
-        * This this test, unicode and line terminators should be escaped to their
-        * JSON unicode sequences.
-        *
-        * @ticket 61170
-        *
-        * @covers ::state
-        * @covers ::config
-        * @covers ::print_client_interactivity_data
-        */
-       public function test_state_and_config_escape_special_characters_non_utf8() {
-               add_filter( 'pre_option_blog_charset', array( $this, 'charset_iso_8859_1' ) );
-               $this->interactivity->state(
-                       'myPlugin',
-                       array(
-                               'ampersand'                              => '&',
-                               'less-than sign'                         => '<',
-                               'greater-than sign'                      => '>',
-                               'solidus'                                => '/',
-                               'line separator'                         => "\u{2028}",
-                               'paragraph separator'                    => "\u{2029}",
-                               'flag of england'                        => "\u{1F3F4}\u{E0067}\u{E0062}\u{E0065}\u{E006E}\u{E0067}\u{E007F}",
-                               'malicious script closer'                => '</script>',
-                               'entity-encoded malicious script closer' => '&lt;/script&gt;',
-                       )
-               );
-               $this->interactivity->config( 'myPlugin', array( 'chars' => '&<>/' ) );
-
-               $interactivity_data_markup = get_echo( array( $this->interactivity, 'print_client_interactivity_data' ) );
-               preg_match( '~<script type="application/json" id="wp-interactivity-data">\s*(\{.*\})\s*</script>~s', $interactivity_data_markup, $interactivity_data_string );
-
-               $expected = <<<"JSON"
-{"config":{"myPlugin":{"chars":"&\\u003C\\u003E/"}},"state":{"myPlugin":{"ampersand":"&","less-than sign":"\\u003C","greater-than sign":"\\u003E","solidus":"/","line separator":"\\u2028","paragraph separator":"\\u2029","flag of england":"\\ud83c\\udff4\\udb40\\udc67\\udb40\\udc62\\udb40\\udc65\\udb40\\udc6e\\udb40\\udc67\\udb40\\udc7f","malicious script closer":"\\u003C/script\\u003E","entity-encoded malicious script closer":"&lt;/script&gt;"}}}
-JSON;
-               $this->assertSame( $expected, $interactivity_data_string[1] );
-       }
-
-       /**
</del><span class="cx" style="display: block; padding: 0 10px">          * Test that calling state without a namespace arg returns the state data
</span><span class="cx" style="display: block; padding: 0 10px">         * for the current namespace in the internal namespace stack.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span></span></pre>
</div>
</div>

</body>
</html>