Completed
Push — fix/autoloader_remove_unneeded... ( ce2eba )
by
unknown
06:46
created

Plugins_Handler::create_map_path_array()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
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
	 * @param bool $skip_single_file_plugins If true, plugins with no dedicated directories will be skipped.
14
	 *
15
	 * @return Array An array of plugin names as strings.
16
	 */
17
	public function get_all_active_plugins( $skip_single_file_plugins = true ) {
18
		$active_plugins = array_merge(
19
			is_multisite()
20
				? array_keys( get_site_option( 'active_sitewide_plugins', array() ) )
21
				: array(),
22
			(array) get_option( 'active_plugins', array() )
23
		);
24
25
		$plugins = array_unique( array_merge( $active_plugins, $this->get_all_activating_plugins() ) );
26
27
		if ( $skip_single_file_plugins ) {
28
			$plugins = array_filter( $plugins, array( $this, 'is_directory_plugin' ) );
29
		}
30
31
		return $plugins;
32
	}
33
34
	/**
35
	 * Ensure the plugin has its own directory and not a single-file plugin.
36
	 *
37
	 * @param string $plugin Plugin name, may be prefixed with "/".
38
	 *
39
	 * @return bool
40
	 */
41
	public function is_directory_plugin( $plugin ) {
42
		return false !== strpos( $plugin, '/', 1 );
43
	}
44
45
	/**
46
	 * Checks whether the autoloader should be reset. The autoloader should be reset
47
	 * when a plugin is activating via a method other than a request, for example
48
	 * using WP-CLI. When this occurs, the activating plugin was not known when
49
	 * the autoloader selected the package versions for the classmap and filemap
50
	 * globals, so the autoloader must reselect the versions.
51
	 *
52
	 * If the current plugin is not already known, this method will add it to the
53
	 * $jetpack_autoloader_activating_plugins global.
54
	 *
55
	 * @return Boolean True if the autoloder must be reset, else false.
56
	 */
57
	public function should_autoloader_reset() {
58
		global $jetpack_autoloader_activating_plugins;
59
60
		$plugins        = $this->get_all_active_plugins();
61
		$current_plugin = $this->get_current_plugin();
62
		$plugin_unknown = ! in_array( $current_plugin, $plugins, true );
63
64
		if ( $plugin_unknown ) {
65
			// If the current plugin isn't known, add it to the activating plugins list.
66
			$jetpack_autoloader_activating_plugins[] = $current_plugin;
67
		}
68
69
		return $plugin_unknown;
70
	}
71
72
	/**
73
	 * Returns the names of activating plugins if the plugins are activating via a request.
74
	 *
75
	 * @return Array The array of the activating plugins or empty array.
76
	 */
77
	private function get_plugins_activating_via_request() {
78
79
		 // phpcs:disable WordPress.Security.NonceVerification.Recommended
80
81
		$action = isset( $_REQUEST['action'] ) ? $_REQUEST['action'] : false;
82
		$plugin = isset( $_REQUEST['plugin'] ) ? $_REQUEST['plugin'] : false;
83
		$nonce  = isset( $_REQUEST['_wpnonce'] ) ? $_REQUEST['_wpnonce'] : false;
84
85
		/**
86
		 * Note: we're not actually checking the nonce here becase it's too early
87
		 * in the execution. The pluggable functions are not yet loaded to give
88
		 * plugins a chance to plug their versions. Therefore we're doing the bare
89
		 * minimum: checking whether the nonce exists and it's in the right place.
90
		 * The request will fail later if the nonce doesn't pass the check.
91
		 */
92
93
		// In case of a single plugin activation there will be a plugin slug.
94
		if ( 'activate' === $action && ! empty( $nonce ) ) {
95
			return array( wp_unslash( $plugin ) );
96
		}
97
98
		$plugins = isset( $_REQUEST['checked'] ) ? $_REQUEST['checked'] : array();
99
100
		// In case of bulk activation there will be an array of plugins.
101
		if ( 'activate-selected' === $action && ! empty( $nonce ) ) {
102
			return array_map( 'wp_unslash', $plugins );
103
		}
104
105
		// phpcs:enable WordPress.Security.NonceVerification.Recommended
106
107
		return array();
108
	}
109
110
	/**
111
	 * Returns an array of the names of all known activating plugins. This includes
112
	 * plugins activating via a request and plugins that are activating via other
113
	 * methods.
114
	 *
115
	 * @return Array The array of all activating plugins or empty array.
116
	 */
117
	private function get_all_activating_plugins() {
118
		global $jetpack_autoloader_activating_plugins;
119
120
		$activating_plugins = $this->get_plugins_activating_via_request();
121
		return array_unique( array_merge( $activating_plugins, $jetpack_autoloader_activating_plugins ) );
122
	}
123
124
	/**
125
	 * Returns the name of the current plugin.
126
	 *
127
	 * @return String The name of the current plugin.
128
	 */
129
	public function get_current_plugin() {
130
		if ( ! function_exists( 'get_plugins' ) ) {
131
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
132
		}
133
134
		$dir  = explode( '/', plugin_basename( __FILE__ ) )[0];
135
		$file = array_keys( get_plugins( "/$dir" ) )[0];
136
		return "$dir/$file";
137
	}
138
}
139