PluginProbe
WindPress – Tailwind CSS integration for WordPress / 3.0.13
WindPress – Tailwind CSS integration for WordPress v3.0.13
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 3.0.7 All 142 releases
windpress / src / Utils / Common.php

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

155 lines 5.3 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 WindPressDeps\EDD_SL\PluginUpdater;
15 use Exception;
16 use WIND_PRESS;
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 \class_exists(PluginUpdater::class);
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