Skip to content
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 @@ -197,18 +197,22 @@ public function get_item( $request ) {
public function get_items( $request ) {
$themes = array();

$active_themes = wp_get_themes();
$current_theme = wp_get_theme();
$status = $request['status'];

foreach ( $active_themes as $theme ) {
$theme_status = ( $this->is_same_theme( $theme, $current_theme ) ) ? 'active' : 'inactive';
if ( is_array( $status ) && ! in_array( $theme_status, $status, true ) ) {
continue;
}

$prepared = $this->prepare_item_for_response( $theme, $request );
if ( array( 'active' ) === $status ) {
$prepared = $this->prepare_item_for_response( $current_theme, $request );
$themes[] = $this->prepare_response_for_collection( $prepared );
} else {
foreach ( wp_get_themes() as $theme ) {
$theme_status = ( $this->is_same_theme( $theme, $current_theme ) ) ? 'active' : 'inactive';
if ( is_array( $status ) && ! in_array( $theme_status, $status, true ) ) {
continue;
}
Comment on lines +208 to +211
Copy link
Member Author

Choose a reason for hiding this comment

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

There might be some refactoring we could do here to avoid handling the active case, now that it's handled above.

Copy link
Member

Choose a reason for hiding this comment

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

It's not completely though, right? The $status can have both active and inactive:

'status' => array(
'description' => __( 'Limit result set to themes assigned one or more statuses.' ),
'type' => 'array',
'items' => array(
'enum' => array( 'active', 'inactive' ),
'type' => 'string',
),
),

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I wrote the comment at the time that I thought $request['status'] was a string value and not an array (prior to a695820), so it's less clear now that there's much refactor opportunity here.


$prepared = $this->prepare_item_for_response( $theme, $request );
$themes[] = $this->prepare_response_for_collection( $prepared );
}
}

$response = rest_ensure_response( $themes );
Expand Down
45 changes: 45 additions & 0 deletions tests/phpunit/tests/rest-api/rest-themes-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,51 @@ public function test_get_items() {
$this->assertSameSets( $fields, array_keys( $data[0] ) );
}

/**
* Test retrieving a collection of active themes.
*
* @ticket 64719
*/
public function test_get_items_active() {
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'GET', self::$themes_route );
$request->set_param( 'status', 'active' );

$response = rest_get_server()->dispatch( $request );

$this->assertSame( 200, $response->get_status() );
$data = $response->get_data();

$fields = array(
'_links',
'author',
'author_uri',
'description',
'is_block_theme',
'name',
'requires_php',
'requires_wp',
'screenshot',
'status',
'stylesheet',
'stylesheet_uri',
'tags',
'template',
'template_uri',
'textdomain',
'theme_uri',
'version',
'default_template_part_areas',
'default_template_types',
'theme_supports',
);
$this->assertIsArray( $data );
$this->assertCount( 1, $data );
$this->assertSameSets( $fields, array_keys( $data[0] ) );

$this->assertEquals( array( 'rest-api' ), wp_list_pluck( $data, 'stylesheet' ) );
}

/**
* Test retrieving a collection of inactive themes.
*
Expand Down
Loading