Skip to content

Commit 41ca504

Browse files
committed
Fix WP_Syntex\Object_Cache_Annihilator\Admin and fatal error in admin bar manipulations.
1 parent c1a126f commit 41ca504

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

src/Admin.php

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* Class Admin
2121
*
2222
* Manages the admin bar integration for object cache operations.
23-
*
23+
*
2424
* @phpstan-type NoticeArray array{
2525
* type: 'success'|'error',
2626
* message: string
@@ -95,7 +95,7 @@ public function add_admin_bar_menu( $wp_admin_bar ): void {
9595
return;
9696
}
9797

98-
$is_enabled = wp_using_ext_object_cache() && $wp_object_cache instanceof Object_Cache_Annihilator;
98+
$is_enabled = wp_using_ext_object_cache() && class_exists( Object_Cache_Annihilator::class ) && $wp_object_cache instanceof Object_Cache_Annihilator;
9999
$action = $is_enabled ? 'disable' : 'enable';
100100
$title = $is_enabled ? __( 'Die 🔫', 'object-cache-annihilator' ) : __( 'Resurrect 👻', 'object-cache-annihilator' );
101101

@@ -135,23 +135,15 @@ public function add_admin_bar_menu( $wp_admin_bar ): void {
135135
* @since 0.1.0
136136
*
137137
* @global WP_Object_Cache|Object_Cache_Annihilator $wp_object_cache
138-
*
139-
* @return never
138+
*
139+
* @return never|void
140140
*/
141141
public function handle_actions() {
142142
global $wp_object_cache;
143143

144-
if ( ! current_user_can( 'manage_options' ) ) {
145-
$this->set_notice( 'error', __( 'You do not have sufficient permissions to access this page.', 'object-cache-annihilator' ) );
146-
wp_safe_redirect( admin_url() );
147-
exit;
148-
}
149-
150-
$action = isset( $_GET['action'] ) ? $_GET['action'] : '';
144+
$action = isset( $_GET['action'] ) ? sanitize_text_field( $_GET['action'] ) : '';
151145
if ( ! in_array( $action, [ 'object_cache_enable', 'object_cache_disable', 'object_cache_flush' ], true ) ) {
152-
$this->set_notice( 'error', __( 'Invalid action.', 'object-cache-annihilator' ) );
153-
wp_safe_redirect( admin_url() );
154-
exit;
146+
return;
155147
}
156148

157149
check_admin_referer( $action );
@@ -160,9 +152,18 @@ public function handle_actions() {
160152
$notice_type = 'success';
161153
$notice_message = '';
162154

155+
if ( ! current_user_can( 'manage_options' ) ) {
156+
$this->set_notice( 'error', __( 'You do not have sufficient permissions to access this page.', 'object-cache-annihilator' ) );
157+
wp_safe_redirect( admin_url(), 302, 'X-Redirect-By: Object Cache Annihilator' );
158+
exit;
159+
}
160+
163161
switch ( $action ) {
164162
case 'object_cache_enable':
165163
$this->dropper->drop();
164+
if ( ! class_exists( Object_Cache_Annihilator::class ) ) {
165+
require_once \OBJECT_CACHE_ANNIHILATOR_DIR . '/drop-in.php';
166+
}
166167
Object_Cache_Annihilator::instance()->resurrect();
167168
if ( $wp_object_cache instanceof Object_Cache_Annihilator ) {
168169
$notice_message = __( 'Object cache enabled successfully.', 'object-cache-annihilator' );
@@ -201,7 +202,7 @@ public function handle_actions() {
201202
$this->set_notice( $notice_type, $notice_message );
202203
}
203204

204-
wp_safe_redirect( $redirect_url );
205+
wp_safe_redirect( $redirect_url, 302, 'X-Redirect-By: Object Cache Annihilator' );
205206
exit;
206207
}
207208

@@ -224,7 +225,7 @@ public function display_notice(): void {
224225
if ( ! is_array( $notice ) || ! isset( $notice['type'] ) || ! isset( $notice['message'] ) ) {
225226
return;
226227
}
227-
228+
228229
$class = 'notice notice-' . esc_attr( $notice['type'] );
229230
$message = wp_kses( $notice['message'], [] );
230231

0 commit comments

Comments
 (0)