diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php index a1203f236423f..8e7e6ae8e0c5d 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php @@ -332,6 +332,10 @@ public function prepare_item_for_response( $item, $request ) { $data['is_block_theme'] = $theme->is_block_theme(); } + if ( rest_is_field_included( 'has_theme_json', $fields ) ) { + $data['has_theme_json'] = wp_theme_has_theme_json(); + } + if ( rest_is_field_included( 'stylesheet_uri', $fields ) ) { if ( $this->is_same_theme( $theme, $current_theme ) ) { $data['stylesheet_uri'] = get_stylesheet_directory_uri(); @@ -546,6 +550,11 @@ public function get_item_schema() { 'type' => 'boolean', 'readonly' => true, ), + 'has_theme_json' => array( + 'description' => __( 'Whether the theme has a theme.json file.' ), + 'type' => 'boolean', + 'readonly' => true, + ), 'name' => array( 'description' => __( 'The name of the theme.' ), 'type' => 'object', diff --git a/tests/phpunit/tests/rest-api/rest-themes-controller.php b/tests/phpunit/tests/rest-api/rest-themes-controller.php index 923e7cda27374..fa5dc4e462e6d 100644 --- a/tests/phpunit/tests/rest-api/rest-themes-controller.php +++ b/tests/phpunit/tests/rest-api/rest-themes-controller.php @@ -180,6 +180,7 @@ public function test_get_items() { 'default_template_types', 'description', 'is_block_theme', + 'has_theme_json', 'name', 'requires_php', 'requires_wp', @@ -222,6 +223,7 @@ public function test_get_items_inactive() { 'author_uri', 'description', 'is_block_theme', + 'has_theme_json', 'name', 'requires_php', 'requires_wp', @@ -363,7 +365,7 @@ public function test_get_item_schema() { $response = self::perform_active_theme_request( 'OPTIONS' ); $data = $response->get_data(); $properties = $data['schema']['properties']; - $this->assertCount( 20, $properties ); + $this->assertCount( 21, $properties ); $this->assertArrayHasKey( 'author', $properties ); $this->assertArrayHasKey( 'raw', $properties['author']['properties'] ); @@ -381,6 +383,7 @@ public function test_get_item_schema() { $this->assertArrayHasKey( 'default_template_types', $properties ); $this->assertArrayHasKey( 'is_block_theme', $properties ); + $this->assertArrayHasKey( 'has_theme_json', $properties ); $this->assertArrayHasKey( 'name', $properties ); $this->assertArrayHasKey( 'raw', $properties['name']['properties'] ); @@ -546,6 +549,27 @@ public function test_theme_is_block_theme() { $this->assertTrue( $result[0]['is_block_theme'] ); } + /** + * @ticket 63253 + * @covers WP_REST_Themes_Controller::prepare_item_for_response + */ + public function test_theme_has_theme_json() { + // Test classic theme (Theme without theme.json). + $response = self::perform_active_theme_request(); + $result = $response->get_data(); + + $this->assertArrayHasKey( 'has_theme_json', $result[0] ); + $this->assertFalse( $result[0]['has_theme_json'] ); + + // Test block theme (Theme with theme.json). + switch_theme( 'block-theme' ); + $response = self::perform_active_theme_request(); + $result = $response->get_data(); + + $this->assertArrayHasKey( 'has_theme_json', $result[0] ); + $this->assertTrue( $result[0]['has_theme_json'] ); + } + /** * @ticket 49906 */ @@ -1372,6 +1396,7 @@ public function test_get_item() { 'author_uri', 'description', 'is_block_theme', + 'has_theme_json', 'name', 'requires_php', 'requires_wp',