Admin
6 years ago
Blocks
6 years ago
Checkout
5 years ago
Internal
5 years ago
Proxies
5 years ago
Utilities
5 years ago
Vendor
5 years ago
Autoloader.php
5 years ago
Container.php
5 years ago
Packages.php
5 years ago
Packages.php
119 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Loads WooCommece packages from the /packages directory. These are packages developed outside of core. |
| 4 | */ |
| 5 | |
| 6 | namespace Automattic\WooCommerce; |
| 7 | |
| 8 | defined( 'ABSPATH' ) || exit; |
| 9 | |
| 10 | /** |
| 11 | * Packages class. |
| 12 | * |
| 13 | * @since 3.7.0 |
| 14 | */ |
| 15 | class Packages { |
| 16 | |
| 17 | /** |
| 18 | * Static-only class. |
| 19 | */ |
| 20 | private function __construct() {} |
| 21 | |
| 22 | /** |
| 23 | * Array of package names and their main package classes. |
| 24 | * |
| 25 | * @var array Key is the package name/directory, value is the main package class which handles init. |
| 26 | */ |
| 27 | protected static $packages = array( |
| 28 | 'woocommerce-blocks' => '\\Automattic\\WooCommerce\\Blocks\\Package', |
| 29 | 'woocommerce-admin' => '\\Automattic\\WooCommerce\\Admin\\Composer\\Package', |
| 30 | ); |
| 31 | |
| 32 | /** |
| 33 | * Init the package loader. |
| 34 | * |
| 35 | * @since 3.7.0 |
| 36 | */ |
| 37 | public static function init() { |
| 38 | add_action( 'plugins_loaded', array( __CLASS__, 'on_init' ) ); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Callback for WordPress init hook. |
| 43 | */ |
| 44 | public static function on_init() { |
| 45 | self::load_packages(); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Checks a package exists by looking for it's directory. |
| 50 | * |
| 51 | * @param string $package Package name. |
| 52 | * @return boolean |
| 53 | */ |
| 54 | public static function package_exists( $package ) { |
| 55 | return file_exists( dirname( __DIR__ ) . '/packages/' . $package ); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Loads packages after plugins_loaded hook. |
| 60 | * |
| 61 | * Each package should include an init file which loads the package so it can be used by core. |
| 62 | */ |
| 63 | protected static function load_packages() { |
| 64 | foreach ( self::$packages as $package_name => $package_class ) { |
| 65 | if ( ! self::package_exists( $package_name ) ) { |
| 66 | self::missing_package( $package_name ); |
| 67 | continue; |
| 68 | } |
| 69 | call_user_func( array( $package_class, 'init' ) ); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * If a package is missing, add an admin notice. |
| 75 | * |
| 76 | * @param string $package Package name. |
| 77 | */ |
| 78 | protected static function missing_package( $package ) { |
| 79 | if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { |
| 80 | error_log( // phpcs:ignore |
| 81 | sprintf( |
| 82 | /* Translators: %s package name. */ |
| 83 | esc_html__( 'Missing the WooCommerce %s package', 'woocommerce' ), |
| 84 | '<code>' . esc_html( $package ) . '</code>' |
| 85 | ) . ' - ' . 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' ) |
| 86 | ); |
| 87 | } |
| 88 | add_action( |
| 89 | 'admin_notices', |
| 90 | function() use ( $package ) { |
| 91 | ?> |
| 92 | <div class="notice notice-error"> |
| 93 | <p> |
| 94 | <strong> |
| 95 | <?php |
| 96 | printf( |
| 97 | /* Translators: %s package name. */ |
| 98 | esc_html__( 'Missing the WooCommerce %s package', 'woocommerce' ), |
| 99 | '<code>' . esc_html( $package ) . '</code>' |
| 100 | ); |
| 101 | ?> |
| 102 | </strong> |
| 103 | <br> |
| 104 | <?php |
| 105 | printf( |
| 106 | /* translators: 1: is a link to a support document. 2: closing link */ |
| 107 | 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' ), |
| 108 | '<a href="' . esc_url( 'https://github.com/woocommerce/woocommerce/wiki/How-to-set-up-WooCommerce-development-environment' ) . '" target="_blank" rel="noopener noreferrer">', |
| 109 | '</a>' |
| 110 | ); |
| 111 | ?> |
| 112 | </p> |
| 113 | </div> |
| 114 | <?php |
| 115 | } |
| 116 | ); |
| 117 | } |
| 118 | } |
| 119 |