This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 219
Serialize the Interactivity API's store from PHP and hydrate it on the client #8447
Merged
Merged
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
5067e7a
Update Interactivity API
DAreRodz 92d0d9d
Change `wp` prefixes to `woo`
DAreRodz 0199ade
Use `woo` prefix for the directives runtime bundle
DAreRodz ceedc72
Merge branch 'trunk' into update/interactivity-api-prefixes
DAreRodz 6277a3c
Update Interactivity API runtime
DAreRodz 423c858
Hardcode php from interactivity API
DAreRodz 9af6478
Temporarily add gutenberg plugin as dependency
DAreRodz 36bd2c5
Merge branch 'trunk' into update/interactivity-api-store-ssr
DAreRodz 78b7db2
Exclude Interactivity API files from phpcs checks
DAreRodz 92f5a85
Update Interactivity API js files
DAreRodz 0c11440
Update Interactivity API php files
DAreRodz 97f39c1
Remove gutenberg from wp-env plugins
DAreRodz 28dd2ad
Fix registered runtime paths
DAreRodz 5453d6b
Fix prefixes when getting attributes in directives
DAreRodz 1874a4c
Fix directive prefix in constants
DAreRodz e3df31b
Merge branch 'trunk' into update/interactivity-api-store-ssr
DAreRodz 0cd9a23
Avoid a Fatal error when importing `wp-html`
DAreRodz 68b0a66
Remove TODO comments from Interactivity API files
DAreRodz 17fe079
Merge branch 'trunk' into update/interactivity-api-store-ssr
DAreRodz fa0ef8a
Merge branch 'trunk' into update/interactivity-api-store-ssr
DAreRodz 92c93af
Merge branch 'trunk' into update/interactivity-api-store-ssr
DAreRodz a824616
Merge branch 'trunk' into update/interactivity-api-store-ssr
DAreRodz f7ee7d1
Add missing prefix to some global functions
DAreRodz ebd1119
Use true as value for boolean attributes
DAreRodz 2f36582
Add `wp-html` file
DAreRodz 248de47
Merge branch 'trunk' into update/interactivity-api-store-ssr
DAreRodz e842182
Change requires in `wp-html` with includes
DAreRodz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Hardcode php from interactivity API
- Loading branch information
commit 423c858cf4dff718e7ef4b53e465fe6459051d62
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
require_once __DIR__ . '/../utils.php'; | ||
|
||
function process_woo_bind( $tags, $context ) { | ||
if ( $tags->is_tag_closer() ) { | ||
return; | ||
} | ||
|
||
$prefixed_attributes = $tags->get_attribute_names_with_prefix( 'woo-bind:' ); | ||
|
||
foreach ( $prefixed_attributes as $attr ) { | ||
list( , $bound_attr ) = explode( ':', $attr ); | ||
if ( empty( $bound_attr ) ) { | ||
continue; | ||
} | ||
|
||
$expr = $tags->get_attribute( $attr ); | ||
$value = evaluate( $expr, $context->get_context() ); | ||
$tags->set_attribute( $bound_attr, $value ); | ||
} | ||
} |
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,26 @@ | ||
<?php | ||
|
||
require_once __DIR__ . '/../utils.php'; | ||
|
||
function process_woo_class( $tags, $context ) { | ||
if ( $tags->is_tag_closer() ) { | ||
return; | ||
} | ||
|
||
$prefixed_attributes = $tags->get_attribute_names_with_prefix( 'woo-class:' ); | ||
|
||
foreach ( $prefixed_attributes as $attr ) { | ||
list( , $class_name ) = explode( ':', $attr ); | ||
if ( empty( $class_name ) ) { | ||
continue; | ||
} | ||
|
||
$expr = $tags->get_attribute( $attr ); | ||
$add_class = evaluate( $expr, $context->get_context() ); | ||
if ( $add_class ) { | ||
$tags->add_class( $class_name ); | ||
} else { | ||
$tags->remove_class( $class_name ); | ||
} | ||
} | ||
} |
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,19 @@ | ||
<?php | ||
|
||
function process_woo_context_attribute( $tags, $context ) { | ||
if ( $tags->is_tag_closer() ) { | ||
$context->rewind_context(); | ||
return; | ||
} | ||
|
||
$value = $tags->get_attribute( 'woo-context' ); | ||
if ( null === $value ) { | ||
// No woo-context directive. | ||
return; | ||
} | ||
|
||
$new_context = json_decode( $value, true ); | ||
// TODO: Error handling. | ||
|
||
$context->set_context( $new_context ); | ||
} |
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,30 @@ | ||
<?php | ||
|
||
require_once __DIR__ . '/../utils.php'; | ||
|
||
function process_woo_style( $tags, $context ) { | ||
if ( $tags->is_tag_closer() ) { | ||
return; | ||
} | ||
|
||
$prefixed_attributes = $tags->get_attribute_names_with_prefix( 'woo-style:' ); | ||
|
||
foreach ( $prefixed_attributes as $attr ) { | ||
list( , $style_name ) = explode( ':', $attr ); | ||
if ( empty( $style_name ) ) { | ||
continue; | ||
} | ||
|
||
$expr = $tags->get_attribute( $attr ); | ||
$style_value = evaluate( $expr, $context->get_context() ); | ||
if ( $style_value ) { | ||
$style_attr = $tags->get_attribute( 'style' ); | ||
$style_attr = set_style( $style_attr, $style_name, $style_value ); | ||
$tags->set_attribute( 'style', $style_attr ); | ||
} else { | ||
// TODO: Do we want to unset styles if they're null? | ||
// $tags->remove_class( $style_name ); | ||
} | ||
} | ||
} | ||
|
73 changes: 73 additions & 0 deletions
73
src/Interactivity/directives/class-woo-directive-context.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,73 @@ | ||
<?php | ||
/** | ||
* Context data implementation. | ||
* | ||
* @package block-hydration-experiments | ||
*/ | ||
|
||
/** | ||
* This is a data structure to hold the current context. | ||
* | ||
* Whenever encountering a `woo-context` directive, we need to update | ||
* the context with the data found in that directive. Conversely, | ||
* when "leaving" that context (by encountering a closing tag), we | ||
* need to reset the context to its previous state. This means that | ||
* we actually need sort of a stack to keep track of all nested contexts. | ||
* | ||
* Example: | ||
* | ||
* <woo-context data='{ "foo": 123 }'> | ||
* <!-- foo should be 123 here. --> | ||
* <woo-context data='{ "foo": 456 }'> | ||
* <!-- foo should be 456 here. --> | ||
* </woo-context> | ||
* <!-- foo should be reset to 123 here. --> | ||
* </woo-context> | ||
*/ | ||
class Woo_Directive_Context { | ||
/** | ||
* The stack used to store contexts internally. | ||
* | ||
* @var array An array of contexts. | ||
*/ | ||
protected $stack = array( array() ); | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* Accepts a context as an argument to initialize this with. | ||
* | ||
* @param array $context A context. | ||
*/ | ||
function __construct( $context = array() ) { | ||
$this->set_context( $context ); | ||
} | ||
|
||
/** | ||
* Return the current context. | ||
* | ||
* @return array The current context. | ||
*/ | ||
public function get_context() { | ||
return end( $this->stack ); | ||
} | ||
|
||
/** | ||
* Set the current context. | ||
* | ||
* @param array $context The context to be set. | ||
* @return void | ||
*/ | ||
public function set_context( $context ) { | ||
array_push( $this->stack, array_replace_recursive( $this->get_context(), $context ) ); | ||
} | ||
|
||
/** | ||
* Reset the context to its previous state. | ||
* | ||
* @return void | ||
*/ | ||
public function rewind_context() { | ||
array_pop( $this->stack ); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/Interactivity/directives/class-woo-directive-store.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,32 @@ | ||
<?php | ||
|
||
class Woo_Directive_Store { | ||
private static array $store = array(); | ||
|
||
static function get_data() { | ||
return self::$store; | ||
} | ||
|
||
static function merge_data( $data ) { | ||
self::$store = array_replace_recursive( self::$store, $data ); | ||
} | ||
|
||
static function serialize() { | ||
return json_encode( self::$store ); | ||
} | ||
|
||
static function reset() { | ||
self::$store = array(); | ||
} | ||
|
||
static function render() { | ||
if ( empty( self::$store ) ) { | ||
return; | ||
} | ||
|
||
// TODO: find a better ID for the script tag. | ||
$id = 'store'; | ||
$store = self::serialize(); | ||
echo "<script id=\"$id\" type=\"application/json\">$store</script>"; | ||
} | ||
} |
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,19 @@ | ||
<?php | ||
|
||
function process_woo_context_tag( $tags, $context ) { | ||
if ( $tags->is_tag_closer() ) { | ||
$context->rewind_context(); | ||
return; | ||
} | ||
|
||
$value = $tags->get_attribute( 'data' ); | ||
if ( null === $value ) { | ||
// No woo-context directive. | ||
return; | ||
} | ||
|
||
$new_context = json_decode( $value, true ); | ||
// TODO: Error handling. | ||
|
||
$context->set_context( $new_context ); | ||
} |
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,51 @@ | ||
<?php | ||
|
||
require_once __DIR__ . '/class-woo-directive-store.php'; | ||
|
||
function store( $data ) { | ||
Woo_Directive_Store::merge_data( $data ); | ||
} | ||
|
||
// TODO: Implement evaluation of complex logical expressions. | ||
function evaluate( string $path, array $context = array() ) { | ||
$current = array_merge( | ||
Woo_Directive_Store::get_data(), | ||
array( 'context' => $context ) | ||
); | ||
|
||
$array = explode( '.', $path ); | ||
foreach ( $array as $p ) { | ||
if ( isset( $current[ $p ] ) ) { | ||
$current = $current[ $p ]; | ||
} else { | ||
return null; | ||
} | ||
} | ||
return $current; | ||
} | ||
|
||
function set_style( $style, $name, $value ) { | ||
$style_assignments = explode( ';', $style ); | ||
$modified = false; | ||
foreach ( $style_assignments as $style_assignment ) { | ||
list( $style_name ) = explode( ':', $style_assignment ); | ||
if ( trim( $style_name ) === $name ) { | ||
// TODO: Retain surrounding whitespace from $style_value, if any. | ||
$style_assignment = $style_name . ': ' . $value; | ||
$modified = true; | ||
break; | ||
} | ||
} | ||
|
||
if ( ! $modified ) { | ||
$new_style_assignment = $name . ': ' . $value; | ||
// If the last element is empty or whitespace-only, we insert | ||
// the new "key: value" pair before it. | ||
if ( empty( trim( end( $style_assignments ) ) ) ) { | ||
array_splice( $style_assignments, - 1, 0, $new_style_assignment ); | ||
} else { | ||
array_push( $style_assignments, $new_style_assignment ); | ||
} | ||
} | ||
return implode( ';', $style_assignments ); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function names in this file might later collide with the ones we're using in the Block Interactivity API. Have you considered prefixing them?