PluginProbe
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score / 1.0.1
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score v1.0.1
trunk 1.0 1.0.1 1.1 1.1.1 1.1.2 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 2.0 2.0.1 2.0.2 2.0.3 2.0.4 2.1 2.1.1 2.1.2 2.2 2.2.1 All 69 releases
powered-cache / includes / functions.php

functions.php in Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score 1.0.1, at includes/functions.php

502 lines 12.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Common functions
4 */
5
6
7 // Exit if accessed directly
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit;
10 }
11
12
13 /**
14 * Get all settings, if db hasn't settings options set defaults.
15 *
16 * @since 1.0
17 * @return mixed|void
18 */
19 function powered_cache_get_settings() {
20 $settings = get_option( 'powered_cache_settings' );
21
22 if ( empty( $settings ) ) {
23 $settings = Powered_Cache_Config::factory()->default_settings();
24 update_option( 'powered_cache_settings', $settings );
25 }
26
27 return apply_filters( 'powered_cache_get_settings', $settings );
28 }
29
30
31 /**
32 * Get single settings item of plugin
33 *
34 * @since 1.0
35 * @param string $key
36 * @param bool|false $default
37 *
38 * @return mixed|void
39 */
40 function powered_cache_get_option( $key = '', $default = false ) {
41 global $powered_cache_options;
42 $value = ! empty( $powered_cache_options[ $key ] ) ? $powered_cache_options[ $key ] : $default;
43 $value = apply_filters( 'powered_cache_get_option', $value, $key, $default );
44
45 return apply_filters( 'powered_cache_get_option_' . $key, $value, $key, $default );
46 }
47
48
49 /**
50 * Flush object cache and clean cache directory
51 *
52 * @since 1.0
53 */
54 function powered_cache_flush() {
55 if ( function_exists( 'wp_cache_flush' ) ) {
56 wp_cache_flush();
57 }
58 powered_cache_clean_page_cache_dir();
59 do_action( 'powered_cache_flushed' );
60 }
61
62
63 /**
64 * Save powered cache settings, update global settings variable and write to file
65 *
66 * @since 1.0
67 *
68 * @param $old_settings array
69 * @param $new_settings array
70 *
71 * @return bool depends on writing settings to file
72 */
73 function powered_cache_save_settings( $old_settings, $new_settings ) {
74 global $powered_cache_options;
75 $settings = array_merge( $old_settings, $new_settings );
76
77 $powered_cache_options = $settings;
78
79 $changed_settings = array_diff_assoc( array_map( 'serialize', $settings ), array_map( 'serialize', $old_settings ) );
80
81 update_option( 'powered_cache_settings', $settings );
82
83 if ( isset( $changed_settings['object_cache'] ) && function_exists( 'wp_cache_flush' ) ) {
84 wp_cache_flush();
85 }
86
87 if ( isset( $changed_settings['enable_page_caching'] ) ) {
88 powered_cache_clean_page_cache_dir();
89 }
90
91 Powered_Cache_Config::factory()->setup_object_cache( $settings['object_cache'] );
92 Powered_Cache_Config::factory()->setup_page_cache( $settings['enable_page_caching'] );
93
94 do_action( 'powered_cache_settings_saved', $settings );
95
96 unset( $settings['extension_settings'] );
97
98 return Powered_Cache_Config::factory()->save_to_file( $settings );
99 }
100
101 /**
102 * Prepare regex string for browser detetch
103 *
104 * @since 1.0
105 * @return mixed|void
106 */
107 function powered_cache_mobile_browsers_regex(){
108 $browsers = powered_cache_mobile_browsers();
109 $regex_str = addcslashes( implode( '|', explode( ',', $browsers ) ),' ');
110
111 return apply_filters( 'powered_cache_mobile_browsers_regex', $regex_str, $browsers );
112 }
113
114 /**
115 * Prepare regex string for mobile prefix
116 *
117 * @since 1.0
118 * @return mixed|void
119 */
120 function powered_cache_mobile_prefixes_regex(){
121 $prefixes = powered_cache_mobile_prefixes();
122 $regex_str = addcslashes( implode( '|', explode( ',', $prefixes ) ),' ');
123
124 return apply_filters( 'powered_cache_mobile_prefixes_regex', $regex_str, $prefixes );
125 }
126
127 /**
128 * Supported mobile browsers
129 *
130 * @since 1.0
131 * @return mixed|void
132 */
133 function powered_cache_mobile_browsers() {
134 $mobile_browsers = '2.0 MMP, 240x320, 400X240, AvantGo, BlackBerry, Blazer, Cellphone, Danger, DoCoMo, Elaine/3.0, EudoraWeb, Googlebot-Mobile, hiptop, IEMobile, KYOCERA/WX310K, LG/U990, MIDP-2., MMEF20, MOT-V, NetFront, Newt, Nintendo Wii, Nitro, Nokia, Opera Mini, Palm, PlayStation Portable, portalmmm, Proxinet, ProxiNet, SHARP-TQ-GX10, SHG-i900, Small, SonyEricsson, Symbian OS, SymbianOS, TS21i-10, UP.Browser, UP.Link, webOS, Windows CE, WinWAP, YahooSeeker/M1A1-R2D2, iPhone, iPod, Android, BlackBerry9530, LG-TU915 Obigo, LGE VX, webOS, Nokia5800';
135
136 return apply_filters( 'powered_cache_mobile_browsers', $mobile_browsers );
137 }
138
139
140 /**
141 * Supported mobile prefixes
142 *
143 * @since 1.0
144 * @return mixed|void
145 */
146 function powered_cache_mobile_prefixes() {
147 $mobile_prefixes = 'w3c , w3c-, acs-, alav, alca, amoi, audi, avan, benq, bird, blac, blaz, brew, cell, cldc, cmd-, dang, doco, eric, hipt, htc_, inno, ipaq, ipod, jigs, kddi, keji, leno, lg-c, lg-d, lg-g, lge-, lg/u, maui, maxo, midp, mits, mmef, mobi, mot-, moto, mwbp, nec-, newt, noki, palm, pana, pant, phil, play, port, prox, qwap, sage, sams, sany, sch-, sec-, send, seri, sgh-, shar, sie-, siem, smal, smar, sony, sph-, symb, t-mo, teli, tim-, tosh, tsm-, upg1, upsi, vk-v, voda, wap-, wapa, wapi, wapp, wapr, webc, winw, winw, xda , xda-';
148
149 return apply_filters( 'powered_cache_mobile_prefixes', $mobile_prefixes );
150 }
151
152
153 /**
154 * Prepare cdn addresses with hostname + zone
155 *
156 * @since 1.0
157 * @return mixed|void
158 */
159 function powered_cache_cdn_addresses() {
160 global $powered_cache_options;
161
162 $hostnames = $powered_cache_options['cdn_hostname'];
163 $zones = $powered_cache_options['cdn_zone'];
164
165 $cdn_addresses = array();
166 foreach ( $hostnames as $host_key => $host ) {
167 if ( ! empty( $host ) ) {
168 $cdn_addresses[ $zones[ $host_key ] ][] = $host;
169 }
170 }
171
172 return apply_filters( 'powered_cache_cdn_addresses', $cdn_addresses );
173 }
174
175
176 /**
177 * Check premium version running
178 * This is just simple helper function, our premium checks are more strict than your thoughts :)
179 *
180 * @since 1.0
181 * @return bool
182 */
183 function powered_cache_is_premium() {
184 if ( defined( 'POWERED_CACHE_PREMIUM' ) && true === POWERED_CACHE_PREMIUM ) {
185 return true;
186 }
187
188 return false;
189 }
190
191
192 function powered_cache_maybe_require_premium_html() {
193
194 if ( ! powered_cache_is_premium() ) {?>
195 <div class="<?php echo( ! powered_cache_is_premium() ? 'need-upgrade' : '' ); ?>">
196 <span class="upgrade-msg"><?php echo __( 'This feature available only premium users', 'powered-cache' ); ?></span>
197 </div>
198 <?php
199 }
200 }
201
202
203 /**
204 * get page cache dir
205 *
206 * @param string $url
207 * @since 1.0
208 *
209 * @return string
210 */
211 function powered_cache_get_page_cache_dir( $url = false ) {
212 if ( $url ) {
213 $url_info = parse_url( $url );
214 $sub_dir = $url_info['host'] . $url_info['path'];
215
216 return powered_cache_get_cache_dir() . 'powered-cache/' . ltrim( $sub_dir, '/' );
217 }
218
219 return powered_cache_get_cache_dir() . 'powered-cache/';
220 }
221
222 /**
223 * Delete cache file
224 *
225 * @param string $url
226 *
227 * @since 1.0
228 * @return bool true when found cache dir, otherwhise false
229 */
230 function powered_cache_delete_page_cache( $url ) {
231
232 $dir = trailingslashit( powered_cache_get_page_cache_dir( $url ) );
233
234 if ( is_dir( $dir ) ) {
235 $files = scandir( $dir );
236 foreach ( $files as $file ) {
237 /**
238 * Don't need to lookup for index-https, index-https-mobile etc..
239 * Just clean that directory's files only.
240 */
241 if ( ! is_dir( $dir . $file ) && ! in_array( $file, array( '.', '..' ) ) ) {
242 unlink( $dir . $file );
243 }
244 }
245
246 return true;
247 }
248
249 return false;
250 }
251
252 /**
253 * Get all settings for plugin
254 *
255 * @since 1.0
256 * @param $extension_id
257 *
258 * @return bool
259 */
260 function powered_cache_get_extension_settings( $extension_id ) {
261 $extension_settings = powered_cache_get_option( 'extension_settings' );
262
263 if ( ! is_array( $extension_settings ) ) {
264 return false;
265 }
266
267 if ( array_key_exists( $extension_id, $extension_settings ) ) {
268 return $extension_settings[ $extension_id ];
269 }
270
271 return false;
272 }
273
274
275 /**
276 * Update options of extension
277 * @since 1.0
278 * @param $extension_id
279 * @param array $settings
280 *
281 * @return bool
282 */
283 function powered_cache_update_extension_option( $extension_id, $settings = array() ) {
284 $options = get_option( 'powered_cache_settings' );
285
286 $options['extension_settings'][ $extension_id ] = $settings;
287
288 $options_updated = update_option( 'powered_cache_settings', $options );
289
290 do_action( 'powered_cache_extension_option_updated', $options );
291
292 return $options_updated;
293 }
294
295
296 /**
297 * Get single option of plugin. Generally used for fields
298 *
299 * @since 1.0
300 * @param $extension_id
301 * @param string $option_name
302 * @param bool|false $default
303 *
304 * @return bool
305 */
306 function powered_cache_get_extension_option( $extension_id, $option_name = '', $default = false ) {
307 $option = powered_cache_get_extension_settings( $extension_id );
308
309 if ( is_array( $option ) && array_key_exists( $option_name, $option ) ) {
310 return $option[ $option_name ];
311 }
312
313 return $default;
314 }
315
316 /**
317 * return base caching dir
318 * use this function to get base caching directory instead of directly calling constant
319 *
320 * @since 1.0
321 * @return string path
322 */
323 function powered_cache_get_cache_dir() {
324 if ( defined( 'POWERED_CACHE_CACHE_DIR' ) ) {
325 return POWERED_CACHE_CACHE_DIR;
326 }
327
328 return WP_CONTENT_DIR . '/cache/';
329 }
330
331
332 /**
333 * Clean up cache directory
334 *
335 * @since 1.0
336 * @return mixed
337 */
338 function powered_cache_clean_page_cache_dir() {
339 global $wp_filesystem;
340
341 return $wp_filesystem->rmdir( untrailingslashit( powered_cache_get_cache_dir() ) . '/powered-cache', true );
342 }
343
344
345
346 /**
347 * Collect post related urls
348 *
349 * @since 1.0
350 * @param $post_id
351 *
352 * @return array
353 */
354 function powered_cache_get_post_related_urls( $post_id ) {
355
356 $current_post_status = get_post_status( $post_id );
357
358 // array to collect all our URLs
359 $related_urls = array();
360
361 if ( get_permalink( $post_id ) == true ) {
362 // we're going to add a ton of things to flush.
363
364 // related category urls
365 $categories = get_the_category( $post_id );
366 if ( $categories ) {
367 foreach ( $categories as $cat ) {
368 array_push( $related_urls, get_category_link( $cat->term_id ) );
369 }
370 }
371
372 // related tags url
373 $tags = get_the_tags( $post_id );
374 if ( $tags ) {
375 foreach ( $tags as $tag ) {
376 array_push( $related_urls, get_tag_link( $tag->term_id ) );
377 }
378 }
379
380 // Author URL
381 array_push( $related_urls, get_author_posts_url( get_post_field( 'post_author', $post_id ) ), get_author_feed_link( get_post_field( 'post_author', $post_id ) ) );
382
383 // Archives and their feeds
384 if ( get_post_type_archive_link( get_post_type( $post_id ) ) == true ) {
385 array_push( $related_urls, get_post_type_archive_link( get_post_type( $post_id ) ), get_post_type_archive_feed_link( get_post_type( $post_id ) ) );
386 }
387
388 // Post URL
389 array_push( $related_urls, get_permalink( $post_id ) );
390
391 // Also clean URL for trashed post.
392 if ( $current_post_status == "trash" ) {
393 $trashpost = get_permalink( $post_id );
394 $trashpost = str_replace( "__trashed", "", $trashpost );
395 array_push( $related_urls, $trashpost, $trashpost . 'feed/' );
396 }
397
398 // Add in AMP permalink if Automattic's AMP is installed
399 if ( function_exists( 'amp_get_permalink' ) ) {
400 array_push( $related_urls, amp_get_permalink( $post_id ) );
401 }
402
403 // Regular AMP url for posts
404 array_push( $related_urls, get_permalink( $post_id ) . 'amp/' );
405
406 // Feeds
407 array_push( $related_urls, get_bloginfo_rss( 'rdf_url' ), get_bloginfo_rss( 'rss_url' ), get_bloginfo_rss( 'rss2_url' ), get_bloginfo_rss( 'atom_url' ), get_bloginfo_rss( 'comments_rss2_url' ), get_post_comments_feed_link( $post_id ) );
408
409 // Home Page and (if used) posts page
410 array_push( $related_urls, trailingslashit( home_url() ) );
411 if ( get_option( 'show_on_front' ) == 'page' ) {
412 // Ensure we have a page_for_posts setting to avoid empty URL
413 if ( get_option( 'page_for_posts' ) ) {
414 array_push( $related_urls, get_permalink( get_option( 'page_for_posts' ) ) );
415 }
416 }
417 }
418
419 return $related_urls;
420 }
421
422
423 /**
424 * Get site related info. This could help us to debug.
425 *
426 * @since 1.0
427 * @return array
428 */
429 function powered_cache_get_debug_info() {
430 global $wpdb;
431 if ( ! function_exists( 'get_plugins' ) ) {
432 require_once ABSPATH . 'wp-admin/includes/plugin.php';
433 }
434
435 $active_plugins_option = get_option( 'active_plugins' );
436 $active_plugins = array();
437 $plugins = get_plugins();
438
439 foreach ( $active_plugins_option as $aplugin ) {
440 $active_plugins[ $plugins[ $aplugin ]['Name'] ] = array(
441 'file' => $aplugin,
442 'Name' => $plugins[ $aplugin ]['Name'],
443 'Version' => $plugins[ $aplugin ]['Version'],
444 );
445 }
446
447
448 $theme = wp_get_theme();
449
450 $theme_info = array(
451 'Name' => $theme->get( 'Name' ),
452 'Version' => $theme->get( 'Version' ),
453 'ThemeURI' => $theme->get( 'ThemeURI' ),
454 );
455
456
457 $mysql_version = $wpdb->get_var( 'select version() as mysqlversion' );
458 $php_version = phpversion();
459
460 $debug_info['php_version'] = $php_version;
461 $debug_info['mysql_version'] = $mysql_version;
462 $debug_info['active_plugins'] = $active_plugins;
463 $debug_info['active_theme'] = $theme_info;
464 $debug_info['is_multisite'] = is_multisite();
465
466 if ( is_multisite() && function_exists( 'is_subdomain_install' ) ) {
467 $debug_info['is_subdomain_install'] = is_subdomain_install();
468 }
469
470 return $debug_info;
471 }
472
473
474 /**
475 * Is saving options now?
476 *
477 * @since 1.0
478 * @return bool
479 */
480 function powered_cache_is_saving_options() {
481 if ( defined( 'POWERED_CACHE_SAVING_OPTIONS' ) && true === POWERED_CACHE_SAVING_OPTIONS ) {
482 return true;
483 }
484
485 return false;
486 }
487
488
489 if ( ! function_exists( 'boolval' ) ) {
490 /**
491 * For compatible reason.
492 * If we create compat file someday this function should move there
493 *
494 * @since 1.0
495 * @param $val
496 *
497 * @return bool
498 */
499 function boolval( $val ) {
500 return (bool) $val;
501 }
502 }