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

Add srcset and sizes in content with a filter #177

Merged
merged 1 commit into from
Sep 23, 2015
Merged

Conversation

joemcgill
Copy link
Member

This changes our approach from one where srcset and sizes are written to the
image markup in post content to one where the responsive attributes are added on the
front end when the page is being rendered. There is a small performance hit but the
benefit is worth the tradeoff for what is gained. What exactly do we gain? Glad you
asked:

  • Automatically extend support to images in posts published before we responsive image
    support was available.
  • Removes the need to update the markup in TinyMCE when the image size is changed.
  • Keeps sizes attributes, which should probably be adjusted based on the needs of the
    theme, from being stored in the database.
  • Is non-destructive if a site needs to change available image sizes and regenerate
    thumbnails.

Changes:

  • Removes the old JS code for handling markup changes in TinyMCE
  • Adds tevkori_filter_content_images() as a display filter.
  • Adds _tevkori_filter_content_images_callback() to process filtered images.
  • Adds tests

Props @jaspermdegroot, @peterwilsoncc
Related: #170
Replaces: #175
Resolves: #83

@joemcgill joemcgill modified the milestone: 2.4.0 Sep 22, 2015
@joemcgill joemcgill self-assigned this Sep 22, 2015
@joemcgill joemcgill added this to the 2.5 milestone Sep 22, 2015
This changes our approach from one where `srcset` and `sizes` are written to the
image markup in post content to one where the responsive attributes are added on the
front end when the page is being rendered. There is a small performance hit but the
benefit is worth the tradeoff for what is gained. What exactly do we gain? Glad you
asked:

* Automatically extend support to images in posts published before we responsive image
support was available.
* Removes the need to update the markup in TinyMCE when the image size is changed.
* Keeps `sizes` attributes, which should probably be adjusted based on the needs of the
theme, from being stored in the database.
* Is non-destructive if a site needs to change available image sizes and regenerate
thumbnails.

Changes:

* Removes the old JS code for handling markup changes in TinyMCE
* Adds `tevkori_filter_content_images()` as a display filter.
* Adds `_tevkori_filter_content_images_callback()` to process filtered images.
* Adds tests
@joemcgill
Copy link
Member Author

I replaced the tevkori_filter_content_sizes() function in order to provide coverage to content that has been created with the plugin active, so users don't experience #178. At some point, we'll need to provide users with a cleanup strategy for posts including srcset attributes with data-sizes attributes.

$uploads_dir = wp_upload_dir();
$path_to_upload_dir = $uploads_dir['baseurl'];

$content = preg_replace_callback( '|<img ([^>]+' . $path_to_upload_dir . '[^>]+)[\s?][\/?]>|i', '_tevkori_filter_content_images_callback', $content );
Copy link
Member

Choose a reason for hiding this comment

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

@joemcgill

I am not sure but when I look at this regex it appears to me that the first group will contain everything up to the first > that it finds. So if there is a space and slash before the closing tag, it will be included.
Shouldn't we remove [\s?][\/?] and then change line 418 from

$image_html = "<img " . $atts . " " . $srcset . " " . $sizes . " />";

to

$image_html = "<img $srcset . " " . $sizes . " " . $atts . ">";

?
The space and the slash will be part of $atts.
Or am I wrong?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants