| 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_supports_block_templates() ) { |
| 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 |
'view_items' => __( 'View Templates', 'gutenberg' ), |
| 26 |
'all_items' => __( 'All Templates', 'gutenberg' ), |
| 27 |
'search_items' => __( 'Search Templates', 'gutenberg' ), |
| 28 |
'parent_item_colon' => __( 'Parent Template:', 'gutenberg' ), |
| 29 |
'not_found' => __( 'No templates found.', 'gutenberg' ), |
| 30 |
'not_found_in_trash' => __( 'No templates found in Trash.', 'gutenberg' ), |
| 31 |
'archives' => __( 'Template archives', 'gutenberg' ), |
| 32 |
'insert_into_item' => __( 'Insert into template', 'gutenberg' ), |
| 33 |
'uploaded_to_this_item' => __( 'Uploaded to this template', 'gutenberg' ), |
| 34 |
'filter_items_list' => __( 'Filter templates list', 'gutenberg' ), |
| 35 |
'items_list_navigation' => __( 'Templates list navigation', 'gutenberg' ), |
| 36 |
'items_list' => __( 'Templates list', 'gutenberg' ), |
| 37 |
); |
| 38 |
|
| 39 |
$args = array( |
| 40 |
'labels' => $labels, |
| 41 |
'description' => __( 'Templates to include in your theme.', 'gutenberg' ), |
| 42 |
'public' => false, |
| 43 |
'has_archive' => false, |
| 44 |
'show_ui' => true, |
| 45 |
'show_in_menu' => false, |
| 46 |
'show_in_admin_bar' => false, |
| 47 |
'show_in_rest' => true, |
| 48 |
'rest_base' => 'templates', |
| 49 |
'rest_controller_class' => 'Gutenberg_REST_Templates_Controller', |
| 50 |
'capability_type' => array( 'template', 'templates' ), |
| 51 |
'map_meta_cap' => true, |
| 52 |
'supports' => array( |
| 53 |
'title', |
| 54 |
'slug', |
| 55 |
'excerpt', |
| 56 |
'editor', |
| 57 |
'revisions', |
| 58 |
'author', |
| 59 |
), |
| 60 |
); |
| 61 |
|
| 62 |
register_post_type( 'wp_template', $args ); |
| 63 |
} |
| 64 |
add_action( 'init', 'gutenberg_register_template_post_type' ); |
| 65 |
|
| 66 |
/** |
| 67 |
* Registers block editor 'wp_theme' taxonomy. |
| 68 |
*/ |
| 69 |
function gutenberg_register_wp_theme_taxonomy() { |
| 70 |
if ( ! gutenberg_supports_block_templates() && ! WP_Theme_JSON_Resolver_Gutenberg::theme_has_support() ) { |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
register_taxonomy( |
| 75 |
'wp_theme', |
| 76 |
array( 'wp_template', 'wp_template_part', 'wp_global_styles' ), |
| 77 |
array( |
| 78 |
'public' => false, |
| 79 |
'hierarchical' => false, |
| 80 |
'labels' => array( |
| 81 |
'name' => __( 'Themes', 'gutenberg' ), |
| 82 |
'singular_name' => __( 'Theme', 'gutenberg' ), |
| 83 |
), |
| 84 |
'query_var' => false, |
| 85 |
'rewrite' => false, |
| 86 |
'show_ui' => false, |
| 87 |
'_builtin' => true, |
| 88 |
'show_in_nav_menus' => false, |
| 89 |
'show_in_rest' => false, |
| 90 |
) |
| 91 |
); |
| 92 |
} |
| 93 |
add_action( 'init', 'gutenberg_register_wp_theme_taxonomy' ); |
| 94 |
|
| 95 |
/** |
| 96 |
* Filters the capabilities of a user to conditionally grant them capabilities for managing 'wp_template' posts. |
| 97 |
* |
| 98 |
* Any user who can 'edit_theme_options' will have access. |
| 99 |
* |
| 100 |
* @param array $allcaps A user's capabilities. |
| 101 |
* @return array Filtered $allcaps. |
| 102 |
*/ |
| 103 |
function gutenberg_grant_template_caps( array $allcaps ) { |
| 104 |
if ( isset( $allcaps['edit_theme_options'] ) ) { |
| 105 |
$allcaps['edit_templates'] = $allcaps['edit_theme_options']; |
| 106 |
$allcaps['edit_others_templates'] = $allcaps['edit_theme_options']; |
| 107 |
$allcaps['edit_published_templates'] = $allcaps['edit_theme_options']; |
| 108 |
$allcaps['edit_private_templates'] = $allcaps['edit_theme_options']; |
| 109 |
$allcaps['delete_templates'] = $allcaps['edit_theme_options']; |
| 110 |
$allcaps['delete_others_templates'] = $allcaps['edit_theme_options']; |
| 111 |
$allcaps['delete_published_templates'] = $allcaps['edit_theme_options']; |
| 112 |
$allcaps['delete_private_templates'] = $allcaps['edit_theme_options']; |
| 113 |
$allcaps['publish_templates'] = $allcaps['edit_theme_options']; |
| 114 |
$allcaps['read_private_templates'] = $allcaps['edit_theme_options']; |
| 115 |
} |
| 116 |
|
| 117 |
return $allcaps; |
| 118 |
} |
| 119 |
add_filter( 'user_has_cap', 'gutenberg_grant_template_caps' ); |
| 120 |
|
| 121 |
/** |
| 122 |
* Fixes the label of the 'wp_template' admin menu entry. |
| 123 |
*/ |
| 124 |
function gutenberg_fix_template_admin_menu_entry() { |
| 125 |
if ( ! gutenberg_supports_block_templates() ) { |
| 126 |
return; |
| 127 |
} |
| 128 |
global $submenu; |
| 129 |
if ( ! isset( $submenu['themes.php'] ) ) { |
| 130 |
return; |
| 131 |
} |
| 132 |
$post_type = get_post_type_object( 'wp_template' ); |
| 133 |
if ( ! $post_type ) { |
| 134 |
return; |
| 135 |
} |
| 136 |
foreach ( $submenu['themes.php'] as $key => $submenu_entry ) { |
| 137 |
if ( $post_type->labels->all_items === $submenu['themes.php'][ $key ][0] ) { |
| 138 |
$submenu['themes.php'][ $key ][0] = $post_type->labels->menu_name; // phpcs:ignore WordPress.WP.GlobalVariablesOverride |
| 139 |
break; |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
add_action( 'admin_menu', 'gutenberg_fix_template_admin_menu_entry' ); |
| 144 |
|
| 145 |
// Customize the `wp_template` admin list. |
| 146 |
add_filter( 'manage_wp_template_posts_columns', 'gutenberg_templates_lists_custom_columns' ); |
| 147 |
add_action( 'manage_wp_template_posts_custom_column', 'gutenberg_render_templates_lists_custom_column', 10, 2 ); |
| 148 |
add_filter( 'views_edit-wp_template', 'gutenberg_filter_templates_edit_views' ); |
| 149 |
|
| 150 |
/** |
| 151 |
* Sets a custom slug when creating auto-draft templates. |
| 152 |
* This is only needed for auto-drafts created by the regular WP editor. |
| 153 |
* If this page is to be removed, this won't be necessary. |
| 154 |
* |
| 155 |
* @param int $post_id Post ID. |
| 156 |
*/ |
| 157 |
function set_unique_slug_on_create_template( $post_id ) { |
| 158 |
$post = get_post( $post_id ); |
| 159 |
if ( 'auto-draft' !== $post->post_status ) { |
| 160 |
return; |
| 161 |
} |
| 162 |
|
| 163 |
if ( ! $post->post_name ) { |
| 164 |
wp_update_post( |
| 165 |
array( |
| 166 |
'ID' => $post_id, |
| 167 |
'post_name' => 'custom_slug_' . uniqid(), |
| 168 |
) |
| 169 |
); |
| 170 |
} |
| 171 |
|
| 172 |
$terms = get_the_terms( $post_id, 'wp_theme' ); |
| 173 |
if ( ! $terms || ! count( $terms ) ) { |
| 174 |
wp_set_post_terms( $post_id, wp_get_theme()->get_stylesheet(), 'wp_theme' ); |
| 175 |
} |
| 176 |
} |
| 177 |
add_action( 'save_post_wp_template', 'set_unique_slug_on_create_template' ); |
| 178 |
|
| 179 |
/** |
| 180 |
* Print the skip-link script & styles. |
| 181 |
* |
| 182 |
* @todo Remove this when WP 5.8 is the minimum required version. |
| 183 |
* |
| 184 |
* @return void |
| 185 |
*/ |
| 186 |
function gutenberg_the_skip_link() { |
| 187 |
// Early exit if not a block theme. |
| 188 |
if ( ! gutenberg_supports_block_templates() ) { |
| 189 |
return; |
| 190 |
} |
| 191 |
|
| 192 |
// Early exit if not a block template. |
| 193 |
global $_wp_current_template_content; |
| 194 |
if ( ! $_wp_current_template_content ) { |
| 195 |
return; |
| 196 |
} |
| 197 |
?> |
| 198 |
|
| 199 |
<?php |
| 200 |
/** |
| 201 |
* Print the skip-link styles. |
| 202 |
*/ |
| 203 |
?> |
| 204 |
<style id="skip-link-styles"> |
| 205 |
.skip-link.screen-reader-text { |
| 206 |
border: 0; |
| 207 |
clip: rect(1px,1px,1px,1px); |
| 208 |
clip-path: inset(50%); |
| 209 |
height: 1px; |
| 210 |
margin: -1px; |
| 211 |
overflow: hidden; |
| 212 |
padding: 0; |
| 213 |
position: absolute !important; |
| 214 |
width: 1px; |
| 215 |
word-wrap: normal !important; |
| 216 |
} |
| 217 |
|
| 218 |
.skip-link.screen-reader-text:focus { |
| 219 |
background-color: #eee; |
| 220 |
clip: auto !important; |
| 221 |
clip-path: none; |
| 222 |
color: #444; |
| 223 |
display: block; |
| 224 |
font-size: 1em; |
| 225 |
height: auto; |
| 226 |
left: 5px; |
| 227 |
line-height: normal; |
| 228 |
padding: 15px 23px 14px; |
| 229 |
text-decoration: none; |
| 230 |
top: 5px; |
| 231 |
width: auto; |
| 232 |
z-index: 100000; |
| 233 |
} |
| 234 |
</style> |
| 235 |
<?php |
| 236 |
/** |
| 237 |
* Print the skip-link script. |
| 238 |
*/ |
| 239 |
?> |
| 240 |
<script> |
| 241 |
( function() { |
| 242 |
var skipLinkTarget = document.querySelector( 'main' ), |
| 243 |
sibling, |
| 244 |
skipLinkTargetID, |
| 245 |
skipLink; |
| 246 |
|
| 247 |
// Early exit if a skip-link target can't be located. |
| 248 |
if ( ! skipLinkTarget ) { |
| 249 |
return; |
| 250 |
} |
| 251 |
|
| 252 |
// Get the site wrapper. |
| 253 |
// The skip-link will be injected in the beginning of it. |
| 254 |
sibling = document.querySelector( '.wp-site-blocks' ); |
| 255 |
|
| 256 |
// Early exit if the root element was not found. |
| 257 |
if ( ! sibling ) { |
| 258 |
return; |
| 259 |
} |
| 260 |
|
| 261 |
// Get the skip-link target's ID, and generate one if it doesn't exist. |
| 262 |
skipLinkTargetID = skipLinkTarget.id; |
| 263 |
if ( ! skipLinkTargetID ) { |
| 264 |
skipLinkTargetID = 'wp--skip-link--target'; |
| 265 |
skipLinkTarget.id = skipLinkTargetID; |
| 266 |
} |
| 267 |
|
| 268 |
// Create the skip link. |
| 269 |
skipLink = document.createElement( 'a' ); |
| 270 |
skipLink.classList.add( 'skip-link', 'screen-reader-text' ); |
| 271 |
skipLink.href = '#' + skipLinkTargetID; |
| 272 |
skipLink.innerHTML = '<?php esc_html_e( 'Skip to content', 'gutenberg' ); ?>'; |
| 273 |
|
| 274 |
// Inject the skip link. |
| 275 |
sibling.parentElement.insertBefore( skipLink, sibling ); |
| 276 |
}() ); |
| 277 |
</script> |
| 278 |
<?php |
| 279 |
} |
| 280 |
|
| 281 |
remove_action( 'wp_footer', 'the_block_template_skip_link' ); |
| 282 |
add_action( 'wp_footer', 'gutenberg_the_skip_link' ); |
| 283 |
|