[ 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 * 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 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 * @since 6.6.0 Handle the `__default` attribute for pattern overrides. 240 * @since 6.7.0 Return any updated bindings metadata in the computed attributes. 241 * 242 * @return array The computed block attributes for the provided block bindings. 243 */ 244 private function process_block_bindings() { 245 $parsed_block = $this->parsed_block; 246 $computed_attributes = array(); 247 $supported_block_attributes = array( 248 'core/paragraph' => array( 'content' ), 249 'core/heading' => array( 'content' ), 250 'core/image' => array( 'id', 'url', 'title', 'alt' ), 251 'core/button' => array( 'url', 'text', 'linkTarget', 'rel' ), 252 ); 253 254 // If the block doesn't have the bindings property, isn't one of the supported 255 // block types, or the bindings property is not an array, return the block content. 256 if ( 257 ! isset( $supported_block_attributes[ $this->name ] ) || 258 empty( $parsed_block['attrs']['metadata']['bindings'] ) || 259 ! is_array( $parsed_block['attrs']['metadata']['bindings'] ) 260 ) { 261 return $computed_attributes; 262 } 263 264 $bindings = $parsed_block['attrs']['metadata']['bindings']; 265 266 /* 267 * If the default binding is set for pattern overrides, replace it 268 * with a pattern override binding for all supported attributes. 269 */ 270 if ( 271 isset( $bindings['__default']['source'] ) && 272 'core/pattern-overrides' === $bindings['__default']['source'] 273 ) { 274 $updated_bindings = array(); 275 276 /* 277 * Build a binding array of all supported attributes. 278 * Note that this also omits the `__default` attribute from the 279 * resulting array. 280 */ 281 foreach ( $supported_block_attributes[ $parsed_block['blockName'] ] as $attribute_name ) { 282 // Retain any non-pattern override bindings that might be present. 283 $updated_bindings[ $attribute_name ] = isset( $bindings[ $attribute_name ] ) 284 ? $bindings[ $attribute_name ] 285 : array( 'source' => 'core/pattern-overrides' ); 286 } 287 $bindings = $updated_bindings; 288 /* 289 * Update the bindings metadata of the computed attributes. 290 * This ensures the block receives the expanded __default binding metadata when it renders. 291 */ 292 $computed_attributes['metadata'] = array_merge( 293 $parsed_block['attrs']['metadata'], 294 array( 'bindings' => $bindings ) 295 ); 296 } 297 298 foreach ( $bindings as $attribute_name => $block_binding ) { 299 // If the attribute is not in the supported list, process next attribute. 300 if ( ! in_array( $attribute_name, $supported_block_attributes[ $this->name ], true ) ) { 301 continue; 302 } 303 // If no source is provided, or that source is not registered, process next attribute. 304 if ( ! isset( $block_binding['source'] ) || ! is_string( $block_binding['source'] ) ) { 305 continue; 306 } 307 308 $block_binding_source = get_block_bindings_source( $block_binding['source'] ); 309 if ( null === $block_binding_source ) { 310 continue; 311 } 312 313 // Adds the necessary context defined by the source. 314 if ( ! empty( $block_binding_source->uses_context ) ) { 315 foreach ( $block_binding_source->uses_context as $context_name ) { 316 if ( array_key_exists( $context_name, $this->available_context ) ) { 317 $this->context[ $context_name ] = $this->available_context[ $context_name ]; 318 } 319 } 320 } 321 322 $source_args = ! empty( $block_binding['args'] ) && is_array( $block_binding['args'] ) ? $block_binding['args'] : array(); 323 $source_value = $block_binding_source->get_value( $source_args, $this, $attribute_name ); 324 325 // If the value is not null, process the HTML based on the block and the attribute. 326 if ( ! is_null( $source_value ) ) { 327 $computed_attributes[ $attribute_name ] = $source_value; 328 } 329 } 330 331 return $computed_attributes; 332 } 333 334 /** 335 * Depending on the block attribute name, replace its value in the HTML based on the value provided. 336 * 337 * @since 6.5.0 338 * 339 * @param string $block_content Block content. 340 * @param string $attribute_name The attribute name to replace. 341 * @param mixed $source_value The value used to replace in the HTML. 342 * @return string The modified block content. 343 */ 344 private function replace_html( string $block_content, string $attribute_name, $source_value ) { 345 $block_type = $this->block_type; 346 if ( ! isset( $block_type->attributes[ $attribute_name ]['source'] ) ) { 347 return $block_content; 348 } 349 350 // Depending on the attribute source, the processing will be different. 351 switch ( $block_type->attributes[ $attribute_name ]['source'] ) { 352 case 'html': 353 case 'rich-text': 354 $block_reader = new WP_HTML_Tag_Processor( $block_content ); 355 356 // TODO: Support for CSS selectors whenever they are ready in the HTML API. 357 // In the meantime, support comma-separated selectors by exploding them into an array. 358 $selectors = explode( ',', $block_type->attributes[ $attribute_name ]['selector'] ); 359 // Add a bookmark to the first tag to be able to iterate over the selectors. 360 $block_reader->next_tag(); 361 $block_reader->set_bookmark( 'iterate-selectors' ); 362 363 // TODO: This shouldn't be needed when the `set_inner_html` function is ready. 364 // Store the parent tag and its attributes to be able to restore them later in the button. 365 // The button block has a wrapper while the paragraph and heading blocks don't. 366 if ( 'core/button' === $this->name ) { 367 $button_wrapper = $block_reader->get_tag(); 368 $button_wrapper_attribute_names = $block_reader->get_attribute_names_with_prefix( '' ); 369 $button_wrapper_attrs = array(); 370 foreach ( $button_wrapper_attribute_names as $name ) { 371 $button_wrapper_attrs[ $name ] = $block_reader->get_attribute( $name ); 372 } 373 } 374 375 foreach ( $selectors as $selector ) { 376 // If the parent tag, or any of its children, matches the selector, replace the HTML. 377 if ( strcasecmp( $block_reader->get_tag(), $selector ) === 0 || $block_reader->next_tag( 378 array( 379 'tag_name' => $selector, 380 ) 381 ) ) { 382 $block_reader->release_bookmark( 'iterate-selectors' ); 383 384 // TODO: Use `set_inner_html` method whenever it's ready in the HTML API. 385 // Until then, it is hardcoded for the paragraph, heading, and button blocks. 386 // Store the tag and its attributes to be able to restore them later. 387 $selector_attribute_names = $block_reader->get_attribute_names_with_prefix( '' ); 388 $selector_attrs = array(); 389 foreach ( $selector_attribute_names as $name ) { 390 $selector_attrs[ $name ] = $block_reader->get_attribute( $name ); 391 } 392 $selector_markup = "<$selector>" . wp_kses_post( $source_value ) . "</$selector>"; 393 $amended_content = new WP_HTML_Tag_Processor( $selector_markup ); 394 $amended_content->next_tag(); 395 foreach ( $selector_attrs as $attribute_key => $attribute_value ) { 396 $amended_content->set_attribute( $attribute_key, $attribute_value ); 397 } 398 if ( 'core/paragraph' === $this->name || 'core/heading' === $this->name ) { 399 return $amended_content->get_updated_html(); 400 } 401 if ( 'core/button' === $this->name ) { 402 $button_markup = "<$button_wrapper>{$amended_content->get_updated_html()}</$button_wrapper>"; 403 $amended_button = new WP_HTML_Tag_Processor( $button_markup ); 404 $amended_button->next_tag(); 405 foreach ( $button_wrapper_attrs as $attribute_key => $attribute_value ) { 406 $amended_button->set_attribute( $attribute_key, $attribute_value ); 407 } 408 return $amended_button->get_updated_html(); 409 } 410 } else { 411 $block_reader->seek( 'iterate-selectors' ); 412 } 413 } 414 $block_reader->release_bookmark( 'iterate-selectors' ); 415 return $block_content; 416 417 case 'attribute': 418 $amended_content = new WP_HTML_Tag_Processor( $block_content ); 419 if ( ! $amended_content->next_tag( 420 array( 421 // TODO: build the query from CSS selector. 422 'tag_name' => $block_type->attributes[ $attribute_name ]['selector'], 423 ) 424 ) ) { 425 return $block_content; 426 } 427 $amended_content->set_attribute( $block_type->attributes[ $attribute_name ]['attribute'], $source_value ); 428 return $amended_content->get_updated_html(); 429 430 default: 431 return $block_content; 432 } 433 } 434 435 436 /** 437 * Generates the render output for the block. 438 * 439 * @since 5.5.0 440 * @since 6.5.0 Added block bindings processing. 441 * 442 * @global WP_Post $post Global post object. 443 * 444 * @param array $options { 445 * Optional options object. 446 * 447 * @type bool $dynamic Defaults to 'true'. Optionally set to false to avoid using the block's render_callback. 448 * } 449 * @return string Rendered block output. 450 */ 451 public function render( $options = array() ) { 452 global $post; 453 454 /* 455 * There can be only one root interactive block at a time because the rendered HTML of that block contains 456 * the rendered HTML of all its inner blocks, including any interactive block. 457 */ 458 static $root_interactive_block = null; 459 /** 460 * Filters whether Interactivity API should process directives. 461 * 462 * @since 6.6.0 463 * 464 * @param bool $enabled Whether the directives processing is enabled. 465 */ 466 $interactivity_process_directives_enabled = apply_filters( 'interactivity_process_directives', true ); 467 if ( 468 $interactivity_process_directives_enabled && null === $root_interactive_block && ( 469 ( isset( $this->block_type->supports['interactivity'] ) && true === $this->block_type->supports['interactivity'] ) || 470 ! empty( $this->block_type->supports['interactivity']['interactive'] ) 471 ) 472 ) { 473 $root_interactive_block = $this; 474 } 475 476 $options = wp_parse_args( 477 $options, 478 array( 479 'dynamic' => true, 480 ) 481 ); 482 483 // Process the block bindings and get attributes updated with the values from the sources. 484 $computed_attributes = $this->process_block_bindings(); 485 if ( ! empty( $computed_attributes ) ) { 486 // Merge the computed attributes with the original attributes. 487 $this->attributes = array_merge( $this->attributes, $computed_attributes ); 488 } 489 490 $is_dynamic = $options['dynamic'] && $this->name && null !== $this->block_type && $this->block_type->is_dynamic(); 491 $block_content = ''; 492 493 if ( ! $options['dynamic'] || empty( $this->block_type->skip_inner_blocks ) ) { 494 $index = 0; 495 496 foreach ( $this->inner_content as $chunk ) { 497 if ( is_string( $chunk ) ) { 498 $block_content .= $chunk; 499 } else { 500 $inner_block = $this->inner_blocks[ $index ]; 501 $parent_block = $this; 502 503 /** This filter is documented in wp-includes/blocks.php */ 504 $pre_render = apply_filters( 'pre_render_block', null, $inner_block->parsed_block, $parent_block ); 505 506 if ( ! is_null( $pre_render ) ) { 507 $block_content .= $pre_render; 508 } else { 509 $source_block = $inner_block->parsed_block; 510 511 /** This filter is documented in wp-includes/blocks.php */ 512 $inner_block->parsed_block = apply_filters( 'render_block_data', $inner_block->parsed_block, $source_block, $parent_block ); 513 514 /** This filter is documented in wp-includes/blocks.php */ 515 $inner_block->context = apply_filters( 'render_block_context', $inner_block->context, $inner_block->parsed_block, $parent_block ); 516 517 $block_content .= $inner_block->render(); 518 } 519 520 ++$index; 521 } 522 } 523 } 524 525 if ( ! empty( $computed_attributes ) && ! empty( $block_content ) ) { 526 foreach ( $computed_attributes as $attribute_name => $source_value ) { 527 $block_content = $this->replace_html( $block_content, $attribute_name, $source_value ); 528 } 529 } 530 531 if ( $is_dynamic ) { 532 $global_post = $post; 533 $parent = WP_Block_Supports::$block_to_render; 534 535 WP_Block_Supports::$block_to_render = $this->parsed_block; 536 537 $block_content = (string) call_user_func( $this->block_type->render_callback, $this->attributes, $block_content, $this ); 538 539 WP_Block_Supports::$block_to_render = $parent; 540 541 $post = $global_post; 542 } 543 544 if ( ( ! empty( $this->block_type->script_handles ) ) ) { 545 foreach ( $this->block_type->script_handles as $script_handle ) { 546 wp_enqueue_script( $script_handle ); 547 } 548 } 549 550 if ( ! empty( $this->block_type->view_script_handles ) ) { 551 foreach ( $this->block_type->view_script_handles as $view_script_handle ) { 552 wp_enqueue_script( $view_script_handle ); 553 } 554 } 555 556 if ( ! empty( $this->block_type->view_script_module_ids ) ) { 557 foreach ( $this->block_type->view_script_module_ids as $view_script_module_id ) { 558 wp_enqueue_script_module( $view_script_module_id ); 559 } 560 } 561 562 if ( ( ! empty( $this->block_type->style_handles ) ) ) { 563 foreach ( $this->block_type->style_handles as $style_handle ) { 564 wp_enqueue_style( $style_handle ); 565 } 566 } 567 568 if ( ( ! empty( $this->block_type->view_style_handles ) ) ) { 569 foreach ( $this->block_type->view_style_handles as $view_style_handle ) { 570 wp_enqueue_style( $view_style_handle ); 571 } 572 } 573 574 /** 575 * Filters the content of a single block. 576 * 577 * @since 5.0.0 578 * @since 5.9.0 The `$instance` parameter was added. 579 * 580 * @param string $block_content The block content. 581 * @param array $block The full block, including name and attributes. 582 * @param WP_Block $instance The block instance. 583 */ 584 $block_content = apply_filters( 'render_block', $block_content, $this->parsed_block, $this ); 585 586 /** 587 * Filters the content of a single block. 588 * 589 * The dynamic portion of the hook name, `$name`, refers to 590 * the block name, e.g. "core/paragraph". 591 * 592 * @since 5.7.0 593 * @since 5.9.0 The `$instance` parameter was added. 594 * 595 * @param string $block_content The block content. 596 * @param array $block The full block, including name and attributes. 597 * @param WP_Block $instance The block instance. 598 */ 599 $block_content = apply_filters( "render_block_{$this->name}", $block_content, $this->parsed_block, $this ); 600 601 if ( $root_interactive_block === $this ) { 602 // The root interactive block has finished rendering. Time to process directives. 603 $block_content = wp_interactivity_process_directives( $block_content ); 604 $root_interactive_block = null; 605 } 606 607 return $block_content; 608 } 609 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Sat Dec 21 08:20:01 2024 | Cross-referenced by PHPXref |