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

Feature: Expose post_types field to Templates REST API #7530

Open
wants to merge 5 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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 @@ -783,6 +783,10 @@ public function prepare_item_for_response( $item, $request ) {
}
}

if ( rest_is_field_included( 'post_types', $fields ) ) {
$data['post_types'] = is_array( $template->post_types ) ? $template->post_types : array();
}

$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 @@ -1163,6 +1167,12 @@ public function get_item_schema() {
'user',
),
),
'post_types' => array(
'description' => __( 'Post types associated with the template.' ),
'type' => 'array',
'readonly' => true,
'context' => array( 'embed', 'view', 'edit' ),
),
),
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ public function test_get_item_schema() {

$properties = $data['schema']['properties'];

$this->assertCount( 19, $properties );
$this->assertCount( 20, $properties );
$this->assertArrayHasKey( 'id', $properties, 'ID key should exist in properties.' );
$this->assertArrayHasKey( 'slug', $properties, 'Slug key should exist in properties.' );
$this->assertArrayHasKey( 'theme', $properties, 'Theme key should exist in properties.' );
Expand All @@ -329,6 +329,7 @@ public function test_get_item_schema() {
$this->assertArrayHasKey( 'author_text', $properties, 'author_text key should exist in properties.' );
$this->assertArrayHasKey( 'original_source', $properties, 'original_source key should exist in properties.' );
$this->assertArrayHasKey( 'plugin', $properties, 'plugin key should exist in properties.' );
$this->assertArrayHasKey( 'post_types', $properties, 'post_types key should exist in properties.' );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ public function test_get_item_schema() {
$data = $response->get_data();
$properties = $data['schema']['properties'];

$this->assertCount( 19, $properties );
$this->assertCount( 20, $properties );
$this->assertArrayHasKey( 'id', $properties, 'ID key should exist in properties.' );
$this->assertArrayHasKey( 'slug', $properties, 'Slug key should exist in properties.' );
$this->assertArrayHasKey( 'theme', $properties, 'Theme key should exist in properties.' );
Expand All @@ -468,6 +468,7 @@ public function test_get_item_schema() {
$this->assertArrayHasKey( 'author_text', $properties, 'author_text key should exist in properties.' );
$this->assertArrayHasKey( 'original_source', $properties, 'original_source key should exist in properties.' );
$this->assertArrayHasKey( 'plugin', $properties, 'plugin key should exist in properties.' );
$this->assertArrayHasKey( 'post_types', $properties, 'post_types key should exist in properties.' );
}

/**
Expand Down
13 changes: 12 additions & 1 deletion tests/phpunit/tests/rest-api/wpRestTemplatesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ public function test_get_items() {
'modified' => mysql_to_rfc3339( self::$template_post->post_modified ),
'author_text' => 'Test Blog',
'original_source' => 'site',
'post_types' => array(),
),
$this->find_and_normalize_template_by_id( $data, 'default//my_template' )
);
Expand Down Expand Up @@ -205,6 +206,7 @@ public function test_get_items_editor() {
'modified' => mysql_to_rfc3339( self::$template_post->post_modified ),
'author_text' => 'Test Blog',
'original_source' => 'site',
'post_types' => array(),
),
$this->find_and_normalize_template_by_id( $data, 'default//my_template' )
);
Expand Down Expand Up @@ -262,6 +264,7 @@ public function test_get_item() {
'modified' => mysql_to_rfc3339( self::$template_post->post_modified ),
'author_text' => 'Test Blog',
'original_source' => 'site',
'post_types' => array(),
),
$data
);
Expand Down Expand Up @@ -299,6 +302,7 @@ public function test_get_item_editor() {
'modified' => mysql_to_rfc3339( self::$template_post->post_modified ),
'author_text' => 'Test Blog',
'original_source' => 'site',
'post_types' => array(),
),
$data
);
Expand Down Expand Up @@ -349,6 +353,7 @@ public function test_get_item_works_with_a_single_slash( $endpoint_url ) {
'modified' => mysql_to_rfc3339( self::$template_post->post_modified ),
'author_text' => 'Test Blog',
'original_source' => 'site',
'post_types' => array(),
),
$data
);
Expand Down Expand Up @@ -412,6 +417,7 @@ public function test_get_item_with_valid_theme_dirname( $theme_dir, $template, a
'modified' => mysql_to_rfc3339( $post->post_modified ),
'author_text' => $author_name,
'original_source' => 'user',
'post_types' => array(),
),
$data
);
Expand Down Expand Up @@ -644,6 +650,7 @@ public function test_create_item() {
'modified' => mysql_to_rfc3339( $modified ),
'author_text' => $author_name,
'original_source' => 'user',
'post_types' => array(),
),
$data
);
Expand Down Expand Up @@ -696,6 +703,7 @@ public function test_create_item_with_numeric_slug() {
'modified' => mysql_to_rfc3339( $modified ),
'author_text' => $author_name,
'original_source' => 'user',
'post_types' => array(),
),
$data
);
Expand Down Expand Up @@ -752,6 +760,7 @@ public function test_create_item_raw() {
'modified' => mysql_to_rfc3339( $modified ),
'author_text' => $author_name,
'original_source' => 'user',
'post_types' => array(),
),
$data
);
Expand Down Expand Up @@ -910,7 +919,7 @@ public function test_get_item_schema() {
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$properties = $data['schema']['properties'];
$this->assertCount( 18, $properties );
$this->assertCount( 19, $properties );
$this->assertArrayHasKey( 'id', $properties );
$this->assertArrayHasKey( 'description', $properties );
$this->assertArrayHasKey( 'slug', $properties );
Expand All @@ -930,6 +939,7 @@ public function test_get_item_schema() {
$this->assertArrayHasKey( 'author_text', $properties );
$this->assertArrayHasKey( 'original_source', $properties );
$this->assertArrayHasKey( 'plugin', $properties );
$this->assertArrayHasKey( 'post_types', $properties );
}

protected function find_and_normalize_template_by_id( $templates, $id ) {
Expand Down Expand Up @@ -966,6 +976,7 @@ public function test_create_item_with_is_wp_suggestion( array $body_params, arra
$expected['modified'] = mysql_to_rfc3339( $modified );
$expected['author_text'] = get_user_by( 'id', self::$admin_id )->get( 'display_name' );
$expected['original_source'] = 'user';
$expected['post_types'] = array();

unset( $data['_links'] );
unset( $data['wp_id'] );
Expand Down
Loading