PluginProbe
WindPress – Tailwind CSS integration for WordPress / 3.0.1
WindPress – Tailwind CSS integration for WordPress v3.0.1
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.0.1, at src/Utils/Common.php

126 lines 4.2 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 /**
17 * Common utility functions for the plugin.
18 *
19 * @since 3.0.0
20 */
21 class Common
22 {
23 /**
24 * Check the type of the current request.
25 *
26 * @param string $type The type of the request. Available values: `admin`, `ajax`, `frontend`, `rest`, `cron`.
27 */
28 public static function is_request(string $type): bool
29 {
30 switch ($type) {
31 case 'admin':
32 return is_admin();
33 case 'ajax':
34 return wp_doing_ajax();
35 case 'rest':
36 return defined('REST_REQUEST') && \REST_REQUEST;
37 case 'cron':
38 return wp_doing_cron();
39 case 'json':
40 return wp_is_json_request();
41 case 'xmlrpc':
42 return defined('XMLRPC_REQUEST') && \XMLRPC_REQUEST;
43 case 'xml':
44 return wp_is_xml_request();
45 case 'frontend':
46 return (!is_admin() || wp_doing_ajax()) && !wp_doing_cron();
47 default:
48 return \false;
49 }
50 }
51 /**
52 * Get the plugin's data.
53 *
54 * @param string $key The key to retrieve.
55 * @return mixed
56 */
57 public static function plugin_data(?string $key = null)
58 {
59 if (!function_exists('get_plugin_data')) {
60 require_once \ABSPATH . 'wp-admin/includes/plugin.php';
61 }
62 $plugin_data = wp_cache_get('plugin_data', WIND_PRESS::WP_OPTION);
63 if (!$plugin_data) {
64 $plugin_data = get_plugin_data(WIND_PRESS::FILE);
65 wp_cache_set('plugin_data', $plugin_data, WIND_PRESS::WP_OPTION);
66 }
67 return $key ? $plugin_data[$key] : $plugin_data;
68 }
69 public static function random_slug(int $length = 21): string
70 {
71 return (new \WindPressPackages\Hidehalo\Nanoid\Client())->generateId($length, \WindPressPackages\Hidehalo\Nanoid\Client::MODE_DYNAMIC);
72 }
73 /**
74 * Redirect to the given location. If headers are already sent, use a meta refresh.
75 *
76 * @param string $location The location to redirect to.
77 * @param bool $safe Whether to use wp_safe_redirect() or not.
78 */
79 public static function redirect(string $location, bool $safe = \false, int $status = 302, string $x_redirect_by = 'WordPress')
80 {
81 if (!headers_sent()) {
82 if ($safe) {
83 wp_safe_redirect($location, $status, $x_redirect_by);
84 } else {
85 wp_redirect($location, $status, $x_redirect_by);
86 }
87 } else {
88 echo '<meta http-equiv="refresh" content="0;url=' . esc_url($location) . '">';
89 }
90 exit;
91 }
92 /**
93 * Save the worker result into the file.
94 *
95 * @param mixed $content The content of the file.
96 * @param string $file_path The file path.
97 * @param int $flags The flags to pass to the file_put_contents() function.
98 * @throws Exception
99 */
100 public static function save_file($content, string $file_path, int $flags = 0): void
101 {
102 /**
103 * @var \WP_Filesystem_Base $wp_filesystem
104 */
105 global $wp_filesystem;
106 if (!file_exists($file_path)) {
107 wp_mkdir_p(dirname($file_path));
108 }
109 if (!$wp_filesystem) {
110 require_once \ABSPATH . 'wp-admin/includes/file.php';
111 WP_Filesystem();
112 }
113 $result = $wp_filesystem->put_contents($file_path, $content, $flags);
114 if ($result === \false) {
115 throw new Exception('Failed to save to file.', 500);
116 }
117 // if write is successful continue
118 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Local file
119 $saved_content = file_get_contents($file_path);
120 // if read is successful continue
121 if ($saved_content === \false) {
122 throw new Exception('The saved file is not readable.', 500);
123 }
124 }
125 }
126