Coding Standards: Use consistent placement for `::prepare_links()` methods.

This moves the `::prepare_links()` methods in REST API classes next to `::prepare_item_for_response()` where they are used, to bring some consistency across the classes and make code navigation easier.

Includes wrapping some long lines for better readability.

Follow-up to [52079], [52051], [52342], [53721], [53722].

See #55647.
Built from https://develop.svn.wordpress.org/trunk@53724


git-svn-id: http://core.svn.wordpress.org/trunk@53283 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2022-07-19 16:22:09 +00:00
parent 69a8ecce33
commit 81b7b229ed
8 changed files with 126 additions and 110 deletions

View File

@ -653,7 +653,14 @@ class WP_REST_Application_Passwords_Controller extends WP_REST_Controller {
protected function prepare_links( WP_User $user, $item ) { protected function prepare_links( WP_User $user, $item ) {
return array( return array(
'self' => array( 'self' => array(
'href' => rest_url( sprintf( '%s/users/%d/application-passwords/%s', $this->namespace, $user->ID, $item['uuid'] ) ), 'href' => rest_url(
sprintf(
'%s/users/%d/application-passwords/%s',
$this->namespace,
$user->ID,
$item['uuid']
)
),
), ),
); );
} }

View File

@ -347,7 +347,11 @@ class WP_REST_Block_Types_Controller extends WP_REST_Controller {
if ( $block_type->is_dynamic() ) { if ( $block_type->is_dynamic() ) {
$links['https://api.w.org/render-block'] = array( $links['https://api.w.org/render-block'] = array(
'href' => add_query_arg( 'context', 'edit', rest_url( sprintf( '%s/%s/%s', 'wp/v2', 'block-renderer', $block_type->name ) ) ), 'href' => add_query_arg(
'context',
'edit',
rest_url( sprintf( '%s/%s/%s', 'wp/v2', 'block-renderer', $block_type->name ) )
),
); );
} }

View File

@ -125,6 +125,34 @@ class WP_REST_Global_Styles_Controller extends WP_REST_Controller {
return urldecode( $id_or_stylesheet ); return urldecode( $id_or_stylesheet );
} }
/**
* Get the post, if the ID is valid.
*
* @since 5.9.0
*
* @param int $id Supplied ID.
* @return WP_Post|WP_Error Post object if ID is valid, WP_Error otherwise.
*/
protected function get_post( $id ) {
$error = new WP_Error(
'rest_global_styles_not_found',
__( 'No global styles config exist with that id.' ),
array( 'status' => 404 )
);
$id = (int) $id;
if ( $id <= 0 ) {
return $error;
}
$post = get_post( $id );
if ( empty( $post ) || empty( $post->ID ) || $this->post_type !== $post->post_type ) {
return $error;
}
return $post;
}
/** /**
* Checks if a given request has access to read a single global style. * Checks if a given request has access to read a single global style.
* *
@ -377,35 +405,6 @@ class WP_REST_Global_Styles_Controller extends WP_REST_Controller {
return $response; return $response;
} }
/**
* Get the post, if the ID is valid.
*
* @since 5.9.0
*
* @param int $id Supplied ID.
* @return WP_Post|WP_Error Post object if ID is valid, WP_Error otherwise.
*/
protected function get_post( $id ) {
$error = new WP_Error(
'rest_global_styles_not_found',
__( 'No global styles config exist with that id.' ),
array( 'status' => 404 )
);
$id = (int) $id;
if ( $id <= 0 ) {
return $error;
}
$post = get_post( $id );
if ( empty( $post ) || empty( $post->ID ) || $this->post_type !== $post->post_type ) {
return $error;
}
return $post;
}
/** /**
* Prepares links for the request. * Prepares links for the request.
* *

View File

@ -207,6 +207,44 @@ class WP_REST_Menu_Locations_Controller extends WP_REST_Controller {
return apply_filters( 'rest_prepare_menu_location', $response, $location, $request ); return apply_filters( 'rest_prepare_menu_location', $response, $location, $request );
} }
/**
* Prepares links for the request.
*
* @since 5.9.0
*
* @param stdClass $location Menu location.
* @return array Links for the given menu location.
*/
protected function prepare_links( $location ) {
$base = sprintf( '%s/%s', $this->namespace, $this->rest_base );
// Entity meta.
$links = array(
'self' => array(
'href' => rest_url( trailingslashit( $base ) . $location->name ),
),
'collection' => array(
'href' => rest_url( $base ),
),
);
$locations = get_nav_menu_locations();
$menu = isset( $locations[ $location->name ] ) ? $locations[ $location->name ] : 0;
if ( $menu ) {
$path = rest_get_route_for_term( $menu );
if ( $path ) {
$url = rest_url( $path );
$links['https://api.w.org/menu'][] = array(
'href' => $url,
'embeddable' => true,
);
}
}
return $links;
}
/** /**
* Retrieves the menu location's schema, conforming to JSON Schema. * Retrieves the menu location's schema, conforming to JSON Schema.
* *
@ -260,42 +298,4 @@ class WP_REST_Menu_Locations_Controller extends WP_REST_Controller {
'context' => $this->get_context_param( array( 'default' => 'view' ) ), 'context' => $this->get_context_param( array( 'default' => 'view' ) ),
); );
} }
/**
* Prepares links for the request.
*
* @since 5.9.0
*
* @param stdClass $location Menu location.
* @return array Links for the given menu location.
*/
protected function prepare_links( $location ) {
$base = sprintf( '%s/%s', $this->namespace, $this->rest_base );
// Entity meta.
$links = array(
'self' => array(
'href' => rest_url( trailingslashit( $base ) . $location->name ),
),
'collection' => array(
'href' => rest_url( $base ),
),
);
$locations = get_nav_menu_locations();
$menu = isset( $locations[ $location->name ] ) ? $locations[ $location->name ] : 0;
if ( $menu ) {
$path = rest_get_route_for_term( $menu );
if ( $path ) {
$url = rest_url( $path );
$links['https://api.w.org/menu'][] = array(
'href' => $url,
'embeddable' => true,
);
}
}
return $links;
}
} }

