CLIRunner
in package
CLI tool for migrating order data to/from custom table.
Credits https://github.com/liquidweb/woocommerce-custom-orders-table/blob/develop/includes/class-woocommerce-custom-orders-table-cli.php.
Class CLIRunner
Table of Contents
- $controller : CustomOrdersTableController
- CustomOrdersTableController instance.
- $post_to_cot_migrator : PostsToOrdersMigrationController
- PostsToOrdersMigrationController instance.
- $synchronizer : DataSynchronizer
- DataSynchronizer instance.
- backfill() : mixed
- Backfills an order from either the HPOS or the posts datastore.
- cleanup_post_data() : void
- When HPOS is enabled, this command lets you remove redundant data from the postmeta table for migrated orders.
- count_unmigrated() : int
- Count how many orders have yet to be migrated into the custom orders table.
- diff() : mixed
- Displays differences for an order between the HPOS and post datastore.
- disable() : mixed
- Disables custom order tables (HPOS) and posts to authoritative if HPOS and post tables are in sync.
- disable_compat_mode() : void
- Disables compatibility mode, which keeps the HPOS and posts datastore in sync.
- enable() : void
- Set custom order tables (HPOS) to authoritative if: 1). HPOS and posts tables are in sync, or, 2). This is a new shop (in this case also create tables). Additionally, all installed WC plugins should be compatible.
- enable_compat_mode() : void
- Enables compatibility mode, which keeps the HPOS and posts datastore in sync.
- migrate() : mixed
- [Deprecated] Use `wp wc hpos sync` instead.
- register_commands() : mixed
- Registers commands for CLI.
- status() : mixed
- Displays a summary of HPOS situation on this site.
- sync() : mixed
- Sync order data between the custom order tables and the core WordPress post tables.
- verify_cot_data() : mixed
- Verify migrated order data with original posts data.
- free_in_memory_usage() : mixed
- Free some in-memory usage.
- get_verify_order_count() : int
- Helper method to get count for orders needing verification.
- is_enabled() : bool
- Check if the COT feature is enabled.
- normalize_raw_meta_data() : array<string|int, mixed>
- Helper method to normalize response from meta queries into order_id > meta_key > meta_values.
- toggle_compat_mode() : void
- Toggles compatibility mode on or off.
- verify_meta_data() : array<string|int, mixed>
- Verify meta data as part of verifying the order object.
Properties
$controller
CustomOrdersTableController instance.
private
CustomOrdersTableController
$controller
$post_to_cot_migrator
PostsToOrdersMigrationController instance.
private
PostsToOrdersMigrationController
$post_to_cot_migrator
$synchronizer
DataSynchronizer instance.
private
DataSynchronizer
$synchronizer
Methods
backfill()
Backfills an order from either the HPOS or the posts datastore.
public
backfill([array<string|int, mixed> $args = array() ][, array<string|int, mixed> $assoc_args = array() ]) : mixed
OPTIONS
<order_id> : The ID of the order.
--from=
: Source datastore. Either 'hpos' or 'posts'.
options:
- hpos
- posts
--to=
: Destination datastore. Either 'hpos' or 'posts'.
options:
- hpos
- posts
[--meta_keys=<meta_keys>] : Comma separated list of meta keys to backfill.
[--props=
Parameters
- $args : array<string|int, mixed> = array()
-
Positional arguments passed to the command.
- $assoc_args : array<string|int, mixed> = array()
-
Associative arguments (options) passed to the command.
Tags
Return values
mixed —cleanup_post_data()
When HPOS is enabled, this command lets you remove redundant data from the postmeta table for migrated orders.
public
cleanup_post_data([array<string|int, mixed> $args = array() ][, array<string|int, mixed> $assoc_args = array() ]) : void
OPTIONS
<all|id|range>... : ID or range of orders to clean up.
[--batch-size=]
: Number of orders to process per batch. Applies only to cleaning up of 'all' orders.
default: 500
[--force] : When true, post meta will be cleaned up even if the post appears to have been updated more recently than the order.
default: false
EXAMPLES
Cleanup post data for order 314.
$ wp wc hpos cleanup 314
Cleanup postmeta for orders with IDs betweeen 10 and 100 and order 314.
$ wp wc hpos cleanup 10-100 314
Cleanup postmeta for all orders.
wp wc hpos cleanup all
Cleanup postmeta for all orders with a batch size of 200 (instead of the default 500).
wp wc hpos cleanup all --batch-size=200
Parameters
- $args : array<string|int, mixed> = array()
-
Positional arguments passed to the command.
- $assoc_args : array<string|int, mixed> = array()
-
Associative arguments (options) passed to the command.
Return values
void —count_unmigrated()
Count how many orders have yet to be migrated into the custom orders table.
public
count_unmigrated([array<string|int, mixed> $args = array() ][, array<string|int, mixed> $assoc_args = array() ]) : int
EXAMPLES
wp wc hpos count_unmigrated
Parameters
- $args : array<string|int, mixed> = array()
-
Positional arguments passed to the command.
- $assoc_args : array<string|int, mixed> = array()
-
Associative arguments (options) passed to the command.
Return values
int — The number of orders to be migrated.*diff()
Displays differences for an order between the HPOS and post datastore.
public
diff([array<string|int, mixed> $args = array() ][, array<string|int, mixed> $assoc_args = array() ]) : mixed
OPTIONS
<order_id> :The ID of the order.
[--format=]
: Render output in a particular format.
default: table options:
- table
- csv
- json
- yaml
EXAMPLES
Find differences between datastores for order 123.
$ wp wc hpos diff 123
Find differences for order 123 and display as CSV.
$ wp wc hpos diff 123 --format=csv
Parameters
- $args : array<string|int, mixed> = array()
-
Positional arguments passed to the command.
- $assoc_args : array<string|int, mixed> = array()
-
Associative arguments (options) passed to the command.
Tags
Return values
mixed —disable()
Disables custom order tables (HPOS) and posts to authoritative if HPOS and post tables are in sync.
public
disable(array<string|int, mixed> $args, array<string|int, mixed> $assoc_args) : mixed
OPTIONS
[--with-sync] : Also disables sync (if it's currently enabled).
default: false
EXAMPLES
Disable HPOS.
wp wc hpos disable
Parameters
- $args : array<string|int, mixed>
-
Positional arguments passed to the command.
- $assoc_args : array<string|int, mixed>
-
Associative arguments (options) passed to the command.
Return values
mixed —disable_compat_mode()
Disables compatibility mode, which keeps the HPOS and posts datastore in sync.
public
disable_compat_mode() : void
Tags
Return values
void —enable()
Set custom order tables (HPOS) to authoritative if: 1). HPOS and posts tables are in sync, or, 2). This is a new shop (in this case also create tables). Additionally, all installed WC plugins should be compatible.
public
enable([array<string|int, mixed> $args = array() ][, array<string|int, mixed> $assoc_args = array() ]) : void
OPTIONS
[--for-new-shop] : Enable only if this is a new shop, irrespective of whether tables are in sync.
default: false
[--with-sync] : Also enables sync (if it's currently not enabled).
default: false
EXAMPLES
# Enable HPOS on new shops.
wp wc hpos enable --for-new-shop
Parameters
- $args : array<string|int, mixed> = array()
-
Positional arguments passed to the command.
- $assoc_args : array<string|int, mixed> = array()
-
Associative arguments (options) passed to the command.
Return values
void —enable_compat_mode()
Enables compatibility mode, which keeps the HPOS and posts datastore in sync.
public
enable_compat_mode() : void
Tags
Return values
void —migrate()
[Deprecated] Use `wp wc hpos sync` instead.
public
migrate([array<string|int, mixed> $args = array() ][, array<string|int, mixed> $assoc_args = array() ]) : mixed
Copy order data into the postmeta table.
Note that this could dramatically increase the size of your postmeta table, but is recommended if you wish to stop using the custom orders table plugin.
OPTIONS
[--batch-size=]
: The number of orders to process in each batch. Passing a value of 0 will disable batching.
default: 500
EXAMPLES
# Copy all order data into the post meta table, 500 posts at a time.
wp wc cot migrate --batch-size=500
Parameters
- $args : array<string|int, mixed> = array()
-
Positional arguments passed to the command.
- $assoc_args : array<string|int, mixed> = array()
-
Associative arguments (options) passed to the command.
Return values
mixed —register_commands()
Registers commands for CLI.
public
register_commands() : mixed
Return values
mixed —status()
Displays a summary of HPOS situation on this site.
public
status([array<string|int, mixed> $args = array() ][, array<string|int, mixed> $assoc_args = array() ]) : mixed
Parameters
- $args : array<string|int, mixed> = array()
-
Positional arguments passed to the command.
- $assoc_args : array<string|int, mixed> = array()
-
Associative arguments (options) passed to the command.
Tags
Return values
mixed —sync()
Sync order data between the custom order tables and the core WordPress post tables.
public
sync([array<string|int, mixed> $args = array() ][, array<string|int, mixed> $assoc_args = array() ]) : mixed
OPTIONS
[--batch-size=]
: The number of orders to process in each batch.
default: 500
EXAMPLES
wp wc hpos sync --batch-size=500
Parameters
- $args : array<string|int, mixed> = array()
-
Positional arguments passed to the command.
- $assoc_args : array<string|int, mixed> = array()
-
Associative arguments (options) passed to the command.
Return values
mixed —verify_cot_data()
Verify migrated order data with original posts data.
public
verify_cot_data([array<string|int, mixed> $args = array() ][, array<string|int, mixed> $assoc_args = array() ]) : mixed
OPTIONS
[--batch-size=]
: The number of orders to verify in each batch.
default: 500
[--start-from=<order_id>] : Order ID to start from.
default: 0
[--end-at=<order_id>] : Order ID to end at.
default: -1
[--verbose] : Whether to output errors as they happen in batch, or output them all together at the end.
default: false
[--order-types] : Comma seperated list of order types that needs to be verified. For example, --order-types=shop_order,shop_order_refund
default: Output of function wc_get_order_types( 'cot-migration' )
[--re-migrate] : Attempt to re-migrate orders that failed verification. You should only use this option when you have never run the site with HPOS as authoritative source of order data yet, or you have manually checked the reported errors, otherwise, you risk stale data overwriting the more recent data. default: false
EXAMPLES
# Verify migrated order data, 500 orders at a time.
wp wc hpos verify_cot_data --batch-size=500 --start-from=0 --end-at=10000
Parameters
- $args : array<string|int, mixed> = array()
-
Positional arguments passed to the command.
- $assoc_args : array<string|int, mixed> = array()
-
Associative arguments (options) passed to the command.
Return values
mixed —free_in_memory_usage()
Free some in-memory usage.
private
free_in_memory_usage() : mixed
Return values
mixed —get_verify_order_count()
Helper method to get count for orders needing verification.
private
get_verify_order_count(int $order_id_start, int $order_id_end, array<string|int, mixed> $order_types[, bool $log = true ]) : int
Parameters
- $order_id_start : int
-
Order ID to start from.
- $order_id_end : int
-
Order ID to end at.
- $order_types : array<string|int, mixed>
-
List of order types to verify.
- $log : bool = true
-
Whether to also log an error message.
Return values
int — Order count.is_enabled()
Check if the COT feature is enabled.
private
is_enabled([bool $log = true ]) : bool
Parameters
- $log : bool = true
-
Optionally log a error message.
Return values
bool — Whether the COT feature is enabled.normalize_raw_meta_data()
Helper method to normalize response from meta queries into order_id > meta_key > meta_values.
private
normalize_raw_meta_data(array<string|int, mixed> $data) : array<string|int, mixed>
Parameters
- $data : array<string|int, mixed>
-
Data fetched from meta queries.
Return values
array<string|int, mixed> — Normalized data.toggle_compat_mode()
Toggles compatibility mode on or off.
private
toggle_compat_mode(bool $enabled) : void
Parameters
- $enabled : bool
-
TRUE to enable compatibility mode, FALSE to disable.
Tags
Return values
void —verify_meta_data()
Verify meta data as part of verifying the order object.
private
verify_meta_data(array<string|int, mixed> $order_ids, array<string|int, mixed> $failed_ids) : array<string|int, mixed>
Parameters
- $order_ids : array<string|int, mixed>
-
Order IDs.
- $failed_ids : array<string|int, mixed>
-
Array for storing failed IDs.