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

Consider path filtering for images in content. #190

Closed
joemcgill opened this issue Sep 28, 2015 · 12 comments · Fixed by #206
Closed

Consider path filtering for images in content. #190

joemcgill opened this issue Sep 28, 2015 · 12 comments · Fixed by #206

Comments

@joemcgill
Copy link
Member

When we filter the HTML of images in posts, we only do so if the image path matches the value of wp_uploads_dir(). We may need to keep in mind cases where a developer would want to filter images that have been uploaded somewhere else (e.g. S3).

See: #177 (comment)

@tevko
Copy link
Member

tevko commented Sep 28, 2015 via email

@joemcgill
Copy link
Member Author

cc: @bradt

@bradt
Copy link

bradt commented Sep 30, 2015

@tevko I don't understand the question.

@joemcgill
Copy link
Member Author

@bradt I believe he's asking if it's possible to identify the Attachment ID of an image that's been moved offsite, when the uploads directory remains on the server. And if not, how would we determine the other available image sizes to include in the srcset attribute?

@joemcgill
Copy link
Member Author

We should try to address this before a core merge, if possible.

@joemcgill joemcgill added this to the 2.5.2 milestone Oct 1, 2015
@peterwilsoncc
Copy link

wp_upload_dir['baseurl'] is filterable, so a plugin offloading images to a CDN/S3 should probably change the URL before it hits the regular expression in tevkori_filter_content_images.

The full filter in wp_upload_dir is:

/**
 * Filter the uploads directory data.
 *
 * @since 2.0.0
 *
 * @param array $uploads Array of upload directory data with keys of 'path',
 *                       'url', 'subdir, 'basedir', and 'error'.
 */
$uploads = apply_filters( 'upload_dir',
    array(
        'path'    => $dir,
        'url'     => $url,
        'subdir'  => $subdir,
        'basedir' => $basedir,
        'baseurl' => $baseurl,
        'error'   => false,
    ) );

@joemcgill
Copy link
Member Author

That's what I was thinking too, but I wasn't sure if @bradt had a use case where this wouldn't be the case.

@bradt
Copy link

bradt commented Oct 1, 2015

Yes, there is, as I described here: #177 (comment)

@bradt
Copy link

bradt commented Oct 1, 2015

I'll lay it out in more detail...

Let's say you have a site with a bunch of existing media, but you want to start offloading any NEW media to S3. You could install WP Offload S3 (free version) and do just that. Now your new content will have S3 URLs and old content will have URLs to your server.

In that case, you're going to want RICG to look for both S3 URLs and URLs to your local server.

peterwilsoncc added a commit to peterwilsoncc/wp-tevko-responsive-images that referenced this issue Oct 2, 2015
@joemcgill joemcgill removed this from the 2.5.2 milestone Oct 2, 2015
@joemcgill
Copy link
Member Author

Important to get this one right. Easier to add a filter than move it later, so I'm moving it off this milestone for now.

@peterwilsoncc
Copy link

I'm glad you caught this, it occurred to me after the event filtering the regex would have an affect on the matches array & may cause problems with reset of the function.

Some random thoughts:

  • change the callback so it accepts the entire image tag as a string and works from that.
  • use a filter similar to pre_option_<option name> that continues if the returned value is false and returns the result as $content if it's a string.

@joemcgill
Copy link
Member Author

change the callback so it accepts the entire image tag as a string and works from that

This is exactly what I've been thinking. Currently, the callback only works if we can parse the id from the HTML, so it shouldn't matter what the src is. Now that we're no longer using a preg_replace_callback() it makes less sense for us to use a callback, and a utility function that accepts a full img element as a string and returns the HTML with srcset and sizes added, would be way more useful.

I'll add a PR with the code for how this could work shortly.

joemcgill added a commit that referenced this issue Oct 4, 2015
Now that we're no longer using a `preg_replace_callback` function in
`tevkori_filter_content_images()`, it doesn't make as much sense to use
the callback function the way we originally set it up. However, rather
that merging the two function, this turns the callback into a useful
utility function that accepts an `img` element and returns the HTML
including `srcset` and `sizes` attributes if we can.

