Skip to content

Commit e49f452

Browse files
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. git-svn-id: https://develop.svn.wordpress.org/trunk@53724 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 788a070 commit e49f452

7 files changed

Lines changed: 125 additions & 109 deletions

src/wp-includes/rest-api/endpoints/class-wp-rest-application-passwords-controller.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,14 @@ public function prepare_item_for_response( $item, $request ) {
653653
protected function prepare_links( WP_User $user, $item ) {
654654
return array(
655655
'self' => array(
656-
'href' => rest_url( sprintf( '%s/users/%d/application-passwords/%s', $this->namespace, $user->ID, $item['uuid'] ) ),
656+
'href' => rest_url(
657+
sprintf(
658+
'%s/users/%d/application-passwords/%s',
659+
$this->namespace,
660+
$user->ID,
661+
$item['uuid']
662+
)
663+
),
657664
),
658665
);
659666
}

src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,11 @@ protected function prepare_links( $block_type ) {
347347

348348
if ( $block_type->is_dynamic() ) {
349349
$links['https://api.w.org/render-block'] = array(
350-
'href' => add_query_arg( 'context', 'edit', rest_url( sprintf( '%s/%s/%s', 'wp/v2', 'block-renderer', $block_type->name ) ) ),
350+
'href' => add_query_arg(
351+
'context',
352+
'edit',
353+
rest_url( sprintf( '%s/%s/%s', 'wp/v2', 'block-renderer', $block_type->name ) )
354+
),
351355
);
352356
}
353357

src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,34 @@ public function _sanitize_global_styles_callback( $id_or_stylesheet ) {
125125
return urldecode( $id_or_stylesheet );
126126
}
127127

128+
/**
129+
* Get the post, if the ID is valid.
130+
*
131+
* @since 5.9.0
132+
*
133+
* @param int $id Supplied ID.
134+
* @return WP_Post|WP_Error Post object if ID is valid, WP_Error otherwise.
135+
*/
136+
protected function get_post( $id ) {
137+
$error = new WP_Error(
138+
'rest_global_styles_not_found',
139+
__( 'No global styles config exist with that id.' ),
140+
array( 'status' => 404 )
141+
);
142+
143+
$id = (int) $id;
144+
if ( $id <= 0 ) {
145+
return $error;
146+
}
147+
148+
$post = get_post( $id );
149+
if ( empty( $post ) || empty( $post->ID ) || $this->post_type !== $post->post_type ) {
150+
return $error;
151+
}
152+
153+
return $post;
154+
}
155+
128156
/**
129157
* Checks if a given request has access to read a single global style.
130158
*
@@ -377,35 +405,6 @@ public function prepare_item_for_response( $post, $request ) { // phpcs:ignore V
377405
return $response;
378406
}
379407

380-
/**
381-
* Get the post, if the ID is valid.
382-
*
383-
* @since 5.9.0
384-
*
385-
* @param int $id Supplied ID.
386-
* @return WP_Post|WP_Error Post object if ID is valid, WP_Error otherwise.
387-
*/
388-
protected function get_post( $id ) {
389-
$error = new WP_Error(
390-
'rest_global_styles_not_found',
391-
__( 'No global styles config exist with that id.' ),
392-
array( 'status' => 404 )
393-
);
394-
395-
$id = (int) $id;
396-
if ( $id <= 0 ) {
397-
return $error;
398-
}
399-
400-
$post = get_post( $id );
401-
if ( empty( $post ) || empty( $post->ID ) || $this->post_type !== $post->post_type ) {
402-
return $error;
403-
}
404-
405-
return $post;
406-
}
407-
408-
409408
/**
410409
* Prepares links for the request.
411410
*

src/wp-includes/rest-api/endpoints/class-wp-rest-menu-locations-controller.php

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,44 @@ public function prepare_item_for_response( $item, $request ) {
207207
return apply_filters( 'rest_prepare_menu_location', $response, $location, $request );
208208
}
209209

210+
/**
211+
* Prepares links for the request.
212+
*
213+
* @since 5.9.0
214+
*
215+
* @param stdClass $location Menu location.
216+
* @return array Links for the given menu location.
217+
*/
218+
protected function prepare_links( $location ) {
219+
$base = sprintf( '%s/%s', $this->namespace, $this->rest_base );
220+
221+
// Entity meta.
222+
$links = array(
223+
'self' => array(
224+
'href' => rest_url( trailingslashit( $base ) . $location->name ),
225+
),
226+
'collection' => array(
227+
'href' => rest_url( $base ),
228+
),
229+
);
230+
231+
$locations = get_nav_menu_locations();
232+
$menu = isset( $locations[ $location->name ] ) ? $locations[ $location->name ] : 0;
233+
if ( $menu ) {
234+
$path = rest_get_route_for_term( $menu );
235+
if ( $path ) {
236+
$url = rest_url( $path );
237+
238+
$links['https://api.w.org/menu'][] = array(
239+
'href' => $url,
240+
'embeddable' => true,
241+
);
242+
}
243+
}
244+
245+
return $links;
246+
}
247+
210248
/**
211249
* Retrieves the menu location's schema, conforming to JSON Schema.
212250
*
@@ -260,42 +298,4 @@ public function get_collection_params() {
260298
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
261299
);
262300
}
263-
264-
/**
265-
* Prepares links for the request.
266-
*
267-
* @since 5.9.0
268-
*
269-
* @param stdClass $location Menu location.
270-
* @return array Links for the given menu location.
271-
*/
272-
protected function prepare_links( $location ) {
273-
$base = sprintf( '%s/%s', $this->namespace, $this->rest_base );
274-
275-
// Entity meta.
276-
$links = array(
277-
'self' => array(
278-
'href' => rest_url( trailingslashit( $base ) . $location->name ),
279-
),
280-
'collection' => array(
281-
'href' => rest_url( $base ),
282-
),
283-
);
284-
285-
$locations = get_nav_menu_locations();
286-
$menu = isset( $locations[ $location->name ] ) ? $locations[ $location->name ] : 0;
287-
if ( $menu ) {
288-
$path = rest_get_route_for_term( $menu );
289-
if ( $path ) {
290-
$url = rest_url( $path );
291-
292-
$links['https://api.w.org/menu'][] = array(
293-
'href' => $url,
294-
'embeddable' => true,
295-
);
296-
}
297-
}
298-
299-
return $links;
300-
}
301301
}

src/wp-includes/rest-api/endpoints/class-wp-rest-plugins-controller.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,14 @@ public function prepare_item_for_response( $item, $request ) {
625625
protected function prepare_links( $item ) {
626626
return array(
627627
'self' => array(
628-
'href' => rest_url( sprintf( '%s/%s/%s', $this->namespace, $this->rest_base, substr( $item['_file'], 0, - 4 ) ) ),
628+
'href' => rest_url(
629+
sprintf(
630+
'%s/%s/%s',
631+
$this->namespace,
632+
$this->rest_base,
633+
substr( $item['_file'], 0, - 4 )
634+
)
635+
),
629636
),
630637
);
631638
}

src/wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,25 @@ public function prepare_item_for_response( $item, $request ) {
260260
return apply_filters( 'rest_prepare_post_type', $response, $post_type, $request );
261261
}
262262

263+
/**
264+
* Prepares links for the request.
265+
*
266+
* @since 6.1.0
267+
*
268+
* @param WP_Post_Type $post_type The post type.
269+
* @return array Links for the given post type.
270+
*/
271+
protected function prepare_links( $post_type ) {
272+
return array(
273+
'collection' => array(
274+
'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
275+
),
276+
'https://api.w.org/items' => array(
277+
'href' => rest_url( rest_get_route_for_post_type_items( $post_type->name ) ),
278+
),
279+
);
280+
}
281+
263282
/**
264283
* Retrieves the post type's schema, conforming to JSON Schema.
265284
*
@@ -384,23 +403,4 @@ public function get_collection_params() {
384403
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
385404
);
386405
}
387-
388-
/**
389-
* Prepares links for the request.
390-
*
391-
* @since 6.1.0
392-
*
393-
* @param WP_Post_Type $post_type The post type.
394-
* @return array Links for the given post type.
395-
*/
396-
protected function prepare_links( $post_type ) {
397-
return array(
398-
'collection' => array(
399-
'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
400-
),
401-
'https://api.w.org/items' => array(
402-
'href' => rest_url( rest_get_route_for_post_type_items( $post_type->name ) ),
403-
),
404-
);
405-
}
406406
}

src/wp-includes/rest-api/endpoints/class-wp-rest-taxonomies-controller.php

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,25 @@ public function prepare_item_for_response( $item, $request ) {
288288
return apply_filters( 'rest_prepare_taxonomy', $response, $taxonomy, $request );
289289
}
290290

291+
/**
292+
* Prepares links for the request.
293+
*
294+
* @since 6.1.0
295+
*
296+
* @param @param WP_Taxonomy $taxonomy The taxonomy.
297+
* @return array Links for the given taxonomy.
298+
*/
299+
protected function prepare_links( $taxonomy ) {
300+
return array(
301+
'collection' => array(
302+
'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
303+
),
304+
'https://api.w.org/items' => array(
305+
'href' => rest_url( rest_get_route_for_taxonomy_items( $taxonomy->name ) ),
306+
),
307+
);
308+
}
309+
291310
/**
292311
* Retrieves the taxonomy's schema, conforming to JSON Schema.
293312
*
@@ -427,24 +446,4 @@ public function get_collection_params() {
427446
);
428447
return $new_params;
429448
}
430-
431-
/**
432-
* Prepares links for the request.
433-
*
434-
* @since 6.1.0
435-
*
436-
* @param @param WP_Taxonomy $taxonomy The taxonomy.
437-
* @return array Links for the given taxonomy.
438-
*/
439-
protected function prepare_links( $taxonomy ) {
440-
return array(
441-
'collection' => array(
442-
'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
443-
),
444-
'https://api.w.org/items' => array(
445-
'href' => rest_url( rest_get_route_for_taxonomy_items( $taxonomy->name ) ),
446-
),
447-
);
448-
}
449-
450449
}

0 commit comments

Comments
 (0)