PluginProbe
Gutenberg / 9.5.2
Gutenberg v9.5.2
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 / templates.php

templates.php in Gutenberg 9.5.2, at lib/templates.php

286 lines 10.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Block template functions.
4 *
5 * @package gutenberg
6 */
7
8 /**
9 * Returns all block template file path of the current theme and its parent theme.
10 * Includes demo block template files if demo experiment is enabled.
11 *
12 * @return array $block_template_files A list of paths to all template files.
13 */
14 function gutenberg_get_template_paths() {
15 $block_template_files = glob( get_stylesheet_directory() . '/block-templates/*.html' );
16 $block_template_files = is_array( $block_template_files ) ? $block_template_files : array();
17
18 if ( is_child_theme() ) {
19 $child_block_template_files = glob( get_template_directory() . '/block-templates/*.html' );
20 $child_block_template_files = is_array( $child_block_template_files ) ? $child_block_template_files : array();
21 $block_template_files = array_merge( $block_template_files, $child_block_template_files );
22 }
23
24 return $block_template_files;
25 }
26
27 /**
28 * Registers block editor 'wp_template' post type.
29 */
30 function gutenberg_register_template_post_type() {
31 if ( ! gutenberg_is_fse_theme() ) {
32 return;
33 }
34
35 $labels = array(
36 'name' => __( 'Templates', 'gutenberg' ),
37 'singular_name' => __( 'Template', 'gutenberg' ),
38 'menu_name' => _x( 'Templates', 'Admin Menu text', 'gutenberg' ),
39 'add_new' => _x( 'Add New', 'Template', 'gutenberg' ),
40 'add_new_item' => __( 'Add New Template', 'gutenberg' ),
41 'new_item' => __( 'New Template', 'gutenberg' ),
42 'edit_item' => __( 'Edit Template', 'gutenberg' ),
43 'view_item' => __( 'View Template', 'gutenberg' ),
44 'all_items' => __( 'All Templates', 'gutenberg' ),
45 'search_items' => __( 'Search Templates', 'gutenberg' ),
46 'parent_item_colon' => __( 'Parent Template:', 'gutenberg' ),
47 'not_found' => __( 'No templates found.', 'gutenberg' ),
48 'not_found_in_trash' => __( 'No templates found in Trash.', 'gutenberg' ),
49 'archives' => __( 'Template archives', 'gutenberg' ),
50 'insert_into_item' => __( 'Insert into template', 'gutenberg' ),
51 'uploaded_to_this_item' => __( 'Uploaded to this template', 'gutenberg' ),
52 'filter_items_list' => __( 'Filter templates list', 'gutenberg' ),
53 'items_list_navigation' => __( 'Templates list navigation', 'gutenberg' ),
54 'items_list' => __( 'Templates list', 'gutenberg' ),
55 );
56
57 $args = array(
58 'labels' => $labels,
59 'description' => __( 'Templates to include in your theme.', 'gutenberg' ),
60 'public' => false,
61 'has_archive' => false,
62 'show_ui' => true,
63 'show_in_menu' => 'themes.php',
64 'show_in_admin_bar' => false,
65 'show_in_rest' => true,
66 'rest_base' => 'templates',
67 'capability_type' => array( 'template', 'templates' ),
68 'map_meta_cap' => true,
69 'supports' => array(
70 'title',
71 'slug',
72 'excerpt',
73 'editor',
74 'revisions',
75 ),
76 );
77
78 register_post_type( 'wp_template', $args );
79 }
80 add_action( 'init', 'gutenberg_register_template_post_type' );
81
82 /**
83 * Registers block editor 'wp_theme' taxonomy.
84 */
85 function gutenberg_register_wp_theme_taxonomy() {
86 if ( ! gutenberg_is_fse_theme() ) {
87 return;
88 }
89
90 register_taxonomy(
91 'wp_theme',
92 array( 'wp_template', 'wp_template_part' ),
93 array(
94 'public' => false,
95 'hierarchical' => false,
96 'labels' => array(
97 'name' => __( 'Themes', 'gutenberg' ),
98 'singular_name' => __( 'Theme', 'gutenberg' ),
99 ),
100 'query_var' => false,
101 'rewrite' => false,
102 'show_ui' => false,
103 '_builtin' => true,
104 'show_in_nav_menus' => false,
105 'show_in_rest' => true,
106 )
107 );
108 }
109 add_action( 'init', 'gutenberg_register_wp_theme_taxonomy' );
110
111 /**
112 * Automatically set the theme meta for templates.
113 *
114 * @param array $post_id Template ID.
115 */
116 function gutenberg_set_template_and_template_part_post_theme( $post_id ) {
117 $themes = wp_get_post_terms( $post_id, 'wp_theme' );
118 if ( ! $themes ) {
119 wp_set_post_terms( $post_id, array( wp_get_theme()->get_stylesheet() ), 'wp_theme', true );
120 }
121 }
122 add_action( 'save_post_wp_template', 'gutenberg_set_template_and_template_part_post_theme', 10, 3 );
123 add_action( 'save_post_wp_template_part', 'gutenberg_set_template_and_template_part_post_theme', 10, 3 );
124
125 /**
126 * Filters the capabilities of a user to conditionally grant them capabilities for managing 'wp_template' posts.
127 *
128 * Any user who can 'edit_theme_options' will have access.
129 *
130 * @param array $allcaps A user's capabilities.
131 * @return array Filtered $allcaps.
132 */
133 function gutenberg_grant_template_caps( array $allcaps ) {
134 if ( isset( $allcaps['edit_theme_options'] ) ) {
135 $allcaps['edit_templates'] = $allcaps['edit_theme_options'];
136 $allcaps['edit_others_templates'] = $allcaps['edit_theme_options'];
137 $allcaps['edit_published_templates'] = $allcaps['edit_theme_options'];
138 $allcaps['edit_private_templates'] = $allcaps['edit_theme_options'];
139 $allcaps['delete_templates'] = $allcaps['edit_theme_options'];
140 $allcaps['delete_others_templates'] = $allcaps['edit_theme_options'];
141 $allcaps['delete_published_templates'] = $allcaps['edit_theme_options'];
142 $allcaps['delete_private_templates'] = $allcaps['edit_theme_options'];
143 $allcaps['publish_templates'] = $allcaps['edit_theme_options'];
144 $allcaps['read_private_templates'] = $allcaps['edit_theme_options'];
145 }
146
147 return $allcaps;
148 }
149 add_filter( 'user_has_cap', 'gutenberg_grant_template_caps' );
150
151 /**
152 * Filters `wp_template` posts slug resolution to bypass deduplication logic as
153 * template slugs should be unique.
154 *
155 * @param string $slug The resolved slug (post_name).
156 * @param int $post_ID Post ID.
157 * @param string $post_status No uniqueness checks are made if the post is still draft or pending.
158 * @param string $post_type Post type.
159 * @param int $post_parent Post parent ID.
160 * @param int $original_slug The desired slug (post_name).
161 * @return string The original, desired slug.
162 */
163 function gutenberg_filter_wp_template_wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug ) {
164 if ( 'wp_template' === $post_type ) {
165 return $original_slug;
166 }
167 return $slug;
168 }
169 add_filter( 'wp_unique_post_slug', 'gutenberg_filter_wp_template_wp_unique_post_slug', 10, 6 );
170
171 /**
172 * Fixes the label of the 'wp_template' admin menu entry.
173 */
174 function gutenberg_fix_template_admin_menu_entry() {
175 if ( ! gutenberg_is_fse_theme() ) {
176 return;
177 }
178 global $submenu;
179 if ( ! isset( $submenu['themes.php'] ) ) {
180 return;
181 }
182 $post_type = get_post_type_object( 'wp_template' );
183 if ( ! $post_type ) {
184 return;
185 }
186 foreach ( $submenu['themes.php'] as $key => $submenu_entry ) {
187 if ( $post_type->labels->all_items === $submenu['themes.php'][ $key ][0] ) {
188 $submenu['themes.php'][ $key ][0] = $post_type->labels->menu_name; // phpcs:ignore WordPress.WP.GlobalVariablesOverride
189 break;
190 }
191 }
192 }
193 add_action( 'admin_menu', 'gutenberg_fix_template_admin_menu_entry' );
194
195 // Customize the `wp_template` admin list.
196 add_filter( 'manage_wp_template_posts_columns', 'gutenberg_templates_lists_custom_columns' );
197 add_action( 'manage_wp_template_posts_custom_column', 'gutenberg_render_templates_lists_custom_column', 10, 2 );
198 add_filter( 'views_edit-wp_template', 'gutenberg_filter_templates_edit_views' );
199
200 /**
201 * Filter for adding a `resolved` parameter to `wp_template` queries.
202 *
203 * @param array $query_params The query parameters.
204 * @return array Filtered $query_params.
205 */
206 function filter_rest_wp_template_collection_params( $query_params ) {
207 $query_params += array(
208 'resolved' => array(
209 'description' => __( 'Whether to filter for resolved templates', 'gutenberg' ),
210 'type' => 'boolean',
211 ),
212 );
213 return $query_params;
214 }
215 apply_filters( 'rest_wp_template_collection_params', 'filter_rest_wp_template_collection_params', 99, 1 );
216
217 /**
218 * Filter for supporting the `resolved` parameter in `wp_template` queries.
219 *
220 * @param array $args The query arguments.
221 * @param WP_REST_Request $request The request object.
222 * @return array Filtered $args.
223 */
224 function filter_rest_wp_template_query( $args, $request ) {
225 if ( $request['resolved'] ) {
226 $template_ids = array( 0 ); // Return nothing by default (the 0 is needed for `post__in`).
227 $template_types = $request['slug'] ? $request['slug'] : gutenberg_get_template_type_slugs();
228
229 foreach ( $template_types as $template_type ) {
230 // Skip 'embed' for now because it is not a regular template type.
231 if ( in_array( $template_type, array( 'embed' ), true ) ) {
232 continue;
233 }
234
235 $current_template = gutenberg_resolve_template( $template_type );
236 if ( $current_template ) {
237 $template_ids[] = $current_template->ID;
238 }
239 }
240 $args['post__in'] = $template_ids;
241 $args['post_status'] = array( 'publish', 'auto-draft' );
242 }
243
244 return $args;
245 }
246 add_filter( 'rest_wp_template_query', 'filter_rest_wp_template_query', 99, 2 );
247
248 /**
249 * Filters the post data for a response.
250 *
251 * @param WP_REST_Response $response The response object.
252 * @return WP_REST_Response
253 */
254 function filter_rest_prepare_add_wp_theme_slug_and_file_based( $response ) {
255 if ( isset( $response->data ) && is_array( $response->data ) && isset( $response->data['id'] ) ) {
256 $response->data['wp_theme_slug'] = false;
257
258 // Get the wp_theme terms.
259 $wp_themes = wp_get_post_terms( $response->data['id'], 'wp_theme' );
260
261 // If a theme is assigned, add it to the REST response.
262 if ( $wp_themes && is_array( $wp_themes ) ) {
263 $wp_theme_slugs = array_column( $wp_themes, 'slug' );
264
265 $file_based = in_array( '_wp_file_based', $wp_theme_slugs, true );
266 $response->data['file_based'] = $file_based;
267
268 $theme_slug = array_values(
269 array_filter(
270 $wp_theme_slugs,
271 function( $slug ) {
272 return '_wp_file_based' !== $slug;
273 }
274 )
275 );
276 if ( $theme_slug ) {
277 $response->data['wp_theme_slug'] = $theme_slug[0];
278 }
279 }
280 }
281
282 return $response;
283 }
284 add_filter( 'rest_prepare_wp_template', 'filter_rest_prepare_add_wp_theme_slug_and_file_based' );
285 add_filter( 'rest_prepare_wp_template_part', 'filter_rest_prepare_add_wp_theme_slug_and_file_based' );
286