@@ -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 *
0 commit comments