Skip to content

Commit

Permalink
REST API: Add post class list field.
Browse files Browse the repository at this point in the history
See WordPress/gutenberg#60642.
See #6716.

Fixes #61360.

Props antonvlasenko, timothyblynjacobs, ellatrix, oandregal.



git-svn-id: https://develop.svn.wordpress.org/trunk@58326 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
ellatrix committed Jun 4, 2024
1 parent 98281ee commit 278ec2f
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1998,6 +1998,10 @@ public function prepare_item_for_response( $item, $request ) {
$data['generated_slug'] = $sample_permalink[1];
}
}

if ( rest_is_field_included( 'class_list', $fields ) ) {
$data['class_list'] = get_post_class( array(), $post->ID );
}
}

$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
Expand Down Expand Up @@ -2353,6 +2357,16 @@ public function get_item_schema() {
'context' => array( 'edit' ),
'readonly' => true,
);

$schema['properties']['class_list'] = array(
'description' => __( 'An array of the class names for the post container element.' ),
'type' => 'array',
'context' => array( 'view', 'edit' ),
'readonly' => true,
'items' => array(
'type' => 'string',
),
);
}

if ( $post_type_obj->hierarchical ) {
Expand Down
3 changes: 2 additions & 1 deletion tests/phpunit/tests/rest-api/rest-attachments-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -1646,7 +1646,7 @@ public function test_get_item_schema() {
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$properties = $data['schema']['properties'];
$this->assertCount( 28, $properties );
$this->assertCount( 29, $properties );
$this->assertArrayHasKey( 'author', $properties );
$this->assertArrayHasKey( 'alt_text', $properties );
$this->assertArrayHasKey( 'caption', $properties );
Expand Down Expand Up @@ -1681,6 +1681,7 @@ public function test_get_item_schema() {
$this->assertArrayHasKey( 'type', $properties );
$this->assertArrayHasKey( 'missing_image_sizes', $properties );
$this->assertArrayHasKey( 'featured_media', $properties );
$this->assertArrayHasKey( 'class_list', $properties );
}

public function test_get_additional_field_registration() {
Expand Down
3 changes: 2 additions & 1 deletion tests/phpunit/tests/rest-api/rest-pages-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ public function test_get_item_schema() {
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$properties = $data['schema']['properties'];
$this->assertCount( 24, $properties );
$this->assertCount( 25, $properties );
$this->assertArrayHasKey( 'author', $properties );
$this->assertArrayHasKey( 'comment_status', $properties );
$this->assertArrayHasKey( 'content', $properties );
Expand All @@ -773,6 +773,7 @@ public function test_get_item_schema() {
$this->assertArrayHasKey( 'template', $properties );
$this->assertArrayHasKey( 'title', $properties );
$this->assertArrayHasKey( 'type', $properties );
$this->assertArrayHasKey( 'class_list', $properties );
}

public function filter_theme_page_templates( $page_templates ) {
Expand Down
5 changes: 4 additions & 1 deletion tests/phpunit/tests/rest-api/rest-posts-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -4362,7 +4362,7 @@ public function test_get_item_schema() {
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$properties = $data['schema']['properties'];
$this->assertCount( 26, $properties );
$this->assertCount( 27, $properties );
$this->assertArrayHasKey( 'author', $properties );
$this->assertArrayHasKey( 'comment_status', $properties );
$this->assertArrayHasKey( 'content', $properties );
Expand All @@ -4389,6 +4389,7 @@ public function test_get_item_schema() {
$this->assertArrayHasKey( 'type', $properties );
$this->assertArrayHasKey( 'tags', $properties );
$this->assertArrayHasKey( 'categories', $properties );
$this->assertArrayHasKey( 'class_list', $properties );
}

/**
Expand Down Expand Up @@ -4418,6 +4419,7 @@ public function test_get_post_view_context_properties() {
$expected_keys = array(
'author',
'categories',
'class_list',
'comment_status',
'content',
'date',
Expand Down Expand Up @@ -4456,6 +4458,7 @@ public function test_get_post_edit_context_properties() {
$expected_keys = array(
'author',
'categories',
'class_list',
'comment_status',
'content',
'date',
Expand Down
24 changes: 15 additions & 9 deletions tests/phpunit/tests/rest-api/rest-schema-setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -759,17 +759,23 @@ private function normalize_fixture( $data, $path ) {
return $data;
}

$datetime_keys = array( 'date', 'date_gmt', 'modified', 'modified_gmt' );

foreach ( $data as $key => $value ) {
if ( is_string( $value ) && (
'date' === $key ||
'date_gmt' === $key ||
'modified' === $key ||
'modified_gmt' === $key
) ) {
$data[ $key ] = '2017-02-14T00:00:00';
} else {
$data[ $key ] = $this->normalize_fixture( $value, "$path.$key" );
if ( is_string( $value ) ) {
if ( in_array( $key, $datetime_keys, true ) ) {
$data[ $key ] = '2017-02-14T00:00:00';
continue;
}

if ( 1 === preg_match( '/^post-\d+$/', $value ) ) {
// Normalize the class value to ensure test stability.
$data[ $key ] = 'post-1073';
continue;
}
}

$data[ $key ] = $this->normalize_fixture( $value, "$path.$key" );
}

return $data;
Expand Down
50 changes: 48 additions & 2 deletions tests/qunit/fixtures/wp-api-generated.js
Original file line number Diff line number Diff line change
Expand Up @@ -12322,6 +12322,15 @@ mockedApiResponse.PostsCollection = [
1
],
"tags": [],
"class_list": [
"post-1073",
"post",
"type-post",
"status-publish",
"format-standard",
"hentry",
"category-uncategorized"
],
"_links": {
"self": [
{
Expand Down Expand Up @@ -12421,7 +12430,16 @@ mockedApiResponse.PostModel = {
"categories": [
1
],
"tags": []
"tags": [],
"class_list": [
"post-1073",
"post",
"type-post",
"status-publish",
"format-standard",
"hentry",
"category-uncategorized"
]
};

mockedApiResponse.postRevisions = [
Expand Down Expand Up @@ -12613,6 +12631,13 @@ mockedApiResponse.PagesCollection = [
"meta": {
"meta_key": ""
},
"class_list": [
"post-1073",
"page",
"type-page",
"status-publish",
"hentry"
],
"_links": {
"self": [
{
Expand Down Expand Up @@ -12696,7 +12721,14 @@ mockedApiResponse.PageModel = {
"template": "",
"meta": {
"meta_key": ""
}
},
"class_list": [
"post-1073",
"page",
"type-page",
"status-publish",
"hentry"
]
};

mockedApiResponse.pageRevisions = [
Expand Down Expand Up @@ -12878,6 +12910,13 @@ mockedApiResponse.MediaCollection = [
"meta": {
"meta_key": ""
},
"class_list": [
"post-1073",
"attachment",
"type-attachment",
"status-inherit",
"hentry"
],
"description": {
"rendered": "<p class=\"attachment\"><!-- <a...><img.../></a> --></p>"
},
Expand Down Expand Up @@ -12940,6 +12979,13 @@ mockedApiResponse.MediaModel = {
"meta": {
"meta_key": ""
},
"class_list": [
"post-1073",
"attachment",
"type-attachment",
"status-inherit",
"hentry"
],
"description": {
"rendered": "<p class=\"attachment\"><!-- <a...><img.../></a> --></p>"
},
Expand Down

0 comments on commit 278ec2f

Please sign in to comment.