Completed
Push — update/refactor_autoloader_fun... ( 460731...df4d56 )
by
unknown
48:56 queued 42:47
created

Plugins_Handler   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 140
rs 10
c 0
b 0
f 0
wmc 18
lcom 1
cbo 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A get_all_active_plugins() 0 12 2
A create_map_path_array() 0 8 1
A get_active_plugins_paths() 0 4 1
A should_autoloader_reset() 0 14 2
B get_plugins_activating_via_request() 0 32 9
A get_all_activating_plugins() 0 6 1
A get_current_plugin() 0 9 2
1
<?php
2
/* HEADER */ // phpcs:ignore
3
4
/**
5
 * This class provides information about the current plugin and the site's active plugins.
6
 */
7
class Plugins_Handler {
8
9
	/**
10
	 * Returns an array containing all active plugins and all known activating
11
	 * plugins.
12
	 *
13
	 * @return Array An array of plugin names as strings.
14
	 */
15
	public function get_all_active_plugins() {
16
		$active_plugins = array_merge(
17
			is_multisite()
18
				? array_keys( get_site_option( 'active_sitewide_plugins', array() ) )
19
				: array(),
20
			(array) get_option( 'active_plugins', array() )
21
		);
22
23
		$plugins = array_unique( array_merge( $active_plugins, $this->get_all_activating_plugins() ) );
24
25
		return $plugins;
26
	}
27
28
	/**
29
	 * Creates an array containing the paths to the classmap and filemap for the given plugin.
30
	 * The classmap and filemap filenames are the names of the files generated by Jetpack
31
	 * Autoloader with versions >=2.0.
32
	 *
33
	 * @param String $plugin The plugin string.
34
	 * @return Array An array containing the paths to the plugin's classmap and filemap.
35
	 */
36
	public function create_map_path_array( $plugin ) {
37
		$plugin_path = plugin_dir_path( trailingslashit( WP_PLUGIN_DIR ) . $plugin );
38
39
		return array(
40
			'class' => trailingslashit( $plugin_path ) . 'vendor/composer/jetpack_autoload_classmap.php',
41
			'file'  => trailingslashit( $plugin_path ) . 'vendor/composer/jetpack_autoload_filemap.php',
42
		);
43
	}
44
45
	/**
46
	 * Returns an array containing the paths to the classmap and filemap for the active plugins.
47
	 */
48
	public function get_active_plugins_paths() {
49
		$active_plugins = $this->get_all_active_plugins();
50
		return array_map( array( $this, 'create_map_path_array' ), $active_plugins );
51
	}
52
53
	/**
54
	 * Checks whether the autoloader should be reset. The autoloader should be reset
55
	 * when a plugin is activating via a method other than a request, for example
56
	 * using WP-CLI. When this occurs, the activating plugin was not known when
57
	 * the autoloader selected the package versions for the classmap and filemap
58
	 * globals, so the autoloader must reselect the versions.
59
	 *
60
	 * If the current plugin is not already known, this method will add it to the
61
	 * $jetpack_autoloader_activating_plugins global.
62
	 *
63
	 * @return Boolean True if the autoloder must be reset, else false.
64
	 */
65
	public function should_autoloader_reset() {
66
		global $jetpack_autoloader_activating_plugins;
67
68
		$plugins        = $this->get_all_active_plugins();
69
		$current_plugin = $this->get_current_plugin();
70
		$plugin_unknown = ! in_array( $current_plugin, $plugins, true );
71
72
		if ( $plugin_unknown ) {
73
			// If the current plugin isn't known, add it to the activating plugins list.
74
			$jetpack_autoloader_activating_plugins[] = $current_plugin;
75
		}
76
77
		return $plugin_unknown;
78
	}
79
80
	/**
81
	 * Returns the names of activating plugins if the plugins are activating via a request.
82
	 *
83
	 * @return Array The array of the activating plugins or empty array.
84
	 */
85
	private function get_plugins_activating_via_request() {
86
87
		 // phpcs:disable WordPress.Security.NonceVerification.Recommended
88
89
		$action = isset( $_REQUEST['action'] ) ? $_REQUEST['action'] : false;
90
		$plugin = isset( $_REQUEST['plugin'] ) ? $_REQUEST['plugin'] : false;
91
		$nonce  = isset( $_REQUEST['_wpnonce'] ) ? $_REQUEST['_wpnonce'] : false;
92
93
		/**
94
		 * Note: we're not actually checking the nonce here becase it's too early
95
		 * in the execution. The pluggable functions are not yet loaded to give
96
		 * plugins a chance to plug their versions. Therefore we're doing the bare
97
		 * minimum: checking whether the nonce exists and it's in the right place.
98
		 * The request will fail later if the nonce doesn't pass the check.
99
		 */
100
101
		// In case of a single plugin activation there will be a plugin slug.
102
		if ( 'activate' === $action && ! empty( $nonce ) ) {
103
			return array( wp_unslash( $plugin ) );
104
		}
105
106
		$plugins = isset( $_REQUEST['checked'] ) ? $_REQUEST['checked'] : array();
107
108
		// In case of bulk activation there will be an array of plugins.
109
		if ( 'activate-selected' === $action && ! empty( $nonce ) ) {
110
			return array_map( 'wp_unslash', $plugins );
111
		}
112
113
		// phpcs:enable WordPress.Security.NonceVerification.Recommended
114
115
		return array();
116
	}
117
118
	/**
119
	 * Returns an array of the names of all known activating plugins. This includes
120
	 * plugins activating via a request and plugins that are activating via other
121
	 * methods.
122
	 *
123
	 * @return Array The array of all activating plugins or empty array.
124
	 */
125
	private function get_all_activating_plugins() {
126
		global $jetpack_autoloader_activating_plugins;
127
128
		$activating_plugins = $this->get_plugins_activating_via_request();
129
		return array_unique( array_merge( $activating_plugins, $jetpack_autoloader_activating_plugins ) );
130
	}
131
132
	/**
133
	 * Returns the name of the current plugin.
134
	 *
135
	 * @return String The name of the current plugin.
136
	 */
137
	public function get_current_plugin() {
138
		if ( ! function_exists( 'get_plugins' ) ) {
139
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
140
		}
141
142
		$dir  = explode( '/', plugin_basename( __FILE__ ) )[0];
143
		$file = array_keys( get_plugins( "/$dir" ) )[0];
144
		return "$dir/$file";
145
	}
146
}
147