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

Plugin_Locator::find_current_plugin()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
1
<?php
2
/* HEADER */ // phpcs:ignore
3
4
/**
5
 * This class scans the WordPress installation to find active plugins.
6
 */
7
class Plugin_Locator {
8
9
	/**
10
	 * The path processor for finding plugin paths.
11
	 *
12
	 * @var Path_Processor
13
	 */
14
	private $path_processor;
15
16
	/**
17
	 * The constructor.
18
	 *
19
	 * @param Path_Processor $path_processor The Path_Processor instance.
20
	 */
21
	public function __construct( $path_processor ) {
22
		$this->path_processor = $path_processor;
23
	}
24
25
	/**
26
	 * Finds the path to the current plugin.
27
	 *
28
	 * @return string $path The path to the current plugin.
29
	 * @throws \RuntimeException If the current plugin does not have an autoloader.
30
	 */
31
	public function find_current_plugin() {
32
		// Escape from `vendor/__DIR__` to root plugin directory.
33
		$plugin_directory = dirname( dirname( __DIR__ ) );
34
35
		// Use the path processor to ensure that this is an autoloader we're referencing.
36
		$path = $this->path_processor->find_directory_with_autoloader( $plugin_directory, array() );
37
		if ( false === $path ) {
38
			throw new \RuntimeException( 'Failed to locate plugin ' . $plugin_directory );
39
		}
40
41
		return $path;
42
	}
43
44
	/**
45
	 * Checks a given option for plugin paths.
46
	 *
47
	 * @param string $option_name  The option that we want to check for plugin information.
48
	 * @param bool   $site_option  Indicates whether or not we want to check the site option.
49
	 *
50
	 * @return array $plugin_paths The list of absolute paths we've found.
51
	 */
52
	public function find_using_option( $option_name, $site_option = false ) {
53
		$raw = $site_option ? get_site_option( $option_name, array() ) : get_option( $option_name, array() );
54
		if ( empty( $raw ) ) {
55
			return array();
56
		}
57
58
		return $this->convert_plugins_to_paths( $raw );
59
	}
60
61
	/**
62
	 * Checks for plugins that are being activated in this request and returns all that it finds.
63
	 *
64
	 * @return array $plugin_paths The list of absolute paths we've found.
65
	 */
66
	public function find_activating_this_request() {
67
		// phpcs:disable WordPress.Security.NonceVerification.Recommended
68
69
		/**
70
		 * Note: we're not actually checking the nonce here because it's too early
71
		 * in the execution. The pluggable functions are not yet loaded to give
72
		 * plugins a chance to plug their versions. Therefore we're doing the bare
73
		 * minimum: checking whether the nonce exists and it's in the right place.
74
		 * The request will fail later if the nonce doesn't pass the check.
75
		 */
76
		if ( empty( $_REQUEST['_wpnonce'] ) ) {
77
			return array();
78
		}
79
80
		$plugin_slugs = array();
81
82
		$action = isset( $_REQUEST['action'] ) ? wp_unslash( $_REQUEST['action'] ) : false;
83
		switch ( $action ) {
84
			case 'activate':
85
				if ( empty( $_REQUEST['plugin'] ) ) {
86
					break;
87
				}
88
89
				$plugin_slugs[] = wp_unslash( $_REQUEST['plugin'] );
90
				break;
91
92
			case 'activate-selected':
93
				if ( empty( $_REQUEST['checked'] ) ) {
94
					break;
95
				}
96
97
				$plugin_slugs = wp_unslash( $_REQUEST['checked'] );
98
				break;
99
		}
100
101
		return $this->convert_plugins_to_paths( $plugin_slugs );
0 ignored issues
show
Documentation introduced by
$plugin_slugs is of type string|array, but the function expects a array<integer,string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
102
	}
103
104
	/**
105
	 * Given an array of plugin slugs or paths, this will convert them to absolute paths and filter
106
	 * out the plugins that are not directory plugins. Note that array keys will also be included
107
	 * if they are plugin paths!
108
	 *
109
	 * @param string[] $plugins Plugin paths or slugs to filter.
110
	 *
111
	 * @return string[]
112
	 */
113
	private function convert_plugins_to_paths( $plugins ) {
114
		// We're going to look for plugins in the standard directories.
115
		$path_constants = array( WP_PLUGIN_DIR, WPMU_PLUGIN_DIR );
116
117
		$plugin_paths = array();
118
		foreach ( $plugins as $key => $value ) {
119
			$path = $this->path_processor->find_directory_with_autoloader( $key, $path_constants );
120
			if ( $path ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $path of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
121
				$plugin_paths[] = $path;
122
			}
123
124
			$path = $this->path_processor->find_directory_with_autoloader( $value, $path_constants );
125
			if ( $path ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $path of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
126
				$plugin_paths[] = $path;
127
			}
128
		}
129
130
		return $plugin_paths;
131
	}
132
}
133