diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php index 40955b3c1d..4c495041af 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php @@ -1923,10 +1923,16 @@ class WP_REST_Posts_Controller extends WP_REST_Controller { case 'post-formats': $supports_formats = get_theme_support( 'post-formats' ); + + // Force to an array. Supports formats can return true even if empty in some cases. + $supports_formats = is_array( $supports_formats ) ? array_values( $supports_formats[0] ) : array(); + + $supported_formats = array_merge( array( 'standard' ), $supports_formats ); + $schema['properties']['format'] = array( 'description' => __( 'The format for the object.' ), 'type' => 'string', - 'enum' => array_merge( array( 'standard' ), $supports_formats ? array_values( $supports_formats[0] ) : array() ), + 'enum' => $supported_formats, 'context' => array( 'view', 'edit' ), ); break; diff --git a/tests/phpunit/tests/rest-api/rest-posts-controller.php b/tests/phpunit/tests/rest-api/rest-posts-controller.php index 7389b971b3..04b255d0f7 100644 --- a/tests/phpunit/tests/rest-api/rest-posts-controller.php +++ b/tests/phpunit/tests/rest-api/rest-posts-controller.php @@ -858,6 +858,24 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te $this->assertEquals( $post2, $data[0]['id'] ); } + public function test_get_items_no_supported_post_formats() { + // This causes get_theme_support( 'post-formats' ) to return `true` (not an array) + add_theme_support( 'post-formats' ); + + $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts' ); + $response = $this->server->dispatch( $request ); + $data = $response->get_data(); + + // Set the expected state back for the rest of the tests. + global $_wp_theme_features; + unset( $_wp_theme_features['post-formats'] ); + add_theme_support( 'post-formats', array( 'post', 'gallery' ) ); + + $formats = array( 'standard' ); + + $this->assertEquals( $formats, $data['schema']['properties']['format']['enum'] ); + } + public function test_get_item() { $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $response = $this->server->dispatch( $request );