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