Skip to content

Commit 1c3c08c

Browse files
committed
Fix inability to delete via UI
1 parent 7dcb655 commit 1c3c08c

File tree

2 files changed

+39
-46
lines changed

2 files changed

+39
-46
lines changed

plugins/wp-graphql-headless-webhooks/src/Admin/WebhooksAdmin.php

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,6 @@ public function handle_actions(): void {
147147
if ( isset( $_POST['action'] ) && 'save_webhook' === $_POST['action'] ) {
148148
$this->handle_webhook_save();
149149
}
150-
151-
if ( isset( $_GET['action'] ) && 'delete' === $_GET['action'] && isset( $_GET['webhook_id'] ) ) {
152-
$this->handle_webhook_delete();
153-
}
154150
}
155151

156152
/**
@@ -186,7 +182,7 @@ private function verify_nonce( string $nonce_name, string $action ): bool {
186182
*/
187183
public function handle_webhook_save() {
188184
// Verify permissions and nonce
189-
if ( ! $this->verify_admin_permission() || ! $this->verify_nonce( 'webhook_save', 'webhook_nonce' ) ) {
185+
if ( ! $this->verify_admin_permission() || ! $this->verify_nonce( 'webhook_nonce', 'webhook_save' ) ) {
190186
wp_die( __( 'Unauthorized', 'wp-graphql-webhooks' ) );
191187
}
192188

@@ -200,7 +196,7 @@ public function handle_webhook_save() {
200196
];
201197

202198
// Validate data
203-
$validation = $this->repository->validate_data( $data );
199+
$validation = $this->repository->validate_data( $data['event'], $data['url'], $data['method'] );
204200
if ( is_wp_error( $validation ) ) {
205201
wp_die( $validation->get_error_message() );
206202
}
@@ -231,15 +227,44 @@ public function handle_webhook_delete() {
231227
* Handle admin actions
232228
*/
233229
public function handle_admin_actions() {
234-
// Handle bulk actions from WP_List_Table
235-
if ( isset( $_REQUEST['action'] ) && 'delete' === $_REQUEST['action'] ||
236-
isset( $_REQUEST['action2'] ) && 'delete' === $_REQUEST['action2'] ) {
230+
// Only process on our admin page
231+
if ( ! isset( $_GET['page'] ) || self::ADMIN_PAGE_SLUG !== $_GET['page'] ) {
232+
return;
233+
}
234+
235+
// Handle single delete action
236+
if ( isset( $_GET['action'] ) && 'delete' === $_GET['action'] && isset( $_GET['webhook'] ) ) {
237+
if ( ! $this->verify_admin_permission() ) {
238+
return;
239+
}
240+
241+
$webhook_id = intval( $_GET['webhook'] );
242+
$nonce = isset( $_GET['_wpnonce'] ) ? $_GET['_wpnonce'] : '';
243+
244+
if ( ! wp_verify_nonce( $nonce, 'delete-webhook-' . $webhook_id ) ) {
245+
wp_die( __( 'Security check failed.', 'wp-graphql-headless-webhooks' ) );
246+
}
247+
248+
if ( $this->repository->delete( $webhook_id ) ) {
249+
wp_redirect( add_query_arg( [ 'deleted' => 1 ], remove_query_arg( [ 'action', 'webhook', '_wpnonce' ], $this->get_admin_url() ) ) );
250+
exit;
251+
}
252+
}
253+
254+
// Handle bulk delete actions from WP_List_Table
255+
if ( isset( $_POST['action'] ) && 'delete' === $_POST['action'] ||
256+
isset( $_POST['action2'] ) && 'delete' === $_POST['action2'] ) {
237257

238-
if ( ! $this->verify_admin_permission() || ! $this->verify_nonce( 'bulk-webhooks', '_wpnonce' ) ) {
258+
if ( ! $this->verify_admin_permission() ) {
239259
return;
240260
}
241261

242-
$webhook_ids = isset( $_REQUEST['webhook'] ) ? array_map( 'intval', (array) $_REQUEST['webhook'] ) : [];
262+
// Check bulk action nonce
263+
if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'bulk-webhooks' ) ) {
264+
wp_die( __( 'Security check failed.', 'wp-graphql-headless-webhooks' ) );
265+
}
266+
267+
$webhook_ids = isset( $_POST['webhook'] ) ? array_map( 'intval', (array) $_POST['webhook'] ) : [];
243268
$deleted = 0;
244269

245270
foreach ( $webhook_ids as $webhook_id ) {
@@ -249,7 +274,7 @@ public function handle_admin_actions() {
249274
}
250275

251276
if ( $deleted > 0 ) {
252-
wp_redirect( add_query_arg( [ 'deleted' => $deleted ], $this->get_admin_url() ) );
277+
wp_redirect( add_query_arg( [ 'deleted' => $deleted ], remove_query_arg( [ 'action', 'action2', 'webhook', '_wpnonce' ], $this->get_admin_url() ) ) );
253278
exit;
254279
}
255280
}

plugins/wp-graphql-headless-webhooks/src/Admin/WebhooksListTable.php

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -77,47 +77,15 @@ public function get_sortable_columns() {
7777
*/
7878
public function get_bulk_actions() {
7979
return [
80-
'bulk-delete' => __( 'Delete', 'wp-graphql-webhooks' ),
80+
'delete' => __( 'Delete', 'wp-graphql-webhooks' ),
8181
];
8282
}
8383

84-
/**
85-
* Process bulk actions
86-
*/
87-
public function process_bulk_action() {
88-
// Handle bulk delete
89-
if ( 'bulk-delete' === $this->current_action() ) {
90-
$webhook_ids = isset( $_POST['webhook'] ) ? array_map( 'intval', $_POST['webhook'] ) : [];
91-
92-
if ( ! empty( $webhook_ids ) && isset( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'bulk-' . $this->_args['plural'] ) ) {
93-
foreach ( $webhook_ids as $id ) {
94-
$this->repository->delete( $id );
95-
}
96-
97-
wp_redirect( add_query_arg( 'deleted', count( $webhook_ids ), remove_query_arg( [ 'action', 'webhook', '_wpnonce' ] ) ) );
98-
exit;
99-
}
100-
}
101-
102-
// Handle single delete
103-
if ( 'delete' === $this->current_action() ) {
104-
$webhook_id = isset( $_GET['webhook'] ) ? intval( $_GET['webhook'] ) : 0;
105-
$nonce = isset( $_GET['_wpnonce'] ) ? $_GET['_wpnonce'] : '';
106-
107-
if ( $webhook_id && wp_verify_nonce( $nonce, 'delete-webhook-' . $webhook_id ) ) {
108-
$this->repository->delete( $webhook_id );
109-
wp_redirect( add_query_arg( 'deleted', 1, remove_query_arg( [ 'action', 'webhook', '_wpnonce' ] ) ) );
110-
exit;
111-
}
112-
}
113-
}
11484

11585
/**
11686
* Prepare items for display
11787
*/
118-
public function prepare_items() {
119-
$this->process_bulk_action();
120-
88+
public function prepare_items() {
12189
$per_page = $this->get_items_per_page( 'webhooks_per_page', 20 );
12290
$current_page = $this->get_pagenum();
12391

0 commit comments

Comments
 (0)