| 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 |
$labels = array( |
| 13 |
'name' => __( 'Templates', 'gutenberg' ), |
| 14 |
'singular_name' => __( 'Template', 'gutenberg' ), |
| 15 |
'menu_name' => _x( 'Templates', 'Admin Menu text', 'gutenberg' ), |
| 16 |
'add_new' => _x( 'Add New', 'Template', 'gutenberg' ), |
| 17 |
'add_new_item' => __( 'Add New Template', 'gutenberg' ), |
| 18 |
'new_item' => __( 'New Template', 'gutenberg' ), |
| 19 |
'edit_item' => __( 'Edit Template', 'gutenberg' ), |
| 20 |
'view_item' => __( 'View Template', 'gutenberg' ), |
| 21 |
'all_items' => __( 'All Templates', 'gutenberg' ), |
| 22 |
'search_items' => __( 'Search Templates', 'gutenberg' ), |
| 23 |
'parent_item_colon' => __( 'Parent Template:', 'gutenberg' ), |
| 24 |
'not_found' => __( 'No templates found.', 'gutenberg' ), |
| 25 |
'not_found_in_trash' => __( 'No templates found in Trash.', 'gutenberg' ), |
| 26 |
'archives' => __( 'Template archives', 'gutenberg' ), |
| 27 |
'insert_into_item' => __( 'Insert into template', 'gutenberg' ), |
| 28 |
'uploaded_to_this_item' => __( 'Uploaded to this template', 'gutenberg' ), |
| 29 |
'filter_items_list' => __( 'Filter templates list', 'gutenberg' ), |
| 30 |
'items_list_navigation' => __( 'Templates list navigation', 'gutenberg' ), |
| 31 |
'items_list' => __( 'Templates list', 'gutenberg' ), |
| 32 |
); |
| 33 |
|
| 34 |
$args = array( |
| 35 |
'labels' => $labels, |
| 36 |
'description' => __( 'Templates to include in your theme.', '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' => 'templates', |
| 44 |
'capability_type' => array( 'template', 'templates' ), |
| 45 |
'map_meta_cap' => true, |
| 46 |
'supports' => array( |
| 47 |
'title', |
| 48 |
'slug', |
| 49 |
'editor', |
| 50 |
'revisions', |
| 51 |
), |
| 52 |
); |
| 53 |
|
| 54 |
register_post_type( 'wp_template', $args ); |
| 55 |
} |
| 56 |
add_action( 'init', 'gutenberg_register_template_post_type' ); |
| 57 |
|
| 58 |
/** |
| 59 |
* Filters the capabilities of a user to conditionally grant them capabilities for managing 'wp_template' posts. |
| 60 |
* |
| 61 |
* Any user who can 'edit_theme_options' will have access. |
| 62 |
* |
| 63 |
* @param array $allcaps A user's capabilities. |
| 64 |
* @return array Filtered $allcaps. |
| 65 |
*/ |
| 66 |
function gutenberg_grant_template_caps( array $allcaps ) { |
| 67 |
if ( isset( $allcaps['edit_theme_options'] ) ) { |
| 68 |
$allcaps['edit_templates'] = $allcaps['edit_theme_options']; |
| 69 |
$allcaps['edit_others_templates'] = $allcaps['edit_theme_options']; |
| 70 |
$allcaps['edit_published_templates'] = $allcaps['edit_theme_options']; |
| 71 |
$allcaps['edit_private_templates'] = $allcaps['edit_theme_options']; |
| 72 |
$allcaps['delete_templates'] = $allcaps['edit_theme_options']; |
| 73 |
$allcaps['delete_others_templates'] = $allcaps['edit_theme_options']; |
| 74 |
$allcaps['delete_published_templates'] = $allcaps['edit_theme_options']; |
| 75 |
$allcaps['delete_private_templates'] = $allcaps['edit_theme_options']; |
| 76 |
$allcaps['publish_templates'] = $allcaps['edit_theme_options']; |
| 77 |
$allcaps['read_private_templates'] = $allcaps['edit_theme_options']; |
| 78 |
} |
| 79 |
|
| 80 |
return $allcaps; |
| 81 |
} |
| 82 |
add_filter( 'user_has_cap', 'gutenberg_grant_template_caps' ); |
| 83 |
|
| 84 |
/** |
| 85 |
* Filters `wp_template` posts slug resolution to bypass deduplication logic as |
| 86 |
* template slugs should be unique. |
| 87 |
* |
| 88 |
* @param string $slug The resolved slug (post_name). |
| 89 |
* @param int $post_ID Post ID. |
| 90 |
* @param string $post_status No uniqueness checks are made if the post is still draft or pending. |
| 91 |
* @param string $post_type Post type. |
| 92 |
* @param int $post_parent Post parent ID. |
| 93 |
* @param int $original_slug The desired slug (post_name). |
| 94 |
* @return string The original, desired slug. |
| 95 |
*/ |
| 96 |
function gutenberg_filter_wp_template_wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug ) { |
| 97 |
if ( 'wp_template' === $post_type ) { |
| 98 |
return $original_slug; |
| 99 |
} |
| 100 |
return $slug; |
| 101 |
} |
| 102 |
add_filter( 'wp_unique_post_slug', 'gutenberg_filter_wp_template_wp_unique_post_slug', 10, 6 ); |
| 103 |
|
| 104 |
/** |
| 105 |
* Fixes the label of the 'wp_template' admin menu entry. |
| 106 |
*/ |
| 107 |
function gutenberg_fix_template_admin_menu_entry() { |
| 108 |
global $submenu; |
| 109 |
if ( ! isset( $submenu['themes.php'] ) ) { |
| 110 |
return; |
| 111 |
} |
| 112 |
$post_type = get_post_type_object( 'wp_template' ); |
| 113 |
if ( ! $post_type ) { |
| 114 |
return; |
| 115 |
} |
| 116 |
foreach ( $submenu['themes.php'] as $key => $submenu_entry ) { |
| 117 |
if ( $post_type->labels->all_items === $submenu['themes.php'][ $key ][0] ) { |
| 118 |
$submenu['themes.php'][ $key ][0] = $post_type->labels->menu_name; // phpcs:ignore WordPress.WP.GlobalVariablesOverride |
| 119 |
break; |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
add_action( 'admin_menu', 'gutenberg_fix_template_admin_menu_entry' ); |
| 124 |
|
| 125 |
/** |
| 126 |
* Filters the 'wp_template' post type columns in the admin list table. |
| 127 |
* |
| 128 |
* @param array $columns Columns to display. |
| 129 |
* @return array Filtered $columns. |
| 130 |
*/ |
| 131 |
function gutenberg_filter_template_list_table_columns( array $columns ) { |
| 132 |
$columns['slug'] = __( 'Slug', 'gutenberg' ); |
| 133 |
if ( isset( $columns['date'] ) ) { |
| 134 |
unset( $columns['date'] ); |
| 135 |
} |
| 136 |
return $columns; |
| 137 |
} |
| 138 |
add_filter( 'manage_wp_template_posts_columns', 'gutenberg_filter_template_list_table_columns' ); |
| 139 |
|
| 140 |
/** |
| 141 |
* Renders column content for the 'wp_template' post type list table. |
| 142 |
* |
| 143 |
* @param string $column_name Column name to render. |
| 144 |
* @param int $post_id Post ID. |
| 145 |
*/ |
| 146 |
function gutenberg_render_template_list_table_column( $column_name, $post_id ) { |
| 147 |
if ( 'slug' !== $column_name ) { |
| 148 |
return; |
| 149 |
} |
| 150 |
$post = get_post( $post_id ); |
| 151 |
echo esc_html( $post->post_name ); |
| 152 |
} |
| 153 |
add_action( 'manage_wp_template_posts_custom_column', 'gutenberg_render_template_list_table_column', 10, 2 ); |
| 154 |
|
| 155 |
/** |
| 156 |
* Filter for adding a `resolved` parameter to `wp_template` queries. |
| 157 |
* |
| 158 |
* @param array $query_params The query parameters. |
| 159 |
* @return array Filtered $query_params. |
| 160 |
*/ |
| 161 |
function filter_rest_wp_template_collection_params( $query_params ) { |
| 162 |
$query_params += array( |
| 163 |
'resolved' => array( |
| 164 |
'description' => __( 'Whether to filter for resolved templates', 'gutenberg' ), |
| 165 |
'type' => 'boolean', |
| 166 |
), |
| 167 |
); |
| 168 |
return $query_params; |
| 169 |
} |
| 170 |
apply_filters( 'rest_wp_template_collection_params', 'filter_rest_wp_template_collection_params', 99, 1 ); |
| 171 |
|
| 172 |
/** |
| 173 |
* Filter for supporting the `resolved` parameter in `wp_template` queries. |
| 174 |
* |
| 175 |
* @param array $args The query arguments. |
| 176 |
* @param WP_REST_Request $request The request object. |
| 177 |
* @return array Filtered $args. |
| 178 |
*/ |
| 179 |
function filter_rest_wp_template_query( $args, $request ) { |
| 180 |
if ( $request['resolved'] ) { |
| 181 |
$template_ids = array( 0 ); // Return nothing by default (the 0 is needed for `post__in`). |
| 182 |
$template_types = $request['slug'] ? $request['slug'] : get_template_types(); |
| 183 |
|
| 184 |
foreach ( $template_types as $template_type ) { |
| 185 |
// Skip 'embed' for now because it is not a regular template type. |
| 186 |
if ( in_array( $template_type, array( 'embed' ), true ) ) { |
| 187 |
continue; |
| 188 |
} |
| 189 |
|
| 190 |
$current_template = gutenberg_find_template_post_and_parts( $template_type ); |
| 191 |
if ( isset( $current_template ) ) { |
| 192 |
$template_ids[] = $current_template['template_post']->ID; |
| 193 |
} |
| 194 |
} |
| 195 |
$args['post__in'] = $template_ids; |
| 196 |
$args['post_status'] = array( 'publish', 'auto-draft' ); |
| 197 |
} |
| 198 |
|
| 199 |
return $args; |
| 200 |
} |
| 201 |
add_filter( 'rest_wp_template_query', 'filter_rest_wp_template_query', 99, 2 ); |
| 202 |
|