Completed
Push — fix/useBlockEditContext-usage ( 1e6b8b...d10938 )
by
unknown
199:36 queued 191:34
created

Classes_Handler::create_classmap_path_array()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 9
loc 9
rs 9.9666
c 0
b 0
f 0
1
<?php
2
/* HEADER */ // phpcs:ignore
3
4
/**
5
 * This class selects the package versions for the package classes.
6
 */
7
class Classes_Handler {
8
9
	/**
10
	 * The Plugins_Handler object.
11
	 *
12
	 * @var Plugins_Handler
13
	 */
14
	private $plugins_handler = null;
15
16
	/**
17
	 * The Version_Selector object.
18
	 *
19
	 * @var Version_Selector
20
	 */
21
	private $version_selector = null;
22
23
	/**
24
	 * The constructor.
25
	 *
26
	 * @param Plugins_Handler  $plugins_handler The Plugins_Handler object.
27
	 * @param Version_Selector $version_selector The Version_Selector object.
28
	 */
29
	public function __construct( $plugins_handler, $version_selector ) {
30
		$this->plugins_handler  = $plugins_handler;
31
		$this->version_selector = $version_selector;
32
	}
33
34
	/**
35
	 * Adds the version of a package to the $jetpack_packages_classmap global
36
	 * array so that the autoloader is able to find it.
37
	 *
38
	 * @param string $class_name Name of the class that you want to autoload.
39
	 * @param string $version Version of the class.
40
	 * @param string $path Absolute path to the class so that we can load it.
41
	 */
42 View Code Duplication
	public function enqueue_package_class( $class_name, $version, $path ) {
43
		global $jetpack_packages_classmap;
44
45
		if ( isset( $jetpack_packages_classmap[ $class_name ]['version'] ) ) {
46
			$selected_version = $jetpack_packages_classmap[ $class_name ]['version'];
47
		} else {
48
			$selected_version = null;
49
		}
50
51
		if ( $this->version_selector->is_version_update_required( $selected_version, $version ) ) {
52
			$jetpack_packages_classmap[ $class_name ] = array(
53
				'version' => $version,
54
				'path'    => $path,
55
			);
56
		}
57
	}
58
59
	/**
60
	 * Creates an array containing paths to the plugins' classmaps. The classmap filename is the filename
61
	 * generated by Jetpack Autoloader version >= 2.0.
62
	 *
63
	 * @param String $plugin The plugin name.
64
	 *
65
	 * @return Array An array of plugin names and classmap paths.
66
	 */
67 View Code Duplication
	public function create_classmap_path_array( $plugin ) {
68
		$plugin_path = trailingslashit( WP_PLUGIN_DIR ) . $plugin;
69
70
		if ( ! file_exists( $plugin_path ) ) {
71
			$plugin_path = trailingslashit( WPMU_PLUGIN_DIR ) . $plugin;
72
		}
73
74
		return trailingslashit( $plugin_path ) . 'vendor/composer/jetpack_autoload_classmap.php';
75
	}
76
77
	/**
78
	 *  Initializes the classmap.
79
	 */
80 View Code Duplication
	public function set_class_paths() {
81
		$active_plugins = $this->plugins_handler->get_all_active_plugins();
82
		$plugins_paths  = array_map( array( $this, 'create_classmap_path_array' ), $active_plugins );
83
84
		foreach ( $plugins_paths as $path ) {
85
			if ( is_readable( $path ) ) {
86
				$class_map = require $path;
87
88
				if ( is_array( $class_map ) ) {
89
					foreach ( $class_map as $class_name => $class_info ) {
90
						$this->enqueue_package_class( $class_name, $class_info['version'], $class_info['path'] );
91
					}
92
				}
93
			}
94
		}
95
	}
96
}
97