forked from WordPress/gutenberg
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow view access of template rest endpoint to anyone with the edit_p…
…ost capability (WordPress#60317) Co-authored-by: fabiankaegy <fabiankaegy@git.wordpress.org> Co-authored-by: TimothyBJacobs <timothyblynjacobs@git.wordpress.org>
- Loading branch information
1 parent
872f210
commit a322538
Showing
3 changed files
with
103 additions
and
0 deletions.
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