From a32253824ef7522ef86d9abcd6c69699e88d42dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20K=C3=A4gy?= Date: Sat, 30 Mar 2024 15:40:09 +0100 Subject: [PATCH] allow view access of template rest endpoint to anyone with the edit_post capability (#60317) Co-authored-by: fabiankaegy Co-authored-by: TimothyBJacobs --- ...utenberg-rest-templates-controller-6-6.php | 70 +++++++++++++++++++ lib/compat/wordpress-6.6/rest-api.php | 31 ++++++++ lib/load.php | 2 + 3 files changed, 103 insertions(+) create mode 100644 lib/compat/wordpress-6.6/class-gutenberg-rest-templates-controller-6-6.php create mode 100644 lib/compat/wordpress-6.6/rest-api.php diff --git a/lib/compat/wordpress-6.6/class-gutenberg-rest-templates-controller-6-6.php b/lib/compat/wordpress-6.6/class-gutenberg-rest-templates-controller-6-6.php new file mode 100644 index 0000000000000..ebbe0da14fc34 --- /dev/null +++ b/lib/compat/wordpress-6.6/class-gutenberg-rest-templates-controller-6-6.php @@ -0,0 +1,70 @@ + 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(), + ) + ); + } +} diff --git a/lib/compat/wordpress-6.6/rest-api.php b/lib/compat/wordpress-6.6/rest-api.php new file mode 100644 index 0000000000000..bf462cd11ca4b --- /dev/null +++ b/lib/compat/wordpress-6.6/rest-api.php @@ -0,0 +1,31 @@ +