[ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Blocks API: WP_Block class 4 * 5 * @package WordPress 6 * @since 5.5.0 7 */ 8 9 /** 10 * Class representing a parsed instance of a block. 11 * 12 * @since 5.5.0 13 * @property array $attributes 14 */ 15 #[AllowDynamicProperties] 16 class WP_Block { 17 18 /** 19 * Original parsed array representation of block. 20 * 21 * @since 5.5.0 22 * @var array 23 */ 24 public $parsed_block; 25 26 /** 27 * Name of block. 28 * 29 * @example "core/paragraph" 30 * 31 * @since 5.5.0 32 * @var string 33 */ 34 public $name; 35 36 /** 37 * Block type associated with the instance. 38 * 39 * @since 5.5.0 40 * @var WP_Block_Type 41 */ 42 public $block_type; 43 44 /** 45 * Block context values. 46 * 47 * @since 5.5.0 48 * @var array 49 */ 50 public $context = array(); 51 52 /** 53 * All available context of the current hierarchy. 54 * 55 * @since 5.5.0 56 * @var array 57 * @access protected 58 */ 59 protected $available_context; 60 61 /** 62 * Block type registry. 63 * 64 * @since 5.9.0 65 * @var WP_Block_Type_Registry 66 * @access protected 67 */ 68 protected $registry; 69 70 /** 71 * List of inner blocks (of this same class) 72 * 73 * @since 5.5.0 74 * @var WP_Block_List 75 */ 76 public $inner_blocks = array(); 77 78 /** 79 * Resultant HTML from inside block comment delimiters after removing inner 80 * blocks. 81 * 82 * @example "...Just <!-- wp:test /--> testing..." -> "Just testing..." 83 * 84 * @since 5.5.0 85 * @var string 86 */ 87 public $inner_html = ''; 88 89 /** 90 * List of string fragments and null markers where inner blocks were found 91 * 92 * @example array( 93 * 'inner_html' => 'BeforeInnerAfter', 94 * 'inner_blocks' => array( block, block ), 95 * 'inner_content' => array( 'Before', null, 'Inner', null, 'After' ), 96 * ) 97 * 98 * @since 5.5.0 99 * @var array 100 */ 101 public $inner_content = array(); 102 103 /** 104 * Constructor. 105 * 106 * Populates object properties from the provided block instance argument. 107 * 108 * The given array of context values will not necessarily be available on 109 * the instance itself, but is treated as the full set of values provided by 110 * the block's ancestry. This is assigned to the private `available_context` 111 * property. Only values which are configured to consumed by the block via 112 * its registered type will be assigned to the block's `context` property. 113 * 114 * @since 5.5.0 115 * 116 * @param array $block { 117 * A representative array of a single parsed block object. See WP_Block_Parser_Block. 118 * 119 * @type string $blockName Name of block. 120 * @type array $attrs Attributes from block comment delimiters. 121 * @type array $innerBlocks List of inner blocks. An array of arrays that 122 * have the same structure as this one. 123 * @type string $innerHTML HTML from inside block comment delimiters. 124 * @type array $innerContent List of string fragments and null markers where inner blocks were found. 125 * } 126 * @param array $available_context Optional array of ancestry context values. 127 * @param WP_Block_Type_Registry $registry Optional block type registry. 128 */ 129 public function __construct( $block, $available_context = array(), $registry = null ) { 130 $this->parsed_block = $block; 131 $this->name = $block['blockName']; 132 133 if ( is_null( $registry ) ) { 134 $registry = WP_Block_Type_Registry::get_instance(); 135 } 136 137 $this->registry = $registry; 138 139 $this->block_type = $registry->get_registered( $this->name ); 140 141 $this->available_context = $available_context; 142 143 if ( ! empty( $this->block_type->uses_context ) ) { 144 foreach ( $this->block_type->uses_context as $context_name ) { 145 if ( array_key_exists( $context_name, $this->available_context ) ) { 146 $this->context[ $context_name ] = $this->available_context[ $context_name ]; 147 } 148 } 149 } 150 151 if ( ! empty( $block['innerBlocks'] ) ) { 152 $child_context = $this->available_context; 153 154 if ( ! empty( $this->block_type->provides_context ) ) { 155 foreach ( $this->block_type->provides_context as $context_name => $attribute_name ) { 156 if ( array_key_exists( $attribute_name, $this->attributes ) ) { 157 $child_context[ $context_name ] = $this->attributes[ $attribute_name ]; 158 } 159 } 160 } 161 162 $this->inner_blocks = new WP_Block_List( $block['innerBlocks'], $child_context, $registry ); 163 } 164 165 if ( ! empty( $block['innerHTML'] ) ) { 166 $this->inner_html = $block['innerHTML']; 167 } 168 169 if ( ! empty( $block['innerContent'] ) ) { 170 $this->inner_content = $block['innerContent']; 171 } 172 } 173 174 /** 175 * Returns a value from an inaccessible property. 176 * 177 * This is used to lazily initialize the `attributes` property of a block, 178 * such that it is only prepared with default attributes at the time that 179 * the property is accessed. For all other inaccessible properties, a `null` 180 * value is returned. 181 * 182 * @since 5.5.0 183 * 184 * @param string $name Property name. 185 * @return array|null Prepared attributes, or null. 186 */ 187 public function __get( $name ) { 188 if ( 'attributes' === $name ) { 189 $this->attributes = isset( $this->parsed_block['attrs'] ) ? 190 $this->parsed_block['attrs'] : 191 array(); 192 193 if ( ! is_null( $this->block_type ) ) { 194 $this->attributes = $this->block_type->prepare_attributes_for_render( $this->attributes ); 195 } 196 197 return $this->attributes; 198 } 199 200 return null; 201 } 202 203 /** 204 * Processes the block bindings and updates the block attributes with the values from the sources. 205 * 206 * A block might contain bindings in its attributes. Bindings are mappings 207 * between an attribute of the block and a source. A "source" is a function 208 * registered with `register_block_bindings_source()` that defines how to 209 * retrieve a value from outside the block, e.g. from post meta. 210 * 211 * This function will process those bindings and update the block's attributes 212 * with the values coming from the bindings. 213 * 214 * ### Example 215 * 216 * The "bindings" property for an Image block might look like this: 217 * 218 * ```json 219 * { 220 * "metadata": { 221 * "bindings": { 222 * "title": { 223 * "source": "core/post-meta", 224 * "args": { "key": "text_custom_field" } 225 * }, 226 * "url": { 227 * "source": "core/post-meta", 228 * "args": { "key": "url_custom_field" } 229 * } 230 * } 231 * } 232 * } 233 * ``` 234 * 235 * The above example will replace the `title` and `url` attributes of the Image 236 * block with the values of the `text_custom_field` and `url_custom_field` post meta. 237 * 238 * @since 6.5.0 239 * 240 * @return array The computed block attributes for the provided block bindings. 241 */ 242 private function process_block_bindings() { 243 $parsed_block = $this->parsed_block; 244 $computed_attributes = array(); 245 $supported_block_attributes = array( 246 'core/paragraph' => array( 'content' ), 247 'core/heading' => array( 'content' ), 248 'core/image' => array( 'id', 'url', 'title', 'alt' ), 249 'core/button' => array( 'url', 'text', 'linkTarget', 'rel' ), 250 ); 251 252 // If the block doesn't have the bindings property, isn't one of the supported 253 // block types, or the bindings property is not an array, return the block content. 254 if ( 255 ! isset( $supported_block_attributes[ $this->name ] ) || 256 empty( $parsed_block['attrs']['metadata']['bindings'] ) || 257 ! is_array( $parsed_block['attrs']['metadata']['bindings'] ) 258 ) { 259 return $computed_attributes; 260 } 261 262 foreach ( $parsed_block['attrs']['metadata']['bindings'] as $attribute_name => $block_binding ) { 263 // If the attribute is not in the supported list, process next attribute. 264 if ( ! in_array( $attribute_name, $supported_block_attributes[ $this->name ], true ) ) { 265 continue; 266 } 267 // If no source is provided, or that source is not registered, process next attribute. 268 if ( ! isset( $block_binding['source'] ) || ! is_string( $block_binding['source'] ) ) { 269 continue; 270 } 271 272 $block_binding_source = get_block_bindings_source( $block_binding['source'] ); 273 if ( null === $block_binding_source ) { 274 continue; 275 } 276 277 $source_args = ! empty( $block_binding['args'] ) && is_array( $block_binding['args'] ) ? $block_binding['args'] : array(); 278 $source_value = $block_binding_source->get_value( $source_args, $this, $attribute_name ); 279 280 // If the value is not null, process the HTML based on the block and the attribute. 281 if ( ! is_null( $source_value ) ) { 282 $computed_attributes[ $attribute_name ] = $source_value; 283 } 284 } 285 286 return $computed_attributes; 287 } 288 289 /** 290 * Depending on the block attribute name, replace its value in the HTML based on the value provided. 291 * 292 * @since 6.5.0 293 * 294 * @param string $block_content Block content. 295 * @param string $attribute_name The attribute name to replace. 296 * @param mixed $source_value The value used to replace in the HTML. 297 * @return string The modified block content. 298 */ 299 private function replace_html( string $block_content, string $attribute_name, $source_value ) { 300 $block_type = $this->block_type; 301 if ( ! isset( $block_type->attributes[ $attribute_name ]['source'] ) ) { 302 return $block_content; 303 } 304 305 // Depending on the attribute source, the processing will be different. 306 switch ( $block_type->attributes[ $attribute_name ]['source'] ) { 307 case 'html': 308 case 'rich-text': 309 $block_reader = new WP_HTML_Tag_Processor( $block_content ); 310 311 // TODO: Support for CSS selectors whenever they are ready in the HTML API. 312 // In the meantime, support comma-separated selectors by exploding them into an array. 313 $selectors = explode( ',', $block_type->attributes[ $attribute_name ]['selector'] ); 314 // Add a bookmark to the first tag to be able to iterate over the selectors. 315 $block_reader->next_tag(); 316 $block_reader->set_bookmark( 'iterate-selectors' ); 317 318 // TODO: This shouldn't be needed when the `set_inner_html` function is ready. 319 // Store the parent tag and its attributes to be able to restore them later in the button. 320 // The button block has a wrapper while the paragraph and heading blocks don't. 321 if ( 'core/button' === $this->name ) { 322 $button_wrapper = $block_reader->get_tag(); 323 $button_wrapper_attribute_names = $block_reader->get_attribute_names_with_prefix( '' ); 324 $button_wrapper_attrs = array(); 325 foreach ( $button_wrapper_attribute_names as $name ) { 326 $button_wrapper_attrs[ $name ] = $block_reader->get_attribute( $name ); 327 } 328 } 329 330 foreach ( $selectors as $selector ) { 331 // If the parent tag, or any of its children, matches the selector, replace the HTML. 332 if ( strcasecmp( $block_reader->get_tag( $selector ), $selector ) === 0 || $block_reader->next_tag( 333 array( 334 'tag_name' => $selector, 335 ) 336 ) ) { 337 $block_reader->release_bookmark( 'iterate-selectors' ); 338 339 // TODO: Use `set_inner_html` method whenever it's ready in the HTML API. 340 // Until then, it is hardcoded for the paragraph, heading, and button blocks. 341 // Store the tag and its attributes to be able to restore them later. 342 $selector_attribute_names = $block_reader->get_attribute_names_with_prefix( '' ); 343 $selector_attrs = array(); 344 foreach ( $selector_attribute_names as $name ) { 345 $selector_attrs[ $name ] = $block_reader->get_attribute( $name ); 346 } 347 $selector_markup = "<$selector>" . wp_kses_post( $source_value ) . "</$selector>"; 348 $amended_content = new WP_HTML_Tag_Processor( $selector_markup ); 349 $amended_content->next_tag(); 350 foreach ( $selector_attrs as $attribute_key => $attribute_value ) { 351 $amended_content->set_attribute( $attribute_key, $attribute_value ); 352 } 353 if ( 'core/paragraph' === $this->name || 'core/heading' === $this->name ) { 354 return $amended_content->get_updated_html(); 355 } 356 if ( 'core/button' === $this->name ) { 357 $button_markup = "<$button_wrapper>{$amended_content->get_updated_html()}</$button_wrapper>"; 358 $amended_button = new WP_HTML_Tag_Processor( $button_markup ); 359 $amended_button->next_tag(); 360 foreach ( $button_wrapper_attrs as $attribute_key => $attribute_value ) { 361 $amended_button->set_attribute( $attribute_key, $attribute_value ); 362 } 363 return $amended_button->get_updated_html(); 364 } 365 } else { 366 $block_reader->seek( 'iterate-selectors' ); 367 } 368 } 369 $block_reader->release_bookmark( 'iterate-selectors' ); 370 return $block_content; 371 372 case 'attribute': 373 $amended_content = new WP_HTML_Tag_Processor( $block_content ); 374 if ( ! $amended_content->next_tag( 375 array( 376 // TODO: build the query from CSS selector. 377 'tag_name' => $block_type->attributes[ $attribute_name ]['selector'], 378 ) 379 ) ) { 380 return $block_content; 381 } 382 $amended_content->set_attribute( $block_type->attributes[ $attribute_name ]['attribute'], $source_value ); 383 return $amended_content->get_updated_html(); 384 break; 385 386 default: 387 return $block_content; 388 break; 389 } 390 return; 391 } 392 393 394 /** 395 * Generates the render output for the block. 396 * 397 * @since 5.5.0 398 * @since 6.5.0 Added block bindings processing. 399 * 400 * @global WP_Post $post Global post object. 401 * 402 * @param array $options { 403 * Optional options object. 404 * 405 * @type bool $dynamic Defaults to 'true'. Optionally set to false to avoid using the block's render_callback. 406 * } 407 * @return string Rendered block output. 408 */ 409 public function render( $options = array() ) { 410 global $post; 411 $options = wp_parse_args( 412 $options, 413 array( 414 'dynamic' => true, 415 ) 416 ); 417 418 // Process the block bindings and get attributes updated with the values from the sources. 419 $computed_attributes = $this->process_block_bindings(); 420 if ( ! empty( $computed_attributes ) ) { 421 // Merge the computed attributes with the original attributes. 422 $this->attributes = array_merge( $this->attributes, $computed_attributes ); 423 } 424 425 $is_dynamic = $options['dynamic'] && $this->name && null !== $this->block_type && $this->block_type->is_dynamic(); 426 $block_content = ''; 427 428 if ( ! $options['dynamic'] || empty( $this->block_type->skip_inner_blocks ) ) { 429 $index = 0; 430 431 foreach ( $this->inner_content as $chunk ) { 432 if ( is_string( $chunk ) ) { 433 $block_content .= $chunk; 434 } else { 435 $inner_block = $this->inner_blocks[ $index ]; 436 $parent_block = $this; 437 438 /** This filter is documented in wp-includes/blocks.php */ 439 $pre_render = apply_filters( 'pre_render_block', null, $inner_block->parsed_block, $parent_block ); 440 441 if ( ! is_null( $pre_render ) ) { 442 $block_content .= $pre_render; 443 } else { 444 $source_block = $inner_block->parsed_block; 445 446 /** This filter is documented in wp-includes/blocks.php */ 447 $inner_block->parsed_block = apply_filters( 'render_block_data', $inner_block->parsed_block, $source_block, $parent_block ); 448 449 /** This filter is documented in wp-includes/blocks.php */ 450 $inner_block->context = apply_filters( 'render_block_context', $inner_block->context, $inner_block->parsed_block, $parent_block ); 451 452 $block_content .= $inner_block->render(); 453 } 454 455 ++$index; 456 } 457 } 458 } 459 460 if ( ! empty( $computed_attributes ) && ! empty( $block_content ) ) { 461 foreach ( $computed_attributes as $attribute_name => $source_value ) { 462 $block_content = $this->replace_html( $block_content, $attribute_name, $source_value ); 463 } 464 } 465 466 if ( $is_dynamic ) { 467 $global_post = $post; 468 $parent = WP_Block_Supports::$block_to_render; 469 470 WP_Block_Supports::$block_to_render = $this->parsed_block; 471 472 $block_content = (string) call_user_func( $this->block_type->render_callback, $this->attributes, $block_content, $this ); 473 474 WP_Block_Supports::$block_to_render = $parent; 475 476 $post = $global_post; 477 } 478 479 if ( ( ! empty( $this->block_type->script_handles ) ) ) { 480 foreach ( $this->block_type->script_handles as $script_handle ) { 481 wp_enqueue_script( $script_handle ); 482 } 483 } 484 485 if ( ! empty( $this->block_type->view_script_handles ) ) { 486 foreach ( $this->block_type->view_script_handles as $view_script_handle ) { 487 wp_enqueue_script( $view_script_handle ); 488 } 489 } 490 491 if ( ! empty( $this->block_type->view_script_module_ids ) ) { 492 foreach ( $this->block_type->view_script_module_ids as $view_script_module_id ) { 493 wp_enqueue_script_module( $view_script_module_id ); 494 } 495 } 496 497 if ( ( ! empty( $this->block_type->style_handles ) ) ) { 498 foreach ( $this->block_type->style_handles as $style_handle ) { 499 wp_enqueue_style( $style_handle ); 500 } 501 } 502 503 if ( ( ! empty( $this->block_type->view_style_handles ) ) ) { 504 foreach ( $this->block_type->view_style_handles as $view_style_handle ) { 505 wp_enqueue_style( $view_style_handle ); 506 } 507 } 508 509 /** 510 * Filters the content of a single block. 511 * 512 * @since 5.0.0 513 * @since 5.9.0 The `$instance` parameter was added. 514 * 515 * @param string $block_content The block content. 516 * @param array $block The full block, including name and attributes. 517 * @param WP_Block $instance The block instance. 518 */ 519 $block_content = apply_filters( 'render_block', $block_content, $this->parsed_block, $this ); 520 521 /** 522 * Filters the content of a single block. 523 * 524 * The dynamic portion of the hook name, `$name`, refers to 525 * the block name, e.g. "core/paragraph". 526 * 527 * @since 5.7.0 528 * @since 5.9.0 The `$instance` parameter was added. 529 * 530 * @param string $block_content The block content. 531 * @param array $block The full block, including name and attributes. 532 * @param WP_Block $instance The block instance. 533 */ 534 $block_content = apply_filters( "render_block_{$this->name}", $block_content, $this->parsed_block, $this ); 535 536 return $block_content; 537 } 538 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Thu May 9 08:20:02 2024 | Cross-referenced by PHPXref |