From 762e94a8447e1d7ff1b562353e4a48c63d128985 Mon Sep 17 00:00:00 2001 From: Mark Biek Date: Wed, 10 Jul 2024 08:12:43 -0400 Subject: [PATCH] WPCOMSH: Post transfer action to conditionally enable HPOS on WooCommerce sites (#38119) * changelog * Post transfer action to conditionally enable WooCommerce HPOS * Version bump * Rename hpos flag for clarity * Switch to a direct call to check if Woo is active * Switch to direct call to check if HPOS is already enabled --- .../changelog/1468-update-hpos-wpcomsh-wpcli | 4 ++ projects/plugins/wpcomsh/composer.json | 2 +- projects/plugins/wpcomsh/package.json | 2 +- projects/plugins/wpcomsh/woa.php | 60 +++++++++++++++++++ projects/plugins/wpcomsh/wpcomsh.php | 1 - 5 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 projects/plugins/wpcomsh/changelog/1468-update-hpos-wpcomsh-wpcli diff --git a/projects/plugins/wpcomsh/changelog/1468-update-hpos-wpcomsh-wpcli b/projects/plugins/wpcomsh/changelog/1468-update-hpos-wpcomsh-wpcli new file mode 100644 index 0000000000000..44dbb59c946a4 --- /dev/null +++ b/projects/plugins/wpcomsh/changelog/1468-update-hpos-wpcomsh-wpcli @@ -0,0 +1,4 @@ +Significance: minor +Type: added + +WOA Post Transfer: Ensure that HPOS is enabled for WooCommerce sites. diff --git a/projects/plugins/wpcomsh/composer.json b/projects/plugins/wpcomsh/composer.json index 1098de7cb1343..fcc0aa228e161 100644 --- a/projects/plugins/wpcomsh/composer.json +++ b/projects/plugins/wpcomsh/composer.json @@ -149,4 +149,4 @@ "WPCOMSH_VERSION": "wpcomsh.php" } } -} +} \ No newline at end of file diff --git a/projects/plugins/wpcomsh/package.json b/projects/plugins/wpcomsh/package.json index 732fcf9e89431..79575c62d8cf8 100644 --- a/projects/plugins/wpcomsh/package.json +++ b/projects/plugins/wpcomsh/package.json @@ -31,4 +31,4 @@ "test": "jest --bail --ci --verbose", "test:watch": "jest --verbose --watchAll" } -} +} \ No newline at end of file diff --git a/projects/plugins/wpcomsh/woa.php b/projects/plugins/wpcomsh/woa.php index 77b16319b318e..92ac221e88b63 100644 --- a/projects/plugins/wpcomsh/woa.php +++ b/projects/plugins/wpcomsh/woa.php @@ -99,6 +99,66 @@ function wpcomsh_woa_post_transfer_update_safecss_to_custom_css( $args, $assoc_a add_action( 'wpcomsh_woa_post_transfer', 'wpcomsh_woa_post_transfer_update_safecss_to_custom_css', 10, 2 ); add_action( 'wpcomsh_woa_post_reset', 'wpcomsh_woa_post_transfer_update_safecss_to_custom_css', 10, 2 ); +/** + * Debug and error logging for the post-transfer action to enable HPOS. + * + * @param string $message Message to log. + */ +function wpcomsh_woa_post_transfer_maybe_enable_woocommerce_hpos_log( $message ) { + $message = sprintf( 'maybe_enable_woocommerce_hpos: %s', $message ); + + // The error_log call can be uncommented for debugging. + // error_log( $message ); + WPCOMSH_Log::unsafe_direct_log( $message ); +} + +/** + * Enable HPOS for WooCommerce sites that don't already have it enabled. + * + * @param array $args Arguments. + * @param array $assoc_args Associated arguments. + */ +function wpcomsh_woa_post_transfer_maybe_enable_woocommerce_hpos( $args, $assoc_args ) { + // This flag is only set for sites with ECOMMERCE_MANAGED_PLUGINS. Sites without this feature are skipped. + $enable_woocommerce_hpos = WP_CLI\Utils\get_flag_value( $assoc_args, 'enable_woocommerce_hpos', false ); + if ( ! $enable_woocommerce_hpos ) { + return; + } + + // Verify WooCommerce is installed and active. + $woocommerce_is_active = is_plugin_active( 'woocommerce/woocommerce.php' ); + + if ( false === $woocommerce_is_active ) { + wpcomsh_woa_post_transfer_maybe_enable_woocommerce_hpos_log( 'WooCommerce not active' ); + return; + } + + // Verify HPOS isn't already enabled + $option_value = get_option( 'woocommerce_custom_orders_table_enabled', false ); + + if ( 'yes' === $option_value ) { + wpcomsh_woa_post_transfer_maybe_enable_woocommerce_hpos_log( 'HPOS is already enabled' ); + return; + } + + // Enable HPOS + $result = WP_CLI::runcommand( + 'wc hpos enable', + array( + 'return' => 'all', + 'launch' => false, + 'exit_error' => false, + ) + ); + if ( 0 !== $result->return_code ) { + wpcomsh_woa_post_transfer_maybe_enable_woocommerce_hpos_log( sprintf( 'Error enabling HPOS: %s', $result->stderr ) ); + return; + } + + wpcomsh_woa_post_transfer_maybe_enable_woocommerce_hpos_log( 'Successfully enabled HPOS' ); +} +add_action( 'wpcomsh_woa_post_transfer', 'wpcomsh_woa_post_transfer_maybe_enable_woocommerce_hpos', 10, 2 ); + /** * Woo Express: Free Trial - deactivate simple site activated plugins. * diff --git a/projects/plugins/wpcomsh/wpcomsh.php b/projects/plugins/wpcomsh/wpcomsh.php index f89f56bae46c1..ef63d292f1129 100644 --- a/projects/plugins/wpcomsh/wpcomsh.php +++ b/projects/plugins/wpcomsh/wpcomsh.php @@ -9,7 +9,6 @@ * @package wpcomsh */ -// Increase version number if you change something in wpcomsh. define( 'WPCOMSH_VERSION', '3.28.0-alpha' ); // If true, Typekit fonts will be available in addition to Google fonts