[ Index ] |
PHP Cross Reference of WordPress |
[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 class WP_Block { 16 17 /** 18 * Original parsed array representation of block. 19 * 20 * @since 5.5.0 21 * @var array 22 */ 23 public $parsed_block; 24 25 /** 26 * Name of block. 27 * 28 * @example "core/paragraph" 29 * 30 * @since 5.5.0 31 * @var string 32 */ 33 public $name; 34 35 /** 36 * Block type associated with the instance. 37 * 38 * @since 5.5.0 39 * @var WP_Block_Type 40 */ 41 public $block_type; 42 43 /** 44 * Block context values. 45 * 46 * @since 5.5.0 47 * @var array 48 */ 49 public $context = array(); 50 51 /** 52 * All available context of the current hierarchy. 53 * 54 * @since 5.5.0 55 * @var array 56 * @access protected 57 */ 58 protected $available_context; 59 60 /** 61 * Block type registry. 62 * 63 * @since 5.9.0 64 * @var WP_Block_Type_Registry 65 * @access protected 66 */ 67 protected $registry; 68 69 /** 70 * List of inner blocks (of this same class) 71 * 72 * @since 5.5.0 73 * @var WP_Block_List 74 */ 75 public $inner_blocks = array(); 76 77 /** 78 * Resultant HTML from inside block comment delimiters after removing inner 79 * blocks. 80 * 81 * @example "...Just <!-- wp:test /--> testing..." -> "Just testing..." 82 * 83 * @since 5.5.0 84 * @var string 85 */ 86 public $inner_html = ''; 87 88 /** 89 * List of string fragments and null markers where inner blocks were found 90 * 91 * @example array( 92 * 'inner_html' => 'BeforeInnerAfter', 93 * 'inner_blocks' => array( block, block ), 94 * 'inner_content' => array( 'Before', null, 'Inner', null, 'After' ), 95 * ) 96 * 97 * @since 5.5.0 98 * @var array 99 */ 100 public $inner_content = array(); 101 102 /** 103 * Constructor. 104 * 105 * Populates object properties from the provided block instance argument. 106 * 107 * The given array of context values will not necessarily be available on 108 * the instance itself, but is treated as the full set of values provided by 109 * the block's ancestry. This is assigned to the private `available_context` 110 * property. Only values which are configured to consumed by the block via 111 * its registered type will be assigned to the block's `context` property. 112 * 113 * @since 5.5.0 114 * 115 * @param array $block Array of parsed block properties. 116 * @param array $available_context Optional array of ancestry context values. 117 * @param WP_Block_Type_Registry $registry Optional block type registry. 118 */ 119 public function __construct( $block, $available_context = array(), $registry = null ) { 120 $this->parsed_block = $block; 121 $this->name = $block['blockName']; 122 123 if ( is_null( $registry ) ) { 124 $registry = WP_Block_Type_Registry::get_instance(); 125 } 126 127 $this->registry = $registry; 128 129 $this->block_type = $registry->get_registered( $this->name ); 130 131 $this->available_context = $available_context; 132 133 if ( ! empty( $this->block_type->uses_context ) ) { 134 foreach ( $this->block_type->uses_context as $context_name ) { 135 if ( array_key_exists( $context_name, $this->available_context ) ) { 136 $this->context[ $context_name ] = $this->available_context[ $context_name ]; 137 } 138 } 139 } 140 141 if ( ! empty( $block['innerBlocks'] ) ) { 142 $child_context = $this->available_context; 143 144 if ( ! empty( $this->block_type->provides_context ) ) { 145 foreach ( $this->block_type->provides_context as $context_name => $attribute_name ) { 146 if ( array_key_exists( $attribute_name, $this->attributes ) ) { 147 $child_context[ $context_name ] = $this->attributes[ $attribute_name ]; 148 } 149 } 150 } 151 152 $this->inner_blocks = new WP_Block_List( $block['innerBlocks'], $child_context, $registry ); 153 } 154 155 if ( ! empty( $block['innerHTML'] ) ) { 156 $this->inner_html = $block['innerHTML']; 157 } 158 159 if ( ! empty( $block['innerContent'] ) ) { 160 $this->inner_content = $block['innerContent']; 161 } 162 } 163 164 /** 165 * Returns a value from an inaccessible property. 166 * 167 * This is used to lazily initialize the `attributes` property of a block, 168 * such that it is only prepared with default attributes at the time that 169 * the property is accessed. For all other inaccessible properties, a `null` 170 * value is returned. 171 * 172 * @since 5.5.0 173 * 174 * @param string $name Property name. 175 * @return array|null Prepared attributes, or null. 176 */ 177 public function __get( $name ) { 178 if ( 'attributes' === $name ) { 179 $this->attributes = isset( $this->parsed_block['attrs'] ) ? 180 $this->parsed_block['attrs'] : 181 array(); 182 183 if ( ! is_null( $this->block_type ) ) { 184 $this->attributes = $this->block_type->prepare_attributes_for_render( $this->attributes ); 185 } 186 187 return $this->attributes; 188 } 189 190 return null; 191 } 192 193 /** 194 * Generates the render output for the block. 195 * 196 * @since 5.5.0 197 * 198 * @param array $options { 199 * Optional options object. 200 * 201 * @type bool $dynamic Defaults to 'true'. Optionally set to false to avoid using the block's render_callback. 202 * } 203 * @return string Rendered block output. 204 */ 205 public function render( $options = array() ) { 206 global $post; 207 $options = wp_parse_args( 208 $options, 209 array( 210 'dynamic' => true, 211 ) 212 ); 213 214 $is_dynamic = $options['dynamic'] && $this->name && null !== $this->block_type && $this->block_type->is_dynamic(); 215 $block_content = ''; 216 217 if ( ! $options['dynamic'] || empty( $this->block_type->skip_inner_blocks ) ) { 218 $index = 0; 219 220 foreach ( $this->inner_content as $chunk ) { 221 if ( is_string( $chunk ) ) { 222 $block_content .= $chunk; 223 } else { 224 $inner_block = $this->inner_blocks[ $index ]; 225 $parent_block = $this; 226 227 /** This filter is documented in wp-includes/blocks.php */ 228 $pre_render = apply_filters( 'pre_render_block', null, $inner_block->parsed_block, $parent_block ); 229 230 if ( ! is_null( $pre_render ) ) { 231 $block_content .= $pre_render; 232 } else { 233 $source_block = $inner_block->parsed_block; 234 235 /** This filter is documented in wp-includes/blocks.php */ 236 $inner_block->parsed_block = apply_filters( 'render_block_data', $inner_block->parsed_block, $source_block, $parent_block ); 237 238 /** This filter is documented in wp-includes/blocks.php */ 239 $inner_block->context = apply_filters( 'render_block_context', $inner_block->context, $inner_block->parsed_block, $parent_block ); 240 241 $block_content .= $inner_block->render(); 242 } 243 244 $index++; 245 } 246 } 247 } 248 249 if ( $is_dynamic ) { 250 $global_post = $post; 251 $parent = WP_Block_Supports::$block_to_render; 252 253 WP_Block_Supports::$block_to_render = $this->parsed_block; 254 255 $block_content = (string) call_user_func( $this->block_type->render_callback, $this->attributes, $block_content, $this ); 256 257 WP_Block_Supports::$block_to_render = $parent; 258 259 $post = $global_post; 260 } 261 262 if ( ! empty( $this->block_type->script ) ) { 263 wp_enqueue_script( $this->block_type->script ); 264 } 265 266 if ( ! empty( $this->block_type->view_script ) && empty( $this->block_type->render_callback ) ) { 267 wp_enqueue_script( $this->block_type->view_script ); 268 } 269 270 if ( ! empty( $this->block_type->style ) ) { 271 wp_enqueue_style( $this->block_type->style ); 272 } 273 274 /** 275 * Filters the content of a single block. 276 * 277 * @since 5.0.0 278 * @since 5.9.0 The `$instance` parameter was added. 279 * 280 * @param string $block_content The block content about to be appended. 281 * @param array $block The full block, including name and attributes. 282 * @param WP_Block $instance The block instance. 283 */ 284 $block_content = apply_filters( 'render_block', $block_content, $this->parsed_block, $this ); 285 286 /** 287 * Filters the content of a single block. 288 * 289 * The dynamic portion of the hook name, `$name`, refers to 290 * the block name, e.g. "core/paragraph". 291 * 292 * @since 5.7.0 293 * @since 5.9.0 The `$instance` parameter was added. 294 * 295 * @param string $block_content The block content about to be appended. 296 * @param array $block The full block, including name and attributes. 297 * @param WP_Block $instance The block instance. 298 */ 299 $block_content = apply_filters( "render_block_{$this->name}", $block_content, $this->parsed_block, $this ); 300 301 return $block_content; 302 } 303 304 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Nov 21 01:00:03 2024 | Cross-referenced by PHPXref 0.7.1 |