Change hook from post_update to wp_after_insert_post#4205
Open
obstschale wants to merge 1 commit into
Open
Conversation
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.
Detect permalink changes caused by taxonomy term updates in the block editor
Summary
When the "URL Monitor" feature is enabled for a post type whose permalink
structure includes a taxonomy placeholder (e.g.
/%category%/%postname%/),changing the assigned term in the block editor (Gutenberg) currently does
not create a redirect, even though the post's URL has effectively changed.
The same change made in the classic editor works correctly. The root cause is
the order in which WordPress fires its post and taxonomy hooks in the two save
flows.
Background
Red_Monitorinmodels/monitor.phprecords the old permalink inpre_post_update, then compares it toget_permalink()inpost_updatedandcreates a 301 redirect when they differ.
In
wp_insert_post()(classic editor) the relevant calls run in this order:pre_post_updateactiontax_inputterms are written (line 4998 ofwp-includes/post.php)post_updatedaction (line 5163)wp_after_insert_postaction (line 5208)In
WP_REST_Posts_Controller::update_item()(block editor / REST) the order isdifferent:
wp_update_post()is called, which internally firespost_updatedhandle_terms()writes the new terms (line 1015 ofclass-wp-rest-posts-controller.php)wp_after_insert_postaction (line 1047)In the REST flow
post_updatedtherefore fires before the new terms arewritten, so
get_permalink()still returns the URL with the old term andRed_Monitor::check_for_modified_slug()sees no difference.Change
Switch the monitor from the
post_updatedaction towp_after_insert_post,which is the only hook that fires after term assignment in both the classic
and REST flows.
The existing comparison logic in
check_for_modified_slug(),pre_post_update(), andcan_monitor_post()is unchanged. A small adaptermethod
post_after_insert()translates the four-argumentwp_after_insert_postsignature to the existing
post_updated()method and skips brand-new inserts(
$update === false), since a slug cannot change on insertion.Files touched
models/monitor.phpadd_action( 'post_updated', ... )withadd_action( 'wp_after_insert_post', array( $this, 'post_after_insert' ), 11, 4 ).post_after_insert( int $post_id, ?WP_Post $post, bool $update, ?WP_Post $post_before )which delegates to the existing
post_updated()when$updateistrue.tests/integration/models/test-monitor.phphas_action()assertions intestNoHooksandtestHasHooksfrom
post_updated/post_updatedtowp_after_insert_post/post_after_insert. Tests that call$monitor->post_updated(...)directly are unaffected because the methodsignature is unchanged.
What this fixes
that is part of the permalink structure now produces a redirect from the old
URL to the new URL, matching the behaviour of the classic editor.
What this does not fix (out of scope)
hierarchical taxonomy. These actions change the URLs of every post in that
term but do not save the posts themselves, so neither
post_updatednorwp_after_insert_postfires. Handling those cases would require additionalhooks on
edited_term/edited_term_taxonomyplus a UI option for selectingwhich taxonomies to monitor, and is intentionally left for a follow-up.
Compatibility
wp_after_insert_postwas introduced in WordPress 5.6 (December 2020), whichis well below the plugin's supported minimum.
Testing
php -lon both changed files: no syntax errors.tests/integration/models/test-monitor.phpcontinue to exercise the same code path via the unchanged
post_updated()method; the updated
has_action()assertions verify the new hookregistration.
Suggested manual verification
/%category%/%postname%/.the configured monitor group.