Skip to content

Commit

Permalink
WPCOMSH: Post transfer action to conditionally enable HPOS on WooComm…
Browse files Browse the repository at this point in the history
…erce 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
  • Loading branch information
markbiek authored Jul 10, 2024
1 parent da8c335 commit 762e94a
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

WOA Post Transfer: Ensure that HPOS is enabled for WooCommerce sites.
2 changes: 1 addition & 1 deletion projects/plugins/wpcomsh/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,4 @@
"WPCOMSH_VERSION": "wpcomsh.php"
}
}
}
}
2 changes: 1 addition & 1 deletion projects/plugins/wpcomsh/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@
"test": "jest --bail --ci --verbose",
"test:watch": "jest --verbose --watchAll"
}
}
}
60 changes: 60 additions & 0 deletions projects/plugins/wpcomsh/woa.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
1 change: 0 additions & 1 deletion projects/plugins/wpcomsh/wpcomsh.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 762e94a

Please sign in to comment.