Skip to content

Commit

Permalink
allow view access of template rest endpoint to anyone with the edit_p…
Browse files Browse the repository at this point in the history
…ost capability (WordPress#60317)

Co-authored-by: fabiankaegy <fabiankaegy@git.wordpress.org>
Co-authored-by: TimothyBJacobs <timothyblynjacobs@git.wordpress.org>
  • Loading branch information
3 people authored and cbravobernal committed Apr 9, 2024
1 parent 872f210 commit a322538
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
@@ -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(),
)
);
}
}
31 changes: 31 additions & 0 deletions lib/compat/wordpress-6.6/rest-api.php
@@ -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
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

0 comments on commit a322538

Please sign in to comment.