Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Updates from gutenberg #58752, #58735, and #58736 to address feedback…
… comments
  • Loading branch information
creativecoder committed Feb 7, 2024
commit a0836f0f6b4fffc3259a2711f2ea43b02823a6e2
5 changes: 2 additions & 3 deletions src/wp-includes/fonts/class-wp-font-library.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,13 @@ public function get_font_collections() {
* @since 6.5.0
*
* @param string $slug Font collection slug.
* @return WP_Font_Collection|WP_Error Font collection object,
* or WP_Error object if the font collection doesn't exist.
* @return WP_Font_Collection|null Font collection object, or null if the font collection doesn't exist.
*/
public function get_font_collection( $slug ) {
if ( $this->is_collection_registered( $slug ) ) {
return $this->collections[ $slug ];
}
return new WP_Error( 'font_collection_not_found', __( 'Font collection not found.' ) );
return null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,8 @@ public function get_item( $request ) {
$slug = $request->get_param( 'slug' );
$collection = WP_Font_Library::get_instance()->get_font_collection( $slug );

// If the collection doesn't exist returns a 404.
if ( is_wp_error( $collection ) ) {
$collection->add_data( array( 'status' => 404 ) );
return $collection;
if ( ! $collection ) {
return new WP_Error( 'rest_font_collection_not_found', __( 'Font collection not found.' ), array( 'status' => 404 ) );
}

return $this->prepare_item_for_response( $collection, $request );
Expand All @@ -156,38 +154,38 @@ public function get_item( $request ) {
*
* @since 6.5.0
*
* @param WP_Font_Collection $collection Collection object.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response Response object.
* @param WP_Font_Collection $item Font collection object.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/
public function prepare_item_for_response( $collection, $request ) {
public function prepare_item_for_response( $item, $request ) {
$fields = $this->get_fields_for_response( $request );
$item = array();
$data = array();

if ( rest_is_field_included( 'slug', $fields ) ) {
$item['slug'] = $collection->slug;
$data['slug'] = $item->slug;
}

// If any data fields are requested, get the collection data.
$data_fields = array( 'name', 'description', 'font_families', 'categories' );
if ( ! empty( array_intersect( $fields, $data_fields ) ) ) {
$collection_data = $collection->get_data();
$collection_data = $item->get_data();
if ( is_wp_error( $collection_data ) ) {
$collection_data->add_data( array( 'status' => 500 ) );
return $collection_data;
}

foreach ( $data_fields as $field ) {
if ( rest_is_field_included( $field, $fields ) ) {
$item[ $field ] = $collection_data[ $field ];
$data[ $field ] = $collection_data[ $field ];
}
}
}

$response = rest_ensure_response( $item );
$response = rest_ensure_response( $data );

if ( rest_is_field_included( '_links', $fields ) ) {
$links = $this->prepare_links( $collection );
$links = $this->prepare_links( $item );
$response->add_links( $links );
}

Expand All @@ -196,17 +194,15 @@ public function prepare_item_for_response( $collection, $request ) {
$response->data = $this->filter_response_by_context( $response->data, $context );

/**
* Filters a font collection returned from the REST API.
*
* Allows modification of the font collection right before it is returned.
* Filters the font collection data for a REST API response.
*
* @since 6.5.0
*
* @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_Response $response The response object.
* @param WP_Font_Collection $item The font collection object.
* @param WP_REST_Request $request Request used to generate the response.
*/
return apply_filters( 'rest_prepare_font_collection', $response, $collection, $request );
return apply_filters( 'rest_prepare_font_collection', $response, $item, $request );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@
* Class to access font faces through the REST API.
*/
class WP_REST_Font_Faces_Controller extends WP_REST_Posts_Controller {

/**
* The latest version of theme.json schema supported by the controller.
*
* @since 6.5.0
* @var int
*/
const LATEST_THEME_JSON_VERSION_SUPPORTED = 2;

/**
* Whether the controller supports batching.
*
Expand Down Expand Up @@ -231,9 +240,8 @@ public function validate_create_font_face_settings( $value, $request ) {
*
* @since 6.5.0
*
* @param string $value Encoded JSON string of font face settings.
* @param WP_REST_Request $request Request object.
* @return array Decoded array of font face settings.
* @param string $value Encoded JSON string of font face settings.
* @return array Decoded and sanitized array of font face settings.
*/
public function sanitize_font_face_settings( $value ) {
// Settings arrive as stringified JSON, since this is a multipart/form-data request.
Expand Down Expand Up @@ -326,7 +334,7 @@ public function create_item( $request ) {
'update_post_term_cache' => false,
)
);
if ( ! empty( $query->get_posts() ) ) {
if ( ! empty( $query->posts ) ) {
return new WP_Error(
'rest_duplicate_font_face',
__( 'A font face matching those settings already exists.' ),
Expand Down Expand Up @@ -416,7 +424,7 @@ public function delete_item( $request ) {
return new WP_Error(
'rest_trash_not_supported',
/* translators: %s: force=true */
sprintf( __( "Font faces do not support trashing. Set '%s' to delete." ), 'force=true' ),
sprintf( __( 'Font faces do not support trashing. Set "%s" to delete.' ), 'force=true' ),
array( 'status' => 501 )
);
}
Expand All @@ -441,7 +449,7 @@ public function prepare_item_for_response( $item, $request ) {
$data['id'] = $item->ID;
}
if ( rest_is_field_included( 'theme_json_version', $fields ) ) {
$data['theme_json_version'] = 2;
$data['theme_json_version'] = static::LATEST_THEME_JSON_VERSION_SUPPORTED;
}

if ( rest_is_field_included( 'parent', $fields ) ) {
Expand Down Expand Up @@ -502,9 +510,9 @@ public function get_item_schema() {
'theme_json_version' => array(
'description' => __( 'Version of the theme.json schema used for the typography settings.' ),
'type' => 'integer',
'default' => 2,
'default' => static::LATEST_THEME_JSON_VERSION_SUPPORTED,
'minimum' => 2,
'maximum' => 2,
'maximum' => static::LATEST_THEME_JSON_VERSION_SUPPORTED,
'context' => array( 'view', 'edit', 'embed' ),
),
'parent' => array(
Expand Down Expand Up @@ -697,14 +705,16 @@ public function get_collection_params() {
$query_params = parent::get_collection_params();

// Remove unneeded params.
unset( $query_params['after'] );
unset( $query_params['modified_after'] );
unset( $query_params['before'] );
unset( $query_params['modified_before'] );
unset( $query_params['search'] );
unset( $query_params['search_columns'] );
unset( $query_params['slug'] );
unset( $query_params['status'] );
unset(
$query_params['after'],
$query_params['modified_after'],
$query_params['before'],
$query_params['modified_before'],
$query_params['search'],
$query_params['search_columns'],
$query_params['slug'],
$query_params['status']
);

$query_params['orderby']['default'] = 'id';
$query_params['orderby']['enum'] = array( 'id', 'include' );
Expand Down Expand Up @@ -801,7 +811,7 @@ protected function prepare_links( $post ) {
* @since 6.5.0
*
* @param WP_REST_Request $request Request object.
* @return stdClass|WP_Error Post object or WP_Error.
* @return stdClass Post object.
*/
protected function prepare_item_for_database( $request ) {
$prepared_post = new stdClass();
Expand Down Expand Up @@ -829,7 +839,6 @@ protected function prepare_item_for_database( $request ) {
* @since 6.5.0
*
* @param string $value Font face src that is a URL or the key for a $_FILES array item.
*
* @return string Sanitized value.
*/
protected function sanitize_src( $value ) {
Expand All @@ -843,7 +852,7 @@ protected function sanitize_src( $value ) {
* @since 6.5.0
*
* @param array $file Single file item from $_FILES.
* @return array Array containing uploaded file attributes on success, or error on failure.
* @return array|WP_Error Array containing uploaded file attributes on success, or WP_Error object on failure.
*/
protected function handle_font_file_upload( $file ) {
add_filter( 'upload_mimes', array( 'WP_Font_Utils', 'get_allowed_font_mime_types' ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@
* @since 6.5.0
*/
class WP_REST_Font_Families_Controller extends WP_REST_Posts_Controller {

/**
* The latest version of theme.json schema supported by the controller.
*
* @since 6.5.0
* @var int
*/
const LATEST_THEME_JSON_VERSION_SUPPORTED = 2;

/**
* Whether the controller supports batching.
*
Expand All @@ -29,7 +38,7 @@ class WP_REST_Font_Families_Controller extends WP_REST_Posts_Controller {
* @param WP_REST_Request $request Full details about the request.
* @return true|WP_Error True if the request has read access, WP_Error object otherwise.
*/
public function get_items_permissions_check( $request ) {
public function get_items_permissions_check( $request ) {
$post_type = get_post_type_object( $this->post_type );

if ( ! current_user_can( $post_type->cap->read ) ) {
Expand Down Expand Up @@ -136,9 +145,8 @@ public function validate_font_family_settings( $value, $request ) {
*
* @since 6.5.0
*
* @param string $value Encoded JSON string of font family settings.
* @param WP_REST_Request $request Request object.
* @return array Decoded array font family settings.
* @param string $value Encoded JSON string of font family settings.
* @return array Decoded array of font family settings.
*/
public function sanitize_font_family_settings( $value ) {
// Settings arrive as stringified JSON, since this is a multipart/form-data request.
Expand Down Expand Up @@ -175,7 +183,7 @@ public function create_item( $request ) {
'update_post_term_cache' => false,
)
);
if ( ! empty( $query->get_posts() ) ) {
if ( ! empty( $query->posts ) ) {
return new WP_Error(
'rest_duplicate_font_family',
/* translators: %s: Font family slug. */
Expand Down Expand Up @@ -203,7 +211,7 @@ public function delete_item( $request ) {
return new WP_Error(
'rest_trash_not_supported',
/* translators: %s: force=true */
sprintf( __( "Font faces do not support trashing. Set '%s' to delete." ), 'force=true' ),
sprintf( __( 'Font faces do not support trashing. Set "%s" to delete.' ), 'force=true' ),
array( 'status' => 501 )
);
}
Expand All @@ -229,7 +237,7 @@ public function prepare_item_for_response( $item, $request ) {
}

if ( rest_is_field_included( 'theme_json_version', $fields ) ) {
$data['theme_json_version'] = 2;
$data['theme_json_version'] = static::LATEST_THEME_JSON_VERSION_SUPPORTED;
}

if ( rest_is_field_included( 'font_faces', $fields ) ) {
Expand Down Expand Up @@ -290,9 +298,9 @@ public function get_item_schema() {
'theme_json_version' => array(
'description' => __( 'Version of the theme.json schema used for the typography settings.' ),
'type' => 'integer',
'default' => 2,
'default' => static::LATEST_THEME_JSON_VERSION_SUPPORTED,
'minimum' => 2,
'maximum' => 2,
'maximum' => static::LATEST_THEME_JSON_VERSION_SUPPORTED,
'context' => array( 'view', 'edit', 'embed' ),
),
'font_faces' => array(
Expand Down Expand Up @@ -383,13 +391,15 @@ public function get_collection_params() {
$query_params = parent::get_collection_params();

// Remove unneeded params.
unset( $query_params['after'] );
unset( $query_params['modified_after'] );
unset( $query_params['before'] );
unset( $query_params['modified_before'] );
unset( $query_params['search'] );
unset( $query_params['search_columns'] );
unset( $query_params['status'] );
unset(
$query_params['after'],
$query_params['modified_after'],
$query_params['before'],
$query_params['modified_before'],
$query_params['search'],
$query_params['search_columns'],
$query_params['status']
);

$query_params['orderby']['default'] = 'id';
$query_params['orderby']['enum'] = array( 'id', 'include' );
Expand Down Expand Up @@ -453,7 +463,7 @@ protected function get_font_face_ids( $font_family_id ) {
)
);

return $query->get_posts();
return $query->posts;
}

/**
Expand Down Expand Up @@ -487,7 +497,7 @@ protected function prepare_font_face_links( $font_family_id ) {
foreach ( $font_face_ids as $font_face_id ) {
$links[] = array(
'embeddable' => true,
'href' => rest_url( $this->namespace . '/' . $this->rest_base . '/' . $font_family_id . '/font-faces/' . $font_face_id ),
'href' => rest_url( sprintf( '%s/%s/%s/font-faces/%s', $this->namespace, $this->rest_base, $font_family_id, $font_face_id ) ),
);
}
return $links;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ public function test_should_get_font_collection() {

public function test_should_get_no_font_collection_if_the_slug_is_not_registered() {
$font_collection = WP_Font_Library::get_instance()->get_font_collection( 'not-registered-font-collection' );
$this->assertWPError( $font_collection );
$this->assertNull( $font_collection );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ class Tests_REST_WpRestFontCollectionsController extends WP_Test_REST_Controller


public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
// Clear the font collections.
$collections = WP_Font_Library::get_instance()->get_font_collections();
foreach ( $collections as $slug => $collection ) {
WP_Font_Library::get_instance()->unregister_font_collection( $slug );
}

self::$admin_id = $factory->user->create(
array(
'role' => 'administrator',
Expand Down Expand Up @@ -115,7 +121,7 @@ public function test_get_item_invalid_slug() {
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/font-collections/non-existing-collection' );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'font_collection_not_found', $response, 404 );
$this->assertErrorResponse( 'rest_font_collection_not_found', $response, 404 );
}

/**
Expand Down
Loading