Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow view access of template rest endpoint to anyone with the edit_post capability #60317

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
/**
* REST API: Gutenberg_REST_Templates_Controller_6_6 class
*
* @package gutenberg
*/

/**
* Gutenberg_REST_Templates_Controller_6_6 class
*
* Templates and template parts currently only allow access to administrators with the
* `edit_theme_options` capability. In order to allow other roles to also view the templates,
* we need to override the permissions check for the REST API endpoints.
*/
class Gutenberg_REST_Templates_Controller_6_6 extends Gutenberg_REST_Templates_Controller_6_4 {

/**
* Checks if a given request has access to read templates.
*
* @since 6.6
*
* @param WP_REST_Request $request Full details about the request.
* @return true|WP_Error True if the request has read access, WP_Error object otherwise.
*/
public function get_items_permissions_check( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
if ( current_user_can( 'edit_posts' ) ) {
return true;
}
foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) {
if ( current_user_can( $post_type->cap->edit_posts ) ) {
return true;
}
}
Comment on lines +26 to +33
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change appears to have caused an unintended regression.

Currently, Gutenberg uses the following code to determine whether the site editor can be accessed, mainly using the command palette, and switches the link destination based on the result.

wp.data.select('core').canUser( 'read', 'templates' )

With this PR, this code now returns true even for non-administrator users, which causes links that they are not allowed to access to be displayed in commands, etc.

See #61419


return new WP_Error(
'rest_cannot_manage_templates',
__( 'Sorry, you are not allowed to access the templates on this site.', 'default' ),
array(
'status' => rest_authorization_required_code(),
)
);
}

/**
* Checks if a given request has access to read templates.
*
* @since 6.6
*
* @param WP_REST_Request $request Full details about the request.
* @return true|WP_Error True if the request has read access, WP_Error object otherwise.
*/
public function get_item_permissions_check( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
if ( current_user_can( 'edit_posts' ) ) {
return true;
}
foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) {
if ( current_user_can( $post_type->cap->edit_posts ) ) {
return true;
}
}

return new WP_Error(
'rest_cannot_manage_templates',
__( 'Sorry, you are not allowed to access the templates on this site.', 'default' ),
array(
'status' => rest_authorization_required_code(),
)
);
}
}
31 changes: 31 additions & 0 deletions lib/compat/wordpress-6.6/rest-api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* PHP and WordPress configuration compatibility functions for the Gutenberg
* editor plugin changes related to REST API.
*
* @package gutenberg
*/

if ( ! defined( 'ABSPATH' ) ) {
die( 'Silence is golden.' );
}

if ( ! function_exists( 'wp_api_template_access_controller' ) ) {
/**
* Hook in to the template and template part post types and modify the
* access control for the rest endpoint to allow lower user roles to access
* the templates and template parts.
*
* @param array $args Current registered post type args.
* @param string $post_type Name of post type.
*
* @return array
*/
function wp_api_template_access_controller( $args, $post_type ) {
if ( 'wp_template' === $post_type || 'wp_template_part' === $post_type ) {
$args['rest_controller_class'] = 'Gutenberg_REST_Templates_Controller_6_6';
}
return $args;
}
}
add_filter( 'register_post_type_args', 'wp_api_template_access_controller', 10, 2 );
2 changes: 2 additions & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ function gutenberg_is_experiment_enabled( $name ) {
// WordPress 6.6 compat.
require __DIR__ . '/compat/wordpress-6.6/block-bindings/pattern-overrides.php';
require __DIR__ . '/compat/wordpress-6.6/option.php';
require __DIR__ . '/compat/wordpress-6.6/class-gutenberg-rest-templates-controller-6-6.php';
require __DIR__ . '/compat/wordpress-6.6/rest-api.php';

// Experimental features.
require __DIR__ . '/experimental/block-editor-settings-mobile.php';
Expand Down
Loading