Completed
Push — master ( c63cd8...c4c8fb )
by Claudio
10:23
created

Packages::package_exists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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