| 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_is_experiment_enabled( 'gutenberg-full-site-editing' ) ) { |
| 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 |
'all_items' => __( 'All Templates', 'gutenberg' ), |
| 26 |
'search_items' => __( 'Search Templates', 'gutenberg' ), |
| 27 |
'parent_item_colon' => __( 'Parent Template:', 'gutenberg' ), |
| 28 |
'not_found' => __( 'No templates found.', 'gutenberg' ), |
| 29 |
'not_found_in_trash' => __( 'No templates found in Trash.', 'gutenberg' ), |
| 30 |
'archives' => __( 'Template archives', 'gutenberg' ), |
| 31 |
'insert_into_item' => __( 'Insert into template', 'gutenberg' ), |
| 32 |
'uploaded_to_this_item' => __( 'Uploaded to this template', 'gutenberg' ), |
| 33 |
'filter_items_list' => __( 'Filter templates list', 'gutenberg' ), |
| 34 |
'items_list_navigation' => __( 'Templates list navigation', 'gutenberg' ), |
| 35 |
'items_list' => __( 'Templates list', 'gutenberg' ), |
| 36 |
); |
| 37 |
|
| 38 |
$args = array( |
| 39 |
'labels' => $labels, |
| 40 |
'description' => __( 'Templates to include in your theme.', 'gutenberg' ), |
| 41 |
'public' => false, |
| 42 |
'has_archive' => false, |
| 43 |
'show_ui' => true, |
| 44 |
'show_in_menu' => 'themes.php', |
| 45 |
'show_in_admin_bar' => false, |
| 46 |
'show_in_rest' => true, |
| 47 |
'rest_base' => 'templates', |
| 48 |
'capability_type' => array( 'template', 'templates' ), |
| 49 |
'map_meta_cap' => true, |
| 50 |
'supports' => array( |
| 51 |
'title', |
| 52 |
'slug', |
| 53 |
'editor', |
| 54 |
'revisions', |
| 55 |
), |
| 56 |
); |
| 57 |
|
| 58 |
register_post_type( 'wp_template', $args ); |
| 59 |
} |
| 60 |
add_action( 'init', 'gutenberg_register_template_post_type' ); |
| 61 |
|
| 62 |
/** |
| 63 |
* Filters the capabilities of a user to conditionally grant them capabilities for managing 'wp_template' posts. |
| 64 |
* |
| 65 |
* Any user who can 'edit_theme_options' will have access. |
| 66 |
* |
| 67 |
* @param array $allcaps A user's capabilities. |
| 68 |
* @return array Filtered $allcaps. |
| 69 |
*/ |
| 70 |
function gutenberg_grant_template_caps( array $allcaps ) { |
| 71 |
if ( isset( $allcaps['edit_theme_options'] ) ) { |
| 72 |
$allcaps['edit_templates'] = $allcaps['edit_theme_options']; |
| 73 |
$allcaps['edit_others_templates'] = $allcaps['edit_theme_options']; |
| 74 |
$allcaps['edit_published_templates'] = $allcaps['edit_theme_options']; |
| 75 |
$allcaps['edit_private_templates'] = $allcaps['edit_theme_options']; |
| 76 |
$allcaps['delete_templates'] = $allcaps['edit_theme_options']; |
| 77 |
$allcaps['delete_others_templates'] = $allcaps['edit_theme_options']; |
| 78 |
$allcaps['delete_published_templates'] = $allcaps['edit_theme_options']; |
| 79 |
$allcaps['delete_private_templates'] = $allcaps['edit_theme_options']; |
| 80 |
$allcaps['publish_templates'] = $allcaps['edit_theme_options']; |
| 81 |
$allcaps['read_private_templates'] = $allcaps['edit_theme_options']; |
| 82 |
} |
| 83 |
|
| 84 |
return $allcaps; |
| 85 |
} |
| 86 |
add_filter( 'user_has_cap', 'gutenberg_grant_template_caps' ); |
| 87 |
|
| 88 |
/** |
| 89 |
* Fixes the label of the 'wp_template' admin menu entry. |
| 90 |
*/ |
| 91 |
function gutenberg_fix_template_admin_menu_entry() { |
| 92 |
global $submenu; |
| 93 |
if ( ! isset( $submenu['themes.php'] ) ) { |
| 94 |
return; |
| 95 |
} |
| 96 |
$post_type = get_post_type_object( 'wp_template' ); |
| 97 |
if ( ! $post_type ) { |
| 98 |
return; |
| 99 |
} |
| 100 |
foreach ( $submenu['themes.php'] as $key => $submenu_entry ) { |
| 101 |
if ( $post_type->labels->all_items === $submenu['themes.php'][ $key ][0] ) { |
| 102 |
$submenu['themes.php'][ $key ][0] = $post_type->labels->menu_name; // phpcs:ignore WordPress.WP.GlobalVariablesOverride |
| 103 |
break; |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
add_action( 'admin_menu', 'gutenberg_fix_template_admin_menu_entry' ); |
| 108 |
|
| 109 |
/** |
| 110 |
* Filters the 'wp_template' post type columns in the admin list table. |
| 111 |
* |
| 112 |
* @param array $columns Columns to display. |
| 113 |
* @return array Filtered $columns. |
| 114 |
*/ |
| 115 |
function gutenberg_filter_template_list_table_columns( array $columns ) { |
| 116 |
$columns['slug'] = __( 'Slug', 'gutenberg' ); |
| 117 |
if ( isset( $columns['date'] ) ) { |
| 118 |
unset( $columns['date'] ); |
| 119 |
} |
| 120 |
return $columns; |
| 121 |
} |
| 122 |
add_filter( 'manage_wp_template_posts_columns', 'gutenberg_filter_template_list_table_columns' ); |
| 123 |
|
| 124 |
/** |
| 125 |
* Renders column content for the 'wp_template' post type list table. |
| 126 |
* |
| 127 |
* @param string $column_name Column name to render. |
| 128 |
* @param int $post_id Post ID. |
| 129 |
*/ |
| 130 |
function gutenberg_render_template_list_table_column( $column_name, $post_id ) { |
| 131 |
if ( 'slug' !== $column_name ) { |
| 132 |
return; |
| 133 |
} |
| 134 |
$post = get_post( $post_id ); |
| 135 |
echo esc_html( $post->post_name ); |
| 136 |
} |
| 137 |
add_action( 'manage_wp_template_posts_custom_column', 'gutenberg_render_template_list_table_column', 10, 2 ); |
| 138 |
|