Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Output post classes in the editor #6716

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 16 commits
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
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(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add a readonly keyword.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Fixed in e4c1641
Thanks.

'description' => __( 'An array of the class names for the post container element.' ),
'type' => 'array',
'context' => array( 'view', 'edit' ),
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about the context.
It seems that 'view' and 'edit' would be appropriate for this field, but I could use some help here. I'd be happy to update the PR if these contexts are not correct for the given field.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah view and edit looks correc to me. Basically everything should have edit, and view should be used if it's a non-privileged field.

The embed context is a bit of a bar to meet. It should be used when developers would often need this information when including the resource as an embed. That doesn't seem the case to me here, since the class list would most often be used when looping over the list of posts as a main query.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the information. It's indeed very helpful in understanding when to use the embed context.

'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
23 changes: 14 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,22 @@ 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 ) ) {
$data[ $key ] = 'post-1073';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this? Why is this hardcoded? Is it for test stability? Might be good to comment.

Copy link
Author

@anton-vlasenko anton-vlasenko Jun 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's to ensure test stability. Fixed in d24ee39.
P.S. I'll quckly check if hardcoding the value can be avoided. For now, let's just add the comment.

Copy link
Author

@anton-vlasenko anton-vlasenko Jun 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've discovered an improved method for adding these fixture replacements. While the current implementation works, the approach detailed here offers a better solution: WordPress Core Ticket #61369.

This isn't urgent since the current setup functions adequately for mocking the REST API. However, it would be great if someone could review the new approach when they have time.

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
Loading