Skip to content

Commit

Permalink
Merge pull request #1176 from Automattic/attachment-schema
Browse files Browse the repository at this point in the history
Display schema.org image data for 'attachment' post type
  • Loading branch information
westonruter authored May 27, 2018
2 parents cfee839 + 15ea3d9 commit ab199da
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
2 changes: 2 additions & 0 deletions includes/amp-helper-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,8 @@ function amp_get_post_image_metadata( $post = null ) {

if ( has_post_thumbnail( $post->ID ) ) {
$post_image_id = get_post_thumbnail_id( $post->ID );
} elseif ( ( 'attachment' === $post->post_type ) && wp_attachment_is( 'image', $post ) ) {
$post_image_id = $post->ID;
} else {
$attached_image_ids = get_posts(
array(
Expand Down
45 changes: 45 additions & 0 deletions tests/test-amp-helper-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,51 @@ public function test_amp_get_post_image_metadata() {
) );
$metadata = amp_get_post_image_metadata( $post_id );
$this->assertStringEndsWith( 'test-image.jpg', $metadata['url'] );

// Test an 'attachment' post type.
$attachment_src = 'example/attachment.jpeg';
$attachment_height = 45;
$attachment_width = 600;
$attachment_id = $this->factory()->attachment->create_object(
$attachment_src,
0,
array(
'post_mime_type' => 'image/jpeg',
)
);
$expected_attachment_img = wp_get_attachment_image_src( $attachment_id, 'full', false );

update_post_meta(
$attachment_id,
'_wp_attachment_metadata',
array(
'height' => $attachment_height,
'width' => $attachment_width,
)
);
$this->go_to( get_permalink( $attachment_id ) );

$this->assertEquals(
array(
'@type' => 'ImageObject',
'height' => $attachment_height,
'url' => $expected_attachment_img[0],
'width' => $attachment_width,
),
amp_get_post_image_metadata( $attachment_id )
);

// Test a video as an 'attachment' post type, which shouldn't have a schema.org image.
$attachment_src = 'example/test-video.mpeg';
$attachment_id = $this->factory()->attachment->create_object(
$attachment_src,
0,
array(
'post_mime_type' => 'video/mpeg',
)
);
$this->go_to( get_permalink( $attachment_id ) );
$this->assertFalse( amp_get_post_image_metadata( $attachment_id ) );
}

/**
Expand Down

0 comments on commit ab199da

Please sign in to comment.