Merge pull request #32814 from woocommerce/fix/32797-wca-fatal

Fix WCA fatal with bail conditions for WCA execution paths
This commit is contained in:
Ilyas Foo 2022-04-29 10:50:38 +08:00 committed by GitHub
commit 959ba869c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 67 additions and 20 deletions

View File

@ -0,0 +1,4 @@
Significance: minor
Type: fix
Fix fatal errors when activated alongside WooCommerce Admin plugin

View File

@ -4,6 +4,7 @@ namespace Automattic\WooCommerce\Admin;
use Automattic\WooCommerce\Admin\DeprecatedClassFacade;
use Automattic\WooCommerce\Admin\Features\Features;
use Automattic\WooCommerce\Internal\Admin\WCAdminAssets;
/**
* Loader Class.
@ -12,6 +13,20 @@ use Automattic\WooCommerce\Admin\Features\Features;
*/
class Loader extends DeprecatedClassFacade {
/**
* The name of the non-deprecated class that this facade covers.
*
* @var string
*/
protected static $facade_over_classname = 'Automattic\WooCommerce\Internal\Admin\Loader';
/**
* The version that this class was deprecated in.
*
* @var string
*/
protected static $deprecated_in_version = '6.3.0';
/**
* Returns if a specific wc-admin feature is enabled.
*
@ -55,4 +70,20 @@ class Loader extends DeprecatedClassFacade {
wc_deprecated_function( 'is_embed_page', '6.3', '\Automattic\WooCommerce\Admin\PageController::is_embed_page()' );
return PageController::is_embed_page();
}
/**
* Determines if a minified JS file should be served.
*
* @param boolean $script_debug Only serve unminified files if script debug is on.
* @return boolean If js asset should use minified version.
*
* @deprecated since 6.3.0, use WCAdminAssets::should_use_minified_js_file( $script_debug )
*/
public static function should_use_minified_js_file( $script_debug ) {
// Bail if WC isn't initialized (This can be called from WCAdmin's entrypoint).
if ( ! defined( 'WC_ABSPATH' ) ) {
return;
}
return WCAdminAssets::should_use_minified_js_file( $script_debug );
}
}

View File

@ -67,6 +67,11 @@ class FeaturePlugin {
* Init the feature plugin, only if we can detect both Gutenberg and WooCommerce.
*/
public function init() {
// Bail if WC isn't initialized (This can be called from WCAdmin's entrypoint).
if ( ! defined( 'WC_ABSPATH' ) ) {
return;
}
// Load the page controller functions file first to prevent fatal errors when disabling WooCommerce Admin.
$this->define_constants();
require_once WC_ADMIN_ABSPATH . '/includes/react-admin/page-controller-functions.php';
@ -114,7 +119,15 @@ class FeaturePlugin {
* @deprecated 3.3.0
* @var string
*/
define( 'WC_ADMIN_VERSION_NUMBER', '3.3.0' );
if ( ! defined( 'WC_ADMIN_VERSION_NUMBER' ) ) {
/**
* Define the current WC Admin version.
*
* @deprecated 3.3.0
* @var string
*/
define( 'WC_ADMIN_VERSION_NUMBER', '3.3.0' );
}
}
/**

View File

@ -84,30 +84,29 @@ class Loader {
*/
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
add_action( 'admin_init', array( __CLASS__, 'is_using_installed_wc_admin_plugin' ) );
add_action( 'admin_init', array( __CLASS__, 'deactivate_wc_admin_plugin' ) );
}
/**
* Verifies which plugin version is being used. If WooCommerce Admin is installed and activated but not in use
* it will show a warning.
* If WooCommerce Admin is installed and activated, it will attempt to deactivate and show a notice.
*/
public static function is_using_installed_wc_admin_plugin() {
public static function deactivate_wc_admin_plugin() {
if ( PluginsHelper::is_plugin_active( 'woocommerce-admin' ) ) {
$path = PluginsHelper::get_plugin_data( 'woocommerce-admin' );
if ( WC_ADMIN_VERSION_NUMBER !== $path['Version'] ) {
add_action(
'admin_notices',
function() {
echo '<div class="error"><p>';
printf(
/* translators: %s: is referring to the plugin's name. */
esc_html__( 'You have the %s plugin activated but it is not being used.', 'woocommerce' ),
'<code>WooCommerce Admin</code>'
);
echo '</p></div>';
}
);
}
$path = PluginsHelper::get_plugin_path_from_slug( 'woocommerce-admin' );
deactivate_plugins( $path );
add_action(
'admin_notices',
function() {
echo '<div class="error"><p>';
printf(
/* translators: %s: is referring to the plugin's name. */
esc_html__( '%1$s plugin has been deactivated to avoid conflicts with %2$s plugin.', 'woocommerce' ),
'<code>WooCommerce Admin</code>',
'<code>WooCommerce</code>'
);
echo '</p></div>';
}
);
}
}