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