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

Index a full srcset for the post thumbnail #3693

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
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
1 change: 1 addition & 0 deletions assets/css/instant-results/result.css
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
display: block;
height: auto;
margin: 0;
object-fit: cover;
width: 100%;
}
}
Expand Down
4 changes: 2 additions & 2 deletions assets/js/instant-results/components/common/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ import { WPElement } from '@wordpress/element';
*
* @returns {WPElement} Component element.
*/
export default ({ alt, height, ID, src, width, ...props }) => {
return <img alt={alt} src={src} width={width} height={height} {...props} />;
export default ({ alt, height, ID, src, width, srcset, ...props }) => {
return <img alt={alt} src={src} width={width} height={height} srcSet={srcset} {...props} />;
};
24 changes: 15 additions & 9 deletions includes/classes/Indexable/Post/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -566,9 +566,6 @@ public function prepare_thumbnail( $post ) {
/**
* Filters the image size to use when indexing the post thumbnail.
*
* Defaults to the `woocommerce_thumbnail` size if WooCommerce is in
* use. Otherwise the `thumbnail` size is used.
*
* @hook ep_thumbnail_image_size
* @since 4.0.0
* @param {string|int[]} $image_size Image size. Can be any registered
Expand All @@ -580,23 +577,32 @@ public function prepare_thumbnail( $post ) {
*/
$image_size = apply_filters(
'ep_post_thumbnail_image_size',
function_exists( 'WC' ) ? 'woocommerce_thumbnail' : 'thumbnail',
'medium',
$post
);

$image_src = wp_get_attachment_image_src( $attachment_id, $image_size );
$image = wp_get_attachment_image_src( $attachment_id, $image_size );
$image_alt = trim( wp_strip_all_tags( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ) );

if ( ! $image_src ) {
if ( ! $image ) {
return null;
}

list( $src, $width, $height ) = $image;

$image_meta = wp_get_attachment_metadata( $attachment_id );

$size_array = array( absint( $width ), absint( $height ) );
$srcset = wp_calculate_image_srcset( $size_array, $src, $image_meta, $attachment_id );
$sizes = wp_calculate_image_sizes( $size_array, $src, $image_meta, $attachment_id );

return [
'ID' => $attachment_id,
'src' => $image_src[0],
'width' => $image_src[1],
'height' => $image_src[2],
'src' => $src,
'width' => $width,
'height' => $height,
'alt' => $image_alt,
'srcset' => $srcset,
];
}

Expand Down
35 changes: 35 additions & 0 deletions tests/php/indexables/TestPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -8756,6 +8756,41 @@ function( $post_types ) {
$this->assertArrayHasKey( '_thumbnail_id', $ep_post['meta'] );
}

/**
* Test if post thumbnail has all attributes.
*
* @since 5.0.0
*/
public function testPostThumbnailHasAllAttributes() {
$post_id = $this->ep_factory->post->create();
$thumbnail_id = $this->factory->attachment->create_object(
'test.jpg',
$post_id,
[
'post_mime_type' => 'image/jpeg',
'post_type' => 'attachment',
]
);

set_post_thumbnail( $post_id, $thumbnail_id );

$thumbnail_id = get_post_thumbnail_id( $post_id );
$this->assertEquals( $thumbnail_id, get_post_meta( $post_id, '_thumbnail_id', true ) );

ElasticPress\Indexables::factory()->get( 'post' )->index( $post_id, true );
ElasticPress\Elasticsearch::factory()->refresh_indices();

$ep_post = ElasticPress\Indexables::factory()->get( 'post' )->get( $post_id );
$thumbnail = $ep_post['thumbnail'];

$this->assertEquals( 6, count( $thumbnail ) );

$keys = [ 'ID', 'src', 'width', 'height', 'alt', 'srcset' ];
foreach ( $keys as $key ) {
$this->assertArrayHasKey( $key, $thumbnail );
}
}

/**
* Test that query with unsupported orderby does not use EP.
*
Expand Down