Skip to content

Commit f708a52

Browse files
committed
resolve bulk delete issue
1 parent a43f170 commit f708a52

File tree

2 files changed

+73
-69
lines changed

2 files changed

+73
-69
lines changed

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

Lines changed: 29 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ public function __construct( WebhookRepositoryInterface $repository ) {
4545
add_action( 'admin_menu', [ $this, 'add_admin_menu' ] );
4646
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ] );
4747
add_action( 'admin_post_graphql_webhook_save', [ $this, 'handle_webhook_save' ] );
48-
add_action( 'admin_post_graphql_webhook_delete', [ $this, 'handle_webhook_delete' ] );
49-
add_action( 'admin_init', [ $this, 'handle_admin_actions' ] );
48+
add_action( 'admin_init', [ $this, 'handle_delete_actions' ] );
5049
add_action( 'wp_ajax_test_webhook', [ $this, 'ajax_test_webhook' ] );
5150
}
5251

@@ -132,20 +131,6 @@ private function get_header_row_template(): string {
132131
return ob_get_clean();
133132
}
134133

135-
/**
136-
* Handles admin actions from the webhooks page.
137-
*
138-
* @return void
139-
*/
140-
public function handle_actions(): void {
141-
if ( ! isset( $_GET['page'] ) || self::ADMIN_PAGE_SLUG !== $_GET['page'] ) {
142-
return;
143-
}
144-
145-
if ( isset( $_POST['action'] ) && 'save_webhook' === $_POST['action'] ) {
146-
$this->handle_webhook_save();
147-
}
148-
}
149134

