Skip to content

Feature: Add wp cbt save CLI command #830

Open

Description

@bph

Part of #828 (which fulfills #688). Depends on the CBT_Theme_Save service refactor tracked in #828.

Goal

Expose the CBT_Theme_Save service as a WP-CLI subcommand so developers can persist Editor changes to disk from the terminal.

Scope

New file: includes/cli/class-cbt-cli-command.php

New class: CBT_CLI_Command extending WP_CLI_Command, with a save method.

Registration (in includes/class-create-block-theme.php):

if ( defined( 'WP_CLI' ) && WP_CLI ) {
    require_once plugin_dir_path( __DIR__ ) . 'includes/cli/class-cbt-cli-command.php';
    WP_CLI::add_command( 'cbt', 'CBT_CLI_Command' );
}

CLI surface

wp cbt save [--fonts] [--templates] [--styles] [--patterns]
            [--all] [--only-modified-templates] [--dry-run]
  • Flags are kebab-case publicly; the command translates internally to the saveFonts / saveTemplates / etc. keys the service expects
  • --all is a convenience for "save everything"
  • --only-modified-templates maps to processOnlySavedTemplates
  • --dry-run reports what would change without writing

Required behavior

  • Guard with wp_is_block_theme() — WP_CLI::error() if not, mirroring the plugin UI gate documented in AGENTS.md
  • WP_CLI::error() if no save flag is passed (no silent no-op success)
  • All user-facing strings i18n'd via __( ..., 'create-block-theme' )
  • PHPDoc with shortdesc, full ## OPTIONS, and at least two ## EXAMPLES
  • Translate WP_Error from the service into WP_CLI::error()

Tests

PHPUnit tests against the command class (the service itself is covered in its own issue):

  • Flag normalization (kebab-case → service options)
  • Guard fires on non-block themes
  • Empty invocation produces an error

Out of scope

Acceptance

  • Manual smoke test: install plugin in a Studio site, run each flag combination against a block theme
  • wp help cbt save displays the documented options and examples
  • lint:php, test:php, test:unit all green

Activity

  1. added
    enhancementNew feature or request
    wp-clihandling wp-cli commands in this plugin
    on Apr 30, 2026
  2. KarunyaChavan commented on Jul 7, 2026

    @KarunyaChavan

    Hi @bph , I saw the implementation plan in issue #828 and noticed that the first phase (the CBT_Theme_Save extraction) has already been implemented in PR-833
    I'd be interested in contributing to the remaining phases (#830, #831, and #832), if that would be helpful. Before I get started, I wanted to check with you whether you'd prefer that I build on top of the work in PR #833 in my own branch, or if you'd rather I work independently from trunk.
    I just wanted to check the preferred approach first so I can avoid duplicating work and follow the direction you have in mind

  3. bph commented on Jul 7, 2026

    @bph
    ContributorAuthor

    Hi @KarunyaChavan Thanks for commenting. This repo also has an issue + PR policy so, so submit a new branch and when #833 is merged, the other subsequent PRs can be built upon.

  4. bph commented on Jul 7, 2026

    @bph
    ContributorAuthor

    Design note — multisite capability gate

    Trunk PR #850 (landed after #829 / PR #833) tightened the /save REST route's permission_callback from an inline current_user_can( 'edit_theme_options' ) closure to CBT_Theme_API::can_modify_theme(), which additionally requires super-admin on multisite.

    The CBT_Theme_Save service does not enforce capability checks itself (by design — see its class docblock); callers are responsible. That means the wp cbt save command needs to replicate the gate:

    if ( ! CBT_Theme_API::can_modify_theme() ) {
        WP_CLI::error(
            __( 'Current user is not permitted to modify theme files.', 'create-block-theme' )
        );
    }

    Without this, a non-super-admin user on a multisite install with WP-CLI shell access could persist theme changes via wp cbt save even though the equivalent REST call would be rejected.

    CBT_Theme_API::can_modify_theme() is already public static, so the CLI command can call it directly — no visibility change needed.

    The Required behavior section of this issue should be updated to add capability enforcement alongside the wp_is_block_theme() guard, and the test list should include a case for the capability check firing when the current user lacks the cap.

  5. jasalt commented on Jul 24, 2026

    @jasalt

    Made a temporary wrapper plugin for this purpose as I needed this feature for git workflow https://github.com/jasalt/wpcli-create-block-theme.

    Current version has extra workarounds with Block Pattern saving issues that have came up, plain/simple version in history: jasalt/wpcli-create-block-theme@f441dac

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestwp-clihandling wp-cli commands in this plugin

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions