| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the WindPress package. |
| 5 |
* |
| 6 |
* (c) Joshua Gugun Siagian <suabahasa@gmail.com> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
declare (strict_types=1); |
| 12 |
namespace WindPress\WindPress\Utils; |
| 13 |
|
| 14 |
use WIND_PRESS; |
| 15 |
/** |
| 16 |
* Check plugin requirements. |
| 17 |
* |
| 18 |
* @since 3.0.0 |
| 19 |
*/ |
| 20 |
class Requirement |
| 21 |
{ |
| 22 |
/** |
| 23 |
* @var list<string> |
| 24 |
*/ |
| 25 |
protected $doesNotMeet = []; |
| 26 |
public function __construct() |
| 27 |
{ |
| 28 |
require_once \ABSPATH . 'wp-admin/includes/plugin.php'; |
| 29 |
} |
| 30 |
/** |
| 31 |
* Get the status of the requirements check. |
| 32 |
*/ |
| 33 |
public function met(): bool |
| 34 |
{ |
| 35 |
return $this->doesNotMeet === []; |
| 36 |
} |
| 37 |
/** |
| 38 |
* Check if the plugin is running on PHP version greater than or equal to |
| 39 |
* the specified version. |
| 40 |
* |
| 41 |
* @param string $minVersion The minimum PHP version required. |
| 42 |
*/ |
| 43 |
public function php(string $minVersion): self |
| 44 |
{ |
| 45 |
if (version_compare(\PHP_VERSION, $minVersion, '<')) { |
| 46 |
$this->doesNotMeet[] = sprintf('<strong>%s</strong> <code>%s</code> or higher', 'PHP', $minVersion); |
| 47 |
} |
| 48 |
return $this; |
| 49 |
} |
| 50 |
/** |
| 51 |
* Check if the plugin is running on WordPress version greater than or equal to |
| 52 |
* the specified version. |
| 53 |
* |
| 54 |
* @param string $minVersion The minimum WordPress version required. |
| 55 |
*/ |
| 56 |
public function wp(string $minVersion): self |
| 57 |
{ |
| 58 |
if (version_compare(get_bloginfo('version'), $minVersion, '<')) { |
| 59 |
$this->doesNotMeet[] = sprintf('<strong>%s</strong> <code>%s</code> or higher', 'WordPress', $minVersion); |
| 60 |
} |
| 61 |
return $this; |
| 62 |
} |
| 63 |
/** |
| 64 |
* Check if the plugin is running on multisite. |
| 65 |
* |
| 66 |
* @param bool $must Whether the plugin must be running on multisite. |
| 67 |
*/ |
| 68 |
public function multisite(bool $must): self |
| 69 |
{ |
| 70 |
if ($must && !is_multisite()) { |
| 71 |
$this->doesNotMeet[] = 'WordPress Multisite'; |
| 72 |
} |
| 73 |
return $this; |
| 74 |
} |
| 75 |
/** |
| 76 |
* Check if plugins are active and may have the minimum version specified. |
| 77 |
* |
| 78 |
* @param list<string|array{0: string, 1: string}> $plugins The plugins to check. Use the plugin file path, e.g. `windpress/windpress.php`. |
| 79 |
*/ |
| 80 |
public function plugins(array $plugins): self |
| 81 |
{ |
| 82 |
foreach ($plugins as $k => $v) { |
| 83 |
if (is_int($k)) { |
| 84 |
if (!is_plugin_active($v)) { |
| 85 |
$pluginName = get_plugin_data(\WP_PLUGIN_DIR . '/' . $v)['Name']; |
| 86 |
$this->doesNotMeet[] = sprintf('<strong>%s</strong>', $pluginName); |
| 87 |
} |
| 88 |
} else { |
| 89 |
$pluginName = get_plugin_data(\WP_PLUGIN_DIR . '/' . $k)['Name']; |
| 90 |
if (!is_plugin_active($k)) { |
| 91 |
$this->doesNotMeet[] = sprintf('<strong>%s</strong>', $pluginName); |
| 92 |
} elseif (version_compare(get_plugin_data(\WP_PLUGIN_DIR . '/' . $k)['Version'], $v, '<')) { |
| 93 |
$this->doesNotMeet[] = sprintf('<strong>%s</strong> <code>%s</code> or higher', $pluginName, $v); |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
return $this; |
| 98 |
} |
| 99 |
/** |
| 100 |
* Check if the site is using the specified theme. |
| 101 |
* |
| 102 |
* @param string $parentTheme The parent theme to check. |
| 103 |
* @param string|null $version The minimum version of the parent theme. |
| 104 |
*/ |
| 105 |
public function theme(string $parentTheme, ?string $version = null): self |
| 106 |
{ |
| 107 |
$theme = wp_get_theme(); |
| 108 |
if (get_template() !== $parentTheme) { |
| 109 |
$this->doesNotMeet[] = sprintf('<strong>%s</strong>', $parentTheme); |
| 110 |
} elseif ($version && version_compare(($theme->parent() ?: $theme)->get('Version'), $version, '<')) { |
| 111 |
$this->doesNotMeet[] = sprintf('<strong>%s</strong> <code>%s</code> or higher', $parentTheme, $version); |
| 112 |
} |
| 113 |
return $this; |
| 114 |
} |
| 115 |
public function printNotice() |
| 116 |
{ |
| 117 |
$name = esc_html(get_plugin_data(WIND_PRESS::FILE, \false)['Name']); |
| 118 |
if (!current_user_can('activate_plugins')) { |
| 119 |
return; |
| 120 |
} |
| 121 |
$notice = sprintf( |
| 122 |
/* translators: 1: plugin name, 2: list of requirements */ |
| 123 |
esc_html__('The %1$s plugin minimum requirements are not met:', 'windpress'), |
| 124 |
'<strong>' . $name . '</strong>' |
| 125 |
); |
| 126 |
$requirements = count($this->doesNotMeet) === 0 ? '' : '<ul><li>' . implode('</li><li>', $this->doesNotMeet) . '</li></ul>'; |
| 127 |
printf('<div class="notice notice-error"><p>%1$s</p>%2$s</div>', wp_kses_post($notice), wp_kses_post($requirements)); |
| 128 |
} |
| 129 |
} |
| 130 |
|