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

Prevent optimizing post previews by default #1848

Merged
merged 6 commits into from
Feb 6, 2025

Conversation

hbhalodia
Copy link
Contributor

Summary

Fixes #1841

Relevant technical choices

Copy link

github-actions bot commented Feb 5, 2025

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: hbhalodia <[email protected]>
Co-authored-by: westonruter <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Copy link

codecov bot commented Feb 5, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 65.98%. Comparing base (68c6deb) to head (e961c37).
Report is 7 commits behind head on trunk.

Additional details and impacted files
@@           Coverage Diff           @@
##            trunk    #1848   +/-   ##
=======================================
  Coverage   65.97%   65.98%           
=======================================
  Files          88       88           
  Lines        6895     6896    +1     
=======================================
+ Hits         4549     4550    +1     
  Misses       2346     2346           
Flag Coverage Δ
multisite 65.98% <100.00%> (+<0.01%) ⬆️
single 38.18% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@westonruter westonruter added [Type] Bug An existing feature is broken [Plugin] Optimization Detective Issues for the Optimization Detective plugin labels Feb 5, 2025
@westonruter
Copy link
Member

Thank you! Could you please add a test case to make sure that this condition is working as expected?

@hbhalodia
Copy link
Contributor Author

hbhalodia commented Feb 5, 2025

Hi @westonruter, I had tried to add the test case for the same, Could you please check if below is the correct way to add a test case for this?

'singular_as_post_preview'  => array(
  'set_up'   => static function (): string {
	  $post_id = self::factory()->post->create( array( 'post_title' => 'Hello', 'post_status' => 'draft' ) );
	  return get_permalink( $post_id );
  },
  'expected' => false,
),

We can add the above here after this,

'home_customizer_preview_as_anonymous' => array(
'set_up' => static function (): string {
global $wp_customize;
require_once ABSPATH . 'wp-includes/class-wp-customize-manager.php';
$wp_customize = new WP_Customize_Manager();
$wp_customize->start_previewing_theme();
return home_url( '/' );
},
'expected' => false,

If yes, I would update the PR with the changes, I can update the test case name as per the standards.

I do have run the test locally, by mocking this as publish and draft and it results in correct output as expected after adding the condition.

Thank You,

@westonruter
Copy link
Member

@hbhalodia that mostly looks good, but probably should use get_preview_post_link().

@hbhalodia
Copy link
Contributor Author

Hi @westonruter, Thanks for the suggestion.

I have one issue, when I try to commit this, phpstan is giving issue related to return value as it should only be string, but function return either string|null. I tried adding the return value to accept string|null, but it throws error related native union type, I committed it with --no-verify, Can you please suggest how can I pass this failed action.

Thank You,

@hbhalodia
Copy link
Contributor Author

Hi @westonruter, This is now fixed. Updated the PR with the changes and test cases.

Thank You,

@@ -167,6 +167,8 @@ function od_can_optimize_response(): bool {
// > Access to script at '.../detect.js?ver=0.4.1' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
// So it's better to just avoid attempting to optimize Post Embed responses (which don't need optimization anyway).
is_embed() ||
// Skip posts that aren't published yet.
is_preview() ||
Copy link
Member

Choose a reason for hiding this comment

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

I tried commenting out this line to see if the new test would start to fail as expected, but it still passed. This is because the test's set_up logic wasn't setting the current user to be able to view the post preview in the first place. When this happens, the $wp_query->posts loop is then empty. This results in od_get_cache_purge_post_id() returning null which as can be seen below also results in this od_can_optimize_response() function returning false. So I update the test in e961c37 to explicitly set the current user so that is_preview() will return true.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @westonruter, for the fix and insights on this.

@westonruter westonruter changed the title Fix: Add is_preview() condition to not show url metric if post is not in published status Prevent optimizing post previews by default Feb 5, 2025
Copy link
Member

@westonruter westonruter left a comment

Choose a reason for hiding this comment

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

Thank you!

@westonruter westonruter merged commit ff428a3 into WordPress:trunk Feb 6, 2025
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Plugin] Optimization Detective Issues for the Optimization Detective plugin [Type] Bug An existing feature is broken
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Optimization Detective should be disabled by default on post previews
2 participants