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

Fix a fatal error when CC is active and the user tries to activate Woo #175

Merged
merged 1 commit into from Jan 13, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
79 changes: 78 additions & 1 deletion classic-commerce.php
Expand Up @@ -38,6 +38,27 @@ function cc_wc_already_active_notice() {
echo '</div>';
}

/**
* Shows an error message when Classic Commerce is active and the user attempts
* to activate WooCommerce.
*
* WooCommerce and Classic Commerce cannot both be active at once.
*
* @return void
*/
function cc_wc_activate_attempted_notice() {
echo '<div class="notice error is-dismissible">';
echo '<p><strong>';
echo esc_html__( 'You must deactivate Classic Commerce before activating WooCommerce.', 'classic-commerce' );
echo '</strong></p>';
echo '<p>';
echo esc_html__( 'WooCommerce has not been activated.', 'classic-commerce' );
echo '</p>';
echo '</div>';
}

$_cc_can_load = true;

// Check if WooCommerce is already active. In this case we need to block
// Classic Commerce from being activated to avoid fatal errors.
if (
Expand All @@ -58,8 +79,64 @@ function cc_wc_already_active_notice() {
unset( $_GET['activate'] );

// Do not proceed further with Classic Commerce loading.
$_cc_can_load = false;

} else if (
// Check if this is a request that would activate WooCommerce. Since
// Classic Commerce is already active then we also need to prevent
// WooCommerce from being activated, again to avoid fatal errors.
//
// Plugin activation happens after plugins load and after `init` so we need
// to check for the presence of the related request parameters here.
//
// See also src/wp-admin/plugins.php in core.
is_admin() &&
strpos( $_SERVER['REQUEST_URI'], '/plugins.php' ) !== false &&
( isset( $_REQUEST['action'] ) || isset( $_REQUEST['action2'] ) ) &&
// Make sure we are really looking at WooCommerce and not the compatibility plugin!
file_exists( WP_PLUGIN_DIR . '/woocommerce/includes/class-woocommerce.php' )
) {
$is_activate_woo_request = false;

// Check if the user tried to activate WooCommerce by itself.
if (
isset( $_GET['action'] ) &&
$_GET['action'] === 'activate' &&
isset( $_GET['plugin'] ) &&
$_GET['plugin'] === 'woocommerce/woocommerce.php'
) {
$is_activate_woo_request = true;
}

// Check if the user tried to activate WooCommerce using either of the two
// "Bulk Actions" dropdown boxes.
if (
(
( isset( $_POST['action'] ) && $_POST['action'] === 'activate-selected' ) ||
( isset( $_POST['action2'] ) && $_POST['action2'] === 'activate-selected' )
) &&
isset( $_POST['checked'] ) &&
in_array( 'woocommerce/woocommerce.php', (array) wp_unslash( $_POST['checked'] ) )
Copy link
Contributor Author

@nylen nylen Jan 13, 2020

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

Looks good to me.

image

It's not that ugly :)

That's a nice fix!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, the end result is fine, but the internals are definitely not ideal.

I looked for a filter that might work to block a plugin activation but didn't find one. Also, activating a plugin happens fairly late in the load order, but we need to get in before the admin_notices action at least.

Thanks for taking a look!

) {
$is_activate_woo_request = true;
}

if ( $is_activate_woo_request ) {
// Show an admin notice.
add_action( 'admin_notices', 'cc_wc_activate_attempted_notice' );

// Block WooCommerce from being activated.
unset( $_GET['action'] );
unset( $_POST['action'] );
unset( $_REQUEST['action'] );
unset( $_POST['action2'] );
unset( $_REQUEST['action2'] );

// Proceed normally with the Classic Commerce loading process below.
}
}

} else {
if ( $_cc_can_load ) {

////////////////////////////////////////////
// BEGIN CLASSIC COMMERCE LOADING PROCESS //
Expand Down