PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 8.8
Jetpack – WP Security, Backup, Speed, & Growth v8.8
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / vendor / class-plugins-handler.php

class-plugins-handler.php in Jetpack – WP Security, Backup, Speed, & Growth 8.8, at vendor/class-plugins-handler.php

159 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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\jpf07841f6f4aa8fb73b701bb0850cd730;
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 ) ) );
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