Move all HPOS CLI tools to the "hpos" namespace (#46766)

* Deprecate CLI tools in the “cot” namespace

* PHPCS fixes

* Add changelog

* Restore a few args for backwards compatibility
This commit is contained in:
Jorge A. Torres 2024-04-22 14:24:40 +01:00 committed by GitHub
parent 3ac3a497ba
commit 05fed19271
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 40 additions and 27 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: update
Deprecate CLI tools under "cot" namespace and add aliases in "hpos" namespace.

View File

@ -58,16 +58,26 @@ class CLIRunner {
* Registers commands for CLI.
*/
public function register_commands() {
WP_CLI::add_command( 'wc cot count_unmigrated', array( $this, 'count_unmigrated' ) );
WP_CLI::add_command( 'wc cot migrate', array( $this, 'migrate' ) );
WP_CLI::add_command( 'wc cot sync', array( $this, 'sync' ) );
WP_CLI::add_command( 'wc cot verify_cot_data', array( $this, 'verify_cot_data' ) );
WP_CLI::add_command( 'wc cot enable', array( $this, 'enable' ) );
WP_CLI::add_command( 'wc cot disable', array( $this, 'disable' ) );
$legacy_commands = array( 'count_unmigrated', 'sync', 'verify_cot_data', 'enable', 'disable' );
foreach ( $legacy_commands as $cmd ) {
$new_cmd_name = 'verify_cot_data' === $cmd ? 'verify_data' : $cmd;
WP_CLI::add_command( "wc hpos {$new_cmd_name}", array( $this, $cmd ) );
WP_CLI::add_command(
"wc cot {$cmd}",
function ( array $args = array(), array $assoc_args = array() ) use ( $cmd, $new_cmd_name ) {
WP_CLI::warning( "Command `wc cot {$cmd}` is deprecated since 8.9.0. Please use `wc hpos {$new_cmd_name}` instead." );
return call_user_func( array( $this, $cmd ), $args, $assoc_args );
}
);
}
WP_CLI::add_command( 'wc hpos cleanup', array( $this, 'cleanup_post_data' ) );
WP_CLI::add_command( 'wc hpos status', array( $this, 'status' ) );
WP_CLI::add_command( 'wc hpos diff', array( $this, 'diff' ) );
WP_CLI::add_command( 'wc hpos backfill', array( $this, 'backfill' ) );
WP_CLI::add_command( 'wc cot migrate', array( $this, 'migrate' ) ); // Fully deprecated. No longer works.
}
/**
@ -77,7 +87,7 @@ class CLIRunner {
*
* @return bool Whether the COT feature is enabled.
*/
private function is_enabled( $log = true ) : bool {
private function is_enabled( $log = true ): bool {
if ( ! $this->controller->custom_orders_table_usage_is_enabled() ) {
if ( $log ) {
WP_CLI::log(
@ -98,7 +108,7 @@ class CLIRunner {
*
* ## EXAMPLES
*
* wp wc cot count_unmigrated
* wp wc hpos count_unmigrated
*
* @param array $args Positional arguments passed to the command.
*
@ -106,7 +116,7 @@ class CLIRunner {
*
* @return int The number of orders to be migrated.*
*/
public function count_unmigrated( $args = array(), $assoc_args = array() ) : int {
public function count_unmigrated( $args = array(), $assoc_args = array() ): int {
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
$order_count = $this->synchronizer->get_current_orders_pending_sync_count();
@ -147,7 +157,7 @@ class CLIRunner {
*
* ## EXAMPLES
*
* wp wc cot sync --batch-size=500
* wp wc hpos sync --batch-size=500
*
* @param array $args Positional arguments passed to the command.
* @param array $assoc_args Associative arguments (options) passed to the command.
@ -212,7 +222,7 @@ class CLIRunner {
)
);
$batch_count ++;
++$batch_count;
$total_time += $batch_total_time;
$progress->tick();
@ -252,7 +262,7 @@ class CLIRunner {
}
/**
* [Deprecated] Use `wp wc cot sync` instead.
* [Deprecated] Use `wp wc hpos sync` instead.
* Copy order data into the postmeta table.
*
* Note that this could dramatically increase the size of your postmeta table, but is recommended
@ -274,7 +284,7 @@ class CLIRunner {
* @param array $args Positional arguments passed to the command.
* @param array $assoc_args Associative arguments (options) passed to the command.
*/
public function migrate( $args = array(), $assoc_args = array() ) {
public function migrate( array $args = array(), array $assoc_args = array() ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed -- for backwards compat.
WP_CLI::log( __( 'Migrate command is deprecated. Please use `sync` instead.', 'woocommerce' ) );
}
@ -319,7 +329,7 @@ class CLIRunner {
* ## EXAMPLES
*
* # Verify migrated order data, 500 orders at a time.
* wp wc cot verify_cot_data --batch-size=500 --start-from=0 --end-at=10000
* wp wc hpos verify_cot_data --batch-size=500 --start-from=0 --end-at=10000
*
* @param array $args Positional arguments passed to the command.
* @param array $assoc_args Associative arguments (options) passed to the command.
@ -414,7 +424,7 @@ class CLIRunner {
$error_processing = $error_processing || ! empty( $failed_ids_in_current_batch );
$processed += count( $order_ids );
$batch_total_time = microtime( true ) - $batch_start_time;
$batch_count ++;
++$batch_count;
$total_time += $batch_total_time;
if ( count( $failed_ids_in_current_batch ) > 0 ) {
@ -467,7 +477,7 @@ class CLIRunner {
} else {
array_walk(
$errors_in_remigrate_batch,
function( &$errors_for_order ) {
function ( &$errors_for_order ) {
$errors_for_order[] = array( 'remigrate_failed' => true );
}
);
@ -559,7 +569,7 @@ class CLIRunner {
*
* @return int Order count.
*/
private function get_verify_order_count( int $order_id_start, int $order_id_end, array $order_types, bool $log = true ) : int {
private function get_verify_order_count( int $order_id_start, int $order_id_end, array $order_types, bool $log = true ): int {
global $wpdb;
$order_types_placeholder = implode( ',', array_fill( 0, count( $order_types ), '%s' ) );
@ -605,7 +615,7 @@ class CLIRunner {
*
* @return array Failed IDs with meta details.
*/
private function verify_meta_data( array $order_ids, array $failed_ids ) : array {
private function verify_meta_data( array $order_ids, array $failed_ids ): array {
$meta_keys_to_ignore = $this->synchronizer->get_ignored_order_props();
global $wpdb;
@ -685,7 +695,7 @@ ORDER BY $meta_table.order_id ASC, $meta_table.meta_key ASC;
*
* @return array Normalized data.
*/
private function normalize_raw_meta_data( array $data ) : array {
private function normalize_raw_meta_data( array $data ): array {
$clubbed_data = array();
foreach ( $data as $row ) {
if ( ! isset( $clubbed_data[ $row['entity_id'] ] ) ) {
@ -719,7 +729,7 @@ ORDER BY $meta_table.order_id ASC, $meta_table.meta_key ASC;
* ### EXAMPLES
*
* # Enable HPOS on new shops.
* wp wc cot enable --for-new-shop
* wp wc hpos enable --for-new-shop
*
* @param array $args Positional arguments passed to the command.
* @param array $assoc_args Associative arguments (options) passed to the command.
@ -778,7 +788,7 @@ ORDER BY $meta_table.order_id ASC, $meta_table.meta_key ASC;
sprintf(
// translators: %s is the command to run (wp wc cot sync).
__( '[Failed] There are orders pending sync. Please run `%s` to sync pending orders.', 'woocommerce' ),
'wp wc cot sync',
'wp wc hpos sync',
)
);
$enable_hpos = false;
@ -826,7 +836,7 @@ ORDER BY $meta_table.order_id ASC, $meta_table.meta_key ASC;
* ### EXAMPLES
*
* # Disable HPOS.
* wp wc cot disable
* wp wc hpos disable
*
* @param array $args Positional arguments passed to the command.
* @param array $assoc_args Associative arguments (options) passed to the command.
@ -849,7 +859,7 @@ ORDER BY $meta_table.order_id ASC, $meta_table.meta_key ASC;
sprintf(
// translators: %s is the command to run (wp wc cot sync).
__( '[Failed] There are orders pending sync. Please run `%s` to sync pending orders.', 'woocommerce' ),
'wp wc cot sync',
'wp wc hpos sync',
)
);
}
@ -958,7 +968,7 @@ ORDER BY $meta_table.order_id ASC, $meta_table.meta_key ASC;
foreach ( $order_ids as $order_id ) {
try {
$handler->cleanup_post_data( $order_id, $force );
$count++;
++$count;
// translators: %d is an order ID.
WP_CLI::debug( sprintf( __( 'Cleanup completed for order %d.', 'woocommerce' ), $order_id ) );
@ -1012,7 +1022,7 @@ ORDER BY $meta_table.order_id ASC, $meta_table.meta_key ASC;
* @param array $args Positional arguments passed to the command.
* @param array $assoc_args Associative arguments (options) passed to the command.
*/
public function status( array $args = array(), array $assoc_args = array() ) {
public function status( array $args = array(), array $assoc_args = array() ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed -- for backwards compat.
$legacy_handler = wc_get_container()->get( LegacyDataHandler::class );
// translators: %s is either 'yes' or 'no'.
@ -1084,7 +1094,7 @@ ORDER BY $meta_table.order_id ASC, $meta_table.meta_key ASC;
// Format the diff array.
$diff = array_map(
function( $key, $hpos_value, $cpt_value ) {
function ( $key, $hpos_value, $cpt_value ) {
// Format for dates.
$hpos_value = is_a( $hpos_value, \WC_DateTime::class ) ? $hpos_value->format( DATE_ATOM ) : $hpos_value;
$cpt_value = is_a( $cpt_value, \WC_DateTime::class ) ? $cpt_value->format( DATE_ATOM ) : $cpt_value;
@ -1203,5 +1213,4 @@ ORDER BY $meta_table.order_id ASC, $meta_table.meta_key ASC;
)
);
}
}