Skip to content

Refactor WP-CLI commands to modern standards #789

@GaryJones

Description

@GaryJones

Summary

The current WP-CLI implementation in classes/class-wpcom-liveblog-wp-cli.php uses legacy patterns and needs modernisation to align with our plugin standards.

Current State

The class contains two commands:

  1. readme_for_github - Converts readme.txt to markdown for GitHub README. This is obsolete since we now maintain README.md directly.

  2. fix-archive - Repairs wp_commentmeta for archived liveblogs where edited entries have incorrect replaces meta values. This appears to be a legitimate repair tool for edge cases.

Issues

  • Old-style multi-method class extending WP_CLI_Command
  • Business logic mixed with CLI output concerns
  • No dependency injection
  • No tests (behat or unit)
  • readme_for_github is obsolete

Proposed Changes

1. Delete readme_for_github command

This command is no longer needed. We maintain README.md directly.

2. Evaluate fix-archive necessity

Before refactoring, determine if this command is still needed:

  • When was it last used?
  • Is the underlying bug that causes corrupted data still present?
  • Could the root cause be fixed instead?

3. If fix-archive is still needed, refactor to modern standards

Create command class classes/CLI/Fix_Archive_Command.php:

class Fix_Archive_Command {
    private Archive_Repair_Service $service;

    public function __construct( Archive_Repair_Service $service ) {
        $this->service = $service;
    }

    public function __invoke( array $args, array $assoc_args ): void {
        $dry_run = isset( $assoc_args['dry-run'] );
        // Delegate to service, handle CLI output
    }
}

Create service class classes/Services/Archive_Repair_Service.php:

class Archive_Repair_Service {
    public function get_liveblogs(): array { }
    public function get_edited_entries( int $post_id ): array { }
    public function repair_entry( int $entry_id, int $correct_id ): void { }
}

Register command in main plugin file:

if ( defined( 'WP_CLI' ) && WP_CLI ) {
    WP_CLI::add_command( 'liveblog fix-archive', new Fix_Archive_Command( new Archive_Repair_Service() ) );
}

4. Add behat tests

Create features/cli-fix-archive.feature to test:

  • Dry run mode
  • Actual repair execution
  • Edge cases (no liveblogs, no edits, already correct)

Acceptance Criteria

  • readme_for_github command removed
  • Decision documented on whether fix-archive is still needed
  • If needed: fix-archive refactored to one-class-per-command pattern
  • If needed: Business logic extracted to service class
  • If needed: Behat tests added for CLI command
  • Old class-wpcom-liveblog-wp-cli.php file deleted

References

  • Current implementation: classes/class-wpcom-liveblog-wp-cli.php

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions