← All changes
|
vendor/jetpack-autoloader/class-plugin-locator.php
+153
-0
16.2-beta
→
16.3
View file →
| @@ -1,0 +1,153 @@ | ||
| 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\jpf11009ded9fc4592b6a05b61ce272b3c_jetpackⓥ16_3\al6_0_1; | |
| 9 | + | |
| 10 | + // phpcs:ignore | |
| 11 | + | |
| 12 | +/** | |
| 13 | + * This class scans the WordPress installation to find active plugins. | |
| 14 | + */ | |
| 15 | +class Plugin_Locator { | |
| 16 | + | |
| 17 | + /** | |
| 18 | + * The path processor for finding plugin paths. | |
| 19 | + * | |
| 20 | + * @var Path_Processor | |
| 21 | + */ | |
| 22 | + private $path_processor; | |
| 23 | + | |
| 24 | + /** | |
| 25 | + * The constructor. | |
| 26 | + * | |
| 27 | + * @param Path_Processor $path_processor The Path_Processor instance. | |
| 28 | + */ | |
| 29 | + public function __construct( $path_processor ) { | |
| 30 | + $this->path_processor = $path_processor; | |
| 31 | + } | |
| 32 | + | |
| 33 | + /** | |
| 34 | + * Finds the path to the current plugin. | |
| 35 | + * | |
| 36 | + * @return string $path The path to the current plugin. | |
| 37 | + * | |
| 38 | + * @throws \RuntimeException If the current plugin does not have an autoloader. | |
| 39 | + */ | |
| 40 | + public function find_current_plugin() { | |
| 41 | + // Escape from `vendor/__DIR__` to root plugin directory. | |
| 42 | + $plugin_directory = dirname( __DIR__, 2 ); | |
| 43 | + | |
| 44 | + // Use the path processor to ensure that this is an autoloader we're referencing. | |
| 45 | + $path = $this->path_processor->find_directory_with_autoloader( $plugin_directory, array() ); | |
| 46 | + if ( false === $path ) { | |
| 47 | + throw new \RuntimeException( 'Failed to locate plugin ' . $plugin_directory ); | |
| 48 | + } | |
| 49 | + | |
| 50 | + return $path; | |
| 51 | + } | |
| 52 | + | |
| 53 | + /** | |
| 54 | + * Checks a given option for plugin paths. | |
| 55 | + * | |
| 56 | + * @param string $option_name The option that we want to check for plugin information. | |
| 57 | + * @param bool $site_option Indicates whether or not we want to check the site option. | |
| 58 | + * | |
| 59 | + * @return array $plugin_paths The list of absolute paths we've found. | |
| 60 | + */ | |
| 61 | + public function find_using_option( $option_name, $site_option = false ) { | |
| 62 | + $raw = $site_option ? get_site_option( $option_name ) : get_option( $option_name ); | |
| 63 | + if ( false === $raw ) { | |
| 64 | + return array(); | |
| 65 | + } | |
| 66 | + | |
| 67 | + return $this->convert_plugins_to_paths( $raw ); | |
| 68 | + } | |
| 69 | + | |
| 70 | + /** | |
| 71 | + * Checks for plugins in the `action` request parameter. | |
| 72 | + * | |
| 73 | + * @param string[] $allowed_actions The actions that we're allowed to return plugins for. | |
| 74 | + * | |
| 75 | + * @return array $plugin_paths The list of absolute paths we've found. | |
| 76 | + */ | |
| 77 | + public function find_using_request_action( $allowed_actions ) { | |
| 78 | + /** | |
| 79 | + * Note: we're not actually checking the nonce here because it's too early | |
| 80 | + * in the execution. The pluggable functions are not yet loaded to give | |
| 81 | + * plugins a chance to plug their versions. Therefore we're doing the bare | |
| 82 | + * minimum: checking whether the nonce exists and it's in the right place. | |
| 83 | + * The request will fail later if the nonce doesn't pass the check. | |
| 84 | + */ | |
| 85 | + if ( empty( $_REQUEST['_wpnonce'] ) ) { | |
| 86 | + return array(); | |
| 87 | + } | |
| 88 | + | |
| 89 | + // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Validated just below. | |
| 90 | + $action = isset( $_REQUEST['action'] ) ? wp_unslash( $_REQUEST['action'] ) : false; | |
| 91 | + if ( ! in_array( $action, $allowed_actions, true ) ) { | |
| 92 | + return array(); | |
| 93 | + } | |
| 94 | + | |
| 95 | + $plugin_slugs = array(); | |
| 96 | + switch ( $action ) { | |
| 97 | + case 'activate': | |
| 98 | + case 'deactivate': | |
| 99 | + if ( empty( $_REQUEST['plugin'] ) ) { | |
| 100 | + break; | |
| 101 | + } | |
| 102 | + | |
| 103 | + // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Validated by convert_plugins_to_paths. | |
| 104 | + $plugin_slugs[] = wp_unslash( $_REQUEST['plugin'] ); | |
| 105 | + break; | |
| 106 | + | |
| 107 | + case 'activate-selected': | |
| 108 | + case 'deactivate-selected': | |
| 109 | + if ( empty( $_REQUEST['checked'] ) ) { | |
| 110 | + break; | |
| 111 | + } | |
| 112 | + | |
| 113 | + // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Validated by convert_plugins_to_paths. | |
| 114 | + $plugin_slugs = wp_unslash( $_REQUEST['checked'] ); | |
| 115 | + break; | |
| 116 | + } | |
| 117 | + | |
| 118 | + return $this->convert_plugins_to_paths( $plugin_slugs ); | |
| 119 | + } | |
| 120 | + | |
| 121 | + /** | |
| 122 | + * Given an array of plugin slugs or paths, this will convert them to absolute paths and filter | |
| 123 | + * out the plugins that are not directory plugins. Note that array keys will also be included | |
| 124 | + * if they are plugin paths! | |
| 125 | + * | |
| 126 | + * @param string[] $plugins Plugin paths or slugs to filter. | |
| 127 | + * | |
| 128 | + * @return string[] | |
| 129 | + */ | |
| 130 | + private function convert_plugins_to_paths( $plugins ) { | |
| 131 | + if ( ! is_array( $plugins ) || empty( $plugins ) ) { | |
| 132 | + return array(); | |
| 133 | + } | |
| 134 | + | |
| 135 | + // We're going to look for plugins in the standard directories. | |
| 136 | + $path_constants = array( WP_PLUGIN_DIR, WPMU_PLUGIN_DIR ); | |
| 137 | + | |
| 138 | + $plugin_paths = array(); | |
| 139 | + foreach ( $plugins as $key => $value ) { | |
| 140 | + $path = $this->path_processor->find_directory_with_autoloader( $key, $path_constants ); | |
| 141 | + if ( $path ) { | |
| 142 | + $plugin_paths[] = $path; | |
| 143 | + } | |
| 144 | + | |
| 145 | + $path = $this->path_processor->find_directory_with_autoloader( $value, $path_constants ); | |
| 146 | + if ( $path ) { | |
| 147 | + $plugin_paths[] = $path; | |
| 148 | + } | |
| 149 | + } | |
| 150 | + | |
| 151 | + return $plugin_paths; | |
| 152 | + } | |
| 153 | +} | |