Completed
Push — update/show-recurring-payments... ( 106a5e...b030b6 )
by
unknown
415:44 queued 407:29
created

get_plugins_activating_via_request()   B

Complexity

Conditions 9
Paths 40

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
nc 40
nop 0
dl 0
loc 32
rs 8.0555
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file was automatically generated by automattic/jetpack-autoloader.
4
 *
5
 * @package automattic/jetpack-autoloader
6
 */
7
8
namespace Automattic\Jetpack\Autoloader\jp02f167abb8e9ed34472e5bd963af3d06;
9
10
 // phpcs:ignore
11
12
/**
13
 * This class provides information about the current plugin and the site's active plugins.
14
 */
15
class Plugins_Handler {
16
17
	/**
18
	 * Returns an array containing the paths of all active plugins and all known activating plugins.
19
	 *
20
	 * @return array An array of plugin paths as strings or an empty array.
21
	 */
22
	public function get_all_active_plugins_paths() {
23
		global $jetpack_autoloader_activating_plugins_paths;
24
25
		$active_plugins_paths    = $this->get_active_plugins_paths();
26
		$multisite_plugins_paths = $this->get_multisite_plugins_paths();
27
		$active_plugins_paths    = array_merge( $multisite_plugins_paths, $active_plugins_paths );
28
29
		$activating_plugins_paths = $this->get_plugins_activating_via_request();
30
		$activating_plugins_paths = array_unique( array_merge( $activating_plugins_paths, $jetpack_autoloader_activating_plugins_paths ) );
31
32
		$plugins_paths = array_unique( array_merge( $active_plugins_paths, $activating_plugins_paths ) );
33
34
		return $plugins_paths;
35
	}
36
37
	/**
38
	 * Returns an array containing the paths of the active sitewide plugins in a multisite environment.
39
	 *
40
	 * @return array The paths of the active sitewide plugins or an empty array.
41
	 */
42
	protected function get_multisite_plugins_paths() {
43
		$plugin_slugs = is_multisite()
44
			? array_keys( get_site_option( 'active_sitewide_plugins', array() ) )
45
			: array();
46
47
		$plugin_slugs = array_filter( $plugin_slugs, array( $this, 'is_directory_plugin' ) );
48
		return array_map( array( $this, 'create_plugin_path' ), $plugin_slugs );
49
	}
50
51
	/**
52
	 * Returns an array containing the paths of the currently active plugins.
53
	 *
54
	 * @return array The active plugins' paths or an empty array.
55
	 */
56
	protected function get_active_plugins_paths() {
57
		$plugin_slugs = (array) get_option( 'active_plugins', array() );
58
		$plugin_slugs = array_filter( $plugin_slugs, array( $this, 'is_directory_plugin' ) );
59
		return array_map( array( $this, 'create_plugin_path' ), $plugin_slugs );
60
	}
61
62
	/**
63
	 * Adds the plugin directory from the WP_PLUGIN_DIR constant to the plugin slug.
64
	 *
65
	 * @param string $plugin_slug The plugin slug.
66
	 */
67
	private function create_plugin_path( $plugin_slug ) {
68
		$plugin_dir = str_replace( '\\', '/', WP_PLUGIN_DIR );
69
		return trailingslashit( $plugin_dir ) . substr( $plugin_slug, 0, strrpos( $plugin_slug, '/' ) );
70
	}
71
72
	/**
73
	 * Ensure the plugin has its own directory and not a single-file plugin.
74
	 *
75
	 * @param string $plugin Plugin name, may be prefixed with "/".
76
	 *
77
	 * @return bool
78
	 */
79
	public function is_directory_plugin( $plugin ) {
80
		return strlen( $plugin ) > 1 && false !== strpos( $plugin, '/', 1 );
81
	}
82
83
	/**
84
	 * Checks whether the autoloader should be reset. The autoloader should be reset
85
	 * when a plugin is activating via a method other than a request, for example
86
	 * using WP-CLI. When this occurs, the activating plugin was not known when
87
	 * the autoloader selected the package versions for the classmap and filemap
88
	 * globals, so the autoloader must reselect the versions.
89
	 *
90
	 * If the current plugin is not already known, this method will add it to the
91
	 * $jetpack_autoloader_activating_plugins_paths global.
92
	 *
93
	 * @return boolean True if the autoloder must be reset, else false.
94
	 */
95
	public function should_autoloader_reset() {
96
		global $jetpack_autoloader_activating_plugins_paths;
97
98
		$plugins_paths       = $this->get_all_active_plugins_paths();
99
		$current_plugin_path = $this->get_current_plugin_path();
100
		$plugin_unknown      = ! in_array( $current_plugin_path, $plugins_paths, true );
101
102
		if ( $plugin_unknown ) {
103
			// If the current plugin isn't known, add it to the activating plugins list.
104
			$jetpack_autoloader_activating_plugins_paths[] = $current_plugin_path;
105
		}
106
107
		return $plugin_unknown;
108
	}
109
110
	/**
111
	 * Returns an array containing the names of plugins that are activating via a request.
112
	 *
113
	 * @return array An array of names of the activating plugins or an empty array.
114
	 */
115
	private function get_plugins_activating_via_request() {
116
117
		 // phpcs:disable WordPress.Security.NonceVerification.Recommended
118
119
		$action = isset( $_REQUEST['action'] ) ? $_REQUEST['action'] : false;
120
		$plugin = isset( $_REQUEST['plugin'] ) ? $_REQUEST['plugin'] : false;
121
		$nonce  = isset( $_REQUEST['_wpnonce'] ) ? $_REQUEST['_wpnonce'] : false;
122
123
		/**
124
		 * Note: we're not actually checking the nonce here becase it's too early
125
		 * in the execution. The pluggable functions are not yet loaded to give
126
		 * plugins a chance to plug their versions. Therefore we're doing the bare
127
		 * minimum: checking whether the nonce exists and it's in the right place.
128
		 * The request will fail later if the nonce doesn't pass the check.
129
		 */
130
131
		// In case of a single plugin activation there will be a plugin slug.
132
		if ( 'activate' === $action && ! empty( $nonce ) ) {
133
			return array( $this->create_plugin_path( wp_unslash( $plugin ) ) );
0 ignored issues
show
Bug introduced by
It seems like wp_unslash($plugin) targeting wp_unslash() can also be of type array<integer,string>; however, Automattic\Jetpack\Autol...r::create_plugin_path() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
134
		}
135
136
		$plugins = isset( $_REQUEST['checked'] ) ? $_REQUEST['checked'] : array();
137
138
		// In case of bulk activation there will be an array of plugins.
139
		if ( 'activate-selected' === $action && ! empty( $nonce ) ) {
140
			$plugin_slugs = array_map( 'wp_unslash', $plugins );
141
			return array_map( array( $this, 'create_plugin_path' ), $plugin_slugs );
142
		}
143
144
		// phpcs:enable WordPress.Security.NonceVerification.Recommended
145
		return array();
146
	}
147
148
	/**
149
	 * Returns the path of the current plugin.
150
	 *
151
	 * @return string The path of the current plugin.
152
	 */
153
	public function get_current_plugin_path() {
154
		$vendor_path = str_replace( '\\', '/', dirname( __FILE__ ) );
155
		// Path to the plugin's folder (the parent of the vendor folder).
156
		return substr( $vendor_path, 0, strrpos( $vendor_path, '/' ) );
157
	}
158
}
159