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