| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class WordPress\Plugin_Check\Utilities |
| 4 |
* |
| 5 |
* @package plugin-check |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WordPress\Plugin_Check\Utilities; |
| 9 |
|
| 10 |
use Exception; |
| 11 |
use WordPress\Plugin_Check\Checker\Abstract_Check_Runner; |
| 12 |
use WordPress\Plugin_Check\Checker\AJAX_Runner; |
| 13 |
use WordPress\Plugin_Check\Checker\CLI_Runner; |
| 14 |
|
| 15 |
/** |
| 16 |
* Class providing utility methods to return plugin information based on the request. |
| 17 |
* |
| 18 |
* @since 1.0.0 |
| 19 |
*/ |
| 20 |
class Plugin_Request_Utility { |
| 21 |
|
| 22 |
/** |
| 23 |
* Instance of the current runner based on the request. |
| 24 |
* |
| 25 |
* @since 1.0.0 |
| 26 |
* @var Abstract_Check_Runner|null |
| 27 |
*/ |
| 28 |
protected static $runner; |
| 29 |
|
| 30 |
/** |
| 31 |
* The universal runtime preparation cleanups if applied. |
| 32 |
* |
| 33 |
* @since 1.0.0 |
| 34 |
* @var callable|null |
| 35 |
*/ |
| 36 |
protected static $cleanup; |
| 37 |
|
| 38 |
/** |
| 39 |
* Returns the plugin basename based on the input provided. |
| 40 |
* |
| 41 |
* @since 1.0.0 |
| 42 |
* |
| 43 |
* @param string $plugin_slug The plugin slug or basename. |
| 44 |
* @return string The plugin basename. |
| 45 |
* |
| 46 |
* @throws Exception Thrown if an invalid basename or plugin slug is provided. |
| 47 |
*/ |
| 48 |
public static function get_plugin_basename_from_input( $plugin_slug ) { |
| 49 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 50 |
|
| 51 |
if ( empty( $plugin_slug ) ) { |
| 52 |
throw new Exception( 'Invalid plugin slug: Plugin slug must not be empty.' ); |
| 53 |
} |
| 54 |
|
| 55 |
$plugins = get_plugins(); |
| 56 |
|
| 57 |
// Is the provided value is a full plugin basename? |
| 58 |
if ( isset( $plugins[ $plugin_slug ] ) ) { |
| 59 |
return $plugin_slug; |
| 60 |
} |
| 61 |
|
| 62 |
if ( strpos( $plugin_slug, '/' ) ) { |
| 63 |
throw new Exception( |
| 64 |
sprintf( |
| 65 |
'Invalid plugin basename: Plugin with basename %s is not installed.', |
| 66 |
$plugin_slug |
| 67 |
) |
| 68 |
); |
| 69 |
} |
| 70 |
|
| 71 |
foreach ( array_keys( $plugins ) as $plugin_basename ) { |
| 72 |
if ( strpos( $plugin_basename, $plugin_slug . '/' ) === 0 ) { |
| 73 |
return $plugin_basename; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
throw new Exception( |
| 78 |
sprintf( |
| 79 |
'Invalid plugin slug: Plugin with slug %s is not installed.', |
| 80 |
$plugin_slug |
| 81 |
) |
| 82 |
); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Initializes the runner classes. |
| 87 |
* |
| 88 |
* @since 1.0.0 |
| 89 |
*/ |
| 90 |
public static function initialize_runner() { |
| 91 |
$runners = array( |
| 92 |
new CLI_Runner(), |
| 93 |
new AJAX_Runner(), |
| 94 |
); |
| 95 |
|
| 96 |
foreach ( $runners as $runner ) { |
| 97 |
if ( $runner->is_plugin_check() ) { |
| 98 |
add_action( |
| 99 |
'muplugins_loaded', |
| 100 |
function () use ( $runner ) { |
| 101 |
static::$cleanup = $runner->prepare(); |
| 102 |
static::$runner = $runner; |
| 103 |
} |
| 104 |
); |
| 105 |
|
| 106 |
break; |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Get the Runner class for the current request. |
| 113 |
* |
| 114 |
* @since 1.0.0 |
| 115 |
* |
| 116 |
* @return Abstract_Check_Runner|null The Runner class for the request or null. |
| 117 |
*/ |
| 118 |
public static function get_runner() { |
| 119 |
if ( null !== static::$runner ) { |
| 120 |
return static::$runner; |
| 121 |
} |
| 122 |
|
| 123 |
return null; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Runs the cleanup functions and destroys the runner. |
| 128 |
* |
| 129 |
* @since 1.0.0 |
| 130 |
*/ |
| 131 |
public static function destroy_runner() { |
| 132 |
// Run the cleanup functions. |
| 133 |
if ( null !== self::$cleanup ) { |
| 134 |
call_user_func( self::$cleanup ); |
| 135 |
} |
| 136 |
|
| 137 |
static::$runner = null; |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Gets the directories to ignore using the filter. |
| 142 |
* |
| 143 |
* @since 1.0.0 |
| 144 |
*/ |
| 145 |
public static function get_directories_to_ignore() { |
| 146 |
// By default, ignore the '.git', 'vendor' and 'node_modules' directories. |
| 147 |
$default_ignore_directories = array( |
| 148 |
'.git', |
| 149 |
'vendor', |
| 150 |
'node_modules', |
| 151 |
); |
| 152 |
|
| 153 |
/** |
| 154 |
* Filters the directories to ignore. |
| 155 |
* |
| 156 |
* @since 1.0.0 |
| 157 |
* |
| 158 |
* @param array $default_ignore_directories An array of directories to ignore. |
| 159 |
*/ |
| 160 |
$directories_to_ignore = (array) apply_filters( 'wp_plugin_check_ignore_directories', $default_ignore_directories ); |
| 161 |
|
| 162 |
return $directories_to_ignore; |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Gets the files to ignore using the filter. |
| 167 |
* |
| 168 |
* @since 1.0.2 |
| 169 |
*/ |
| 170 |
public static function get_files_to_ignore() { |
| 171 |
$default_ignore_files = array(); |
| 172 |
|
| 173 |
/** |
| 174 |
* Filters the files to ignore. |
| 175 |
* |
| 176 |
* @since 1.0.2 |
| 177 |
* |
| 178 |
* @param array $default_ignore_files An array of files to ignore. |
| 179 |
*/ |
| 180 |
$files_to_ignore = (array) apply_filters( 'wp_plugin_check_ignore_files', $default_ignore_files ); |
| 181 |
|
| 182 |
return $files_to_ignore; |
| 183 |
} |
| 184 |
} |
| 185 |
|