[ 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 Array of parsed block properties. 117 * @param array $available_context Optional array of ancestry context values. 118 * @param WP_Block_Type_Registry $registry Optional block type registry. 119 */ 120 public function __construct( $block, $available_context = array(), $registry = null ) { 121 $this->parsed_block = $block; 122 $this->name = $block['blockName']; 123 124 if ( is_null( $registry ) ) { 125 $registry = WP_Block_Type_Registry::get_instance(); 126 } 127 128 $this->registry = $registry; 129 130 $this->block_type = $registry->get_registered( $this->name ); 131 132 $this->available_context = $available_context; 133 134 if ( ! empty( $this->block_type->uses_context ) ) { 135 foreach ( $this->block_type->uses_context as $context_name ) { 136 if ( array_key_exists( $context_name, $this->available_context ) ) { 137 $this->context[ $context_name ] = $this->available_context[ $context_name ]; 138 } 139 } 140 } 141 142 if ( ! empty( $block['innerBlocks'] ) ) { 143 $child_context = $this->available_context; 144 145 if ( ! empty( $this->block_type->provides_context ) ) { 146 foreach ( $this->block_type->provides_context as $context_name => $attribute_name ) { 147 if ( array_key_exists( $attribute_name, $this->attributes ) ) { 148 $child_context[ $context_name ] = $this->attributes[ $attribute_name ]; 149 } 150 } 151 } 152 153 $this->inner_blocks = new WP_Block_List( $block['innerBlocks'], $child_context, $registry ); 154 } 155 156 if ( ! empty( $block['innerHTML'] ) ) { 157 $this->inner_html = $block['innerHTML']; 158 } 159 160 if ( ! empty( $block['innerContent'] ) ) { 161 $this->inner_content = $block['innerContent']; 162 } 163 } 164 165 /** 166 * Returns a value from an inaccessible property. 167 * 168 * This is used to lazily initialize the `attributes` property of a block, 169 * such that it is only prepared with default attributes at the time that 170 * the property is accessed. For all other inaccessible properties, a `null` 171 * value is returned. 172 * 173 * @since 5.5.0 174 * 175 * @param string $name Property name. 176 * @return array|null Prepared attributes, or null. 177 */ 178 public function __get( $name ) { 179 if ( 'attributes' === $name ) { 180 $this->attributes = isset( $this->parsed_block['attrs'] ) ? 181 $this->parsed_block['attrs'] : 182 array(); 183 184 if ( ! is_null( $this->block_type ) ) { 185 $this->attributes = $this->block_type->prepare_attributes_for_render( $this->attributes ); 186 } 187 188 return $this->attributes; 189 } 190 191 return null; 192 } 193 194 /** 195 * Generates the render output for the block. 196 * 197 * @since 5.5.0 198 * 199 * @global WP_Post $post Global post object. 200 * 201 * @param array $options { 202 * Optional options object. 203 * 204 * @type bool $dynamic Defaults to 'true'. Optionally set to false to avoid using the block's render_callback. 205 * } 206 * @return string Rendered block output. 207 */ 208 public function render( $options = array() ) { 209 global $post; 210 $options = wp_parse_args( 211 $options, 212 array( 213 'dynamic' => true, 214 ) 215 ); 216 217 $is_dynamic = $options['dynamic'] && $this->name && null !== $this->block_type && $this->block_type->is_dynamic(); 218 $block_content = ''; 219 220 if ( ! $options['dynamic'] || empty( $this->block_type->skip_inner_blocks ) ) { 221 $index = 0; 222 223 foreach ( $this->inner_content as $chunk ) { 224 if ( is_string( $chunk ) ) { 225 $block_content .= $chunk; 226 } else { 227 $inner_block = $this->inner_blocks[ $index ]; 228 $parent_block = $this; 229 230 /** This filter is documented in wp-includes/blocks.php */ 231 $pre_render = apply_filters( 'pre_render_block', null, $inner_block->parsed_block, $parent_block ); 232 233 if ( ! is_null( $pre_render ) ) { 234 $block_content .= $pre_render; 235 } else { 236 $source_block = $inner_block->parsed_block; 237 238 /** This filter is documented in wp-includes/blocks.php */ 239 $inner_block->parsed_block = apply_filters( 'render_block_data', $inner_block->parsed_block, $source_block, $parent_block ); 240 241 /** This filter is documented in wp-includes/blocks.php */ 242 $inner_block->context = apply_filters( 'render_block_context', $inner_block->context, $inner_block->parsed_block, $parent_block ); 243 244 $block_content .= $inner_block->render(); 245 } 246 247 $index++; 248 } 249 } 250 } 251 252 if ( $is_dynamic ) { 253 $global_post = $post; 254 $parent = WP_Block_Supports::$block_to_render; 255 256 WP_Block_Supports::$block_to_render = $this->parsed_block; 257 258 $block_content = (string) call_user_func( $this->block_type->render_callback, $this->attributes, $block_content, $this ); 259 260 WP_Block_Supports::$block_to_render = $parent; 261 262 $post = $global_post; 263 } 264 265 if ( ( ! empty( $this->block_type->script_handles ) ) ) { 266 foreach ( $this->block_type->script_handles as $script_handle ) { 267 wp_enqueue_script( $script_handle ); 268 } 269 } 270 271 if ( ! empty( $this->block_type->view_script_handles ) ) { 272 foreach ( $this->block_type->view_script_handles as $view_script_handle ) { 273 wp_enqueue_script( $view_script_handle ); 274 } 275 } 276 277 if ( ( ! empty( $this->block_type->style_handles ) ) ) { 278 foreach ( $this->block_type->style_handles as $style_handle ) { 279 wp_enqueue_style( $style_handle ); 280 } 281 } 282 283 /** 284 * Filters the content of a single block. 285 * 286 * @since 5.0.0 287 * @since 5.9.0 The `$instance` parameter was added. 288 * 289 * @param string $block_content The block content. 290 * @param array $block The full block, including name and attributes. 291 * @param WP_Block $instance The block instance. 292 */ 293 $block_content = apply_filters( 'render_block', $block_content, $this->parsed_block, $this ); 294 295 /** 296 * Filters the content of a single block. 297 * 298 * The dynamic portion of the hook name, `$name`, refers to 299 * the block name, e.g. "core/paragraph". 300 * 301 * @since 5.7.0 302 * @since 5.9.0 The `$instance` parameter was added. 303 * 304 * @param string $block_content The block content. 305 * @param array $block The full block, including name and attributes. 306 * @param WP_Block $instance The block instance. 307 */ 308 $block_content = apply_filters( "render_block_{$this->name}", $block_content, $this->parsed_block, $this ); 309 310 return $block_content; 311 } 312 313 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Sun Jun 4 08:20:02 2023 | Cross-referenced by PHPXref |