PluginProbe
Gutenberg / 7.0.0
Gutenberg v7.0.0
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / lib / templates.php

templates.php in Gutenberg 7.0.0, at lib/templates.php

167 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * Filters capabilities to prevent deletion of the 'wp_template' post with slug 'index'.
90 *
91 * Similar to today's themes, this template should always exist.
92 *
93 * @param array $caps Array of the user's capabilities.
94 * @param string $cap Capability name.
95 * @param int $user_id The user ID.
96 * @param array $args Adds the context to the cap. Typically the object ID.
97 * @return array Filtered $caps.
98 */
99 function gutenberg_prevent_index_template_deletion( $caps, $cap, $user_id, $args ) {
100 if ( 'delete_post' !== $cap || ! isset( $args[0] ) ) {
101 return $caps;
102 }
103
104 $post = get_post( $args[0] );
105 if ( ! $post || 'wp_template' !== $post->post_type ) {
106 return $caps;
107 }
108
109 if ( 'index' === $post->post_name ) {
110 $caps[] = 'do_not_allow';
111 }
112
113 return $caps;
114 }
115 add_filter( 'map_meta_cap', 'gutenberg_prevent_index_template_deletion', 10, 4 );
116
117 /**
118 * Fixes the label of the 'wp_template' admin menu entry.
119 */
120 function gutenberg_fix_template_admin_menu_entry() {
121 global $submenu;
122 if ( ! isset( $submenu['themes.php'] ) ) {
123 return;
124 }
125 $post_type = get_post_type_object( 'wp_template' );
126 if ( ! $post_type ) {
127 return;
128 }
129 foreach ( $submenu['themes.php'] as $key => $submenu_entry ) {
130 if ( $post_type->labels->all_items === $submenu['themes.php'][ $key ][0] ) {
131 $submenu['themes.php'][ $key ][0] = $post_type->labels->menu_name; // phpcs:ignore WordPress.WP.GlobalVariablesOverride
132 break;
133 }
134 }
135 }
136 add_action( 'admin_menu', 'gutenberg_fix_template_admin_menu_entry' );
137
138 /**
139 * Filters the 'wp_template' post type columns in the admin list table.
140 *
141 * @param array $columns Columns to display.
142 * @return array Filtered $columns.
143 */
144 function gutenberg_filter_template_list_table_columns( array $columns ) {
145 $columns['slug'] = __( 'Slug', 'gutenberg' );
146 if ( isset( $columns['date'] ) ) {
147 unset( $columns['date'] );
148 }
149 return $columns;
150 }
151 add_filter( 'manage_wp_template_posts_columns', 'gutenberg_filter_template_list_table_columns' );
152
153 /**
154 * Renders column content for the 'wp_template' post type list table.
155 *
156 * @param string $column_name Column name to render.
157 * @param int $post_id Post ID.
158 */
159 function gutenberg_render_template_list_table_column( $column_name, $post_id ) {
160 if ( 'slug' !== $column_name ) {
161 return;
162 }
163 $post = get_post( $post_id );
164 echo esc_html( $post->post_name );
165 }
166 add_action( 'manage_wp_template_posts_custom_column', 'gutenberg_render_template_list_table_column', 10, 2 );
167