| @@ -12,8 +12,9 @@ | ||
| 12 | 12 | namespace WindPress\WindPress\Utils; |
| 13 | 13 | |
| 14 | 14 | use Exception; |
| 15 | 15 | use WIND_PRESS; |
| 16 | +use WindPress\WindPress\Plugin; | |
| 16 | 17 | /** |
| 17 | 18 | * Common utility functions for the plugin. |
| 18 | 19 | * |
| 19 | 20 | * @since 3.0.0 |
| @@ -65,11 +66,20 @@ | ||
| 65 | 66 | wp_cache_set('plugin_data', $plugin_data, WIND_PRESS::WP_OPTION); |
| 66 | 67 | } |
| 67 | 68 | return $key ? $plugin_data[$key] : $plugin_data; |
| 68 | 69 | } |
| 70 | + public static function is_updater_library_available(): bool | |
| 71 | + { | |
| 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'); | |
| 78 | + } | |
| 69 | 79 | public static function random_slug(int $length = 21): string |
| 70 | 80 | { |
| 71 | - 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); | |
| 72 | 82 | } |
| 73 | 83 | /** |
| 74 | 84 | * Redirect to the given location. If headers are already sent, use a meta refresh. |
| 75 | 85 | * |
| @@ -115,11 +125,194 @@ | ||
| 115 | 125 | throw new Exception('Failed to save to file.', 500); |
| 116 | 126 | } |
| 117 | 127 | // if write is successful continue |
| 118 | 128 | // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Local file |
| 119 | - $saved_content = file_get_contents($file_path); | |
| 129 | + $saved_content = $wp_filesystem->get_contents($file_path); | |
| 120 | 130 | // if read is successful continue |
| 121 | 131 | if ($saved_content === \false) { |
| 122 | 132 | throw new Exception('The saved file is not readable.', 500); |
| 123 | 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; | |
| 124 | 317 | } |
| 125 | 318 | } |