PluginProbe
Gutenberg / 8.6.1
Gutenberg v8.6.1
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 8.6.1, at lib/templates.php

206 lines 7.4 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 * Registers block editor 'wp_template' post type.
10 */
11 function gutenberg_register_template_post_type() {
12 if ( ! gutenberg_is_experiment_enabled( 'gutenberg-full-site-editing' ) ) {
13 return;
14 }
15
16 $labels = array(
17 'name' => __( 'Templates', 'gutenberg' ),
18 'singular_name' => __( 'Template', 'gutenberg' ),
19 'menu_name' => _x( 'Templates', 'Admin Menu text', 'gutenberg' ),
20 'add_new' => _x( 'Add New', 'Template', 'gutenberg' ),
21 'add_new_item' => __( 'Add New Template', 'gutenberg' ),
22 'new_item' => __( 'New Template', 'gutenberg' ),
23 'edit_item' => __( 'Edit Template', 'gutenberg' ),
24 'view_item' => __( 'View Template', 'gutenberg' ),
25 'all_items' => __( 'All Templates', 'gutenberg' ),
26 'search_items' => __( 'Search Templates', 'gutenberg' ),
27 'parent_item_colon' => __( 'Parent Template:', 'gutenberg' ),
28 'not_found' => __( 'No templates found.', 'gutenberg' ),
29 'not_found_in_trash' => __( 'No templates found in Trash.', 'gutenberg' ),
30 'archives' => __( 'Template archives', 'gutenberg' ),
31 'insert_into_item' => __( 'Insert into template', 'gutenberg' ),
32 'uploaded_to_this_item' => __( 'Uploaded to this template', 'gutenberg' ),
33 'filter_items_list' => __( 'Filter templates list', 'gutenberg' ),
34 'items_list_navigation' => __( 'Templates list navigation', 'gutenberg' ),
35 'items_list' => __( 'Templates list', 'gutenberg' ),
36 );
37
38 $args = array(
39 'labels' => $labels,
40 'description' => __( 'Templates to include in your theme.', 'gutenberg' ),
41 'public' => false,
42 'has_archive' => false,
43 'show_ui' => true,
44 'show_in_menu' => 'themes.php',
45 'show_in_admin_bar' => false,
46 'show_in_rest' => true,
47 'rest_base' => 'templates',
48 'capability_type' => array( 'template', 'templates' ),
49 'map_meta_cap' => true,
50 'supports' => array(
51 'title',
52 'slug',
53 'editor',
54 'revisions',
55 ),
56 );
57
58 register_post_type( 'wp_template', $args );
59 }
60 add_action( 'init', 'gutenberg_register_template_post_type' );
61
62 /**
63 * Filters the capabilities of a user to conditionally grant them capabilities for managing 'wp_template' posts.
64 *
65 * Any user who can 'edit_theme_options' will have access.
66 *
67 * @param array $allcaps A user's capabilities.
68 * @return array Filtered $allcaps.
69 */
70 function gutenberg_grant_template_caps( array $allcaps ) {
71 if ( isset( $allcaps['edit_theme_options'] ) ) {
72 $allcaps['edit_templates'] = $allcaps['edit_theme_options'];
73 $allcaps['edit_others_templates'] = $allcaps['edit_theme_options'];
74 $allcaps['edit_published_templates'] = $allcaps['edit_theme_options'];
75 $allcaps['edit_private_templates'] = $allcaps['edit_theme_options'];
76 $allcaps['delete_templates'] = $allcaps['edit_theme_options'];
77 $allcaps['delete_others_templates'] = $allcaps['edit_theme_options'];
78 $allcaps['delete_published_templates'] = $allcaps['edit_theme_options'];
79 $allcaps['delete_private_templates'] = $allcaps['edit_theme_options'];
80 $allcaps['publish_templates'] = $allcaps['edit_theme_options'];
81 $allcaps['read_private_templates'] = $allcaps['edit_theme_options'];
82 }
83
84 return $allcaps;
85 }
86 add_filter( 'user_has_cap', 'gutenberg_grant_template_caps' );
87
88 /**
89 * Filters `wp_template` posts slug resolution to bypass deduplication logic as
90 * template slugs should be unique.
91 *
92 * @param string $slug The resolved slug (post_name).
93 * @param int $post_ID Post ID.
94 * @param string $post_status No uniqueness checks are made if the post is still draft or pending.
95 * @param string $post_type Post type.
96 * @param int $post_parent Post parent ID.
97 * @param int $original_slug The desired slug (post_name).
98 * @return string The original, desired slug.
99 */
100 function gutenberg_filter_wp_template_wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug ) {
101 if ( 'wp_template' === $post_type ) {
102 return $original_slug;
103 }
104 return $slug;
105 }
106 add_filter( 'wp_unique_post_slug', 'gutenberg_filter_wp_template_wp_unique_post_slug', 10, 6 );
107
108 /**
109 * Fixes the label of the 'wp_template' admin menu entry.
110 */
111 function gutenberg_fix_template_admin_menu_entry() {
112 global $submenu;
113 if ( ! isset( $submenu['themes.php'] ) ) {
114 return;
115 }
116 $post_type = get_post_type_object( 'wp_template' );
117 if ( ! $post_type ) {
118 return;
119 }
120 foreach ( $submenu['themes.php'] as $key => $submenu_entry ) {
121 if ( $post_type->labels->all_items === $submenu['themes.php'][ $key ][0] ) {
122 $submenu['themes.php'][ $key ][0] = $post_type->labels->menu_name; // phpcs:ignore WordPress.WP.GlobalVariablesOverride
123 break;
124 }
125 }
126 }
127 add_action( 'admin_menu', 'gutenberg_fix_template_admin_menu_entry' );
128
129 /**
130 * Filters the 'wp_template' post type columns in the admin list table.
131 *
132 * @param array $columns Columns to display.
133 * @return array Filtered $columns.
134 */
135 function gutenberg_filter_template_list_table_columns( array $columns ) {
136 $columns['slug'] = __( 'Slug', 'gutenberg' );
137 if ( isset( $columns['date'] ) ) {
138 unset( $columns['date'] );
139 }
140 return $columns;
141 }
142 add_filter( 'manage_wp_template_posts_columns', 'gutenberg_filter_template_list_table_columns' );
143
144 /**
145 * Renders column content for the 'wp_template' post type list table.
146 *
147 * @param string $column_name Column name to render.
148 * @param int $post_id Post ID.
149 */
150 function gutenberg_render_template_list_table_column( $column_name, $post_id ) {
151 if ( 'slug' !== $column_name ) {
152 return;
153 }
154 $post = get_post( $post_id );
155 echo esc_html( $post->post_name );
156 }
157 add_action( 'manage_wp_template_posts_custom_column', 'gutenberg_render_template_list_table_column', 10, 2 );
158
159 /**
160 * Filter for adding a `resolved` parameter to `wp_template` queries.
161 *
162 * @param array $query_params The query parameters.
163 * @return array Filtered $query_params.
164 */
165 function filter_rest_wp_template_collection_params( $query_params ) {
166 $query_params += array(
167 'resolved' => array(
168 'description' => __( 'Whether to filter for resolved templates', 'gutenberg' ),
169 'type' => 'boolean',
170 ),
171 );
172 return $query_params;
173 }
174 apply_filters( 'rest_wp_template_collection_params', 'filter_rest_wp_template_collection_params', 99, 1 );
175
176 /**
177 * Filter for supporting the `resolved` parameter in `wp_template` queries.
178 *
179 * @param array $args The query arguments.
180 * @param WP_REST_Request $request The request object.
181 * @return array Filtered $args.
182 */
183 function filter_rest_wp_template_query( $args, $request ) {
184 if ( $request['resolved'] ) {
185 $template_ids = array( 0 ); // Return nothing by default (the 0 is needed for `post__in`).
186 $template_types = $request['slug'] ? $request['slug'] : get_template_types();
187
188 foreach ( $template_types as $template_type ) {
189 // Skip 'embed' for now because it is not a regular template type.
190 if ( in_array( $template_type, array( 'embed' ), true ) ) {
191 continue;
192 }
193
194 $current_template = gutenberg_find_template_post_and_parts( $template_type );
195 if ( isset( $current_template ) ) {
196 $template_ids[] = $current_template['template_post']->ID;
197 }
198 }
199 $args['post__in'] = $template_ids;
200 $args['post_status'] = array( 'publish', 'auto-draft' );
201 }
202
203 return $args;
204 }
205 add_filter( 'rest_wp_template_query', 'filter_rest_wp_template_query', 99, 2 );
206