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