Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #1157: Display schema.org image data for 'attachment' post type #1176

Merged
merged 3 commits into from
May 27, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
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 ) {
Copy link
Member

Choose a reason for hiding this comment

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

What if the attachment isn't an image? I think this should also include wp_attachment_is( 'image', $post )

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, commit 15ea3d ensures the attachment is an image.

$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->assertNull( amp_get_post_image_metadata( $attachment_id ) );
}

/**
Expand Down