| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class WordPress\Plugin_Check\Plugin_Context |
| 4 |
* |
| 5 |
* @package plugin-check |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WordPress\Plugin_Check; |
| 9 |
|
| 10 |
use Exception; |
| 11 |
use WordPress\Plugin_Check\Traits\Find_Readme; |
| 12 |
use WordPressdotorg\Plugin_Directory\Readme\Parser; |
| 13 |
use function WP_CLI\Utils\normalize_path; |
| 14 |
|
| 15 |
/** |
| 16 |
* Class representing the context in which the plugin is running. |
| 17 |
* |
| 18 |
* @since 1.0.0 |
| 19 |
*/ |
| 20 |
class Plugin_Context { |
| 21 |
|
| 22 |
use Find_Readme; |
| 23 |
|
| 24 |
/** |
| 25 |
* Absolute path of the plugin main file. |
| 26 |
* |
| 27 |
* @since 1.0.0 |
| 28 |
* @var string |
| 29 |
*/ |
| 30 |
protected $main_file; |
| 31 |
|
| 32 |
/** |
| 33 |
* The minimum supported WordPress version of the plugin. |
| 34 |
* |
| 35 |
* @since 1.0.0 |
| 36 |
* @var string |
| 37 |
*/ |
| 38 |
protected $minimum_supported_wp; |
| 39 |
|
| 40 |
/** |
| 41 |
* Constructor. |
| 42 |
* |
| 43 |
* @since 1.0.0 |
| 44 |
* |
| 45 |
* @param string $main_file The absolute path to the plugin main file. |
| 46 |
* |
| 47 |
* @throws Exception Throws exception if not called via regular WP-CLI or WordPress bootstrap order. |
| 48 |
*/ |
| 49 |
public function __construct( $main_file ) { |
| 50 |
if ( function_exists( 'wp_normalize_path' ) ) { |
| 51 |
$this->main_file = wp_normalize_path( $main_file ); |
| 52 |
} elseif ( function_exists( '\WP_CLI\Utils\normalize_path' ) ) { |
| 53 |
$this->main_file = normalize_path( $main_file ); |
| 54 |
} else { |
| 55 |
throw new Exception( |
| 56 |
__( 'Unknown environment, normalize_path function not found', 'plugin-check' ) |
| 57 |
); |
| 58 |
} |
| 59 |
|
| 60 |
if ( false === strpos( $this->main_file, '.php' ) ) { |
| 61 |
$files = glob( $this->main_file . '/*.php' ); |
| 62 |
foreach ( $files as $file ) { |
| 63 |
$plugin_data = get_plugin_data( $file ); |
| 64 |
if ( ! empty( $plugin_data['Name'] ) ) { |
| 65 |
$this->main_file = $file; |
| 66 |
break; |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Returns the plugin basename. |
| 74 |
* |
| 75 |
* @since 1.0.0 |
| 76 |
* |
| 77 |
* @return string Plugin basename. |
| 78 |
*/ |
| 79 |
public function basename() { |
| 80 |
return plugin_basename( $this->main_file ); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Returns the plugin main file. |
| 85 |
* |
| 86 |
* @since 1.0.2 |
| 87 |
* |
| 88 |
* @return string Plugin main file. |
| 89 |
*/ |
| 90 |
public function main_file() { |
| 91 |
return $this->main_file; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Returns the absolute path for a relative path to the plugin directory. |
| 96 |
* |
| 97 |
* @since 1.0.0 |
| 98 |
* |
| 99 |
* @param string $relative_path Optional. Relative path. Default '/'. |
| 100 |
* @return string Absolute path. |
| 101 |
*/ |
| 102 |
public function path( $relative_path = '/' ) { |
| 103 |
if ( is_dir( $this->main_file ) ) { |
| 104 |
return trailingslashit( $this->main_file ) . ltrim( $relative_path, '/' ); |
| 105 |
} else { |
| 106 |
return plugin_dir_path( $this->main_file ) . ltrim( $relative_path, '/' ); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Returns the full URL for a path relative to the plugin directory. |
| 112 |
* |
| 113 |
* @since 1.0.0 |
| 114 |
* |
| 115 |
* @param string $relative_path Optional. Relative path. Default '/'. |
| 116 |
* @return string Full URL. |
| 117 |
*/ |
| 118 |
public function url( $relative_path = '/' ) { |
| 119 |
return plugin_dir_url( $this->main_file ) . ltrim( $relative_path, '/' ); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Returns the plugin location. |
| 124 |
* |
| 125 |
* @since 1.0.0 |
| 126 |
* |
| 127 |
* @return string The plugin file if single file plugin. Or the plugin folder. |
| 128 |
*/ |
| 129 |
public function location() { |
| 130 |
$path = $this->path(); |
| 131 |
|
| 132 |
// Return the plugin path and basename if the path matches the plugin directory. |
| 133 |
if ( WP_PLUGIN_DIR . '/' === $path ) { |
| 134 |
return $path . $this->basename(); |
| 135 |
} |
| 136 |
|
| 137 |
return $path; |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Checks if the plugin is a single file plugin without a dedicated directory. |
| 142 |
* |
| 143 |
* This is the case when the single file is directly placed within `wp-content/plugins`. |
| 144 |
* |
| 145 |
* @since 1.0.0 |
| 146 |
* |
| 147 |
* @return bool true if the single file plugin, otherwise false. |
| 148 |
*/ |
| 149 |
public function is_single_file_plugin() { |
| 150 |
return $this->path() !== $this->location(); |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Determine the minimum supported WordPress version of the plugin. |
| 155 |
* |
| 156 |
* @since 1.0.0 |
| 157 |
* |
| 158 |
* @return string The minimum version supported, or empty string if unknown. |
| 159 |
*/ |
| 160 |
public function minimum_supported_wp() { |
| 161 |
if ( ! is_null( $this->minimum_supported_wp ) ) { |
| 162 |
return $this->minimum_supported_wp; |
| 163 |
} |
| 164 |
|
| 165 |
$this->minimum_supported_wp = ''; |
| 166 |
|
| 167 |
$headers = get_plugin_data( $this->main_file ); |
| 168 |
if ( ! empty( $headers['RequiresWP'] ) ) { |
| 169 |
$this->minimum_supported_wp = $headers['RequiresWP']; |
| 170 |
} elseif ( ! $this->is_single_file_plugin() ) { |
| 171 |
// Look for the readme. |
| 172 |
$readme_files = glob( $this->path() . '*' ); |
| 173 |
$readme_files = $this->filter_files_for_readme( $readme_files, $this->path() ); |
| 174 |
$readme_file = reset( $readme_files ); |
| 175 |
if ( $readme_file ) { |
| 176 |
$parser = new Parser( $readme_file ); |
| 177 |
|
| 178 |
if ( ! empty( $parser->requires ) ) { |
| 179 |
$this->minimum_supported_wp = $parser->requires; |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
return $this->minimum_supported_wp; |
| 185 |
} |
| 186 |
} |
| 187 |
|