PluginProbe
JCH Optimize / trunk
JCH Optimize vtrunk
6.0.2 6.0.1 trunk 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.2.0 1.2.1 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.1.0 2.10.0 2.2.0 2.2.1 All 58 releases
jch-optimize / src / Platform / Utility.php

Utility.php in JCH Optimize trunk, at src/Platform/Utility.php

258 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * JCH Optimize - Performs several front-end optimizations for fast downloads
5 *
6 * @package jchoptimize/wordpress-platform
7 * @author Samuel Marshall <samuel@jch-optimize.net>
8 * @copyright Copyright (c) 2020 Samuel Marshall / JCH Optimize
9 * @license GNU/GPLv3, or later. See LICENSE file
10 *
11 * If LICENSE file missing, see <http://www.gnu.org/licenses/>.
12 */
13
14 namespace JchOptimize\WordPress\Platform;
15
16 use JchOptimize\Core\Platform\UtilityInterface;
17 use JchOptimize\Core\Registry;
18 use stdClass;
19
20 use function __;
21 use function add_action;
22 use function array_search;
23 use function count;
24 use function function_exists;
25 use function header;
26 use function headers_list;
27 use function headers_sent;
28 use function is_admin;
29 use function is_user_logged_in;
30 use function preg_match_all;
31 use function strripos;
32 use function wp_create_nonce;
33 use function wp_is_mobile;
34
35 defined('_WP_EXEC') or die('Restricted access');
36
37 class Utility implements UtilityInterface
38 {
39 /**
40 *
41 * @param string $text
42 *
43 * @return string
44 */
45 public function translate(string $text): string
46 {
47 return __($text, 'jch-optimize');
48 }
49
50 /**
51 * Checks if user is not logged in
52 *
53 */
54 public function isGuest(): bool
55 {
56 if (defined('TEST_SITE_ROOT')) {
57 return true;
58 }
59
60 if (function_exists('is_user_logged_in')) {
61 return !is_user_logged_in();
62 }
63
64 return false;
65 }
66
67 /**
68 * @param array $headers
69 *
70 * @return void
71 */
72 public function sendHeaders(array $headers): void
73 {
74 /** @psalm-var array<string, string> $headers */
75 if (!empty($headers)) {
76 foreach ($headers as $header => $value) {
77 add_action('jch_optimize_send_headers', function () use ($header, $value) {
78 header($header . ': ' . $value, false);
79 });
80 }
81 }
82 }
83
84 public function userAgent($userAgent): stdClass
85 {
86 global $is_chrome, $is_IE, $is_edge, $is_safari, $is_opera, $is_gecko, $is_winIE, $is_macIE, $is_iphone;
87
88 $oUA = new stdClass();
89 $oUA->browser = 'Unknown';
90 $oUA->browserVersion = 'Unknown';
91 $oUA->os = 'Unknown';
92
93 if ($is_chrome) {
94 $oUA->browser = 'Chrome';
95 } elseif ($is_gecko) {
96 $oUA->browser = 'Firefox';
97 } elseif ($is_safari) {
98 $oUA->browser = 'Safari';
99 } elseif ($is_edge) {
100 $oUA->browser = 'Edge';
101 } elseif ($is_IE) {
102 $oUA->browser = 'Internet Explorer';
103 } elseif ($is_opera) {
104 $oUA->browser = 'Opera';
105 }
106
107
108 if ($oUA->browser != 'Unknown') {
109 // Build the REGEX pattern to match the browser version string within the user agent string.
110 $pattern = '#(?<browser>Version|' . $oUA->browser . ')[/ :]+(?<version>[0-9.|a-zA-Z.]*)#';
111
112 // Attempt to find version strings in the user agent string.
113 $matches = array();
114
115 if (preg_match_all($pattern, $userAgent, $matches)) {
116 // Do we have both a Version and browser match?
117 if (count($matches['browser']) == 2) {
118 // See whether Version or browser came first, and use the number accordingly.
119 if (strripos($userAgent, 'Version') < strripos($userAgent, $oUA->browser)) {
120 $oUA->browserVersion = $matches['version'][0];
121 } else {
122 $oUA->browserVersion = $matches['version'][1];
123 }
124 } elseif (count($matches['browser']) > 2) {
125 $key = array_search('Version', $matches['browser']);
126
127 if ($key) {
128 $oUA->browserVersion = $matches['version'][$key];
129 }
130 } else {
131 // We only have a Version or a browser so use what we have.
132 $oUA->browserVersion = $matches['version'][0];
133 }
134 }
135 }
136
137 if ($is_winIE) {
138 $oUA->os = 'Windows';
139 } elseif ($is_macIE) {
140 $oUA->os = 'Mac';
141 } elseif ($is_iphone) {
142 $oUA->os = 'iOS';
143 }
144
145
146 return $oUA;
147 }
148
149 public function bsTooltipContentAttribute(): string
150 {
151 return 'data-bs-content';
152 }
153
154 /**
155 * @param Registry $params
156 * @param bool $nativeCache
157 *
158 * @return bool
159 * @deprecated Use Cache::isPageCacheEnabled()
160 */
161 public function isPageCacheEnabled(Registry $params, bool $nativeCache = false): bool
162 {
163 return (bool)$params->get('cache_enable', '0');
164 }
165
166 public function isMobile(): bool
167 {
168 return wp_is_mobile();
169 }
170
171 public function getMobileUserAgentPattern(): string
172 {
173 return 'Mobile|Android|Silk/|Kindle|BlackBerry|Opera Mini|Opera Mobi';
174 }
175
176 public function usesMobileClientHint(): bool
177 {
178 return true;
179 }
180
181 /**
182 * @param Registry $params
183 *
184 * @return string
185 * @deprecated Use Cache::getCacheStorage()
186 */
187 public function getCacheStorage(Registry $params): string
188 {
189 return $params->get('pro_cache_storage_adapter', 'filesystem');
190 }
191
192 public function getHeaders(): array
193 {
194 return headers_list();
195 }
196
197 public function publishAdminMessages(string $message, string $messageType): void
198 {
199 add_action('admin_notices', function () use ($message, $messageType) {
200 echo <<<HTML
201 <div class="notice notice-{$messageType} is-dismissible"><p>{$message}</p></div>
202 HTML;
203 });
204 }
205
206 public function getLogsPath(): string
207 {
208 return JCH_PLUGIN_DIR . 'logs';
209 }
210
211 public function isSiteGzipEnabled(): bool
212 {
213 return false;
214 }
215
216 /**
217 * @param array $data
218 *
219 * @return void
220 * @deprecated Use Cache::outputData()
221 */
222 #[NoReturn]
223 public function outputData(array $data): void
224 {
225 /** @psalm-var array{headers:array<array-key, string>, body: string} $data */
226 if (!empty($data['headers'])) {
227 foreach ($data['headers'] as $header) {
228 header($header);
229 }
230 }
231
232 echo $data['body'];
233
234 exit();
235 }
236
237 /**
238 * @param array|null $data
239 *
240 * @return array|null
241 * @deprecated Use Cache::prepareDataFromCache()
242 */
243 public function prepareDataFromCache(?array $data): ?array
244 {
245 return $data;
246 }
247
248 public function isAdmin(): bool
249 {
250 return is_admin();
251 }
252
253 public function getNonce(string $id): string
254 {
255 return wp_create_nonce($id);
256 }
257 }
258