| 1 |
<?php |
| 2 |
/** |
| 3 |
* Temporary compatibility shims for block APIs present in Gutenberg. |
| 4 |
* |
| 5 |
* @package gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Checks whether the experimental Interactivity API should be used for a block. |
| 10 |
* |
| 11 |
* Note: This function is located here instead of in interactivity-api/blocks.php because it has to be available earler. |
| 12 |
* |
| 13 |
* @param string $block_name Block name. |
| 14 |
* @return bool Whether Interactivity API is used for block. |
| 15 |
*/ |
| 16 |
function gutenberg_should_block_use_interactivity_api( $block_name ) { |
| 17 |
|
| 18 |
/** |
| 19 |
* Filters whether the experimental Interactivity API should be used for a block. |
| 20 |
* |
| 21 |
* @since 6.3.0 |
| 22 |
* |
| 23 |
* @param bool $enabled Whether Interactivity API is used for block. |
| 24 |
* @param string $block_name Block name. |
| 25 |
*/ |
| 26 |
return (bool) apply_filters( 'gutenberg_should_block_use_interactivity_api', true, $block_name ); |
| 27 |
} |
| 28 |
|
| 29 |
if ( ! function_exists( 'wp_enqueue_block_view_script' ) ) { |
| 30 |
/** |
| 31 |
* Enqueues a frontend script for a specific block. |
| 32 |
* |
| 33 |
* Scripts enqueued using this function will only get printed |
| 34 |
* when the block gets rendered on the frontend. |
| 35 |
* |
| 36 |
* @since 6.2.0 |
| 37 |
* |
| 38 |
* @param string $block_name The block name, including namespace. |
| 39 |
* @param array $args An array of arguments [handle,src,deps,ver,media,textdomain]. |
| 40 |
* |
| 41 |
* @return void |
| 42 |
*/ |
| 43 |
function wp_enqueue_block_view_script( $block_name, $args ) { |
| 44 |
$args = wp_parse_args( |
| 45 |
$args, |
| 46 |
array( |
| 47 |
'handle' => '', |
| 48 |
'src' => '', |
| 49 |
'deps' => array(), |
| 50 |
'ver' => false, |
| 51 |
'in_footer' => false, |
| 52 |
|
| 53 |
// Additional args to allow translations for the script's textdomain. |
| 54 |
'textdomain' => '', |
| 55 |
) |
| 56 |
); |
| 57 |
|
| 58 |
/** |
| 59 |
* Callback function to register and enqueue scripts. |
| 60 |
* |
| 61 |
* @param string $content When the callback is used for the render_block filter, |
| 62 |
* the content needs to be returned so the function parameter |
| 63 |
* is to ensure the content exists. |
| 64 |
* @return string Block content. |
| 65 |
*/ |
| 66 |
$callback = static function( $content, $block ) use ( $args, $block_name ) { |
| 67 |
|
| 68 |
// Sanity check. |
| 69 |
if ( empty( $block['blockName'] ) || $block_name !== $block['blockName'] ) { |
| 70 |
return $content; |
| 71 |
} |
| 72 |
|
| 73 |
// Register the stylesheet. |
| 74 |
if ( ! empty( $args['src'] ) ) { |
| 75 |
wp_register_script( $args['handle'], $args['src'], $args['deps'], $args['ver'], $args['in_footer'] ); |
| 76 |
} |
| 77 |
|
| 78 |
// Enqueue the stylesheet. |
| 79 |
wp_enqueue_script( $args['handle'] ); |
| 80 |
|
| 81 |
// If a textdomain is defined, use it to set the script translations. |
| 82 |
if ( ! empty( $args['textdomain'] ) && in_array( 'wp-i18n', $args['deps'], true ) ) { |
| 83 |
wp_set_script_translations( $args['handle'], $args['textdomain'], $args['domainpath'] ); |
| 84 |
} |
| 85 |
|
| 86 |
return $content; |
| 87 |
}; |
| 88 |
|
| 89 |
/* |
| 90 |
* The filter's callback here is an anonymous function because |
| 91 |
* using a named function in this case is not possible. |
| 92 |
* |
| 93 |
* The function cannot be unhooked, however, users are still able |
| 94 |
* to dequeue the script registered/enqueued by the callback |
| 95 |
* which is why in this case, using an anonymous function |
| 96 |
* was deemed acceptable. |
| 97 |
*/ |
| 98 |
add_filter( 'render_block', $callback, 10, 2 ); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
/** |
| 104 |
* Registers the metadata block attribute for block types. |
| 105 |
* |
| 106 |
* @param array $args Array of arguments for registering a block type. |
| 107 |
* @return array $args |
| 108 |
*/ |
| 109 |
function gutenberg_register_metadata_attribute( $args ) { |
| 110 |
// Setup attributes if needed. |
| 111 |
if ( ! isset( $args['attributes'] ) || ! is_array( $args['attributes'] ) ) { |
| 112 |
$args['attributes'] = array(); |
| 113 |
} |
| 114 |
|
| 115 |
if ( ! array_key_exists( 'metadata', $args['attributes'] ) ) { |
| 116 |
$args['attributes']['metadata'] = array( |
| 117 |
'type' => 'object', |
| 118 |
); |
| 119 |
} |
| 120 |
|
| 121 |
return $args; |
| 122 |
} |
| 123 |
add_filter( 'register_block_type_args', 'gutenberg_register_metadata_attribute' ); |
| 124 |
|
| 125 |
|
| 126 |
$gutenberg_experiments = get_option( 'gutenberg-experiments' ); |
| 127 |
if ( $gutenberg_experiments && array_key_exists( 'gutenberg-connections', $gutenberg_experiments ) ) { |
| 128 |
/** |
| 129 |
* Renders the block meta attributes. |
| 130 |
* |
| 131 |
* @param string $block_content Block Content. |
| 132 |
* @param array $block Block attributes. |
| 133 |
* @param WP_Block $block_instance The block instance. |
| 134 |
*/ |
| 135 |
function gutenberg_render_block_connections( $block_content, $block, $block_instance ) { |
| 136 |
$connection_sources = require __DIR__ . '/connection-sources/index.php'; |
| 137 |
$block_type = $block_instance->block_type; |
| 138 |
|
| 139 |
// Allowlist of blocks that support block connections. |
| 140 |
// Currently, we only allow the following blocks and attributes: |
| 141 |
// - Paragraph: content. |
| 142 |
// - Image: url. |
| 143 |
$blocks_attributes_allowlist = array( |
| 144 |
'core/paragraph' => array( 'content' ), |
| 145 |
'core/image' => array( 'url' ), |
| 146 |
); |
| 147 |
|
| 148 |
// Whitelist of the block types that support block connections. |
| 149 |
// Currently, we only allow the Paragraph and Image blocks to use block connections. |
| 150 |
if ( ! in_array( $block['blockName'], array_keys( $blocks_attributes_allowlist ), true ) ) { |
| 151 |
return $block_content; |
| 152 |
} |
| 153 |
|
| 154 |
// If for some reason, the block type is not found, skip it. |
| 155 |
if ( null === $block_type ) { |
| 156 |
return $block_content; |
| 157 |
} |
| 158 |
|
| 159 |
// If the block does not have support for block connections, skip it. |
| 160 |
if ( ! block_has_support( $block_type, array( '__experimentalConnections' ), false ) ) { |
| 161 |
return $block_content; |
| 162 |
} |
| 163 |
|
| 164 |
// Get all the attributes that have a connection. |
| 165 |
$connected_attributes = _wp_array_get( $block['attrs'], array( 'connections', 'attributes' ), false ); |
| 166 |
if ( ! $connected_attributes ) { |
| 167 |
return $block_content; |
| 168 |
} |
| 169 |
|
| 170 |
foreach ( $connected_attributes as $attribute_name => $attribute_value ) { |
| 171 |
|
| 172 |
// If the attribute is not in the allowlist, skip it. |
| 173 |
if ( ! in_array( $attribute_name, $blocks_attributes_allowlist[ $block['blockName'] ], true ) ) { |
| 174 |
continue; |
| 175 |
} |
| 176 |
|
| 177 |
// If the source value is not "meta_fields", skip it because the only supported |
| 178 |
// connection source is meta (custom fields) for now. |
| 179 |
if ( 'meta_fields' !== $attribute_value['source'] ) { |
| 180 |
continue; |
| 181 |
} |
| 182 |
|
| 183 |
// If the attribute does not have a source, skip it. |
| 184 |
if ( ! isset( $block_type->attributes[ $attribute_name ]['source'] ) ) { |
| 185 |
continue; |
| 186 |
} |
| 187 |
|
| 188 |
// If the attribute does not specify the name of the custom field, skip it. |
| 189 |
if ( ! isset( $attribute_value['value'] ) ) { |
| 190 |
continue; |
| 191 |
} |
| 192 |
|
| 193 |
// Get the content from the connection source. |
| 194 |
$custom_value = $connection_sources[ $attribute_value['source'] ]( |
| 195 |
$block_instance, |
| 196 |
$attribute_value['value'] |
| 197 |
); |
| 198 |
|
| 199 |
$tags = new WP_HTML_Tag_Processor( $block_content ); |
| 200 |
$found = $tags->next_tag( |
| 201 |
array( |
| 202 |
// TODO: In the future, when blocks other than Paragraph and Image are |
| 203 |
// supported, we should build the full query from CSS selector. |
| 204 |
'tag_name' => $block_type->attributes[ $attribute_name ]['selector'], |
| 205 |
) |
| 206 |
); |
| 207 |
if ( ! $found ) { |
| 208 |
return $block_content; |
| 209 |
}; |
| 210 |
$tag_name = $tags->get_tag(); |
| 211 |
$markup = "<$tag_name>$custom_value</$tag_name>"; |
| 212 |
$updated_tags = new WP_HTML_Tag_Processor( $markup ); |
| 213 |
$updated_tags->next_tag(); |
| 214 |
|
| 215 |
// Get all the attributes from the original block and add them to the new markup. |
| 216 |
$names = $tags->get_attribute_names_with_prefix( '' ); |
| 217 |
foreach ( $names as $name ) { |
| 218 |
$updated_tags->set_attribute( $name, $tags->get_attribute( $name ) ); |
| 219 |
} |
| 220 |
|
| 221 |
return $updated_tags->get_updated_html(); |
| 222 |
} |
| 223 |
|
| 224 |
return $block_content; |
| 225 |
} |
| 226 |
add_filter( 'render_block', 'gutenberg_render_block_connections', 10, 3 ); |
| 227 |
} |
| 228 |
|