Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,15 @@ public function prepare_item_for_response( $ability, $request ) {
),
);

$links['wp:action-run'] = array(
$category = $ability->get_category();
if ( '' !== $category ) {
$links['up'] = array(
'href' => rest_url( sprintf( '%s/categories/%s', $this->namespace, $category ) ),
'embeddable' => true,
);
}

$links['https://api.w.org/action-run'] = array(
'href' => rest_url( sprintf( '%s/%s/%s/run', $this->namespace, $this->rest_base, $ability->get_name() ) ),
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -650,22 +650,25 @@ public function prepare_item_for_response( $item, $request ) {
* Prepares links for the request.
*
* @since 5.6.0
* @since 7.2.0 Added the `collection` and `up` links.
*
* @param WP_User $user The requested user.
* @param array $item The application password.
* @return array The list of links.
*/
protected function prepare_links( WP_User $user, $item ) {
$collection = sprintf( '%s/users/%d/application-passwords', $this->namespace, $user->ID );

return array(
'self' => array(
'href' => rest_url(
sprintf(
'%s/users/%d/application-passwords/%s',
$this->namespace,
$user->ID,
$item['uuid']
)
),
'self' => array(
'href' => rest_url( $collection . '/' . $item['uuid'] ),
),
'collection' => array(
'href' => rest_url( $collection ),
),
'up' => array(
'href' => rest_url( sprintf( '%s/users/%d', $this->namespace, $user->ID ) ),
'embeddable' => true,
),
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ class WP_REST_Autosaves_Controller extends WP_REST_Revisions_Controller {
* The base of the parent controller's route.
*
* @since 5.0.0
* @since 7.2.0 Changed from private to protected.
* @var string
*/
private $parent_base;
protected $parent_base;

/**
* Constructor.
Expand Down Expand Up @@ -472,6 +473,17 @@ public function prepare_item_for_response( $item, $request ) {
$response = $this->revisions_controller->prepare_item_for_response( $post, $request );
$fields = $this->get_fields_for_response( $request );

if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
// The revisions controller links to the revisions routes; point them at the autosaves routes instead.
$links = $this->prepare_links( $post );

foreach ( array_keys( $links ) as $rel ) {
$response->remove_link( $rel );
}

$response->add_links( $links );
}

if ( in_array( 'preview_link', $fields, true ) ) {
$parent_id = wp_is_post_autosave( $post );
$preview_post_id = false === $parent_id ? $post->ID : $parent_id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,7 @@ protected function get_parent_font_family_post( $font_family_id ) {
* Prepares links for the request.
*
* @since 6.5.0
* @since 7.2.0 Added the `about` link.
*
* @param WP_Post $post Post object.
* @return array Links for the given post.
Expand All @@ -809,6 +810,9 @@ protected function prepare_links( $post ) {
'collection' => array(
'href' => rest_url( $this->namespace . '/font-families/' . $post->post_parent . '/font-faces' ),
),
'about' => array(
'href' => rest_url( 'wp/v2/types/' . $this->post_type ),
),
'parent' => array(
'href' => rest_url( $this->namespace . '/font-families/' . $post->post_parent ),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@ protected function get_font_face_ids( $font_family_id ) {
* Prepares font family links for the request.
*
* @since 6.5.0
* @since 7.2.0 Added the `about` link.
*
* @param WP_Post $post Post object.
* @return array Links for the given post.
Expand All @@ -490,6 +491,7 @@ protected function prepare_links( $post ) {
return array(
'self' => $links['self'],
'collection' => $links['collection'],
'about' => $links['about'],
'font_faces' => $this->prepare_font_face_links( $post->ID ),
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ public function prepare_item_for_response( $post, $request ) {
*
* @since 5.9.0
* @since 6.3.0 Adds revisions count and rest URL href to version-history.
* @since 7.2.0 Added the `predecessor-version` and `https://api.w.org/theme` links.
*
* @param int $id ID.
* @return array Links for the given post.
Expand All @@ -411,6 +412,22 @@ protected function prepare_links( $id ) {
'href' => rest_url( $revisions_base ),
'count' => $revisions_count,
);

if ( $revisions_count > 0 ) {
$links['predecessor-version'] = array(
'href' => rest_url( $revisions_base . '/' . $revisions['latest_id'] ),
'id' => $revisions['latest_id'],
);
}
}

// The `wp_theme` term name is the stylesheet of the theme the styles belong to.
$theme_terms = get_the_terms( $id, 'wp_theme' );

if ( is_array( $theme_terms ) && ! empty( $theme_terms ) ) {
$links['https://api.w.org/theme'] = array(
'href' => rest_url( sprintf( 'wp/v2/themes/%s', $theme_terms[0]->name ) ),
);
}

return $links;
Expand Down Expand Up @@ -593,9 +610,15 @@ public function get_theme_item( $request ) {

if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
$links = array(
'self' => array(
'self' => array(
'href' => rest_url( sprintf( '%s/%s/themes/%s', $this->namespace, $this->rest_base, $request['stylesheet'] ) ),
),
'https://api.w.org/theme' => array(
'href' => rest_url( 'wp/v2/themes/' . $request['stylesheet'] ),
),
'https://api.w.org/theme-style-variations' => array(
'href' => rest_url( sprintf( '%s/%s/themes/%s/variations', $this->namespace, $this->rest_base, $request['stylesheet'] ) ),
),
);
$resolved_theme_uris = WP_Theme_JSON_Resolver::get_resolved_theme_uris( $theme );
if ( ! empty( $resolved_theme_uris ) ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ public function get_items( $request ) {
*
* @since 6.3.0
* @since 6.6.0 Added resolved URI links to the response.
* @since 7.2.0 Added the `self`, `collection`, `parent`, and `author` links.
*
* @param WP_Post $post Post revision object.
* @param WP_REST_Request $request Request object.
Expand Down Expand Up @@ -365,6 +366,10 @@ public function prepare_item_for_response( $post, $request ) {
$response = rest_ensure_response( $data );
$resolved_theme_uris = WP_Theme_JSON_Resolver::get_resolved_theme_uris( $theme_json );

if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
$response->add_links( $this->prepare_links( $post ) );
}

if ( ! empty( $resolved_theme_uris ) ) {
$response->add_links(
array(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,38 @@ public function prepare_item_for_response( $item, $request ) {
}
}

$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );
return rest_ensure_response( $data );
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );
$response = rest_ensure_response( $data );

if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
$response->add_links( $this->prepare_links( $item ) );
}

return $response;
}

/**
* Prepares links for the request.
*
* @since 7.2.0
*
* @param array $item Raw icon collection as registered.
* @return array Links for the given icon collection.
*/
protected function prepare_links( $item ) {
return array(
'self' => array(
'href' => rest_url( sprintf( '%s/%s/%s', $this->namespace, $this->rest_base, $item['slug'] ) ),
),
'collection' => array(
'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
),
'https://api.w.org/items' => array(
'href' => rest_url( sprintf( '%s/icons/%s', $this->namespace, $item['slug'] ) ),
),
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,44 @@ public function prepare_item_for_response( $item, $request ) {
$data['keywords'] = isset( $item['keywords'] ) ? array_values( $item['keywords'] ) : array();
}

$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );
return rest_ensure_response( $data );
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );
$response = rest_ensure_response( $data );

if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
$response->add_links( $this->prepare_links( $item ) );
}

return $response;
}

/**
* Prepares links for the request.
*
* @since 7.2.0
*
* @param array $item Raw icon as registered.
* @return array Links for the given icon.
*/
protected function prepare_links( $item ) {
$links = array(
'self' => array(
'href' => rest_url( sprintf( '%s/%s/%s', $this->namespace, $this->rest_base, $item['name'] ) ),
),
'collection' => array(
'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
),
);

if ( ! empty( $item['collection'] ) ) {
$links['up'] = array(
'href' => rest_url( sprintf( '%s/icon-collections/%s', $this->namespace, $item['collection'] ) ),
'embeddable' => true,
);
}

return $links;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,7 @@ public function prepare_item_for_response( $item, $request ) {
* Prepares links for the request.
*
* @since 5.9.0
* @since 7.2.0 Added the `up` link to the parent menu item.
*
* @param WP_Post $post Post object.
* @return array Links for the given post.
Expand All @@ -665,6 +666,18 @@ protected function prepare_links( $post ) {
$links = parent::prepare_links( $post );
$menu_item = $this->get_nav_menu_item( $post->ID );

// Menu items store their parent in meta rather than `post_parent`, so the posts controller cannot link it.
if ( ! empty( $menu_item->menu_item_parent ) ) {
$parent_route = rest_get_route_for_post( (int) $menu_item->menu_item_parent );

if ( $parent_route ) {
$links['up'] = array(
'href' => rest_url( $parent_route ),
'embeddable' => true,
);
}
}

if ( empty( $menu_item->object_id ) ) {
return $links;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -623,13 +623,14 @@ public function prepare_item_for_response( $item, $request ) {
* Prepares links for the request.
*
* @since 5.5.0
* @since 7.2.0 Added the `collection` link.
*
* @param array $item The plugin item.
* @return array[]
*/
protected function prepare_links( $item ) {
return array(
'self' => array(
'self' => array(
'href' => rest_url(
sprintf(
'%s/%s/%s',
Expand All @@ -639,6 +640,9 @@ protected function prepare_links( $item ) {
)
),
),
'collection' => array(
'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
),
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,9 @@ public function prepare_item_for_response( $item, $request ) {

$response = rest_ensure_response( $data );

$response->add_link( 'self', rest_url( sprintf( '%s/%s/%s', $this->namespace, $this->rest_base, $status->name ) ) );
$response->add_link( 'collection', rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ) );

$rest_url = rest_url( rest_get_route_for_post_type_items( 'post' ) );
if ( 'publish' === $status->name ) {
$response->add_link( 'archives', $rest_url );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,19 +294,34 @@ public function prepare_item_for_response( $item, $request ) {
* Prepares links for the request.
*
* @since 6.1.0
* @since 7.2.0 Added the `self` and `https://api.w.org/taxonomy` links.
*
* @param WP_Post_Type $post_type The post type.
* @return array Links for the given post type.
*/
protected function prepare_links( $post_type ) {
return array(
$links = array(
'self' => array(
'href' => rest_url( sprintf( '%s/%s/%s', $this->namespace, $this->rest_base, $post_type->name ) ),
),
'collection' => array(
'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
),
'https://api.w.org/items' => array(
'href' => rest_url( rest_get_route_for_post_type_items( $post_type->name ) ),
),
);

foreach ( get_object_taxonomies( $post_type->name, 'objects' ) as $taxonomy ) {
if ( $taxonomy->show_in_rest ) {
$links['https://api.w.org/taxonomy'][] = array(
'href' => rest_url( 'wp/v2/taxonomies/' . $taxonomy->name ),
'taxonomy' => $taxonomy->name,
);
}
}

return $links;
}

/**
Expand Down
Loading
Loading