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 replacing of images only in frontend context #424

Merged
merged 8 commits into from Jul 14, 2022

Conversation

akshitsethi
Copy link
Contributor

@akshitsethi akshitsethi commented Jul 12, 2022

Summary

Fixes #379

Relevant technical choices

Bails early if the request is not for the frontend context or is coming from a feed url. We also check if the request is made inside the wp_head hook so that original images are returned for use within <head> tags.

In-progress:

  • Add tests to verify

Checklist

  • PR has either [Focus] or Infrastructure label.
  • PR has a [Type] label.
  • PR has a milestone or the no milestone label.

@akshitsethi akshitsethi added [Type] Enhancement A suggestion for improvement of an existing feature [Focus] Images Issues related to the Images focus area [Plugin] Modern Image Formats Issues for the Modern Image Formats plugin (formerly WebP Uploads) labels Jul 12, 2022
@akshitsethi akshitsethi added this to the 1.3.0 milestone Jul 12, 2022
@bethanylang bethanylang added this to Backlog in [Focus] Images via automation Jul 12, 2022
@bethanylang bethanylang moved this from Backlog to Review in [Focus] Images Jul 12, 2022
@@ -240,3 +240,19 @@ function webp_uploads_get_attachment_sources( $attachment_id, $size = 'thumbnail
// Return an empty array if no sources found.
return array();
}

/**
* Verify if the request if for the frontend context.
Copy link
Member

Choose a reason for hiding this comment

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

It should be Verify if the request is for the frontend context.

Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

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

@akshitsethi This looks like a good start, however some of the conditions need to be refined a bit. In combination with that, some more accurate test coverage would be helpful too.

modules/images/webp-uploads/helper.php Outdated Show resolved Hide resolved
modules/images/webp-uploads/helper.php Outdated Show resolved Hide resolved
tests/modules/images/webp-uploads/load-tests.php Outdated Show resolved Hide resolved
tests/modules/images/webp-uploads/load-tests.php Outdated Show resolved Hide resolved
tests/modules/images/webp-uploads/load-tests.php Outdated Show resolved Hide resolved
tests/modules/images/webp-uploads/load-tests.php Outdated Show resolved Hide resolved

$tag = wp_get_attachment_image( $attachment_id, 'medium', false, array( 'class' => "wp-image-{$attachment_id}" ) );
$this->assertNotSame( $tag, webp_uploads_update_image_references( $tag ) );
}
Copy link
Member

Choose a reason for hiding this comment

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

Let's add one more test that covers the scenario where you're in a frontend context, but before the user facing content (i.e. template_redirect has run but wp_head hasn't). Maybe that could be called it_should_not_replace_image_references_when_in_frontend_context_but_before_body.

@felixarntz
Copy link
Member

@akshitsethi @adamsilverstein I have made the additional changes here around the conditions and enhanced test coverage, made it specific to the new function.

@akshitsethi Your previous point on did_action( 'wp_head' ) being problematic was correct, however it is only a problem for full site editing themes, because for them, all the page content is generated before the page is rendered, so wp_head will only run after all the content hooks. On traditional themes, it works fine. With that said, of course this is not an acceptable solution, so I decided it is okay to omit the check for now. Content in the head should always be rendered as part of wp_head, and that applies even to full site editing themes.

Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

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

Thanks @akshitsethi!

Copy link
Member

@adamsilverstein adamsilverstein left a comment

Choose a reason for hiding this comment

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

Nice!

@felixarntz felixarntz merged commit 050b5ad into trunk Jul 14, 2022
[Focus] Images automation moved this from Review to Done Jul 14, 2022
@felixarntz felixarntz deleted the fix/379-replace-content-images-frontend branch July 14, 2022 20:39
Comment on lines +457 to +489

public function test_webp_uploads_in_frontend_body_without_wp_query() {
unset( $GLOBALS['wp_query'] );

$this->assertFalse( webp_uploads_in_frontend_body() );
}

public function test_webp_uploads_in_frontend_body_with_feed() {
$this->mock_empty_action( 'template_redirect' );
$GLOBALS['wp_query']->is_feed = true;

$this->assertFalse( webp_uploads_in_frontend_body() );
}

public function test_webp_uploads_in_frontend_body_without_template_redirect() {
$this->assertFalse( webp_uploads_in_frontend_body() );
}

public function test_webp_uploads_in_frontend_body_before_template_redirect() {
$result = webp_uploads_in_frontend_body();
$this->mock_empty_action( 'template_redirect' );

$this->assertFalse( $result );
}

public function test_webp_uploads_in_frontend_body_after_template_redirect() {
$this->mock_empty_action( 'template_redirect' );
$result = webp_uploads_in_frontend_body();

$this->assertTrue( $result );
}

public function test_webp_uploads_in_frontend_body_within_wp_head() {
Copy link
Member

Choose a reason for hiding this comment

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

More of nit-pick here but the test function names are different than what we actually use the names here like it_should or it_ like that. And these functions doesn't have function doc comment.

cc: @felixarntz @akshitsethi

Copy link
Member

Choose a reason for hiding this comment

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

@mehulkaklotar I didn't think it was too critical to point it out in earlier tests for the plugin, but the convention for WordPress core tests is to always use test_ followed by the function name, then optionally anything else. So I stuck with that here.

I would advise to follow that convention for future tests to monitor the core plan.

Regarding doc comments, test methods do not require any doc comments per core specifications - only when helpful. Preferably, the name of the method should be clear enough to know what is going on, or when it makes sense to clarify specific code, inline comments should be used.

cc @akshitsethi @adamsilverstein @mukeshpanchal27 @jjgrainger @eugene-manuilov

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Focus] Images Issues related to the Images focus area [Plugin] Modern Image Formats Issues for the Modern Image Formats plugin (formerly WebP Uploads) [Type] Enhancement A suggestion for improvement of an existing feature
Projects
Development

Successfully merging this pull request may close these issues.

Only replace content images with additional MIME type version when in a frontend request context by default
6 participants