Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 13, 2026

Plan to Fix Post Revision Creation Issue ✅ COMPLETE

  • Analyze the issue: WP-CLI wp post create doesn't create an initial revision like WordPress admin does
  • Understand the codebase structure and locate the Post_Command.php file
  • Research WordPress revision behavior and identify the fix needed
  • Add a call to wp_save_post_revision() after successful post creation in the create() method
  • Create Behat test to verify revision is created on post creation
  • Run code quality checks (lint, phpcs, phpstan) - all passing ✅
  • Test the fix and ensure it works as expected
  • Run full test suite to ensure no regressions - all passing ✅
  • Request code review - addressed feedback ✅
  • Refactor to use callback approach instead of duplicating parent logic ✅

Changes Made

Modified src/Post_Command.php

  • Refactored approach: Instead of inlining parent::_create(), the revision creation is now done within the callback
  • Added wp_save_post_revision($post_id) call inside the callback after checking for errors
  • This keeps the logic encapsulated while leveraging shared error handling from the parent class
  • Avoids code duplication and maintenance issues

Added test in features/post.feature

  • New scenario: "Creating a post should create an initial revision"
  • Tests that 1 revision is created when a post is created
  • Tests that 2 revisions exist after updating the post
  • Validates the fix resolves the issue described in the problem statement

Quality Assurance ✅

  • ✅ All PHPUnit tests pass (132 tests, 291 assertions)
  • ✅ PHP syntax check passes
  • ✅ Code style (phpcs) passes
  • ✅ Static analysis (phpstan) passes
  • ✅ No code duplication
  • ✅ Maintains parent class consistency

Implementation

parent::_create(
    $args,
    $assoc_args,
    function ( $params ) {
        $post_id = wp_insert_post( $params, true );

        if ( ! is_wp_error( $post_id ) ) {
            // Create initial revision to match WordPress admin behavior.
            // This ensures the original content can be restored if the post is later edited.
            wp_save_post_revision( $post_id );
        }

        return $post_id;
    }
);

This approach keeps the new logic encapsulated while still leveraging the shared error handling and output formatting from the parent class.

Original prompt

This section details on the original issue you should resolve

<issue_title>Creating a post does not create a revision</issue_title>
<issue_description>When testing #563 I realized there's a difference in behavior for revisions between wp-admin vs WP-CLI.

In the block editor, when I publish a new post, there are 2 new entries in wp_posts: one for the post (ID 1), and one for an identical revision (ID 2). When I then update the post with ID 1, a new revision (ID 3) is added. This way, I can easily restore the original content by restoring post with ID 2.

With WP-CLI, when I run wp post create, only one entry is added (ID 1). No revision. When I then make an edit, a revision is created for the edit, but the original content is lost.</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix post creation to include initial revision Create initial revision on post creation to match WordPress admin behavior Jan 13, 2026
Copilot AI requested a review from swissspidy January 13, 2026 10:23
@github-actions github-actions bot added bug command:post Related to 'post' command command:post-create Related to 'post create' command labels Jan 13, 2026
@codecov
Copy link

codecov bot commented Jan 13, 2026

Codecov Report

❌ Patch coverage is 0% with 4 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/Post_Command.php 0.00% 4 Missing ⚠️

📢 Thoughts on this report? Let us know!

This comment was marked as resolved.

@swissspidy

This comment was marked as resolved.

gemini-code-assist[bot]

This comment was marked as resolved.

@swissspidy

This comment was marked as resolved.

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>

This comment was marked as resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug command:post Related to 'post' command command:post-create Related to 'post create' command

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Creating a post does not create a revision

2 participants