Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WPCOMSH: Post transfer action to conditionally enable HPOS on WooCommerce sites #38119

Merged
merged 6 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above, can we make a more direct call for this? It may be worth going via WP_CLI for this one, as getting the general load status right for WooCommerce may be easier from the CLI command than from "raw" code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you elaborate a little? We're already using the WP_CLI in this case so I'm not sure what the change would be.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did some digging and the wc hpos enable command has quite a bit going on behind this scenes (as does manually enabling HPOS from the settings).

https://github.com/woocommerce/woocommerce/blob/d4438a883397b5a72617dc511bb387d380cfe7ff/plugins/woocommerce/src/Database/Migrations/CustomOrderTable/CLIRunner.php#L754

My inclination is to leave this as a WP_CLI command.

'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