9
|
1 |
<?php |
|
2 |
/** |
|
3 |
* Functions related to registering and parsing blocks. |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
* @subpackage Blocks |
|
7 |
* @since 5.0.0 |
|
8 |
*/ |
|
9 |
|
|
10 |
/** |
16
|
11 |
* Removes the block asset's path prefix if provided. |
|
12 |
* |
|
13 |
* @since 5.5.0 |
|
14 |
* |
|
15 |
* @param string $asset_handle_or_path Asset handle or prefixed path. |
|
16 |
* @return string Path without the prefix or the original value. |
|
17 |
*/ |
|
18 |
function remove_block_asset_path_prefix( $asset_handle_or_path ) { |
|
19 |
$path_prefix = 'file:'; |
|
20 |
if ( 0 !== strpos( $asset_handle_or_path, $path_prefix ) ) { |
|
21 |
return $asset_handle_or_path; |
|
22 |
} |
19
|
23 |
$path = substr( |
16
|
24 |
$asset_handle_or_path, |
|
25 |
strlen( $path_prefix ) |
|
26 |
); |
19
|
27 |
if ( strpos( $path, './' ) === 0 ) { |
|
28 |
$path = substr( $path, 2 ); |
|
29 |
} |
|
30 |
return $path; |
16
|
31 |
} |
|
32 |
|
|
33 |
/** |
|
34 |
* Generates the name for an asset based on the name of the block |
|
35 |
* and the field name provided. |
|
36 |
* |
|
37 |
* @since 5.5.0 |
|
38 |
* |
|
39 |
* @param string $block_name Name of the block. |
|
40 |
* @param string $field_name Name of the metadata field. |
|
41 |
* @return string Generated asset name for the block's field. |
|
42 |
*/ |
|
43 |
function generate_block_asset_handle( $block_name, $field_name ) { |
18
|
44 |
if ( 0 === strpos( $block_name, 'core/' ) ) { |
|
45 |
$asset_handle = str_replace( 'core/', 'wp-block-', $block_name ); |
|
46 |
if ( 0 === strpos( $field_name, 'editor' ) ) { |
|
47 |
$asset_handle .= '-editor'; |
|
48 |
} |
19
|
49 |
if ( 0 === strpos( $field_name, 'view' ) ) { |
|
50 |
$asset_handle .= '-view'; |
|
51 |
} |
18
|
52 |
return $asset_handle; |
|
53 |
} |
|
54 |
|
16
|
55 |
$field_mappings = array( |
|
56 |
'editorScript' => 'editor-script', |
|
57 |
'script' => 'script', |
19
|
58 |
'viewScript' => 'view-script', |
16
|
59 |
'editorStyle' => 'editor-style', |
|
60 |
'style' => 'style', |
|
61 |
); |
|
62 |
return str_replace( '/', '-', $block_name ) . |
|
63 |
'-' . $field_mappings[ $field_name ]; |
|
64 |
} |
|
65 |
|
|
66 |
/** |
|
67 |
* Finds a script handle for the selected block metadata field. It detects |
|
68 |
* when a path to file was provided and finds a corresponding asset file |
|
69 |
* with details necessary to register the script under automatically |
|
70 |
* generated handle name. It returns unprocessed script handle otherwise. |
|
71 |
* |
|
72 |
* @since 5.5.0 |
|
73 |
* |
|
74 |
* @param array $metadata Block metadata. |
|
75 |
* @param string $field_name Field name to pick from metadata. |
18
|
76 |
* @return string|false Script handle provided directly or created through |
|
77 |
* script's registration, or false on failure. |
16
|
78 |
*/ |
|
79 |
function register_block_script_handle( $metadata, $field_name ) { |
|
80 |
if ( empty( $metadata[ $field_name ] ) ) { |
|
81 |
return false; |
|
82 |
} |
|
83 |
$script_handle = $metadata[ $field_name ]; |
|
84 |
$script_path = remove_block_asset_path_prefix( $metadata[ $field_name ] ); |
|
85 |
if ( $script_handle === $script_path ) { |
|
86 |
return $script_handle; |
|
87 |
} |
|
88 |
|
|
89 |
$script_handle = generate_block_asset_handle( $metadata['name'], $field_name ); |
19
|
90 |
$script_asset_path = wp_normalize_path( |
|
91 |
realpath( |
|
92 |
dirname( $metadata['file'] ) . '/' . |
|
93 |
substr_replace( $script_path, '.asset.php', - strlen( '.js' ) ) |
|
94 |
) |
16
|
95 |
); |
|
96 |
if ( ! file_exists( $script_asset_path ) ) { |
18
|
97 |
_doing_it_wrong( |
|
98 |
__FUNCTION__, |
|
99 |
sprintf( |
|
100 |
/* translators: 1: Field name, 2: Block name. */ |
|
101 |
__( 'The asset file for the "%1$s" defined in "%2$s" block definition is missing.' ), |
|
102 |
$field_name, |
|
103 |
$metadata['name'] |
|
104 |
), |
|
105 |
'5.5.0' |
16
|
106 |
); |
|
107 |
return false; |
|
108 |
} |
19
|
109 |
// Path needs to be normalized to work in Windows env. |
|
110 |
$wpinc_path_norm = wp_normalize_path( realpath( ABSPATH . WPINC ) ); |
|
111 |
$theme_path_norm = wp_normalize_path( get_theme_file_path() ); |
|
112 |
$script_path_norm = wp_normalize_path( realpath( dirname( $metadata['file'] ) . '/' . $script_path ) ); |
|
113 |
$is_core_block = isset( $metadata['file'] ) && 0 === strpos( $metadata['file'], $wpinc_path_norm ); |
|
114 |
$is_theme_block = 0 === strpos( $script_path_norm, $theme_path_norm ); |
|
115 |
|
|
116 |
$script_uri = plugins_url( $script_path, $metadata['file'] ); |
|
117 |
if ( $is_core_block ) { |
|
118 |
$script_uri = includes_url( str_replace( $wpinc_path_norm, '', $script_path_norm ) ); |
|
119 |
} elseif ( $is_theme_block ) { |
|
120 |
$script_uri = get_theme_file_uri( str_replace( $theme_path_norm, '', $script_path_norm ) ); |
|
121 |
} |
|
122 |
|
|
123 |
$script_asset = require $script_asset_path; |
|
124 |
$script_dependencies = isset( $script_asset['dependencies'] ) ? $script_asset['dependencies'] : array(); |
|
125 |
$result = wp_register_script( |
16
|
126 |
$script_handle, |
19
|
127 |
$script_uri, |
|
128 |
$script_dependencies, |
|
129 |
isset( $script_asset['version'] ) ? $script_asset['version'] : false |
16
|
130 |
); |
18
|
131 |
if ( ! $result ) { |
|
132 |
return false; |
|
133 |
} |
|
134 |
|
19
|
135 |
if ( ! empty( $metadata['textdomain'] ) && in_array( 'wp-i18n', $script_dependencies, true ) ) { |
18
|
136 |
wp_set_script_translations( $script_handle, $metadata['textdomain'] ); |
|
137 |
} |
|
138 |
|
|
139 |
return $script_handle; |
16
|
140 |
} |
|
141 |
|
|
142 |
/** |
|
143 |
* Finds a style handle for the block metadata field. It detects when a path |
|
144 |
* to file was provided and registers the style under automatically |
|
145 |
* generated handle name. It returns unprocessed style handle otherwise. |
|
146 |
* |
|
147 |
* @since 5.5.0 |
|
148 |
* |
18
|
149 |
* @param array $metadata Block metadata. |
16
|
150 |
* @param string $field_name Field name to pick from metadata. |
18
|
151 |
* @return string|false Style handle provided directly or created through |
|
152 |
* style's registration, or false on failure. |
16
|
153 |
*/ |
|
154 |
function register_block_style_handle( $metadata, $field_name ) { |
|
155 |
if ( empty( $metadata[ $field_name ] ) ) { |
|
156 |
return false; |
|
157 |
} |
19
|
158 |
$wpinc_path_norm = wp_normalize_path( realpath( ABSPATH . WPINC ) ); |
|
159 |
$theme_path_norm = wp_normalize_path( get_theme_file_path() ); |
|
160 |
$is_core_block = isset( $metadata['file'] ) && 0 === strpos( $metadata['file'], $wpinc_path_norm ); |
18
|
161 |
if ( $is_core_block && ! wp_should_load_separate_core_block_assets() ) { |
|
162 |
return false; |
|
163 |
} |
|
164 |
|
|
165 |
// Check whether styles should have a ".min" suffix or not. |
|
166 |
$suffix = SCRIPT_DEBUG ? '' : '.min'; |
|
167 |
|
16
|
168 |
$style_handle = $metadata[ $field_name ]; |
|
169 |
$style_path = remove_block_asset_path_prefix( $metadata[ $field_name ] ); |
18
|
170 |
|
|
171 |
if ( $style_handle === $style_path && ! $is_core_block ) { |
16
|
172 |
return $style_handle; |
|
173 |
} |
|
174 |
|
18
|
175 |
$style_uri = plugins_url( $style_path, $metadata['file'] ); |
|
176 |
if ( $is_core_block ) { |
|
177 |
$style_path = "style$suffix.css"; |
|
178 |
$style_uri = includes_url( 'blocks/' . str_replace( 'core/', '', $metadata['name'] ) . "/style$suffix.css" ); |
|
179 |
} |
|
180 |
|
19
|
181 |
$style_path_norm = wp_normalize_path( realpath( dirname( $metadata['file'] ) . '/' . $style_path ) ); |
|
182 |
$is_theme_block = 0 === strpos( $style_path_norm, $theme_path_norm ); |
|
183 |
|
|
184 |
if ( $is_theme_block ) { |
|
185 |
$style_uri = get_theme_file_uri( str_replace( $theme_path_norm, '', $style_path_norm ) ); |
|
186 |
} |
|
187 |
|
18
|
188 |
$style_handle = generate_block_asset_handle( $metadata['name'], $field_name ); |
|
189 |
$block_dir = dirname( $metadata['file'] ); |
|
190 |
$style_file = realpath( "$block_dir/$style_path" ); |
|
191 |
$has_style_file = false !== $style_file; |
|
192 |
$version = ! $is_core_block && isset( $metadata['version'] ) ? $metadata['version'] : false; |
|
193 |
$style_uri = $has_style_file ? $style_uri : false; |
|
194 |
$result = wp_register_style( |
16
|
195 |
$style_handle, |
18
|
196 |
$style_uri, |
16
|
197 |
array(), |
18
|
198 |
$version |
16
|
199 |
); |
18
|
200 |
if ( file_exists( str_replace( '.css', '-rtl.css', $style_file ) ) ) { |
|
201 |
wp_style_add_data( $style_handle, 'rtl', 'replace' ); |
|
202 |
} |
|
203 |
if ( $has_style_file ) { |
|
204 |
wp_style_add_data( $style_handle, 'path', $style_file ); |
|
205 |
} |
|
206 |
|
|
207 |
$rtl_file = str_replace( "$suffix.css", "-rtl$suffix.css", $style_file ); |
|
208 |
if ( is_rtl() && file_exists( $rtl_file ) ) { |
|
209 |
wp_style_add_data( $style_handle, 'path', $rtl_file ); |
|
210 |
} |
|
211 |
|
16
|
212 |
return $result ? $style_handle : false; |
|
213 |
} |
|
214 |
|
|
215 |
/** |
19
|
216 |
* Gets i18n schema for block's metadata read from `block.json` file. |
|
217 |
* |
|
218 |
* @since 5.9.0 |
|
219 |
* |
|
220 |
* @return object The schema for block's metadata. |
|
221 |
*/ |
|
222 |
function get_block_metadata_i18n_schema() { |
|
223 |
static $i18n_block_schema; |
|
224 |
|
|
225 |
if ( ! isset( $i18n_block_schema ) ) { |
|
226 |
$i18n_block_schema = wp_json_file_decode( __DIR__ . '/block-i18n.json' ); |
|
227 |
} |
|
228 |
|
|
229 |
return $i18n_block_schema; |
|
230 |
} |
|
231 |
|
|
232 |
/** |
18
|
233 |
* Registers a block type from the metadata stored in the `block.json` file. |
16
|
234 |
* |
|
235 |
* @since 5.5.0 |
19
|
236 |
* @since 5.7.0 Added support for `textdomain` field and i18n handling for all translatable fields. |
|
237 |
* @since 5.9.0 Added support for `variations` and `viewScript` fields. |
16
|
238 |
* |
|
239 |
* @param string $file_or_folder Path to the JSON file with metadata definition for |
|
240 |
* the block or path to the folder where the `block.json` file is located. |
19
|
241 |
* If providing the path to a JSON file, the filename must end with `block.json`. |
18
|
242 |
* @param array $args Optional. Array of block type arguments. Accepts any public property |
|
243 |
* of `WP_Block_Type`. See WP_Block_Type::__construct() for information |
|
244 |
* on accepted arguments. Default empty array. |
16
|
245 |
* @return WP_Block_Type|false The registered block type on success, or false on failure. |
|
246 |
*/ |
|
247 |
function register_block_type_from_metadata( $file_or_folder, $args = array() ) { |
|
248 |
$filename = 'block.json'; |
|
249 |
$metadata_file = ( substr( $file_or_folder, -strlen( $filename ) ) !== $filename ) ? |
|
250 |
trailingslashit( $file_or_folder ) . $filename : |
|
251 |
$file_or_folder; |
|
252 |
if ( ! file_exists( $metadata_file ) ) { |
|
253 |
return false; |
|
254 |
} |
|
255 |
|
19
|
256 |
$metadata = wp_json_file_decode( $metadata_file, array( 'associative' => true ) ); |
16
|
257 |
if ( ! is_array( $metadata ) || empty( $metadata['name'] ) ) { |
|
258 |
return false; |
|
259 |
} |
19
|
260 |
$metadata['file'] = wp_normalize_path( realpath( $metadata_file ) ); |
16
|
261 |
|
18
|
262 |
/** |
|
263 |
* Filters the metadata provided for registering a block type. |
|
264 |
* |
|
265 |
* @since 5.7.0 |
|
266 |
* |
|
267 |
* @param array $metadata Metadata for registering a block type. |
|
268 |
*/ |
|
269 |
$metadata = apply_filters( 'block_type_metadata', $metadata ); |
|
270 |
|
|
271 |
// Add `style` and `editor_style` for core blocks if missing. |
|
272 |
if ( ! empty( $metadata['name'] ) && 0 === strpos( $metadata['name'], 'core/' ) ) { |
|
273 |
$block_name = str_replace( 'core/', '', $metadata['name'] ); |
|
274 |
|
|
275 |
if ( ! isset( $metadata['style'] ) ) { |
|
276 |
$metadata['style'] = "wp-block-$block_name"; |
|
277 |
} |
|
278 |
if ( ! isset( $metadata['editorStyle'] ) ) { |
|
279 |
$metadata['editorStyle'] = "wp-block-{$block_name}-editor"; |
|
280 |
} |
|
281 |
} |
|
282 |
|
16
|
283 |
$settings = array(); |
|
284 |
$property_mappings = array( |
19
|
285 |
'apiVersion' => 'api_version', |
16
|
286 |
'title' => 'title', |
|
287 |
'category' => 'category', |
|
288 |
'parent' => 'parent', |
19
|
289 |
'ancestor' => 'ancestor', |
16
|
290 |
'icon' => 'icon', |
|
291 |
'description' => 'description', |
|
292 |
'keywords' => 'keywords', |
|
293 |
'attributes' => 'attributes', |
|
294 |
'providesContext' => 'provides_context', |
|
295 |
'usesContext' => 'uses_context', |
|
296 |
'supports' => 'supports', |
|
297 |
'styles' => 'styles', |
19
|
298 |
'variations' => 'variations', |
16
|
299 |
'example' => 'example', |
|
300 |
); |
19
|
301 |
$textdomain = ! empty( $metadata['textdomain'] ) ? $metadata['textdomain'] : null; |
|
302 |
$i18n_schema = get_block_metadata_i18n_schema(); |
16
|
303 |
|
|
304 |
foreach ( $property_mappings as $key => $mapped_key ) { |
|
305 |
if ( isset( $metadata[ $key ] ) ) { |
19
|
306 |
$settings[ $mapped_key ] = $metadata[ $key ]; |
|
307 |
if ( $textdomain && isset( $i18n_schema->$key ) ) { |
|
308 |
$settings[ $mapped_key ] = translate_settings_using_i18n_schema( $i18n_schema->$key, $settings[ $key ], $textdomain ); |
18
|
309 |
} |
16
|
310 |
} |
|
311 |
} |
|
312 |
|
|
313 |
if ( ! empty( $metadata['editorScript'] ) ) { |
|
314 |
$settings['editor_script'] = register_block_script_handle( |
|
315 |
$metadata, |
|
316 |
'editorScript' |
|
317 |
); |
|
318 |
} |
|
319 |
|
|
320 |
if ( ! empty( $metadata['script'] ) ) { |
|
321 |
$settings['script'] = register_block_script_handle( |
|
322 |
$metadata, |
|
323 |
'script' |
|
324 |
); |
|
325 |
} |
|
326 |
|
19
|
327 |
if ( ! empty( $metadata['viewScript'] ) ) { |
|
328 |
$settings['view_script'] = register_block_script_handle( |
|
329 |
$metadata, |
|
330 |
'viewScript' |
|
331 |
); |
|
332 |
} |
|
333 |
|
16
|
334 |
if ( ! empty( $metadata['editorStyle'] ) ) { |
|
335 |
$settings['editor_style'] = register_block_style_handle( |
|
336 |
$metadata, |
|
337 |
'editorStyle' |
|
338 |
); |
|
339 |
} |
|
340 |
|
|
341 |
if ( ! empty( $metadata['style'] ) ) { |
|
342 |
$settings['style'] = register_block_style_handle( |
|
343 |
$metadata, |
|
344 |
'style' |
|
345 |
); |
|
346 |
} |
|
347 |
|
18
|
348 |
/** |
|
349 |
* Filters the settings determined from the block type metadata. |
|
350 |
* |
|
351 |
* @since 5.7.0 |
|
352 |
* |
|
353 |
* @param array $settings Array of determined settings for registering a block type. |
|
354 |
* @param array $metadata Metadata provided for registering a block type. |
|
355 |
*/ |
|
356 |
$settings = apply_filters( |
|
357 |
'block_type_metadata_settings', |
16
|
358 |
array_merge( |
|
359 |
$settings, |
|
360 |
$args |
18
|
361 |
), |
|
362 |
$metadata |
|
363 |
); |
|
364 |
|
|
365 |
return WP_Block_Type_Registry::get_instance()->register( |
|
366 |
$metadata['name'], |
|
367 |
$settings |
16
|
368 |
); |
|
369 |
} |
|
370 |
|
|
371 |
/** |
18
|
372 |
* Registers a block type. The recommended way is to register a block type using |
|
373 |
* the metadata stored in the `block.json` file. |
|
374 |
* |
|
375 |
* @since 5.0.0 |
19
|
376 |
* @since 5.8.0 First parameter now accepts a path to the `block.json` file. |
18
|
377 |
* |
|
378 |
* @param string|WP_Block_Type $block_type Block type name including namespace, or alternatively |
|
379 |
* a path to the JSON file with metadata definition for the block, |
|
380 |
* or a path to the folder where the `block.json` file is located, |
|
381 |
* or a complete WP_Block_Type instance. |
|
382 |
* In case a WP_Block_Type is provided, the $args parameter will be ignored. |
|
383 |
* @param array $args Optional. Array of block type arguments. Accepts any public property |
|
384 |
* of `WP_Block_Type`. See WP_Block_Type::__construct() for information |
|
385 |
* on accepted arguments. Default empty array. |
|
386 |
* |
|
387 |
* @return WP_Block_Type|false The registered block type on success, or false on failure. |
|
388 |
*/ |
|
389 |
function register_block_type( $block_type, $args = array() ) { |
|
390 |
if ( is_string( $block_type ) && file_exists( $block_type ) ) { |
|
391 |
return register_block_type_from_metadata( $block_type, $args ); |
|
392 |
} |
|
393 |
|
|
394 |
return WP_Block_Type_Registry::get_instance()->register( $block_type, $args ); |
|
395 |
} |
|
396 |
|
|
397 |
/** |
|
398 |
* Unregisters a block type. |
|
399 |
* |
|
400 |
* @since 5.0.0 |
|
401 |
* |
|
402 |
* @param string|WP_Block_Type $name Block type name including namespace, or alternatively |
|
403 |
* a complete WP_Block_Type instance. |
|
404 |
* @return WP_Block_Type|false The unregistered block type on success, or false on failure. |
|
405 |
*/ |
|
406 |
function unregister_block_type( $name ) { |
|
407 |
return WP_Block_Type_Registry::get_instance()->unregister( $name ); |
|
408 |
} |
|
409 |
|
|
410 |
/** |
19
|
411 |
* Determines whether a post or content string has blocks. |
9
|
412 |
* |
|
413 |
* This test optimizes for performance rather than strict accuracy, detecting |
|
414 |
* the pattern of a block but not validating its structure. For strict accuracy, |
|
415 |
* you should use the block parser on post content. |
|
416 |
* |
|
417 |
* @since 5.0.0 |
16
|
418 |
* |
9
|
419 |
* @see parse_blocks() |
|
420 |
* |
18
|
421 |
* @param int|string|WP_Post|null $post Optional. Post content, post ID, or post object. |
|
422 |
* Defaults to global $post. |
9
|
423 |
* @return bool Whether the post has blocks. |
|
424 |
*/ |
|
425 |
function has_blocks( $post = null ) { |
|
426 |
if ( ! is_string( $post ) ) { |
|
427 |
$wp_post = get_post( $post ); |
|
428 |
if ( $wp_post instanceof WP_Post ) { |
|
429 |
$post = $wp_post->post_content; |
|
430 |
} |
|
431 |
} |
|
432 |
|
|
433 |
return false !== strpos( (string) $post, '<!-- wp:' ); |
|
434 |
} |
|
435 |
|
|
436 |
/** |
19
|
437 |
* Determines whether a $post or a string contains a specific block type. |
9
|
438 |
* |
|
439 |
* This test optimizes for performance rather than strict accuracy, detecting |
18
|
440 |
* whether the block type exists but not validating its structure and not checking |
|
441 |
* reusable blocks. For strict accuracy, you should use the block parser on post content. |
9
|
442 |
* |
|
443 |
* @since 5.0.0 |
16
|
444 |
* |
9
|
445 |
* @see parse_blocks() |
|
446 |
* |
18
|
447 |
* @param string $block_name Full block type to look for. |
|
448 |
* @param int|string|WP_Post|null $post Optional. Post content, post ID, or post object. |
|
449 |
* Defaults to global $post. |
9
|
450 |
* @return bool Whether the post content contains the specified block. |
|
451 |
*/ |
16
|
452 |
function has_block( $block_name, $post = null ) { |
9
|
453 |
if ( ! has_blocks( $post ) ) { |
|
454 |
return false; |
|
455 |
} |
|
456 |
|
|
457 |
if ( ! is_string( $post ) ) { |
|
458 |
$wp_post = get_post( $post ); |
|
459 |
if ( $wp_post instanceof WP_Post ) { |
|
460 |
$post = $wp_post->post_content; |
|
461 |
} |
|
462 |
} |
|
463 |
|
16
|
464 |
/* |
|
465 |
* Normalize block name to include namespace, if provided as non-namespaced. |
|
466 |
* This matches behavior for WordPress 5.0.0 - 5.3.0 in matching blocks by |
|
467 |
* their serialized names. |
|
468 |
*/ |
|
469 |
if ( false === strpos( $block_name, '/' ) ) { |
|
470 |
$block_name = 'core/' . $block_name; |
|
471 |
} |
|
472 |
|
|
473 |
// Test for existence of block by its fully qualified name. |
|
474 |
$has_block = false !== strpos( $post, '<!-- wp:' . $block_name . ' ' ); |
|
475 |
|
|
476 |
if ( ! $has_block ) { |
|
477 |
/* |
|
478 |
* If the given block name would serialize to a different name, test for |
|
479 |
* existence by the serialized form. |
|
480 |
*/ |
|
481 |
$serialized_block_name = strip_core_block_namespace( $block_name ); |
|
482 |
if ( $serialized_block_name !== $block_name ) { |
|
483 |
$has_block = false !== strpos( $post, '<!-- wp:' . $serialized_block_name . ' ' ); |
|
484 |
} |
|
485 |
} |
|
486 |
|
|
487 |
return $has_block; |
9
|
488 |
} |
|
489 |
|
|
490 |
/** |
|
491 |
* Returns an array of the names of all registered dynamic block types. |
|
492 |
* |
|
493 |
* @since 5.0.0 |
|
494 |
* |
16
|
495 |
* @return string[] Array of dynamic block names. |
9
|
496 |
*/ |
|
497 |
function get_dynamic_block_names() { |
|
498 |
$dynamic_block_names = array(); |
|
499 |
|
|
500 |
$block_types = WP_Block_Type_Registry::get_instance()->get_all_registered(); |
|
501 |
foreach ( $block_types as $block_type ) { |
|
502 |
if ( $block_type->is_dynamic() ) { |
|
503 |
$dynamic_block_names[] = $block_type->name; |
|
504 |
} |
|
505 |
} |
|
506 |
|
|
507 |
return $dynamic_block_names; |
|
508 |
} |
|
509 |
|
|
510 |
/** |
16
|
511 |
* Given an array of attributes, returns a string in the serialized attributes |
|
512 |
* format prepared for post content. |
|
513 |
* |
|
514 |
* The serialized result is a JSON-encoded string, with unicode escape sequence |
|
515 |
* substitution for characters which might otherwise interfere with embedding |
|
516 |
* the result in an HTML comment. |
|
517 |
* |
18
|
518 |
* This function must produce output that remains in sync with the output of |
|
519 |
* the serializeAttributes JavaScript function in the block editor in order |
|
520 |
* to ensure consistent operation between PHP and JavaScript. |
|
521 |
* |
16
|
522 |
* @since 5.3.1 |
|
523 |
* |
|
524 |
* @param array $block_attributes Attributes object. |
|
525 |
* @return string Serialized attributes. |
|
526 |
*/ |
|
527 |
function serialize_block_attributes( $block_attributes ) { |
18
|
528 |
$encoded_attributes = wp_json_encode( $block_attributes, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); |
16
|
529 |
$encoded_attributes = preg_replace( '/--/', '\\u002d\\u002d', $encoded_attributes ); |
|
530 |
$encoded_attributes = preg_replace( '/</', '\\u003c', $encoded_attributes ); |
|
531 |
$encoded_attributes = preg_replace( '/>/', '\\u003e', $encoded_attributes ); |
|
532 |
$encoded_attributes = preg_replace( '/&/', '\\u0026', $encoded_attributes ); |
|
533 |
// Regex: /\\"/ |
|
534 |
$encoded_attributes = preg_replace( '/\\\\"/', '\\u0022', $encoded_attributes ); |
|
535 |
|
|
536 |
return $encoded_attributes; |
|
537 |
} |
|
538 |
|
|
539 |
/** |
|
540 |
* Returns the block name to use for serialization. This will remove the default |
|
541 |
* "core/" namespace from a block name. |
|
542 |
* |
|
543 |
* @since 5.3.1 |
|
544 |
* |
|
545 |
* @param string $block_name Original block name. |
|
546 |
* @return string Block name to use for serialization. |
|
547 |
*/ |
|
548 |
function strip_core_block_namespace( $block_name = null ) { |
|
549 |
if ( is_string( $block_name ) && 0 === strpos( $block_name, 'core/' ) ) { |
|
550 |
return substr( $block_name, 5 ); |
|
551 |
} |
|
552 |
|
|
553 |
return $block_name; |
|
554 |
} |
|
555 |
|
|
556 |
/** |
|
557 |
* Returns the content of a block, including comment delimiters. |
|
558 |
* |
|
559 |
* @since 5.3.1 |
|
560 |
* |
18
|
561 |
* @param string|null $block_name Block name. Null if the block name is unknown, |
|
562 |
* e.g. Classic blocks have their name set to null. |
|
563 |
* @param array $block_attributes Block attributes. |
|
564 |
* @param string $block_content Block save content. |
16
|
565 |
* @return string Comment-delimited block content. |
|
566 |
*/ |
18
|
567 |
function get_comment_delimited_block_content( $block_name, $block_attributes, $block_content ) { |
16
|
568 |
if ( is_null( $block_name ) ) { |
|
569 |
return $block_content; |
|
570 |
} |
|
571 |
|
|
572 |
$serialized_block_name = strip_core_block_namespace( $block_name ); |
|
573 |
$serialized_attributes = empty( $block_attributes ) ? '' : serialize_block_attributes( $block_attributes ) . ' '; |
|
574 |
|
|
575 |
if ( empty( $block_content ) ) { |
|
576 |
return sprintf( '<!-- wp:%s %s/-->', $serialized_block_name, $serialized_attributes ); |
|
577 |
} |
|
578 |
|
|
579 |
return sprintf( |
|
580 |
'<!-- wp:%s %s-->%s<!-- /wp:%s -->', |
|
581 |
$serialized_block_name, |
|
582 |
$serialized_attributes, |
|
583 |
$block_content, |
|
584 |
$serialized_block_name |
|
585 |
); |
|
586 |
} |
|
587 |
|
|
588 |
/** |
|
589 |
* Returns the content of a block, including comment delimiters, serializing all |
|
590 |
* attributes from the given parsed block. |
|
591 |
* |
|
592 |
* This should be used when preparing a block to be saved to post content. |
|
593 |
* Prefer `render_block` when preparing a block for display. Unlike |
|
594 |
* `render_block`, this does not evaluate a block's `render_callback`, and will |
|
595 |
* instead preserve the markup as parsed. |
|
596 |
* |
|
597 |
* @since 5.3.1 |
|
598 |
* |
19
|
599 |
* @param array $block A representative array of a single parsed block object. See WP_Block_Parser_Block. |
16
|
600 |
* @return string String of rendered HTML. |
|
601 |
*/ |
|
602 |
function serialize_block( $block ) { |
|
603 |
$block_content = ''; |
|
604 |
|
|
605 |
$index = 0; |
|
606 |
foreach ( $block['innerContent'] as $chunk ) { |
|
607 |
$block_content .= is_string( $chunk ) ? $chunk : serialize_block( $block['innerBlocks'][ $index++ ] ); |
|
608 |
} |
|
609 |
|
|
610 |
if ( ! is_array( $block['attrs'] ) ) { |
|
611 |
$block['attrs'] = array(); |
|
612 |
} |
|
613 |
|
|
614 |
return get_comment_delimited_block_content( |
|
615 |
$block['blockName'], |
|
616 |
$block['attrs'], |
|
617 |
$block_content |
|
618 |
); |
|
619 |
} |
|
620 |
|
|
621 |
/** |
|
622 |
* Returns a joined string of the aggregate serialization of the given parsed |
|
623 |
* blocks. |
|
624 |
* |
|
625 |
* @since 5.3.1 |
|
626 |
* |
19
|
627 |
* @param array[] $blocks An array of representative arrays of parsed block objects. See serialize_block(). |
16
|
628 |
* @return string String of rendered HTML. |
|
629 |
*/ |
|
630 |
function serialize_blocks( $blocks ) { |
|
631 |
return implode( '', array_map( 'serialize_block', $blocks ) ); |
|
632 |
} |
|
633 |
|
|
634 |
/** |
|
635 |
* Filters and sanitizes block content to remove non-allowable HTML from |
|
636 |
* parsed block attribute values. |
|
637 |
* |
|
638 |
* @since 5.3.1 |
|
639 |
* |
|
640 |
* @param string $text Text that may contain block content. |
|
641 |
* @param array[]|string $allowed_html An array of allowed HTML elements |
|
642 |
* and attributes, or a context name |
|
643 |
* such as 'post'. |
|
644 |
* @param string[] $allowed_protocols Array of allowed URL protocols. |
|
645 |
* @return string The filtered and sanitized content result. |
|
646 |
*/ |
|
647 |
function filter_block_content( $text, $allowed_html = 'post', $allowed_protocols = array() ) { |
|
648 |
$result = ''; |
|
649 |
|
|
650 |
$blocks = parse_blocks( $text ); |
|
651 |
foreach ( $blocks as $block ) { |
|
652 |
$block = filter_block_kses( $block, $allowed_html, $allowed_protocols ); |
|
653 |
$result .= serialize_block( $block ); |
|
654 |
} |
|
655 |
|
|
656 |
return $result; |
|
657 |
} |
|
658 |
|
|
659 |
/** |
|
660 |
* Filters and sanitizes a parsed block to remove non-allowable HTML from block |
|
661 |
* attribute values. |
|
662 |
* |
|
663 |
* @since 5.3.1 |
|
664 |
* |
|
665 |
* @param WP_Block_Parser_Block $block The parsed block object. |
|
666 |
* @param array[]|string $allowed_html An array of allowed HTML |
|
667 |
* elements and attributes, or a |
|
668 |
* context name such as 'post'. |
|
669 |
* @param string[] $allowed_protocols Allowed URL protocols. |
|
670 |
* @return array The filtered and sanitized block object result. |
|
671 |
*/ |
|
672 |
function filter_block_kses( $block, $allowed_html, $allowed_protocols = array() ) { |
|
673 |
$block['attrs'] = filter_block_kses_value( $block['attrs'], $allowed_html, $allowed_protocols ); |
|
674 |
|
|
675 |
if ( is_array( $block['innerBlocks'] ) ) { |
|
676 |
foreach ( $block['innerBlocks'] as $i => $inner_block ) { |
|
677 |
$block['innerBlocks'][ $i ] = filter_block_kses( $inner_block, $allowed_html, $allowed_protocols ); |
|
678 |
} |
|
679 |
} |
|
680 |
|
|
681 |
return $block; |
|
682 |
} |
|
683 |
|
|
684 |
/** |
|
685 |
* Filters and sanitizes a parsed block attribute value to remove non-allowable |
|
686 |
* HTML. |
|
687 |
* |
|
688 |
* @since 5.3.1 |
|
689 |
* |
|
690 |
* @param string[]|string $value The attribute value to filter. |
|
691 |
* @param array[]|string $allowed_html An array of allowed HTML elements |
|
692 |
* and attributes, or a context name |
|
693 |
* such as 'post'. |
|
694 |
* @param string[] $allowed_protocols Array of allowed URL protocols. |
|
695 |
* @return string[]|string The filtered and sanitized result. |
|
696 |
*/ |
|
697 |
function filter_block_kses_value( $value, $allowed_html, $allowed_protocols = array() ) { |
|
698 |
if ( is_array( $value ) ) { |
|
699 |
foreach ( $value as $key => $inner_value ) { |
|
700 |
$filtered_key = filter_block_kses_value( $key, $allowed_html, $allowed_protocols ); |
|
701 |
$filtered_value = filter_block_kses_value( $inner_value, $allowed_html, $allowed_protocols ); |
|
702 |
|
|
703 |
if ( $filtered_key !== $key ) { |
|
704 |
unset( $value[ $key ] ); |
|
705 |
} |
|
706 |
|
|
707 |
$value[ $filtered_key ] = $filtered_value; |
|
708 |
} |
|
709 |
} elseif ( is_string( $value ) ) { |
|
710 |
return wp_kses( $value, $allowed_html, $allowed_protocols ); |
|
711 |
} |
|
712 |
|
|
713 |
return $value; |
|
714 |
} |
|
715 |
|
|
716 |
/** |
9
|
717 |
* Parses blocks out of a content string, and renders those appropriate for the excerpt. |
|
718 |
* |
|
719 |
* As the excerpt should be a small string of text relevant to the full post content, |
|
720 |
* this function renders the blocks that are most likely to contain such text. |
|
721 |
* |
|
722 |
* @since 5.0.0 |
|
723 |
* |
|
724 |
* @param string $content The content to parse. |
|
725 |
* @return string The parsed and filtered content. |
|
726 |
*/ |
|
727 |
function excerpt_remove_blocks( $content ) { |
|
728 |
$allowed_inner_blocks = array( |
|
729 |
// Classic blocks have their blockName set to null. |
|
730 |
null, |
|
731 |
'core/freeform', |
|
732 |
'core/heading', |
|
733 |
'core/html', |
|
734 |
'core/list', |
|
735 |
'core/media-text', |
|
736 |
'core/paragraph', |
|
737 |
'core/preformatted', |
|
738 |
'core/pullquote', |
|
739 |
'core/quote', |
|
740 |
'core/table', |
|
741 |
'core/verse', |
|
742 |
); |
|
743 |
|
18
|
744 |
$allowed_wrapper_blocks = array( |
|
745 |
'core/columns', |
|
746 |
'core/column', |
|
747 |
'core/group', |
|
748 |
); |
|
749 |
|
|
750 |
/** |
|
751 |
* Filters the list of blocks that can be used as wrapper blocks, allowing |
|
752 |
* excerpts to be generated from the `innerBlocks` of these wrappers. |
|
753 |
* |
|
754 |
* @since 5.8.0 |
|
755 |
* |
19
|
756 |
* @param string[] $allowed_wrapper_blocks The list of names of allowed wrapper blocks. |
18
|
757 |
*/ |
|
758 |
$allowed_wrapper_blocks = apply_filters( 'excerpt_allowed_wrapper_blocks', $allowed_wrapper_blocks ); |
|
759 |
|
|
760 |
$allowed_blocks = array_merge( $allowed_inner_blocks, $allowed_wrapper_blocks ); |
9
|
761 |
|
|
762 |
/** |
|
763 |
* Filters the list of blocks that can contribute to the excerpt. |
|
764 |
* |
|
765 |
* If a dynamic block is added to this list, it must not generate another |
|
766 |
* excerpt, as this will cause an infinite loop to occur. |
|
767 |
* |
|
768 |
* @since 5.0.0 |
|
769 |
* |
19
|
770 |
* @param string[] $allowed_blocks The list of names of allowed blocks. |
9
|
771 |
*/ |
|
772 |
$allowed_blocks = apply_filters( 'excerpt_allowed_blocks', $allowed_blocks ); |
|
773 |
$blocks = parse_blocks( $content ); |
|
774 |
$output = ''; |
|
775 |
|
|
776 |
foreach ( $blocks as $block ) { |
|
777 |
if ( in_array( $block['blockName'], $allowed_blocks, true ) ) { |
|
778 |
if ( ! empty( $block['innerBlocks'] ) ) { |
18
|
779 |
if ( in_array( $block['blockName'], $allowed_wrapper_blocks, true ) ) { |
|
780 |
$output .= _excerpt_render_inner_blocks( $block, $allowed_blocks ); |
9
|
781 |
continue; |
|
782 |
} |
|
783 |
|
|
784 |
// Skip the block if it has disallowed or nested inner blocks. |
|
785 |
foreach ( $block['innerBlocks'] as $inner_block ) { |
|
786 |
if ( |
|
787 |
! in_array( $inner_block['blockName'], $allowed_inner_blocks, true ) || |
|
788 |
! empty( $inner_block['innerBlocks'] ) |
|
789 |
) { |
|
790 |
continue 2; |
|
791 |
} |
|
792 |
} |
|
793 |
} |
|
794 |
|
|
795 |
$output .= render_block( $block ); |
|
796 |
} |
|
797 |
} |
|
798 |
|
|
799 |
return $output; |
|
800 |
} |
|
801 |
|
|
802 |
/** |
18
|
803 |
* Render inner blocks from the allowed wrapper blocks |
|
804 |
* for generating an excerpt. |
9
|
805 |
* |
19
|
806 |
* @since 5.8.0 |
9
|
807 |
* @access private |
|
808 |
* |
18
|
809 |
* @param array $parsed_block The parsed block. |
9
|
810 |
* @param array $allowed_blocks The list of allowed inner blocks. |
|
811 |
* @return string The rendered inner blocks. |
|
812 |
*/ |
18
|
813 |
function _excerpt_render_inner_blocks( $parsed_block, $allowed_blocks ) { |
9
|
814 |
$output = ''; |
|
815 |
|
18
|
816 |
foreach ( $parsed_block['innerBlocks'] as $inner_block ) { |
|
817 |
if ( ! in_array( $inner_block['blockName'], $allowed_blocks, true ) ) { |
|
818 |
continue; |
|
819 |
} |
|
820 |
|
|
821 |
if ( empty( $inner_block['innerBlocks'] ) ) { |
|
822 |
$output .= render_block( $inner_block ); |
|
823 |
} else { |
|
824 |
$output .= _excerpt_render_inner_blocks( $inner_block, $allowed_blocks ); |
9
|
825 |
} |
|
826 |
} |
|
827 |
|
|
828 |
return $output; |
|
829 |
} |
|
830 |
|
|
831 |
/** |
|
832 |
* Renders a single block into a HTML string. |
|
833 |
* |
|
834 |
* @since 5.0.0 |
|
835 |
* |
16
|
836 |
* @global WP_Post $post The post to edit. |
9
|
837 |
* |
16
|
838 |
* @param array $parsed_block A single parsed block object. |
9
|
839 |
* @return string String of rendered HTML. |
|
840 |
*/ |
16
|
841 |
function render_block( $parsed_block ) { |
18
|
842 |
global $post; |
19
|
843 |
$parent_block = null; |
9
|
844 |
|
|
845 |
/** |
16
|
846 |
* Allows render_block() to be short-circuited, by returning a non-null value. |
9
|
847 |
* |
|
848 |
* @since 5.1.0 |
19
|
849 |
* @since 5.9.0 The `$parent_block` parameter was added. |
9
|
850 |
* |
19
|
851 |
* @param string|null $pre_render The pre-rendered content. Default null. |
|
852 |
* @param array $parsed_block The block being rendered. |
|
853 |
* @param WP_Block|null $parent_block If this is a nested block, a reference to the parent block. |
9
|
854 |
*/ |
19
|
855 |
$pre_render = apply_filters( 'pre_render_block', null, $parsed_block, $parent_block ); |
9
|
856 |
if ( ! is_null( $pre_render ) ) { |
|
857 |
return $pre_render; |
|
858 |
} |
|
859 |
|
16
|
860 |
$source_block = $parsed_block; |
9
|
861 |
|
|
862 |
/** |
|
863 |
* Filters the block being rendered in render_block(), before it's processed. |
|
864 |
* |
|
865 |
* @since 5.1.0 |
19
|
866 |
* @since 5.9.0 The `$parent_block` parameter was added. |
9
|
867 |
* |
19
|
868 |
* @param array $parsed_block The block being rendered. |
|
869 |
* @param array $source_block An un-modified copy of $parsed_block, as it appeared in the source content. |
|
870 |
* @param WP_Block|null $parent_block If this is a nested block, a reference to the parent block. |
9
|
871 |
*/ |
19
|
872 |
$parsed_block = apply_filters( 'render_block_data', $parsed_block, $source_block, $parent_block ); |
16
|
873 |
|
|
874 |
$context = array(); |
9
|
875 |
|
16
|
876 |
if ( $post instanceof WP_Post ) { |
|
877 |
$context['postId'] = $post->ID; |
9
|
878 |
|
16
|
879 |
/* |
|
880 |
* The `postType` context is largely unnecessary server-side, since the ID |
|
881 |
* is usually sufficient on its own. That being said, since a block's |
|
882 |
* manifest is expected to be shared between the server and the client, |
|
883 |
* it should be included to consistently fulfill the expectation. |
|
884 |
*/ |
|
885 |
$context['postType'] = $post->post_type; |
9
|
886 |
} |
|
887 |
|
|
888 |
/** |
16
|
889 |
* Filters the default context provided to a rendered block. |
9
|
890 |
* |
16
|
891 |
* @since 5.5.0 |
19
|
892 |
* @since 5.9.0 The `$parent_block` parameter was added. |
9
|
893 |
* |
19
|
894 |
* @param array $context Default context. |
|
895 |
* @param array $parsed_block Block being rendered, filtered by `render_block_data`. |
|
896 |
* @param WP_Block|null $parent_block If this is a nested block, a reference to the parent block. |
9
|
897 |
*/ |
19
|
898 |
$context = apply_filters( 'render_block_context', $context, $parsed_block, $parent_block ); |
16
|
899 |
|
|
900 |
$block = new WP_Block( $parsed_block, $context ); |
|
901 |
|
|
902 |
return $block->render(); |
9
|
903 |
} |
|
904 |
|
|
905 |
/** |
|
906 |
* Parses blocks out of a content string. |
|
907 |
* |
|
908 |
* @since 5.0.0 |
|
909 |
* |
|
910 |
* @param string $content Post content. |
16
|
911 |
* @return array[] Array of parsed block objects. |
9
|
912 |
*/ |
|
913 |
function parse_blocks( $content ) { |
|
914 |
/** |
|
915 |
* Filter to allow plugins to replace the server-side block parser |
|
916 |
* |
|
917 |
* @since 5.0.0 |
|
918 |
* |
|
919 |
* @param string $parser_class Name of block parser class. |
|
920 |
*/ |
|
921 |
$parser_class = apply_filters( 'block_parser_class', 'WP_Block_Parser' ); |
|
922 |
|
|
923 |
$parser = new $parser_class(); |
|
924 |
return $parser->parse( $content ); |
|
925 |
} |
|
926 |
|
|
927 |
/** |
|
928 |
* Parses dynamic blocks out of `post_content` and re-renders them. |
|
929 |
* |
|
930 |
* @since 5.0.0 |
|
931 |
* |
16
|
932 |
* @param string $content Post content. |
9
|
933 |
* @return string Updated post content. |
|
934 |
*/ |
|
935 |
function do_blocks( $content ) { |
|
936 |
$blocks = parse_blocks( $content ); |
|
937 |
$output = ''; |
|
938 |
|
|
939 |
foreach ( $blocks as $block ) { |
|
940 |
$output .= render_block( $block ); |
|
941 |
} |
|
942 |
|
|
943 |
// If there are blocks in this content, we shouldn't run wpautop() on it later. |
|
944 |
$priority = has_filter( 'the_content', 'wpautop' ); |
|
945 |
if ( false !== $priority && doing_filter( 'the_content' ) && has_blocks( $content ) ) { |
|
946 |
remove_filter( 'the_content', 'wpautop', $priority ); |
|
947 |
add_filter( 'the_content', '_restore_wpautop_hook', $priority + 1 ); |
|
948 |
} |
|
949 |
|
|
950 |
return $output; |
|
951 |
} |
|
952 |
|
|
953 |
/** |
|
954 |
* If do_blocks() needs to remove wpautop() from the `the_content` filter, this re-adds it afterwards, |
|
955 |
* for subsequent `the_content` usage. |
|
956 |
* |
19
|
957 |
* @since 5.0.0 |
9
|
958 |
* @access private |
|
959 |
* |
|
960 |
* @param string $content The post content running through this filter. |
|
961 |
* @return string The unmodified content. |
|
962 |
*/ |
|
963 |
function _restore_wpautop_hook( $content ) { |
|
964 |
$current_priority = has_filter( 'the_content', '_restore_wpautop_hook' ); |
|
965 |
|
|
966 |
add_filter( 'the_content', 'wpautop', $current_priority - 1 ); |
|
967 |
remove_filter( 'the_content', '_restore_wpautop_hook', $current_priority ); |
|
968 |
|
|
969 |
return $content; |
|
970 |
} |
|
971 |
|
|
972 |
/** |
|
973 |
* Returns the current version of the block format that the content string is using. |
|
974 |
* |
|
975 |
* If the string doesn't contain blocks, it returns 0. |
|
976 |
* |
|
977 |
* @since 5.0.0 |
|
978 |
* |
|
979 |
* @param string $content Content to test. |
|
980 |
* @return int The block format version is 1 if the content contains one or more blocks, 0 otherwise. |
|
981 |
*/ |
|
982 |
function block_version( $content ) { |
|
983 |
return has_blocks( $content ) ? 1 : 0; |
|
984 |
} |
16
|
985 |
|
|
986 |
/** |
|
987 |
* Registers a new block style. |
|
988 |
* |
|
989 |
* @since 5.3.0 |
|
990 |
* |
|
991 |
* @param string $block_name Block type name including namespace. |
|
992 |
* @param array $style_properties Array containing the properties of the style name, |
|
993 |
* label, style (name of the stylesheet to be enqueued), |
|
994 |
* inline_style (string containing the CSS to be added). |
18
|
995 |
* @return bool True if the block style was registered with success and false otherwise. |
16
|
996 |
*/ |
|
997 |
function register_block_style( $block_name, $style_properties ) { |
|
998 |
return WP_Block_Styles_Registry::get_instance()->register( $block_name, $style_properties ); |
|
999 |
} |
|
1000 |
|
|
1001 |
/** |
|
1002 |
* Unregisters a block style. |
|
1003 |
* |
|
1004 |
* @since 5.3.0 |
|
1005 |
* |
|
1006 |
* @param string $block_name Block type name including namespace. |
18
|
1007 |
* @param string $block_style_name Block style name. |
|
1008 |
* @return bool True if the block style was unregistered with success and false otherwise. |
16
|
1009 |
*/ |
|
1010 |
function unregister_block_style( $block_name, $block_style_name ) { |
|
1011 |
return WP_Block_Styles_Registry::get_instance()->unregister( $block_name, $block_style_name ); |
|
1012 |
} |
18
|
1013 |
|
|
1014 |
/** |
|
1015 |
* Checks whether the current block type supports the feature requested. |
|
1016 |
* |
|
1017 |
* @since 5.8.0 |
|
1018 |
* |
|
1019 |
* @param WP_Block_Type $block_type Block type to check for support. |
|
1020 |
* @param string $feature Name of the feature to check support for. |
19
|
1021 |
* @param mixed $default Optional. Fallback value for feature support. Default false. |
|
1022 |
* @return bool Whether the feature is supported. |
18
|
1023 |
*/ |
|
1024 |
function block_has_support( $block_type, $feature, $default = false ) { |
|
1025 |
$block_support = $default; |
|
1026 |
if ( $block_type && property_exists( $block_type, 'supports' ) ) { |
|
1027 |
$block_support = _wp_array_get( $block_type->supports, $feature, $default ); |
|
1028 |
} |
|
1029 |
|
|
1030 |
return true === $block_support || is_array( $block_support ); |
|
1031 |
} |
|
1032 |
|
|
1033 |
/** |
|
1034 |
* Converts typography keys declared under `supports.*` to `supports.typography.*`. |
|
1035 |
* |
|
1036 |
* Displays a `_doing_it_wrong()` notice when a block using the older format is detected. |
|
1037 |
* |
|
1038 |
* @since 5.8.0 |
|
1039 |
* |
|
1040 |
* @param array $metadata Metadata for registering a block type. |
|
1041 |
* @return array Filtered metadata for registering a block type. |
|
1042 |
*/ |
|
1043 |
function wp_migrate_old_typography_shape( $metadata ) { |
|
1044 |
if ( ! isset( $metadata['supports'] ) ) { |
|
1045 |
return $metadata; |
|
1046 |
} |
|
1047 |
|
|
1048 |
$typography_keys = array( |
|
1049 |
'__experimentalFontFamily', |
|
1050 |
'__experimentalFontStyle', |
|
1051 |
'__experimentalFontWeight', |
|
1052 |
'__experimentalLetterSpacing', |
|
1053 |
'__experimentalTextDecoration', |
|
1054 |
'__experimentalTextTransform', |
|
1055 |
'fontSize', |
|
1056 |
'lineHeight', |
|
1057 |
); |
|
1058 |
|
|
1059 |
foreach ( $typography_keys as $typography_key ) { |
|
1060 |
$support_for_key = _wp_array_get( $metadata['supports'], array( $typography_key ), null ); |
|
1061 |
|
|
1062 |
if ( null !== $support_for_key ) { |
|
1063 |
_doing_it_wrong( |
|
1064 |
'register_block_type_from_metadata()', |
|
1065 |
sprintf( |
|
1066 |
/* translators: 1: Block type, 2: Typography supports key, e.g: fontSize, lineHeight, etc. 3: block.json, 4: Old metadata key, 5: New metadata key. */ |
|
1067 |
__( 'Block "%1$s" is declaring %2$s support in %3$s file under %4$s. %2$s support is now declared under %5$s.' ), |
|
1068 |
$metadata['name'], |
|
1069 |
"<code>$typography_key</code>", |
|
1070 |
'<code>block.json</code>', |
|
1071 |
"<code>supports.$typography_key</code>", |
|
1072 |
"<code>supports.typography.$typography_key</code>" |
|
1073 |
), |
|
1074 |
'5.8.0' |
|
1075 |
); |
|
1076 |
|
|
1077 |
_wp_array_set( $metadata['supports'], array( 'typography', $typography_key ), $support_for_key ); |
|
1078 |
unset( $metadata['supports'][ $typography_key ] ); |
|
1079 |
} |
|
1080 |
} |
|
1081 |
|
|
1082 |
return $metadata; |
|
1083 |
} |
|
1084 |
|
|
1085 |
/** |
|
1086 |
* Helper function that constructs a WP_Query args array from |
|
1087 |
* a `Query` block properties. |
|
1088 |
* |
|
1089 |
* It's used in Query Loop, Query Pagination Numbers and Query Pagination Next blocks. |
|
1090 |
* |
|
1091 |
* @since 5.8.0 |
|
1092 |
* |
|
1093 |
* @param WP_Block $block Block instance. |
|
1094 |
* @param int $page Current query's page. |
|
1095 |
* |
|
1096 |
* @return array Returns the constructed WP_Query arguments. |
|
1097 |
*/ |
|
1098 |
function build_query_vars_from_query_block( $block, $page ) { |
|
1099 |
$query = array( |
|
1100 |
'post_type' => 'post', |
|
1101 |
'order' => 'DESC', |
|
1102 |
'orderby' => 'date', |
|
1103 |
'post__not_in' => array(), |
|
1104 |
); |
|
1105 |
|
|
1106 |
if ( isset( $block->context['query'] ) ) { |
|
1107 |
if ( ! empty( $block->context['query']['postType'] ) ) { |
|
1108 |
$post_type_param = $block->context['query']['postType']; |
|
1109 |
if ( is_post_type_viewable( $post_type_param ) ) { |
|
1110 |
$query['post_type'] = $post_type_param; |
|
1111 |
} |
|
1112 |
} |
|
1113 |
if ( isset( $block->context['query']['sticky'] ) && ! empty( $block->context['query']['sticky'] ) ) { |
|
1114 |
$sticky = get_option( 'sticky_posts' ); |
|
1115 |
if ( 'only' === $block->context['query']['sticky'] ) { |
|
1116 |
$query['post__in'] = $sticky; |
|
1117 |
} else { |
|
1118 |
$query['post__not_in'] = array_merge( $query['post__not_in'], $sticky ); |
|
1119 |
} |
|
1120 |
} |
|
1121 |
if ( ! empty( $block->context['query']['exclude'] ) ) { |
|
1122 |
$excluded_post_ids = array_map( 'intval', $block->context['query']['exclude'] ); |
|
1123 |
$excluded_post_ids = array_filter( $excluded_post_ids ); |
|
1124 |
$query['post__not_in'] = array_merge( $query['post__not_in'], $excluded_post_ids ); |
|
1125 |
} |
|
1126 |
if ( |
|
1127 |
isset( $block->context['query']['perPage'] ) && |
|
1128 |
is_numeric( $block->context['query']['perPage'] ) |
|
1129 |
) { |
|
1130 |
$per_page = absint( $block->context['query']['perPage'] ); |
|
1131 |
$offset = 0; |
|
1132 |
|
|
1133 |
if ( |
|
1134 |
isset( $block->context['query']['offset'] ) && |
|
1135 |
is_numeric( $block->context['query']['offset'] ) |
|
1136 |
) { |
|
1137 |
$offset = absint( $block->context['query']['offset'] ); |
|
1138 |
} |
|
1139 |
|
|
1140 |
$query['offset'] = ( $per_page * ( $page - 1 ) ) + $offset; |
|
1141 |
$query['posts_per_page'] = $per_page; |
|
1142 |
} |
19
|
1143 |
// Migrate `categoryIds` and `tagIds` to `tax_query` for backwards compatibility. |
|
1144 |
if ( ! empty( $block->context['query']['categoryIds'] ) || ! empty( $block->context['query']['tagIds'] ) ) { |
|
1145 |
$tax_query = array(); |
|
1146 |
if ( ! empty( $block->context['query']['categoryIds'] ) ) { |
|
1147 |
$tax_query[] = array( |
|
1148 |
'taxonomy' => 'category', |
|
1149 |
'terms' => array_filter( array_map( 'intval', $block->context['query']['categoryIds'] ) ), |
|
1150 |
'include_children' => false, |
|
1151 |
); |
|
1152 |
} |
|
1153 |
if ( ! empty( $block->context['query']['tagIds'] ) ) { |
|
1154 |
$tax_query[] = array( |
|
1155 |
'taxonomy' => 'post_tag', |
|
1156 |
'terms' => array_filter( array_map( 'intval', $block->context['query']['tagIds'] ) ), |
|
1157 |
'include_children' => false, |
|
1158 |
); |
|
1159 |
} |
|
1160 |
$query['tax_query'] = $tax_query; |
18
|
1161 |
} |
19
|
1162 |
if ( ! empty( $block->context['query']['taxQuery'] ) ) { |
|
1163 |
$query['tax_query'] = array(); |
|
1164 |
foreach ( $block->context['query']['taxQuery'] as $taxonomy => $terms ) { |
|
1165 |
if ( is_taxonomy_viewable( $taxonomy ) && ! empty( $terms ) ) { |
|
1166 |
$query['tax_query'][] = array( |
|
1167 |
'taxonomy' => $taxonomy, |
|
1168 |
'terms' => array_filter( array_map( 'intval', $terms ) ), |
|
1169 |
'include_children' => false, |
|
1170 |
); |
|
1171 |
} |
|
1172 |
} |
18
|
1173 |
} |
|
1174 |
if ( |
|
1175 |
isset( $block->context['query']['order'] ) && |
|
1176 |
in_array( strtoupper( $block->context['query']['order'] ), array( 'ASC', 'DESC' ), true ) |
|
1177 |
) { |
|
1178 |
$query['order'] = strtoupper( $block->context['query']['order'] ); |
|
1179 |
} |
|
1180 |
if ( isset( $block->context['query']['orderBy'] ) ) { |
|
1181 |
$query['orderby'] = $block->context['query']['orderBy']; |
|
1182 |
} |
|
1183 |
if ( |
|
1184 |
isset( $block->context['query']['author'] ) && |
|
1185 |
(int) $block->context['query']['author'] > 0 |
|
1186 |
) { |
|
1187 |
$query['author'] = (int) $block->context['query']['author']; |
|
1188 |
} |
|
1189 |
if ( ! empty( $block->context['query']['search'] ) ) { |
|
1190 |
$query['s'] = $block->context['query']['search']; |
|
1191 |
} |
|
1192 |
} |
|
1193 |
return $query; |
|
1194 |
} |
19
|
1195 |
|
|
1196 |
/** |
|
1197 |
* Helper function that returns the proper pagination arrow HTML for |
|
1198 |
* `QueryPaginationNext` and `QueryPaginationPrevious` blocks based |
|
1199 |
* on the provided `paginationArrow` from `QueryPagination` context. |
|
1200 |
* |
|
1201 |
* It's used in QueryPaginationNext and QueryPaginationPrevious blocks. |
|
1202 |
* |
|
1203 |
* @since 5.9.0 |
|
1204 |
* |
|
1205 |
* @param WP_Block $block Block instance. |
|
1206 |
* @param boolean $is_next Flag for handling `next/previous` blocks. |
|
1207 |
* |
|
1208 |
* @return string|null The pagination arrow HTML or null if there is none. |
|
1209 |
*/ |
|
1210 |
function get_query_pagination_arrow( $block, $is_next ) { |
|
1211 |
$arrow_map = array( |
|
1212 |
'none' => '', |
|
1213 |
'arrow' => array( |
|
1214 |
'next' => '→', |
|
1215 |
'previous' => '←', |
|
1216 |
), |
|
1217 |
'chevron' => array( |
|
1218 |
'next' => '»', |
|
1219 |
'previous' => '«', |
|
1220 |
), |
|
1221 |
); |
|
1222 |
if ( ! empty( $block->context['paginationArrow'] ) && array_key_exists( $block->context['paginationArrow'], $arrow_map ) && ! empty( $arrow_map[ $block->context['paginationArrow'] ] ) ) { |
|
1223 |
$pagination_type = $is_next ? 'next' : 'previous'; |
|
1224 |
$arrow_attribute = $block->context['paginationArrow']; |
|
1225 |
$arrow = $arrow_map[ $block->context['paginationArrow'] ][ $pagination_type ]; |
|
1226 |
$arrow_classes = "wp-block-query-pagination-$pagination_type-arrow is-arrow-$arrow_attribute"; |
|
1227 |
return "<span class='$arrow_classes'>$arrow</span>"; |
|
1228 |
} |
|
1229 |
return null; |
|
1230 |
} |
|
1231 |
|
|
1232 |
/** |
|
1233 |
* Allows multiple block styles. |
|
1234 |
* |
|
1235 |
* @since 5.9.0 |
|
1236 |
* |
|
1237 |
* @param array $metadata Metadata for registering a block type. |
|
1238 |
* @return array Metadata for registering a block type. |
|
1239 |
*/ |
|
1240 |
function _wp_multiple_block_styles( $metadata ) { |
|
1241 |
foreach ( array( 'style', 'editorStyle' ) as $key ) { |
|
1242 |
if ( ! empty( $metadata[ $key ] ) && is_array( $metadata[ $key ] ) ) { |
|
1243 |
$default_style = array_shift( $metadata[ $key ] ); |
|
1244 |
foreach ( $metadata[ $key ] as $handle ) { |
|
1245 |
$args = array( 'handle' => $handle ); |
|
1246 |
if ( 0 === strpos( $handle, 'file:' ) && isset( $metadata['file'] ) ) { |
|
1247 |
$style_path = remove_block_asset_path_prefix( $handle ); |
|
1248 |
$theme_path_norm = wp_normalize_path( get_theme_file_path() ); |
|
1249 |
$style_path_norm = wp_normalize_path( realpath( dirname( $metadata['file'] ) . '/' . $style_path ) ); |
|
1250 |
$is_theme_block = isset( $metadata['file'] ) && 0 === strpos( $metadata['file'], $theme_path_norm ); |
|
1251 |
|
|
1252 |
$style_uri = plugins_url( $style_path, $metadata['file'] ); |
|
1253 |
|
|
1254 |
if ( $is_theme_block ) { |
|
1255 |
$style_uri = get_theme_file_uri( str_replace( $theme_path_norm, '', $style_path_norm ) ); |
|
1256 |
} |
|
1257 |
|
|
1258 |
$args = array( |
|
1259 |
'handle' => sanitize_key( "{$metadata['name']}-{$style_path}" ), |
|
1260 |
'src' => $style_uri, |
|
1261 |
); |
|
1262 |
} |
|
1263 |
|
|
1264 |
wp_enqueue_block_style( $metadata['name'], $args ); |
|
1265 |
} |
|
1266 |
|
|
1267 |
// Only return the 1st item in the array. |
|
1268 |
$metadata[ $key ] = $default_style; |
|
1269 |
} |
|
1270 |
} |
|
1271 |
return $metadata; |
|
1272 |
} |
|
1273 |
add_filter( 'block_type_metadata', '_wp_multiple_block_styles' ); |
|
1274 |
|
|
1275 |
/** |
|
1276 |
* Helper function that constructs a comment query vars array from the passed |
|
1277 |
* block properties. |
|
1278 |
* |
|
1279 |
* It's used with the Comment Query Loop inner blocks. |
|
1280 |
* |
|
1281 |
* @since 6.0.0 |
|
1282 |
* |
|
1283 |
* @param WP_Block $block Block instance. |
|
1284 |
* |
|
1285 |
* @return array Returns the comment query parameters to use with the |
|
1286 |
* WP_Comment_Query constructor. |
|
1287 |
*/ |
|
1288 |
function build_comment_query_vars_from_block( $block ) { |
|
1289 |
|
|
1290 |
$comment_args = array( |
|
1291 |
'orderby' => 'comment_date_gmt', |
|
1292 |
'order' => 'ASC', |
|
1293 |
'status' => 'approve', |
|
1294 |
'no_found_rows' => false, |
|
1295 |
); |
|
1296 |
|
|
1297 |
if ( is_user_logged_in() ) { |
|
1298 |
$comment_args['include_unapproved'] = array( get_current_user_id() ); |
|
1299 |
} else { |
|
1300 |
$unapproved_email = wp_get_unapproved_comment_author_email(); |
|
1301 |
|
|
1302 |
if ( $unapproved_email ) { |
|
1303 |
$comment_args['include_unapproved'] = array( $unapproved_email ); |
|
1304 |
} |
|
1305 |
} |
|
1306 |
|
|
1307 |
if ( ! empty( $block->context['postId'] ) ) { |
|
1308 |
$comment_args['post_id'] = (int) $block->context['postId']; |
|
1309 |
} |
|
1310 |
|
|
1311 |
if ( get_option( 'thread_comments' ) ) { |
|
1312 |
$comment_args['hierarchical'] = 'threaded'; |
|
1313 |
} else { |
|
1314 |
$comment_args['hierarchical'] = false; |
|
1315 |
} |
|
1316 |
|
|
1317 |
if ( get_option( 'page_comments' ) === '1' || get_option( 'page_comments' ) === true ) { |
|
1318 |
$per_page = get_option( 'comments_per_page' ); |
|
1319 |
$default_page = get_option( 'default_comments_page' ); |
|
1320 |
if ( $per_page > 0 ) { |
|
1321 |
$comment_args['number'] = $per_page; |
|
1322 |
|
|
1323 |
$page = (int) get_query_var( 'cpage' ); |
|
1324 |
if ( $page ) { |
|
1325 |
$comment_args['paged'] = $page; |
|
1326 |
} elseif ( 'oldest' === $default_page ) { |
|
1327 |
$comment_args['paged'] = 1; |
|
1328 |
} elseif ( 'newest' === $default_page ) { |
|
1329 |
$max_num_pages = (int) ( new WP_Comment_Query( $comment_args ) )->max_num_pages; |
|
1330 |
if ( 0 !== $max_num_pages ) { |
|
1331 |
$comment_args['paged'] = $max_num_pages; |
|
1332 |
} |
|
1333 |
} |
|
1334 |
// Set the `cpage` query var to ensure the previous and next pagination links are correct |
|
1335 |
// when inheriting the Discussion Settings. |
|
1336 |
if ( 0 === $page && isset( $comment_args['paged'] ) && $comment_args['paged'] > 0 ) { |
|
1337 |
set_query_var( 'cpage', $comment_args['paged'] ); |
|
1338 |
} |
|
1339 |
} |
|
1340 |
} |
|
1341 |
|
|
1342 |
return $comment_args; |
|
1343 |
} |
|
1344 |
|
|
1345 |
/** |
|
1346 |
* Helper function that returns the proper pagination arrow HTML for |
|
1347 |
* `CommentsPaginationNext` and `CommentsPaginationPrevious` blocks based on the |
|
1348 |
* provided `paginationArrow` from `CommentsPagination` context. |
|
1349 |
* |
|
1350 |
* It's used in CommentsPaginationNext and CommentsPaginationPrevious blocks. |
|
1351 |
* |
|
1352 |
* @since 6.0.0 |
|
1353 |
* |
|
1354 |
* @param WP_Block $block Block instance. |
|
1355 |
* @param string $pagination_type Type of the arrow we will be rendering. |
|
1356 |
* Default 'next'. Accepts 'next' or 'previous'. |
|
1357 |
* |
|
1358 |
* @return string|null The pagination arrow HTML or null if there is none. |
|
1359 |
*/ |
|
1360 |
function get_comments_pagination_arrow( $block, $pagination_type = 'next' ) { |
|
1361 |
$arrow_map = array( |
|
1362 |
'none' => '', |
|
1363 |
'arrow' => array( |
|
1364 |
'next' => '→', |
|
1365 |
'previous' => '←', |
|
1366 |
), |
|
1367 |
'chevron' => array( |
|
1368 |
'next' => '»', |
|
1369 |
'previous' => '«', |
|
1370 |
), |
|
1371 |
); |
|
1372 |
if ( ! empty( $block->context['comments/paginationArrow'] ) && ! empty( $arrow_map[ $block->context['comments/paginationArrow'] ][ $pagination_type ] ) ) { |
|
1373 |
$arrow_attribute = $block->context['comments/paginationArrow']; |
|
1374 |
$arrow = $arrow_map[ $block->context['comments/paginationArrow'] ][ $pagination_type ]; |
|
1375 |
$arrow_classes = "wp-block-comments-pagination-$pagination_type-arrow is-arrow-$arrow_attribute"; |
|
1376 |
return "<span class='$arrow_classes'>$arrow</span>"; |
|
1377 |
} |
|
1378 |
return null; |
|
1379 |
} |