Skip to content

Commit 9daac96

Browse files
committed
Add warning when caching is overridden by filters
1 parent ba1893b commit 9daac96

File tree

1 file changed

+101
-40
lines changed

1 file changed

+101
-40
lines changed

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

Lines changed: 101 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -231,50 +231,111 @@ public function add_settings( $plugins_data ) {
231231

232232
$default_settings = $this->defaults();
233233

234-
$cache_settings = array(
235-
array(
236-
'id' => 'caching',
237-
'type' => 'checkbox',
238-
'title' => esc_html__( 'Enable Caching', 'gk-gravityview' ),
239-
'description' => esc_html__( 'Enabling caching improves performance by reducing the number of queries during page loads. When enabled, you can also specify cache duration for entries and DataTables output.', 'gk-gravityview' ),
240-
'value' => $this->get( 'caching', $default_settings['caching'] ),
241-
),
242-
array(
243-
'id' => 'caching_entries',
244-
'type' => 'number',
245-
'requires' => array(
246-
'id' => 'caching',
247-
'operator' => '==',
248-
'value' => 1,
249-
),
250-
'validation' => array(
251-
array(
252-
'rule' => 'min:1',
253-
'message' => esc_html__( 'The cache duration must be at least 1 second.', 'gk-gravityview' ),
234+
$cache_filters_in_use = [];
235+
236+
if ( has_filter( 'gravityview_use_cache' ) ) {
237+
$cache_filters_in_use[] = 'gravityview_use_cache';
238+
}
239+
240+
if ( has_filter( 'gravityview_cache_time_entries' ) ) {
241+
$cache_filters_in_use[] = 'gravityview_cache_time_entries';
242+
}
243+
244+
if ( has_filter( 'gravityview_cache_time_datatables_output' ) ) {
245+
$cache_filters_in_use[] = 'gravityview_cache_time_datatables_output';
246+
}
247+
248+
$cache_settings = [];
249+
250+
if ( ! empty( $cache_filters_in_use ) ) {
251+
$notice = 1 === count( $cache_filters_in_use )
252+
? esc_html__( 'Caching settings are being manually overridden by the [filter] filter.', 'gk-gravityview' )
253+
: esc_html_x( 'Caching settings are being manually overridden by the following filters: [filters].', 'gk-gravityview' );
254+
255+
$notice = strtr(
256+
$notice,
257+
[
258+
'[filter]' => '<code>' . implode( '</code>, <code>', $cache_filters_in_use ) . '</code>',
259+
'[filters]' => '<code>' . implode( '</code>, <code>', $cache_filters_in_use ) . '</code>',
260+
]
261+
);
262+
263+
$notice = <<<HTML
264+
<div class="bg-yellow-50 p-4 rounded-md">
265+
<div class="flex">
266+
<div class="flex-shrink-0">
267+
<svg class="h-5 w-5 text-yellow-400" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
268+
<path fill-rule="evenodd" d="M8.257 3.099c.765-1.36 2.722-1.36 3.486 0l5.58 9.92c.75 1.334-.213 2.98-1.742 2.98H4.42c-1.53 0-2.493-1.646-1.743-2.98l5.58-9.92zM11 13a1 1 0 11-2 0 1 1 0 012 0zm-1-8a1 1 0 00-1 1v3a1 1 0 002 0V6a1 1 0 00-1-1z" clip-rule="evenodd" />
269+
</svg>
270+
</div>
271+
<div class="ml-3">
272+
<p class="text-sm">
273+
{$notice}
274+
</p>
275+
</div>
276+
</div>
277+
</div>
278+
HTML;
279+
280+
$cache_settings[] = [
281+
'id' => 'caching_filters_notice',
282+
'html' => $notice,
283+
'markdown' => false,
284+
'excludeFromSave' => true,
285+
];
286+
}
287+
288+
$cache_settings = array_merge( $cache_settings, [
289+
array(
290+
'id' => 'caching',
291+
'type' => 'checkbox',
292+
'title' => esc_html__( 'Enable Caching', 'gk-gravityview' ),
293+
'description' => strtr(
294+
esc_html_x( '[url]Enabling caching[/url] improves performance by reducing the number of queries during page loads. When enabled, you can also specify cache duration for entries and DataTables output.', 'Placeholders inside [] are not to be translated.', 'gk-gravityview' ),
295+
[
296+
'[url]' => '<a class="underline" href="https://docs.gravitykit.com/article/58-about-gravityview-caching" rel="noopener noreferrer">',
297+
'[/url]' => '</a>',
298+
]
254299
),
300+
'value' => $this->get( 'caching', $default_settings['caching'] ),
255301
),
256-
'title' => esc_html__( 'Entry Cache Duration', 'gk-gravityview' ),
257-
'description' => 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' ),
258-
'value' => $this->get( 'caching_entries', $default_settings['caching_entries'] ),
259-
),
260-
array(
261-
'id' => 'caching_datatables_output',
262-
'type' => 'number',
263-
'requires' => array(
264-
'id' => 'caching',
265-
'operator' => '==',
266-
'value' => 1,
267-
),
268-
'validation' => array(
269-
array(
270-
'rule' => 'min:1',
271-
'message' => esc_html__( 'The cache duration must be at least 1 second.', 'gk-gravityview' ),
302+
array(
303+
'id' => 'caching_entries',
304+
'type' => 'number',
305+
'requires' => array(
306+
'id' => 'caching',
307+
'operator' => '==',
308+
'value' => 1,
309+
),
310+
'validation' => array(
311+
array(
312+
'rule' => 'min:1',
313+
'message' => esc_html__( 'The cache duration must be at least 1 second.', 'gk-gravityview' ),
314+
),
272315
),
316+
'title' => esc_html__( 'Entry Cache Duration', 'gk-gravityview' ),
317+
'description' => 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' ),
318+
'value' => $this->get( 'caching_entries', $default_settings['caching_entries'] ),
273319
),
274-
'title' => esc_html__( 'DataTables Cache Duration', 'gk-gravityview' ),
275-
'description' => esc_html__( 'Define the cache lifetime in seconds for DataTables output. Adjusting this setting can balance between performance gains and data currency for your DataTables Views.', 'gk-gravityview' ),
276-
'value' => $this->get( 'caching_datatables_output', $default_settings['caching_datatables_output'] ),
277-
),
320+
array(
321+
'id' => 'caching_datatables_output',
322+
'type' => 'number',
323+
'requires' => array(
324+
'id' => 'caching',
325+
'operator' => '==',
326+
'value' => 1,
327+
),
328+
'validation' => array(
329+
array(
330+
'rule' => 'min:1',
331+
'message' => esc_html__( 'The cache duration must be at least 1 second.', 'gk-gravityview' ),
332+
),
333+
),
334+
'title' => esc_html__( 'DataTables Cache Duration', 'gk-gravityview' ),
335+
'description' => esc_html__( 'Define the cache lifetime in seconds for DataTables output. Adjusting this setting can balance between performance gains and data currency for your DataTables Views.', 'gk-gravityview' ),
336+
'value' => $this->get( 'caching_datatables_output', $default_settings['caching_datatables_output'] ),
337+
)
338+
]
278339
);
279340

280341
$settings = array(

0 commit comments

Comments
 (0)