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

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

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