Skip to content

Commit 2fbbd80

Browse files
authored
Object Cache Disk Modal For Risk Acceptance (#1057)
* Add popup modal for when Object Cache is enabled with Disk selected or is already enabled but Disk is selected. * Fix Minify modal not unchecking Enable if the modal is closed without accepting risks. * Adjust JS to close modals when pressing Escape.
1 parent efb8972 commit 2fbbd80

File tree

6 files changed

+195
-0
lines changed

6 files changed

+195
-0
lines changed

Minify_GeneralPage_View_ShowHelp.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,23 @@ function w3tc_show_minify_help() {
88
'&w3tc_action=minify_help',
99
callback: function(lightbox) {
1010
jQuery('.btn-primary', lightbox.container).click(function() {
11+
jQuery(document).off('keyup.w3tc_lightbox'); // Cleanup event listener.
1112
lightbox.close();
1213
});
14+
jQuery( '.lightbox-close' ).click(
15+
function() {
16+
jQuery( '#minify__enabled' ).prop('checked', false);
17+
jQuery(document).off('keyup.w3tc_lightbox'); // Cleanup event listener.
18+
lightbox.close();
19+
}
20+
);
21+
jQuery(document).on('keyup.w3tc_lightbox', function(e) {
22+
if ('Escape' === e.key) {
23+
jQuery('#minify__enabled').prop('checked', false);
24+
jQuery(document).off('keyup.w3tc_lightbox'); // Cleanup event listener.
25+
lightbox.close();
26+
}
27+
});
1328
lightbox.resize();
1429
}
1530
});

ObjectCache_DiskPopup.js

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/**
2+
* File: ObjectCache_DiskPopup.js
3+
*
4+
* @since X.X.X
5+
*
6+
* @package W3TC
7+
*/
8+
9+
/**
10+
* Modal for object cache disk usage risk acceptance.
11+
*
12+
* @since X.X.X
13+
*
14+
* @return void
15+
*/
16+
function w3tc_show_objectcache_diskpopup(previous, triggeredByEngineChange) {
17+
W3tc_Lightbox.open({
18+
id: 'w3tc-overlay',
19+
close: '',
20+
width: 800,
21+
height: 300,
22+
url: ajaxurl + '?action=w3tc_ajax&_wpnonce=' + w3tc_nonce + '&w3tc_action=objectcache_diskpopup',
23+
callback: function(lightbox) {
24+
jQuery('.btn-primary', lightbox.container).click(function() {
25+
jQuery(document).off('keyup.w3tc_lightbox'); // Cleanup event listener.
26+
lightbox.close();
27+
});
28+
29+
jQuery('.btn-secondary, .lightbox-close').click(function() {
30+
if (triggeredByEngineChange) {
31+
// Case 2: Revert engine selection to previous.
32+
if (previous) {
33+
jQuery('#objectcache__engine').val(previous);
34+
}
35+
} else {
36+
// Case 1: Uncheck enable checkbox.
37+
jQuery('#objectcache__enabled').prop('checked', false);
38+
}
39+
40+
jQuery('.objectcache_disk_notice').hide();
41+
jQuery(document).off('keyup.w3tc_lightbox'); // Cleanup event listener.
42+
lightbox.close();
43+
});
44+
45+
jQuery(document).on('keyup.w3tc_lightbox', function(e) {
46+
if ('Escape' === e.key) {
47+
if (triggeredByEngineChange) {
48+
// Case 2: Revert engine selection to previous.
49+
if (previous) {
50+
jQuery('#objectcache__engine').val(previous);
51+
}
52+
} else {
53+
// Case 1: Uncheck enable checkbox.
54+
jQuery('#objectcache__enabled').prop('checked', false);
55+
}
56+
57+
jQuery('.objectcache_disk_notice').hide();
58+
jQuery(document).off('keyup.w3tc_lightbox'); // Cleanup event listener.
59+
lightbox.close();
60+
}
61+
});
62+
63+
lightbox.resize();
64+
}
65+
});
66+
}
67+
68+
jQuery(function($) {
69+
$('#objectcache__enabled').click(function() {
70+
const checked = $(this).is(':checked'),
71+
engine = $('#objectcache__engine').val();
72+
73+
if (!checked) {
74+
return;
75+
}
76+
77+
if ('file' === engine) {
78+
w3tc_show_objectcache_diskpopup(null, false);
79+
}
80+
});
81+
82+
let previous = null;
83+
84+
$('#objectcache__engine').on('focus', function() {
85+
previous = this.value;
86+
}).change(function() {
87+
const checked = $('#objectcache__enabled').is(':checked'),
88+
engine = $(this).val();
89+
90+
if (!checked) {
91+
return;
92+
}
93+
94+
if ('file' === engine) {
95+
w3tc_show_objectcache_diskpopup(previous, true);
96+
} else {
97+
// Only update `previous` if the new selection is NOT 'file'.
98+
previous = engine;
99+
}
100+
});
101+
});

