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