| [ 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|null 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|null $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 = $this->parsed_block['attrs'] ?? 226 array(); 227 228 if ( ! is_null( $this->block_type ) ) { 229 $this->attributes = $this->block_type->prepare_attributes_for_render( $this->attributes ); 230 } 231 232 return $this->attributes; 233 } 234 235 return null; 236 } 237 238 /** 239 * Processes the block bindings and updates the block attributes with the values from the sources. 240 * 241 * A block might contain bindings in its attributes. Bindings are mappings 242 * between an attribute of the block and a source. A "source" is a function 243 * registered with `register_block_bindings_source()` that defines how to 244 * retrieve a value from outside the block, e.g. from post meta. 245 * 246 * This function will process those bindings and update the block's attributes 247 * with the values coming from the bindings. 248 * 249 * ### Example 250 * 251 * The "bindings" property for an Image block might look like this: 252 * 253 * ```json 254 * { 255 * "metadata": { 256 * "bindings": { 257 * "title": { 258 * "source": "core/post-meta", 259 * "args": { "key": "text_custom_field" } 260 * }, 261 * "url": { 262 * "source": "core/post-meta", 263 * "args": { "key": "url_custom_field" } 264 * } 265 * } 266 * } 267 * } 268 * ``` 269 * 270 * The above example will replace the `title` and `url` attributes of the Image 271 * block with the values of the `text_custom_field` and `url_custom_field` post meta. 272 * 273 * @since 6.5.0 274 * @since 6.6.0 Handle the `__default` attribute for pattern overrides. 275 * @since 6.7.0 Return any updated bindings metadata in the computed attributes. 276 * 277 * @return array The computed block attributes for the provided block bindings. 278 */ 279 private function process_block_bindings() { 280 $block_type = $this->name; 281 $parsed_block = $this->parsed_block; 282 $computed_attributes = array(); 283 $supported_block_attributes = get_block_bindings_supported_attributes( $block_type ); 284 285 // If the block doesn't have the bindings property, isn't one of the supported 286 // block types, or the bindings property is not an array, return the block content. 287 if ( 288 empty( $supported_block_attributes ) || 289 empty( $parsed_block['attrs']['metadata']['bindings'] ) || 290 ! is_array( $parsed_block['attrs']['metadata']['bindings'] ) 291 ) { 292 return $computed_attributes; 293 } 294 295 $bindings = $parsed_block['attrs']['metadata']['bindings']; 296 297 /* 298 * If the default binding is set for pattern overrides, replace it 299 * with a pattern override binding for all supported attributes. 300 */ 301 if ( 302 isset( $bindings['__default']['source'] ) && 303 'core/pattern-overrides' === $bindings['__default']['source'] 304 ) { 305 $updated_bindings = array(); 306 307 /* 308 * Build a binding array of all supported attributes. 309 * Note that this also omits the `__default` attribute from the 310 * resulting array. 311 */ 312 foreach ( $supported_block_attributes as $attribute_name ) { 313 // Retain any non-pattern override bindings that might be present. 314 $updated_bindings[ $attribute_name ] = $bindings[ $attribute_name ] ?? array( 'source' => 'core/pattern-overrides' ); 315 } 316 $bindings = $updated_bindings; 317 /* 318 * Update the bindings metadata of the computed attributes. 319 * This ensures the block receives the expanded __default binding metadata when it renders. 320 */ 321 $computed_attributes['metadata'] = array_merge( 322 $parsed_block['attrs']['metadata'], 323 array( 'bindings' => $bindings ) 324 ); 325 } 326 327 foreach ( $bindings as $attribute_name => $block_binding ) { 328 // If the attribute is not in the supported list, process next attribute. 329 if ( ! in_array( $attribute_name, $supported_block_attributes, true ) ) { 330 continue; 331 } 332 // If no source is provided, or that source is not registered, process next attribute. 333 if ( ! isset( $block_binding['source'] ) || ! is_string( $block_binding['source'] ) ) { 334 continue; 335 } 336 337 $block_binding_source = get_block_bindings_source( $block_binding['source'] ); 338 if ( null === $block_binding_source ) { 339 continue; 340 } 341 342 // Adds the necessary context defined by the source. 343 if ( ! empty( $block_binding_source->uses_context ) ) { 344 foreach ( $block_binding_source->uses_context as $context_name ) { 345 if ( array_key_exists( $context_name, $this->available_context ) ) { 346 $this->context[ $context_name ] = $this->available_context[ $context_name ]; 347 } 348 } 349 } 350 351 $source_args = ! empty( $block_binding['args'] ) && is_array( $block_binding['args'] ) ? $block_binding['args'] : array(); 352 $source_value = $block_binding_source->get_value( $source_args, $this, $attribute_name ); 353 354 // If the value is not null, process the HTML based on the block and the attribute. 355 if ( ! is_null( $source_value ) ) { 356 $computed_attributes[ $attribute_name ] = $source_value; 357 } 358 } 359 360 return $computed_attributes; 361 } 362 363 /** 364 * Depending on the block attribute name, replace its value in the HTML based on the value provided. 365 * 366 * @since 6.5.0 367 * @since 7.1.0 Added the optional `$inner_block_offsets` parameter. 368 * 369 * @param string $block_content Block content. 370 * @param string $attribute_name The attribute name to replace. 371 * @param mixed $source_value The value used to replace in the HTML. 372 * @param int[] $inner_block_offsets Optional. Byte offsets where each inner block's 373 * rendered output begins. Default empty array. 374 * @return string The modified block content. 375 */ 376 private function replace_html( string $block_content, string $attribute_name, $source_value, array $inner_block_offsets = array() ) { 377 $block_type = $this->block_type; 378 if ( ! isset( $block_type->attributes[ $attribute_name ]['source'] ) ) { 379 return $block_content; 380 } 381 382 // Depending on the attribute source, the processing will be different. 383 switch ( $block_type->attributes[ $attribute_name ]['source'] ) { 384 case 'html': 385 case 'rich-text': 386 $block_reader = self::get_block_bindings_processor( $block_content ); 387 388 // TODO: Support for CSS selectors whenever they are ready in the HTML API. 389 // In the meantime, support comma-separated selectors by exploding them into an array. 390 $selectors = explode( ',', $block_type->attributes[ $attribute_name ]['selector'] ); 391 // Add a bookmark to the first tag to be able to iterate over the selectors. 392 $block_reader->next_tag(); 393 $block_reader->set_bookmark( 'iterate-selectors' ); 394 395 foreach ( $selectors as $selector ) { 396 // If the parent tag, or any of its children, matches the selector, replace the HTML. 397 if ( strcasecmp( $block_reader->get_tag(), $selector ) === 0 || $block_reader->next_tag( 398 array( 399 'tag_name' => $selector, 400 ) 401 ) ) { 402 /* 403 * TODO: Use `WP_HTML_Processor::set_inner_html()` once it's available. 404 * Any replacement must preserve already-rendered inner block 405 * markup verbatim (it may come from dynamic blocks), so it 406 * cannot re-serialize the element's contents. Until an API with 407 * that guarantee exists, the replacement is spliced by byte 408 * offset, leaving inner block output untouched. 409 */ 410 $block_reader->release_bookmark( 'iterate-selectors' ); 411 $block_reader->replace_rich_text( wp_kses_post( $source_value ), $inner_block_offsets ); 412 return $block_reader->get_updated_html(); 413 } else { 414 $block_reader->seek( 'iterate-selectors' ); 415 } 416 } 417 $block_reader->release_bookmark( 'iterate-selectors' ); 418 return $block_content; 419 420 case 'attribute': 421 $amended_content = new WP_HTML_Tag_Processor( $block_content ); 422 if ( ! $amended_content->next_tag( 423 array( 424 // TODO: build the query from CSS selector. 425 'tag_name' => $block_type->attributes[ $attribute_name ]['selector'], 426 ) 427 ) ) { 428 return $block_content; 429 } 430 $amended_content->set_attribute( $block_type->attributes[ $attribute_name ]['attribute'], $source_value ); 431 return $amended_content->get_updated_html(); 432 433 default: 434 return $block_content; 435 } 436 } 437 438 private static function get_block_bindings_processor( string $block_content ) { 439 $internal_processor_class = new class('', WP_HTML_Processor::CONSTRUCTOR_UNLOCK_CODE) extends WP_HTML_Processor { 440 /** 441 * Replace the rich text content between a tag opener and matching closer. 442 * 443 * When stopped on a tag opener, replace the content enclosed by it and its 444 * matching closer with the provided rich text. 445 * 446 * If byte offsets of inner blocks' rendered output are provided, the 447 * replacement stops at the first inner block found inside the element, 448 * preserving any markup produced by nested inner blocks (e.g. a List 449 * block nested inside a List Item). 450 * 451 * @param string $rich_text The rich text to replace the original content with. 452 * @param int[] $inner_block_offsets Optional. Byte offsets in the source HTML where 453 * inner blocks' rendered output begins. Default empty array. 454 * @return bool True on success. 455 */ 456 public function replace_rich_text( $rich_text, $inner_block_offsets = array() ) { 457 if ( $this->is_tag_closer() || ! $this->expects_closer() ) { 458 return false; 459 } 460 461 $depth = $this->get_current_depth(); 462 $tag_name = $this->get_tag(); 463 464 $this->set_bookmark( '_wp_block_bindings' ); 465 // The bookmark names are prefixed with `_` so the key below has an extra `_`. 466 $tag_opener = $this->bookmarks['__wp_block_bindings']; 467 $start = $tag_opener->start + $tag_opener->length; 468 469 // Find matching tag closer. 470 while ( $this->next_token() && $this->get_current_depth() >= $depth ) { 471 } 472 473 if ( ! $this->is_tag_closer() || $tag_name !== $this->get_tag() ) { 474 return false; 475 } 476 477 $this->set_bookmark( '_wp_block_bindings' ); 478 $tag_closer = $this->bookmarks['__wp_block_bindings']; 479 $end = $tag_closer->start; 480 481 /* 482 * Stop at the first inner block that renders inside this element so 483 * its markup is preserved. The block's own rich text always precedes 484 * its inner blocks, so replacing up to the first inner block offset 485 * replaces only that rich text. Offsets are recorded during render in 486 * the same byte coordinates as this fragment, and are in ascending 487 * order, so the first match is the earliest inner block. 488 * 489 * The lower bound is inclusive of `$start`: when an inner block 490 * begins immediately, with no leading rich text, the (empty) rich 491 * text is still replaced instead of the inner block markup. 492 */ 493 foreach ( $inner_block_offsets as $inner_block_offset ) { 494 if ( $inner_block_offset >= $start && $inner_block_offset < $end ) { 495 $end = $inner_block_offset; 496 break; 497 } 498 } 499 500 $this->lexical_updates[] = new WP_HTML_Text_Replacement( 501 $start, 502 $end - $start, 503 $rich_text 504 ); 505 506 return true; 507 } 508 }; 509 510 return $internal_processor_class::create_fragment( $block_content ); 511 } 512 513 /** 514 * Generates the render output for the block. 515 * 516 * @since 5.5.0 517 * @since 6.5.0 Added block bindings processing. 518 * @since 7.1.0 Preserve inner blocks when binding a rich text attribute. 519 * 520 * @global WP_Post $post Global post object. 521 * 522 * @param array $options { 523 * Optional options object. 524 * 525 * @type bool $dynamic Defaults to 'true'. Optionally set to false to avoid using the block's render_callback. 526 * } 527 * @return string Rendered block output. 528 */ 529 public function render( $options = array() ) { 530 global $post; 531 532 $before_wp_enqueue_scripts_count = did_action( 'wp_enqueue_scripts' ); 533 534 // Capture the current assets queues. 535 $before_styles_queue = wp_styles()->queue; 536 $before_scripts_queue = wp_scripts()->queue; 537 $before_script_modules_queue = wp_script_modules()->get_queue(); 538 539 /* 540 * There can be only one root interactive block at a time because the rendered HTML of that block contains 541 * the rendered HTML of all its inner blocks, including any interactive block. 542 */ 543 static $root_interactive_block = null; 544 /** 545 * Filters whether Interactivity API should process directives. 546 * 547 * @since 6.6.0 548 * 549 * @param bool $enabled Whether the directives processing is enabled. 550 */ 551 $interactivity_process_directives_enabled = apply_filters( 'interactivity_process_directives', true ); 552 if ( 553 $interactivity_process_directives_enabled && null === $root_interactive_block && ( 554 ( isset( $this->block_type->supports['interactivity'] ) && true === $this->block_type->supports['interactivity'] ) || 555 ! empty( $this->block_type->supports['interactivity']['interactive'] ) 556 ) 557 ) { 558 $root_interactive_block = $this; 559 } 560 561 $options = wp_parse_args( 562 $options, 563 array( 564 'dynamic' => true, 565 ) 566 ); 567 568 // Process the block bindings and get attributes updated with the values from the sources. 569 $computed_attributes = $this->process_block_bindings(); 570 if ( ! empty( $computed_attributes ) ) { 571 // Merge the computed attributes with the original attributes. 572 $this->attributes = array_merge( $this->attributes, $computed_attributes ); 573 } 574 575 $is_dynamic = $options['dynamic'] && $this->name && null !== $this->block_type && $this->block_type->is_dynamic(); 576 $block_content = ''; 577 578 /* 579 * Byte offsets in $block_content where each inner block's rendered output 580 * begins. Block bindings rich-text replacement uses these to stop at the 581 * first inner block inside a selector, so it replaces only the block's own 582 * rich text and never the markup produced by nested inner blocks. 583 * 584 * They are only collected when the block has bound attributes to resolve; 585 * otherwise they are never read, so recording them would add work to every 586 * block render for no benefit. 587 */ 588 $inner_block_offsets = array(); 589 $collect_offsets = ! empty( $computed_attributes ); 590 591 if ( ! $options['dynamic'] || empty( $this->block_type->skip_inner_blocks ) ) { 592 $index = 0; 593 594 foreach ( $this->inner_content as $chunk ) { 595 if ( is_string( $chunk ) ) { 596 $block_content .= $chunk; 597 } else { 598 if ( $collect_offsets ) { 599 $inner_block_offsets[] = strlen( $block_content ); 600 } 601 $inner_block = $this->inner_blocks[ $index ]; 602 $parent_block = $this; 603 604 /** This filter is documented in wp-includes/blocks.php */ 605 $pre_render = apply_filters( 'pre_render_block', null, $inner_block->parsed_block, $parent_block ); 606 607 if ( ! is_null( $pre_render ) ) { 608 $block_content .= $pre_render; 609 } else { 610 $source_block = $inner_block->parsed_block; 611 $inner_block_context = $inner_block->context; 612 613 /** This filter is documented in wp-includes/blocks.php */ 614 $inner_block->parsed_block = apply_filters( 'render_block_data', $inner_block->parsed_block, $source_block, $parent_block ); 615 616 /** This filter is documented in wp-includes/blocks.php */ 617 $inner_block->context = apply_filters( 'render_block_context', $inner_block->context, $inner_block->parsed_block, $parent_block ); 618 619 /* 620 * The `refresh_context_dependents()` method already calls `refresh_parsed_block_dependents()`. 621 * Therefore the second condition is irrelevant if the first one is satisfied. 622 */ 623 if ( $inner_block->context !== $inner_block_context ) { 624 $inner_block->refresh_context_dependents(); 625 } elseif ( $inner_block->parsed_block !== $source_block ) { 626 $inner_block->refresh_parsed_block_dependents(); 627 } 628 629 $block_content .= $inner_block->render(); 630 } 631 632 ++$index; 633 } 634 } 635 } 636 637 if ( ! empty( $computed_attributes ) && ! empty( $block_content ) ) { 638 foreach ( $computed_attributes as $attribute_name => $source_value ) { 639 $updated_block_content = $this->replace_html( $block_content, $attribute_name, $source_value, $inner_block_offsets ); 640 641 /* 642 * The offsets describe $block_content as it was assembled. A 643 * replacement that modifies the markup shifts byte positions, so 644 * once the content changes the remaining attributes fall back to 645 * offset-free replacement rather than clamp at a stale position. 646 * Attributes that leave the markup untouched keep the offsets 647 * valid: the computed `metadata` attribute produced by a pattern 648 * overrides `__default` binding has no HTML source, so it must 649 * not invalidate the offsets for the rich text that follows it. 650 */ 651 if ( $updated_block_content !== $block_content ) { 652 $inner_block_offsets = array(); 653 } 654 655 $block_content = $updated_block_content; 656 } 657 } 658 659 if ( $is_dynamic ) { 660 $global_post = $post; 661 $parent = WP_Block_Supports::$block_to_render; 662 663 WP_Block_Supports::$block_to_render = $this->parsed_block; 664 665 $block_content = (string) call_user_func( $this->block_type->render_callback, $this->attributes, $block_content, $this ); 666 667 WP_Block_Supports::$block_to_render = $parent; 668 669 $post = $global_post; 670 } 671 672 if ( ( ! empty( $this->block_type->script_handles ) ) ) { 673 foreach ( $this->block_type->script_handles as $script_handle ) { 674 wp_enqueue_script( $script_handle ); 675 } 676 } 677 678 if ( ! empty( $this->block_type->view_script_handles ) ) { 679 foreach ( $this->block_type->view_script_handles as $view_script_handle ) { 680 wp_enqueue_script( $view_script_handle ); 681 } 682 } 683 684 if ( ! empty( $this->block_type->view_script_module_ids ) ) { 685 foreach ( $this->block_type->view_script_module_ids as $view_script_module_id ) { 686 wp_enqueue_script_module( $view_script_module_id ); 687 } 688 } 689 690 /* 691 * For Core blocks, these styles are only enqueued if `wp_should_load_separate_core_block_assets()` returns 692 * true. Otherwise these `wp_enqueue_style()` calls will not have any effect, as the Core blocks are relying on 693 * the combined 'wp-block-library' stylesheet instead, which is unconditionally enqueued. 694 */ 695 if ( ( ! empty( $this->block_type->style_handles ) ) ) { 696 foreach ( $this->block_type->style_handles as $style_handle ) { 697 wp_enqueue_style( $style_handle ); 698 } 699 } 700 701 if ( ( ! empty( $this->block_type->view_style_handles ) ) ) { 702 foreach ( $this->block_type->view_style_handles as $view_style_handle ) { 703 wp_enqueue_style( $view_style_handle ); 704 } 705 } 706 707 /** 708 * Filters the content of a single block. 709 * 710 * @since 5.0.0 711 * @since 5.9.0 The `$instance` parameter was added. 712 * 713 * @param string $block_content The block content. 714 * @param array $block The full block, including name and attributes. 715 * @param WP_Block $instance The block instance. 716 */ 717 $block_content = apply_filters( 'render_block', $block_content, $this->parsed_block, $this ); 718 719 /** 720 * Filters the content of a single block. 721 * 722 * The dynamic portion of the hook name, `$name`, refers to 723 * the block name, e.g. "core/paragraph". 724 * 725 * @since 5.7.0 726 * @since 5.9.0 The `$instance` parameter was added. 727 * 728 * @param string $block_content The block content. 729 * @param array $block The full block, including name and attributes. 730 * @param WP_Block $instance The block instance. 731 */ 732 $block_content = apply_filters( "render_block_{$this->name}", $block_content, $this->parsed_block, $this ); 733 734 if ( $root_interactive_block === $this ) { 735 // The root interactive block has finished rendering. Time to process directives. 736 $block_content = wp_interactivity_process_directives( $block_content ); 737 $root_interactive_block = null; 738 } 739 740 // Capture the new assets enqueued during rendering, and restore the queues the state prior to rendering. 741 $after_styles_queue = wp_styles()->queue; 742 $after_scripts_queue = wp_scripts()->queue; 743 $after_script_modules_queue = wp_script_modules()->get_queue(); 744 745 /* 746 * As a very special case, a dynamic block may in fact include a call to wp_head() (and thus wp_enqueue_scripts()), 747 * in which all of its enqueued assets are targeting wp_footer. In this case, nothing would be printed, but this 748 * shouldn't indicate that the just-enqueued assets should be dequeued due to it being an empty block. 749 */ 750 $just_did_wp_enqueue_scripts = ( did_action( 'wp_enqueue_scripts' ) !== $before_wp_enqueue_scripts_count ); 751 752 $has_new_styles = ( $before_styles_queue !== $after_styles_queue ); 753 $has_new_scripts = ( $before_scripts_queue !== $after_scripts_queue ); 754 $has_new_script_modules = ( $before_script_modules_queue !== $after_script_modules_queue ); 755 756 // Dequeue the newly enqueued assets with the existing assets if the rendered block was empty & wp_enqueue_scripts did not fire. 757 if ( 758 ! $just_did_wp_enqueue_scripts && 759 ( $has_new_styles || $has_new_scripts || $has_new_script_modules ) && 760 ( 761 trim( $block_content ) === '' && 762 /** 763 * Filters whether to enqueue assets for a block which has no rendered content. 764 * 765 * @since 6.9.0 766 * 767 * @param bool $enqueue Whether to enqueue assets. 768 * @param string $block_name Block name. 769 */ 770 ! (bool) apply_filters( 'enqueue_empty_block_content_assets', false, $this->name ) 771 ) 772 ) { 773 foreach ( array_diff( $after_styles_queue, $before_styles_queue ) as $handle ) { 774 wp_dequeue_style( $handle ); 775 } 776 foreach ( array_diff( $after_scripts_queue, $before_scripts_queue ) as $handle ) { 777 wp_dequeue_script( $handle ); 778 } 779 foreach ( array_diff( $after_script_modules_queue, $before_script_modules_queue ) as $handle ) { 780 wp_dequeue_script_module( $handle ); 781 } 782 } 783 784 return $block_content; 785 } 786 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Sun Jul 5 08:20:13 2026 | Cross-referenced by PHPXref |