From 37a18f01666a4a50226af37b16899e26a11d9069 Mon Sep 17 00:00:00 2001 From: Carlo Manfredi Date: Fri, 16 Apr 2021 12:51:09 +1000 Subject: [PATCH 1/3] Standardise post type capabilities --- lib/class-wp-theme-json-resolver.php | 10 +------- lib/full-site-editing/full-site-editing.php | 22 +++++++++++++++++ lib/full-site-editing/template-parts.php | 1 + lib/full-site-editing/templates.php | 27 +-------------------- 4 files changed, 25 insertions(+), 35 deletions(-) diff --git a/lib/class-wp-theme-json-resolver.php b/lib/class-wp-theme-json-resolver.php index 86c3708b11103..2b29d8698a762 100644 --- a/lib/class-wp-theme-json-resolver.php +++ b/lib/class-wp-theme-json-resolver.php @@ -422,15 +422,7 @@ public static function register_user_custom_post_type() { 'show_ui' => false, 'show_in_rest' => true, 'rest_base' => '__experimental/global-styles', - 'capabilities' => array( - 'read' => 'edit_theme_options', - 'create_posts' => 'edit_theme_options', - 'edit_posts' => 'edit_theme_options', - 'edit_published_posts' => 'edit_theme_options', - 'delete_published_posts' => 'edit_theme_options', - 'edit_others_posts' => 'edit_theme_options', - 'delete_others_posts' => 'edit_theme_options', - ), + 'capabilities' => gutenberg_get_default_capabilities_for_fse(), 'map_meta_cap' => true, 'supports' => array( 'title', diff --git a/lib/full-site-editing/full-site-editing.php b/lib/full-site-editing/full-site-editing.php index 564332bbf6896..505718bad1074 100644 --- a/lib/full-site-editing/full-site-editing.php +++ b/lib/full-site-editing/full-site-editing.php @@ -143,3 +143,25 @@ function gutenberg_site_editor_load_block_editor_scripts_and_styles( $is_block_e : $is_block_editor_screen; } add_filter( 'should_load_block_editor_scripts_and_styles', 'gutenberg_site_editor_load_block_editor_scripts_and_styles' ); + +/** + * Used by wp_template, wp_template_part and wp_global_styles to map post type capabilities to edit_theme_options. + * + * @return array Default capabilities. + */ +function gutenberg_get_default_capabilities_for_fse() { + return array( + 'create_posts' => 'edit_theme_options', + 'delete_posts' => 'edit_theme_options', + 'delete_others_posts' => 'edit_theme_options', + 'delete_private_posts' => 'edit_theme_options', + 'delete_published_posts' => 'edit_theme_options', + 'edit_posts' => 'edit_theme_options', + 'edit_others_posts' => 'edit_theme_options', + 'edit_private_posts' => 'edit_theme_options', + 'edit_published_posts' => 'edit_theme_options', + 'publish_posts' => 'edit_theme_options', + 'read' => 'edit_theme_options', + 'read_private_posts' => 'edit_theme_options', + ); +} diff --git a/lib/full-site-editing/template-parts.php b/lib/full-site-editing/template-parts.php index e3e0609a99710..1e94d917d0778 100644 --- a/lib/full-site-editing/template-parts.php +++ b/lib/full-site-editing/template-parts.php @@ -46,6 +46,7 @@ function gutenberg_register_template_part_post_type() { 'show_in_rest' => true, 'rest_base' => 'template-parts', 'rest_controller_class' => 'WP_REST_Templates_Controller', + 'capabilities' => gutenberg_get_default_capabilities_for_fse(), 'map_meta_cap' => true, 'supports' => array( 'title', diff --git a/lib/full-site-editing/templates.php b/lib/full-site-editing/templates.php index 0ecfcc9a68d8e..71ff04b101d52 100644 --- a/lib/full-site-editing/templates.php +++ b/lib/full-site-editing/templates.php @@ -66,6 +66,7 @@ function gutenberg_register_template_post_type() { 'rest_base' => 'templates', 'rest_controller_class' => 'WP_REST_Templates_Controller', 'capability_type' => array( 'template', 'templates' ), + 'capabilities' => gutenberg_get_default_capabilities_for_fse(), 'map_meta_cap' => true, 'supports' => array( 'title', @@ -109,32 +110,6 @@ function gutenberg_register_wp_theme_taxonomy() { } add_action( 'init', 'gutenberg_register_wp_theme_taxonomy' ); -/** - * Filters the capabilities of a user to conditionally grant them capabilities for managing 'wp_template' posts. - * - * Any user who can 'edit_theme_options' will have access. - * - * @param array $allcaps A user's capabilities. - * @return array Filtered $allcaps. - */ -function gutenberg_grant_template_caps( array $allcaps ) { - if ( isset( $allcaps['edit_theme_options'] ) ) { - $allcaps['edit_templates'] = $allcaps['edit_theme_options']; - $allcaps['edit_others_templates'] = $allcaps['edit_theme_options']; - $allcaps['edit_published_templates'] = $allcaps['edit_theme_options']; - $allcaps['edit_private_templates'] = $allcaps['edit_theme_options']; - $allcaps['delete_templates'] = $allcaps['edit_theme_options']; - $allcaps['delete_others_templates'] = $allcaps['edit_theme_options']; - $allcaps['delete_published_templates'] = $allcaps['edit_theme_options']; - $allcaps['delete_private_templates'] = $allcaps['edit_theme_options']; - $allcaps['publish_templates'] = $allcaps['edit_theme_options']; - $allcaps['read_private_templates'] = $allcaps['edit_theme_options']; - } - - return $allcaps; -} -add_filter( 'user_has_cap', 'gutenberg_grant_template_caps' ); - /** * Fixes the label of the 'wp_template' admin menu entry. */ From ef86dd38abcbcdcef89bd9066203043350983748 Mon Sep 17 00:00:00 2001 From: Carlo Manfredi Date: Tue, 4 May 2021 21:51:04 +1000 Subject: [PATCH 2/3] Internal function --- lib/class-wp-theme-json-resolver.php | 2 +- lib/full-site-editing/full-site-editing.php | 5 ++++- lib/full-site-editing/template-parts.php | 2 +- lib/full-site-editing/templates.php | 2 +- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/class-wp-theme-json-resolver.php b/lib/class-wp-theme-json-resolver.php index 2b29d8698a762..c0e6b1ef54830 100644 --- a/lib/class-wp-theme-json-resolver.php +++ b/lib/class-wp-theme-json-resolver.php @@ -422,7 +422,7 @@ public static function register_user_custom_post_type() { 'show_ui' => false, 'show_in_rest' => true, 'rest_base' => '__experimental/global-styles', - 'capabilities' => gutenberg_get_default_capabilities_for_fse(), + 'capabilities' => _gutenberg_get_default_capabilities_for_fse_post_types(), 'map_meta_cap' => true, 'supports' => array( 'title', diff --git a/lib/full-site-editing/full-site-editing.php b/lib/full-site-editing/full-site-editing.php index 505718bad1074..1c8d0d3158560 100644 --- a/lib/full-site-editing/full-site-editing.php +++ b/lib/full-site-editing/full-site-editing.php @@ -147,9 +147,12 @@ function gutenberg_site_editor_load_block_editor_scripts_and_styles( $is_block_e /** * Used by wp_template, wp_template_part and wp_global_styles to map post type capabilities to edit_theme_options. * + * @access private + * @internal + * * @return array Default capabilities. */ -function gutenberg_get_default_capabilities_for_fse() { +function _gutenberg_get_default_capabilities_for_fse_post_types() { return array( 'create_posts' => 'edit_theme_options', 'delete_posts' => 'edit_theme_options', diff --git a/lib/full-site-editing/template-parts.php b/lib/full-site-editing/template-parts.php index 1e94d917d0778..d90172bb16cec 100644 --- a/lib/full-site-editing/template-parts.php +++ b/lib/full-site-editing/template-parts.php @@ -46,7 +46,7 @@ function gutenberg_register_template_part_post_type() { 'show_in_rest' => true, 'rest_base' => 'template-parts', 'rest_controller_class' => 'WP_REST_Templates_Controller', - 'capabilities' => gutenberg_get_default_capabilities_for_fse(), + 'capabilities' => _gutenberg_get_default_capabilities_for_fse_post_types(), 'map_meta_cap' => true, 'supports' => array( 'title', diff --git a/lib/full-site-editing/templates.php b/lib/full-site-editing/templates.php index 71ff04b101d52..c62d6a0854653 100644 --- a/lib/full-site-editing/templates.php +++ b/lib/full-site-editing/templates.php @@ -66,7 +66,7 @@ function gutenberg_register_template_post_type() { 'rest_base' => 'templates', 'rest_controller_class' => 'WP_REST_Templates_Controller', 'capability_type' => array( 'template', 'templates' ), - 'capabilities' => gutenberg_get_default_capabilities_for_fse(), + 'capabilities' => _gutenberg_get_default_capabilities_for_fse_post_types(), 'map_meta_cap' => true, 'supports' => array( 'title', From 6aa96a5c289cc33b7148d51117d733ddafa419c1 Mon Sep 17 00:00:00 2001 From: Carlo Manfredi Date: Tue, 4 May 2021 21:52:13 +1000 Subject: [PATCH 3/3] Remove capability_type for templates (will need to be added back for #27597) --- lib/full-site-editing/templates.php | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/full-site-editing/templates.php b/lib/full-site-editing/templates.php index c62d6a0854653..9b155fe2ec3ee 100644 --- a/lib/full-site-editing/templates.php +++ b/lib/full-site-editing/templates.php @@ -65,7 +65,6 @@ function gutenberg_register_template_post_type() { 'show_in_rest' => true, 'rest_base' => 'templates', 'rest_controller_class' => 'WP_REST_Templates_Controller', - 'capability_type' => array( 'template', 'templates' ), 'capabilities' => _gutenberg_get_default_capabilities_for_fse_post_types(), 'map_meta_cap' => true, 'supports' => array(