fix(republish): preserve capability-gated content in scheduled republish#494
Open
enricobattocchi wants to merge 2 commits intotrunkfrom
Open
fix(republish): preserve capability-gated content in scheduled republish#494enricobattocchi wants to merge 2 commits intotrunkfrom
enricobattocchi wants to merge 2 commits intotrunkfrom
Conversation
WP-Cron runs without a logged-in user, which caused capability-gated save filters (kses, WPBakery's wpb_remove_custom_html, and similar) to strip otherwise valid content during a scheduled Rewrite & Republish, removing the inner content of shortcodes such as [vc_raw_html] from the original post. Run the republish as the copy author so those filters evaluate against a real user, and restore the previous user afterwards in a try/finally. Setting the user also triggers WordPress's own kses_init via the set_current_user action, replacing the previous manual kses_remove_filters / kses_init_filters workaround. Fixes #210
- Lower YOASTCS_THRESHOLD_ERRORS from 57 to 52 to match the new count after removing the kses_remove_filters / kses_init_filters block. - Grant super-admin to the test user on multisite in test_republish_scheduled_post_preserves_raw_html_content. On multisite regular Administrators do not have the unfiltered_html capability, only Super Admins do, which caused the simulated stripping filter to strip content even when the fix was working correctly.
Coverage Report for CI Build 25155582808Warning No base build found for commit Coverage: 59.896%Details
Uncovered ChangesNo uncovered changes found. Coverage RegressionsRequires a base build to compare against. How to fix this → Coverage Stats
💛 - Coveralls |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
The Rewrite & Republish feature publishes scheduled copies via WP-Cron. WP-Cron runs without a logged-in user, which caused capability-gated
post_contentfilters to treat the operation as untrusted: WordPress's kses, WPBakery Page Builder'swpb_remove_custom_html(which strips[vc_raw_html],[vc_raw_js]and[vc_gmaps]shortcodes when the current user lacksunfiltered_html), and other similar filters from third-party plugins. As a result, the inner content of WPBakery Raw HTML elements was being deleted from the original post when a Rewrite & Republish copy was scheduled to be published in the future, even though the user who scheduled the republish hadunfiltered_html.Summary
This PR can be summarized in the following changelog entry:
[vc_raw_html](and similar capability-gated) shortcodes was deleted from the original post when a scheduled Rewrite & Republish was published via WP-Cron.changelog: bugfixRelevant technical choices:
republish()and restore the previous user afterwards in atry/finally. This makes capability-gated filters evaluate against a real user the way they do for any normal save.wp_set_current_user()also triggers WordPress's ownkses_initthrough theset_current_useraction, which adds or removes kses filters based on that user's actual capabilities. This makes the previous manualkses_remove_filters()/kses_init_filters()workaround unnecessary, so it has been removed.post_contentsave filters on capability checks now behaves correctly during scheduled republish.try/finallycorrectly restores the previous user whenrepublish()throws).Test instructions
Test instructions for the acceptance test before the PR gets merged
This PR can be acceptance tested by following these steps:
<script>tag inside).wp cron event run --due-nowwith WP-CLI).Relevant test scenarios
The bug is in the cron handler, so it should reproduce regardless of which editor was used to schedule the copy. Worth verifying both Block Editor and Classic Editor flows still schedule and republish correctly.
Test instructions for QA when the code is in the RC
Impact check
This PR affects the following parts of the plugin, which may require extra testing:
Post_Republisher::republish_scheduled_post). Immediate Republish (Block Editor REST and Classic Editor) is unchanged because it already runs in an authenticated user context.wp_insert_post_dataorcontent_save_prewith capability checks will now see the copy author as the current user during scheduled republish (previously: user 0). This is the intended behaviour and matches what those filters see during any normal save by the same user.UI changes
Documentation
Quality assurance
Innovation
innovationlabel and noted the work hours.Fixes #210