Completed
Push — add/jetpack-assistant-ui ( a6f776...33ce41 )
by Jeremy
202:03 queued 191:10
created

Plugins_Handler::create_plugin_path()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
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\jp95016e8b7af5cfd6a3cbf90d4433a769;
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
		$activating_plugins_paths = $this->get_plugins_activating_via_request();
28
29
		return array_unique(
30
			array_merge(
31
				$active_plugins_paths,
32
				$activating_plugins_paths,
33
				$multisite_plugins_paths,
34
				(array) $jetpack_autoloader_activating_plugins_paths
35
			)
36
		);
37
	}
38
39
	/**
40
	 * Returns an array containing the paths of the active sitewide plugins in a multisite environment.
41
	 *
42
	 * @return array The paths of the active sitewide plugins or an empty array.
43
	 */
44 View Code Duplication
	protected function get_multisite_plugins_paths() {
45
		$plugin_slugs = is_multisite()
46
			? array_keys( get_site_option( 'active_sitewide_plugins', array() ) )
47
			: array();
48
49
		$plugin_slugs = array_filter( $plugin_slugs, array( $this, 'is_directory_plugin' ) );
50
		return array_map( array( $this, 'create_plugin_path' ), $plugin_slugs );
51
	}
52
53
	/**
54
	 * Returns an array containing the paths of the currently active plugins.
55
	 *
56
	 * @return array The active plugins' paths or an empty array.
57
	 */
58
	protected function get_active_plugins_paths() {
59
		$plugin_slugs = (array) get_option( 'active_plugins', array() );
60
		$plugin_slugs = array_filter( $plugin_slugs, array( $this, 'is_directory_plugin' ) );
61
		return array_map( array( $this, 'create_plugin_path' ), $plugin_slugs );
62
	}
63
64
	/**
65
	 * Adds the plugin directory from the WP_PLUGIN_DIR constant to the plugin slug.
66
	 *
67
	 * @param string $plugin_slug The plugin slug.
68
	 */
69
	private function create_plugin_path( $plugin_slug ) {
70
		$plugin_dir = str_replace( '\\', '/', WP_PLUGIN_DIR );
71
		return trailingslashit( $plugin_dir ) . substr( $plugin_slug, 0, strrpos( $plugin_slug, '/' ) );
72
	}
73
74
	/**
75
	 * Ensure the plugin has its own directory and not a single-file plugin.
76
	 *
77
	 * @param string $plugin Plugin name, may be prefixed with "/".
78
	 *
79
	 * @return bool
80
	 */
81
	public function is_directory_plugin( $plugin ) {
82
		return strlen( $plugin ) > 1 && false !== strpos( $plugin, '/', 1 );
83
	}
84
85
	/**
86
	 * Returns an array containing the names of plugins that are activating via a request.
87
	 *
88
	 * @return array An array of names of the activating plugins or an empty array.
89
	 */
90 View Code Duplication
	private function get_plugins_activating_via_request() {
91
92
		// phpcs:disable WordPress.Security.NonceVerification.Recommended
93
		// phpcs:disable WordPress.Security.ValidatedSanitizedInput.MissingUnslash
94
		// phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
95
96
		$action = isset( $_REQUEST['action'] ) ? $_REQUEST['action'] : false;
97
		$plugin = isset( $_REQUEST['plugin'] ) ? $_REQUEST['plugin'] : false;
98
		$nonce  = isset( $_REQUEST['_wpnonce'] ) ? $_REQUEST['_wpnonce'] : false;
99
100
		/**
101
		 * Note: we're not actually checking the nonce here becase it's too early
102
		 * in the execution. The pluggable functions are not yet loaded to give
103
		 * plugins a chance to plug their versions. Therefore we're doing the bare
104
		 * minimum: checking whether the nonce exists and it's in the right place.
105
		 * The request will fail later if the nonce doesn't pass the check.
106
		 */
107
108
		// In case of a single plugin activation there will be a plugin slug.
109
		if ( 'activate' === $action && ! empty( $nonce ) ) {
110
			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...
111
		}
112
113
		$plugins = isset( $_REQUEST['checked'] ) ? $_REQUEST['checked'] : array();
114
115
		// In case of bulk activation there will be an array of plugins.
116
		if ( 'activate-selected' === $action && ! empty( $nonce ) ) {
117
			$plugin_slugs = array_map( 'wp_unslash', $plugins );
118
			return array_map( array( $this, 'create_plugin_path' ), $plugin_slugs );
119
		}
120
121
		// phpcs:enable WordPress.Security.NonceVerification.Recommended
122
		// phpcs:enable WordPress.Security.ValidatedSanitizedInput.MissingUnslash
123
		// phpcs:enable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
124
		return array();
125
	}
126
127
	/**
128
	 * Returns the path of the current plugin.
129
	 *
130
	 * @return string The path of the current plugin.
131
	 */
132
	public function get_current_plugin_path() {
133
		$vendor_path = str_replace( '\\', '/', dirname( dirname( __FILE__ ) ) );
134
		// Path to the plugin's folder (the parent of the vendor/jetpack-autoloader folder).
135
		return substr( $vendor_path, 0, strrpos( $vendor_path, '/' ) );
136
	}
137
}
138