admin
5 years ago
api
5 years ago
database
6 years ago
deprecated
5 years ago
donors
5 years ago
emails
6 years ago
forms
5 years ago
frontend
6 years ago
gateways
5 years ago
libraries
6 years ago
payments
5 years ago
actions.php
6 years ago
ajax-functions.php
5 years ago
class-give-async-process.php
6 years ago
class-give-background-updater.php
6 years ago
class-give-cache-setting.php
5 years ago
class-give-cache.php
6 years ago
class-give-cli-commands.php
6 years ago
class-give-comment.php
6 years ago
class-give-cron.php
6 years ago
class-give-donate-form.php
6 years ago
class-give-donor.php
6 years ago
class-give-email-access.php
6 years ago
class-give-license-handler.php
5 years ago
class-give-logging.php
6 years ago
class-give-readme-parser.php
6 years ago
class-give-roles.php
6 years ago
class-give-scripts.php
5 years ago
class-give-session.php
5 years ago
class-give-stats.php
6 years ago
class-give-template-loader.php
6 years ago
class-give-tooltips.php
6 years ago
class-give-translation.php
6 years ago
class-notices.php
5 years ago
country-functions.php
5 years ago
currencies-list.php
6 years ago
currency-functions.php
6 years ago
error-tracking.php
6 years ago
filters.php
6 years ago
formatting.php
6 years ago
install.php
5 years ago
login-register.php
6 years ago
misc-functions.php
5 years ago
plugin-compatibility.php
6 years ago
post-types.php
5 years ago
price-functions.php
6 years ago
process-donation.php
5 years ago
setting-functions.php
6 years ago
shortcodes.php
6 years ago
template-functions.php
6 years ago
user-functions.php
6 years ago
class-give-cache.php
808 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class for managing cache |
| 4 | * Note: only use for internal purpose. |
| 5 | * |
| 6 | * @package Give |
| 7 | * @subpackage Classes/Give_Cache |
| 8 | * @copyright Copyright (c) 2017, GiveWP |
| 9 | * @license https://opensource.org/licenses/gpl-license GNU Public License |
| 10 | * @since 1.8.7 |
| 11 | */ |
| 12 | |
| 13 | // Exit if accessed directly. |
| 14 | if ( ! defined( 'ABSPATH' ) ) { |
| 15 | exit; |
| 16 | } |
| 17 | |
| 18 | class Give_Cache { |
| 19 | /** |
| 20 | * Instance. |
| 21 | * |
| 22 | * @since 1.8.7 |
| 23 | * @access private |
| 24 | * @var Give_Cache |
| 25 | */ |
| 26 | private static $instance; |
| 27 | |
| 28 | /** |
| 29 | * Flag to check if caching enabled or not. |
| 30 | * |
| 31 | * @since 2.0 |
| 32 | * @access private |
| 33 | * @var |
| 34 | */ |
| 35 | private $is_cache; |
| 36 | |
| 37 | /** |
| 38 | * Singleton pattern. |
| 39 | * |
| 40 | * @since 1.8.7 |
| 41 | * @access private |
| 42 | * Give_Cache constructor. |
| 43 | */ |
| 44 | private function __construct() { |
| 45 | } |
| 46 | |
| 47 | |
| 48 | /** |
| 49 | * Get instance. |
| 50 | * |
| 51 | * @since 1.8.7 |
| 52 | * @access public |
| 53 | * @return static |
| 54 | */ |
| 55 | public static function get_instance() { |
| 56 | if ( ! isset( self::$instance ) && ! ( self::$instance instanceof Give_Cache ) ) { |
| 57 | self::$instance = new Give_Cache(); |
| 58 | } |
| 59 | |
| 60 | return self::$instance; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Setup hooks. |
| 65 | * |
| 66 | * @since 1.8.7 |
| 67 | * @access public |
| 68 | */ |
| 69 | public function setup() { |
| 70 | // Currently enable cache only for backend. |
| 71 | self::$instance->is_cache = defined( 'GIVE_CACHE' ) ? GIVE_CACHE : $this->is_cache_enabled(); |
| 72 | |
| 73 | // weekly delete all expired cache. |
| 74 | Give_Cron::add_weekly_event( array( $this, 'delete_all_expired' ) ); |
| 75 | |
| 76 | add_action( 'save_post_give_forms', array( $this, 'delete_form_related_cache' ) ); |
| 77 | add_action( 'save_post_give_payment', array( $this, 'delete_payment_related_cache' ) ); |
| 78 | add_action( 'give_deleted_give-donors_cache', array( $this, 'delete_donor_related_cache' ), 10, 3 ); |
| 79 | add_action( 'give_deleted_give-donations_cache', array( $this, 'delete_donations_related_cache' ), 10, 3 ); |
| 80 | |
| 81 | add_action( 'give_save_settings_give_settings', array( __CLASS__, 'flush_cache' ) ); |
| 82 | |
| 83 | add_action( 'wp', array( __CLASS__, 'prevent_caching' ) ); |
| 84 | add_action( 'admin_notices', array( $this, '__notices' ) ); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Get result if cache enabled or not. |
| 89 | * |
| 90 | * @return bool |
| 91 | * @since 2.6.1 |
| 92 | */ |
| 93 | private function is_cache_enabled() { |
| 94 | return give_is_setting_enabled( give_get_option( 'cache', 'enabled' ) ) && ( is_admin() || false !== strpos( $_SERVER['REQUEST_URI'], rest_get_url_prefix() . '/give-api/' ) ); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Prevent caching on certain pages |
| 99 | * |
| 100 | * @since 2.0.5 |
| 101 | * @access public |
| 102 | * @credit WooCommerce |
| 103 | */ |
| 104 | public static function prevent_caching() { |
| 105 | if ( ! is_blog_installed() ) { |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | $page_ids = array_filter( |
| 110 | array( |
| 111 | give_get_option( 'success_page' ), |
| 112 | give_get_option( 'failure_page' ), |
| 113 | give_get_option( 'history_page' ), |
| 114 | ) |
| 115 | ); |
| 116 | |
| 117 | /** |
| 118 | * Use this filter to prevent |
| 119 | * |
| 120 | * @since 2.6.3 |
| 121 | */ |
| 122 | $canCache = apply_filters( 'give_can_cache_page', is_page( $page_ids ) ); |
| 123 | |
| 124 | if ( $canCache ) { |
| 125 | self::set_nocache_constants(); |
| 126 | nocache_headers(); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Set constants to prevent caching by some plugins. |
| 132 | * |
| 133 | * @since 2.0.5 |
| 134 | * @access public |
| 135 | * @credit WooCommerce |
| 136 | * |
| 137 | * @param mixed $return Value to return. Previously hooked into a filter. |
| 138 | * |
| 139 | * @return mixed |
| 140 | */ |
| 141 | public static function set_nocache_constants( $return = true ) { |
| 142 | give_maybe_define_constant( 'DONOTCACHEPAGE', true ); |
| 143 | give_maybe_define_constant( 'DONOTCACHEOBJECT', true ); |
| 144 | give_maybe_define_constant( 'DONOTCACHEDB', true ); |
| 145 | |
| 146 | return $return; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Notices function. |
| 151 | * |
| 152 | * @since 2.0.5 |
| 153 | * @access public |
| 154 | * @credit WooCommerce |
| 155 | */ |
| 156 | public function __notices() { |
| 157 | if ( ! function_exists( 'w3tc_pgcache_flush' ) || ! function_exists( 'w3_instance' ) ) { |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | $config = w3_instance( 'W3_Config' ); |
| 162 | $enabled = $config->get_integer( 'dbcache.enabled' ); |
| 163 | $settings = array_map( 'trim', $config->get_array( 'dbcache.reject.sql' ) ); |
| 164 | |
| 165 | if ( $enabled && ! in_array( 'give', $settings, true ) ) { |
| 166 | ?> |
| 167 | <div class="error"> |
| 168 | <p><?php echo wp_kses_post( sprintf( __( 'In order for <strong>database caching</strong> to work with GiveWP you must add %1$s to the "Ignored query stems" option in <a href="%2$s">W3 Total Cache settings</a>.', 'give' ), '<code>give</code>', esc_url( admin_url( 'admin.php?page=w3tc_dbcache#dbcache_reject_sql' ) ) ) ); ?></p> |
| 169 | </div> |
| 170 | <?php |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Get cache key. |
| 176 | * |
| 177 | * @since 1.8.7 |
| 178 | * |
| 179 | * @param string $action Cache key prefix. |
| 180 | * @param array $query_args (optional) Query array. |
| 181 | * @param bool $is_prefix |
| 182 | * |
| 183 | * @return string |
| 184 | */ |
| 185 | public static function get_key( $action, $query_args = null, $is_prefix = true ) { |
| 186 | // Bailout. |
| 187 | if ( empty( $action ) ) { |
| 188 | return new WP_Error( 'give_invalid_cache_key_action', __( 'Do not pass empty action to generate cache key.', 'give' ) ); |
| 189 | } |
| 190 | |
| 191 | // Set cache key. |
| 192 | $cache_key = $is_prefix ? "give_cache_{$action}" : $action; |
| 193 | |
| 194 | // Bailout. |
| 195 | if ( ! empty( $query_args ) ) { |
| 196 | $cache_key = "{$cache_key}_" . substr( md5( serialize( $query_args ) ), 0, 15 ); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Filter the cache key name. |
| 201 | * |
| 202 | * @since 2.0 |
| 203 | */ |
| 204 | return apply_filters( 'give_get_cache_key', $cache_key, $action, $query_args ); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Get cache. |
| 209 | * |
| 210 | * @since 1.8.7 |
| 211 | * |
| 212 | * @param string $cache_key |
| 213 | * @param bool $custom_key |
| 214 | * @param mixed $query_args |
| 215 | * |
| 216 | * @return mixed |
| 217 | */ |
| 218 | public static function get( $cache_key, $custom_key = false, $query_args = array() ) { |
| 219 | if ( ! self::is_valid_cache_key( $cache_key ) ) { |
| 220 | if ( empty( $cache_key ) ) { |
| 221 | return new WP_Error( 'give_empty_cache_key', __( 'Do not pass invalid empty cache key', 'give' ) ); |
| 222 | } elseif ( ! $custom_key ) { |
| 223 | return new WP_Error( 'give_invalid_cache_key', __( 'Cache key format should be give_cache_*', 'give' ) ); |
| 224 | } |
| 225 | |
| 226 | $cache_key = self::get_key( $cache_key, $query_args ); |
| 227 | } |
| 228 | |
| 229 | $option = get_option( $cache_key ); |
| 230 | |
| 231 | // Backward compatibility (<1.8.7). |
| 232 | if ( ! is_array( $option ) || empty( $option ) || ! array_key_exists( 'expiration', $option ) ) { |
| 233 | return $option; |
| 234 | } |
| 235 | |
| 236 | // Get current time. |
| 237 | $current_time = time(); |
| 238 | |
| 239 | if ( empty( $option['expiration'] ) || ( $current_time < $option['expiration'] ) ) { |
| 240 | $option = $option['data']; |
| 241 | } else { |
| 242 | $option = false; |
| 243 | } |
| 244 | |
| 245 | return $option; |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Set cache. |
| 250 | * |
| 251 | * @since 1.8.7 |
| 252 | * |
| 253 | * @param string $cache_key |
| 254 | * @param mixed $data |
| 255 | * @param int|null $expiration Timestamp should be in GMT format. |
| 256 | * @param bool $custom_key |
| 257 | * @param mixed $query_args |
| 258 | * |
| 259 | * @return mixed |
| 260 | */ |
| 261 | public static function set( $cache_key, $data, $expiration = null, $custom_key = false, $query_args = array() ) { |
| 262 | if ( ! self::is_valid_cache_key( $cache_key ) ) { |
| 263 | if ( ! $custom_key ) { |
| 264 | return new WP_Error( 'give_invalid_cache_key', __( 'Cache key format should be give_cache_*', 'give' ) ); |
| 265 | } |
| 266 | |
| 267 | $cache_key = self::get_key( $cache_key, $query_args ); |
| 268 | } |
| 269 | |
| 270 | $option_value = array( |
| 271 | 'data' => $data, |
| 272 | 'expiration' => ! is_null( $expiration ) |
| 273 | ? ( $expiration + time() ) |
| 274 | : null, |
| 275 | ); |
| 276 | |
| 277 | $result = update_option( $cache_key, $option_value, false ); |
| 278 | |
| 279 | return $result; |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Delete cache. |
| 284 | * |
| 285 | * Note: only for internal use |
| 286 | * |
| 287 | * @since 1.8.7 |
| 288 | * |
| 289 | * @param string|array $cache_keys |
| 290 | * |
| 291 | * @return bool|WP_Error |
| 292 | */ |
| 293 | public static function delete( $cache_keys ) { |
| 294 | $result = true; |
| 295 | $invalid_keys = array(); |
| 296 | |
| 297 | if ( ! empty( $cache_keys ) ) { |
| 298 | $cache_keys = is_array( $cache_keys ) ? $cache_keys : array( $cache_keys ); |
| 299 | |
| 300 | foreach ( $cache_keys as $cache_key ) { |
| 301 | if ( ! self::is_valid_cache_key( $cache_key ) ) { |
| 302 | $invalid_keys[] = $cache_key; |
| 303 | $result = false; |
| 304 | } |
| 305 | |
| 306 | delete_option( $cache_key ); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | if ( ! $result ) { |
| 311 | $result = new WP_Error( |
| 312 | 'give_invalid_cache_key', |
| 313 | __( 'Cache key format should be give_cache_*', 'give' ), |
| 314 | $invalid_keys |
| 315 | ); |
| 316 | } |
| 317 | |
| 318 | return $result; |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * Delete all logging cache. |
| 323 | * |
| 324 | * Note: only for internal use |
| 325 | * |
| 326 | * @since 1.8.7 |
| 327 | * @access public |
| 328 | * @global wpdb $wpdb |
| 329 | * |
| 330 | * @param bool $force If set to true then all cached values will be delete instead of only expired |
| 331 | * |
| 332 | * @return bool |
| 333 | */ |
| 334 | public static function delete_all_expired( $force = false ) { |
| 335 | global $wpdb; |
| 336 | $options = $wpdb->get_results( |
| 337 | $wpdb->prepare( |
| 338 | "SELECT option_name, option_value |
| 339 | FROM {$wpdb->options} |
| 340 | Where option_name |
| 341 | LIKE '%%%s%%'", |
| 342 | 'give_cache' |
| 343 | ), |
| 344 | ARRAY_A |
| 345 | ); |
| 346 | |
| 347 | // Bailout. |
| 348 | if ( empty( $options ) ) { |
| 349 | return false; |
| 350 | } |
| 351 | |
| 352 | $current_time = time(); |
| 353 | |
| 354 | // Delete log cache. |
| 355 | foreach ( $options as $option ) { |
| 356 | $option['option_value'] = maybe_unserialize( $option['option_value'] ); |
| 357 | |
| 358 | if ( |
| 359 | ( |
| 360 | ! self::is_valid_cache_key( $option['option_name'] ) |
| 361 | || ! is_array( $option['option_value'] ) // Backward compatibility (<1.8.7). |
| 362 | || ! array_key_exists( 'expiration', $option['option_value'] ) // Backward compatibility (<1.8.7). |
| 363 | || empty( $option['option_value']['expiration'] ) |
| 364 | || ( $current_time < $option['option_value']['expiration'] ) |
| 365 | ) |
| 366 | && ! $force |
| 367 | ) { |
| 368 | continue; |
| 369 | } |
| 370 | |
| 371 | self::delete( $option['option_name'] ); |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | |
| 376 | /** |
| 377 | * Get list of options like. |
| 378 | * |
| 379 | * Note: only for internal use |
| 380 | * |
| 381 | * @since 1.8.7 |
| 382 | * @access public |
| 383 | * |
| 384 | * @param string $option_name |
| 385 | * @param bool $fields |
| 386 | * |
| 387 | * @return array |
| 388 | */ |
| 389 | public static function get_options_like( $option_name, $fields = false ) { |
| 390 | global $wpdb; |
| 391 | |
| 392 | $field_names = $fields ? 'option_name, option_value' : 'option_name'; |
| 393 | |
| 394 | if ( $fields ) { |
| 395 | $options = $wpdb->get_results( |
| 396 | $wpdb->prepare( |
| 397 | "SELECT {$field_names } |
| 398 | FROM {$wpdb->options} |
| 399 | Where option_name |
| 400 | LIKE '%%%s%%'", |
| 401 | "give_cache_{$option_name}" |
| 402 | ), |
| 403 | ARRAY_A |
| 404 | ); |
| 405 | } else { |
| 406 | $options = $wpdb->get_col( |
| 407 | $wpdb->prepare( |
| 408 | "SELECT * |
| 409 | FROM {$wpdb->options} |
| 410 | Where option_name |
| 411 | LIKE '%%%s%%'", |
| 412 | "give_cache_{$option_name}" |
| 413 | ), |
| 414 | 1 |
| 415 | ); |
| 416 | } |
| 417 | |
| 418 | if ( ! empty( $options ) && $fields ) { |
| 419 | foreach ( $options as $index => $option ) { |
| 420 | $option['option_value'] = maybe_unserialize( $option['option_value'] ); |
| 421 | $options[ $index ] = $option; |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | return $options; |
| 426 | } |
| 427 | |
| 428 | /** |
| 429 | * Check cache key validity. |
| 430 | * |
| 431 | * @since 1.8.7 |
| 432 | * @access public |
| 433 | * |
| 434 | * @param $cache_key |
| 435 | * |
| 436 | * @return bool |
| 437 | */ |
| 438 | public static function is_valid_cache_key( $cache_key ) { |
| 439 | $is_valid = ( false !== strpos( $cache_key, 'give_cache_' ) ); |
| 440 | |
| 441 | /** |
| 442 | * Filter the flag which tell about cache key valid or not |
| 443 | * |
| 444 | * @since 2.0 |
| 445 | */ |
| 446 | return apply_filters( 'give_is_valid_cache_key', $is_valid, $cache_key ); |
| 447 | } |
| 448 | |
| 449 | |
| 450 | /** |
| 451 | * Get cache from group |
| 452 | * |
| 453 | * @since 2.0 |
| 454 | * @access public |
| 455 | * |
| 456 | * @param int $id |
| 457 | * @param string $group |
| 458 | * |
| 459 | * @return mixed |
| 460 | */ |
| 461 | public static function get_group( $id, $group = '' ) { |
| 462 | $cached_data = null; |
| 463 | |
| 464 | // Bailout. |
| 465 | if ( self::$instance->is_cache && ! empty( $id ) ) { |
| 466 | $group = self::$instance->filter_group_name( $group ); |
| 467 | |
| 468 | $cached_data = wp_cache_get( $id, $group ); |
| 469 | $cached_data = false !== $cached_data ? $cached_data : null; |
| 470 | } |
| 471 | |
| 472 | return $cached_data; |
| 473 | } |
| 474 | |
| 475 | /** |
| 476 | * Cache small chunks inside group |
| 477 | * |
| 478 | * @since 2.0 |
| 479 | * @access public |
| 480 | * |
| 481 | * @param int $id |
| 482 | * @param mixed $data |
| 483 | * @param string $group |
| 484 | * @param int $expire |
| 485 | * |
| 486 | * @return bool |
| 487 | */ |
| 488 | public static function set_group( $id, $data, $group = '', $expire = 0 ) { |
| 489 | $status = false; |
| 490 | |
| 491 | // Bailout. |
| 492 | if ( ! self::$instance->is_cache || empty( $id ) ) { |
| 493 | return $status; |
| 494 | } |
| 495 | |
| 496 | $group = self::$instance->filter_group_name( $group ); |
| 497 | |
| 498 | $status = wp_cache_set( $id, $data, $group, $expire ); |
| 499 | |
| 500 | return $status; |
| 501 | } |
| 502 | |
| 503 | /** |
| 504 | * Cache small db query chunks inside group |
| 505 | * |
| 506 | * @since 2.0 |
| 507 | * @access public |
| 508 | * |
| 509 | * @param int $id |
| 510 | * @param mixed $data |
| 511 | * |
| 512 | * @return bool |
| 513 | */ |
| 514 | public static function set_db_query( $id, $data ) { |
| 515 | $status = false; |
| 516 | |
| 517 | // Bailout. |
| 518 | if ( ! self::$instance->is_cache || empty( $id ) ) { |
| 519 | return $status; |
| 520 | } |
| 521 | |
| 522 | return self::set_group( $id, $data, 'give-db-queries', 0 ); |
| 523 | } |
| 524 | |
| 525 | /** |
| 526 | * Get cache from group |
| 527 | * |
| 528 | * @since 2.0 |
| 529 | * @access public |
| 530 | * |
| 531 | * @param string $id |
| 532 | * |
| 533 | * @return mixed |
| 534 | */ |
| 535 | public static function get_db_query( $id ) { |
| 536 | return self::get_group( $id, 'give-db-queries' ); |
| 537 | } |
| 538 | |
| 539 | /** |
| 540 | * Delete group cache |
| 541 | * |
| 542 | * @since 2.0 |
| 543 | * @access public |
| 544 | * |
| 545 | * @param int|array $ids |
| 546 | * @param string $group |
| 547 | * @param int $expire |
| 548 | * |
| 549 | * @return bool |
| 550 | */ |
| 551 | public static function delete_group( $ids, $group = '', $expire = 0 ) { |
| 552 | $status = false; |
| 553 | |
| 554 | // Bailout. |
| 555 | if ( ! self::$instance->is_cache || empty( $ids ) ) { |
| 556 | return $status; |
| 557 | } |
| 558 | |
| 559 | $group_prefix = $group; |
| 560 | $group = self::$instance->filter_group_name( $group ); |
| 561 | |
| 562 | // Delete single or multiple cache items from cache. |
| 563 | if ( ! is_array( $ids ) ) { |
| 564 | $status = wp_cache_delete( $ids, $group ); |
| 565 | self::$instance->get_incrementer( true ); |
| 566 | |
| 567 | /** |
| 568 | * Fire action when cache deleted for specific id. |
| 569 | * |
| 570 | * @since 2.0 |
| 571 | * |
| 572 | * @param string $ids |
| 573 | * @param string $group |
| 574 | * @param int $expire |
| 575 | */ |
| 576 | do_action( "give_deleted_{$group_prefix}_cache", $ids, $group, $expire, $status ); |
| 577 | |
| 578 | } else { |
| 579 | foreach ( $ids as $id ) { |
| 580 | $status = wp_cache_delete( $id, $group ); |
| 581 | self::$instance->get_incrementer( true ); |
| 582 | |
| 583 | /** |
| 584 | * Fire action when cache deleted for specific id . |
| 585 | * |
| 586 | * @since 2.0 |
| 587 | * |
| 588 | * @param string $ids |
| 589 | * @param string $group |
| 590 | * @param int $expire |
| 591 | */ |
| 592 | do_action( "give_deleted_{$group_prefix}_cache", $id, $group, $expire, $status ); |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | return $status; |
| 597 | } |
| 598 | |
| 599 | |
| 600 | /** |
| 601 | * Delete form related cache |
| 602 | * Note: only use for internal purpose. |
| 603 | * |
| 604 | * @since 2.0 |
| 605 | * @access public |
| 606 | * |
| 607 | * @param int $form_id |
| 608 | */ |
| 609 | public function delete_form_related_cache( $form_id ) { |
| 610 | // If this is just a revision, don't send the email. |
| 611 | if ( wp_is_post_revision( $form_id ) ) { |
| 612 | return; |
| 613 | } |
| 614 | |
| 615 | self::flush_cache( true ); |
| 616 | } |
| 617 | |
| 618 | /** |
| 619 | * Delete payment related cache |
| 620 | * Note: only use for internal purpose. |
| 621 | * |
| 622 | * @since 2.0 |
| 623 | * @access public |
| 624 | * |
| 625 | * @param int $donation_id |
| 626 | */ |
| 627 | public function delete_payment_related_cache( $donation_id ) { |
| 628 | // If this is just a revision, don't send the email. |
| 629 | if ( wp_is_post_revision( $donation_id ) ) { |
| 630 | return; |
| 631 | } |
| 632 | |
| 633 | if ( $donation_id && ( $donor_id = give_get_payment_donor_id( $donation_id ) ) ) { |
| 634 | wp_cache_delete( $donor_id, $this->filter_group_name( 'give-donors' ) ); |
| 635 | } |
| 636 | |
| 637 | wp_cache_delete( $donation_id, $this->filter_group_name( 'give-donations' ) ); |
| 638 | |
| 639 | self::$instance->get_incrementer( true ); |
| 640 | } |
| 641 | |
| 642 | /** |
| 643 | * Delete donor related cache |
| 644 | * Note: only use for internal purpose. |
| 645 | * |
| 646 | * @since 2.0 |
| 647 | * @access public |
| 648 | * |
| 649 | * @param string $id |
| 650 | * @param string $group |
| 651 | * @param int $expire |
| 652 | */ |
| 653 | public function delete_donor_related_cache( $id, $group, $expire ) { |
| 654 | $donation_ids = Give()->donors->get_column( 'payment_ids', $id ); |
| 655 | |
| 656 | if ( ! empty( $donation_ids ) ) { |
| 657 | $donation_ids = array_map( 'trim', (array) explode( ',', trim( $donation_ids ) ) ); |
| 658 | |
| 659 | foreach ( $donation_ids as $donation ) { |
| 660 | wp_cache_delete( $donation, $this->filter_group_name( 'give-donations' ) ); |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | self::$instance->get_incrementer( true ); |
| 665 | } |
| 666 | |
| 667 | /** |
| 668 | * Delete donations related cache |
| 669 | * Note: only use for internal purpose. |
| 670 | * |
| 671 | * @since 2.0 |
| 672 | * @access public |
| 673 | * |
| 674 | * @param string $id |
| 675 | * @param string $group |
| 676 | * @param int $expire |
| 677 | */ |
| 678 | public function delete_donations_related_cache( $id, $group, $expire ) { |
| 679 | if ( $id && ( $donor_id = give_get_payment_donor_id( $id ) ) ) { |
| 680 | wp_cache_delete( $donor_id, $this->filter_group_name( 'give-donors' ) ); |
| 681 | } |
| 682 | |
| 683 | self::$instance->get_incrementer( true ); |
| 684 | } |
| 685 | |
| 686 | |
| 687 | /** |
| 688 | * Get unique incrementer. |
| 689 | * |
| 690 | * @see https://core.trac.wordpress.org/ticket/4476 |
| 691 | * @see https://www.tollmanz.com/invalidation-schemes/ |
| 692 | * |
| 693 | * @since 2.0 |
| 694 | * @access public |
| 695 | * |
| 696 | * @param bool $refresh |
| 697 | * @param string $incrementer_key |
| 698 | * |
| 699 | * @return string |
| 700 | */ |
| 701 | public function get_incrementer( $refresh = false, $incrementer_key = 'give-cache-incrementer-db-queries' ) { |
| 702 | $incrementer_value = wp_cache_get( $incrementer_key ); |
| 703 | |
| 704 | if ( false === $incrementer_value || true === $refresh ) { |
| 705 | $incrementer_value = (string) microtime( true ); |
| 706 | wp_cache_set( $incrementer_key, $incrementer_value ); |
| 707 | } |
| 708 | |
| 709 | return $incrementer_value; |
| 710 | } |
| 711 | |
| 712 | |
| 713 | /** |
| 714 | * Flush cache on cache setting enable/disable |
| 715 | * Note: only for internal use |
| 716 | * |
| 717 | * @since 2.0 |
| 718 | * @access public |
| 719 | * |
| 720 | * @param bool $force Delete cache forcefully. |
| 721 | * |
| 722 | * @return bool |
| 723 | */ |
| 724 | public static function flush_cache( $force = false ) { |
| 725 | if ( |
| 726 | $force |
| 727 | || ( Give_Admin_Settings::is_saving_settings() && isset( $_POST['cache'] ) && give_is_setting_enabled( give_clean( $_POST['cache'] ) ) ) |
| 728 | || ( wp_doing_ajax() && isset( $_GET['action'] ) && 'give_cache_flush' === give_clean( $_GET['action'] ) ) |
| 729 | ) { |
| 730 | self::$instance->get_incrementer( true ); |
| 731 | self::$instance->get_incrementer( true, 'give-cache-incrementer' ); |
| 732 | |
| 733 | /** |
| 734 | * Fire the action when all cache deleted. |
| 735 | * |
| 736 | * @since 2.1.0 |
| 737 | */ |
| 738 | do_action( 'give_flushed_cache' ); |
| 739 | |
| 740 | return true; |
| 741 | } |
| 742 | |
| 743 | return false; |
| 744 | } |
| 745 | |
| 746 | |
| 747 | /** |
| 748 | * Filter the group name |
| 749 | * |
| 750 | * @since 2.0 |
| 751 | * @access private |
| 752 | * |
| 753 | * @param $group |
| 754 | * |
| 755 | * @return mixed |
| 756 | */ |
| 757 | private function filter_group_name( $group ) { |
| 758 | /** |
| 759 | * Filter the group name |
| 760 | * |
| 761 | * @since 2.1.0 |
| 762 | */ |
| 763 | $filtered_group = apply_filters( 'give_cache_filter_group_name', '', $group ); |
| 764 | |
| 765 | if ( empty( $filtered_group ) ) { |
| 766 | |
| 767 | switch ( $group ) { |
| 768 | case 'give-db-queries': |
| 769 | $incrementer = self::$instance->get_incrementer(); |
| 770 | break; |
| 771 | |
| 772 | default: |
| 773 | $incrementer = self::$instance->get_incrementer( false, 'give-cache-incrementer' ); |
| 774 | |
| 775 | } |
| 776 | |
| 777 | $current_blog_id = get_current_blog_id(); |
| 778 | $filtered_group = "{$group}_{$current_blog_id}_{$incrementer}"; |
| 779 | } |
| 780 | |
| 781 | return $filtered_group; |
| 782 | } |
| 783 | |
| 784 | |
| 785 | /** |
| 786 | * Disable cache. |
| 787 | * |
| 788 | * @since 2.0 |
| 789 | * @access public |
| 790 | */ |
| 791 | public static function disable() { |
| 792 | self::get_instance()->is_cache = false; |
| 793 | } |
| 794 | |
| 795 | /** |
| 796 | * Enable cache. |
| 797 | * |
| 798 | * @since 2.0 |
| 799 | * @access public |
| 800 | */ |
| 801 | public static function enable() { |
| 802 | self::get_instance()->is_cache = true; |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | // Initialize |
| 807 | Give_Cache::get_instance()->setup(); |
| 808 |