| 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 |
* @param $old_settings array |
| 68 |
* @param $new_settings array |
| 69 |
*/ |
| 70 |
function powered_cache_save_settings( $old_settings, $new_settings ) { |
| 71 |
global $powered_cache_options; |
| 72 |
$settings = array_merge( $old_settings, $new_settings ); |
| 73 |
|
| 74 |
$powered_cache_options = $settings; |
| 75 |
|
| 76 |
$changed_settings = array_diff_assoc( array_map( 'serialize', $settings ), array_map( 'serialize', $old_settings ) ); |
| 77 |
|
| 78 |
update_option( 'powered_cache_settings', $settings ); |
| 79 |
|
| 80 |
if ( isset( $changed_settings['object_cache'] ) && function_exists( 'wp_cache_flush' ) ) { |
| 81 |
wp_cache_flush(); |
| 82 |
} |
| 83 |
|
| 84 |
if ( isset( $changed_settings['enable_page_caching'] ) ) { |
| 85 |
powered_cache_clean_page_cache_dir(); |
| 86 |
} |
| 87 |
|
| 88 |
Powered_Cache_Config::factory()->setup_object_cache( $settings['object_cache'] ); |
| 89 |
Powered_Cache_Config::factory()->setup_page_cache( $settings['enable_page_caching'] ); |
| 90 |
|
| 91 |
do_action( 'powered_cache_settings_saved', $settings ); |
| 92 |
|
| 93 |
unset( $settings['extension_settings'] ); |
| 94 |
Powered_Cache_Config::factory()->save_to_file( $settings ); |
| 95 |
|
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Prepare regex string for browser detetch |
| 100 |
* |
| 101 |
* @since 1.0 |
| 102 |
* @return mixed|void |
| 103 |
*/ |
| 104 |
function powered_cache_mobile_browsers_regex(){ |
| 105 |
$browsers = powered_cache_mobile_browsers(); |
| 106 |
$regex_str = addcslashes( implode( '|', explode( ',', $browsers ) ),' '); |
| 107 |
|
| 108 |
return apply_filters( 'powered_cache_mobile_browsers_regex', $regex_str, $browsers ); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Prepare regex string for mobile prefix |
| 113 |
* |
| 114 |
* @since 1.0 |
| 115 |
* @return mixed|void |
| 116 |
*/ |
| 117 |
function powered_cache_mobile_prefixes_regex(){ |
| 118 |
$prefixes = powered_cache_mobile_prefixes(); |
| 119 |
$regex_str = addcslashes( implode( '|', explode( ',', $prefixes ) ),' '); |
| 120 |
|
| 121 |
return apply_filters( 'powered_cache_mobile_prefixes_regex', $regex_str, $prefixes ); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Supported mobile browsers |
| 126 |
* |
| 127 |
* @since 1.0 |
| 128 |
* @return mixed|void |
| 129 |
*/ |
| 130 |
function powered_cache_mobile_browsers() { |
| 131 |
$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'; |
| 132 |
|
| 133 |
return apply_filters( 'powered_cache_mobile_browsers', $mobile_browsers ); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
/** |
| 138 |
* Supported mobile prefixes |
| 139 |
* |
| 140 |
* @since 1.0 |
| 141 |
* @return mixed|void |
| 142 |
*/ |
| 143 |
function powered_cache_mobile_prefixes() { |
| 144 |
$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-'; |
| 145 |
|
| 146 |
return apply_filters( 'powered_cache_mobile_prefixes', $mobile_prefixes ); |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
/** |
| 151 |
* Prepare cdn addresses with hostname + zone |
| 152 |
* |
| 153 |
* @since 1.0 |
| 154 |
* @return mixed|void |
| 155 |
*/ |
| 156 |
function powered_cache_cdn_addresses() { |
| 157 |
global $powered_cache_options; |
| 158 |
|
| 159 |
$hostnames = $powered_cache_options['cdn_hostname']; |
| 160 |
$zones = $powered_cache_options['cdn_zone']; |
| 161 |
|
| 162 |
$cdn_addresses = array(); |
| 163 |
foreach ( $hostnames as $host_key => $host ) { |
| 164 |
if ( ! empty( $host ) ) { |
| 165 |
$cdn_addresses[ $zones[ $host_key ] ][] = $host; |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
return apply_filters( 'powered_cache_cdn_addresses', $cdn_addresses ); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
/** |
| 174 |
* Check premium version running |
| 175 |
* This is just simple helper function, our premium checks are more strict than your thoughts :) |
| 176 |
* |
| 177 |
* @since 1.0 |
| 178 |
* @return bool |
| 179 |
*/ |
| 180 |
function powered_cache_is_premium() { |
| 181 |
if ( defined( 'POWERED_CACHE_PREMIUM' ) && true === POWERED_CACHE_PREMIUM ) { |
| 182 |
return true; |
| 183 |
} |
| 184 |
|
| 185 |
return false; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
function powered_cache_maybe_require_premium_html() { |
| 190 |
|
| 191 |
if ( ! powered_cache_is_premium() ) {?> |
| 192 |
<div class="<?php echo( ! powered_cache_is_premium() ? 'need-upgrade' : '' ); ?>"> |
| 193 |
<span class="upgrade-msg"><?php echo __( 'This feature available only premium users', 'powered-cache' ); ?></span> |
| 194 |
</div> |
| 195 |
<?php |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
/** |
| 201 |
* get page cache dir |
| 202 |
* |
| 203 |
* @param string $url |
| 204 |
* @since 1.0 |
| 205 |
* |
| 206 |
* @return string |
| 207 |
*/ |
| 208 |
function powered_cache_get_page_cache_dir( $url = false ) { |
| 209 |
if ( $url ) { |
| 210 |
$url_info = parse_url( $url ); |
| 211 |
$sub_dir = $url_info['host'] . $url_info['path']; |
| 212 |
|
| 213 |
return powered_cache_get_cache_dir() . 'powered-cache/' . ltrim( $sub_dir, '/' ); |
| 214 |
} |
| 215 |
|
| 216 |
return powered_cache_get_cache_dir() . 'powered-cache/'; |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Delete cache file |
| 221 |
* |
| 222 |
* @param string $url |
| 223 |
* |
| 224 |
* @since 1.0 |
| 225 |
* @return bool true when found cache dir, otherwhise false |
| 226 |
*/ |
| 227 |
function powered_cache_delete_page_cache( $url ) { |
| 228 |
|
| 229 |
$dir = trailingslashit( powered_cache_get_page_cache_dir( $url ) ); |
| 230 |
|
| 231 |
if ( is_dir( $dir ) ) { |
| 232 |
$files = scandir( $dir ); |
| 233 |
foreach ( $files as $file ) { |
| 234 |
/** |
| 235 |
* Don't need to lookup for index-https, index-https-mobile etc.. |
| 236 |
* Just clean that directory's files only. |
| 237 |
*/ |
| 238 |
if ( ! is_dir( $dir . $file ) && ! in_array( $file, array( '.', '..' ) ) ) { |
| 239 |
unlink( $dir . $file ); |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
return true; |
| 244 |
} |
| 245 |
|
| 246 |
return false; |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Get all settings for plugin |
| 251 |
* |
| 252 |
* @since 1.0 |
| 253 |
* @param $extension_id |
| 254 |
* |
| 255 |
* @return bool |
| 256 |
*/ |
| 257 |
function powered_cache_get_extension_settings( $extension_id ) { |
| 258 |
$extension_settings = powered_cache_get_option( 'extension_settings' ); |
| 259 |
|
| 260 |
if ( ! is_array( $extension_settings ) ) { |
| 261 |
return false; |
| 262 |
} |
| 263 |
|
| 264 |
if ( array_key_exists( $extension_id, $extension_settings ) ) { |
| 265 |
return $extension_settings[ $extension_id ]; |
| 266 |
} |
| 267 |
|
| 268 |
return false; |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
/** |
| 273 |
* Update options of extension |
| 274 |
* @since 1.0 |
| 275 |
* @param $extension_id |
| 276 |
* @param array $settings |
| 277 |
* |
| 278 |
* @return bool |
| 279 |
*/ |
| 280 |
function powered_cache_update_extension_option( $extension_id, $settings = array() ) { |
| 281 |
$options = get_option( 'powered_cache_settings' ); |
| 282 |
|
| 283 |
$options['extension_settings'][ $extension_id ] = $settings; |
| 284 |
|
| 285 |
$options_updated = update_option( 'powered_cache_settings', $options ); |
| 286 |
|
| 287 |
do_action( 'powered_cache_extension_option_updated', $options ); |
| 288 |
|
| 289 |
return $options_updated; |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
/** |
| 294 |
* Get single option of plugin. Generally used for fields |
| 295 |
* |
| 296 |
* @since 1.0 |
| 297 |
* @param $extension_id |
| 298 |
* @param string $option_name |
| 299 |
* @param bool|false $default |
| 300 |
* |
| 301 |
* @return bool |
| 302 |
*/ |
| 303 |
function powered_cache_get_extension_option( $extension_id, $option_name = '', $default = false ) { |
| 304 |
$option = powered_cache_get_extension_settings( $extension_id ); |
| 305 |
|
| 306 |
if ( is_array( $option ) && array_key_exists( $option_name, $option ) ) { |
| 307 |
return $option[ $option_name ]; |
| 308 |
} |
| 309 |
|
| 310 |
return $default; |
| 311 |
} |
| 312 |
|
| 313 |
/** |
| 314 |
* return base caching dir |
| 315 |
* use this function to get base caching directory instead of directly calling constant |
| 316 |
* |
| 317 |
* @since 1.0 |
| 318 |
* @return string path |
| 319 |
*/ |
| 320 |
function powered_cache_get_cache_dir() { |
| 321 |
if ( defined( 'POWERED_CACHE_CACHE_DIR' ) ) { |
| 322 |
return POWERED_CACHE_CACHE_DIR; |
| 323 |
} |
| 324 |
|
| 325 |
return WP_CONTENT_DIR . '/cache/'; |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
/** |
| 330 |
* Clean up cache directory |
| 331 |
* |
| 332 |
* @since 1.0 |
| 333 |
* @return mixed |
| 334 |
*/ |
| 335 |
function powered_cache_clean_page_cache_dir() { |
| 336 |
global $wp_filesystem; |
| 337 |
|
| 338 |
return $wp_filesystem->rmdir( untrailingslashit( powered_cache_get_cache_dir() ) . '/powered-cache', true ); |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
/** |
| 344 |
* Collect post related urls |
| 345 |
* |
| 346 |
* @since 1.0 |
| 347 |
* @param $post_id |
| 348 |
* |
| 349 |
* @return array |
| 350 |
*/ |
| 351 |
function powered_cache_get_post_related_urls( $post_id ) { |
| 352 |
|
| 353 |
$current_post_status = get_post_status( $post_id ); |
| 354 |
|
| 355 |
// array to collect all our URLs |
| 356 |
$related_urls = array(); |
| 357 |
|
| 358 |
if ( get_permalink( $post_id ) == true ) { |
| 359 |
// we're going to add a ton of things to flush. |
| 360 |
|
| 361 |
// related category urls |
| 362 |
$categories = get_the_category( $post_id ); |
| 363 |
if ( $categories ) { |
| 364 |
foreach ( $categories as $cat ) { |
| 365 |
array_push( $related_urls, get_category_link( $cat->term_id ) ); |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
// related tags url |
| 370 |
$tags = get_the_tags( $post_id ); |
| 371 |
if ( $tags ) { |
| 372 |
foreach ( $tags as $tag ) { |
| 373 |
array_push( $related_urls, get_tag_link( $tag->term_id ) ); |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
// Author URL |
| 378 |
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 ) ) ); |
| 379 |
|
| 380 |
// Archives and their feeds |
| 381 |
if ( get_post_type_archive_link( get_post_type( $post_id ) ) == true ) { |
| 382 |
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 ) ) ); |
| 383 |
} |
| 384 |
|
| 385 |
// Post URL |
| 386 |
array_push( $related_urls, get_permalink( $post_id ) ); |
| 387 |
|
| 388 |
// Also clean URL for trashed post. |
| 389 |
if ( $current_post_status == "trash" ) { |
| 390 |
$trashpost = get_permalink( $post_id ); |
| 391 |
$trashpost = str_replace( "__trashed", "", $trashpost ); |
| 392 |
array_push( $related_urls, $trashpost, $trashpost . 'feed/' ); |
| 393 |
} |
| 394 |
|
| 395 |
// Add in AMP permalink if Automattic's AMP is installed |
| 396 |
if ( function_exists( 'amp_get_permalink' ) ) { |
| 397 |
array_push( $related_urls, amp_get_permalink( $post_id ) ); |
| 398 |
} |
| 399 |
|
| 400 |
// Regular AMP url for posts |
| 401 |
array_push( $related_urls, get_permalink( $post_id ) . 'amp/' ); |
| 402 |
|
| 403 |
// Feeds |
| 404 |
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 ) ); |
| 405 |
|
| 406 |
// Home Page and (if used) posts page |
| 407 |
array_push( $related_urls, trailingslashit( home_url() ) ); |
| 408 |
if ( get_option( 'show_on_front' ) == 'page' ) { |
| 409 |
// Ensure we have a page_for_posts setting to avoid empty URL |
| 410 |
if ( get_option( 'page_for_posts' ) ) { |
| 411 |
array_push( $related_urls, get_permalink( get_option( 'page_for_posts' ) ) ); |
| 412 |
} |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
return $related_urls; |
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
/** |
| 421 |
* Get site related info. This could help us to debug. |
| 422 |
* |
| 423 |
* @since 1.0 |
| 424 |
* @return array |
| 425 |
*/ |
| 426 |
function powered_cache_get_debug_info() { |
| 427 |
global $wpdb; |
| 428 |
if ( ! function_exists( 'get_plugins' ) ) { |
| 429 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 430 |
} |
| 431 |
|
| 432 |
$active_plugins_option = get_option( 'active_plugins' ); |
| 433 |
$active_plugins = array(); |
| 434 |
$plugins = get_plugins(); |
| 435 |
|
| 436 |
foreach ( $active_plugins_option as $aplugin ) { |
| 437 |
$active_plugins[ $plugins[ $aplugin ]['Name'] ] = array( |
| 438 |
'file' => $aplugin, |
| 439 |
'Name' => $plugins[ $aplugin ]['Name'], |
| 440 |
'Version' => $plugins[ $aplugin ]['Version'], |
| 441 |
); |
| 442 |
} |
| 443 |
|
| 444 |
|
| 445 |
$theme = wp_get_theme(); |
| 446 |
|
| 447 |
$theme_info = array( |
| 448 |
'Name' => $theme->get( 'Name' ), |
| 449 |
'Version' => $theme->get( 'Version' ), |
| 450 |
'ThemeURI' => $theme->get( 'ThemeURI' ), |
| 451 |
); |
| 452 |
|
| 453 |
|
| 454 |
$mysql_version = $wpdb->get_var( 'select version() as mysqlversion' ); |
| 455 |
$php_version = phpversion(); |
| 456 |
|
| 457 |
$debug_info['php_version'] = $php_version; |
| 458 |
$debug_info['mysql_version'] = $mysql_version; |
| 459 |
$debug_info['active_plugins'] = $active_plugins; |
| 460 |
$debug_info['active_theme'] = $theme_info; |
| 461 |
$debug_info['is_multisite'] = is_multisite(); |
| 462 |
|
| 463 |
if ( is_multisite() && function_exists( 'is_subdomain_install' ) ) { |
| 464 |
$debug_info['is_subdomain_install'] = is_subdomain_install(); |
| 465 |
} |
| 466 |
|
| 467 |
return $debug_info; |
| 468 |
} |
| 469 |
|
| 470 |
|
| 471 |
/** |
| 472 |
* Is saving options now? |
| 473 |
* |
| 474 |
* @since 1.0 |
| 475 |
* @return bool |
| 476 |
*/ |
| 477 |
function powered_cache_is_saving_options() { |
| 478 |
if ( defined( 'POWERED_CACHE_SAVING_OPTIONS' ) && true === POWERED_CACHE_SAVING_OPTIONS ) { |
| 479 |
return true; |
| 480 |
} |
| 481 |
|
| 482 |
return false; |
| 483 |
} |
| 484 |
|
| 485 |
|
| 486 |
if ( ! function_exists( 'boolval' ) ) { |
| 487 |
/** |
| 488 |
* For compatible reason. |
| 489 |
* If we create compat file someday this function should move there |
| 490 |
* |
| 491 |
* @since 1.0 |
| 492 |
* @param $val |
| 493 |
* |
| 494 |
* @return bool |
| 495 |
*/ |
| 496 |
function boolval( $val ) { |
| 497 |
return (bool) $val; |
| 498 |
} |
| 499 |
} |