Skip to content

Commit 576bc20

Browse files
swissspidyclaude
andcommitted
Apply the requested modification date to the target post only
The filter stayed hooked until the outer wp_insert_post() call returned, so a save_post handler that inserts another post in the meantime would have given that post the command's date too. The callback now unhooks itself on its first run, which is always the command's own post; the cleanup after the call remains as a fallback for when it never ran. A scenario with a mu-plugin that inserts a post from save_post pins it for both create and update. The local-only scenarios now also assert the derived post_modified_gmt value under a non-UTC site timezone. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QWUCg3yvcZ4uDBf2cwQ7X1
1 parent 51e8a79 commit 576bc20

2 files changed

Lines changed: 88 additions & 1 deletion

File tree

‎features/post.feature‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,11 @@ Feature: Manage WordPress posts
882882
Scenario: Set a post's modification date on update
883883
Given a WP install
884884

885+
# A timezone with an offset, so the GMT value derived from the local one is
886+
# distinguishable from it. 1 January is outside DST in New York.
887+
When I run `wp option update timezone_string 'America/New_York'`
888+
Then STDOUT should not be empty
889+
885890
When I run `wp post create --post_title='A post' --post_status=publish --porcelain`
886891
Then STDOUT should be a number
887892
And save STDOUT as {POST_ID}
@@ -898,9 +903,18 @@ Feature: Manage WordPress posts
898903
2020-01-01 12:00:00
899904
"""
900905

906+
When I run `wp post get {POST_ID} --field=post_modified_gmt`
907+
Then STDOUT should be:
908+
"""
909+
2020-01-01 17:00:00
910+
"""
911+
901912
Scenario: Set a post's modification date on create
902913
Given a WP install
903914

915+
When I run `wp option update timezone_string 'America/New_York'`
916+
Then STDOUT should not be empty
917+
904918
When I run `wp post create --post_title='Another post' --post_date='2019-05-05 10:00:00' --post_modified='2020-01-01 12:00:00' --porcelain`
905919
Then STDOUT should be a number
906920
And save STDOUT as {POST_ID}
@@ -911,6 +925,12 @@ Feature: Manage WordPress posts
911925
2020-01-01 12:00:00
912926
"""
913927

928+
When I run `wp post get {POST_ID} --field=post_modified_gmt`
929+
Then STDOUT should be:
930+
"""
931+
2020-01-01 17:00:00
932+
"""
933+
914934
Scenario: A post's modification date defaults to the current time
915935
Given a WP install
916936

@@ -1073,3 +1093,62 @@ Feature: Manage WordPress posts
10731093
"""
10741094
2021-02-02 13:00:00
10751095
"""
1096+
1097+
Scenario: A post inserted by a save hook keeps its own modification date
1098+
Given a WP install
1099+
And a wp-content/mu-plugins/test-save-hook.php file:
1100+
"""
1101+
<?php
1102+
// Plugin Name: Test Save Hook
1103+
add_action(
1104+
'save_post',
1105+
function ( $post_id, $post ) {
1106+
if ( 'Trigger' !== $post->post_title || 'revision' === $post->post_type ) {
1107+
return;
1108+
}
1109+
wp_insert_post(
1110+
[
1111+
'post_title' => 'Inserted by hook',
1112+
'post_status' => 'publish',
1113+
]
1114+
);
1115+
},
1116+
10,
1117+
2
1118+
);
1119+
"""
1120+
1121+
When I run `wp post create --post_title='Trigger' --post_status=publish --post_modified='2020-01-01 12:00:00' --porcelain`
1122+
Then STDOUT should be a number
1123+
And save STDOUT as {POST_ID}
1124+
1125+
When I run `wp post update {POST_ID} --post_modified='2021-02-02 13:00:00'`
1126+
Then STDOUT should be:
1127+
"""
1128+
Success: Updated post {POST_ID}.
1129+
"""
1130+
1131+
When I run `wp post get {POST_ID} --field=post_modified`
1132+
Then STDOUT should be:
1133+
"""
1134+
2021-02-02 13:00:00
1135+
"""
1136+
1137+
# The hook ran once for the create and once for the update, and neither of
1138+
# the posts it inserted should carry the date the command asked for.
1139+
When I run `wp post list --title='Inserted by hook' --format=count`
1140+
Then STDOUT should be:
1141+
"""
1142+
2
1143+
"""
1144+
1145+
When I run `wp post list --title='Inserted by hook' --field=post_modified`
1146+
Then STDOUT should not contain:
1147+
"""
1148+
2020-01-01
1149+
"""
1150+
And STDOUT should not contain:
1151+
"""
1152+
2021-02-02
1153+
"""
1154+
And STDOUT should not be empty

‎src/Post_Command.php‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,12 @@ private static function add_post_modified_filter( $params ) {
319319
'post_modified_gmt' => $gmt,
320320
];
321321

322-
$callback = static function ( $data ) use ( $modified ) {
322+
$callback = static function ( $data ) use ( $modified, &$callback ) {
323+
// The first post to reach this filter is the command's own. A save hook
324+
// can insert further posts before the outer call returns, and those must
325+
// keep their own dates, so the filter unhooks itself right away.
326+
self::remove_post_modified_filter( $callback );
327+
323328
return array_merge( $data, $modified );
324329
};
325330

@@ -333,6 +338,9 @@ private static function add_post_modified_filter( $params ) {
333338
/**
334339
* Removes the callback registered by add_post_modified_filter().
335340
*
341+
* The callback removes itself once it has run; this is the fallback for when
342+
* it never ran, for instance because wp_insert_post() bailed out early.
343+
*
336344
* @param callable $callback The callback to remove.
337345
* @return void
338346
*/

0 commit comments

Comments
 (0)