PluginProbe
Gutenberg / 19.6.1
Gutenberg v19.6.1
24.0.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 All 403 releases
gutenberg / lib / compat / wordpress-6.7 / compat.php

compat.php in Gutenberg 19.6.1, at lib/compat/wordpress-6.7/compat.php

121 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WordPress 6.7 compatibility functions.
4 *
5 * @package gutenberg
6 */
7
8 /**
9 * Hooks into `get_block_templates` so templates from the registry are also returned, as long as there
10 * isn't a theme template with the same slug.
11 *
12 * @param WP_Block_Template[] $query_result Array of found block templates.
13 * @param array $query {
14 * Arguments to retrieve templates. All arguments are optional.
15 *
16 * @type string[] $slug__in List of slugs to include.
17 * @type int $wp_id Post ID of customized template.
18 * @type string $area A 'wp_template_part_area' taxonomy value to filter by (for 'wp_template_part' template type only).
19 * @type string $post_type Post type to get the templates for.
20 * }
21 * @param string $template_type wp_template or wp_template_part.
22 * @return WP_Block_Template[] The same $query_result but might contain some additional templates from
23 * the registry.
24 */
25 function _gutenberg_add_block_templates_from_registry( $query_result, $query, $template_type ) {
26 // Add `plugin` property to templates registered by a plugin.
27 foreach ( $query_result as $key => $value ) {
28 $registered_template = WP_Block_Templates_Registry::get_instance()->get_by_slug( $query_result[ $key ]->slug );
29 if ( $registered_template ) {
30 $query_result[ $key ]->plugin = $registered_template->plugin;
31 $query_result[ $key ]->origin =
32 'theme' !== $query_result[ $key ]->origin && 'theme' !== $query_result[ $key ]->source ?
33 'plugin' :
34 $query_result[ $key ]->origin;
35 $query_result[ $key ]->title =
36 empty( $query_result[ $key ]->title ) || $query_result[ $key ]->title === $query_result[ $key ]->slug ?
37 $registered_template->title : $query_result[ $key ]->title;
38 $query_result[ $key ]->description = empty( $query_result[ $key ]->description ) ? $registered_template->description : $query_result[ $key ]->description;
39 }
40 }
41
42 if ( ! isset( $query['wp_id'] ) ) {
43 $template_files = _gutenberg_get_block_templates_files( $template_type, $query );
44
45 /*
46 * Add templates registered in the template registry. Filtering out the ones which have a theme file.
47 */
48 $registered_templates = WP_Block_Templates_Registry::get_instance()->get_by_query( $query );
49 $matching_registered_templates = array_filter(
50 $registered_templates,
51 function ( $registered_template ) use ( $template_files ) {
52 foreach ( $template_files as $template_file ) {
53 if ( $template_file['slug'] === $registered_template->slug ) {
54 return false;
55 }
56 }
57 return true;
58 }
59 );
60 $query_result = array_merge( $query_result, $matching_registered_templates );
61 }
62
63 return $query_result;
64 }
65 add_filter( 'get_block_templates', '_gutenberg_add_block_templates_from_registry', 10, 3 );
66
67 /**
68 * Hooks into `get_block_template` to add the `plugin` property when necessary.
69 *
70 * @param [WP_Block_Template|null] $block_template The found block template, or null if there isn’t one.
71 * @return [WP_Block_Template|null] The block template that was already found with the plugin property defined if it was registered by a plugin.
72 */
73 function _gutenberg_add_block_template_plugin_attribute( $block_template ) {
74 if ( $block_template ) {
75 $registered_template = WP_Block_Templates_Registry::get_instance()->get_by_slug( $block_template->slug );
76 if ( $registered_template ) {
77 $block_template->plugin = $registered_template->plugin;
78 $block_template->origin =
79 'theme' !== $block_template->origin && 'theme' !== $block_template->source ?
80 'plugin' :
81 $block_template->origin;
82 $block_template->title = empty( $block_template->title ) || $block_template->title === $block_template->slug ? $registered_template->title : $block_template->title;
83 $block_template->description = empty( $block_template->description ) ? $registered_template->description : $block_template->description;
84 }
85 }
86
87 return $block_template;
88 }
89 add_filter( 'get_block_template', '_gutenberg_add_block_template_plugin_attribute', 10, 1 );
90
91 /**
92 * Hooks into `get_block_file_template` so templates from the registry are also returned.
93 *
94 * @param WP_Block_Template|null $block_template The found block template, or null if there is none.
95 * @param string $id Template unique identifier (example: 'theme_slug//template_slug').
96 * @return WP_Block_Template|null The block template that was already found or from the registry. In case the template was already found, add the necessary details from the registry.
97 */
98 function _gutenberg_add_block_file_templates_from_registry( $block_template, $id ) {
99 if ( $block_template ) {
100 $registered_template = WP_Block_Templates_Registry::get_instance()->get_by_slug( $block_template->slug );
101 if ( $registered_template ) {
102 $block_template->plugin = $registered_template->plugin;
103 $block_template->origin =
104 'theme' !== $block_template->origin && 'theme' !== $block_template->source ?
105 'plugin' :
106 $block_template->origin;
107 }
108 return $block_template;
109 }
110
111 $parts = explode( '//', $id, 2 );
112
113 if ( count( $parts ) < 2 ) {
114 return $block_template;
115 }
116
117 list( , $slug ) = $parts;
118 return WP_Block_Templates_Registry::get_instance()->get_by_slug( $slug );
119 }
120 add_filter( 'get_block_file_template', '_gutenberg_add_block_file_templates_from_registry', 10, 2 );
121