Skip to content

Commit

Permalink
Update Font Library REST API code to align with Core standards (#58619)
Browse files Browse the repository at this point in the history
* Remove empty line after @return

* Fix comment format

* Applying fixes from #58607

* Improve readability.

---------

Co-authored-by: Anton Vlasenko <[email protected]>
Co-authored-by: getdave <[email protected]>
Co-authored-by: anton-vlasenko <[email protected]>
Co-authored-by: pbking <[email protected]>
Co-authored-by: matiasbenedetto <[email protected]>
  • Loading branch information
6 people authored Feb 2, 2024
1 parent 5206fb5 commit 38fbc7f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function get_items( $request ) {
if ( $page > $max_pages && $total_items > 0 ) {
return new WP_Error(
'rest_post_invalid_page_number',
__( 'The page number requested is larger than the number of pages available.', 'default' ),
__( 'The page number requested is larger than the number of pages available.' ),
array( 'status' => 400 )
);
}
Expand Down Expand Up @@ -150,23 +150,17 @@ public function get_item( $request ) {
return $collection;
}

$item = $this->prepare_item_for_response( $collection, $request );

if ( is_wp_error( $item ) ) {
return $item;
}

return $item;
return $this->prepare_item_for_response( $collection, $request );
}

/*
/**
* Prepare a single collection output for response.
*
* @since 6.5.0
*
* @param WP_Font_Collection $collection Collection object.
* @param WP_REST_Request $request Request object.
* @return array|WP_Error
* @return WP_REST_Response Response object.
*/
public function prepare_item_for_response( $collection, $request ) {
$fields = $this->get_fields_for_response( $request );
Expand Down Expand Up @@ -210,9 +204,9 @@ public function prepare_item_for_response( $collection, $request ) {
*
* @since 6.5.0
*
* @param WP_REST_Response $response The response object.
* @param WP_REST_Response $response The response object.
* @param WP_Font_Collection $collection The Font Collection object.
* @param WP_REST_Request $request Request used to generate the response.
* @param WP_REST_Request $request Request used to generate the response.
*/
return apply_filters( 'rest_prepare_font_collection', $response, $collection, $request );
}
Expand Down Expand Up @@ -277,15 +271,14 @@ public function get_item_schema() {
* @return array Links for the given font collection.
*/
protected function prepare_links( $collection ) {
$links = array(
return array(
'self' => array(
'href' => rest_url( sprintf( '%s/%s/%s', $this->namespace, $this->rest_base, $collection->slug ) ),
),
'collection' => array(
'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
),
);
return $links;
}

/**
Expand Down Expand Up @@ -320,17 +313,17 @@ public function get_collection_params() {
* @return true|WP_Error True if the request has write access for the item, WP_Error object otherwise.
*/
public function get_items_permissions_check( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable

if ( ! current_user_can( 'edit_theme_options' ) ) {
return new WP_Error(
'rest_cannot_read',
__( 'Sorry, you are not allowed to use the Font Library on this site.', 'gutenberg' ),
array(
'status' => rest_authorization_required_code(),
)
);
if ( current_user_can( 'edit_theme_options' ) ) {
return true;
}
return true;

return new WP_Error(
'rest_cannot_read',
__( 'Sorry, you are not allowed to use the Font Library on this site.', 'gutenberg' ),
array(
'status' => rest_authorization_required_code(),
)
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ protected function get_parent_font_family_post( $font_family_id ) {
*/
protected function prepare_links( $post ) {
// Entity meta.
$links = array(
return array(
'self' => array(
'href' => rest_url( $this->namespace . '/font-families/' . $post->post_parent . '/font-faces/' . $post->ID ),
),
Expand All @@ -709,8 +709,6 @@ protected function prepare_links( $post ) {
'href' => rest_url( $this->namespace . '/font-families/' . $post->post_parent ),
),
);

return $links;
}

/**
Expand Down Expand Up @@ -788,7 +786,7 @@ public function handle_font_file_upload_error( $file, $message ) {
$status = 500;
$code = 'rest_font_upload_unknown_error';

if ( __( 'Sorry, you are not allowed to upload this file type.', 'default' ) === $message ) {
if ( __( 'Sorry, you are not allowed to upload this file type.' ) === $message ) {
$status = 400;
$code = 'rest_font_upload_invalid_file_type';
}
Expand All @@ -799,7 +797,7 @@ public function handle_font_file_upload_error( $file, $message ) {
/**
* Returns relative path to an uploaded font file.
*
* The path is relative to the current fonts dir.
* The path is relative to the current fonts directory.
*
* @since 6.5.0
* @access private
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,6 @@ public function get_endpoint_args_for_item_schema( $method = WP_REST_Server::CRE
*
* @param int $font_family_id Font family post ID.
* @return int[] Array of child font face post IDs.
* .
*/
protected function get_font_face_ids( $font_family_id ) {
$query = new WP_Query(
Expand Down

0 comments on commit 38fbc7f

Please sign in to comment.