-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
fabiankaegy
merged 1 commit into
trunk
from
feature/change-template-rest-controller-to-allow-view-access
Mar 30, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
lib/compat/wordpress-6.6/class-gutenberg-rest-templates-controller-6-6.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
||
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(), | ||
) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
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