diff --git a/classic-commerce.php b/classic-commerce.php index 745ceafa69..5d1c74ae34 100644 --- a/classic-commerce.php +++ b/classic-commerce.php @@ -38,6 +38,27 @@ function cc_wc_already_active_notice() { echo ''; } +/** + * 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 '
'; + echo '

'; + echo esc_html__( 'You must deactivate Classic Commerce before activating WooCommerce.', 'classic-commerce' ); + echo '

'; + echo '

'; + echo esc_html__( 'WooCommerce has not been activated.', 'classic-commerce' ); + echo '

'; + echo '
'; +} + +$_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 ( @@ -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'] ) ) + ) { + $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 //