Skip to content

Commit

Permalink
Rename WP_Interactivity_Store and move filter to scripts file
Browse files Browse the repository at this point in the history
  • Loading branch information
luisherranz committed Jun 2, 2023
1 parent 67c7764 commit 4894886
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* WP_Directive_Store class
* WP_Interactivity_Store class
*
* Manages the initial state of the Interactivity API store in the server and
* its serialization so it can be restored in the browser upon hydration.
Expand All @@ -17,7 +17,7 @@
*
* @access private
*/
class WP_Directive_Store {
class WP_Interactivity_Store {
/**
* Store.
*
Expand Down Expand Up @@ -70,6 +70,6 @@ static function render() {

// TODO: decide the final id name.
$store = self::serialize();
echo "<script id=\"wp-interactivity-store\" type=\"application/json\">$store</script>";
echo "<script id=\"wp-interactivity-store-data\" type=\"application/json\">$store</script>";
}
}
28 changes: 28 additions & 0 deletions lib/experimental/interactivity-api/scripts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* Utils to optimize the interactive scripts.
*
* @package Gutenberg
* @subpackage Interactivity API
*/

/**
* Move interactive scripts to the footer. This is a temporary measure to make
* it work with `wp_store` and it should be replaced with deferred scripts or
* modules.
*/
function gutenberg_interactivity_move_interactive_scripts_to_the_footer() {
// Move the @wordpress/interactivity package to the footer.
wp_script_add_data( 'wp-interactivity', 'group', 1 );

// Move all the view scripts of the interactive blocks to the footer.
$registered_blocks = \WP_Block_Type_Registry::get_instance()->get_all_registered();
foreach ( array_values( $registered_blocks ) as $block ) {
if ( isset( $block->supports['interactivity'] ) && $block->supports['interactivity'] ) {
foreach ( $block->view_script_handles as $handle ) {
wp_script_add_data( $handle, 'group', 1 );
}
}
}
}
add_action( 'wp_enqueue_scripts', 'gutenberg_interactivity_move_interactive_scripts_to_the_footer', 11 );
31 changes: 5 additions & 26 deletions lib/experimental/interactivity-api/store.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* The functions that expose the store of the WP_Directive_Store class.
* The functions that expose the store of the WP_Interactivity_Store class.
*
* @package Gutenberg
* @subpackage Interactivity API
Expand All @@ -15,33 +15,12 @@
*/
function wp_store( $data = null ) {
if ( $data ) {
WP_Directive_Store::merge_data( $data );
WP_Interactivity_Store::merge_data( $data );
}
return WP_Directive_Store::get_data();
return WP_Interactivity_Store::get_data();
}

/**
* Render the store in the frontend.
* Render the Interactivity API store in the frontend.
*/
add_action( 'wp_footer', array( 'WP_Directive_Store', 'render' ), 8 );

/**
* Move interactive scripts to the footer. This is a temporary measure to make
* it work with `wp_store` and it should be replaced with deferred scripts or
* modules.
*/
function gutenberg_interactivity_move_interactive_scripts_to_the_footer() {
// Move the @wordpress/interactivity package to the footer.
wp_script_add_data( 'wp-interactivity', 'group', 1 );

// Move all the view scripts of the interactive blocks to the footer.
$registered_blocks = \WP_Block_Type_Registry::get_instance()->get_all_registered();
foreach ( array_values( $registered_blocks ) as $block ) {
if ( isset( $block->supports['interactivity'] ) && $block->supports['interactivity'] ) {
foreach ( $block->view_script_handles as $handle ) {
wp_script_add_data( $handle, 'group', 1 );
}
}
}
}
add_action( 'wp_enqueue_scripts', 'gutenberg_interactivity_move_interactive_scripts_to_the_footer', 11 );
add_action( 'wp_footer', array( 'WP_Interactivity_Store', 'render' ), 8 );
3 changes: 2 additions & 1 deletion lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,9 @@ function gutenberg_is_experiment_enabled( $name ) {
if ( gutenberg_is_experiment_enabled( 'gutenberg-interactivity-api-core-blocks' ) ) {
require __DIR__ . '/experimental/interactivity-api/blocks.php';
}
require __DIR__ . '/experimental/interactivity-api/class-wp-directive-store.php';
require __DIR__ . '/experimental/interactivity-api/class-wp-interactivity-store.php';
require __DIR__ . '/experimental/interactivity-api/store.php';
require __DIR__ . '/experimental/interactivity-api/scripts.php';

// Fonts API.
if ( ! class_exists( 'WP_Fonts' ) ) {
Expand Down
2 changes: 1 addition & 1 deletion packages/interactivity/src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const deepMerge = ( target, source ) => {
const getSerializedState = () => {
// TODO: change the store tag ID for a better one.
const storeTag = document.querySelector(
`script[type="application/json"]#wp-interactivity-store`
`script[type="application/json"]#wp-interactivity-store-data`
);
if ( ! storeTag ) return {};
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
<?php
/**
* `WP_Directive_Store` class test.
* `WP_Interactivity_Store` class test.
*
* @package Gutenberg
* @subpackage Interactivity API
*/

/**
* Tests for the `WP_Directive_Store` class.
* Tests for the `WP_Interactivity_Store` class.
*
* @group directives
* @covers WP_Directive_Store
* @covers WP_Interactivity_Store
*/
class Tests_Directives_WPDirectiveStore extends WP_UnitTestCase {
class WP_Interactivity_Store_Test extends WP_UnitTestCase {
public function set_up() {
// Clear the state before each test.
WP_Directive_Store::reset();
WP_Interactivity_Store::reset();
}

public function test_store_should_be_empty() {
$this->assertEmpty( WP_Directive_Store::get_data() );
$this->assertEmpty( WP_Interactivity_Store::get_data() );
}

public function test_store_can_be_merged() {
Expand All @@ -34,12 +34,12 @@ public function test_store_can_be_merged() {
),
),
);
WP_Directive_Store::merge_data( $data );
$this->assertSame( $data, WP_Directive_Store::get_data() );
WP_Interactivity_Store::merge_data( $data );
$this->assertSame( $data, WP_Interactivity_Store::get_data() );
}

public function test_store_can_be_extended() {
WP_Directive_Store::merge_data(
WP_Interactivity_Store::merge_data(
array(
'state' => array(
'core' => array(
Expand All @@ -48,7 +48,7 @@ public function test_store_can_be_extended() {
),
)
);
WP_Directive_Store::merge_data(
WP_Interactivity_Store::merge_data(
array(
'state' => array(
'core' => array(
Expand All @@ -72,12 +72,12 @@ public function test_store_can_be_extended() {
),
),
),
WP_Directive_Store::get_data()
WP_Interactivity_Store::get_data()
);
}

public function test_store_existing_props_should_be_overwritten() {
WP_Directive_Store::merge_data(
WP_Interactivity_Store::merge_data(
array(
'state' => array(
'core' => array(
Expand All @@ -86,7 +86,7 @@ public function test_store_existing_props_should_be_overwritten() {
),
)
);
WP_Directive_Store::merge_data(
WP_Interactivity_Store::merge_data(
array(
'state' => array(
'core' => array(
Expand All @@ -103,12 +103,12 @@ public function test_store_existing_props_should_be_overwritten() {
),
),
),
WP_Directive_Store::get_data()
WP_Interactivity_Store::get_data()
);
}

public function test_store_existing_indexed_arrays_should_be_replaced() {
WP_Directive_Store::merge_data(
WP_Interactivity_Store::merge_data(
array(
'state' => array(
'core' => array(
Expand All @@ -117,7 +117,7 @@ public function test_store_existing_indexed_arrays_should_be_replaced() {
),
)
);
WP_Directive_Store::merge_data(
WP_Interactivity_Store::merge_data(
array(
'state' => array(
'core' => array(
Expand All @@ -134,12 +134,12 @@ public function test_store_existing_indexed_arrays_should_be_replaced() {
),
),
),
WP_Directive_Store::get_data()
WP_Interactivity_Store::get_data()
);
}

public function test_store_should_be_correctly_rendered() {
WP_Directive_Store::merge_data(
WP_Interactivity_Store::merge_data(
array(
'state' => array(
'core' => array(
Expand All @@ -148,7 +148,7 @@ public function test_store_should_be_correctly_rendered() {
),
)
);
WP_Directive_Store::merge_data(
WP_Interactivity_Store::merge_data(
array(
'state' => array(
'core' => array(
Expand All @@ -158,10 +158,10 @@ public function test_store_should_be_correctly_rendered() {
)
);
ob_start();
WP_Directive_Store::render();
WP_Interactivity_Store::render();
$rendered = ob_get_clean();
$this->assertSame(
'<script id="wp-interactivity-store" type="application/json">{"state":{"core":{"a":1,"b":2}}}</script>',
'<script id="wp-interactivity-store-data" type="application/json">{"state":{"core":{"a":1,"b":2}}}</script>',
$rendered
);
}
Expand Down

0 comments on commit 4894886

Please sign in to comment.