Repository navigation
Revisions: Save initial revision when inserting posts - #12272
Sukhendu2002 wants to merge 8 commits into
Conversation
|
Hi there! 👋 Thank you for your contribution to WordPress! 💖 It looks like this is your first pull request to No one monitors this repository for new pull requests. Pull requests must be attached to a Trac ticket to be considered for inclusion in WordPress Core. To attach a pull request to a Trac ticket, please include the ticket's full URL in your pull request description. Pull requests are never merged on GitHub. The WordPress codebase continues to be managed through the SVN repository that this GitHub repository mirrors. Please feel free to open pull requests to work on any contribution you are making. More information about how GitHub pull requests can be used to contribute to WordPress can be found in the Core Handbook. Please include automated tests. Including tests in your pull request is one way to help your patch be considered faster. To learn about WordPress' test suites, visit the Automated Testing page in the handbook. If you have not had a chance, please review the Contribute with Code page in the WordPress Core Handbook. The Developer Hub also documents the various coding standards that are followed:
Thank you, |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
|
Having worked with revisisions in late 2024, I am pretty sure that an initial revision is already created when a post is created. Did you verify the reported bug is still valid, before creating the fix? |
|
Yes, I re-verified against current origin/trunk. For the direct wp_insert_post() path covered by this ticket, trunk still creates 0 revisions on insert because wp_save_post_revision_on_insert() returns early when $update is false. I confirmed with a temporary PHPUnit test. The behavior you remember may be from editor flows that create an auto-draft and then update it; that update does create a revision. This patch targets the bare insert path where a post is inserted in one call. |
|
That does make sense, thank you for pointing that out. |
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
|
|
Closing #1336 in favor of this PR. @Sukhendu2002 I'll resolve the merge conflicts then give this a review. Thanks for your work here. |
Resolve the set_up() conflict in the REST revisions tests: trunk dropped the untyped `$revisions` property, so keep the initial-revision fixtures from this branch without it. The global post tests added on trunk expect the filter to see every revision, which now includes the initial one. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018vwYjUwKmjrEva3fyXVCbp
|
Claude suggested these two changes which I'll go ahead and push sicne they are small:
do we need this new parameter? I'll remove for now, let me know if I am missing something.
7.3.0 is right. |
The function is often hooked directly, and callbacks registered with more than one accepted arg would pass a post object or ID in as the new `$revision_author`, crediting revisions to the wrong user. Pick the author inside wp_save_post_revision() instead: with no current user and no existing revisions, the first revision is credited to the post author. This also covers a first revision saved on update without a current user, such as a cron edit of an imported post. Also corrects `@since` to 7.3.0, since trunk is 7.2 beta. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018vwYjUwKmjrEva3fyXVCbp
The revision meta tests added on trunk read the oldest revision, expecting the one saved by the first update. Posts now get a revision on insert, so that is the second oldest. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018vwYjUwKmjrEva3fyXVCbp
adamsilverstein
left a comment
There was a problem hiding this comment.
Thanks for picking this up @Sukhendu2002, this is a much more complete approach than my old #1336, which I've closed in favor of this one. Moving the fix into wp_save_post_revision_on_insert() so it covers every status is the right call, and it's great to see the custom_css workaround finally go.
I pushed a few commits to get this moving again, hope that's ok.
Claude worked through the review and the follow-up commits with me, summary below:
Trunk merge (318ffe4). The branch conflicted in the REST revisions tests
set_up(). Trunk dropped the untyped$revisionsproperty, so the initial-revision fixtures were kept without it. The two global post tests added on trunk since (test_get_items_restores_global_postandtest_get_items_without_global_post_leaves_it_unset) now expect the initial revision too.
wp_save_post_revision()signature (5746b52). The new$revision_authorparameter on a public function that is often hooked directly was risky: a callback registered with more than one accepted arg, eg.add_action( 'save_post', 'wp_save_post_revision', 10, 2 ), would pass aWP_Postin as the author, andabsint()turns that into user1. The public signature is back to( $post_id ), and the author is picked inside the function instead: with no current user and no existing revisions, the first revision goes to the post author. The private_wp_put_post_revision()keeps its new third param. One side effect worth noting: that also applies to a first revision saved on update without a current user (eg. a cron edit of an imported post), which used to get author0.
@sinceis now7.3.0, since trunk is7.2-beta4.New trunk tests (afc3413). The revision meta tests from #66329 read the oldest revision expecting the one saved by the first update, which is now the second oldest. The helper was renamed to
get_first_update_revision_id().The full PHPUnit suite passes locally with these changes.
Still open from the review, for discussion rather than blockers:
- Opting out. The only way to skip the initial revision is removing
wp_save_post_revisionfrompost_updated, which also turns off revisions on update. On the ticket, @peterwilsoncc and @TimothyBJacobs floated aregister_post_status()property; a simple filter inwp_save_post_revision_on_insert()might be enough. Worth adding?- Extra writes. Every programmatic insert of a type that supports revisions now writes a second row (importers,
wp_global_styles,wp_template,wp_navigation,wp_block,custom_css). Seems like the right trade-off, but it belongs in the dev note along with the revision count change for REST and XML-RPC consumers.- Nit: in
test_wp_save_post_revision_with_array_post_meta, theremove_action()/add_action()pair isn't wrapped intry/finallylike the other tests here.
What do you think about the opt-out question?
Posts now get a revision on insert, so the fixture post reports one more revision and the revisions collection includes the initial one. Taken from the CI diff so the generated IDs match. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018vwYjUwKmjrEva3fyXVCbp
Trac ticket: https://core.trac.wordpress.org/ticket/30854
What is this PR about?
This PR makes
wp_insert_post()save an initial revision when inserting a post whose post type supports revisions.Current trunk only saves revisions when an existing post is updated. A post inserted directly in its final state therefore has no revision containing its original content.
Backwards compatibility
Existing function calls remain compatible because the new revision-author parameters are optional.
This intentionally changes revision counts for programmatically inserted revision-supported posts. Such posts now begin with one revision instead of zero.
Use of AI Tools
AI assistance: Yes
Tool(s): pi coding agent
Model(s): GPT-5.6
Used for: Investigating the revision flow, initial code skeleton, code review. Final implementation and tests were reviewed and edited by me.
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.