PluginProbe
Gutenberg / 8.5.1
Gutenberg v8.5.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 / template-parts.php

template-parts.php in Gutenberg 8.5.1, at lib/template-parts.php

250 lines 9.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Block template part functions.
4 *
5 * @package gutenberg
6 */
7
8 /**
9 * Registers block editor 'wp_template_part' post type.
10 */
11 function gutenberg_register_template_part_post_type() {
12 if ( ! gutenberg_is_experiment_enabled( 'gutenberg-full-site-editing' ) ) {
13 return;
14 }
15
16 $labels = array(
17 'name' => __( 'Template Parts', 'gutenberg' ),
18 'singular_name' => __( 'Template Part', 'gutenberg' ),
19 'menu_name' => _x( 'Template Parts', 'Admin Menu text', 'gutenberg' ),
20 'add_new' => _x( 'Add New', 'Template Part', 'gutenberg' ),
21 'add_new_item' => __( 'Add New Template Part', 'gutenberg' ),
22 'new_item' => __( 'New Template Part', 'gutenberg' ),
23 'edit_item' => __( 'Edit Template Part', 'gutenberg' ),
24 'view_item' => __( 'View Template Part', 'gutenberg' ),
25 'all_items' => __( 'All Template Parts', 'gutenberg' ),
26 'search_items' => __( 'Search Template Parts', 'gutenberg' ),
27 'parent_item_colon' => __( 'Parent Template Part:', 'gutenberg' ),
28 'not_found' => __( 'No template parts found.', 'gutenberg' ),
29 'not_found_in_trash' => __( 'No template parts found in Trash.', 'gutenberg' ),
30 'archives' => __( 'Template part archives', 'gutenberg' ),
31 'insert_into_item' => __( 'Insert into template part', 'gutenberg' ),
32 'uploaded_to_this_item' => __( 'Uploaded to this template part', 'gutenberg' ),
33 'filter_items_list' => __( 'Filter template parts list', 'gutenberg' ),
34 'items_list_navigation' => __( 'Template parts list navigation', 'gutenberg' ),
35 'items_list' => __( 'Template parts list', 'gutenberg' ),
36 );
37
38 $args = array(
39 'labels' => $labels,
40 'description' => __( 'Template parts to include in your templates.', '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' => 'template-parts',
48 'map_meta_cap' => true,
49 'supports' => array(
50 'title',
51 'slug',
52 'editor',
53 'revisions',
54 'custom-fields',
55 ),
56 );
57
58 $meta_args = array(
59 'object_subtype' => 'wp_template_part',
60 'type' => 'string',
61 'description' => 'The theme that provided the template part, if any.',
62 'single' => true,
63 'show_in_rest' => true,
64 );
65
66 register_post_type( 'wp_template_part', $args );
67 register_meta( 'post', 'theme', $meta_args );
68 }
69 add_action( 'init', 'gutenberg_register_template_part_post_type' );
70
71 /**
72 * Filters `wp_template_part` posts slug resolution to bypass deduplication logic as
73 * template part slugs should be unique.
74 *
75 * @param string $slug The resolved slug (post_name).
76 * @param int $post_ID Post ID.
77 * @param string $post_status No uniqueness checks are made if the post is still draft or pending.
78 * @param string $post_type Post type.
79 * @param int $post_parent Post parent ID.
80 * @param int $original_slug The desired slug (post_name).
81 * @return string The original, desired slug.
82 */
83 function gutenberg_filter_wp_template_part_wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug ) {
84 if ( 'wp_template_part' === $post_type ) {
85 return $original_slug;
86 }
87 return $slug;
88 }
89 add_filter( 'wp_unique_post_slug', 'gutenberg_filter_wp_template_part_wp_unique_post_slug', 10, 6 );
90
91 /**
92 * Fixes the label of the 'wp_template_part' admin menu entry.
93 */
94 function gutenberg_fix_template_part_admin_menu_entry() {
95 global $submenu;
96 if ( ! isset( $submenu['themes.php'] ) ) {
97 return;
98 }
99 $post_type = get_post_type_object( 'wp_template_part' );
100 if ( ! $post_type ) {
101 return;
102 }
103 foreach ( $submenu['themes.php'] as $key => $submenu_entry ) {
104 if ( $post_type->labels->all_items === $submenu['themes.php'][ $key ][0] ) {
105 $submenu['themes.php'][ $key ][0] = $post_type->labels->menu_name; // phpcs:ignore WordPress.WP.GlobalVariablesOverride
106 break;
107 }
108 }
109 }
110 add_action( 'admin_menu', 'gutenberg_fix_template_part_admin_menu_entry' );
111
112 /**
113 * Filters the 'wp_template_part' post type columns in the admin list table.
114 *
115 * @param array $columns Columns to display.
116 * @return array Filtered $columns.
117 */
118 function gutenberg_filter_template_part_list_table_columns( array $columns ) {
119 $columns['slug'] = __( 'Slug', 'gutenberg' );
120 if ( isset( $columns['date'] ) ) {
121 unset( $columns['date'] );
122 }
123 return $columns;
124 }
125 add_filter( 'manage_wp_template_part_posts_columns', 'gutenberg_filter_template_part_list_table_columns' );
126
127 /**
128 * Renders column content for the 'wp_template_part' post type list table.
129 *
130 * @param string $column_name Column name to render.
131 * @param int $post_id Post ID.
132 */
133 function gutenberg_render_template_part_list_table_column( $column_name, $post_id ) {
134 if ( 'slug' !== $column_name ) {
135 return;
136 }
137 $post = get_post( $post_id );
138 echo esc_html( $post->post_name );
139 }
140 add_action( 'manage_wp_template_part_posts_custom_column', 'gutenberg_render_template_part_list_table_column', 10, 2 );
141
142
143 /**
144 * Filter for adding a `resolved`, a `template`, and a `theme` parameter to `wp_template_part` queries.
145 *
146 * @param array $query_params The query parameters.
147 * @return array Filtered $query_params.
148 */
149 function filter_rest_wp_template_part_collection_params( $query_params ) {
150 $query_params += array(
151 'resolved' => array(
152 'description' => __( 'Whether to filter for resolved template parts.', 'gutenberg' ),
153 'type' => 'boolean',
154 ),
155 'template' => array(
156 'description' => __( 'The template slug for the template that the template part is used by.', 'gutenberg' ),
157 'type' => 'string',
158 ),
159 'theme' => array(
160 'description' => __( 'The theme slug for the theme that created the template part.', 'gutenberg' ),
161 'type' => 'string',
162 ),
163 );
164 return $query_params;
165 }
166 apply_filters( 'rest_wp_template_part_collection_params', 'filter_rest_wp_template_part_collection_params', 99, 1 );
167
168 /**
169 * Filter for supporting the `resolved`, `template`, and `theme` parameters in `wp_template_part` queries.
170 *
171 * @param array $args The query arguments.
172 * @param WP_REST_Request $request The request object.
173 * @return array Filtered $args.
174 */
175 function filter_rest_wp_template_part_query( $args, $request ) {
176 /**
177 * Unlike `filter_rest_wp_template_query`, we resolve queries also if there's only a `template` argument set.
178 * The difference is that in the case of templates, we can use the `slug` field that already exists (as part
179 * of the entities endpoint, wheras for template parts, we have to register the extra `template` argument),
180 * so we need the `resolved` flag to convey the different semantics (only return 'resolved' templates that match
181 * the `slug` vs return _all_ templates that match it (e.g. including all auto-drafts)).
182 *
183 * A template parts query with a `template` arg but not a `resolved` one is conceivable, but probably wouldn't be
184 * very useful: It'd be all template parts for all templates matching that `template` slug (including auto-drafts etc).
185 *
186 * @see filter_rest_wp_template_query
187 * @see filter_rest_wp_template_part_collection_params
188 * @see https://github.com/WordPress/gutenberg/pull/21878#discussion_r436961706
189 */
190 if ( $request['resolved'] || $request['template'] ) {
191 $template_part_ids = array( 0 ); // Return nothing by default (the 0 is needed for `post__in`).
192 $template_types = $request['template'] ? array( $request['template'] ) : get_template_types();
193
194 foreach ( $template_types as $template_type ) {
195 // Skip 'embed' for now because it is not a regular template type.
196 if ( in_array( $template_type, array( 'embed' ), true ) ) {
197 continue;
198 }
199
200 $current_template = gutenberg_find_template_post_and_parts( $template_type );
201 if ( isset( $current_template ) ) {
202 $template_part_ids = $template_part_ids + $current_template['template_part_ids'];
203 }
204 }
205 $args['post__in'] = $template_part_ids;
206 $args['post_status'] = array( 'publish', 'auto-draft' );
207 }
208
209 if ( $request['theme'] ) {
210 $meta_query = isset( $args['meta_query'] ) ? $args['meta_query'] : array();
211 $meta_query[] = array(
212 'key' => 'theme',
213 'value' => $request['theme'],
214 );
215
216 // Ensure auto-drafts of all theme supplied template parts are created.
217 if ( wp_get_theme()->get( 'TextDomain' ) === $request['theme'] ) {
218 // Get file paths for all theme supplied template parts.
219 $template_part_files = glob( get_stylesheet_directory() . '/block-template-parts/*.html' );
220 $template_part_files = is_array( $template_part_files ) ? $template_part_files : array();
221 if ( is_child_theme() ) {
222 $child_template_part_files = glob( get_template_directory() . '/block-template-parts/*.html' );
223 $child_template_part_files = is_array( $child_template_part_files ) ? $child_template_part_files : array();
224 $template_part_files = array_merge( $template_part_files, $child_template_part_files );
225 }
226 // Build and save each template part.
227 foreach ( $template_part_files as $template_part_file ) {
228 $content = file_get_contents( $template_part_file );
229 // Infer slug from filepath.
230 $slug = substr(
231 $template_part_file,
232 // Starting position of slug.
233 strpos( $template_part_file, 'block-template-parts/' ) + 21,
234 // Subtract ending '.html'.
235 -5
236 );
237 // Wrap content with the template part block, parse, and create auto-draft.
238 $template_part_string = '<!-- wp:template-part {"slug":"' . $slug . '","theme":"' . wp_get_theme()->get( 'TextDomain' ) . '"} -->' . $content . '<!-- /wp:template-part -->';
239 $template_part_block = parse_blocks( $template_part_string )[0];
240 create_auto_draft_for_template_part_block( $template_part_block );
241 }
242 };
243
244 $args['meta_query'] = $meta_query;
245 }
246
247 return $args;
248 }
249 add_filter( 'rest_wp_template_part_query', 'filter_rest_wp_template_part_query', 99, 2 );
250