diff --git a/includes/class-amp-story-media.php b/includes/class-amp-story-media.php index af8312c01c2..a25a0fd7d20 100644 --- a/includes/class-amp-story-media.php +++ b/includes/class-amp-story-media.php @@ -141,7 +141,9 @@ public static function filter_schemaorg_metadata_images( $data ) { return $data; } - if ( is_string( $data['image'] ) ) { + if ( empty( $data['image'] ) ) { + $data['image'] = []; + } elseif ( is_string( $data['image'] ) ) { $data['image'] = [ $data['image'] ]; } elseif ( is_array( $data['image'] ) && isset( $data['image']['@type'] ) ) { $data['image'] = [ $data['image'] ]; diff --git a/tests/php/test-class-amp-story-media.php b/tests/php/test-class-amp-story-media.php index 4af3ecf9e3a..8c719424db1 100644 --- a/tests/php/test-class-amp-story-media.php +++ b/tests/php/test-class-amp-story-media.php @@ -32,7 +32,7 @@ public function setUp() { $wp_styles = null; AMP_Options_Manager::update_option( 'experiences', [ AMP_Options_Manager::STORIES_EXPERIENCE ] ); - AMP_Story_Media::init(); + AMP_Story_Post_Type::register(); // This calls AMP_Story_Media::init(). } /** @@ -52,6 +52,61 @@ public function tearDown() { parent::tearDown(); } + /** + * Test filter_schemaorg_metadata_images() + * + * @covers AMP_Story_Media::filter_schemaorg_metadata_images + */ + public function test_filter_schemaorg_metadata_images() { + $this->assertSame( [], AMP_Story_Media::filter_schemaorg_metadata_images( [] ) ); + + $post_id = $this->factory()->post->create( [ 'post_type' => AMP_Story_Post_Type::POST_TYPE_SLUG ] ); + $this->go_to( get_permalink( $post_id ) ); + $this->assertTrue( is_singular( AMP_Story_Post_Type::POST_TYPE_SLUG ) ); + + $custom_image_url = 'https://example.com/foo.jpg'; + $custom_image_obj = [ + 'url' => $custom_image_url, + '@type' => 'ImageObject', + ]; + $fallback_image_url = amp_get_asset_url( 'images/stories-editor/story-fallback-poster.jpg' ); + + $this->assertSame( [ 'image' => [ $fallback_image_url ] ], AMP_Story_Media::filter_schemaorg_metadata_images( [] ) ); + $this->assertSame( [ 'image' => [ $fallback_image_url ] ], AMP_Story_Media::filter_schemaorg_metadata_images( [ 'image' => null ] ) ); + $this->assertSame( [ 'image' => [ $fallback_image_url ] ], AMP_Story_Media::filter_schemaorg_metadata_images( [ 'image' => false ] ) ); + $this->assertSame( [ 'image' => [ $fallback_image_url ] ], AMP_Story_Media::filter_schemaorg_metadata_images( [ 'image' => '' ] ) ); + + $this->assertSame( [ 'image' => [ $fallback_image_url, $custom_image_url ] ], AMP_Story_Media::filter_schemaorg_metadata_images( [ 'image' => $custom_image_url ] ) ); + $this->assertSame( + [ + 'image' => [ + $fallback_image_url, + $custom_image_obj, + ], + ], + AMP_Story_Media::filter_schemaorg_metadata_images( + [ + 'image' => $custom_image_obj, + ] + ) + ); + $this->assertSame( [ 'image' => [ $fallback_image_url, $custom_image_url ] ], AMP_Story_Media::filter_schemaorg_metadata_images( [ 'image' => $custom_image_url ] ) ); + $this->assertSame( + [ + 'image' => [ + $fallback_image_url, + $custom_image_obj, + $custom_image_obj, + ], + ], + AMP_Story_Media::filter_schemaorg_metadata_images( + [ + 'image' => [ $custom_image_obj, $custom_image_obj ], + ] + ) + ); + } + /** * Test rest_api_init() *