Skip to content

Commit

Permalink
Updates Font Families and Font Faces endpoints context param (#58287)
Browse files Browse the repository at this point in the history
  • Loading branch information
creativecoder authored and youknowriad committed Jan 31, 2024
1 parent 43878c7 commit c1270a9
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 38 deletions.
Expand Up @@ -79,7 +79,7 @@ public function register_routes() {
'callback' => array( $this, 'get_item' ),
'permission_callback' => array( $this, 'get_item_permissions_check' ),
'args' => array(
'context' => $this->get_context_param( array( 'default' => 'edit' ) ),
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
),
),
array(
Expand Down Expand Up @@ -427,7 +427,7 @@ public function prepare_item_for_response( $item, $request ) {
$data['font_face_settings'] = $this->get_settings_from_post( $item );
}

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

Expand Down Expand Up @@ -471,7 +471,7 @@ public function get_item_schema() {
'id' => array(
'description' => __( 'Unique identifier for the post.', 'default' ),
'type' => 'integer',
'context' => array( 'edit', 'embed' ),
'context' => array( 'view', 'edit', 'embed' ),
'readonly' => true,
),
'theme_json_version' => array(
Expand All @@ -480,19 +480,19 @@ public function get_item_schema() {
'default' => 2,
'minimum' => 2,
'maximum' => 2,
'context' => array( 'edit', 'embed' ),
'context' => array( 'view', 'edit', 'embed' ),
),
'parent' => array(
'description' => __( 'The ID for the parent font family of the font face.', 'gutenberg' ),
'type' => 'integer',
'context' => array( 'edit', 'embed' ),
'context' => array( 'view', 'edit', 'embed' ),
),
// Font face settings come directly from theme.json schema
// See https://schemas.wp.org/trunk/theme.json
'font_face_settings' => array(
'description' => __( 'font-face declaration in theme.json format.', 'gutenberg' ),
'type' => 'object',
'context' => array( 'edit', 'embed' ),
'context' => array( 'view', 'edit', 'embed' ),
'properties' => array(
'fontFamily' => array(
'description' => __( 'CSS font-family value.', 'gutenberg' ),
Expand Down Expand Up @@ -601,8 +601,6 @@ public function get_item_schema() {
public function get_collection_params() {
$query_params = parent::get_collection_params();

$query_params['context']['default'] = 'edit';

// Remove unneeded params.
unset( $query_params['after'] );
unset( $query_params['modified_after'] );
Expand Down
Expand Up @@ -230,7 +230,7 @@ public function prepare_item_for_response( $item, $request ) {
$data['font_family_settings'] = $this->get_settings_from_post( $item );
}

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

Expand Down Expand Up @@ -274,7 +274,7 @@ public function get_item_schema() {
'id' => array(
'description' => __( 'Unique identifier for the post.', 'default' ),
'type' => 'integer',
'context' => array( 'edit' ),
'context' => array( 'view', 'edit', 'embed' ),
'readonly' => true,
),
'theme_json_version' => array(
Expand All @@ -283,12 +283,12 @@ public function get_item_schema() {
'default' => 2,
'minimum' => 2,
'maximum' => 2,
'context' => array( 'edit' ),
'context' => array( 'view', 'edit', 'embed' ),
),
'font_faces' => array(
'description' => __( 'The IDs of the child font faces in the font family.', 'gutenberg' ),
'type' => 'array',
'context' => array( 'edit' ),
'context' => array( 'view', 'edit', 'embed' ),
'items' => array(
'type' => 'integer',
),
Expand All @@ -298,7 +298,7 @@ public function get_item_schema() {
'font_family_settings' => array(
'description' => __( 'font-face declaration in theme.json format.', 'gutenberg' ),
'type' => 'object',
'context' => array( 'edit' ),
'context' => array( 'view', 'edit', 'embed' ),
'properties' => array(
'name' => array(
'description' => 'Name of the font family preset, translatable.',
Expand Down Expand Up @@ -338,8 +338,6 @@ public function get_item_schema() {
public function get_collection_params() {
$query_params = parent::get_collection_params();

$query_params['context']['default'] = 'edit';

// Remove unneeded params.
unset( $query_params['after'] );
unset( $query_params['modified_after'] );
Expand All @@ -362,21 +360,6 @@ public function get_collection_params() {
return apply_filters( 'rest_wp_font_family_collection_params', $query_params );
}

/**
* Retrieves the query params for the font family collection, defaulting to the 'edit' context.
*
* @since 6.5.0
*
* @param array $args Optional. Additional arguments for context parameter. Default empty array.
* @return array Context parameter details.
*/
public function get_context_param( $args = array() ) {
if ( isset( $args['default'] ) ) {
$args['default'] = 'edit';
}
return parent::get_context_param( $args );
}

/**
* Get the arguments used when creating or updating a font family.
*
Expand Down
Expand Up @@ -136,16 +136,16 @@ public function test_context_param() {
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertArrayNotHasKey( 'allow_batch', $data['endpoints'][0] );
$this->assertSame( 'edit', $data['endpoints'][0]['args']['context']['default'] );
$this->assertSame( array( 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
$this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] );
$this->assertSame( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );

// Single.
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/font-families/' . self::$font_family_id . '/font-faces/' . self::$font_face_id1 );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertArrayNotHasKey( 'allow_batch', $data['endpoints'][0] );
$this->assertSame( 'edit', $data['endpoints'][0]['args']['context']['default'] );
$this->assertSame( array( 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
$this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] );
$this->assertSame( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
}

/**
Expand Down
Expand Up @@ -151,16 +151,16 @@ public function test_context_param() {
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertArrayNotHasKey( 'allow_batch', $data['endpoints'][0] );
$this->assertSame( 'edit', $data['endpoints'][0]['args']['context']['default'] );
$this->assertSame( array( 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
$this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] );
$this->assertSame( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );

// Single.
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/font-families/' . self::$font_family_id1 );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertArrayNotHasKey( 'allow_batch', $data['endpoints'][0] );
$this->assertSame( 'edit', $data['endpoints'][0]['args']['context']['default'] );
$this->assertSame( array( 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
$this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] );
$this->assertSame( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
}


Expand Down

0 comments on commit c1270a9

Please sign in to comment.