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

Responsive image attributes #1819

Merged
merged 15 commits into from
Nov 10, 2018
Merged

Responsive image attributes #1819

merged 15 commits into from
Nov 10, 2018

Conversation

maxxwv
Copy link
Contributor

@maxxwv maxxwv commented Oct 16, 2018

Issue

I didn't see an easy way to get the srcset and sizes attributes for a Timber\Image object that was as simple as post.thumbnail.src.

Solution

It seemed like a good idea to add srcset and sizes as public methods, allowing post.thumbnail.srcset and post.thumbnail.sizes. Unfortunately, .sizes didn't work but img_sizes did.

Impact

As far as I can tell, none.

Usage Changes

Should be no impact.

Considerations

For whatever reason, creating a size() method in the Image object wasn't working. It didn't throw an error, just that post.thumbnail.sizes didn't ever call the method. It'd be a more intuitive call than img_sizes, which would be nice.

Testing

There's really nothing to fail, it's just a method to get the data that's more in keeping with the rest of the project.

@codecov
Copy link

codecov bot commented Oct 16, 2018

Codecov Report

Merging #1819 into master will increase coverage by 0.24%.
The diff coverage is 100%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master    #1819      +/-   ##
============================================
+ Coverage     94.74%   94.98%   +0.24%     
- Complexity     1534     1535       +1     
============================================
  Files            48       48              
  Lines          3632     3629       -3     
============================================
+ Hits           3441     3447       +6     
+ Misses          191      182       -9
Impacted Files Coverage Δ Complexity Δ
lib/Image.php 90.62% <100%> (+5.34%) 73 <4> (+1) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a2a6705...3676282. Read the comment docs.

@jarednova jarednova added the 1.x label Oct 18, 2018
lib/Image.php Outdated
public function img_sizes( $size = "full" ) {
$img_sizes = wp_get_attachment_image_sizes($this->ID, $size);
$img_sizes = apply_filters('timber/image/img_sizes', $img_sizes, $this->ID);
$img_sizes = apply_filters('timber_image_img_sizes', $img_sizes, $this->ID);
Copy link
Member

Choose a reason for hiding this comment

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

We're deprecating this filter style, so you can remove

Suggested change
$img_sizes = apply_filters('timber_image_img_sizes', $img_sizes, $this->ID);

@jarednova
Copy link
Member

Thank you @maxxwv! This looks like a good implementation of the WP methods. Before we merge, I want to make sure we have test coverage (even though it's a pretty simple thing, I want to make sure the functionality is always checked). Is that something you'd be up for trying?

Copy link
Member

@gchtr gchtr left a comment

Choose a reason for hiding this comment

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

This seems like such a simple solution 🎉. Great stuff! And you also included examples, which is always great! Thanks for that!

I add some comments about the filters, because this is really my only concern right now.

lib/Image.php Outdated
public function srcset( $size = "full" ) {
$srcset = wp_get_attachment_image_srcset($this->ID, $size);
$srcset = apply_filters('timber/image/srcset', $srcset, $this->ID);
$srcset = apply_filters('timber_image_srcset', $srcset, $this->ID);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
$srcset = apply_filters('timber_image_srcset', $srcset, $this->ID);

Copy link
Member

Choose a reason for hiding this comment

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

Old filter style, see Jared’s review.

lib/Image.php Outdated
*/
public function srcset( $size = "full" ) {
$srcset = wp_get_attachment_image_srcset($this->ID, $size);
$srcset = apply_filters('timber/image/srcset', $srcset, $this->ID);
Copy link
Member

Choose a reason for hiding this comment

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

I’m not sure if we should add this filter, because the wp_get_attachment_image_srcset() function already provides 3 filters to filter the resulting srcset. What would be the benefit of having this against using the filters in wp_get_attachment_image_srcset()?

lib/Image.php Outdated
*/
public function img_sizes( $size = "full" ) {
$img_sizes = wp_get_attachment_image_sizes($this->ID, $size);
$img_sizes = apply_filters('timber/image/img_sizes', $img_sizes, $this->ID);
Copy link
Member

Choose a reason for hiding this comment

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

Also here, I’m not sure if we need this filter at all, because wp_get_attachment_image_sizes() calls wp_calculate_image_sizes() in the last line, which in turn has a wp_calculate_image_sizes filter in the last line. So the timber/image/img_sizes filter would be called immediately after wp_calculate_image_sizes. What would be the benefit of this?

@maxxwv
Copy link
Contributor Author

maxxwv commented Oct 18, 2018

Thanks for the comments!

@jarednova - Sure, I'll take a stab at some basic tests, though honestly it's been a while since I've done unit testing so I can't guarantee much there. I'll look at the test-timber-image.php file and maybe the resize tests for examples and inspiration, unless there's a more appropriate place.

@gchtr - I'm fine with pulling out those filters. I just put them in to match the src() interface more closely, to be honest. I'll pull them out in the next revision.

I was also thinking it could be handy to have a method that runs both these methods and returns a single string of the attributes, as neither is really all that useful on their own. Thoughts or opinions?

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.05%) to 93.98% when pulling 49a0fe9 on maxxwv:responsive_image into 604b3c1 on timber:master.

@coveralls
Copy link

coveralls commented Oct 19, 2018

Coverage Status

Coverage increased (+0.1%) to 94.294% when pulling 3676282 on maxxwv:responsive_image into a2a6705 on timber:master.

@maxxwv
Copy link
Contributor Author

maxxwv commented Oct 27, 2018

@gchtr @jarednova - not sure what to do about the coverage failure, but everything else should be good to go. Let me know if there's anything else you need from me.

also some bonus `@codeCoverageIgnore`s on some deprecated functions to help juice code coverage scores
@jarednova
Copy link
Member

Everything's looking good @maxxwv. I changed the spacing to match WP style (tabs > spaces). The coverage failure was b/c of the is_image sanity checks. I'm fine to leave as-is w/o writing a bunch of tests to cover the non-results — merged!

Thanks again for the code, tests and examples for the docs!

@jarednova jarednova merged commit 83cf568 into timber:master Nov 10, 2018
@maxxwv maxxwv deleted the responsive_image branch December 2, 2018 05:02
@drzraf
Copy link

drzraf commented Feb 5, 2019

The phpdoc example provided uses sizes="{{ post.thumbnail.sizes }}" instead of sizes="{{ post.thumbnail.img_sizes }}"

maxxwv added a commit to maxxwv/timber that referenced this pull request Feb 16, 2019
@maxxwv maxxwv mentioned this pull request Feb 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants