Skip to content

Commit 1e8b61b

Browse files
committed
REST API: Indicate when a theme supports the Site editor in the Themes REST API response.
This changeset adds a `is_block_theme` property to each theme in the `wp/v2/themes` API response, which uses `WP_Theme::is_block_theme` to determinate whether the theme is block theme or not. Props grantmkin, ironprogrammer, zunaid321, azaozz, spacedmonkey, audrasjb, costdev. Fixes #58123. git-svn-id: https://develop.svn.wordpress.org/trunk@55951 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 9bc6b0a commit 1e8b61b

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

src/wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,10 @@ public function prepare_item_for_response( $item, $request ) {
326326
}
327327
}
328328

329+
if ( rest_is_field_included( 'is_block_theme', $fields ) ) {
330+
$data['is_block_theme'] = $theme->is_block_theme();
331+
}
332+
329333
$data = $this->add_additional_fields_to_object( $data, $request );
330334

331335
// Wrap the data in a response object.
@@ -494,6 +498,11 @@ public function get_item_schema() {
494498
),
495499
),
496500
),
501+
'is_block_theme' => array(
502+
'description' => __( 'Whether the theme is a block-based theme.' ),
503+
'type' => 'boolean',
504+
'readonly' => true,
505+
),
497506
'name' => array(
498507
'description' => __( 'The name of the theme.' ),
499508
'type' => 'object',

tests/phpunit/tests/rest-api/rest-themes-controller.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ public function test_get_items() {
172172
'author',
173173
'author_uri',
174174
'description',
175+
'is_block_theme',
175176
'name',
176177
'requires_php',
177178
'requires_wp',
@@ -185,6 +186,8 @@ public function test_get_items() {
185186
'theme_uri',
186187
'version',
187188
);
189+
$this->assertIsArray( $data );
190+
$this->assertNotEmpty( $data );
188191
$this->assertSameSets( $fields, array_keys( $data[0] ) );
189192
}
190193

@@ -208,6 +211,7 @@ public function test_get_items_inactive() {
208211
'author',
209212
'author_uri',
210213
'description',
214+
'is_block_theme',
211215
'name',
212216
'requires_php',
213217
'requires_wp',
@@ -220,6 +224,8 @@ public function test_get_items_inactive() {
220224
'theme_uri',
221225
'version',
222226
);
227+
$this->assertIsArray( $data );
228+
$this->assertNotEmpty( $data );
223229
$this->assertSameSets( $fields, array_keys( $data[0] ) );
224230

225231
$this->assertContains( 'twentytwenty', wp_list_pluck( $data, 'stylesheet' ) );
@@ -343,7 +349,7 @@ public function test_get_item_schema() {
343349
$response = self::perform_active_theme_request( 'OPTIONS' );
344350
$data = $response->get_data();
345351
$properties = $data['schema']['properties'];
346-
$this->assertCount( 15, $properties );
352+
$this->assertCount( 16, $properties );
347353

348354
$this->assertArrayHasKey( 'author', $properties );
349355
$this->assertArrayHasKey( 'raw', $properties['author']['properties'] );
@@ -357,6 +363,8 @@ public function test_get_item_schema() {
357363
$this->assertArrayHasKey( 'raw', $properties['description']['properties'] );
358364
$this->assertArrayHasKey( 'rendered', $properties['description']['properties'] );
359365

366+
$this->assertArrayHasKey( 'is_block_theme', $properties );
367+
360368
$this->assertArrayHasKey( 'name', $properties );
361369
$this->assertArrayHasKey( 'raw', $properties['name']['properties'] );
362370
$this->assertArrayHasKey( 'rendered', $properties['name']['properties'] );
@@ -471,6 +479,27 @@ public function test_theme_requires_wp() {
471479
$this->assertSame( '5.3', $result[0]['requires_wp'] );
472480
}
473481

482+
/**
483+
* @ticket 58123
484+
* @covers WP_REST_Themes_Controller::prepare_item_for_response
485+
*/
486+
public function test_theme_is_block_theme() {
487+
// Test classic theme, activated in test setup.
488+
$response = self::perform_active_theme_request();
489+
$result = $response->get_data();
490+
491+
$this->assertArrayHasKey( 'is_block_theme', $result[0] );
492+
$this->assertFalse( $result[0]['is_block_theme'] );
493+
494+
// Test block theme.
495+
switch_theme( 'block-theme' );
496+
$response = self::perform_active_theme_request();
497+
$result = $response->get_data();
498+
499+
$this->assertArrayHasKey( 'is_block_theme', $result[0] );
500+
$this->assertTrue( $result[0]['is_block_theme'] );
501+
}
502+
474503
/**
475504
* @ticket 49906
476505
*/
@@ -1234,6 +1263,7 @@ public function test_get_item() {
12341263
'author',
12351264
'author_uri',
12361265
'description',
1266+
'is_block_theme',
12371267
'name',
12381268
'requires_php',
12391269
'requires_wp',

0 commit comments

Comments
 (0)