PluginProbe
Gutenberg / 4.6.0
Gutenberg v4.6.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-type.php

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

210 lines 4.6 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_Type class
4 *
5 * @package gutenberg
6 * @since 0.6.0
7 */
8
9 /**
10 * Core class representing a block type.
11 *
12 * @since 0.6.0
13 *
14 * @see register_block_type()
15 */
16 class WP_Block_Type {
17 /**
18 * Block type key.
19 *
20 * @since 0.6.0
21 * @var string
22 */
23 public $name;
24
25 /**
26 * Block type render callback.
27 *
28 * @since 0.6.0
29 * @var callable
30 */
31 public $render_callback;
32
33 /**
34 * Block type attributes property schemas.
35 *
36 * @since 0.10.0
37 * @var array
38 */
39 public $attributes;
40
41 /**
42 * Block type editor script handle.
43 *
44 * @since 2.0.0
45 * @var string
46 */
47 public $editor_script;
48
49 /**
50 * Block type front end script handle.
51 *
52 * @since 2.0.0
53 * @var string
54 */
55 public $script;
56
57 /**
58 * Block type editor style handle.
59 *
60 * @since 2.0.0
61 * @var string
62 */
63 public $editor_style;
64
65 /**
66 * Block type front end style handle.
67 *
68 * @since 2.0.0
69 * @var string
70 */
71 public $style;
72
73 /**
74 * Constructor.
75 *
76 * Will populate object properties from the provided arguments.
77 *
78 * @since 0.6.0
79 *
80 * @see register_block_type()
81 *
82 * @param string $block_type Block type name including namespace.
83 * @param array|string $args Optional. Array or string of arguments for registering a block type.
84 * Default empty array.
85 */
86 public function __construct( $block_type, $args = array() ) {
87 $this->name = $block_type;
88
89 $this->set_props( $args );
90 }
91
92 /**
93 * Renders the block type output for given attributes.
94 *
95 * @since 0.6.0
96 *
97 * @param array $attributes Optional. Block attributes. Default empty array.
98 * @param string $content Optional. Block content. Default empty string.
99 * @return string Rendered block type output.
100 */
101 public function render( $attributes = array(), $content = '' ) {
102 if ( ! $this->is_dynamic() ) {
103 return '';
104 }
105
106 $attributes = $this->prepare_attributes_for_render( $attributes );
107
108 return (string) call_user_func( $this->render_callback, $attributes, $content );
109 }
110
111 /**
112 * Returns true if the block type is dynamic, or false otherwise. A dynamic
113 * block is one which defers its rendering to occur on-demand at runtime.
114 *
115 * @return boolean Whether block type is dynamic.
116 */
117 public function is_dynamic() {
118 return is_callable( $this->render_callback );
119 }
120
121 /**
122 * Validates attributes against the current block schema, populating
123 * defaulted and missing values.
124 *
125 * @param array $attributes Original block attributes.
126 * @return array Prepared block attributes.
127 */
128 public function prepare_attributes_for_render( $attributes ) {
129 // If there are no attribute definitions for the block type, skip
130 // processing and return vebatim.
131 if ( ! isset( $this->attributes ) ) {
132 return $attributes;
133 }
134
135 foreach ( $attributes as $attribute_name => $value ) {
136 // If the attribute is not defined by the block type, it cannot be
137 // validated.
138 if ( ! isset( $this->attributes[ $attribute_name ] ) ) {
139 continue;
140 }
141
142 $schema = $this->attributes[ $attribute_name ];
143
144 // Validate value by JSON schema. An invalid value should revert to
145 // its default, if one exists. This occurs by virtue of the missing
146 // attributes loop immediately following. If there is not a default
147 // assigned, the attribute value should remain unset.
148 $is_valid = rest_validate_value_from_schema( $value, $schema );
149 if ( is_wp_error( $is_valid ) ) {
150 unset( $attributes[ $attribute_name ] );
151 }
152 }
153
154 // Populate values of any missing attributes for which the block type
155 // defines a default.
156 $missing_schema_attributes = array_diff_key( $this->attributes, $attributes );
157 foreach ( $missing_schema_attributes as $attribute_name => $schema ) {
158 if ( isset( $schema['default'] ) ) {
159 $attributes[ $attribute_name ] = $schema['default'];
160 }
161 }
162
163 return $attributes;
164 }
165
166 /**
167 * Sets block type properties.
168 *
169 * @since 0.6.0
170 *
171 * @param array|string $args Array or string of arguments for registering a block type.
172 */
173 public function set_props( $args ) {
174 $args = wp_parse_args(
175 $args,
176 array(
177 'render_callback' => null,
178 )
179 );
180
181 $args['name'] = $this->name;
182
183 foreach ( $args as $property_name => $property_value ) {
184 $this->$property_name = $property_value;
185 }
186 }
187
188 /**
189 * Get all available block attributes including possible layout attribute from Columns block.
190 *
191 * @return array Array of attributes.
192 */
193 public function get_attributes() {
194 return is_array( $this->attributes ) ?
195 array_merge(
196 $this->attributes,
197 array(
198 'layout' => array(
199 'type' => 'string',
200 ),
201 )
202 ) :
203 array(
204 'layout' => array(
205 'type' => 'string',
206 ),
207 );
208 }
209 }
210