Random Redirect: restore module to the Jetpack plugin - #50940
Conversation
… instead of ORDER BY RAND().
…w disabling the redirect.
|
Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.
Interested in more tips and information?
|
|
Thank you for your PR! When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:
This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖 Follow this PR Review Process:
If you have questions about anything, reach out in #jetpack-developers for guidance! Jetpack plugin: No scheduled milestone found for this plugin. If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack. |
Code Coverage SummaryCoverage changed in 1 file.
1 file is newly checked for coverage.
|
|
@mlaetitia I've published this PR because it's ready for review according to @marktmcgrath and the added label. |
DevinWalker
left a comment
There was a problem hiding this comment.
I have tested this thoroughly and looked at the code. All your testing items above passed and this is ready to merge.
… has (#51037) The module was removed from the plugin in 13.6 (#38310) and restored in 16.1 (#50940). While it was gone, some themes and plugins started shipping their own copy of jetpack_matt_random_redirect(). A theme's functions.php runs before after_setup_theme, where Jetpack loads module-extras.php, so the theme declares the function first and the plugin's copy fatals. Wrap the declaration and its hook in a function_exists() check. The declaration has to live inside the conditional rather than behind an early return: PHP binds unconditional top-level function declarations when the file is compiled, so a runtime check before one never gets the chance to prevent it. Claude-Session: https://claude.ai/code/session_01U3VxzdSXMePRtoxepyNGYZ Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
… has (#51037) The module was removed from the plugin in 13.6 (#38310) and restored in 16.1 (#50940). While it was gone, some themes and plugins started shipping their own copy of jetpack_matt_random_redirect(). A theme's functions.php runs before after_setup_theme, where Jetpack loads module-extras.php, so the theme declares the function first and the plugin's copy fatals. Wrap the declaration and its hook in a function_exists() check. The declaration has to live inside the conditional rather than behind an early return: PHP binds unconditional top-level function declarations when the file is compiled, so a runtime check before one never gets the chance to prevent it. Claude-Session: https://claude.ai/code/session_01U3VxzdSXMePRtoxepyNGYZ Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
Proposed changes
Restore the Random Redirect module that was removed from the Jetpack plugin in #38310, bringing back support for
yoursite.example/?randomredirects to a random published post.While restoring it, the post-selection query was updated to use a COUNT plus random OFFSET lookup instead of
ORDER BY RAND(), which is expensive and uncached on large sites.Note: this 'module' can't be turned off on its own. If that is needed then we need to turn this into a proper Jetpack module. Alternatively, the
jetpack_random_redirect_enabledfilter can be used to turn this off, but it's undocumented at this point.Related product discussion/links
Does this pull request change what data or activity we track or use?
No.
Testing instructions
yoursite.example/?random— repeat visits should redirect to varying published posts.?random&random_post_type=page— redirects to a random page.?random&random_cat_id=<category term ID>— redirects stay within that category.?random&random_cat_id=99999(nonexistent) — page loads normally, no redirect.add_filter( 'jetpack_random_redirect_enabled', '__return_false' );and confirm?randomno longer redirects.