Skip to content

Commit 0e02cdb

Browse files
committed
Add per-View caching support
1 parent 9daac96 commit 0e02cdb

File tree

3 files changed

+70
-26
lines changed

3 files changed

+70
-26
lines changed

future/includes/class-gv-settings-view.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
namespace GV;
33

4+
use GravityKit\GravityView\Foundation\Settings\Framework as SettingsFramework;
5+
46
/** If this file is called directly, abort. */
57
if ( ! defined( 'GRAVITYVIEW_DIR' ) ) {
68
die();
@@ -46,7 +48,6 @@ public static function with_defaults( $detailed = false ) {
4648
* @param boolean $full_width True: Display the input and label together when rendering. False: Display label and input in separate columns when rendering.
4749
*/
4850
public static function defaults( $detailed = false, $group = null ) {
49-
5051
$default_settings = array_merge(
5152
array(
5253
'id' => array(
@@ -100,6 +101,34 @@ public static function defaults( $detailed = false, $group = null ) {
100101
'url' => 'https://docs.gravitykit.com/article/490-entry-approval-gravity-forms',
101102
),
102103
),
104+
'caching' => array(
105+
'label' => __( 'Enable Caching', 'gk-gravityview' ),
106+
'type' => 'checkbox',
107+
'group' => 'default',
108+
'value' => gravityview()->plugin->settings->get( 'caching' ),
109+
'desc' => strtr(
110+
esc_html_x( 'Caching is enabled by default in the global [url]GravityView settings[/url]. Adjusting the settings here will allow you to manage caching on a per-View basis.', 'Placeholders inside [] are not to be translated.', 'gk-gravityview' ),
111+
[
112+
'[url]' => '<a href="' . SettingsFramework::get_instance()->get_plugin_settings_url( Plugin_Settings::SETTINGS_PLUGIN_ID ) . '&s=1">',
113+
'[/url]' => '</a>',
114+
]
115+
),
116+
'show_in_shortcode' => false,
117+
'article' => array(
118+
'id' => '54c67bb6e4b051242988550a',
119+
'url' => 'https://docs.gravitykit.com/article/58-about-gravityview-cachinghttps://docs.gravitykit.com/article/58-about-gravityview-caching',
120+
),
121+
),
122+
'caching_entries' => array(
123+
'label' => __( 'Entry Cache Duration', 'gk-gravityview' ),
124+
'tooltip' => esc_html__( 'Specify the duration in seconds that entry data should remain cached before being refreshed. A shorter duration ensures more up-to-date data, while a longer duration improves performance.', 'gk-gravityview' ),
125+
'type' => 'number',
126+
'group' => 'default',
127+
'value' => gravityview()->plugin->settings->get( 'caching_entries' ),
128+
'show_in_shortcode' => false,
129+
'requires' => 'caching=1',
130+
'min' => 1,
131+
),
103132
'no_entries_options' => array(
104133
'label' => __( 'No Entries Behavior', 'gk-gravityview' ),
105134
'type' => 'select',

includes/admin/metaboxes/views/view-settings.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@
1515

1616
<table class="form-table">
1717
<?php
18-
1918
GravityView_Render_Settings::render_setting_row( 'lightbox', $current_settings );
2019

2120
GravityView_Render_Settings::render_setting_row( 'show_only_approved', $current_settings );
2221

2322
GravityView_Render_Settings::render_setting_row( 'admin_show_all_statuses', $current_settings );
2423

24+
GravityView_Render_Settings::render_setting_row( 'caching', $current_settings );
25+
26+
GravityView_Render_Settings::render_setting_row( 'caching_entries', $current_settings );
27+
2528
do_action( 'gravityview_admin_directory_settings', $current_settings );
2629
?>
2730
</table>

includes/class-cache.php

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -506,39 +506,45 @@ public function get( $key = null ) {
506506
* @return bool If $content is not set, false. Otherwise, returns true if transient was set and false if not.
507507
*/
508508
public function set( $content, $filter_name = '', $expiration = null ) {
509-
510509
// Don't cache empty results
511-
if ( ! empty( $content ) ) {
510+
if ( empty( $content ) ) {
511+
gravityview()->log->debug( 'Cache not set; content is empty' );
512512

513-
$expiration = ! is_int( $expiration ) ? gravityview()->plugin->settings->get( "caching_{$filter_name}", DAY_IN_SECONDS ) : $expiration;
513+
return false;
514+
}
514515

515-
/**
516-
* Modify the cache time for a type of cache.
517-
*
518-
* @param int $time_in_seconds Default: `DAY_IN_SECONDS`
519-
*/
520-
$expiration = (int) apply_filters( 'gravityview_cache_time_' . $filter_name, $expiration );
516+
if ( ! is_int( $expiration ) ) {
517+
// Global cache duration setting.
518+
$expiration = gravityview()->plugin->settings->get( "caching_{$filter_name}", DAY_IN_SECONDS );
521519

522-
gravityview()->log->debug(
523-
'Setting cache with transient key {key} for {expiration} seconds',
524-
array(
525-
'key' => $this->key,
526-
'expiration' => $expiration,
527-
)
528-
);
520+
// View-specific cache duration setting.
521+
if ( is_array( $this->args ) && array_key_exists( "caching_{$filter_name}", $this->args ) ) {
522+
$expiration = $this->args["caching_{$filter_name}"];
523+
}
524+
}
525+
526+
/**
527+
* Modify the cache time for a type of cache.
528+
*
529+
* @param int $time_in_seconds Default: `DAY_IN_SECONDS`
530+
*/
531+
$expiration = (int) apply_filters( 'gravityview_cache_time_' . $filter_name, $expiration );
529532

530-
$transient_was_set = set_transient( $this->key, $content, $expiration );
533+
gravityview()->log->debug(
534+
'Setting cache with transient key {key} for {expiration} seconds',
535+
array(
536+
'key' => $this->key,
537+
'expiration' => $expiration,
538+
)
539+
);
531540

532-
if ( ! $transient_was_set && $this->use_cache() ) {
533-
gravityview()->log->error( 'Transient was not set for this key: ' . $this->key );
534-
}
541+
$transient_was_set = set_transient( $this->key, $content, $expiration );
535542

536-
return $transient_was_set;
543+
if ( ! $transient_was_set && $this->use_cache() ) {
544+
gravityview()->log->error( 'Transient was not set for this key: ' . $this->key );
537545
}
538546

539-
gravityview()->log->debug( 'Cache not set; content is empty' );
540-
541-
return false;
547+
return $transient_was_set;
542548
}
543549

544550
/**
@@ -689,8 +695,14 @@ public function use_cache() {
689695
return $this->use_cache;
690696
}
691697

698+
// Global cache setting.
692699
$use_cache = (bool) gravityview()->plugin->settings->get( 'caching' );
693700

701+
// View-specific cache setting.
702+
if ( is_array( $this->args ) && array_key_exists( 'caching', $this->args ) ) {
703+
$use_cache = $this->args['caching'];
704+
}
705+
694706
if ( GVCommon::has_cap( 'edit_gravityviews' ) ) {
695707

696708
if ( isset( $_GET['cache'] ) || isset( $_GET['nocache'] ) ) {

0 commit comments

Comments
 (0)