-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathtemplate-parts.php
227 lines (204 loc) · 7.64 KB
/
template-parts.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php
/**
* Block template part functions.
*
* @package gutenberg
*/
/**
* Registers block editor 'wp_template_part' post type.
*/
function gutenberg_register_template_part_post_type() {
if ( ! gutenberg_is_fse_theme() ) {
return;
}
$labels = array(
'name' => __( 'Template Parts', 'gutenberg' ),
'singular_name' => __( 'Template Part', 'gutenberg' ),
'menu_name' => _x( 'Template Parts', 'Admin Menu text', 'gutenberg' ),
'add_new' => _x( 'Add New', 'Template Part', 'gutenberg' ),
'add_new_item' => __( 'Add New Template Part', 'gutenberg' ),
'new_item' => __( 'New Template Part', 'gutenberg' ),
'edit_item' => __( 'Edit Template Part', 'gutenberg' ),
'view_item' => __( 'View Template Part', 'gutenberg' ),
'all_items' => __( 'All Template Parts', 'gutenberg' ),
'search_items' => __( 'Search Template Parts', 'gutenberg' ),
'parent_item_colon' => __( 'Parent Template Part:', 'gutenberg' ),
'not_found' => __( 'No template parts found.', 'gutenberg' ),
'not_found_in_trash' => __( 'No template parts found in Trash.', 'gutenberg' ),
'archives' => __( 'Template part archives', 'gutenberg' ),
'insert_into_item' => __( 'Insert into template part', 'gutenberg' ),
'uploaded_to_this_item' => __( 'Uploaded to this template part', 'gutenberg' ),
'filter_items_list' => __( 'Filter template parts list', 'gutenberg' ),
'items_list_navigation' => __( 'Template parts list navigation', 'gutenberg' ),
'items_list' => __( 'Template parts list', 'gutenberg' ),
);
$args = array(
'labels' => $labels,
'description' => __( 'Template parts to include in your templates.', 'gutenberg' ),
'public' => false,
'has_archive' => false,
'show_ui' => true,
'show_in_menu' => 'themes.php',
'show_in_admin_bar' => false,
'show_in_rest' => true,
'rest_base' => 'template-parts',
'map_meta_cap' => true,
'supports' => array(
'title',
'slug',
'editor',
'revisions',
'custom-fields',
),
);
$meta_args = array(
'object_subtype' => 'wp_template_part',
'type' => 'string',
'description' => 'The theme that provided the template part, if any.',
'single' => true,
'show_in_rest' => true,
);
register_post_type( 'wp_template_part', $args );
register_meta( 'post', 'theme', $meta_args );
}
add_action( 'init', 'gutenberg_register_template_part_post_type' );
/**
* Automatically set the theme meta for template parts.
*
* @param array $post_id Template Part ID.
* @param array $post Template Part Post.
* @param bool $update Is update.
*/
function gutenberg_set_template_part_post_theme( $post_id, $post, $update ) {
if ( 'wp_template_part' !== $post->post_type || $update || 'trash' === $post->post_status ) {
return;
}
$theme = get_post_meta( $post_id, 'theme', true );
if ( ! $theme ) {
update_post_meta( $post_id, 'theme', wp_get_theme()->get_stylesheet() );
}
}
add_action( 'save_post', 'gutenberg_set_template_part_post_theme', 10, 3 );
/**
* Filters `wp_template_part` posts slug resolution to bypass deduplication logic as
* template part slugs should be unique.
*
* @param string $slug The resolved slug (post_name).
* @param int $post_ID Post ID.
* @param string $post_status No uniqueness checks are made if the post is still draft or pending.
* @param string $post_type Post type.
* @param int $post_parent Post parent ID.
* @param int $original_slug The desired slug (post_name).
* @return string The original, desired slug.
*/
function gutenberg_filter_wp_template_part_wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug ) {
if ( 'wp_template_part' === $post_type ) {
return $original_slug;
}
return $slug;
}
add_filter( 'wp_unique_post_slug', 'gutenberg_filter_wp_template_part_wp_unique_post_slug', 10, 6 );
/**
* Fixes the label of the 'wp_template_part' admin menu entry.
*/
function gutenberg_fix_template_part_admin_menu_entry() {
if ( ! gutenberg_is_fse_theme() ) {
return;
}
global $submenu;
if ( ! isset( $submenu['themes.php'] ) ) {
return;
}
$post_type = get_post_type_object( 'wp_template_part' );
if ( ! $post_type ) {
return;
}
foreach ( $submenu['themes.php'] as $key => $submenu_entry ) {
if ( $post_type->labels->all_items === $submenu['themes.php'][ $key ][0] ) {
$submenu['themes.php'][ $key ][0] = $post_type->labels->menu_name; // phpcs:ignore WordPress.WP.GlobalVariablesOverride
break;
}
}
}
add_action( 'admin_menu', 'gutenberg_fix_template_part_admin_menu_entry' );
/**
* Filters the 'wp_template_part' post type columns in the admin list table.
*
* @param array $columns Columns to display.
* @return array Filtered $columns.
*/
function gutenberg_filter_template_part_list_table_columns( array $columns ) {
$columns['slug'] = __( 'Slug', 'gutenberg' );
if ( isset( $columns['date'] ) ) {
unset( $columns['date'] );
}
return $columns;
}
add_filter( 'manage_wp_template_part_posts_columns', 'gutenberg_filter_template_part_list_table_columns' );
/**
* Renders column content for the 'wp_template_part' post type list table.
*
* @param string $column_name Column name to render.
* @param int $post_id Post ID.
*/
function gutenberg_render_template_part_list_table_column( $column_name, $post_id ) {
if ( 'slug' !== $column_name ) {
return;
}
$post = get_post( $post_id );
echo esc_html( $post->post_name );
}
add_action( 'manage_wp_template_part_posts_custom_column', 'gutenberg_render_template_part_list_table_column', 10, 2 );
/**
* Filter for adding and a `theme` parameter to `wp_template_part` queries.
*
* @param array $query_params The query parameters.
* @return array Filtered $query_params.
*/
function filter_rest_wp_template_part_collection_params( $query_params ) {
$query_params += array(
'theme' => array(
'description' => __( 'The theme slug for the theme that created the template part.', 'gutenberg' ),
'type' => 'string',
),
);
return $query_params;
}
add_filter( 'rest_wp_template_part_collection_params', 'filter_rest_wp_template_part_collection_params', 99, 1 );
/**
* Filter for supporting the `resolved`, `template`, and `theme` parameters in `wp_template_part` queries.
*
* @param array $args The query arguments.
* @param WP_REST_Request $request The request object.
* @return array Filtered $args.
*/
function filter_rest_wp_template_part_query( $args, $request ) {
if ( $request['theme'] ) {
$meta_query = isset( $args['meta_query'] ) ? $args['meta_query'] : array();
$meta_query[] = array(
'key' => 'theme',
'value' => $request['theme'],
);
$args['meta_query'] = $meta_query;
}
return $args;
}
add_filter( 'rest_wp_template_part_query', 'filter_rest_wp_template_part_query', 99, 2 );
/**
* Run synchrnonization for template part API requests
*
* @param mixed $dispatch_result Dispatch result, will be used if not empty.
* @param WP_REST_Request $request Request used to generate the response.
* @param string $route Route matched for the request.
* @return mixed Dispatch result.
*/
function gutenberg_filter_rest_wp_template_part_dispatch( $dispatch_result, $request, $route ) {
if ( null !== $dispatch_result ) {
return $dispatch_result;
}
if ( 0 === strpos( $route, '/wp/v2/template-parts' ) && 'GET' === $request->get_method() ) {
_gutenberg_synchronize_theme_templates( 'template-part' );
}
return null;
}
add_filter( 'rest_dispatch_request', 'gutenberg_filter_rest_wp_template_part_dispatch', 10, 3 );