-
Notifications
You must be signed in to change notification settings - Fork 385
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
Improve hero image determination #6062
Conversation
Codecov Report
@@ Coverage Diff @@
## develop #6062 +/- ##
=============================================
+ Coverage 74.81% 74.89% +0.08%
- Complexity 5771 5782 +11
=============================================
Files 230 231 +1
Lines 17492 17505 +13
=============================================
+ Hits 13087 13111 +24
+ Misses 4405 4394 -11
Flags with carried forward coverage won't be shown. Click here to find out more.
|
@@ -109,50 +100,64 @@ public function transform( Document $document, ErrorCollection $errors ) { // ph | |||
} | |||
|
|||
if ( $candidate instanceof DOMElement ) { | |||
$hero_image_elements[] = $candidate; | |||
$hero_image_elements[ spl_object_hash( $candidate ) ] = $candidate; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The use of spl_object_hash()
allows for the removal of duplicates.
731b690
to
e8ea9c0
Compare
…de hero candidate
e8ea9c0
to
cb443cc
Compare
cb443cc
to
5100cd4
Compare
case 'featured_image': | ||
$candidate = $this->get_featured_image( $document ); | ||
break; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's an issue here with the featured image. Namely, if there is a template with multiple posts on it, and the first post lacks a featured image but the second post has one, then this featured image will get prerendered:
This issue is mitigated by preserving the loading=lazy
on the original img
as done in ampproject/amp-toolbox-php#149 but it is not ideal. Consider this worse case where there is a content image in the first post, but a featured image in the second. The featured image from the second post will win out over the content image in the first:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See also an e-commerce example (apparently using Astra and WooCommerce) in which the hero image is not marked up as a featured image, nor is it inside of .entry-content
. Rather, it has a selector main > div.ast-woocommerce-container > div.product-section > div > div > div:nth-child(1) > div > amp-img
.
This makes me think that we should perhaps eliminate the featured image check, in favor of getting the first images from the main
element as hero candidates, or if no main
then the first .hentry
.
Importantly, there is now a In a WordPress context, I think we should go ahead and SSR every image that has We could limit SSR of hero images to 2 if they lack |
I think we need to augment the transformer with regular WordPress hooks. Namely, we should add |
…image-candidate-determination * 'develop' of github.com:ampproject/amp-wp: (31 commits) Update snapshots and fix tests Remove references to AMP 'page' since it may be post; harmonize strings Use boolean return type for isDevToolsEnabled selector Add missing name prop for PluginDocumentSettingPanel in AMPDocumentSettingPanel Show AMP Validation sidebar button when all issues have been reviewed Omit AMPToolbarButton when AMP is not enabled Make entry path regex match simpler Show AMP toggle when Dev Tools are disabled Remove redundant build:css script Improve JS test coverage Ignore block editor plugins from test coverage Ignore block editor plugins in unit test Add more unit tests Fix broken E2E test Do not generate empty JS chunks for stylesheets Omit DependencyExtractionWebpackPlugin for CSS Add `build:css` back so that CI continues to work Move the entrypoint config so that it is in alphabetical order Handle CSS build process with Webpack Add unit tests to AMPDocumentStatusNotification ...
…image-candidate-determination * 'develop' of github.com:ampproject/amp-wp: (27 commits) Remove unused $matches Avoid failing validation requests when response is redirect Undo erroneous addition of is_callable() check in d0d2137 Improve static analysis and formatting Catch UnknownConfigurationClass exception when transformer has no config Avoid alignment using tabs Remove double blank lines Fix test failure caused by new amp-onerror script Fix code style issue Revert sorting the services array Add transformer config command Add transformer list command Fix code style issues Use injected optimizer service Complete docblock Add optimizer command Refactor registration of CLI commands to be integrated with the service container Refactor validation command into PSR-4 service-based architecture Test whether optimizer gets caching and fallback downloads Add AmpWPConfiguration test ...
…mage a hero candidate
Plugin builds for 67a2f91 are ready 🛎️!
|
// Twenty Ten. | ||
case 'twentyten': | ||
return [ | ||
'enable_determine_hero_images_transformer' => [], | ||
]; | ||
return []; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose the twentyten
case can be removed then?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried removing but then I got warnings:
[15-Apr-2021 03:16:16 UTC] PHP Warning: array_merge(): Expected parameter 2 to be an array, null given in /app/public/content/plugins/amp/includes/sanitizers/class-amp-core-theme-sanitizer.php on line 495
[15-Apr-2021 03:16:16 UTC] PHP Warning: Invalid argument supplied for foreach() in /app/public/content/plugins/amp/includes/sanitizers/class-amp-core-theme-sanitizer.php on line 499
So it's currently needed, due to how that array is used in the get_theme_features
method.
Co-authored-by: Pierre Gordon <16200219+pierlon@users.noreply.github.com>
} | ||
|
||
/** @covers ::add_data_hero_candidate_attribute() */ | ||
public function add_data_hero_candidate_attribute() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test was not being executed.
public function add_data_hero_candidate_attribute() { | |
public function test_add_data_hero_candidate_attribute() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops! Committed your suggestion in this commit: 67a2f91
/** @covers ::add_data_hero_candidate_attribute() */ | ||
public function add_data_hero_candidate_attribute() { | ||
$this->assertEquals( | ||
$array_with_attr, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$array_with_attr
is undefined here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😊 Fixed in 67a2f91
Co-authored-by: Pierre Gordon <16200219+pierlon@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Quite an impressive feat achieved here, LGTM!
* @return bool Whether the conditional object is needed. | ||
*/ | ||
public static function is_needed() { | ||
return amp_is_request(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll have to pay attention to this one. It means we need to ensure in the CLI context that we need to ensure this evaluates to true, otherwise these filters will be missing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. It will always be false in a WP-CLI context currently:
amp-wp/includes/amp-helper-functions.php
Lines 327 to 329 in 1e7ea02
// Short-circuit for cron, CLI, admin requests or requests to non-frontend pages. | |
if ( wp_doing_cron() || ( defined( 'WP_CLI' ) && WP_CLI ) || is_admin() || in_array( $pagenow, [ 'wp-login.php', 'wp-signup.php', 'wp-activate.php', 'repair.php' ], true ) ) { | |
return false; |
Summary
Fixes #6061
DetermineHeroImages
transformer by default, instead of just for the core themes.HeroCandidateFiltering
service which uses WP filters to automatically adddata-hero-candidate
attribute to images output viaget_custom_logo()
,get_header_image_tag()
, and thewp_get_attachment_image()
when the attachment image is the featured image of the queried object or first the post in the loop.DetermineHeroImages
since better handled now byHeroCandidateFiltering
.DetermineHeroImages
to identify all images beforemain
or the first.entry-content
as being header images, but omit images which were originally marked asloading=lazy
. Include even tiny images which havelogo
in the class name.Depends on:
loading
attribute onnoscript > img
fallback amp-toolbox-php#149Checklist