-
Notifications
You must be signed in to change notification settings - Fork 53
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
Content filtering #170
Content filtering #170
Conversation
|
||
remove_filter( 'editor_max_image_size', 'tevkori_editor_image_size' ); | ||
function tevkori_filter_content_images( $content ) { | ||
preg_match_all( '/src="([^"]*)"/', $content, $matches ); |
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.
Is it possible to skip images with srcset in this line, regexp isn't my strong point sorry.
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 am not a regex expert either, but I don't think they should be used to check if something is not there. If it's possible to write a regex like that it will probably not perform very well.
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.
We should limit this to image elements because iframes also have a src
attribute.
7508cae
to
8362131
Compare
// Build the data-sizes attribute if sizes were returned. | ||
$sizes = tevkori_get_sizes( $id, $size ); | ||
$sizes = $sizes ? 'data-sizes="' . $sizes . '"' : ''; | ||
$attachments = tevkori_attachment_urls_to_loop( $urls ); |
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 URLs that we pass to the function contain the size suffix (eg. -300x200) if present. I think we have to strip that like we do in https://github.com/ResponsiveImagesCG/wp-tevko-responsive-images/pull/170/files#diff-517abe0a5b7ee2c55c6b5659d0c9350eR52 but would be nice if we don't have to do it at two places.
0b29f60
to
7ffecaf
Compare
return; | ||
} | ||
foreach ( $paths as $k => $path ) { | ||
$image_path = parse_url( $path ); |
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.
Here we could probably add the check if the file path matches with the upload dir and skip if not, as suggested by @joemcgill in #144 (comment)
All tests pass but for some reason it doesn't work if I test on a local WordPress installation. I don't get any errors, but no |
I now understand that our tests only test specific functions. We don't have a general test yet that checks if an image in a piece of content gets the correct @peterwilsoncc - Did your code work for you or didn't have a chance to test it? |
@jaspermdegroot I tested it against a post with two images & it appeared to work. I'll investigate further this weekend. I'll throw it against various http & https & FORCE_SSL configs. |
7ffecaf
to
cdfd417
Compare
b176b9f
to
567a2c3
Compare
See commit 915d8c3 in content-filtering-2 branch.
85a3cba
to
2e77803
Compare
Currently we have the following approaches:
The reason we switched from the first to the second approach was because the core classes could be removed from the image markup. I don't know if there are filters to remove those before the markup is inserted, but I think it would be very bad practise if a plugin (or even worse, a theme) does this because it can't be reverted when removing the plugin or switching theme. Of course a user can remove classes in the editor after the image markup has been inserted, but I think we are talking about an edge case here. The downside of using the image url to get the ID and size info is that we need to strip the size (-###x###) and "edited" (-e#############) suffixes from the url. That's tricky because the original file name could contain such a pattern. @peterwilsoncc has been working on the third approach which would reduce DB request and should improve performance, but the code didn't seem to work yet so there are no performance test results of this. However, even if we could improve performance, I think that we should first choose the most reliable way to get the ID and size, and after that optimize for performance. If everyone agrees with that approach I suggest the following next steps:
Update: After taking a closer look at PR #175 I noticed that with @joemcgill his solution we don't need to use a regex to strip the size part from the url to find the image in the database. So the only downside that remains is the extra DB request, but since it's only used as fallback that isn't really a problem. |
Great summary @jaspermdegroot. Another improvement I'd like to add to #175 is to use |
$sizes = $srcset = ''; | ||
|
||
// Check if srcset attribute is not already present. | ||
if ( false !== strpos( 'srcset="', $atts ) ) { |
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.
Correction:
if ( false === strpos( $atts, 'srcset="' ) ) {
I've added some inline notes with bug fixes, you can cherry pick from 9cad921 in my fork. I can add a pull request to this branch if you wish, but PR inception seems excessive for so few changes. |
Threw some SSL related tests at this: front end accessible over SSL, site scheme set to http in options. Brain dump follows. Inserting the image in the editor using:
I'm not sure of the solution as of yet, some wrappers around the WP scheme functions might be in order to better handle relative URLs. |
@jaspermdegroot and @peterwilsoncc: I don't want to lose the progress that has been made towards limiting the potential DB hits needed, so instead of merging the work in #175 into this branch, I've squashed those changes into #177 so it can be merged directly into dev. In the event that we need to further improve the DB query approach in #177, we can come back to this branch for reference and see the full history. Sound good? |
Sounds good. I will address the issues mentioned by @peterwilsoncc in this branch. |
👍, sounds like a plan. |
Closing in favor of #177, but we can reopen as needed. |
Ok. I pushed a few more commits to the branch to fix the issues that @peterwilsoncc mentioned, but there is still more work to do if we want to make this approach work. Since we decided to go with #177 let's not invest time in this now. |
Looks like the needle/haystack are still reversed on the srcset attribute check if you need to come back to it. |
This PR replaces #144
See #83