| 1 |
<?php |
| 2 |
/** |
| 3 |
* Functions and hooks to process the server side rendering of the Interactivity |
| 4 |
* API directives. |
| 5 |
* |
| 6 |
* @package Gutenberg |
| 7 |
* @subpackage Interactivity API |
| 8 |
*/ |
| 9 |
|
| 10 |
/** |
| 11 |
* Mark if the block is a root block. Checks that there is already a root block |
| 12 |
* in order not to mark template-parts or synced patterns as root blocks, where |
| 13 |
* the parent is null. |
| 14 |
* |
| 15 |
* @param array $parsed_block The parsed block. |
| 16 |
* @param array $source_block The source block. |
| 17 |
* @param array $parent_block The parent block. |
| 18 |
* |
| 19 |
* @return array The parsed block. |
| 20 |
*/ |
| 21 |
function gutenberg_interactivity_mark_root_blocks( $parsed_block, $source_block, $parent_block ) { |
| 22 |
if ( ! isset( $parent_block ) && ! WP_Directive_Processor::has_root_block() ) { |
| 23 |
WP_Directive_Processor::mark_root_block( $parsed_block ); |
| 24 |
} |
| 25 |
|
| 26 |
return $parsed_block; |
| 27 |
} |
| 28 |
add_filter( 'render_block_data', 'gutenberg_interactivity_mark_root_blocks', 10, 3 ); |
| 29 |
|
| 30 |
/** |
| 31 |
* Process directives in each root block. |
| 32 |
* |
| 33 |
* @param string $block_content The block content. |
| 34 |
* @param array $block The full block. |
| 35 |
* |
| 36 |
* @return string Filtered block content. |
| 37 |
*/ |
| 38 |
function gutenberg_process_directives_in_root_blocks( $block_content, $block ) { |
| 39 |
if ( WP_Directive_Processor::is_marked_as_root_block( $block ) ) { |
| 40 |
WP_Directive_Processor::unmark_root_block(); |
| 41 |
$directives = array( |
| 42 |
'data-wp-bind' => 'gutenberg_interactivity_process_wp_bind', |
| 43 |
'data-wp-context' => 'gutenberg_interactivity_process_wp_context', |
| 44 |
'data-wp-class' => 'gutenberg_interactivity_process_wp_class', |
| 45 |
'data-wp-style' => 'gutenberg_interactivity_process_wp_style', |
| 46 |
'data-wp-text' => 'gutenberg_interactivity_process_wp_text', |
| 47 |
); |
| 48 |
|
| 49 |
$tags = new WP_Directive_Processor( $block_content ); |
| 50 |
$tags = $tags->process_rendered_html( $tags, 'data-wp-', $directives ); |
| 51 |
return $tags->get_updated_html(); |
| 52 |
|
| 53 |
} |
| 54 |
|
| 55 |
return $block_content; |
| 56 |
} |
| 57 |
add_filter( 'render_block', 'gutenberg_process_directives_in_root_blocks', 10, 2 ); |
| 58 |
|
| 59 |
|
| 60 |
/** |
| 61 |
* Resolve the reference using the store and the context from the provided path. |
| 62 |
* |
| 63 |
* @param string $path Path. |
| 64 |
* @param array $context Context data. |
| 65 |
* @return mixed |
| 66 |
*/ |
| 67 |
function gutenberg_interactivity_evaluate_reference( $path, array $context = array() ) { |
| 68 |
$store = array_merge( |
| 69 |
WP_Interactivity_Store::get_data(), |
| 70 |
array( 'context' => $context ) |
| 71 |
); |
| 72 |
|
| 73 |
/* |
| 74 |
* Check first if the directive path is preceded by a negator operator (!), |
| 75 |
* indicating that the value obtained from the Interactivity Store (or the |
| 76 |
* passed context) using the subsequent path should be negated. |
| 77 |
*/ |
| 78 |
$should_negate_value = '!' === $path[0]; |
| 79 |
|
| 80 |
$path = $should_negate_value ? substr( $path, 1 ) : $path; |
| 81 |
$path_segments = explode( '.', $path ); |
| 82 |
$current = $store; |
| 83 |
foreach ( $path_segments as $p ) { |
| 84 |
if ( isset( $current[ $p ] ) ) { |
| 85 |
$current = $current[ $p ]; |
| 86 |
} else { |
| 87 |
return null; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
/* |
| 92 |
* Check if $current is an anonymous function or an arrow function, and if |
| 93 |
* so, call it passing the store. Other types of callables are ignored on |
| 94 |
* purpose, as arbitrary strings or arrays could be wrongly evaluated as |
| 95 |
* "callables". |
| 96 |
* |
| 97 |
* E.g., "file" is an string and a "callable" (the "file" function exists). |
| 98 |
*/ |
| 99 |
if ( $current instanceof Closure ) { |
| 100 |
$current = call_user_func( $current, $store ); |
| 101 |
} |
| 102 |
|
| 103 |
// Return the opposite if it has a negator operator (!). |
| 104 |
return $should_negate_value ? ! $current : $current; |
| 105 |
} |
| 106 |
|