From 9bb4a1e58ed94614ff137657985fbdeaa68f428e Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Fri, 3 Dec 2021 15:26:50 +0800 Subject: [PATCH] Add links to response for author and theme --- .../class-wp-rest-templates-controller.php | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php index c3df07e7f5041..2db96bf6e0a7d 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php @@ -342,7 +342,7 @@ public function create_item_permissions_check( $request ) { * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. */ public function create_item( $request ) { - $prepared_post = $this->prepare_item_for_database( $request ); + $prepared_post = $this->prepare_item_for_database( $request ); if ( is_wp_error( $prepared_post ) ) { return $prepared_post; @@ -651,7 +651,7 @@ public function prepare_item_for_response( $item, $request ) { // phpcs:ignore V // Wrap the data in a response object. $response = rest_ensure_response( $data ); - $links = $this->prepare_links( $template->id ); + $links = $this->prepare_links( $template ); $response->add_links( $links ); if ( ! empty( $links['self']['href'] ) ) { $actions = $this->get_available_actions(); @@ -664,7 +664,6 @@ public function prepare_item_for_response( $item, $request ) { // phpcs:ignore V return $response; } - /** * Prepares links for the request. * @@ -673,12 +672,12 @@ public function prepare_item_for_response( $item, $request ) { // phpcs:ignore V * @param integer $id ID. * @return array Links for the given post. */ - protected function prepare_links( $id ) { + protected function prepare_links( $template ) { $base = sprintf( '%s/%s', $this->namespace, $this->rest_base ); $links = array( 'self' => array( - 'href' => rest_url( trailingslashit( $base ) . $id ), + 'href' => rest_url( trailingslashit( $base ) . $template->id ), ), 'collection' => array( 'href' => rest_url( $base ), @@ -688,6 +687,20 @@ protected function prepare_links( $id ) { ), ); + if ( post_type_supports( $this->post_type, 'author' ) && ! empty( $template->author ) ) { + $links['author'] = array( + 'href' => rest_url( 'wp/v2/users/' . $template->author ), + 'embeddable' => true, + ); + } + + if ( 'theme' === $template->source || 'theme' === $template->origin ) { + $links['theme'] = array( + 'href' => rest_url( 'wp/v2/themes/' . $template->theme ), + 'embeddable' => true, + ); + } + return $links; } @@ -857,7 +870,7 @@ public function get_item_schema() { 'context' => array( 'embed', 'view', 'edit' ), 'readonly' => true, ), - 'author' => array( + 'author' => array( 'description' => __( 'The ID for the author of the template.' ), 'type' => 'integer', 'context' => array( 'view', 'edit', 'embed' ),