PluginProbe
WindPress – Tailwind CSS integration for WordPress / 3.2.89
WindPress – Tailwind CSS integration for WordPress v3.2.89
3.2.89 3.2.88 3.2.87 3.2.86 3.2.85 3.2.84 3.2.83 3.2.82 3.2.81 trunk 3.0.0 3.0.1 3.0.10 3.0.11 3.0.12 3.0.13 3.0.14 3.0.15 3.0.16 3.0.17 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 All 143 releases
windpress / src / Utils / Common.php

Common.php in WindPress – Tailwind CSS integration for WordPress 3.2.89, at src/Utils/Common.php

319 lines 12.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 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 }
79 public static function random_slug(int $length = 21): string
80 {
81 return (new \WindPressDeps\Hidehalo\Nanoid\Client())->generateId($length, \WindPressDeps\Hidehalo\Nanoid\Client::MODE_DYNAMIC);
82 }
83 /**
84 * Redirect to the given location. If headers are already sent, use a meta refresh.
85 *
86 * @param string $location The location to redirect to.
87 * @param bool $safe Whether to use wp_safe_redirect() or not.
88 */
89 public static function redirect(string $location, bool $safe = \false, int $status = 302, string $x_redirect_by = 'WordPress')
90 {
91 if (!headers_sent()) {
92 if ($safe) {
93 wp_safe_redirect($location, $status, $x_redirect_by);
94 } else {
95 wp_redirect($location, $status, $x_redirect_by);
96 }
97 } else {
98 echo '<meta http-equiv="refresh" content="0;url=' . esc_url($location) . '">';
99 }
100 exit;
101 }
102 /**
103 * Save the worker result into the file.
104 *
105 * @param mixed $content The content of the file.
106 * @param string $file_path The file path.
107 * @param int $flags The flags to pass to the file_put_contents() function.
108 * @throws Exception
109 */
110 public static function save_file($content, string $file_path, int $flags = 0): void
111 {
112 /**
113 * @var \WP_Filesystem_Base $wp_filesystem
114 */
115 global $wp_filesystem;
116 if (!file_exists($file_path)) {
117 wp_mkdir_p(dirname($file_path));
118 }
119 if (!$wp_filesystem) {
120 require_once \ABSPATH . 'wp-admin/includes/file.php';
121 WP_Filesystem();
122 }
123 $result = $wp_filesystem->put_contents($file_path, $content, $flags);
124 if ($result === \false) {
125 throw new Exception('Failed to save to file.', 500);
126 }
127 // if write is successful continue
128 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Local file
129 $saved_content = $wp_filesystem->get_contents($file_path);
130 // if read is successful continue
131 if ($saved_content === \false) {
132 throw new Exception('The saved file is not readable.', 500);
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;
317 }
318 }
319