From 08b8dd99cc318586225e9eacf8389fecbfcf64dc Mon Sep 17 00:00:00 2001 From: Jason Kytros Date: Mon, 30 Sep 2024 13:17:17 +0300 Subject: [PATCH] Revert packages initialization timeline change (#51728) * Revert packages initialization timeline change * Add changefile(s) from automation for the following project(s): woocommerce * Fix lint errors --------- Co-authored-by: github-actions --- .../changelog/51728-fix-issue-51711 | 4 ++++ .../woocommerce/includes/class-wc-brands.php | 2 +- plugins/woocommerce/src/Internal/Brands.php | 20 ++++++++++++++----- plugins/woocommerce/src/Packages.php | 15 +++++++++++++- 4 files changed, 34 insertions(+), 7 deletions(-) create mode 100644 plugins/woocommerce/changelog/51728-fix-issue-51711 diff --git a/plugins/woocommerce/changelog/51728-fix-issue-51711 b/plugins/woocommerce/changelog/51728-fix-issue-51711 new file mode 100644 index 00000000000..dd4438a354b --- /dev/null +++ b/plugins/woocommerce/changelog/51728-fix-issue-51711 @@ -0,0 +1,4 @@ +Significance: patch +Type: tweak +Comment: This PR reverts a change that hasn't yet been released, so no changelog is required. + diff --git a/plugins/woocommerce/includes/class-wc-brands.php b/plugins/woocommerce/includes/class-wc-brands.php index 71e1fa71299..43732102f01 100644 --- a/plugins/woocommerce/includes/class-wc-brands.php +++ b/plugins/woocommerce/includes/class-wc-brands.php @@ -26,7 +26,7 @@ class WC_Brands { public function __construct() { $this->template_url = apply_filters( 'woocommerce_template_url', 'woocommerce/' ); // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment - add_action( 'plugins_loaded', array( $this, 'register_hooks' ), 2 ); + add_action( 'plugins_loaded', array( $this, 'register_hooks' ), 11 ); $this->register_shortcodes(); } diff --git a/plugins/woocommerce/src/Internal/Brands.php b/plugins/woocommerce/src/Internal/Brands.php index 7ac3cdbab05..4abd1c6fc0b 100644 --- a/plugins/woocommerce/src/Internal/Brands.php +++ b/plugins/woocommerce/src/Internal/Brands.php @@ -25,11 +25,6 @@ class Brands { return; } - // If the WooCommerce Brands plugin is activated via the WP CLI using the '--skip-plugins' flag, deactivate it here. - if ( function_exists( 'wc_brands_init' ) ) { - remove_action( 'plugins_loaded', 'wc_brands_init', 1 ); - } - include_once WC_ABSPATH . 'includes/class-wc-brands.php'; include_once WC_ABSPATH . 'includes/class-wc-brands-coupons.php'; include_once WC_ABSPATH . 'includes/class-wc-brands-brand-settings-manager.php'; @@ -58,4 +53,19 @@ class Brands { } return ( $assignment <= 6 ); // Considering 5% of the 0-120 range. } + + /** + * If WooCommerce Brands gets activated forcibly, without WooCommerce active (e.g. via '--skip-plugins'), + * remove WooCommerce Brands initialization functions early on in the 'plugins_loaded' timeline. + */ + public static function prepare() { + + if ( ! self::is_enabled() ) { + return; + } + + if ( function_exists( 'wc_brands_init' ) ) { + remove_action( 'plugins_loaded', 'wc_brands_init', 1 ); + } + } } diff --git a/plugins/woocommerce/src/Packages.php b/plugins/woocommerce/src/Packages.php index eb251bbc908..568ec63c509 100644 --- a/plugins/woocommerce/src/Packages.php +++ b/plugins/woocommerce/src/Packages.php @@ -66,7 +66,8 @@ class Packages { * @since 3.7.0 */ public static function init() { - add_action( 'plugins_loaded', array( __CLASS__, 'on_init' ), 0 ); + add_action( 'plugins_loaded', array( __CLASS__, 'prepare_packages' ), -100 ); + add_action( 'plugins_loaded', array( __CLASS__, 'on_init' ), 10 ); // Prevent plugins already merged into WooCommerce core from getting activated as standalone plugins. add_action( 'activate_plugin', array( __CLASS__, 'deactivate_merged_plugins' ) ); @@ -149,6 +150,18 @@ class Packages { return array_key_exists( $package, self::get_enabled_packages() ); } + /** + * Prepare merged packages for initialization. + * Especially useful when running actions early in the 'plugins_loaded' timeline. + */ + public static function prepare_packages() { + foreach ( self::get_enabled_packages() as $package_name => $package_class ) { + if ( method_exists( $package_class, 'prepare' ) ) { + call_user_func( array( $package_class, 'prepare' ) ); + } + } + } + /** * Deactivates merged feature plugins. *