[ 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 */ 58 protected $available_context = array(); 59 60 /** 61 * Block type registry. 62 * 63 * @since 5.9.0 64 * @var WP_Block_Type_Registry 65 */ 66 protected $registry; 67 68 /** 69 * List of inner blocks (of this same class) 70 * 71 * @since 5.5.0 72 * @var WP_Block_List 73 */ 74 public $inner_blocks = array(); 75 76 /** 77 * Resultant HTML from inside block comment delimiters after removing inner 78 * blocks. 79 * 80 * @example "...Just <!-- wp:test /--> testing..." -> "Just testing..." 81 * 82 * @since 5.5.0 83 * @var string 84 */ 85 public $inner_html = ''; 86 87 /** 88 * List of string fragments and null markers where inner blocks were found 89 * 90 * @example array( 91 * 'inner_html' => 'BeforeInnerAfter', 92 * 'inner_blocks' => array( block, block ), 93 * 'inner_content' => array( 'Before', null, 'Inner', null, 'After' ), 94 * ) 95 * 96 * @since 5.5.0 97 * @var array 98 */ 99 public $inner_content = array(); 100 101 /** 102 * Constructor. 103 * 104 * Populates object properties from the provided block instance argument. 105 * 106 * The given array of context values will not necessarily be available on 107 * the instance itself, but is treated as the full set of values provided by 108 * the block's ancestry. This is assigned to the private `available_context` 109 * property. Only values which are configured to consumed by the block via 110 * its registered type will be assigned to the block's `context` property. 111 * 112 * @since 5.5.0 113 * 114 * @param array $block { 115 * An associative array of a single parsed block object. See WP_Block_Parser_Block. 116 * 117 * @type string $blockName Name of block. 118 * @type array $attrs Attributes from block comment delimiters. 119 * @type array $innerBlocks List of inner blocks. An array of arrays that 120 * have the same structure as this one. 121 * @type string $innerHTML HTML from inside block comment delimiters. 122 * @type array $innerContent List of string fragments and null markers where inner blocks were found. 123 * } 124 * @param array $available_context Optional array of ancestry context values. 125 * @param WP_Block_Type_Registry $registry Optional block type registry. 126 */ 127 public function __construct( $block, $available_context = array(), $registry = null ) { 128 $this->parsed_block = $block; 129 $this->name = $block['blockName']; 130 131 if ( is_null( $registry ) ) { 132 $registry = WP_Block_Type_Registry::get_instance(); 133 } 134 135 $this->registry = $registry; 136 137 $this->block_type = $registry->get_registered( $this->name ); 138 139 $this->available_context = $available_context; 140 141 $this->refresh_context_dependents(); 142 } 143 144 /** 145 * Updates the context for the current block and its inner blocks. 146 * 147 * The method updates the context of inner blocks, if any, by passing down 148 * any context values the block provides (`provides_context`). 149 * 150 * If the block has inner blocks, the method recursively processes them by creating new instances of `WP_Block` 151 * for each inner block and updating their context based on the block's `provides_context` property. 152 * 153 * @since 6.8.0 154 */ 155 public function refresh_context_dependents() { 156 /* 157 * Merging the `$context` property here is not ideal, but for now needs to happen because of backward compatibility. 158 * Ideally, the `$context` property itself would not be filterable directly and only the `$available_context` would be filterable. 159 * However, this needs to be separately explored whether it's possible without breakage. 160 */ 161 $this->available_context = array_merge( $this->available_context, $this->context ); 162 163 if ( ! empty( $this->block_type->uses_context ) ) { 164 foreach ( $this->block_type->uses_context as $context_name ) { 165 if ( array_key_exists( $context_name, $this->available_context ) ) { 166 $this->context[ $context_name ] = $this->available_context[ $context_name ]; 167 } 168 } 169 } 170 171 $this->refresh_parsed_block_dependents(); 172 } 173 174 /** 175 * Updates the parsed block content for the current block and its inner blocks. 176 * 177 * This method sets the `inner_html` and `inner_content` properties of the block based on the parsed 178 * block content provided during initialization. It ensures that the block instance reflects the 179 * most up-to-date content for both the inner HTML and any string fragments around inner blocks. 180 * 181 * If the block has inner blocks, this method initializes a new `WP_Block_List` for them, ensuring the 182 * correct content and context are updated for each nested block. 183 * 184 * @since 6.8.0 185 */ 186 public function refresh_parsed_block_dependents() { 187 if ( ! empty( $this->parsed_block['innerBlocks'] ) ) { 188 $child_context = $this->available_context; 189 190 if ( ! empty( $this->block_type->provides_context ) ) { 191 foreach ( $this->block_type->provides_context as $context_name => $attribute_name ) { 192 if ( array_key_exists( $attribute_name, $this->attributes ) ) { 193 $child_context[ $context_name ] = $this->attributes[ $attribute_name ]; 194 } 195 } 196 } 197 198 $this->inner_blocks = new WP_Block_List( $this->parsed_block['innerBlocks'], $child_context, $this->registry ); 199 } 200 201 if ( ! empty( $this->parsed_block['innerHTML'] ) ) { 202 $this->inner_html = $this->parsed_block['innerHTML']; 203 } 204 205 if ( ! empty( $this->parsed_block['innerContent'] ) ) { 206 $this->inner_content = $this->parsed_block['innerContent']; 207 } 208 } 209 210 /** 211 * Returns a value from an inaccessible property. 212 * 213 * This is used to lazily initialize the `attributes` property of a block, 214 * such that it is only prepared with default attributes at the time that 215 * the property is accessed. For all other inaccessible properties, a `null` 216 * value is returned. 217 * 218 * @since 5.5.0 219 * 220 * @param string $name Property name. 221 * @return array|null Prepared attributes, or null. 222 */ 223 public function __get( $name ) { 224 if ( 'attributes' === $name ) { 225 $this->attributes = isset( $this->parsed_block['attrs'] ) ? 226 $this->parsed_block['attrs'] : 227 array(); 228 229 if ( ! is_null( $this->block_type ) ) { 230 $this->attributes = $this->block_type->prepare_attributes_for_render( $this->attributes ); 231 } 232 233 return $this->attributes; 234 } 235 236 return null; 237 } 238 239 /** 240 * Processes the block bindings and updates the block attributes with the values from the sources. 241 * 242 * A block might contain bindings in its attributes. Bindings are mappings 243 * between an attribute of the block and a source. A "source" is a function 244 * registered with `register_block_bindings_source()` that defines how to 245 * retrieve a value from outside the block, e.g. from post meta. 246 * 247 * This function will process those bindings and update the block's attributes 248 * with the values coming from the bindings. 249 * 250 * ### Example 251 * 252 * The "bindings" property for an Image block might look like this: 253 * 254 * ```json 255 * { 256 * "metadata": { 257 * "bindings": { 258 * "title": { 259 * "source": "core/post-meta", 260 * "args": { "key": "text_custom_field" } 261 * }, 262 * "url": { 263 * "source": "core/post-meta", 264 * "args": { "key": "url_custom_field" } 265 * } 266 * } 267 * } 268 * } 269 * ``` 270 * 271 * The above example will replace the `title` and `url` attributes of the Image 272 * block with the values of the `text_custom_field` and `url_custom_field` post meta. 273 * 274 * @since 6.5.0 275 * @since 6.6.0 Handle the `__default` attribute for pattern overrides. 276 * @since 6.7.0 Return any updated bindings metadata in the computed attributes. 277 * 278 * @return array The computed block attributes for the provided block bindings. 279 */ 280 private function process_block_bindings() { 281 $parsed_block = $this->parsed_block; 282 $computed_attributes = array(); 283 $supported_block_attributes = array( 284 'core/paragraph' => array( 'content' ), 285 'core/heading' => array( 'content' ), 286 'core/image' => array( 'id', 'url', 'title', 'alt' ), 287 'core/button' => array( 'url', 'text', 'linkTarget', 'rel' ), 288 ); 289 290 // If the block doesn't have the bindings property, isn't one of the supported 291 // block types, or the bindings property is not an array, return the block content. 292 if ( 293 ! isset( $supported_block_attributes[ $this->name ] ) || 294 empty( $parsed_block['attrs']['metadata']['bindings'] ) || 295 ! is_array( $parsed_block['attrs']['metadata']['bindings'] ) 296 ) { 297 return $computed_attributes; 298 } 299 300 $bindings = $parsed_block['attrs']['metadata']['bindings']; 301 302 /* 303 * If the default binding is set for pattern overrides, replace it 304 * with a pattern override binding for all supported attributes. 305 */ 306 if ( 307 isset( $bindings['__default']['source'] ) && 308 'core/pattern-overrides' === $bindings['__default']['source'] 309 ) { 310 $updated_bindings = array(); 311 312 /* 313 * Build a binding array of all supported attributes. 314 * Note that this also omits the `__default` attribute from the 315 * resulting array. 316 */ 317 foreach ( $supported_block_attributes[ $parsed_block['blockName'] ] as $attribute_name ) { 318 // Retain any non-pattern override bindings that might be present. 319 $updated_bindings[ $attribute_name ] = isset( $bindings[ $attribute_name ] ) 320 ? $bindings[ $attribute_name ] 321 : array( 'source' => 'core/pattern-overrides' ); 322 } 323 $bindings = $updated_bindings; 324 /* 325 * Update the bindings metadata of the computed attributes. 326 * This ensures the block receives the expanded __default binding metadata when it renders. 327 */ 328 $computed_attributes['metadata'] = array_merge( 329 $parsed_block['attrs']['metadata'], 330 array( 'bindings' => $bindings ) 331 ); 332 } 333 334 foreach ( $bindings as $attribute_name => $block_binding ) { 335 // If the attribute is not in the supported list, process next attribute. 336 if ( ! in_array( $attribute_name, $supported_block_attributes[ $this->name ], true ) ) { 337 continue; 338 } 339 // If no source is provided, or that source is not registered, process next attribute. 340 if ( ! isset( $block_binding['source'] ) || ! is_string( $block_binding['source'] ) ) { 341 continue; 342 } 343 344 $block_binding_source = get_block_bindings_source( $block_binding['source'] ); 345 if ( null === $block_binding_source ) { 346 continue; 347 } 348 349 // Adds the necessary context defined by the source. 350 if ( ! empty( $block_binding_source->uses_context ) ) { 351 foreach ( $block_binding_source->uses_context as $context_name ) { 352 if ( array_key_exists( $context_name, $this->available_context ) ) { 353 $this->context[ $context_name ] = $this->available_context[ $context_name ]; 354 } 355 } 356 } 357 358 $source_args = ! empty( $block_binding['args'] ) && is_array( $block_binding['args'] ) ? $block_binding['args'] : array(); 359 $source_value = $block_binding_source->get_value( $source_args, $this, $attribute_name ); 360 361 // If the value is not null, process the HTML based on the block and the attribute. 362 if ( ! is_null( $source_value ) ) { 363 $computed_attributes[ $attribute_name ] = $source_value; 364 } 365 } 366 367 return $computed_attributes; 368 } 369 370 /** 371 * Depending on the block attribute name, replace its value in the HTML based on the value provided. 372 * 373 * @since 6.5.0 374 * 375 * @param string $block_content Block content. 376 * @param string $attribute_name The attribute name to replace. 377 * @param mixed $source_value The value used to replace in the HTML. 378 * @return string The modified block content. 379 */ 380 private function replace_html( string $block_content, string $attribute_name, $source_value ) { 381 $block_type = $this->block_type; 382 if ( ! isset( $block_type->attributes[ $attribute_name ]['source'] ) ) { 383 return $block_content; 384 } 385 386 // Depending on the attribute source, the processing will be different. 387 switch ( $block_type->attributes[ $attribute_name ]['source'] ) { 388 case 'html': 389 case 'rich-text': 390 $block_reader = new WP_HTML_Tag_Processor( $block_content ); 391 392 // TODO: Support for CSS selectors whenever they are ready in the HTML API. 393 // In the meantime, support comma-separated selectors by exploding them into an array. 394 $selectors = explode( ',', $block_type->attributes[ $attribute_name ]['selector'] ); 395 // Add a bookmark to the first tag to be able to iterate over the selectors. 396 $block_reader->next_tag(); 397 $block_reader->set_bookmark( 'iterate-selectors' ); 398 399 // TODO: This shouldn't be needed when the `set_inner_html` function is ready. 400 // Store the parent tag and its attributes to be able to restore them later in the button. 401 // The button block has a wrapper while the paragraph and heading blocks don't. 402 if ( 'core/button' === $this->name ) { 403 $button_wrapper = $block_reader->get_tag(); 404 $button_wrapper_attribute_names = $block_reader->get_attribute_names_with_prefix( '' ); 405 $button_wrapper_attrs = array(); 406 foreach ( $button_wrapper_attribute_names as $name ) { 407 $button_wrapper_attrs[ $name ] = $block_reader->get_attribute( $name ); 408 } 409 } 410 411 foreach ( $selectors as $selector ) { 412 // If the parent tag, or any of its children, matches the selector, replace the HTML. 413 if ( strcasecmp( $block_reader->get_tag(), $selector ) === 0 || $block_reader->next_tag( 414 array( 415 'tag_name' => $selector, 416 ) 417 ) ) { 418 $block_reader->release_bookmark( 'iterate-selectors' ); 419 420 // TODO: Use `set_inner_html` method whenever it's ready in the HTML API. 421 // Until then, it is hardcoded for the paragraph, heading, and button blocks. 422 // Store the tag and its attributes to be able to restore them later. 423 $selector_attribute_names = $block_reader->get_attribute_names_with_prefix( '' ); 424 $selector_attrs = array(); 425 foreach ( $selector_attribute_names as $name ) { 426 $selector_attrs[ $name ] = $block_reader->get_attribute( $name ); 427 } 428 $selector_markup = "<$selector>" . wp_kses_post( $source_value ) . "</$selector>"; 429 $amended_content = new WP_HTML_Tag_Processor( $selector_markup ); 430 $amended_content->next_tag(); 431 foreach ( $selector_attrs as $attribute_key => $attribute_value ) { 432 $amended_content->set_attribute( $attribute_key, $attribute_value ); 433 } 434 if ( 'core/paragraph' === $this->name || 'core/heading' === $this->name ) { 435 return $amended_content->get_updated_html(); 436 } 437 if ( 'core/button' === $this->name ) { 438 $button_markup = "<$button_wrapper>{$amended_content->get_updated_html()}</$button_wrapper>"; 439 $amended_button = new WP_HTML_Tag_Processor( $button_markup ); 440 $amended_button->next_tag(); 441 foreach ( $button_wrapper_attrs as $attribute_key => $attribute_value ) { 442 $amended_button->set_attribute( $attribute_key, $attribute_value ); 443 } 444 return $amended_button->get_updated_html(); 445 } 446 } else { 447 $block_reader->seek( 'iterate-selectors' ); 448 } 449 } 450 $block_reader->release_bookmark( 'iterate-selectors' ); 451 return $block_content; 452 453 case 'attribute': 454 $amended_content = new WP_HTML_Tag_Processor( $block_content ); 455 if ( ! $amended_content->next_tag( 456 array( 457 // TODO: build the query from CSS selector. 458 'tag_name' => $block_type->attributes[ $attribute_name ]['selector'], 459 ) 460 ) ) { 461 return $block_content; 462 } 463 $amended_content->set_attribute( $block_type->attributes[ $attribute_name ]['attribute'], $source_value ); 464 return $amended_content->get_updated_html(); 465 466 default: 467 return $block_content; 468 } 469 } 470 471 472 /** 473 * Generates the render output for the block. 474 * 475 * @since 5.5.0 476 * @since 6.5.0 Added block bindings processing. 477 * 478 * @global WP_Post $post Global post object. 479 * 480 * @param array $options { 481 * Optional options object. 482 * 483 * @type bool $dynamic Defaults to 'true'. Optionally set to false to avoid using the block's render_callback. 484 * } 485 * @return string Rendered block output. 486 */ 487 public function render( $options = array() ) { 488 global $post; 489 490 /* 491 * There can be only one root interactive block at a time because the rendered HTML of that block contains 492 * the rendered HTML of all its inner blocks, including any interactive block. 493 */ 494 static $root_interactive_block = null; 495 /** 496 * Filters whether Interactivity API should process directives. 497 * 498 * @since 6.6.0 499 * 500 * @param bool $enabled Whether the directives processing is enabled. 501 */ 502 $interactivity_process_directives_enabled = apply_filters( 'interactivity_process_directives', true ); 503 if ( 504 $interactivity_process_directives_enabled && null === $root_interactive_block && ( 505 ( isset( $this->block_type->supports['interactivity'] ) && true === $this->block_type->supports['interactivity'] ) || 506 ! empty( $this->block_type->supports['interactivity']['interactive'] ) 507 ) 508 ) { 509 $root_interactive_block = $this; 510 } 511 512 $options = wp_parse_args( 513 $options, 514 array( 515 'dynamic' => true, 516 ) 517 ); 518 519 // Process the block bindings and get attributes updated with the values from the sources. 520 $computed_attributes = $this->process_block_bindings(); 521 if ( ! empty( $computed_attributes ) ) { 522 // Merge the computed attributes with the original attributes. 523 $this->attributes = array_merge( $this->attributes, $computed_attributes ); 524 } 525 526 $is_dynamic = $options['dynamic'] && $this->name && null !== $this->block_type && $this->block_type->is_dynamic(); 527 $block_content = ''; 528 529 if ( ! $options['dynamic'] || empty( $this->block_type->skip_inner_blocks ) ) { 530 $index = 0; 531 532 foreach ( $this->inner_content as $chunk ) { 533 if ( is_string( $chunk ) ) { 534 $block_content .= $chunk; 535 } else { 536 $inner_block = $this->inner_blocks[ $index ]; 537 $parent_block = $this; 538 539 /** This filter is documented in wp-includes/blocks.php */ 540 $pre_render = apply_filters( 'pre_render_block', null, $inner_block->parsed_block, $parent_block ); 541 542 if ( ! is_null( $pre_render ) ) { 543 $block_content .= $pre_render; 544 } else { 545 $source_block = $inner_block->parsed_block; 546 $inner_block_context = $inner_block->context; 547 548 /** This filter is documented in wp-includes/blocks.php */ 549 $inner_block->parsed_block = apply_filters( 'render_block_data', $inner_block->parsed_block, $source_block, $parent_block ); 550 551 /** This filter is documented in wp-includes/blocks.php */ 552 $inner_block->context = apply_filters( 'render_block_context', $inner_block->context, $inner_block->parsed_block, $parent_block ); 553 554 /* 555 * The `refresh_context_dependents()` method already calls `refresh_parsed_block_dependents()`. 556 * Therefore the second condition is irrelevant if the first one is satisfied. 557 */ 558 if ( $inner_block->context !== $inner_block_context ) { 559 $inner_block->refresh_context_dependents(); 560 } elseif ( $inner_block->parsed_block !== $source_block ) { 561 $inner_block->refresh_parsed_block_dependents(); 562 } 563 564 $block_content .= $inner_block->render(); 565 } 566 567 ++$index; 568 } 569 } 570 } 571 572 if ( ! empty( $computed_attributes ) && ! empty( $block_content ) ) { 573 foreach ( $computed_attributes as $attribute_name => $source_value ) { 574 $block_content = $this->replace_html( $block_content, $attribute_name, $source_value ); 575 } 576 } 577 578 if ( $is_dynamic ) { 579 $global_post = $post; 580 $parent = WP_Block_Supports::$block_to_render; 581 582 WP_Block_Supports::$block_to_render = $this->parsed_block; 583 584 $block_content = (string) call_user_func( $this->block_type->render_callback, $this->attributes, $block_content, $this ); 585 586 WP_Block_Supports::$block_to_render = $parent; 587 588 $post = $global_post; 589 } 590 591 if ( ( ! empty( $this->block_type->script_handles ) ) ) { 592 foreach ( $this->block_type->script_handles as $script_handle ) { 593 wp_enqueue_script( $script_handle ); 594 } 595 } 596 597 if ( ! empty( $this->block_type->view_script_handles ) ) { 598 foreach ( $this->block_type->view_script_handles as $view_script_handle ) { 599 wp_enqueue_script( $view_script_handle ); 600 } 601 } 602 603 if ( ! empty( $this->block_type->view_script_module_ids ) ) { 604 foreach ( $this->block_type->view_script_module_ids as $view_script_module_id ) { 605 wp_enqueue_script_module( $view_script_module_id ); 606 } 607 } 608 609 /* 610 * For Core blocks, these styles are only enqueued if `wp_should_load_separate_core_block_assets()` returns 611 * true. Otherwise these `wp_enqueue_style()` calls will not have any effect, as the Core blocks are relying on 612 * the combined 'wp-block-library' stylesheet instead, which is unconditionally enqueued. 613 */ 614 if ( ( ! empty( $this->block_type->style_handles ) ) ) { 615 foreach ( $this->block_type->style_handles as $style_handle ) { 616 wp_enqueue_style( $style_handle ); 617 } 618 } 619 620 if ( ( ! empty( $this->block_type->view_style_handles ) ) ) { 621 foreach ( $this->block_type->view_style_handles as $view_style_handle ) { 622 wp_enqueue_style( $view_style_handle ); 623 } 624 } 625 626 /** 627 * Filters the content of a single block. 628 * 629 * @since 5.0.0 630 * @since 5.9.0 The `$instance` parameter was added. 631 * 632 * @param string $block_content The block content. 633 * @param array $block The full block, including name and attributes. 634 * @param WP_Block $instance The block instance. 635 */ 636 $block_content = apply_filters( 'render_block', $block_content, $this->parsed_block, $this ); 637 638 /** 639 * Filters the content of a single block. 640 * 641 * The dynamic portion of the hook name, `$name`, refers to 642 * the block name, e.g. "core/paragraph". 643 * 644 * @since 5.7.0 645 * @since 5.9.0 The `$instance` parameter was added. 646 * 647 * @param string $block_content The block content. 648 * @param array $block The full block, including name and attributes. 649 * @param WP_Block $instance The block instance. 650 */ 651 $block_content = apply_filters( "render_block_{$this->name}", $block_content, $this->parsed_block, $this ); 652 653 if ( $root_interactive_block === $this ) { 654 // The root interactive block has finished rendering. Time to process directives. 655 $block_content = wp_interactivity_process_directives( $block_content ); 656 $root_interactive_block = null; 657 } 658 659 return $block_content; 660 } 661 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Fri Jun 27 08:20:01 2025 | Cross-referenced by PHPXref |