PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / trunk
GiveWP – Donation Plugin and Fundraising Platform vtrunk
4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 2.31.0 2.31.1 2.32.0 2.33.0 2.33.1 2.33.2 2.33.3 2.33.4 2.33.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.5.0 2.5.1 2.5.10 2.5.11 2.5.12 2.5.13 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6.0 2.6.1 2.6.2 2.6.3 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.9.0 2.9.1 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.10.0 3.11.0 3.12.0 3.12.1 3.12.2 3.12.3 3.13.0 3.14.0 3.14.1 3.14.2 3.15.0 3.15.1 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.17.0 3.17.1 3.17.2 3.18.0 3.19.0 3.19.1 3.19.2 3.19.3 3.19.4 3.2.0 3.2.1 3.2.2 3.20.0 3.21.0 3.21.1 3.22.0 3.22.1 3.22.2 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.7.0 3.8.0 3.9.0 4.0.0 4.1.0 4.1.1 4.10.0 4.10.1 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.14.5 4.14.6 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.1 4.7.0 4.7.1 4.8.0 4.8.1 4.9.0 trunk 1.9.0 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.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.2 2.11.3 2.12.0 2.12.1 2.12.2 2.12.3 2.13.0 2.13.1 2.13.2 2.13.3 2.13.4 2.14.0 2.15.0 2.16.0 2.16.1 2.17.0 2.17.1 2.17.3 2.18.0 2.18.1 2.19.1 2.19.2 2.19.3 2.19.4 2.19.5 2.19.6 2.19.7 2.19.8 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.20.0 2.20.1 2.20.2 2.21.0 2.21.1 2.21.2 2.21.3 2.21.4 2.22.0 2.22.1 2.22.2 2.22.3 2.23.0 2.23.1 2.23.2 2.24.0 2.24.1 2.24.2 2.25.0 2.25.1 2.25.2 2.25.3 2.26.0 2.27.0 2.27.1 2.27.2 2.27.3 2.28.0 2.29.0 2.29.1 2.29.2
give / includes / class-give-cache.php
give / includes Last commit date
admin 1 day ago api 3 years ago database 5 months ago deprecated 1 month ago donors 5 months ago emails 9 months ago forms 1 day ago frontend 6 years ago gateways 9 months ago libraries 9 months ago payments 2 months ago actions.php 9 months ago ajax-functions.php 3 days ago class-give-async-process.php 1 year ago class-give-background-updater.php 9 months ago class-give-cache-setting.php 1 year ago class-give-cache.php 9 months ago class-give-cli-commands.php 1 year ago class-give-comment.php 9 months ago class-give-cron.php 9 months ago class-give-donate-form.php 1 year ago class-give-donor.php 2 years ago class-give-email-access.php 5 years ago class-give-license-handler.php 1 month ago class-give-logging.php 9 months ago class-give-readme-parser.php 4 years ago class-give-roles.php 5 months ago class-give-scripts.php 2 weeks ago class-give-session.php 9 months 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 4 years ago class-notices.php 9 months ago country-functions.php 7 months ago currencies-list.php 7 months ago currency-functions.php 3 years ago error-tracking.php 6 years ago filters.php 9 months ago formatting.php 9 months ago install.php 9 months ago login-register.php 2 years ago misc-functions.php 1 month ago plugin-compatibility.php 6 years ago post-types.php 1 year ago price-functions.php 6 years ago process-donation.php 1 year ago setting-functions.php 6 years ago shortcodes.php 1 year ago template-functions.php 1 year ago user-functions.php 3 years ago
class-give-cache.php
809 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 4.9.0 rename function - PHP 8 compatibility
153 * @since 2.0.5
154 * @access public
155 * @credit WooCommerce
156 */
157 public function notices() {
158 if ( ! function_exists( 'w3tc_pgcache_flush' ) || ! function_exists( 'w3_instance' ) ) {
159 return;
160 }
161
162 $config = w3_instance( 'W3_Config' );
163 $enabled = $config->get_integer( 'dbcache.enabled' );
164 $settings = array_map( 'trim', $config->get_array( 'dbcache.reject.sql' ) );
165
166 if ( $enabled && ! in_array( 'give', $settings, true ) ) {
167 ?>
168 <div class="error">
169 <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>
170 </div>
171 <?php
172 }
173 }
174
175 /**
176 * Get cache key.
177 *
178 * @since 1.8.7
179 *
180 * @param string $action Cache key prefix.
181 * @param array $query_args (optional) Query array.
182 * @param bool $is_prefix
183 *
184 * @return string
185 */
186 public static function get_key( $action, $query_args = null, $is_prefix = true ) {
187 // Bailout.
188 if ( empty( $action ) ) {
189 return new WP_Error( 'give_invalid_cache_key_action', __( 'Do not pass empty action to generate cache key.', 'give' ) );
190 }
191
192 // Set cache key.
193 $cache_key = $is_prefix ? "give_cache_{$action}" : $action;
194
195 // Bailout.
196 if ( ! empty( $query_args ) ) {
197 $cache_key = "{$cache_key}_" . substr( md5( serialize( $query_args ) ), 0, 15 );
198 }
199
200 /**
201 * Filter the cache key name.
202 *
203 * @since 2.0
204 */
205 return apply_filters( 'give_get_cache_key', $cache_key, $action, $query_args );
206 }
207
208 /**
209 * Get cache.
210 *
211 * @since 1.8.7
212 *
213 * @param string $cache_key
214 * @param bool $custom_key
215 * @param mixed $query_args
216 *
217 * @return mixed
218 */
219 public static function get( $cache_key, $custom_key = false, $query_args = array() ) {
220 if ( ! self::is_valid_cache_key( $cache_key ) ) {
221 if ( empty( $cache_key ) ) {
222 return new WP_Error( 'give_empty_cache_key', __( 'Do not pass invalid empty cache key', 'give' ) );
223 } elseif ( ! $custom_key ) {
224 return new WP_Error( 'give_invalid_cache_key', __( 'Cache key format should be give_cache_*', 'give' ) );
225 }
226
227 $cache_key = self::get_key( $cache_key, $query_args );
228 }
229
230 $option = get_option( $cache_key );
231
232 // Backward compatibility (<1.8.7).
233 if ( ! is_array( $option ) || empty( $option ) || ! array_key_exists( 'expiration', $option ) ) {
234 return $option;
235 }
236
237 // Get current time.
238 $current_time = time();
239
240 if ( empty( $option['expiration'] ) || ( $current_time < $option['expiration'] ) ) {
241 $option = $option['data'];
242 } else {
243 $option = false;
244 }
245
246 return $option;
247 }
248
249 /**
250 * Set cache.
251 *
252 * @since 1.8.7
253 *
254 * @param string $cache_key
255 * @param mixed $data
256 * @param int|null $expiration Timestamp should be in GMT format.
257 * @param bool $custom_key
258 * @param mixed $query_args
259 *
260 * @return mixed
261 */
262 public static function set( $cache_key, $data, $expiration = null, $custom_key = false, $query_args = array() ) {
263 if ( ! self::is_valid_cache_key( $cache_key ) ) {
264 if ( ! $custom_key ) {
265 return new WP_Error( 'give_invalid_cache_key', __( 'Cache key format should be give_cache_*', 'give' ) );
266 }
267
268 $cache_key = self::get_key( $cache_key, $query_args );
269 }
270
271 $option_value = array(
272 'data' => $data,
273 'expiration' => ! is_null( $expiration )
274 ? ( $expiration + time() )
275 : null,
276 );
277
278 $result = update_option( $cache_key, $option_value, false );
279
280 return $result;
281 }
282
283 /**
284 * Delete cache.
285 *
286 * Note: only for internal use
287 *
288 * @since 1.8.7
289 *
290 * @param string|array $cache_keys
291 *
292 * @return bool|WP_Error
293 */
294 public static function delete( $cache_keys ) {
295 $result = true;
296 $invalid_keys = array();
297
298 if ( ! empty( $cache_keys ) ) {
299 $cache_keys = is_array( $cache_keys ) ? $cache_keys : array( $cache_keys );
300
301 foreach ( $cache_keys as $cache_key ) {
302 if ( ! self::is_valid_cache_key( $cache_key ) ) {
303 $invalid_keys[] = $cache_key;
304 $result = false;
305 }
306
307 delete_option( $cache_key );
308 }
309 }
310
311 if ( ! $result ) {
312 $result = new WP_Error(
313 'give_invalid_cache_key',
314 __( 'Cache key format should be give_cache_*', 'give' ),
315 $invalid_keys
316 );
317 }
318
319 return $result;
320 }
321
322 /**
323 * Delete all logging cache.
324 *
325 * Note: only for internal use
326 *
327 * @since 1.8.7
328 * @access public
329 * @global wpdb $wpdb
330 *
331 * @param bool $force If set to true then all cached values will be delete instead of only expired
332 *
333 * @return bool
334 */
335 public static function delete_all_expired( $force = false ) {
336 global $wpdb;
337 $options = $wpdb->get_results(
338 $wpdb->prepare(
339 "SELECT option_name, option_value
340 FROM {$wpdb->options}
341 Where option_name
342 LIKE '%s'",
343 '%give_cache%'
344 ),
345 ARRAY_A
346 );
347
348 // Bailout.
349 if ( empty( $options ) ) {
350 return false;
351 }
352
353 $current_time = time();
354
355 // Delete log cache.
356 foreach ( $options as $option ) {
357 $option['option_value'] = maybe_unserialize( $option['option_value'] );
358
359 if (
360 (
361 ! self::is_valid_cache_key( $option['option_name'] )
362 || ! is_array( $option['option_value'] ) // Backward compatibility (<1.8.7).
363 || ! array_key_exists( 'expiration', $option['option_value'] ) // Backward compatibility (<1.8.7).
364 || empty( $option['option_value']['expiration'] )
365 || ( $current_time < $option['option_value']['expiration'] )
366 )
367 && ! $force
368 ) {
369 continue;
370 }
371
372 self::delete( $option['option_name'] );
373 }
374 }
375
376
377 /**
378 * Get list of options like.
379 *
380 * Note: only for internal use
381 *
382 * @since 1.8.7
383 * @access public
384 *
385 * @param string $option_name
386 * @param bool $fields
387 *
388 * @return array
389 */
390 public static function get_options_like( $option_name, $fields = false ) {
391 global $wpdb;
392
393 $field_names = $fields ? 'option_name, option_value' : 'option_name';
394
395 if ( $fields ) {
396 $options = $wpdb->get_results(
397 $wpdb->prepare(
398 "SELECT {$field_names }
399 FROM {$wpdb->options}
400 Where option_name
401 LIKE '%s'",
402 "%give_cache_{$option_name}%"
403 ),
404 ARRAY_A
405 );
406 } else {
407 $options = $wpdb->get_col(
408 $wpdb->prepare(
409 "SELECT *
410 FROM {$wpdb->options}
411 Where option_name
412 LIKE '%s'",
413 "%give_cache_{$option_name}%"
414 ),
415 1
416 );
417 }
418
419 if ( ! empty( $options ) && $fields ) {
420 foreach ( $options as $index => $option ) {
421 $option['option_value'] = maybe_unserialize( $option['option_value'] );
422 $options[ $index ] = $option;
423 }
424 }
425
426 return $options;
427 }
428
429 /**
430 * Check cache key validity.
431 *
432 * @since 1.8.7
433 * @access public
434 *
435 * @param $cache_key
436 *
437 * @return bool
438 */
439 public static function is_valid_cache_key( $cache_key ) {
440 $is_valid = ( false !== strpos( $cache_key, 'give_cache_' ) );
441
442 /**
443 * Filter the flag which tell about cache key valid or not
444 *
445 * @since 2.0
446 */
447 return apply_filters( 'give_is_valid_cache_key', $is_valid, $cache_key );
448 }
449
450
451 /**
452 * Get cache from group
453 *
454 * @since 2.0
455 * @access public
456 *
457 * @param int $id
458 * @param string $group
459 *
460 * @return mixed
461 */
462 public static function get_group( $id, $group = '' ) {
463 $cached_data = null;
464
465 // Bailout.
466 if ( self::$instance->is_cache && ! empty( $id ) ) {
467 $group = self::$instance->filter_group_name( $group );
468
469 $cached_data = wp_cache_get( $id, $group );
470 $cached_data = false !== $cached_data ? $cached_data : null;
471 }
472
473 return $cached_data;
474 }
475
476 /**
477 * Cache small chunks inside group
478 *
479 * @since 2.0
480 * @access public
481 *
482 * @param int $id
483 * @param mixed $data
484 * @param string $group
485 * @param int $expire
486 *
487 * @return bool
488 */
489 public static function set_group( $id, $data, $group = '', $expire = 0 ) {
490 $status = false;
491
492 // Bailout.
493 if ( ! self::$instance->is_cache || empty( $id ) ) {
494 return $status;
495 }
496
497 $group = self::$instance->filter_group_name( $group );
498
499 $status = wp_cache_set( $id, $data, $group, $expire );
500
501 return $status;
502 }
503
504 /**
505 * Cache small db query chunks inside group
506 *
507 * @since 2.0
508 * @access public
509 *
510 * @param int $id
511 * @param mixed $data
512 *
513 * @return bool
514 */
515 public static function set_db_query( $id, $data ) {
516 $status = false;
517
518 // Bailout.
519 if ( ! self::$instance->is_cache || empty( $id ) ) {
520 return $status;
521 }
522
523 return self::set_group( $id, $data, 'give-db-queries', 0 );
524 }
525
526 /**
527 * Get cache from group
528 *
529 * @since 2.0
530 * @access public
531 *
532 * @param string $id
533 *
534 * @return mixed
535 */
536 public static function get_db_query( $id ) {
537 return self::get_group( $id, 'give-db-queries' );
538 }
539
540 /**
541 * Delete group cache
542 *
543 * @since 2.0
544 * @access public
545 *
546 * @param int|array $ids
547 * @param string $group
548 * @param int $expire
549 *
550 * @return bool
551 */
552 public static function delete_group( $ids, $group = '', $expire = 0 ) {
553 $status = false;
554
555 // Bailout.
556 if ( ! self::$instance->is_cache || empty( $ids ) ) {
557 return $status;
558 }
559
560 $group_prefix = $group;
561 $group = self::$instance->filter_group_name( $group );
562
563 // Delete single or multiple cache items from cache.
564 if ( ! is_array( $ids ) ) {
565 $status = wp_cache_delete( $ids, $group );
566 self::$instance->get_incrementer( true );
567
568 /**
569 * Fire action when cache deleted for specific id.
570 *
571 * @since 2.0
572 *
573 * @param string $ids
574 * @param string $group
575 * @param int $expire
576 */
577 do_action( "give_deleted_{$group_prefix}_cache", $ids, $group, $expire, $status );
578
579 } else {
580 foreach ( $ids as $id ) {
581 $status = wp_cache_delete( $id, $group );
582 self::$instance->get_incrementer( true );
583
584 /**
585 * Fire action when cache deleted for specific id .
586 *
587 * @since 2.0
588 *
589 * @param string $ids
590 * @param string $group
591 * @param int $expire
592 */
593 do_action( "give_deleted_{$group_prefix}_cache", $id, $group, $expire, $status );
594 }
595 }
596
597 return $status;
598 }
599
600
601 /**
602 * Delete form related cache
603 * Note: only use for internal purpose.
604 *
605 * @since 2.0
606 * @access public
607 *
608 * @param int $form_id
609 */
610 public function delete_form_related_cache( $form_id ) {
611 // If this is just a revision, don't send the email.
612 if ( wp_is_post_revision( $form_id ) ) {
613 return;
614 }
615
616 self::flush_cache( true );
617 }
618
619 /**
620 * Delete payment related cache
621 * Note: only use for internal purpose.
622 *
623 * @since 2.0
624 * @access public
625 *
626 * @param int $donation_id
627 */
628 public function delete_payment_related_cache( $donation_id ) {
629 // If this is just a revision, don't send the email.
630 if ( wp_is_post_revision( $donation_id ) ) {
631 return;
632 }
633
634 if ( $donation_id && ( $donor_id = give_get_payment_donor_id( $donation_id ) ) ) {
635 wp_cache_delete( $donor_id, $this->filter_group_name( 'give-donors' ) );
636 }
637
638 wp_cache_delete( $donation_id, $this->filter_group_name( 'give-donations' ) );
639
640 self::$instance->get_incrementer( true );
641 }
642
643 /**
644 * Delete donor related cache
645 * Note: only use for internal purpose.
646 *
647 * @since 2.0
648 * @access public
649 *
650 * @param string $id
651 * @param string $group
652 * @param int $expire
653 */
654 public function delete_donor_related_cache( $id, $group, $expire ) {
655 $donation_ids = Give()->donors->get_column( 'payment_ids', $id );
656
657 if ( ! empty( $donation_ids ) ) {
658 $donation_ids = array_map( 'trim', (array) explode( ',', trim( $donation_ids ) ) );
659
660 foreach ( $donation_ids as $donation ) {
661 wp_cache_delete( $donation, $this->filter_group_name( 'give-donations' ) );
662 }
663 }
664
665 self::$instance->get_incrementer( true );
666 }
667
668 /**
669 * Delete donations related cache
670 * Note: only use for internal purpose.
671 *
672 * @since 2.0
673 * @access public
674 *
675 * @param string $id
676 * @param string $group
677 * @param int $expire
678 */
679 public function delete_donations_related_cache( $id, $group, $expire ) {
680 if ( $id && ( $donor_id = give_get_payment_donor_id( $id ) ) ) {
681 wp_cache_delete( $donor_id, $this->filter_group_name( 'give-donors' ) );
682 }
683
684 self::$instance->get_incrementer( true );
685 }
686
687
688 /**
689 * Get unique incrementer.
690 *
691 * @see https://core.trac.wordpress.org/ticket/4476
692 * @see https://www.tollmanz.com/invalidation-schemes/
693 *
694 * @since 2.0
695 * @access public
696 *
697 * @param bool $refresh
698 * @param string $incrementer_key
699 *
700 * @return string
701 */
702 public function get_incrementer( $refresh = false, $incrementer_key = 'give-cache-incrementer-db-queries' ) {
703 $incrementer_value = wp_cache_get( $incrementer_key );
704
705 if ( false === $incrementer_value || true === $refresh ) {
706 $incrementer_value = (string) microtime( true );
707 wp_cache_set( $incrementer_key, $incrementer_value );
708 }
709
710 return $incrementer_value;
711 }
712
713
714 /**
715 * Flush cache on cache setting enable/disable
716 * Note: only for internal use
717 *
718 * @since 2.0
719 * @access public
720 *
721 * @param bool $force Delete cache forcefully.
722 *
723 * @return bool
724 */
725 public static function flush_cache( $force = false ) {
726 if (
727 $force
728 || ( Give_Admin_Settings::is_saving_settings() && isset( $_POST['cache'] ) && give_is_setting_enabled( give_clean( $_POST['cache'] ) ) )
729 || ( wp_doing_ajax() && isset( $_GET['action'] ) && 'give_cache_flush' === give_clean( $_GET['action'] ) )
730 ) {
731 self::$instance->get_incrementer( true );
732 self::$instance->get_incrementer( true, 'give-cache-incrementer' );
733
734 /**
735 * Fire the action when all cache deleted.
736 *
737 * @since 2.1.0
738 */
739 do_action( 'give_flushed_cache' );
740
741 return true;
742 }
743
744 return false;
745 }
746
747
748 /**
749 * Filter the group name
750 *
751 * @since 2.0
752 * @access private
753 *
754 * @param $group
755 *
756 * @return mixed
757 */
758 private function filter_group_name( $group ) {
759 /**
760 * Filter the group name
761 *
762 * @since 2.1.0
763 */
764 $filtered_group = apply_filters( 'give_cache_filter_group_name', '', $group );
765
766 if ( empty( $filtered_group ) ) {
767
768 switch ( $group ) {
769 case 'give-db-queries':
770 $incrementer = self::$instance->get_incrementer();
771 break;
772
773 default:
774 $incrementer = self::$instance->get_incrementer( false, 'give-cache-incrementer' );
775
776 }
777
778 $current_blog_id = get_current_blog_id();
779 $filtered_group = "{$group}_{$current_blog_id}_{$incrementer}";
780 }
781
782 return $filtered_group;
783 }
784
785
786 /**
787 * Disable cache.
788 *
789 * @since 2.0
790 * @access public
791 */
792 public static function disable() {
793 self::get_instance()->is_cache = false;
794 }
795
796 /**
797 * Enable cache.
798 *
799 * @since 2.0
800 * @access public
801 */
802 public static function enable() {
803 self::get_instance()->is_cache = true;
804 }
805 }
806
807 // Initialize
808 Give_Cache::get_instance()->setup();
809