Additionally, since we only add `srcset` and `sizes` when we're able to
parse the attachment id from the markup, we also no longer need to limit
our matches in the content filter by files containing the path to our
uploads folder. This resolves #190.
joemcgill added a commit that referenced this issue Oct 4, 2015
Now that we're no longer using a `preg_replace_callback` function in
`tevkori_filter_content_images()`, it doesn't make as much sense to use
the callback function the way we originally set it up. However, rather
that merging the two function, this turns the callback into a useful
utility function that accepts an `img` element and returns the HTML
including `srcset` and `sizes` attributes if we can.

Additionally, since we only add `srcset` and `sizes` when we're able to
parse the attachment id from the markup, we also no longer need to limit
our matches in the content filter by files containing the path to our
uploads folder. This resolves #190.
joemcgill added a commit that referenced this issue Oct 4, 2015
Now that we're no longer using a `preg_replace_callback` function in
`tevkori_filter_content_images()`, it doesn't make as much sense to use
the callback function the way we originally set it up. However, rather
that merging the two function, this turns the callback into a useful
utility function that accepts an `img` element and returns the HTML
including `srcset` and `sizes` attributes if we can.

Additionally, since we only add `srcset` and `sizes` when we're able to
parse the attachment id from the markup, we also no longer need to limit
our matches in the content filter by files containing the path to our
uploads folder. This resolves #190.
joemcgill added a commit that referenced this issue Oct 4, 2015
Now that we're no longer using a `preg_replace_callback` function in
`tevkori_filter_content_images()`, it doesn't make as much sense to use
the callback function the way we originally set it up. However, rather
that merging the two function, this turns the callback into a useful
utility function that accepts an `img` element and returns the HTML
including `srcset` and `sizes` attributes if we can.

Additionally, since we only add `srcset` and `sizes` when we're able to
parse the attachment id from the markup, we also no longer need to limit
our matches in the content filter by files containing the path to our
uploads folder. This resolves #190.
joemcgill added a commit that referenced this issue Oct 5, 2015
Now that we're no longer using a `preg_replace_callback` function in
`tevkori_filter_content_images()`, it doesn't make as much sense to use
the callback function the way we originally set it up. However, rather
that merging the two function, this turns the callback into a useful
utility function that accepts an `img` element and returns the HTML
including `srcset` and `sizes` attributes if we can.

Additionally, since we only add `srcset` and `sizes` when we're able to
parse the attachment id from the markup, we also no longer need to limit
our matches in the content filter by files containing the path to our
uploads folder. This resolves #190.
joemcgill added a commit that referenced this issue Oct 6, 2015
Now that we're no longer using a `preg_replace_callback` function in
`tevkori_filter_content_images()`, it doesn't make as much sense to use
the callback function the way we originally set it up. However, rather
that merging the two function, this turns the callback into a useful
utility function that accepts an `img` element and returns the HTML
including `srcset` and `sizes` attributes if we can.

Additionally, since we only add `srcset` and `sizes` when we're able to
parse the attachment id from the markup, we also no longer need to limit
our matches in the content filter by files containing the path to our
uploads folder. This resolves #190.
joemcgill added a commit that referenced this issue Oct 6, 2015
Now that we're no longer using a `preg_replace_callback` function in
`tevkori_filter_content_images()`, it doesn't make as much sense to use
the callback function the way we originally set it up. However, rather
that merging the two function, this turns the callback into a useful
utility function that accepts an `img` element and returns the HTML
including `srcset` and `sizes` attributes if we can.

Additionally, since we only add `srcset` and `sizes` when we're able to
parse the attachment id from the markup, we also no longer need to limit
our matches in the content filter by files containing the path to our
uploads folder. This resolves #190.
joemcgill added a commit that referenced this issue Oct 6, 2015
…dates

Refactor display filtering functions

Now that we're no longer using a preg_replace_callback function in
tevkori_filter_content_images(), it doesn't make as much sense to use
the callback function the way we originally set it up. However, rather
that merging the two function, this turns the callback into a useful
utility function that accepts an img element and returns the HTML
including srcset and sizes attributes if we can.

Additionally, since we only add srcset and sizes when we're able to
parse the attachment id from the markup, we also no longer need to limit
our matches in the content filter by files containing the path to our
uploads folder. This resolves #190.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants