/
templates.php
281 lines (257 loc) · 8.42 KB
/
templates.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<?php
/**
* Block template functions.
*
* @package gutenberg
*/
/**
* Registers block editor 'wp_template' post type.
*/
function gutenberg_register_template_post_type() {
if ( ! gutenberg_supports_block_templates() ) {
return;
}
$labels = array(
'name' => __( 'Templates', 'gutenberg' ),
'singular_name' => __( 'Template', 'gutenberg' ),
'menu_name' => _x( 'Templates', 'Admin Menu text', 'gutenberg' ),
'add_new' => _x( 'Add New', 'Template', 'gutenberg' ),
'add_new_item' => __( 'Add New Template', 'gutenberg' ),
'new_item' => __( 'New Template', 'gutenberg' ),
'edit_item' => __( 'Edit Template', 'gutenberg' ),
'view_item' => __( 'View Template', 'gutenberg' ),
'view_items' => __( 'View Templates', 'gutenberg' ),
'all_items' => __( 'All Templates', 'gutenberg' ),
'search_items' => __( 'Search Templates', 'gutenberg' ),
'parent_item_colon' => __( 'Parent Template:', 'gutenberg' ),
'not_found' => __( 'No templates found.', 'gutenberg' ),
'not_found_in_trash' => __( 'No templates found in Trash.', 'gutenberg' ),
'archives' => __( 'Template archives', 'gutenberg' ),
'insert_into_item' => __( 'Insert into template', 'gutenberg' ),
'uploaded_to_this_item' => __( 'Uploaded to this template', 'gutenberg' ),
'filter_items_list' => __( 'Filter templates list', 'gutenberg' ),
'items_list_navigation' => __( 'Templates list navigation', 'gutenberg' ),
'items_list' => __( 'Templates list', 'gutenberg' ),
);
$args = array(
'labels' => $labels,
'description' => __( 'Templates to include in your theme.', 'gutenberg' ),
'public' => false,
'has_archive' => false,
'show_ui' => true,
'show_in_menu' => false,
'show_in_admin_bar' => false,
'show_in_rest' => true,
'rest_base' => 'templates',
'rest_controller_class' => 'Gutenberg_REST_Templates_Controller',
'capability_type' => array( 'template', 'templates' ),
'map_meta_cap' => true,
'supports' => array(
'title',
'slug',
'excerpt',
'editor',
'revisions',
),
);
register_post_type( 'wp_template', $args );
}
add_action( 'init', 'gutenberg_register_template_post_type' );
/**
* Registers block editor 'wp_theme' taxonomy.
*/
function gutenberg_register_wp_theme_taxonomy() {
if ( ! gutenberg_supports_block_templates() && ! WP_Theme_JSON_Resolver_Gutenberg::theme_has_support() ) {
return;
}
register_taxonomy(
'wp_theme',
array( 'wp_template', 'wp_template_part', 'wp_global_styles' ),
array(
'public' => false,
'hierarchical' => false,
'labels' => array(
'name' => __( 'Themes', 'gutenberg' ),
'singular_name' => __( 'Theme', 'gutenberg' ),
),
'query_var' => false,
'rewrite' => false,
'show_ui' => false,
'_builtin' => true,
'show_in_nav_menus' => false,
'show_in_rest' => false,
)
);
}
add_action( 'init', 'gutenberg_register_wp_theme_taxonomy' );
/**
* Filters the capabilities of a user to conditionally grant them capabilities for managing 'wp_template' posts.
*
* Any user who can 'edit_theme_options' will have access.
*
* @param array $allcaps A user's capabilities.
* @return array Filtered $allcaps.
*/
function gutenberg_grant_template_caps( array $allcaps ) {
if ( isset( $allcaps['edit_theme_options'] ) ) {
$allcaps['edit_templates'] = $allcaps['edit_theme_options'];
$allcaps['edit_others_templates'] = $allcaps['edit_theme_options'];
$allcaps['edit_published_templates'] = $allcaps['edit_theme_options'];
$allcaps['edit_private_templates'] = $allcaps['edit_theme_options'];
$allcaps['delete_templates'] = $allcaps['edit_theme_options'];
$allcaps['delete_others_templates'] = $allcaps['edit_theme_options'];
$allcaps['delete_published_templates'] = $allcaps['edit_theme_options'];
$allcaps['delete_private_templates'] = $allcaps['edit_theme_options'];
$allcaps['publish_templates'] = $allcaps['edit_theme_options'];
$allcaps['read_private_templates'] = $allcaps['edit_theme_options'];
}
return $allcaps;
}
add_filter( 'user_has_cap', 'gutenberg_grant_template_caps' );
/**
* Fixes the label of the 'wp_template' admin menu entry.
*/
function gutenberg_fix_template_admin_menu_entry() {
if ( ! gutenberg_supports_block_templates() ) {
return;
}
global $submenu;
if ( ! isset( $submenu['themes.php'] ) ) {
return;
}
$post_type = get_post_type_object( 'wp_template' );
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_admin_menu_entry' );
// Customize the `wp_template` admin list.
add_filter( 'manage_wp_template_posts_columns', 'gutenberg_templates_lists_custom_columns' );
add_action( 'manage_wp_template_posts_custom_column', 'gutenberg_render_templates_lists_custom_column', 10, 2 );
add_filter( 'views_edit-wp_template', 'gutenberg_filter_templates_edit_views' );
/**
* Sets a custom slug when creating auto-draft templates.
* This is only needed for auto-drafts created by the regular WP editor.
* If this page is to be removed, this won't be necessary.
*
* @param int $post_id Post ID.
*/
function set_unique_slug_on_create_template( $post_id ) {
$post = get_post( $post_id );
if ( 'auto-draft' !== $post->post_status ) {
return;
}
if ( ! $post->post_name ) {
wp_update_post(
array(
'ID' => $post_id,
'post_name' => 'custom_slug_' . uniqid(),
)
);
}
$terms = get_the_terms( $post_id, 'wp_theme' );
if ( ! $terms || ! count( $terms ) ) {
wp_set_post_terms( $post_id, wp_get_theme()->get_stylesheet(), 'wp_theme' );
}
}
add_action( 'save_post_wp_template', 'set_unique_slug_on_create_template' );
/**
* Print the skip-link script & styles.
*
* @todo Remove this when WP 5.8 is the minimum required version.
*
* @return void
*/
function gutenberg_the_skip_link() {
// Early exit if not a block theme.
if ( ! gutenberg_supports_block_templates() ) {
return;
}
// Early exit if not a block template.
global $_wp_current_template_content;
if ( ! $_wp_current_template_content ) {
return;
}
?>
<?php
/**
* Print the skip-link styles.
*/
?>
<style id="skip-link-styles">
.skip-link.screen-reader-text {
border: 0;
clip: rect(1px,1px,1px,1px);
clip-path: inset(50%);
height: 1px;
margin: -1px;
overflow: hidden;
padding: 0;
position: absolute !important;
width: 1px;
word-wrap: normal !important;
}
.skip-link.screen-reader-text:focus {
background-color: #eee;
clip: auto !important;
clip-path: none;
color: #444;
display: block;
font-size: 1em;
height: auto;
left: 5px;
line-height: normal;
padding: 15px 23px 14px;
text-decoration: none;
top: 5px;
width: auto;
z-index: 100000;
}
</style>
<?php
/**
* Print the skip-link script.
*/
?>
<script>
( function() {
var skipLinkTarget = document.querySelector( 'main' ),
sibling,
skipLinkTargetID,
skipLink;
// Early exit if a skip-link target can't be located.
if ( ! skipLinkTarget ) {
return;
}
// Get the site wrapper.
// The skip-link will be injected in the beginning of it.
sibling = document.querySelector( '.wp-site-blocks' );
// Early exit if the root element was not found.
if ( ! sibling ) {
return;
}
// Get the skip-link target's ID, and generate one if it doesn't exist.
skipLinkTargetID = skipLinkTarget.id;
if ( ! skipLinkTargetID ) {
skipLinkTargetID = 'wp--skip-link--target';
skipLinkTarget.id = skipLinkTargetID;
}
// Create the skip link.
skipLink = document.createElement( 'a' );
skipLink.classList.add( 'skip-link', 'screen-reader-text' );
skipLink.href = '#' + skipLinkTargetID;
skipLink.innerHTML = '<?php esc_html_e( 'Skip to content', 'gutenberg' ); ?>';
// Inject the skip link.
sibling.parentElement.insertBefore( skipLink, sibling );
}() );
</script>
<?php
}
remove_action( 'wp_footer', 'the_block_template_skip_link' );
add_action( 'wp_footer', 'gutenberg_the_skip_link' );