Skip to content

Commit 9b37f2c

Browse files
committed
Added defensive mode feature
Added defensive mode feature for Edge Cache
1 parent 634e60b commit 9b37f2c

19 files changed

Lines changed: 895 additions & 76 deletions
Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
<?php
2+
/**
3+
* Pressable Cache Management - Edge Cache Defensive Mode
4+
*
5+
* Mirrors the exact pattern used in edge_cache_admin_action_handler():
6+
* query_ec_backend( $endpoint, array( 'body' => $data ) )
7+
*
8+
* Enable: POST to ddos_until with body timestamp = time() + duration_seconds
9+
* Disable: POST to ddos_until with body timestamp = 0
10+
* Status: get_ec_ddos_until() — returns the Unix timestamp or 0 if off
11+
*/
12+
13+
if ( ! defined( 'ABSPATH' ) ) {
14+
exit;
15+
}
16+
17+
// ─── Duration map: slug => [ label, seconds ] ────────────────────────────────
18+
function pcm_defensive_mode_durations() {
19+
return [
20+
'30-minutes' => [ 'label' => '30 minutes', 'seconds' => 30 * MINUTE_IN_SECONDS ],
21+
'45-minutes' => [ 'label' => '45 minutes', 'seconds' => 45 * MINUTE_IN_SECONDS ],
22+
'1-hour' => [ 'label' => '1 hour', 'seconds' => HOUR_IN_SECONDS ],
23+
'2-hours' => [ 'label' => '2 hours', 'seconds' => 2 * HOUR_IN_SECONDS ],
24+
'3-hours' => [ 'label' => '3 hours', 'seconds' => 3 * HOUR_IN_SECONDS ],
25+
'4-hours' => [ 'label' => '4 hours', 'seconds' => 4 * HOUR_IN_SECONDS ],
26+
'5-hours' => [ 'label' => '5 hours', 'seconds' => 5 * HOUR_IN_SECONDS ],
27+
'6-hours' => [ 'label' => '6 hours', 'seconds' => 6 * HOUR_IN_SECONDS ],
28+
'7-hours' => [ 'label' => '7 hours', 'seconds' => 7 * HOUR_IN_SECONDS ],
29+
'8-hours' => [ 'label' => '8 hours', 'seconds' => 8 * HOUR_IN_SECONDS ],
30+
'9-hours' => [ 'label' => '9 hours', 'seconds' => 9 * HOUR_IN_SECONDS ],
31+
'10-hours' => [ 'label' => '10 hours', 'seconds' => 10 * HOUR_IN_SECONDS ],
32+
'11-hours' => [ 'label' => '11 hours', 'seconds' => 11 * HOUR_IN_SECONDS ],
33+
'12-hours' => [ 'label' => '12 hours', 'seconds' => 12 * HOUR_IN_SECONDS ],
34+
'13-hours' => [ 'label' => '13 hours', 'seconds' => 13 * HOUR_IN_SECONDS ],
35+
'14-hours' => [ 'label' => '14 hours', 'seconds' => 14 * HOUR_IN_SECONDS ],
36+
'15-hours' => [ 'label' => '15 hours', 'seconds' => 15 * HOUR_IN_SECONDS ],
37+
'16-hours' => [ 'label' => '16 hours', 'seconds' => 16 * HOUR_IN_SECONDS ],
38+
'17-hours' => [ 'label' => '17 hours', 'seconds' => 17 * HOUR_IN_SECONDS ],
39+
'18-hours' => [ 'label' => '18 hours', 'seconds' => 18 * HOUR_IN_SECONDS ],
40+
'19-hours' => [ 'label' => '19 hours', 'seconds' => 19 * HOUR_IN_SECONDS ],
41+
'20-hours' => [ 'label' => '20 hours', 'seconds' => 20 * HOUR_IN_SECONDS ],
42+
'21-hours' => [ 'label' => '21 hours', 'seconds' => 21 * HOUR_IN_SECONDS ],
43+
'22-hours' => [ 'label' => '22 hours', 'seconds' => 22 * HOUR_IN_SECONDS ],
44+
'23-hours' => [ 'label' => '23 hours', 'seconds' => 23 * HOUR_IN_SECONDS ],
45+
'1-day' => [ 'label' => '1 day', 'seconds' => DAY_IN_SECONDS ],
46+
'2-days' => [ 'label' => '2 days', 'seconds' => 2 * DAY_IN_SECONDS ],
47+
'3-days' => [ 'label' => '3 days', 'seconds' => 3 * DAY_IN_SECONDS ],
48+
'4-days' => [ 'label' => '4 days', 'seconds' => 4 * DAY_IN_SECONDS ],
49+
'5-days' => [ 'label' => '5 days', 'seconds' => 5 * DAY_IN_SECONDS ],
50+
'6-days' => [ 'label' => '6 days', 'seconds' => 6 * DAY_IN_SECONDS ],
51+
'7-days' => [ 'label' => '7 days', 'seconds' => 7 * DAY_IN_SECONDS ],
52+
];
53+
}
54+
55+
// ─── Enable Defensive Mode ────────────────────────────────────────────────────
56+
function pcm_pressable_enable_defensive_mode() {
57+
if ( ! isset( $_POST['enable_defensive_mode_nonce'] ) ) {
58+
return;
59+
}
60+
if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['enable_defensive_mode_nonce'] ) ), 'enable_defensive_mode_nonce' ) ) {
61+
return;
62+
}
63+
if ( ! current_user_can( 'manage_options' ) ) {
64+
return;
65+
}
66+
67+
$durations = pcm_defensive_mode_durations();
68+
$slug = isset( $_POST['defensive_mode_duration'] )
69+
? sanitize_text_field( wp_unslash( $_POST['defensive_mode_duration'] ) )
70+
: '30-minutes';
71+
72+
if ( ! array_key_exists( $slug, $durations ) ) {
73+
$slug = '30-minutes';
74+
}
75+
76+
if ( ! class_exists( 'Edge_Cache_Plugin' ) ) {
77+
add_action( 'admin_notices', function() {
78+
if ( function_exists( 'pcm_branded_notice' ) ) {
79+
pcm_branded_notice( esc_html__( 'Error: Edge Cache Plugin is not active.', 'pressable_cache_management' ), '#dd3a03' );
80+
}
81+
} );
82+
return;
83+
}
84+
85+
$expires_at = time() + $durations[ $slug ]['seconds'];
86+
87+
$edge_cache = Edge_Cache_Plugin::get_instance();
88+
$result = $edge_cache->query_ec_backend( 'ddos_until', array(
89+
'body' => array(
90+
'timestamp' => $expires_at,
91+
'wp_action' => 'manual_dashboard_set',
92+
),
93+
) );
94+
95+
if ( false === $result['success'] ) {
96+
$err = ! empty( $result['error'] ) ? $result['error'] : esc_html__( 'Unknown error enabling Defensive Mode.', 'pressable_cache_management' );
97+
add_action( 'admin_notices', function() use ( $err ) {
98+
if ( function_exists( 'pcm_branded_notice' ) ) {
99+
pcm_branded_notice( $err, '#dd3a03' );
100+
}
101+
} );
102+
} else {
103+
update_option( 'edge-cache-defensive-mode-active', 'yes' );
104+
update_option( 'edge-cache-defensive-mode-slug', $slug );
105+
update_option( 'edge-cache-defensive-mode-expires-at', $expires_at );
106+
update_option( 'edge-cache-defensive-mode-set-at', gmdate( 'j M Y, g:ia' ) . ' UTC' );
107+
delete_transient( 'pcm_ec_status_cache' );
108+
do_action( 'pcm_after_defensive_mode_change' );
109+
110+
$label = $durations[ $slug ]['label'];
111+
add_action( 'admin_notices', function() use ( $label ) {
112+
if ( function_exists( 'pcm_branded_notice' ) ) {
113+
pcm_branded_notice(
114+
sprintf( esc_html__( 'Defensive Mode enabled for %s.', 'pressable_cache_management' ), $label ),
115+
'#03fcc2'
116+
);
117+
}
118+
} );
119+
}
120+
}
121+
add_action( 'init', 'pcm_pressable_enable_defensive_mode' );
122+
123+
// ─── Disable Defensive Mode ───────────────────────────────────────────────────
124+
function pcm_pressable_disable_defensive_mode() {
125+
if ( ! isset( $_POST['disable_defensive_mode_nonce'] ) ) {
126+
return;
127+
}
128+
if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['disable_defensive_mode_nonce'] ) ), 'disable_defensive_mode_nonce' ) ) {
129+
return;
130+
}
131+
if ( ! current_user_can( 'manage_options' ) ) {
132+
return;
133+
}
134+
135+
if ( ! class_exists( 'Edge_Cache_Plugin' ) ) {
136+
add_action( 'admin_notices', function() {
137+
if ( function_exists( 'pcm_branded_notice' ) ) {
138+
pcm_branded_notice( esc_html__( 'Error: Edge Cache Plugin is not active.', 'pressable_cache_management' ), '#dd3a03' );
139+
}
140+
} );
141+
return;
142+
}
143+
144+
$edge_cache = Edge_Cache_Plugin::get_instance();
145+
$result = $edge_cache->query_ec_backend( 'ddos_until', array(
146+
'body' => array(
147+
'timestamp' => 0,
148+
'wp_action' => 'manual_dashboard_set',
149+
),
150+
) );
151+
152+
if ( false === $result['success'] ) {
153+
$err = ! empty( $result['error'] ) ? $result['error'] : esc_html__( 'Unknown error disabling Defensive Mode.', 'pressable_cache_management' );
154+
add_action( 'admin_notices', function() use ( $err ) {
155+
if ( function_exists( 'pcm_branded_notice' ) ) {
156+
pcm_branded_notice( $err, '#dd3a03' );
157+
}
158+
} );
159+
} else {
160+
update_option( 'edge-cache-defensive-mode-active', 'no' );
161+
update_option( 'edge-cache-defensive-mode-slug', '' );
162+
update_option( 'edge-cache-defensive-mode-expires-at', 0 );
163+
update_option( 'edge-cache-defensive-mode-set-at', '' );
164+
delete_transient( 'pcm_ec_status_cache' );
165+
do_action( 'pcm_after_defensive_mode_change' );
166+
167+
add_action( 'admin_notices', function() {
168+
if ( function_exists( 'pcm_branded_notice' ) ) {
169+
pcm_branded_notice( esc_html__( 'Defensive Mode disabled.', 'pressable_cache_management' ), '#03fcc2' );
170+
}
171+
} );
172+
}
173+
}
174+
add_action( 'init', 'pcm_pressable_disable_defensive_mode' );
175+
176+
// ─── AJAX: check defensive mode status from server ───────────────────────────
177+
// Uses get_ec_ddos_until() which calls query_ec_backend( 'ddos_until' ) as a
178+
// GET (no body args) and returns the ddos_until Unix timestamp, or 0 if off.
179+
function pcm_ajax_check_defensive_mode_status() {
180+
if ( ! current_user_can( 'manage_options' ) ) {
181+
wp_send_json_error( [ 'message' => 'Unauthorized' ], 403 );
182+
return;
183+
}
184+
185+
if ( ! class_exists( 'Edge_Cache_Plugin' ) ) {
186+
wp_send_json_error( [ 'message' => 'Edge Cache Plugin not available.' ] );
187+
return;
188+
}
189+
190+
$edge_cache = Edge_Cache_Plugin::get_instance();
191+
$ddos_until = $edge_cache->get_ec_ddos_until(); // returns int timestamp or EC_ERROR (-1)
192+
193+
// EC_ERROR means the API call failed
194+
if ( Edge_Cache_Plugin::EC_ERROR === $ddos_until ) {
195+
wp_send_json_error( [ 'message' => 'Could not retrieve Defensive Mode status from server.' ] );
196+
return;
197+
}
198+
199+
$is_defensive = $ddos_until > time();
200+
201+
if ( $is_defensive ) {
202+
// Sync local flag if mode was activated externally (WP-CLI, direct API)
203+
update_option( 'edge-cache-defensive-mode-active', 'yes' );
204+
update_option( 'edge-cache-defensive-mode-expires-at', $ddos_until );
205+
206+
$set_at = get_option( 'edge-cache-defensive-mode-set-at', '' );
207+
$expires_str = gmdate( 'j M Y, g:ia', $ddos_until ) . ' UTC';
208+
209+
wp_send_json_success( [
210+
'defensive_active' => true,
211+
'set_at' => $set_at,
212+
'expires_at' => $expires_str,
213+
] );
214+
215+
} else {
216+
// Server says off — sync local options
217+
update_option( 'edge-cache-defensive-mode-active', 'no' );
218+
update_option( 'edge-cache-defensive-mode-slug', '' );
219+
update_option( 'edge-cache-defensive-mode-expires-at', 0 );
220+
update_option( 'edge-cache-defensive-mode-set-at', '' );
221+
222+
wp_send_json_success( [
223+
'defensive_active' => false,
224+
'set_at' => '',
225+
'expires_at' => '',
226+
] );
227+
}
228+
}
229+
add_action( 'wp_ajax_pcm_check_defensive_mode_status', 'pcm_ajax_check_defensive_mode_status' );

admin/settings-callbacks.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -437,9 +437,10 @@ function pcm_ajax_check_edge_cache_status()
437437
function pcm_clear_ec_status_cache() {
438438
delete_transient( 'pcm_ec_status_cache' );
439439
}
440-
add_action( 'pcm_after_edge_cache_enable', 'pcm_clear_ec_status_cache' );
441-
add_action( 'pcm_after_edge_cache_disable', 'pcm_clear_ec_status_cache' );
442-
add_action( 'pcm_after_edge_cache_purge', 'pcm_clear_ec_status_cache' );
440+
add_action( 'pcm_after_edge_cache_enable', 'pcm_clear_ec_status_cache' );
441+
add_action( 'pcm_after_edge_cache_disable', 'pcm_clear_ec_status_cache' );
442+
add_action( 'pcm_after_edge_cache_purge', 'pcm_clear_ec_status_cache' );
443+
add_action( 'pcm_after_defensive_mode_change','pcm_clear_ec_status_cache' );
443444

444445

445446
/**

0 commit comments

Comments
 (0)