66 add_action(
'wp', array( $this,
'schedule_transient_cleanup' ) );
69 add_action(
'gravityview-expired-transients', array( $this,
'delete_expired_transients' ) );
72 add_action(
'gravityview_clear_form_cache', array( $this,
'blocklist_add' ) );
77 add_action(
'gravityview_clear_entry_cache', array( $this,
'entry_property_changed' ) );
79 add_action(
'gform_after_update_entry', array( $this,
'entry_updated' ), 10, 2 );
81 add_action(
'gform_entry_created', array( $this,
'entry_created' ), 10, 2 );
83 add_action(
'gform_post_add_entry', array( $this,
'entry_added' ), 10, 2 );
85 add_action(
'gform_post_update_entry_property', array( $this,
'entry_property_changed' ), 10, 4 );
87 add_action(
'gform_delete_lead', array( $this,
'entry_property_changed' ), 10 );
106 $entry = GFAPI::get_entry( $lead_id );
108 if ( is_wp_error(
$entry ) ) {
110 gravityview()->log->error(
'Could not retrieve entry {entry_id} to delete it: {error}', array(
'entry_id' => $lead_id,
'error' =>
$entry->get_error_message() ) );
115 gravityview()->log->debug(
'adding form {form_id} to blocklist because entry #{lead_id} was deleted', array(
'form_id' =>
$entry[
'form_id'],
'entry_id' => $lead_id,
'data' => array(
'value' => $property_value,
'previous' => $previous_value ) ) );
136 $entry = GFAPI::get_entry( $lead_id );
138 if ( is_wp_error(
$entry ) ) {
140 gravityview()->log->error(
'Could not retrieve entry {entry_id} during cache clearing: {error}', array(
141 'entry_id' => $lead_id,
142 'error' =>
$entry->get_error_message()
148 gravityview()->log->debug(
'adding form {form_id} to blocklist because the {property_name} property was updated for entry #{lead_id}', array(
149 'form_id' =>
$entry[
'form_id'],
150 'entry_id' => $lead_id,
152 'value' => $property_value,
153 'previous' => $previous_value,
154 'property_name' => $property_name,
171 gravityview()->log->debug(
' adding form {form_id} to blocklist because entry #{entry_id} was updated', array(
'form_id' =>
$form[
'id'],
'entry_id' => $lead_id ) );
188 gravityview()->log->debug(
'adding form {form_id} to blocklist because entry #{entry_id} was created', array(
'form_id' =>
$form[
'id'],
'entry_id' =>
$entry[
'id'] ) );
202 if ( is_wp_error(
$entry ) ) {
206 gravityview()->log->debug(
'adding form {form_id} to blocklist because entry #{entry_id} was added', array(
'form_id' =>
$form[
'id'],
'entry_id' =>
$entry[
'id'] ) );
231 return 'gv-cache-' .
$forms .
'-';
241 if ( empty( $this->form_ids ) ) {
251 $this->key = substr(
$key, 0, 45 );
271 $blocklist = get_option( self::BLOCKLIST_OPTION_NAME, array() );
273 return array_map(
'intval', (array) $blocklist );
292 $blocklist = array_merge( (array) $blocklist,
$form_ids );
295 $blocklist = array_unique( $blocklist );
298 $blocklist = array_filter( $blocklist );
300 $updated = update_option( self::BLOCKLIST_OPTION_NAME, $blocklist );
302 if (
false !== $updated ) {
303 gravityview()->log->debug(
'Added form IDs to cache blocklist', array(
'data' => array(
305 '$blocklist' => $blocklist
320 _deprecated_function( __METHOD__,
'2.14',
'GravityView_Cache::blocklist_add()' );
333 $blocklist = get_option( self::BLOCKLIST_OPTION_NAME, array() );
335 $updated_list = array_diff( $blocklist, (array)
$form_ids );
337 gravityview()->log->debug(
'Removing form IDs from cache blocklist', array(
'data' => array(
338 '$form_ids' => $form_ids,
339 '$blocklist' => $blocklist,
340 '$updated_list' => $updated_list
343 return update_option( self::BLOCKLIST_OPTION_NAME, $updated_list );
354 _deprecated_function( __METHOD__,
'2.14',
'GravityView_Cache::blocklist_remove()' );
368 _deprecated_function( __METHOD__,
'2.14',
'GravityView_Cache::in_blocklist()' );
388 gravityview()->log->debug(
'Did not add form to blocklist; empty form ID', array(
'data' =>
$form_ids ) );
395 if ( in_array( (
int) $form_id, $blocklist,
true ) ) {
397 gravityview()->log->debug(
'Form #{form_id} is in the cache blocklist', array(
'form_id' => $form_id ) );
414 public function get(
$key = null ) {
420 gravityview()->log->debug(
'Not using cached results because of GravityView_Cache->use_cache() results' );
425 gravityview()->log->debug(
'Fetching request with transient key {key}', array(
'key' =>
$key ) );
427 $result = get_transient(
$key );
429 if ( is_wp_error( $result ) ) {
431 gravityview()->log->debug(
'Fetching request resulted in error:', array(
'data' => $result ) );
435 } elseif ( $result ) {
437 gravityview()->log->debug(
'Cached results found for transient key {key}', array(
'key' =>
$key ) );
442 gravityview()->log->debug(
'No cached results found for transient key {key}', array(
'key' =>
$key ) );
460 public function set(
$content, $filter_name =
'', $expiration = null ) {
465 $expiration = ! is_int( $expiration ) ? DAY_IN_SECONDS : $expiration;
471 $expiration = (int) apply_filters(
'gravityview_cache_time_' . $filter_name, $expiration );
473 gravityview()->log->debug(
'Setting cache with transient key {key} for {expiration} seconds', array(
'key' => $this->key,
'expiration' => $expiration ) );
475 $transient_was_set = set_transient( $this->key,
$content, $expiration );
477 if ( ! $transient_was_set && $this->
use_cache() ) {
478 gravityview()->log->error(
'Transient was not set for this key: ' . $this->key );
481 return $transient_was_set;
484 gravityview()->log->debug(
'Cache not set; content is empty' );
506 gravityview()->log->debug(
'Did not delete cache; empty form IDs' );
513 $key =
'_transient_gv-cache-';
517 $form_id = intval( $form_id );
520 $key =
"$key%f:$form_id-%";
521 $sql = $wpdb->prepare(
"SELECT option_name FROM {$wpdb->options} WHERE `option_name` LIKE %s",
$key );
523 foreach ( ( $transients = $wpdb->get_col( $sql ) ) as $transient ) {
525 delete_transient( preg_replace(
'#^_transient_#',
'', $transient ) );
528 gravityview()->log->debug(
'Deleting cache for form #{form_id}', array(
'form_id' => $form_id,
'data' => array(
530 sprintf(
'Deleted results: %d', count( $transients ) )
549 $cleanup = apply_filters(
'gravityview_cleanup_transients',
true );
555 if ( ! wp_next_scheduled(
'gravityview-expired-transients' ) ) {
556 wp_schedule_event( time(),
'daily',
'gravityview-expired-transients' );
573 $blog_id = get_current_blog_id();
578 $threshold = time() - 60;
581 $table = $wpdb->get_blog_prefix( $blog_id ) .
'options';
587 join $table t2 on t2.option_name = replace(t1.option_name, '_timeout', '') 588 where (t1.option_name like '\_transient\_timeout\_%' or t1.option_name like '\_site\_transient\_timeout\_%') 589 and t1.option_value < '$threshold' 592 $num_results = $wpdb->query( $sql );
599 option_name like '\_transient\_timeout\_%' 600 or option_name like '\_site\_transient\_timeout\_%' 601 or option_name like 'displayed\_galleries\_%' 602 or option_name like 'displayed\_gallery\_rendering\_%' 604 and option_value < '$threshold' 607 $num_results += $wpdb->query( $sql );
609 gravityview()->log->debug(
'Deleted {count} expired transient records from the database', array(
'count' => $num_results ) );
622 if ( defined(
'GRAVITYVIEW_DISABLE_CACHE' ) && GRAVITYVIEW_DISABLE_CACHE && ! ( defined(
'DOING_GRAVITYVIEW_TESTS' ) && DOING_GRAVITYVIEW_TESTS ) ) {
623 return (
boolean) apply_filters(
'gravityview_use_cache',
false, $this );
635 if ( isset( $_GET[
'cache'] ) || isset( $_GET[
'nocache'] ) ) {
637 gravityview()->log->debug(
'Not using cache: ?cache or ?nocache is in the URL' );
add_hooks()
Add actions for clearing out caches when entries are updated.
get_key()
Allow public access to get transient key.
blocklist_add( $form_ids)
Add form IDs to a "blocklist" to force the cache to be refreshed.
entry_added( $entry, $form)
Clear the cache when entries are added via GFAPI::add_entry().
$use_cache
Whether to use the cache or not.
blacklist_add( $form_ids)
entry_created( $entry, $form)
When an entry is created, add the entry's form to the cache blocklist.
blocklist_remove( $form_ids)
Remove Form IDs from blocklist.
blocklist_get()
Get the blocklist array.
get_cache_key_prefix( $form_ids=NULL)
Calculate the prefix based on the Form IDs.
set_key()
Set the transient key based on the form IDs and the arguments passed to the class.
in_blocklist( $form_ids=NULL)
Is a form ID in the cache blocklist.
entry_status_changed( $lead_id, $property_value='', $previous_value='')
Force refreshing a cache when an entry is deleted.
Handle caching using transients for GravityView.
if(gravityview() ->plugin->is_GF_25()) $form
entry_updated( $form, $lead_id)
When an entry is updated, add the entry's form to the cache blocklist.
const BLACKLIST_OPTION_NAME
if(empty( $field_settings['content'])) $content
schedule_transient_cleanup()
Schedule expired transient cleanup twice a day.
blacklist_remove( $form_ids)
__construct( $form_ids=NULL, $args=array())
delete_expired_transients()
Delete expired transients.
entry_property_changed( $lead_id, $property_name='', $property_value='', $previous_value='')
Force refreshing a cache when an entry is deleted.
if(empty( $created_by)) $form_id
gravityview()
The main GravityView wrapper function.
static has_cap( $caps='', $object_id=null, $user_id=null)
Alias of GravityView_Roles_Capabilities::has_cap()
use_cache()
Check whether to use cached results, if available.
const BLOCKLIST_OPTION_NAME
in_blacklist( $form_ids=NULL)
Is a form ID in the cache blocklist?