PluginProbe
Gutenberg / 9.4.0
Gutenberg v9.4.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 / templates.php

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

261 lines 9.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 'editor',
73 'revisions',
74 ),
75 );
76
77 register_post_type( 'wp_template', $args );
78
79 $meta_args = array(
80 'object_subtype' => 'wp_template',
81 'type' => 'string',
82 'description' => 'The theme that provided the template, if any.',
83 'single' => true,
84 'show_in_rest' => true,
85 );
86
87 register_post_type( 'wp_template', $args );
88 register_meta( 'post', 'theme', $meta_args );
89 }
90 add_action( 'init', 'gutenberg_register_template_post_type' );
91
92 /**
93 * Automatically set the theme meta for templates.
94 *
95 * @param array $post_id Template ID.
96 * @param array $post Template Post.
97 * @param bool $update Is update.
98 */
99 function gutenberg_set_template_post_theme( $post_id, $post, $update ) {
100 if ( 'wp_template' !== $post->post_type || $update || 'trash' === $post->post_status ) {
101 return;
102 }
103
104 $theme = get_post_meta( $post_id, 'theme', true );
105
106 if ( ! $theme ) {
107 update_post_meta( $post_id, 'theme', wp_get_theme()->get_stylesheet() );
108 }
109 }
110
111 add_action( 'save_post', 'gutenberg_set_template_post_theme', 10, 3 );
112
113 /**
114 * Filters the capabilities of a user to conditionally grant them capabilities for managing 'wp_template' posts.
115 *
116 * Any user who can 'edit_theme_options' will have access.
117 *
118 * @param array $allcaps A user's capabilities.
119 * @return array Filtered $allcaps.
120 */
121 function gutenberg_grant_template_caps( array $allcaps ) {
122 if ( isset( $allcaps['edit_theme_options'] ) ) {
123 $allcaps['edit_templates'] = $allcaps['edit_theme_options'];
124 $allcaps['edit_others_templates'] = $allcaps['edit_theme_options'];
125 $allcaps['edit_published_templates'] = $allcaps['edit_theme_options'];
126 $allcaps['edit_private_templates'] = $allcaps['edit_theme_options'];
127 $allcaps['delete_templates'] = $allcaps['edit_theme_options'];
128 $allcaps['delete_others_templates'] = $allcaps['edit_theme_options'];
129 $allcaps['delete_published_templates'] = $allcaps['edit_theme_options'];
130 $allcaps['delete_private_templates'] = $allcaps['edit_theme_options'];
131 $allcaps['publish_templates'] = $allcaps['edit_theme_options'];
132 $allcaps['read_private_templates'] = $allcaps['edit_theme_options'];
133 }
134
135 return $allcaps;
136 }
137 add_filter( 'user_has_cap', 'gutenberg_grant_template_caps' );
138
139 /**
140 * Filters `wp_template` posts slug resolution to bypass deduplication logic as
141 * template slugs should be unique.
142 *
143 * @param string $slug The resolved slug (post_name).
144 * @param int $post_ID Post ID.
145 * @param string $post_status No uniqueness checks are made if the post is still draft or pending.
146 * @param string $post_type Post type.
147 * @param int $post_parent Post parent ID.
148 * @param int $original_slug The desired slug (post_name).
149 * @return string The original, desired slug.
150 */
151 function gutenberg_filter_wp_template_wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug ) {
152 if ( 'wp_template' === $post_type ) {
153 return $original_slug;
154 }
155 return $slug;
156 }
157 add_filter( 'wp_unique_post_slug', 'gutenberg_filter_wp_template_wp_unique_post_slug', 10, 6 );
158
159 /**
160 * Fixes the label of the 'wp_template' admin menu entry.
161 */
162 function gutenberg_fix_template_admin_menu_entry() {
163 if ( ! gutenberg_is_fse_theme() ) {
164 return;
165 }
166 global $submenu;
167 if ( ! isset( $submenu['themes.php'] ) ) {
168 return;
169 }
170 $post_type = get_post_type_object( 'wp_template' );
171 if ( ! $post_type ) {
172 return;
173 }
174 foreach ( $submenu['themes.php'] as $key => $submenu_entry ) {
175 if ( $post_type->labels->all_items === $submenu['themes.php'][ $key ][0] ) {
176 $submenu['themes.php'][ $key ][0] = $post_type->labels->menu_name; // phpcs:ignore WordPress.WP.GlobalVariablesOverride
177 break;
178 }
179 }
180 }
181 add_action( 'admin_menu', 'gutenberg_fix_template_admin_menu_entry' );
182
183 /**
184 * Filters the 'wp_template' post type columns in the admin list table.
185 *
186 * @param array $columns Columns to display.
187 * @return array Filtered $columns.
188 */
189 function gutenberg_filter_template_list_table_columns( array $columns ) {
190 $columns['slug'] = __( 'Slug', 'gutenberg' );
191 if ( isset( $columns['date'] ) ) {
192 unset( $columns['date'] );
193 }
194 return $columns;
195 }
196 add_filter( 'manage_wp_template_posts_columns', 'gutenberg_filter_template_list_table_columns' );
197
198 /**
199 * Renders column content for the 'wp_template' post type list table.
200 *
201 * @param string $column_name Column name to render.
202 * @param int $post_id Post ID.
203 */
204 function gutenberg_render_template_list_table_column( $column_name, $post_id ) {
205 if ( 'slug' !== $column_name ) {
206 return;
207 }
208 $post = get_post( $post_id );
209 echo esc_html( $post->post_name );
210 }
211 add_action( 'manage_wp_template_posts_custom_column', 'gutenberg_render_template_list_table_column', 10, 2 );
212
213 /**
214 * Filter for adding a `resolved` parameter to `wp_template` queries.
215 *
216 * @param array $query_params The query parameters.
217 * @return array Filtered $query_params.
218 */
219 function filter_rest_wp_template_collection_params( $query_params ) {
220 $query_params += array(
221 'resolved' => array(
222 'description' => __( 'Whether to filter for resolved templates', 'gutenberg' ),
223 'type' => 'boolean',
224 ),
225 );
226 return $query_params;
227 }
228 apply_filters( 'rest_wp_template_collection_params', 'filter_rest_wp_template_collection_params', 99, 1 );
229
230 /**
231 * Filter for supporting the `resolved` parameter in `wp_template` queries.
232 *
233 * @param array $args The query arguments.
234 * @param WP_REST_Request $request The request object.
235 * @return array Filtered $args.
236 */
237 function filter_rest_wp_template_query( $args, $request ) {
238 if ( $request['resolved'] ) {
239 $template_ids = array( 0 ); // Return nothing by default (the 0 is needed for `post__in`).
240 $template_types = $request['slug'] ? $request['slug'] : get_template_types();
241
242 foreach ( $template_types as $template_type ) {
243 // Skip 'embed' for now because it is not a regular template type.
244 if ( in_array( $template_type, array( 'embed' ), true ) ) {
245 continue;
246 }
247
248 $current_template = gutenberg_resolve_template( $template_type );
249 if ( $current_template ) {
250 $template_ids[] = $current_template->ID;
251 }
252 }
253 $args['post__in'] = $template_ids;
254 $args['post_status'] = array( 'publish', 'auto-draft' );
255 }
256
257 return $args;
258 }
259 add_filter( 'rest_wp_template_query', 'filter_rest_wp_template_query', 99, 2 );
260
261