Admin
1 year ago
Blocks
2 years ago
Caches
3 years ago
Caching
2 years ago
Checkout
3 years ago
Database
2 years ago
Internal
1 year ago
LayoutTemplates
2 years ago
Proxies
2 years ago
StoreApi
5 months ago
Utilities
2 years ago
Autoloader.php
5 years ago
Container.php
2 years ago
Packages.php
2 years ago
Packages.php
204 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Loads WooCommerce packages from the /packages directory. These are packages developed outside of core. |
| 4 | */ |
| 5 | |
| 6 | namespace Automattic\WooCommerce; |
| 7 | |
| 8 | use Automattic\Jetpack\Constants; |
| 9 | |
| 10 | defined( 'ABSPATH' ) || exit; |
| 11 | |
| 12 | /** |
| 13 | * Packages class. |
| 14 | * |
| 15 | * @since 3.7.0 |
| 16 | */ |
| 17 | class Packages { |
| 18 | |
| 19 | /** |
| 20 | * Static-only class. |
| 21 | */ |
| 22 | private function __construct() {} |
| 23 | |
| 24 | /** |
| 25 | * Array of package names and their main package classes. Once a package has been merged into WooCommerce |
| 26 | * directly it should be removed from here and added to the merged packages array. |
| 27 | * |
| 28 | * @var array Key is the package name/directory, value is the main package class which handles init. |
| 29 | */ |
| 30 | protected static $packages = array(); |
| 31 | |
| 32 | /** |
| 33 | * Array of package names and their main package classes. |
| 34 | * |
| 35 | * One a package has been merged into WooCommerce Core it should be moved fron the package list and placed in |
| 36 | * this list. This will ensure that the feature plugin is disabled as well as provide the class to handle |
| 37 | * initialization for the now-merged feature plugin. |
| 38 | * |
| 39 | * Once a package has been merged into WooCommerce Core it should have its slug added here. This will ensure |
| 40 | * that we deactivate the feature plugin automaticatlly to prevent any problems caused by conflicts between |
| 41 | * the two versions caused by them both being active. |
| 42 | * |
| 43 | * @var array Key is the package name/directory, value is the main package class which handles init. |
| 44 | */ |
| 45 | protected static $merged_packages = array( |
| 46 | 'woocommerce-admin' => '\\Automattic\\WooCommerce\\Admin\\Composer\\Package', |
| 47 | 'woocommerce-gutenberg-products-block' => '\\Automattic\\WooCommerce\\Blocks\\Package', |
| 48 | ); |
| 49 | |
| 50 | /** |
| 51 | * Init the package loader. |
| 52 | * |
| 53 | * @since 3.7.0 |
| 54 | */ |
| 55 | public static function init() { |
| 56 | add_action( 'plugins_loaded', array( __CLASS__, 'on_init' ) ); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Callback for WordPress init hook. |
| 61 | */ |
| 62 | public static function on_init() { |
| 63 | self::deactivate_merged_packages(); |
| 64 | self::initialize_packages(); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Checks a package exists by looking for it's directory. |
| 69 | * |
| 70 | * @param string $package Package name. |
| 71 | * @return boolean |
| 72 | */ |
| 73 | public static function package_exists( $package ) { |
| 74 | return file_exists( dirname( __DIR__ ) . '/packages/' . $package ); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Deactivates merged feature plugins. |
| 79 | * |
| 80 | * Once a feature plugin is merged into WooCommerce Core it should be deactivated. This method will |
| 81 | * ensure that a plugin gets deactivated. Note that for the first request it will still be active, |
| 82 | * and as such, there may be some odd behavior. This is unlikely to cause any issues though |
| 83 | * because it will be deactivated on the request that updates or activates WooCommerce. |
| 84 | */ |
| 85 | protected static function deactivate_merged_packages() { |
| 86 | // Developers may need to be able to run merged feature plugins alongside merged packages for testing purposes. |
| 87 | if ( Constants::is_true( 'WC_ALLOW_MERGED_FEATURE_PLUGINS' ) ) { |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | // Scroll through all of the active plugins and disable them if they're merged packages. |
| 92 | $active_plugins = get_option( 'active_plugins', array() ); |
| 93 | // Deactivate the plugin if possible so that there are no conflicts. |
| 94 | foreach ( $active_plugins as $active_plugin_path ) { |
| 95 | $plugin_file = basename( plugin_basename( $active_plugin_path ), '.php' ); |
| 96 | if ( ! isset( self::$merged_packages[ $plugin_file ] ) ) { |
| 97 | continue; |
| 98 | } |
| 99 | |
| 100 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 101 | |
| 102 | // Make sure to display a message informing the user that the plugin has been deactivated. |
| 103 | $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $active_plugin_path ); |
| 104 | deactivate_plugins( $active_plugin_path ); |
| 105 | add_action( |
| 106 | 'admin_notices', |
| 107 | function() use ( $plugin_data ) { |
| 108 | echo '<div class="error"><p>'; |
| 109 | printf( |
| 110 | /* translators: %s: is referring to the plugin's name. */ |
| 111 | esc_html__( 'The %1$s plugin has been deactivated as the latest improvements are now included with the %2$s plugin.', 'woocommerce' ), |
| 112 | '<code>' . esc_html( $plugin_data['Name'] ) . '</code>', |
| 113 | '<code>WooCommerce</code>' |
| 114 | ); |
| 115 | echo '</p></div>'; |
| 116 | } |
| 117 | ); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Loads packages after plugins_loaded hook. |
| 123 | * |
| 124 | * Each package should include an init file which loads the package so it can be used by core. |
| 125 | */ |
| 126 | protected static function initialize_packages() { |
| 127 | foreach ( self::$merged_packages as $package_name => $package_class ) { |
| 128 | call_user_func( array( $package_class, 'init' ) ); |
| 129 | } |
| 130 | |
| 131 | foreach ( self::$packages as $package_name => $package_class ) { |
| 132 | if ( ! self::package_exists( $package_name ) ) { |
| 133 | self::missing_package( $package_name ); |
| 134 | continue; |
| 135 | } |
| 136 | call_user_func( array( $package_class, 'init' ) ); |
| 137 | } |
| 138 | |
| 139 | // Proxies "activated_plugin" hook for embedded packages listen on WC plugin activation |
| 140 | // https://github.com/woocommerce/woocommerce/issues/28697. |
| 141 | if ( is_admin() ) { |
| 142 | $activated_plugin = get_transient( 'woocommerce_activated_plugin' ); |
| 143 | if ( $activated_plugin ) { |
| 144 | delete_transient( 'woocommerce_activated_plugin' ); |
| 145 | |
| 146 | /** |
| 147 | * WooCommerce is activated hook. |
| 148 | * |
| 149 | * @since 5.0.0 |
| 150 | * @param bool $activated_plugin Activated plugin path, |
| 151 | * generally woocommerce/woocommerce.php. |
| 152 | */ |
| 153 | do_action( 'woocommerce_activated_plugin', $activated_plugin ); |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * If a package is missing, add an admin notice. |
| 160 | * |
| 161 | * @param string $package Package name. |
| 162 | */ |
| 163 | protected static function missing_package( $package ) { |
| 164 | if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { |
| 165 | error_log( // phpcs:ignore |
| 166 | sprintf( |
| 167 | /* Translators: %s package name. */ |
| 168 | esc_html__( 'Missing the WooCommerce %s package', 'woocommerce' ), |
| 169 | '<code>' . esc_html( $package ) . '</code>' |
| 170 | ) . ' - ' . esc_html__( 'Your installation of WooCommerce is incomplete. If you installed WooCommerce from GitHub, please refer to this document to set up your development environment: https://github.com/woocommerce/woocommerce/wiki/How-to-set-up-WooCommerce-development-environment', 'woocommerce' ) |
| 171 | ); |
| 172 | } |
| 173 | add_action( |
| 174 | 'admin_notices', |
| 175 | function() use ( $package ) { |
| 176 | ?> |
| 177 | <div class="notice notice-error"> |
| 178 | <p> |
| 179 | <strong> |
| 180 | <?php |
| 181 | printf( |
| 182 | /* Translators: %s package name. */ |
| 183 | esc_html__( 'Missing the WooCommerce %s package', 'woocommerce' ), |
| 184 | '<code>' . esc_html( $package ) . '</code>' |
| 185 | ); |
| 186 | ?> |
| 187 | </strong> |
| 188 | <br> |
| 189 | <?php |
| 190 | printf( |
| 191 | /* translators: 1: is a link to a support document. 2: closing link */ |
| 192 | esc_html__( 'Your installation of WooCommerce is incomplete. If you installed WooCommerce from GitHub, %1$splease refer to this document%2$s to set up your development environment.', 'woocommerce' ), |
| 193 | '<a href="' . esc_url( 'https://github.com/woocommerce/woocommerce/wiki/How-to-set-up-WooCommerce-development-environment' ) . '" target="_blank" rel="noopener noreferrer">', |
| 194 | '</a>' |
| 195 | ); |
| 196 | ?> |
| 197 | </p> |
| 198 | </div> |
| 199 | <?php |
| 200 | } |
| 201 | ); |
| 202 | } |
| 203 | } |
| 204 |