PluginProbe
Gutenberg / 8.2.0
Gutenberg v8.2.0
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / lib / class-wp-block.php

class-wp-block.php in Gutenberg 8.2.0, at lib/class-wp-block.php

214 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Blocks API: WP_Block class
4 *
5 * @package Gutenberg
6 */
7
8 /**
9 * Class representing a parsed instance of a block.
10 *
11 * @property array $attributes
12 */
13 class WP_Block {
14
15 /**
16 * Original parsed array representation of block.
17 *
18 * @var array
19 */
20 public $parsed_block;
21
22 /**
23 * Name of block.
24 *
25 * @example "core/paragraph"
26 *
27 * @var string
28 */
29 public $name;
30
31 /**
32 * Block type associated with the instance.
33 *
34 * @var WP_Block_Type
35 */
36 public $block_type;
37
38 /**
39 * Block context values.
40 *
41 * @var array
42 */
43 public $context = array();
44
45 /**
46 * All available context of the current hierarchy.
47 *
48 * @var array
49 * @access protected
50 */
51 protected $available_context;
52
53 /**
54 * List of inner blocks (of this same class)
55 *
56 * @var WP_Block[]
57 */
58 public $inner_blocks = array();
59
60 /**
61 * Resultant HTML from inside block comment delimiters after removing inner
62 * blocks.
63 *
64 * @example "...Just <!-- wp:test /--> testing..." -> "Just testing..."
65 *
66 * @var string
67 */
68 public $inner_html = '';
69
70 /**
71 * List of string fragments and null markers where inner blocks were found
72 *
73 * @example array(
74 * 'inner_html' => 'BeforeInnerAfter',
75 * 'inner_blocks' => array( block, block ),
76 * 'inner_content' => array( 'Before', null, 'Inner', null, 'After' ),
77 * )
78 *
79 * @var array
80 */
81 public $inner_content = array();
82
83 /**
84 * Constructor.
85 *
86 * Populates object properties from the provided block instance argument.
87 *
88 * The given array of context values will not necessarily be available on
89 * the instance itself, but is treated as the full set of values provided by
90 * the block's ancestry. This is assigned to the private `available_context`
91 * property. Only values which are configured to consumed by the block via
92 * its registered type will be assigned to the block's `context` property.
93 *
94 * @param array $block Array of parsed block properties.
95 * @param array $available_context Optional array of ancestry context values.
96 * @param WP_Block_Type_Registry $registry Optional block type registry.
97 */
98 public function __construct( $block, $available_context = array(), $registry = null ) {
99 $this->parsed_block = $block;
100 $this->name = $block['blockName'];
101
102 if ( is_null( $registry ) ) {
103 $registry = WP_Block_Type_Registry::get_instance();
104 }
105
106 $this->block_type = $registry->get_registered( $this->name );
107
108 $this->available_context = $available_context;
109
110 if ( ! empty( $this->block_type->context ) ) {
111 foreach ( $this->block_type->context as $context_name ) {
112 if ( array_key_exists( $context_name, $this->available_context ) ) {
113 $this->context[ $context_name ] = $this->available_context[ $context_name ];
114 }
115 }
116 }
117
118 if ( ! empty( $block['innerBlocks'] ) ) {
119 $child_context = $this->available_context;
120
121 /* phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase */
122 if ( ! empty( $this->block_type->providesContext ) ) {
123 foreach ( $this->block_type->providesContext as $context_name => $attribute_name ) {
124 if ( array_key_exists( $attribute_name, $this->attributes ) ) {
125 $child_context[ $context_name ] = $this->attributes[ $attribute_name ];
126 }
127 }
128 }
129 /* phpcs:enable */
130
131 $this->inner_blocks = new WP_Block_List( $block['innerBlocks'], $child_context, $registry );
132 }
133
134 if ( ! empty( $block['innerHTML'] ) ) {
135 $this->inner_html = $block['innerHTML'];
136 }
137
138 if ( ! empty( $block['innerContent'] ) ) {
139 $this->inner_content = $block['innerContent'];
140 }
141 }
142
143 /**
144 * Returns a value from an inaccessible property.
145 *
146 * This is used to lazily initialize the `attributes` property of a block,
147 * such that it is only prepared with default attributes at the time that
148 * the property is accessed. For all other inaccessible properties, a `null`
149 * value is returned.
150 *
151 * @param string $name Property name.
152 *
153 * @return array|null Prepared attributes, or null.
154 */
155 public function __get( $name ) {
156 if ( 'attributes' === $name ) {
157 $this->attributes = isset( $this->parsed_block['attrs'] ) ?
158 $this->parsed_block['attrs'] :
159 array();
160
161 if ( ! is_null( $this->block_type ) ) {
162 $this->attributes = $this->block_type->prepare_attributes_for_render( $this->attributes );
163 }
164
165 return $this->attributes;
166 }
167
168 return null;
169 }
170
171 /**
172 * Generates the render output for the block.
173 *
174 * @param array $options {
175 * Optional options object.
176 *
177 * @type bool $dynamic Defaults to 'true'. Optionally set to false to avoid using the block's render_callback.
178 * }
179 *
180 * @return string Rendered block output.
181 */
182 public function render( $options = array() ) {
183 global $post;
184 $options = array_replace(
185 array(
186 'dynamic' => true,
187 ),
188 $options
189 );
190
191 $is_dynamic = $options['dynamic'] && $this->name && null !== $this->block_type && $this->block_type->is_dynamic();
192 $block_content = '';
193
194 if ( ! $options['dynamic'] || empty( $this->block_type->skip_inner_blocks ) ) {
195 $index = 0;
196 foreach ( $this->inner_content as $chunk ) {
197 $block_content .= is_string( $chunk ) ?
198 $chunk :
199 $this->inner_blocks[ $index++ ]->render();
200 }
201 }
202
203 if ( $is_dynamic ) {
204 $global_post = $post;
205 $block_content = (string) call_user_func( $this->block_type->render_callback, $this->attributes, $block_content, $this );
206 $post = $global_post;
207 }
208
209 /** This filter is documented in src/wp-includes/blocks.php */
210 return apply_filters( 'render_block', $block_content, $this->parsed_block );
211 }
212
213 }
214