150135
/**
151136
* Checks if the current user has permission to manage options.
@@ -219,68 +204,44 @@ public function handle_webhook_save() {
219204
}
220205

221206
/**
222-
* Handles deleting a webhook.
207+
* Handles webhook delete actions (both single and bulk).
208+
* Consolidates delete logic in one place for easier maintenance.
223209
*
224210
* @return void
225211
*/
226-
public function handle_webhook_delete() {
227-
// To be implemented: Individual deletes are handled through the list table's handle_row_actions.
228-
}
229-
230-
/**
231-
* Handles bulk admin actions (such as bulk delete).
232-
*
233-
* @return void
234-
*/
235-
public function handle_admin_actions() {
236-
// Handle single webhook delete
237-
if ( isset( $_REQUEST['action'] ) && 'delete' === $_REQUEST['action'] && isset( $_GET['webhook'] ) ) {
238-
if ( ! $this->verify_admin_permission() ) {
239-
return;
240-
}
241-
242-
$webhook_id = intval( $_GET['webhook'] );
243-
$nonce = isset( $_GET['_wpnonce'] ) ? $_GET['_wpnonce'] : '';
244-
245-
if ( ! wp_verify_nonce( $nonce, 'delete-webhook-' . $webhook_id ) ) {
246-
wp_die( __( 'Security check failed.', 'wp-graphql-headless-webhooks' ) );
247-
}
248-
249-
if ( $this->repository->delete( $webhook_id ) ) {
250-
wp_redirect( add_query_arg( [ 'deleted' => 1 ], remove_query_arg( [ 'action', 'webhook', '_wpnonce' ], $this->get_admin_url() ) ) );
251-
exit;
252-
}
212+
public function handle_delete_actions() {
213+
// Only process on our admin page
214+
if ( ! isset( $_REQUEST['page'] ) || self::ADMIN_PAGE_SLUG !== $_REQUEST['page'] ) {
215+
return;
253216
}
254217

255-
// Handle bulk delete actions from WP_List_Table
256-
if ( isset( $_POST['action'] ) && 'delete' === $_POST['action'] ||
257-
isset( $_POST['action2'] ) && 'delete' === $_POST['action2'] ) {
258-
259-
if ( ! $this->verify_admin_permission() ) {
260-
return;
261-
}
262-
263-
// Check bulk action nonce
264-
if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'bulk-webhooks' ) ) {
265-
wp_die( __( 'Security check failed.', 'wp-graphql-headless-webhooks' ) );
266-
}
267-
268-
$webhook_ids = isset( $_POST['webhook'] ) ? array_map( 'intval', (array) $_POST['webhook'] ) : [];
269-
$deleted = 0;
218+
// Check if this is a delete action
219+
$is_single_delete = isset( $_GET['action'] ) && 'delete' === $_GET['action'] && isset( $_GET['webhook'] );
220+
221+
if ( ! $is_single_delete ) {
222+
return;
223+
}
270224

271-
foreach ( $webhook_ids as $webhook_id ) {
272-
if ( $this->repository->delete( $webhook_id ) ) {
273-
$deleted++;
274-
}
275-
}
225+
// Verify permissions once
226+
if ( ! $this->verify_admin_permission() ) {
227+
return;
228+
}
276229

277-
if ( $deleted > 0 ) {
278-
wp_redirect( add_query_arg( [ 'deleted' => $deleted ], remove_query_arg( [ 'action', 'action2', 'webhook', '_wpnonce' ], $this->get_admin_url() ) ) );
279-
exit;
280-
}
230+
// Single delete
231+
$webhook_id = intval( $_GET['webhook'] );
232+
$nonce = isset( $_GET['_wpnonce'] ) ? $_GET['_wpnonce'] : '';
233+
234+
if ( ! wp_verify_nonce( $nonce, 'delete-webhook-' . $webhook_id ) ) {
235+
wp_die( __( 'Security check failed.', 'wp-graphql-headless-webhooks' ) );
281236
}
237+
238+
// Delete and redirect
239+
$deleted = $this->repository->delete( $webhook_id ) ? 1 : 0;
240+
wp_redirect( add_query_arg( [ 'deleted' => $deleted ], remove_query_arg( [ 'action', 'webhook', '_wpnonce' ], $this->get_admin_url() ) ) );
241+
exit;
282242
}
283243

244+
284245
/**
285246
* Renders the webhooks admin page.
286247
*

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

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,53 @@ public function get_bulk_actions() {
8282
}
8383

8484

85+
/**
86+
* Process bulk actions
87+
*/
88+
public function process_bulk_action() {
89+
// Only handle delete action
90+
if ( 'delete' !== $this->current_action() ) {
91+
return;
92+
}
93+
94+
// Verify nonce
95+
if ( ! isset( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'bulk-' . $this->_args['plural'] ) ) {
96+
wp_die( __( 'Security check failed.', 'wp-graphql-webhooks' ) );
97+
}
98+
99+
// Check permissions
100+
if ( ! current_user_can( 'manage_options' ) ) {
101+
wp_die( __( 'You do not have sufficient permissions to access this page.', 'wp-graphql-headless-webhooks' ) );
102+
}
103+
104+
// Get selected webhooks
105+
$webhook_ids = isset( $_REQUEST['webhook'] ) ? array_map( 'intval', (array) $_REQUEST['webhook'] ) : [];
106+
if ( empty( $webhook_ids ) ) {
107+
return;
108+
}
109+
110+
// Delete webhooks
111+
$deleted = 0;
112+
foreach ( $webhook_ids as $webhook_id ) {
113+
if ( $this->repository->delete( $webhook_id ) ) {
114+
$deleted++;
115+
}
116+
}
117+
118+
// Redirect with success message
119+
if ( $deleted > 0 ) {
120+
wp_redirect( add_query_arg( [ 'deleted' => $deleted ], remove_query_arg( [ 'action', 'action2', 'webhook', '_wpnonce' ] ) ) );
121+
exit;
122+
}
123+
}
124+
85125
/**
86126
* Prepare items for display
87127
*/
88-
public function prepare_items() {
128+
public function prepare_items() {
129+
// Process bulk actions first
130+
$this->process_bulk_action();
131+
89132
$per_page = $this->get_items_per_page( 'webhooks_per_page', 20 );
90133
$current_page = $this->get_pagenum();
91134

0 commit comments

Comments
 (0)