ObjectCache_DiskPopup_View.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
/**
3+
* File: ObjectCache_DiskPopup_View.php
4+
*
5+
* @since X.X.X
6+
*
7+
* @package W3TC
8+
*/
9+
10+
namespace W3TC;
11+
12+
if ( ! defined( 'W3TC' ) ) {
13+
die();
14+
}
15+
?>
16+
<div class="w3tc-overlay-logo"></div>
17+
<header>
18+
</header>
19+
<div class="w3tchelp_content">
20+
<h3><b><?php esc_html_e( 'Warning: Disk-Based Object Caching Selected', 'w3-total-cache' ); ?></b></h3>
21+
<p>
22+
<?php esc_html_e( 'Using disk as the object cache engine comes with risks:', 'w3-total-cache' ); ?>
23+
</p>
24+
<p>
25+
<li style="margin-left:15px;"><?php esc_html_e( 'Using disk as the cache engine for object caching is not recommended due to its potential for slow performance depending on storage device types and server configuration.', 'w3-total-cache' ); ?></li>
26+
<li style="margin-left:15px;"><?php esc_html_e( 'This setting can potentially create a large number of files. Please be aware of any inode or disk space limits you may have on your hosting account.', 'w3-total-cache' ); ?></li>
27+
</p>
28+
<p>
29+
<?php esc_html_e( 'For optimal performance, consider using a memory-based caching solution like Redis or Memcached.', 'w3-total-cache' ); ?>
30+
<a target="_blank" href="<?php echo esc_url( 'https://www.boldgrid.com/comparing-disk-redis-memcached-caching/' ); ?>"
31+
title="<?php esc_attr_e( 'Comparing Disk, Redis, and Memcached: Understanding Caching Solutions', 'w3-total-cache' ); ?>">
32+
<?php esc_html_e( 'Learn more', 'w3-total-cache' ); ?> <span class="dashicons dashicons-external"></span></a>
33+
</p>
34+
<div>
35+
<input type="submit" class="btn w3tc-size image btn-primary outset save palette-turquoise"
36+
value="<?php esc_attr_e( 'I Understand the Risks', 'w3-total-cache' ); ?>">
37+
<input type="button" class="btn w3tc-size image btn-secondary outset palette-light-grey"
38+
value="<?php esc_attr_e( 'Cancel', 'w3-total-cache' ); ?>">
39+
</div>
40+
</div>

ObjectCache_Plugin_Admin.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ function run() {
1515
add_filter( 'w3tc_usage_statistics_summary_from_history', array(
1616
$this, 'w3tc_usage_statistics_summary_from_history' ), 10, 2 );
1717
}
18+
19+
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
20+
add_action( 'w3tc_ajax_objectcache_diskpopup', array( $this, 'w3tc_ajax_objectcache_diskpopup' ) );
1821
}
1922

2023
public function w3tc_save_options( $data ) {
@@ -124,4 +127,35 @@ public function w3tc_usage_statistics_summary_from_history( $summary, $history )
124127

125128
return $summary;
126129
}
130+
131+
/**
132+
* Enqueue disk usage risk acceptance script.
133+
*
134+
* @since X.X.X
135+
*
136+
* @return void
137+
*/
138+
public function admin_enqueue_scripts() {
139+
$page_val = Util_Request::get_string( 'page' );
140+
if ( 'w3tc_general' === $page_val ) {
141+
wp_enqueue_script(
142+
'w3tc-objectcache-diskpopup',
143+
plugins_url( 'ObjectCache_DiskPopup.js', W3TC_FILE ),
144+
array(),
145+
W3TC_VERSION,
146+
false
147+
);
148+
}
149+
}
150+
151+
/**
152+
* Popup modal for Object Cache disk usage risk acceptance.
153+
*
154+
* @since X.X.X
155+
*
156+
* @return void
157+
*/
158+
public function w3tc_ajax_objectcache_diskpopup() {
159+
include W3TC_DIR . '/ObjectCache_DiskPopup_View.php';
160+
}
127161
}

pub/css/lightbox.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
ul {
2+
list-style-type: disc;
3+
}
4+
15
#w3tc_lightbox_content .metabox-holder {
26
padding-top: 0;
37
}

pub/css/popup.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ h1 {
3434
}
3535

3636
ul {
37+
font-size:35px;
3738
list-style: none;
3839
}
3940

0 commit comments

Comments
 (0)