| 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 Exception; |
| 15 |
use WIND_PRESS; |
| 16 |
use WindPress\WindPress\Plugin; |
| 17 |
/** |
| 18 |
* Common utility functions for the plugin. |
| 19 |
* |
| 20 |
* @since 3.0.0 |
| 21 |
*/ |
| 22 |
class Common |
| 23 |
{ |
| 24 |
/** |
| 25 |
* Check the type of the current request. |
| 26 |
* |
| 27 |
* @param string $type The type of the request. Available values: `admin`, `ajax`, `frontend`, `rest`, `cron`. |
| 28 |
*/ |
| 29 |
public static function is_request(string $type): bool |
| 30 |
{ |
| 31 |
switch ($type) { |
| 32 |
case 'admin': |
| 33 |
return is_admin(); |
| 34 |
case 'ajax': |
| 35 |
return wp_doing_ajax(); |
| 36 |
case 'rest': |
| 37 |
return defined('REST_REQUEST') && \REST_REQUEST; |
| 38 |
case 'cron': |
| 39 |
return wp_doing_cron(); |
| 40 |
case 'json': |
| 41 |
return wp_is_json_request(); |
| 42 |
case 'xmlrpc': |
| 43 |
return defined('XMLRPC_REQUEST') && \XMLRPC_REQUEST; |
| 44 |
case 'xml': |
| 45 |
return wp_is_xml_request(); |
| 46 |
case 'frontend': |
| 47 |
return (!is_admin() || wp_doing_ajax()) && !wp_doing_cron(); |
| 48 |
default: |
| 49 |
return \false; |
| 50 |
} |
| 51 |
} |
| 52 |
/** |
| 53 |
* Get the plugin's data. |
| 54 |
* |
| 55 |
* @param string $key The key to retrieve. |
| 56 |
* @return mixed |
| 57 |
*/ |
| 58 |
public static function plugin_data(?string $key = null) |
| 59 |
{ |
| 60 |
if (!function_exists('get_plugin_data')) { |
| 61 |
require_once \ABSPATH . 'wp-admin/includes/plugin.php'; |
| 62 |
} |
| 63 |
$plugin_data = wp_cache_get('plugin_data', WIND_PRESS::WP_OPTION); |
| 64 |
if (!$plugin_data) { |
| 65 |
$plugin_data = get_plugin_data(WIND_PRESS::FILE); |
| 66 |
wp_cache_set('plugin_data', $plugin_data, WIND_PRESS::WP_OPTION); |
| 67 |
} |
| 68 |
return $key ? $plugin_data[$key] : $plugin_data; |
| 69 |
} |
| 70 |
public static function is_updater_library_available(): bool |
| 71 |
{ |
| 72 |
return Plugin::is_pro_edition(); |
| 73 |
} |
| 74 |
public static function random_slug(int $length = 21): string |
| 75 |
{ |
| 76 |
return (new \WindPressDeps\Hidehalo\Nanoid\Client())->generateId($length, \WindPressDeps\Hidehalo\Nanoid\Client::MODE_DYNAMIC); |
| 77 |
} |
| 78 |
/** |
| 79 |
* Redirect to the given location. If headers are already sent, use a meta refresh. |
| 80 |
* |
| 81 |
* @param string $location The location to redirect to. |
| 82 |
* @param bool $safe Whether to use wp_safe_redirect() or not. |
| 83 |
*/ |
| 84 |
public static function redirect(string $location, bool $safe = \false, int $status = 302, string $x_redirect_by = 'WordPress') |
| 85 |
{ |
| 86 |
if (!headers_sent()) { |
| 87 |
if ($safe) { |
| 88 |
wp_safe_redirect($location, $status, $x_redirect_by); |
| 89 |
} else { |
| 90 |
wp_redirect($location, $status, $x_redirect_by); |
| 91 |
} |
| 92 |
} else { |
| 93 |
echo '<meta http-equiv="refresh" content="0;url=' . esc_url($location) . '">'; |
| 94 |
} |
| 95 |
exit; |
| 96 |
} |
| 97 |
/** |
| 98 |
* Save the worker result into the file. |
| 99 |
* |
| 100 |
* @param mixed $content The content of the file. |
| 101 |
* @param string $file_path The file path. |
| 102 |
* @param int $flags The flags to pass to the file_put_contents() function. |
| 103 |
* @throws Exception |
| 104 |
*/ |
| 105 |
public static function save_file($content, string $file_path, int $flags = 0): void |
| 106 |
{ |
| 107 |
/** |
| 108 |
* @var \WP_Filesystem_Base $wp_filesystem |
| 109 |
*/ |
| 110 |
global $wp_filesystem; |
| 111 |
if (!file_exists($file_path)) { |
| 112 |
wp_mkdir_p(dirname($file_path)); |
| 113 |
} |
| 114 |
if (!$wp_filesystem) { |
| 115 |
require_once \ABSPATH . 'wp-admin/includes/file.php'; |
| 116 |
WP_Filesystem(); |
| 117 |
} |
| 118 |
$result = $wp_filesystem->put_contents($file_path, $content, $flags); |
| 119 |
if ($result === \false) { |
| 120 |
throw new Exception('Failed to save to file.', 500); |
| 121 |
} |
| 122 |
// if write is successful continue |
| 123 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Local file |
| 124 |
$saved_content = $wp_filesystem->get_contents($file_path); |
| 125 |
// if read is successful continue |
| 126 |
if ($saved_content === \false) { |
| 127 |
throw new Exception('The saved file is not readable.', 500); |
| 128 |
} |
| 129 |
} |
| 130 |
/** |
| 131 |
* Deletes a file or directory. |
| 132 |
* |
| 133 |
* @param string $file_path Path to the file or directory. |
| 134 |
* @param bool $recursive Optional. If set to true, deletes files and folders recursively. |
| 135 |
* Default false. |
| 136 |
* @param string|false $type Type of resource. 'f' for file, 'd' for directory. |
| 137 |
* Default false. |
| 138 |
*/ |
| 139 |
public static function delete_file($file_path, $recursive = \false, $type = \false): void |
| 140 |
{ |
| 141 |
/** |
| 142 |
* @var \WP_Filesystem_Base $wp_filesystem |
| 143 |
*/ |
| 144 |
global $wp_filesystem; |
| 145 |
if (!$wp_filesystem) { |
| 146 |
require_once \ABSPATH . 'wp-admin/includes/file.php'; |
| 147 |
WP_Filesystem(); |
| 148 |
} |
| 149 |
$result = $wp_filesystem->delete($file_path, $recursive, $type); |
| 150 |
if ($result === \false) { |
| 151 |
throw new Exception('Failed to delete the file.', 500); |
| 152 |
} |
| 153 |
} |
| 154 |
/** |
| 155 |
* Get all installed plugins |
| 156 |
* |
| 157 |
* @return array Array of installed plugins |
| 158 |
*/ |
| 159 |
public static function get_all_plugins() |
| 160 |
{ |
| 161 |
require_once \ABSPATH . 'wp-admin/includes/plugin.php'; |
| 162 |
return get_plugins(); |
| 163 |
} |
| 164 |
/** |
| 165 |
* Check if a plugin is installed by name or slug |
| 166 |
* Supports wildcard patterns (e.g., "GreenShift*") and regex patterns (e.g., "/^GreenShift/i") |
| 167 |
* |
| 168 |
* @param string|array $plugin_name The name, slug, or array of aliases of the plugin to check. Can be: |
| 169 |
* - Exact match: "GreenShift" or "greenshift/greenshift.php" |
| 170 |
* - Wildcard: "GreenShift*" or "greenshift/*" |
| 171 |
* - Regex: "/^GreenShift/i" or "/^greenshift\//i" |
| 172 |
* - Array: ['GreenShift', 'greenshift/*', '/^GreenShift/i'] |
| 173 |
* @return bool True if the plugin is installed, false otherwise |
| 174 |
*/ |
| 175 |
public static function is_plugin_installed($plugin_name) |
| 176 |
{ |
| 177 |
// Handle array input - check if any alias matches |
| 178 |
if (is_array($plugin_name)) { |
| 179 |
foreach ($plugin_name as $alias) { |
| 180 |
if (self::is_plugin_installed($alias)) { |
| 181 |
return \true; |
| 182 |
} |
| 183 |
} |
| 184 |
return \false; |
| 185 |
} |
| 186 |
$all_plugins = self::get_all_plugins(); |
| 187 |
// Check if it's a regex pattern (starts and ends with /) |
| 188 |
$is_regex = preg_match('/^\/.*\/[imsxeADSUXJu]*$/', $plugin_name); |
| 189 |
if ($is_regex) { |
| 190 |
// Use regex pattern directly - check both name and slug |
| 191 |
foreach ($all_plugins as $plugin_path => $plugin_data) { |
| 192 |
if (preg_match($plugin_name, $plugin_data['Name']) || preg_match($plugin_name, $plugin_path)) { |
| 193 |
return \true; |
| 194 |
} |
| 195 |
} |
| 196 |
} else { |
| 197 |
// Check if contains wildcards (* or ?) |
| 198 |
$has_wildcards = strpos($plugin_name, '*') !== \false || strpos($plugin_name, '?') !== \false; |
| 199 |
if ($has_wildcards) { |
| 200 |
// Convert wildcard pattern to regex |
| 201 |
// First replace wildcards with placeholders, then escape, then convert placeholders to regex |
| 202 |
$pattern = str_replace(['*', '?'], ['__WILDCARD_ASTERISK__', '__WILDCARD_QUESTION__'], $plugin_name); |
| 203 |
$pattern = preg_quote($pattern, '/'); |
| 204 |
$pattern = str_replace(['__WILDCARD_ASTERISK__', '__WILDCARD_QUESTION__'], ['.*', '.'], $pattern); |
| 205 |
$pattern = '/^' . $pattern . '$/i'; |
| 206 |
foreach ($all_plugins as $plugin_path => $plugin_data) { |
| 207 |
if (preg_match($pattern, $plugin_data['Name']) || preg_match($pattern, $plugin_path)) { |
| 208 |
return \true; |
| 209 |
} |
| 210 |
} |
| 211 |
} else { |
| 212 |
// Exact match - check both name and slug |
| 213 |
foreach ($all_plugins as $plugin_path => $plugin_data) { |
| 214 |
if ($plugin_data['Name'] == $plugin_name || $plugin_path == $plugin_name) { |
| 215 |
return \true; |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
return \false; |
| 221 |
} |
| 222 |
/** |
| 223 |
* Check if a plugin is active by name or slug |
| 224 |
* Supports wildcard patterns (e.g., "GreenShift*") and regex patterns (e.g., "/^GreenShift/i") |
| 225 |
* |
| 226 |
* @param string|array $plugin_name The name, slug, or array of aliases of the plugin to check. Can be: |
| 227 |
* - Exact match: "GreenShift" or "greenshift/greenshift.php" |
| 228 |
* - Wildcard: "GreenShift*" or "greenshift/*" |
| 229 |
* - Regex: "/^GreenShift/i" or "/^greenshift\//i" |
| 230 |
* - Array: ['GreenShift', 'greenshift/*', '/^GreenShift/i'] |
| 231 |
* @return bool True if the plugin is active, false otherwise |
| 232 |
*/ |
| 233 |
public static function is_plugin_active_by_name($plugin_name) |
| 234 |
{ |
| 235 |
// Handle array input - check if any alias matches |
| 236 |
if (is_array($plugin_name)) { |
| 237 |
foreach ($plugin_name as $alias) { |
| 238 |
if (self::is_plugin_active_by_name($alias)) { |
| 239 |
return \true; |
| 240 |
} |
| 241 |
} |
| 242 |
return \false; |
| 243 |
} |
| 244 |
$all_plugins = self::get_all_plugins(); |
| 245 |
// Check if it's a regex pattern (starts and ends with /) |
| 246 |
$is_regex = preg_match('/^\/.*\/[imsxeADSUXJu]*$/', $plugin_name); |
| 247 |
if ($is_regex) { |
| 248 |
// Use regex pattern directly - check both name and slug |
| 249 |
foreach ($all_plugins as $plugin_path => $plugin_data) { |
| 250 |
if ((preg_match($plugin_name, $plugin_data['Name']) || preg_match($plugin_name, $plugin_path)) && is_plugin_active($plugin_path)) { |
| 251 |
return \true; |
| 252 |
} |
| 253 |
} |
| 254 |
} else { |
| 255 |
// Check if contains wildcards (* or ?) |
| 256 |
$has_wildcards = strpos($plugin_name, '*') !== \false || strpos($plugin_name, '?') !== \false; |
| 257 |
if ($has_wildcards) { |
| 258 |
// Convert wildcard pattern to regex |
| 259 |
// First replace wildcards with placeholders, then escape, then convert placeholders to regex |
| 260 |
$pattern = str_replace(['*', '?'], ['__WILDCARD_ASTERISK__', '__WILDCARD_QUESTION__'], $plugin_name); |
| 261 |
$pattern = preg_quote($pattern, '/'); |
| 262 |
$pattern = str_replace(['__WILDCARD_ASTERISK__', '__WILDCARD_QUESTION__'], ['.*', '.'], $pattern); |
| 263 |
$pattern = '/^' . $pattern . '$/i'; |
| 264 |
foreach ($all_plugins as $plugin_path => $plugin_data) { |
| 265 |
if ((preg_match($pattern, $plugin_data['Name']) || preg_match($pattern, $plugin_path)) && is_plugin_active($plugin_path)) { |
| 266 |
return \true; |
| 267 |
} |
| 268 |
} |
| 269 |
} else { |
| 270 |
// Exact match - check both name and slug |
| 271 |
foreach ($all_plugins as $plugin_path => $plugin_data) { |
| 272 |
if (($plugin_data['Name'] == $plugin_name || $plugin_path == $plugin_name) && is_plugin_active($plugin_path)) { |
| 273 |
return \true; |
| 274 |
} |
| 275 |
} |
| 276 |
} |
| 277 |
} |
| 278 |
return \false; |
| 279 |
} |
| 280 |
/** |
| 281 |
* Check if a theme is installed by name |
| 282 |
* |
| 283 |
* @param string $theme_name The name of the theme to check |
| 284 |
* @return bool True if the theme is installed, false otherwise |
| 285 |
*/ |
| 286 |
public static function is_theme_installed($theme_name) |
| 287 |
{ |
| 288 |
$all_themes = wp_get_themes(); |
| 289 |
foreach ($all_themes as $theme) { |
| 290 |
if ($theme->get('Name') == $theme_name) { |
| 291 |
return \true; |
| 292 |
} |
| 293 |
} |
| 294 |
return \false; |
| 295 |
} |
| 296 |
/** |
| 297 |
* Check if a theme is active by name |
| 298 |
* |
| 299 |
* @param string $theme_name The name of the theme or parent theme to check |
| 300 |
* @return bool True if the theme is active, false otherwise |
| 301 |
*/ |
| 302 |
public static function is_theme_active_by_name($theme_name) |
| 303 |
{ |
| 304 |
$current_theme = wp_get_theme(); |
| 305 |
if ($current_theme->get('Name') == $theme_name) { |
| 306 |
return \true; |
| 307 |
} |
| 308 |
if ($current_theme->parent() && $current_theme->parent()->get('Name') == $theme_name) { |
| 309 |
return \true; |
| 310 |
} |
| 311 |
return \false; |
| 312 |
} |
| 313 |
} |
| 314 |
|