View File

@ -625,7 +625,14 @@ class WP_REST_Plugins_Controller extends WP_REST_Controller {
protected function prepare_links( $item ) { protected function prepare_links( $item ) {
return array( return array(
'self' => array( 'self' => array(
'href' => rest_url( sprintf( '%s/%s/%s', $this->namespace, $this->rest_base, substr( $item['_file'], 0, - 4 ) ) ), 'href' => rest_url(
sprintf(
'%s/%s/%s',
$this->namespace,
$this->rest_base,
substr( $item['_file'], 0, - 4 )
)
),
), ),
); );
} }

View File

@ -260,6 +260,25 @@ class WP_REST_Post_Types_Controller extends WP_REST_Controller {
return apply_filters( 'rest_prepare_post_type', $response, $post_type, $request ); return apply_filters( 'rest_prepare_post_type', $response, $post_type, $request );
} }
/**
* Prepares links for the request.
*
* @since 6.1.0
*
* @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(
'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 ) ),
),
);
}
/** /**
* Retrieves the post type's schema, conforming to JSON Schema. * Retrieves the post type's schema, conforming to JSON Schema.
* *
@ -384,23 +403,4 @@ class WP_REST_Post_Types_Controller extends WP_REST_Controller {
'context' => $this->get_context_param( array( 'default' => 'view' ) ), 'context' => $this->get_context_param( array( 'default' => 'view' ) ),
); );
} }
/**
* Prepares links for the request.
*
* @since 6.1.0
*
* @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(
'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 ) ),
),
);
}
} }

View File

@ -288,6 +288,25 @@ class WP_REST_Taxonomies_Controller extends WP_REST_Controller {
return apply_filters( 'rest_prepare_taxonomy', $response, $taxonomy, $request ); return apply_filters( 'rest_prepare_taxonomy', $response, $taxonomy, $request );
} }
/**
* Prepares links for the request.
*
* @since 6.1.0
*
* @param @param WP_Taxonomy $taxonomy The taxonomy.
* @return array Links for the given taxonomy.
*/
protected function prepare_links( $taxonomy ) {
return array(
'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_taxonomy_items( $taxonomy->name ) ),
),
);
}
/** /**
* Retrieves the taxonomy's schema, conforming to JSON Schema. * Retrieves the taxonomy's schema, conforming to JSON Schema.
* *
@ -427,24 +446,4 @@ class WP_REST_Taxonomies_Controller extends WP_REST_Controller {
); );
return $new_params; return $new_params;
} }
/**
* Prepares links for the request.
*
* @since 6.1.0
*
* @param @param WP_Taxonomy $taxonomy The taxonomy.
* @return array Links for the given taxonomy.
*/
protected function prepare_links( $taxonomy ) {
return array(
'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_taxonomy_items( $taxonomy->name ) ),
),
);
}
} }

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '6.1-alpha-53723'; $wp_version = '6.1-alpha-53724';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.