HEX
Server: nginx
System: Linux pool195-106-36.bur.atomicsites.net 6.12.57+deb12-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.12.57-1~bpo12+1 (2025-11-17) x86_64
User: (0)
PHP: 8.3.32
Disabled: pcntl_fork
Upload Files
File: /wordpress/plugins/wp-cloud-client/1.1.5/src/Cli/BackfillCommand.php
<?php

declare(strict_types=1);

namespace VPlugins\WPCloudClient\Cli;

use VPlugins\WPCloudClient\Settings\SettingsRepository;
use VPlugins\WPCloudClient\Support\Logger;

/**
 * WP-CLI commands for wp-cloud-client backfill operations.
 *
 * Registered as: wp wpcc backfill-orders
 *
 * Methods follow WP-CLI naming conventions (snake_case → kebab-case subcommand).
 */
final class BackfillCommand {

	/**
	 * Class constructor.
	 *
	 * @param SettingsRepository $settings Plugin settings.
	 * @param Logger             $logger   Logger service.
	 */
	public function __construct(
		private readonly SettingsRepository $settings,
		private readonly Logger $logger
	) {}

	/**
	 * Trigger a WooCommerce order backfill on wp-cloud-manager.
	 *
	 * Sends a POST to /wordpress/v1/backfill-wc-orders/ which kicks off the
	 * catch-up worker on the manager side to re-sync orders after migration
	 * cutover. The manager processes the backfill asynchronously via Machinery.
	 *
	 * ## EXAMPLES
	 *
	 *   wp wpcc backfill-orders
	 *
	 * @subcommand backfill-orders
	 *
	 * @param array<int, string>    $args      Positional arguments (unused).
	 * @param array<string, string> $assocArgs Named arguments (unused).
	 * @return void
	 */
	public function backfill_orders( array $args, array $assocArgs ): void { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
		if ( ! $this->settings->isConfigured() ) {
			\WP_CLI::error( 'wp-cloud-client is not configured — set site ID and API key first.' );
			return;
		}

		$managerUrl = $this->settings->getManagerUrl();
		if ( empty( $managerUrl ) ) {
			\WP_CLI::error( 'WPCC_MANAGER_URL is not set. Define the constant or hook wpcc_manager_url.' );
			return;
		}

		$url    = rtrim( $managerUrl, '/' ) . '/wordpress/v1/backfill-wc-orders/';
		$siteId = (string) $this->settings->getSiteId();
		$apiKey = (string) $this->settings->getApiKey();

		\WP_CLI::log( "Sending backfill request to {$url} …" );

		$response = wp_remote_post(
			$url,
			[
				'body'        => wp_json_encode( [] ),
				'data_format' => 'body',
				'headers'     => [
					'Content-Type'   => 'application/json',
					'X-WPCC-Site-ID' => $siteId,
					'X-WPCC-API-Key' => $apiKey,
				],
				'timeout'     => 15,
			]
		);

		if ( is_wp_error( $response ) ) {
			$message = $response->get_error_message();
			$this->logger->error( 'WC backfill-orders CLI transport error', $message );
			\WP_CLI::error( "Transport error: {$message}" );
			return;
		}

		$code = (int) wp_remote_retrieve_response_code( $response );
		$body = wp_remote_retrieve_body( $response );

		if ( 202 === $code ) {
			\WP_CLI::success( 'Backfill accepted — manager is processing orders asynchronously.' );
		} elseif ( 401 === $code ) {
			$this->logger->error( 'WC backfill-orders CLI unauthorized', $body );
			\WP_CLI::error( "Unauthorized (401). Check site ID, API key, and route strategy. Response: {$body}" );
		} else {
			$this->logger->error(
				'WC backfill-orders CLI unexpected response',
				[
					'status' => $code,
					'body'   => $body,
				]
			);
			\WP_CLI::error( "Unexpected response {$code}: {$body}" );
		}
	}
}