-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Interactivity API: Implement
wp_initial_state()
(#57556)
* Update directive processing to handle namespaces * Add required namespace param to evaluate reference * Handle references with namespaces * Rename parse_reference to parse_attribute_value * Update directive implementations * Improve comments in `parse_attribute_value` * Restore changes from `store()` migration PR * Do not make namespace optional in `get_state()` * Replace Store with Initial_State * Fix set up and tear down of Initial State tests * Update directive tests after implementation changes * Add TODO comment to derived state * Refactor directive processing tests * Move namespace logic to a directive * Add tests for namespaces logic * Fix typo in comments * Handle empty attributes in wp-interactive * Handle boolean attributes in wp-context * Do not support derived state yet * Add root blocks bail out quick by name --------- Co-authored-by: Carlos Bravo <carlos.bravo@automattic.com>
- Loading branch information
1 parent
9323cdd
commit 76e1642
Showing
21 changed files
with
723 additions
and
431 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
lib/experimental/interactivity-api/class-wp-interactivity-initial-state.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
/** | ||
* WP_Interactivity_Initial_State class | ||
* | ||
* @package Gutenberg | ||
* @subpackage Interactivity API | ||
*/ | ||
|
||
if ( class_exists( 'WP_Interactivity_Initial_State' ) ) { | ||
return; | ||
} | ||
|
||
/** | ||
* 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. | ||
* | ||
* @package Gutenberg | ||
* @subpackage Interactivity API | ||
*/ | ||
class WP_Interactivity_Initial_State { | ||
/** | ||
* Map of initial state by namespace. | ||
* | ||
* @var array | ||
*/ | ||
private static $initial_state = array(); | ||
|
||
/** | ||
* Get state from a given namespace. | ||
* | ||
* @param string $store_ns Namespace. | ||
* | ||
* @return array The requested state. | ||
*/ | ||
public static function get_state( $store_ns ) { | ||
return self::$initial_state[ $store_ns ] ?? array(); | ||
} | ||
|
||
/** | ||
* Merge data into the state with the given namespace. | ||
* | ||
* @param string $store_ns Namespace. | ||
* @param array $data State to merge. | ||
* | ||
* @return void | ||
*/ | ||
public static function merge_state( $store_ns, $data ) { | ||
self::$initial_state[ $store_ns ] = array_replace_recursive( | ||
self::get_state( $store_ns ), | ||
$data | ||
); | ||
} | ||
|
||
/** | ||
* Get store data. | ||
* | ||
* @return array | ||
*/ | ||
public static function get_data() { | ||
return self::$initial_state; | ||
} | ||
|
||
/** | ||
* Reset the initial state. | ||
*/ | ||
public static function reset() { | ||
self::$initial_state = array(); | ||
} | ||
|
||
/** | ||
* Render the initial state. | ||
*/ | ||
public static function render() { | ||
if ( empty( self::$initial_state ) ) { | ||
return; | ||
} | ||
echo sprintf( | ||
'<script id="wp-interactivity-initial-state" type="application/json">%s</script>', | ||
wp_json_encode( self::$initial_state, JSON_HEX_TAG | JSON_HEX_AMP ) | ||
); | ||
} | ||
} |
69 changes: 0 additions & 69 deletions
69
lib/experimental/interactivity-api/class-wp-interactivity-store.php
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.