From a815bbc86740959665781be976f1193ca59e4ed1 Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Wed, 24 Nov 2021 17:33:51 +0800 Subject: [PATCH] Replicate API changes for template author support from WP core --- .../wordpress-5.9/block-template-utils.php | 1 + ...ss-gutenberg-rest-templates-controller.php | 24 +++++++++++++++++++ .../class-wp-block-template.php | 7 ++++++ lib/full-site-editing/template-parts.php | 1 + lib/full-site-editing/templates.php | 1 + 5 files changed, 34 insertions(+) diff --git a/lib/compat/wordpress-5.9/block-template-utils.php b/lib/compat/wordpress-5.9/block-template-utils.php index 9f189d83e8b61..79d2c3119d9b6 100644 --- a/lib/compat/wordpress-5.9/block-template-utils.php +++ b/lib/compat/wordpress-5.9/block-template-utils.php @@ -544,6 +544,7 @@ function _build_block_template_result_from_post( $post ) { $template->status = $post->post_status; $template->has_theme_file = $has_theme_file; $template->is_custom = true; + $template->post_author = $post->post_author; if ( 'wp_template' === $post->post_type && isset( $default_template_types[ $template->slug ] ) ) { $template->is_custom = false; diff --git a/lib/compat/wordpress-5.9/class-gutenberg-rest-templates-controller.php b/lib/compat/wordpress-5.9/class-gutenberg-rest-templates-controller.php index eb7f047a8e98d..6ec5db1dce988 100644 --- a/lib/compat/wordpress-5.9/class-gutenberg-rest-templates-controller.php +++ b/lib/compat/wordpress-5.9/class-gutenberg-rest-templates-controller.php @@ -385,6 +385,24 @@ protected function prepare_item_for_database( $request ) { } } + if ( ! empty( $request['author'] ) ) { + $post_author = (int) $request['author']; + + if ( get_current_user_id() !== $post_author ) { + $user_obj = get_userdata( $post_author ); + + if ( ! $user_obj ) { + return new WP_Error( + 'rest_invalid_author', + __( 'Invalid author ID.' ), + array( 'status' => 400 ) + ); + } + } + + $changes->post_author = $post_author; + } + return $changes; } @@ -412,6 +430,7 @@ public function prepare_item_for_response( $template, $request ) { // phpcs:igno 'status' => $template->status, 'wp_id' => $template->wp_id, 'has_theme_file' => $template->has_theme_file, + 'author' => (int) $template->post_author, ); if ( 'wp_template_part' === $template->type ) { @@ -579,6 +598,11 @@ public function get_item_schema() { 'context' => array( 'embed', 'view', 'edit' ), 'readonly' => true, ), + 'author' => array( + 'description' => __( 'The ID for the author of the template.' ), + 'type' => 'integer', + 'context' => array( 'view', 'edit', 'embed' ), + ), ), ); diff --git a/lib/full-site-editing/class-wp-block-template.php b/lib/full-site-editing/class-wp-block-template.php index 176e1105554df..967c49de953d7 100644 --- a/lib/full-site-editing/class-wp-block-template.php +++ b/lib/full-site-editing/class-wp-block-template.php @@ -94,4 +94,11 @@ class WP_Block_Template { * @var bool */ public $is_custom = true; + + /** + * Author. + * + * @var int + */ + public $post_author = 0; } diff --git a/lib/full-site-editing/template-parts.php b/lib/full-site-editing/template-parts.php index 77ef42d59c03e..979a357a083df 100644 --- a/lib/full-site-editing/template-parts.php +++ b/lib/full-site-editing/template-parts.php @@ -54,6 +54,7 @@ function gutenberg_register_template_part_post_type() { 'excerpt', 'editor', 'revisions', + 'author', ), ); diff --git a/lib/full-site-editing/templates.php b/lib/full-site-editing/templates.php index 051d69647da11..f20e5f5e4c40b 100644 --- a/lib/full-site-editing/templates.php +++ b/lib/full-site-editing/templates.php @@ -55,6 +55,7 @@ function gutenberg_register_template_post_type() { 'excerpt', 'editor', 'revisions', + 'author', ), );