Merged in feature/MAW-855-import-code-into-aws (pull request #2)

code import from pantheon

* code import from pantheon
This commit is contained in:
Tony Volpe
2023-12-04 23:08:14 +00:00
parent 8c9b1312bc
commit 8f4b5efda6
4766 changed files with 185592 additions and 239967 deletions

View File

@@ -5,6 +5,7 @@ namespace Automattic\WooCommerce\DataBase\Migrations\CustomOrderTable;
use Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController;
use Automattic\WooCommerce\Internal\DataStores\Orders\DataSynchronizer;
use Automattic\WooCommerce\Internal\DataStores\Orders\OrdersTableDataStore;
use Automattic\WooCommerce\Internal\Features\FeaturesController;
use WP_CLI;
/**
@@ -60,6 +61,8 @@ class CLIRunner {
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' ) );
}
/**
@@ -70,7 +73,7 @@ class CLIRunner {
* @return bool Whether the COT feature is enabled.
*/
private function is_enabled( $log = true ) : bool {
if ( ! $this->controller->is_feature_visible() ) {
if ( ! $this->controller->custom_orders_table_usage_is_enabled() ) {
if ( $log ) {
WP_CLI::log(
sprintf(
@@ -82,14 +85,7 @@ class CLIRunner {
}
}
return $this->controller->is_feature_visible();
}
/**
* Helper method to log warning that feature is not yet production ready.
*/
private function log_production_warning() {
WP_CLI::log( __( 'This feature is not production ready yet. Make sure you are not running these commands in your production environment.', 'woocommerce' ) );
return $this->controller->custom_orders_table_usage_is_enabled();
}
/**
@@ -106,10 +102,6 @@ class CLIRunner {
* @return int The number of orders to be migrated.*
*/
public function count_unmigrated( $args = array(), $assoc_args = array() ) : int {
if ( ! $this->is_enabled() ) {
return 0;
}
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
$order_count = $this->synchronizer->get_current_orders_pending_sync_count();
@@ -156,9 +148,14 @@ class CLIRunner {
* @param array $assoc_args Associative arguments (options) passed to the command.
*/
public function sync( $args = array(), $assoc_args = array() ) {
$this->log_production_warning();
if ( ! $this->is_enabled() ) {
return;
if ( ! $this->synchronizer->check_orders_table_exists() ) {
WP_CLI::warning( __( 'Custom order tables does not exist, creating...', 'woocommerce' ) );
$this->synchronizer->create_database_tables();
if ( $this->synchronizer->check_orders_table_exists() ) {
WP_CLI::success( __( 'Custom order tables were created successfully.', 'woocommerce' ) );
} else {
WP_CLI::error( __( 'Custom order tables could not be created.', 'woocommerce' ) );
}
}
$order_count = $this->count_unmigrated();
@@ -244,6 +241,7 @@ class CLIRunner {
}
/**
* [Deprecated] Use `wp wc cot sync` instead.
* Copy order data into the postmeta table.
*
* Note that this could dramatically increase the size of your postmeta table, but is recommended
@@ -266,7 +264,6 @@ class CLIRunner {
* @param array $assoc_args Associative arguments (options) passed to the command.
*/
public function migrate( $args = array(), $assoc_args = array() ) {
$this->log_production_warning();
WP_CLI::log( __( 'Migrate command is deprecated. Please use `sync` instead.', 'woocommerce' ) );
}
@@ -319,8 +316,9 @@ class CLIRunner {
*/
public function verify_cot_data( $args = array(), $assoc_args = array() ) {
global $wpdb;
$this->log_production_warning();
if ( ! $this->is_enabled() ) {
if ( ! $this->synchronizer->check_orders_table_exists() ) {
WP_CLI::error( __( 'Orders table does not exist.', 'woocommerce' ) );
return;
}
@@ -583,11 +581,19 @@ class CLIRunner {
* @return array Failed IDs with meta details.
*/
private function verify_meta_data( array $order_ids, array $failed_ids ) : array {
$meta_keys_to_ignore = array(
'_paid_date', // This is set by the CPT datastore but no longer used anywhere.
'_edit_lock',
);
global $wpdb;
if ( ! count( $order_ids ) ) {
return array();
}
$excluded_columns = $this->post_to_cot_migrator->get_migrated_meta_keys();
$excluded_columns = array_merge(
$this->post_to_cot_migrator->get_migrated_meta_keys(),
$meta_keys_to_ignore
);
$excluded_columns_placeholder = implode( ', ', array_fill( 0, count( $excluded_columns ), '%s' ) );
$order_ids_placeholder = implode( ', ', array_fill( 0, count( $order_ids ), '%d' ) );
$meta_table = OrdersTableDataStore::get_meta_table_name();
@@ -671,4 +677,188 @@ ORDER BY $meta_table.order_id ASC, $meta_table.meta_key ASC;
return $clubbed_data;
}
/**
* 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.
*
* ## 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 cot enable --for-new-shop
*
* @param array $args Positional arguments passed to the command.
* @param array $assoc_args Associative arguments (options) passed to the command.
*
* @return void
*/
public function enable( array $args = array(), array $assoc_args = array() ) {
$assoc_args = wp_parse_args(
$assoc_args,
array(
'for-new-shop' => false,
'with-sync' => false,
)
);
$enable_hpos = true;
WP_CLI::log( __( 'Running pre-enable checks...', 'woocommerce' ) );
$is_new_shop = \WC_Install::is_new_install();
if ( $assoc_args['for-new-shop'] && ! $is_new_shop ) {
WP_CLI::error( __( '[Failed] This is not a new shop, but --for-new-shop flag was passed.', 'woocommerce' ) );
}
/** Feature controller instance @var FeaturesController $feature_controller */
$feature_controller = wc_get_container()->get( FeaturesController::class );
$plugin_info = $feature_controller->get_compatible_plugins_for_feature( 'custom_order_tables', true );
if ( count( array_merge( $plugin_info['uncertain'], $plugin_info['incompatible'] ) ) > 0 ) {
WP_CLI::warning( __( '[Failed] Some installed plugins are incompatible. Please review the plugins by going to WooCommerce > Settings > Advanced > Features and see the "Order data storage" section.', 'woocommerce' ) );
$enable_hpos = false;
}
/** DataSynchronizer instance @var DataSynchronizer $data_synchronizer */
$data_synchronizer = wc_get_container()->get( DataSynchronizer::class );
$pending_orders = $data_synchronizer->get_total_pending_count();
$table_exists = $data_synchronizer->check_orders_table_exists();
if ( ! $table_exists ) {
WP_CLI::warning( __( 'Orders table does not exist. Creating...', 'woocommerce' ) );
if ( $is_new_shop || 0 === $pending_orders ) {
$data_synchronizer->create_database_tables();
if ( $data_synchronizer->check_orders_table_exists() ) {
WP_CLI::log( __( 'Orders table created.', 'woocommerce' ) );
$table_exists = true;
} else {
WP_CLI::warning( __( '[Failed] Orders table could not be created.', 'woocommerce' ) );
$enable_hpos = false;
}
} else {
WP_CLI::warning( __( '[Failed] The orders table does not exist and this is not a new shop. Please create the table by going to WooCommerce > Settings > Advanced > Features and enabling sync.', 'woocommerce' ) );
$enable_hpos = false;
}
}
if ( $pending_orders > 0 ) {
WP_CLI::warning(
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',
)
);
$enable_hpos = false;
}
if ( $assoc_args['with-sync'] && $table_exists ) {
if ( $data_synchronizer->data_sync_is_enabled() ) {
WP_CLI::warning( __( 'Sync is already enabled.', 'woocommerce' ) );
} else {
$feature_controller->change_feature_enable( DataSynchronizer::ORDERS_DATA_SYNC_ENABLED_OPTION, true );
WP_CLI::success( __( 'Sync enabled.', 'woocommerce' ) );
}
}
if ( ! $enable_hpos ) {
WP_CLI::error( __( 'HPOS pre-checks failed, please see the errors above', 'woocommerce' ) );
return;
}
/** CustomOrdersTableController instance @var CustomOrdersTableController $cot_status */
$cot_status = wc_get_container()->get( CustomOrdersTableController::class );
if ( $cot_status->custom_orders_table_usage_is_enabled() ) {
WP_CLI::warning( __( 'HPOS is already enabled.', 'woocommerce' ) );
} else {
$feature_controller->change_feature_enable( 'custom_order_tables', true );
if ( $cot_status->custom_orders_table_usage_is_enabled() ) {
WP_CLI::success( __( 'HPOS enabled.', 'woocommerce' ) );
} else {
WP_CLI::error( __( 'HPOS could not be enabled.', 'woocommerce' ) );
}
}
}
/**
* Disables custom order tables (HPOS) and posts to authoritative if HPOS and post tables are in sync.
*
* ## OPTIONS
*
* [--with-sync]
* : Also disables sync (if it's currently enabled).
* ---
* default: false
* ---
*
* ### EXAMPLES
*
* # Disable HPOS.
* wp wc cot disable
*
* @param array $args Positional arguments passed to the command.
* @param array $assoc_args Associative arguments (options) passed to the command.
*/
public function disable( $args, $assoc_args ) {
$assoc_args = wp_parse_args(
$assoc_args,
array(
'with-sync' => false,
)
);
WP_CLI::log( __( 'Running pre-disable checks...', 'woocommerce' ) );
/** DataSynchronizer instance @var DataSynchronizer $data_synchronizer */
$data_synchronizer = wc_get_container()->get( DataSynchronizer::class );
$pending_orders = $data_synchronizer->get_total_pending_count();
if ( $pending_orders > 0 ) {
return WP_CLI::error(
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',
)
);
}
/** FeaturesController instance @var FeaturesController $feature_controller */
$feature_controller = wc_get_container()->get( FeaturesController::class );
/** CustomOrdersTableController instance @var CustomOrdersTableController $cot_status */
$cot_status = wc_get_container()->get( CustomOrdersTableController::class );
if ( ! $cot_status->custom_orders_table_usage_is_enabled() ) {
WP_CLI::warning( __( 'HPOS is already disabled.', 'woocommerce' ) );
} else {
$feature_controller->change_feature_enable( 'custom_order_tables', false );
if ( $cot_status->custom_orders_table_usage_is_enabled() ) {
return WP_CLI::warning( __( 'HPOS could not be disabled.', 'woocommerce' ) );
} else {
WP_CLI::success( __( 'HPOS disabled.', 'woocommerce' ) );
}
}
if ( $assoc_args['with-sync'] ) {
if ( ! $data_synchronizer->data_sync_is_enabled() ) {
return WP_CLI::warning( __( 'Sync is already disabled.', 'woocommerce' ) );
}
$feature_controller->change_feature_enable( DataSynchronizer::ORDERS_DATA_SYNC_ENABLED_OPTION, false );
if ( $data_synchronizer->data_sync_is_enabled() ) {
return WP_CLI::warning( __( 'Sync could not be disabled.', 'woocommerce' ) );
} else {
WP_CLI::success( __( 'Sync disabled.', 'woocommerce' ) );
}
}
}
}

View File

@@ -16,7 +16,7 @@ use Automattic\WooCommerce\Internal\DataStores\Orders\OrdersTableDataStore;
*/
class PostToOrderAddressTableMigrator extends MetaToCustomTableMigrator {
/**
* Type of addresses being migrated, could be billing|shipping.
* Type of addresses being migrated; 'billing' or 'shipping'.
*
* @var $type
*/
@@ -25,7 +25,7 @@ class PostToOrderAddressTableMigrator extends MetaToCustomTableMigrator {
/**
* PostToOrderAddressTableMigrator constructor.
*
* @param string $type Type of addresses being migrated, could be billing|shipping.
* @param string $type Type of address being migrated; 'billing' or 'shipping'.
*/
public function __construct( $type ) {
$this->type = $type;