Admin
4 weeks ago
Api
4 weeks ago
Blocks
4 weeks ago
Caches
4 weeks ago
Caching
1 year ago
Checkout
4 weeks ago
Database
3 months ago
Enums
4 weeks ago
Gateways
4 weeks ago
Internal
4 weeks ago
LayoutTemplates
2 years ago
Proxies
4 months ago
StoreApi
4 weeks ago
Utilities
4 weeks ago
Autoloader.php
3 months ago
Container.php
4 months ago
Deprecated.php
1 year ago
Packages.php
3 months ago
Packages.php
367 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Loads WooCommerce packages from the /packages directory. These are packages developed outside of core. |
| 4 | */ |
| 5 | |
| 6 | declare( strict_types=1 ); |
| 7 | |
| 8 | namespace Automattic\WooCommerce; |
| 9 | |
| 10 | use Automattic\Jetpack\Constants; |
| 11 | |
| 12 | defined( 'ABSPATH' ) || exit; |
| 13 | |
| 14 | /** |
| 15 | * Packages class. |
| 16 | * |
| 17 | * @since 3.7.0 |
| 18 | */ |
| 19 | class Packages { |
| 20 | |
| 21 | /** |
| 22 | * Static-only class. |
| 23 | */ |
| 24 | private function __construct() {} |
| 25 | |
| 26 | /** |
| 27 | * Array of package names and their main package classes. Once a package has been merged into WooCommerce |
| 28 | * directly it should be removed from here and added to the merged packages array. |
| 29 | * |
| 30 | * @var array Key is the package name/directory, value is the main package class which handles init. |
| 31 | */ |
| 32 | protected static $packages = array( |
| 33 | 'email-editor' => '\\Automattic\\WooCommerce\\Internal\\EmailEditor\\Package', |
| 34 | ); |
| 35 | |
| 36 | /** |
| 37 | * Array of package names and their main package classes. |
| 38 | * |
| 39 | * One a package has been merged into WooCommerce Core it should be moved from the package list and placed in |
| 40 | * this list. This will ensure that the feature plugin is disabled as well as provide the class to handle |
| 41 | * initialization for the now-merged feature plugin. |
| 42 | * |
| 43 | * Once a package has been merged into WooCommerce Core it should have its slug added here. This will ensure |
| 44 | * that we deactivate the feature plugin automatically to prevent any problems caused by conflicts between |
| 45 | * the two versions caused by them both being active. |
| 46 | * |
| 47 | * The packages included in this array cannot be deactivated and will always load with WooCommerce core. |
| 48 | * |
| 49 | * @var array Key is the package name/directory, value is the main package class which handles init. |
| 50 | */ |
| 51 | protected static $base_packages = array( |
| 52 | 'woocommerce-admin' => '\\Automattic\\WooCommerce\\Admin\\Composer\\Package', |
| 53 | 'woocommerce-gutenberg-products-block' => '\\Automattic\\WooCommerce\\Blocks\\Package', |
| 54 | ); |
| 55 | |
| 56 | /** |
| 57 | * Similar to $base_packages, but |
| 58 | * the packages included in this array can be deactivated via the 'woocommerce_merged_packages' filter. |
| 59 | * |
| 60 | * @var array Key is the package name/directory, value is the main package class which handles init. |
| 61 | */ |
| 62 | protected static $merged_packages = array( |
| 63 | 'woocommerce-brands' => '\\Automattic\\WooCommerce\\Internal\\Brands', |
| 64 | ); |
| 65 | |
| 66 | |
| 67 | /** |
| 68 | * Init the package loader. |
| 69 | * |
| 70 | * @since 3.7.0 |
| 71 | */ |
| 72 | public static function init() { |
| 73 | add_action( 'plugins_loaded', array( __CLASS__, 'prepare_packages' ), -100 ); |
| 74 | add_action( 'plugins_loaded', array( __CLASS__, 'on_init' ), 10 ); |
| 75 | |
| 76 | // Prevent plugins already merged into WooCommerce core from getting activated as standalone plugins. |
| 77 | add_action( 'activate_plugin', array( __CLASS__, 'deactivate_merged_plugins' ) ); |
| 78 | |
| 79 | // Display a notice in the Plugins tab next to plugins already merged into WooCommerce core. |
| 80 | add_filter( 'all_plugins', array( __CLASS__, 'mark_merged_plugins_as_pending_update' ), 10, 1 ); |
| 81 | add_action( 'after_plugin_row', array( __CLASS__, 'display_notice_for_merged_plugins' ), 10, 1 ); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Callback for WordPress init hook. |
| 86 | */ |
| 87 | public static function on_init() { |
| 88 | self::deactivate_merged_packages(); |
| 89 | self::initialize_packages(); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Checks a package exists by looking for it's directory. |
| 94 | * |
| 95 | * @param string $package Package name. |
| 96 | * @return boolean |
| 97 | */ |
| 98 | public static function package_exists( $package ) { |
| 99 | return file_exists( dirname( __DIR__ ) . '/packages/' . $package ); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Checks a package exists by looking for it's directory. |
| 104 | * |
| 105 | * @param string $class_name Class name. |
| 106 | * @return boolean |
| 107 | */ |
| 108 | public static function should_load_class( $class_name ) { |
| 109 | |
| 110 | foreach ( self::$merged_packages as $merged_package_name => $merged_package_class ) { |
| 111 | if ( str_replace( 'woocommerce-', 'wc_', $merged_package_name ) === $class_name ) { |
| 112 | return true; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Gets all merged, enabled packages. |
| 121 | * |
| 122 | * @return array |
| 123 | */ |
| 124 | protected static function get_enabled_packages() { |
| 125 | $enabled_packages = array(); |
| 126 | |
| 127 | foreach ( self::$merged_packages as $merged_package_name => $package_class ) { |
| 128 | |
| 129 | $option = 'wc_feature_' . str_replace( '-', '_', $merged_package_name ) . '_enabled'; |
| 130 | $option_value = get_option( $option, '' ); |
| 131 | |
| 132 | // Opt out from the feature. |
| 133 | if ( 'no' === $option_value ) { |
| 134 | continue; |
| 135 | } |
| 136 | |
| 137 | // Force enable feature -- mainly for testing purpose. |
| 138 | if ( 'yes' === $option_value ) { |
| 139 | $enabled_packages[ $merged_package_name ] = $package_class; |
| 140 | continue; |
| 141 | } |
| 142 | |
| 143 | // If an option is not set, ensure that a package is enabled for user's remote variant number. Mainly for gradual releases. |
| 144 | $experimental_package_enabled = method_exists( $package_class, 'is_enabled' ) ? |
| 145 | call_user_func( array( $package_class, 'is_enabled' ) ) : |
| 146 | false; |
| 147 | |
| 148 | if ( ! $experimental_package_enabled ) { |
| 149 | continue; |
| 150 | } |
| 151 | |
| 152 | $enabled_packages[ $merged_package_name ] = $package_class; |
| 153 | } |
| 154 | |
| 155 | return array_merge( $enabled_packages, self::$base_packages ); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Checks if a package is enabled. |
| 160 | * |
| 161 | * @param string $package Package name. |
| 162 | * @return boolean |
| 163 | */ |
| 164 | public static function is_package_enabled( $package ) { |
| 165 | return array_key_exists( $package, self::get_enabled_packages() ); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Prepare merged packages for initialization. |
| 170 | * Especially useful when running actions early in the 'plugins_loaded' timeline. |
| 171 | */ |
| 172 | public static function prepare_packages() { |
| 173 | foreach ( self::get_enabled_packages() as $package_name => $package_class ) { |
| 174 | if ( method_exists( $package_class, 'prepare' ) ) { |
| 175 | call_user_func( array( $package_class, 'prepare' ) ); |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Deactivates merged feature plugins. |
| 182 | * |
| 183 | * Once a feature plugin is merged into WooCommerce Core it should be deactivated. This method will |
| 184 | * ensure that a plugin gets deactivated. Note that for the first request it will still be active, |
| 185 | * and as such, there may be some odd behavior. This is unlikely to cause any issues though |
| 186 | * because it will be deactivated on the request that updates or activates WooCommerce. |
| 187 | */ |
| 188 | protected static function deactivate_merged_packages() { |
| 189 | // Developers may need to be able to run merged feature plugins alongside merged packages for testing purposes. |
| 190 | if ( Constants::is_true( 'WC_ALLOW_MERGED_FEATURE_PLUGINS' ) ) { |
| 191 | return; |
| 192 | } |
| 193 | |
| 194 | // Scroll through all of the active plugins and disable them if they're merged packages. |
| 195 | $active_plugins = get_option( 'active_plugins', array() ); |
| 196 | // Deactivate the plugin if possible so that there are no conflicts. |
| 197 | foreach ( $active_plugins as $active_plugin_path ) { |
| 198 | $plugin_file = basename( plugin_basename( $active_plugin_path ), '.php' ); |
| 199 | |
| 200 | if ( ! self::is_package_enabled( $plugin_file ) ) { |
| 201 | continue; |
| 202 | } |
| 203 | |
| 204 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 205 | |
| 206 | // Make sure to display a message informing the user that the plugin has been deactivated. |
| 207 | $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $active_plugin_path ); |
| 208 | deactivate_plugins( $active_plugin_path ); |
| 209 | add_action( |
| 210 | 'admin_notices', |
| 211 | function () use ( $plugin_data ) { |
| 212 | echo '<div class="error"><p>'; |
| 213 | printf( |
| 214 | /* translators: %s: is referring to the plugin's name. */ |
| 215 | esc_html__( 'The %1$s plugin has been deactivated as the latest improvements are now included with the %2$s plugin.', 'woocommerce' ), |
| 216 | '<code>' . esc_html( $plugin_data['Name'] ) . '</code>', |
| 217 | '<code>WooCommerce</code>' |
| 218 | ); |
| 219 | echo '</p></div>'; |
| 220 | } |
| 221 | ); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Prevent plugins already merged into WooCommerce core from getting activated as standalone plugins. |
| 227 | * |
| 228 | * @param string $plugin Plugin name. |
| 229 | */ |
| 230 | public static function deactivate_merged_plugins( $plugin ) { |
| 231 | $plugin_dir = basename( dirname( $plugin ) ); |
| 232 | |
| 233 | if ( self::is_package_enabled( $plugin_dir ) ) { |
| 234 | $plugins_url = esc_url( admin_url( 'plugins.php' ) ); |
| 235 | wp_die( |
| 236 | esc_html__( 'This plugin cannot be activated because its functionality is now included in WooCommerce core.', 'woocommerce' ), |
| 237 | esc_html__( 'Plugin Activation Error', 'woocommerce' ), |
| 238 | array( |
| 239 | 'link_url' => esc_url( $plugins_url ), |
| 240 | 'link_text' => esc_html__( 'Return to the Plugins page', 'woocommerce' ), |
| 241 | ), |
| 242 | ); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Mark merged plugins as pending update. |
| 248 | * This is required for correctly displaying maintenance notices. |
| 249 | * |
| 250 | * @param array $plugins Plugins list. |
| 251 | */ |
| 252 | public static function mark_merged_plugins_as_pending_update( $plugins ) { |
| 253 | foreach ( $plugins as $plugin_name => $plugin_data ) { |
| 254 | $plugin_dir = basename( dirname( $plugin_name ) ); |
| 255 | if ( self::is_package_enabled( $plugin_dir ) ) { |
| 256 | // Necessary to properly display notice within row. |
| 257 | $plugins[ $plugin_name ]['update'] = 1; |
| 258 | } |
| 259 | } |
| 260 | return $plugins; |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Displays a maintenance notice next to merged plugins, to inform users |
| 265 | * that the plugin functionality is now offered by WooCommerce core. |
| 266 | * |
| 267 | * Requires 'mark_merged_plugins_as_pending_update' to properly display this notice. |
| 268 | * |
| 269 | * @param string $plugin_file Plugin file. |
| 270 | */ |
| 271 | public static function display_notice_for_merged_plugins( $plugin_file ) { |
| 272 | global $wp_list_table; |
| 273 | |
| 274 | $plugin_dir = basename( dirname( $plugin_file ) ); |
| 275 | if ( ! self::is_package_enabled( $plugin_dir ) || is_null( $wp_list_table ) ) { |
| 276 | return; |
| 277 | } |
| 278 | |
| 279 | $columns_count = $wp_list_table->get_column_count(); |
| 280 | $notice = __( 'This plugin can no longer be activated because its functionality is now included in <strong>WooCommerce</strong>. It is recommended to <strong>delete</strong> it.', 'woocommerce' ); |
| 281 | echo '<tr class="plugin-update-tr"><td colspan="' . esc_attr( $columns_count ) . '" class="plugin-update"><div class="update-message notice inline notice-error notice-alt"><p>' . wp_kses_post( $notice ) . '</p></div></td></tr>'; |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Loads packages after plugins_loaded hook. |
| 286 | * |
| 287 | * Each package should include an init file which loads the package so it can be used by core. |
| 288 | */ |
| 289 | protected static function initialize_packages() { |
| 290 | foreach ( self::get_enabled_packages() as $package_name => $package_class ) { |
| 291 | call_user_func( array( $package_class, 'init' ) ); |
| 292 | } |
| 293 | |
| 294 | foreach ( self::$packages as $package_name => $package_class ) { |
| 295 | if ( ! self::package_exists( $package_name ) ) { |
| 296 | self::missing_package( $package_name ); |
| 297 | continue; |
| 298 | } |
| 299 | call_user_func( array( $package_class, 'init' ) ); |
| 300 | } |
| 301 | |
| 302 | // Proxies "activated_plugin" hook for embedded packages listen on WC plugin activation |
| 303 | // https://github.com/woocommerce/woocommerce/issues/28697. |
| 304 | if ( is_admin() ) { |
| 305 | $activated_plugin = get_transient( 'woocommerce_activated_plugin' ); |
| 306 | if ( $activated_plugin ) { |
| 307 | delete_transient( 'woocommerce_activated_plugin' ); |
| 308 | |
| 309 | /** |
| 310 | * WooCommerce is activated hook. |
| 311 | * |
| 312 | * @since 5.0.0 |
| 313 | * @param bool $activated_plugin Activated plugin path, |
| 314 | * generally woocommerce/woocommerce.php. |
| 315 | */ |
| 316 | do_action( 'woocommerce_activated_plugin', $activated_plugin ); |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * If a package is missing, add an admin notice. |
| 323 | * |
| 324 | * @param string $package Package name. |
| 325 | */ |
| 326 | protected static function missing_package( $package ) { |
| 327 | if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { |
| 328 | error_log( // phpcs:ignore |
| 329 | sprintf( |
| 330 | /* Translators: %s package name. */ |
| 331 | esc_html__( 'Missing the WooCommerce %s package', 'woocommerce' ), |
| 332 | '<code>' . esc_html( $package ) . '</code>' |
| 333 | ) . ' - ' . 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://developer.woocommerce.com/docs/contribution/contributing/#setting-up-your-development-environment', 'woocommerce' ) |
| 334 | ); |
| 335 | } |
| 336 | add_action( |
| 337 | 'admin_notices', |
| 338 | function () use ( $package ) { |
| 339 | ?> |
| 340 | <div class="notice notice-error"> |
| 341 | <p> |
| 342 | <strong> |
| 343 | <?php |
| 344 | printf( |
| 345 | /* Translators: %s package name. */ |
| 346 | esc_html__( 'Missing the WooCommerce %s package', 'woocommerce' ), |
| 347 | '<code>' . esc_html( $package ) . '</code>' |
| 348 | ); |
| 349 | ?> |
| 350 | </strong> |
| 351 | <br> |
| 352 | <?php |
| 353 | printf( |
| 354 | /* translators: 1: is a link to a support document. 2: closing link */ |
| 355 | 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' ), |
| 356 | '<a href="' . esc_url( 'https://developer.woocommerce.com/docs/contribution/contributing/#setting-up-your-development-environment' ) . '" target="_blank" rel="noopener noreferrer">', |
| 357 | '</a>' |
| 358 | ); |
| 359 | ?> |
| 360 | </p> |
| 361 | </div> |
| 362 | <?php |
| 363 | } |
| 364 | ); |
| 365 | } |
| 366 | } |
| 367 |