Skip to content

Commit 80c3396

Browse files
GaryJonesclaude
andcommitted
refactor: replace die() with wp_die() in AJAX handlers
Replace all direct die() calls with wp_die() across AJAX handlers to enable proper PHPUnit testing. The die() function terminates the PHP process immediately, making AJAX handlers untestable in isolation. WordPress's wp_die() function provides the same functionality whilst allowing test frameworks to catch WPDieException, enabling proper test coverage of AJAX endpoints. This change affects seven modules: calendar, custom-status, editorial-comments, editorial-metadata, notifications, settings, and user-groups. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent ae7fa7c commit 80c3396

File tree

7 files changed

+45
-45
lines changed

7 files changed

+45
-45
lines changed

modules/calendar/calendar.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -395,20 +395,20 @@ public function handle_ics_subscription() {
395395

396396
// Only do .ics subscriptions when the option is active
397397
if ( 'on' != $this->module->options->ics_subscription ) {
398-
die(); // @todo return accepted response value.
398+
wp_die(); // @todo return accepted response value.
399399
}
400400

401401
// Confirm all of the arguments are present
402402
if ( ! isset( $_GET['user'], $_GET['user_key'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
403-
die(); // @todo return an error response
403+
wp_die(); // @todo return an error response
404404
}
405405

406406
// Confirm this is a valid request
407407
$user = sanitize_user( $_GET['user'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
408408
$user_key = sanitize_user( $_GET['user_key'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
409409
$ics_secret_key = $this->module->options->ics_secret_key;
410410
if ( ! $ics_secret_key || md5( $user . $ics_secret_key ) !== $user_key ) {
411-
die( esc_html( $this->module->messages['nonce-failed'] ) );
411+
wp_die( esc_html( $this->module->messages['nonce-failed'] ) );
412412
}
413413

414414
// Set up the post data to be printed
@@ -497,7 +497,7 @@ public function handle_ics_subscription() {
497497
}
498498
}
499499
}
500-
die();
500+
wp_die();
501501
}
502502

503503
/**

modules/custom-status/custom-status.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,11 +1092,11 @@ public function ajax_inline_save_status() {
10921092
global $edit_flow;
10931093

10941094
if ( ! isset( $_POST['inline_edit'] ) || ! wp_verify_nonce( $_POST['inline_edit'], 'custom-status-inline-edit-nonce' ) ) {
1095-
die( esc_html( $this->module->messages['nonce-failed'] ) );
1095+
wp_die( esc_html( $this->module->messages['nonce-failed'] ) );
10961096
}
10971097

10981098
if ( ! current_user_can( 'manage_options' ) ) {
1099-
die( esc_html( $this->module->messages['invalid-permissions'] ) );
1099+
wp_die( esc_html( $this->module->messages['invalid-permissions'] ) );
11001100
}
11011101

11021102
$term_id = isset( $_POST['status_id'] ) ? (int) $_POST['status_id'] : 0;
@@ -1107,38 +1107,38 @@ public function ajax_inline_save_status() {
11071107
// Check if name field was filled in
11081108
if ( empty( $status_name ) ) {
11091109
$change_error = new WP_Error( 'invalid', esc_html__( 'Please enter a name for the status.', 'edit-flow' ) );
1110-
die( esc_html( $change_error->get_error_message() ) );
1110+
wp_die( esc_html( $change_error->get_error_message() ) );
11111111
}
11121112

11131113
// Check that the name isn't numeric
11141114
if ( is_numeric( $status_name ) ) {
11151115
$change_error = new WP_Error( 'invalid', esc_html__( 'Please enter a valid, non-numeric name for the status.', 'edit-flow' ) );
1116-
die( esc_html( $change_error->get_error_message() ) );
1116+
wp_die( esc_html( $change_error->get_error_message() ) );
11171117
}
11181118

11191119
// Check that the status name doesn't exceed 20 chars
11201120
if ( strlen( $status_name ) > 20 ) {
11211121
$change_error = new WP_Error( 'invalid', esc_html__( 'Status name cannot exceed 20 characters. Please try a shorter name.', 'edit-flow' ) );
1122-
die( esc_html( $change_error->get_error_message() ) );
1122+
wp_die( esc_html( $change_error->get_error_message() ) );
11231123
}
11241124

11251125
// Check to make sure the name is not restricted
11261126
if ( $edit_flow->custom_status->is_restricted_status( strtolower( $status_name ) ) ) {
11271127
$change_error = new WP_Error( 'invalid', esc_html__( 'Status name is restricted. Please chose another name.', 'edit-flow' ) );
1128-
die( esc_html( $change_error->get_error_message() ) );
1128+
wp_die( esc_html( $change_error->get_error_message() ) );
11291129
}
11301130

11311131
// Check to make sure the status doesn't already exist
11321132
if ( $this->get_custom_status_by( 'slug', $status_slug ) && ( $this->get_custom_status_by( 'id', $term_id )->slug != $status_slug ) ) {
11331133
$change_error = new WP_Error( 'invalid', esc_html__( 'Status already exists. Please choose another name.', 'edit-flow' ) );
1134-
die( esc_html( $change_error->get_error_message() ) );
1134+
wp_die( esc_html( $change_error->get_error_message() ) );
11351135
}
11361136

11371137
// Check to make sure the status doesn't already exist as another term because otherwise we'd get a fatal error
11381138
$term_exists = term_exists( sanitize_title( $status_name ), self::taxonomy_key );
11391139
if ( $term_exists && isset( $term_exists['term_id'] ) && $term_exists['term_id'] != $term_id ) {
11401140
$change_error = new WP_Error( 'invalid', esc_html__( 'Status name conflicts with existing term. Please choose another.', 'edit-flow' ) );
1141-
die( esc_html( $change_error->get_error_message() ) );
1141+
wp_die( esc_html( $change_error->get_error_message() ) );
11421142
}
11431143

11441144
// get status_name & status_description
@@ -1153,11 +1153,11 @@ public function ajax_inline_save_status() {
11531153
$wp_list_table = new EF_Custom_Status_List_Table();
11541154
$wp_list_table->prepare_items();
11551155
echo wp_kses_post( $wp_list_table->single_row( $return ) );
1156-
die();
1156+
wp_die();
11571157
} else {
11581158
/* translators: 1: the status's name */
11591159
$change_error = new WP_Error( 'invalid', sprintf( __( 'Could not update the status: <strong>%s</strong>', 'edit-flow' ), $status_name ) );
1160-
die( wp_kses( $change_error->get_error_message(), 'strong' ) );
1160+
wp_die( wp_kses( $change_error->get_error_message(), 'strong' ) );
11611161
}
11621162
}
11631163

modules/editorial-comments/editorial-comments.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ public function ajax_insert_comment() {
300300

301301
// Verify nonce
302302
if ( ! isset( $_POST['_nonce'] ) || ! wp_verify_nonce( $_POST['_nonce'], 'comment' ) ) {
303-
die( esc_html__( "Nonce check failed. Please ensure you're supposed to be adding editorial comments.", 'edit-flow' ) );
303+
wp_die( esc_html__( "Nonce check failed. Please ensure you're supposed to be adding editorial comments.", 'edit-flow' ) );
304304
}
305305

306306
// Get user info
@@ -315,13 +315,13 @@ public function ajax_insert_comment() {
315315
// Only allow the comment if user can edit post
316316
// @TODO: allow contributers to add comments as well (?)
317317
if ( ! current_user_can( 'edit_post', $post_id ) ) {
318-
die( esc_html__( 'Sorry, you don\'t have the privileges to add editorial comments. Please talk to your Administrator.', 'edit-flow' ) );
318+
wp_die( esc_html__( 'Sorry, you don\'t have the privileges to add editorial comments. Please talk to your Administrator.', 'edit-flow' ) );
319319
}
320320

321321
// Verify that comment was actually entered
322322
$comment_content = isset( $_POST['content'] ) ? trim( $_POST['content'] ) : '';
323323
if ( ! $comment_content ) {
324-
die( esc_html__( 'Please enter a comment.', 'edit-flow' ) );
324+
wp_die( esc_html__( 'Please enter a comment.', 'edit-flow' ) );
325325
}
326326

327327
// Check that we have a post_id and user logged in
@@ -405,7 +405,7 @@ public function ajax_insert_comment() {
405405
$response->send();
406406

407407
} else {
408-
die( esc_html__( 'There was a problem of some sort. Try again or contact your administrator.', 'edit-flow' ) );
408+
wp_die( esc_html__( 'There was a problem of some sort. Try again or contact your administrator.', 'edit-flow' ) );
409409
}
410410
}
411411

modules/editorial-metadata/editorial-metadata.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,16 +1275,16 @@ public function handle_change_editorial_metadata_visibility() {
12751275
public function handle_ajax_inline_save_term() {
12761276

12771277
if ( ! isset( $_POST['inline_edit'] ) || ! wp_verify_nonce( $_POST['inline_edit'], 'editorial-metadata-inline-edit-nonce' ) ) {
1278-
die( esc_html( $this->module->messages['nonce-failed'] ) );
1278+
wp_die( esc_html( $this->module->messages['nonce-failed'] ) );
12791279
}
12801280

12811281
if ( ! current_user_can( 'manage_options' ) ) {
1282-
die( esc_html( $this->module->messages['invalid-permissions'] ) );
1282+
wp_die( esc_html( $this->module->messages['invalid-permissions'] ) );
12831283
}
12841284

12851285
$term_id = isset( $_POST['term_id'] ) ? (int) $_POST['term_id'] : 0;
12861286
if ( ! $existing_term = $this->get_editorial_metadata_term_by( 'id', $term_id ) ) {
1287-
die( esc_html( $this->module->messages['term-missing'] ) );
1287+
wp_die( esc_html( $this->module->messages['term-missing'] ) );
12881288
}
12891289

12901290
$metadata_name = isset( $_POST['name'] ) ? sanitize_text_field( trim( $_POST['name'] ) ) : '';
@@ -1296,40 +1296,40 @@ public function handle_ajax_inline_save_term() {
12961296
// Check if name field was filled in
12971297
if ( empty( $metadata_name ) ) {
12981298
$change_error = new WP_Error( 'invalid', _esc_html__( 'Please enter a name for the editorial metadata', 'edit-flow' ) );
1299-
die( esc_html( $change_error->get_error_message() ) );
1299+
wp_die( esc_html( $change_error->get_error_message() ) );
13001300
}
13011301

13021302
// Check that the name isn't numeric
13031303
if ( is_numeric( $metadata_name ) ) {
13041304
$change_error = new WP_Error( 'invalid', esc_html__( 'Please enter a valid, non-numeric name for the editorial metadata.', 'edit-flow' ) );
1305-
die( esc_html( $change_error->get_error_message() ) );
1305+
wp_die( esc_html( $change_error->get_error_message() ) );
13061306
}
13071307

13081308
// Check that the term name doesn't exceed 200 chars
13091309
if ( strlen( $metadata_name ) > 200 ) {
13101310
$change_error = new WP_Error( 'invalid', esc_html__( 'Name cannot exceed 200 characters. Please try a shorter name.' ) );
1311-
die( esc_html( $change_error->get_error_message() ) );
1311+
wp_die( esc_html( $change_error->get_error_message() ) );
13121312
}
13131313

13141314
// Check to make sure the status doesn't already exist as another term because otherwise we'd get a fatal error
13151315
$term_exists = term_exists( sanitize_title( $metadata_name ) );
13161316
if ( $term_exists && $term_exists != $term_id ) {
13171317
$change_error = new WP_Error( 'invalid', esc_html____( 'Metadata name conflicts with existing term. Please choose another.', 'edit-flow' ) );
1318-
die( esc_html( $change_error->get_error_message() ) );
1318+
wp_die( esc_html( $change_error->get_error_message() ) );
13191319
}
13201320

13211321
// Check to ensure a term with the same name doesn't exist,
13221322
$search_term = $this->get_editorial_metadata_term_by( 'name', $metadata_name );
13231323
if ( is_object( $search_term ) && $search_term->term_id != $existing_term->term_id ) {
13241324
$change_error = new WP_Error( 'invalid', esc_html__( 'Name already in use. Please choose another.', 'edit-flow' ) );
1325-
die( esc_html( $change_error->get_error_message() ) );
1325+
wp_die( esc_html( $change_error->get_error_message() ) );
13261326
}
13271327

13281328
// or that the term name doesn't map to an existing term's slug
13291329
$search_term = $this->get_editorial_metadata_term_by( 'slug', sanitize_title( $metadata_name ) );
13301330
if ( is_object( $search_term ) && $search_term->term_id != $existing_term->term_id ) {
13311331
$change_error = new WP_Error( 'invalid', esc_html__( 'Name conflicts with slug for another term. Please choose again.', 'edit-flow' ) );
1332-
die( esc_html( $change_error->get_error_message() ) );
1332+
wp_die( esc_html( $change_error->get_error_message() ) );
13331333
}
13341334

13351335
// Prepare the term name and description for saving
@@ -1343,11 +1343,11 @@ public function handle_ajax_inline_save_term() {
13431343
$wp_list_table = new EF_Editorial_Metadata_List_Table();
13441344
$wp_list_table->prepare_items();
13451345
echo wp_kses_post( $wp_list_table->single_row( $return ) );
1346-
die();
1346+
wp_die();
13471347
} else {
13481348
/* Translators: 1: the name of the term that could not be found */
13491349
$change_error = new WP_Error( 'invalid', sprintf( __( 'Could not update the term: <strong>%s</strong>', 'edit-flow' ), $metadata_name ) );
1350-
die( wp_kses( $change_error->get_error_message() ) );
1350+
wp_die( wp_kses( $change_error->get_error_message() ) );
13511351
}
13521352
}
13531353

modules/notifications/notifications.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -412,15 +412,15 @@ public function ajax_save_post_subscriptions() {
412412

413413
// Verify nonce.
414414
if ( ! isset( $_POST['_nonce'] ) || ! wp_verify_nonce( $_POST['_nonce'], 'save_user_usergroups' ) ) {
415-
die( esc_html__( 'Nonce check failed. Please ensure you can add users or user groups to a post.', 'edit-flow' ) );
415+
wp_die( esc_html__( 'Nonce check failed. Please ensure you can add users or user groups to a post.', 'edit-flow' ) );
416416
}
417417

418418
$post_id = isset( $_POST['post_id'] ) ? (int) $_POST['post_id'] : 0;
419419
$post = get_post( $post_id );
420420

421421
$valid_post = ! is_null( $post ) && ! wp_is_post_revision( $post_id ) && ! wp_is_post_autosave( $post_id );
422422
if ( ! isset( $_POST['ef_notifications_name'] ) || ! $valid_post || ! current_user_can( $this->edit_post_subscriptions_cap ) ) {
423-
die();
423+
wp_die();
424424
}
425425

426426
$user_group_ids = [];
@@ -469,7 +469,7 @@ function ( $user_id ) use ( $post_id ) {
469469
$this->save_post_following_usergroups( $post, $user_group_ids );
470470
}
471471

472-
die();
472+
wp_die();
473473
}
474474

475475
/**

modules/settings/settings.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public function ajax_change_edit_flow_module_state() {
9999
}
100100

101101
if ( ! isset( $_POST['module_action'], $_POST['slug'] ) ) {
102-
die( '-1' );
102+
wp_die( '-1' );
103103
}
104104

105105
$module_action = sanitize_key( $_POST['module_action'] );
@@ -108,7 +108,7 @@ public function ajax_change_edit_flow_module_state() {
108108
$module = $edit_flow->get_module_by( 'slug', $slug );
109109

110110
if ( ! $module ) {
111-
die( '-1' );
111+
wp_die( '-1' );
112112
}
113113

114114
if ( 'enable' == $module_action ) {
@@ -118,9 +118,9 @@ public function ajax_change_edit_flow_module_state() {
118118
}
119119

120120
if ( $return ) {
121-
die( '1' );
121+
wp_die( '1' );
122122
} else {
123-
die( '-1' );
123+
wp_die( '-1' );
124124
}
125125
}
126126

modules/user-groups/user-groups.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -458,16 +458,16 @@ public function handle_delete_usergroup() {
458458
public function handle_ajax_inline_save_usergroup() {
459459

460460
if ( ! isset( $_POST['inline_edit'] ) || ! wp_verify_nonce( $_POST['inline_edit'], 'usergroups-inline-edit-nonce' ) ) {
461-
die( esc_html( $this->module->messages['nonce-failed'] ) );
461+
wp_die( esc_html( $this->module->messages['nonce-failed'] ) );
462462
}
463463

464464
if ( ! current_user_can( $this->manage_usergroups_cap ) ) {
465-
die( esc_html( $this->module->messages['invalid-permissions'] ) );
465+
wp_die( esc_html( $this->module->messages['invalid-permissions'] ) );
466466
}
467467

468468
$usergroup_id = isset( $_POST['usergroup_id'] ) ? (int) $_POST['usergroup_id'] : 0;
469469
if ( ! $existing_term = $this->get_usergroup_by( 'id', $usergroup_id ) ) {
470-
die( esc_html( $this->module->messages['usergroup-missing'] ) );
470+
wp_die( esc_html( $this->module->messages['usergroup-missing'] ) );
471471
}
472472

473473
$name = isset( $_POST['name'] ) ? sanitize_text_field( trim( $_POST['name'] ) ) : '';
@@ -479,24 +479,24 @@ public function handle_ajax_inline_save_usergroup() {
479479
// Check if name field was filled in
480480
if ( empty( $name ) ) {
481481
$change_error = new WP_Error( 'invalid', esc_html__( 'Please enter a name for the user group.', 'edit-flow' ) );
482-
die( esc_html( $change_error->get_error_message() ) );
482+
wp_die( esc_html( $change_error->get_error_message() ) );
483483
}
484484
// Check that the name doesn't exceed 40 chars
485485
if ( strlen( $name ) > 40 ) {
486486
$change_error = new WP_Error( 'invalid', esc_html__( 'User group name cannot exceed 40 characters. Please try a shorter name.' ) );
487-
die( esc_html( $change_error->get_error_message() ) );
487+
wp_die( esc_html( $change_error->get_error_message() ) );
488488
}
489489
// Check to ensure a term with the same name doesn't exist
490490
$search_term = $this->get_usergroup_by( 'name', $name );
491491
if ( is_object( $search_term ) && $search_term->term_id != $existing_term->term_id ) {
492492
$change_error = new WP_Error( 'invalid', esc_html__( 'Name already in use. Please choose another.', 'edit-flow' ) );
493-
die( esc_html( $change_error->get_error_message() ) );
493+
wp_die( esc_html( $change_error->get_error_message() ) );
494494
}
495495
// Check to ensure a term with the same slug doesn't exist
496496
$search_term = $this->get_usergroup_by( 'slug', sanitize_title( $name ) );
497497
if ( is_object( $search_term ) && $search_term->term_id != $existing_term->term_id ) {
498498
$change_error = new WP_Error( 'invalid', esc_html__( 'Name conflicts with slug for another term. Please choose again.', 'edit-flow' ) );
499-
die( esc_html( $change_error->get_error_message() ) );
499+
wp_die( esc_html( $change_error->get_error_message() ) );
500500
}
501501

502502
// Prepare the term name and description for saving
@@ -510,11 +510,11 @@ public function handle_ajax_inline_save_usergroup() {
510510
$wp_list_table = new EF_Usergroups_List_Table();
511511
$wp_list_table->prepare_items();
512512
echo wp_kses_post( $wp_list_table->single_row( $return ) );
513-
die();
513+
wp_die();
514514
} else {
515515
// translators: %s is the name of the user group
516516
$change_error = new WP_Error( 'invalid', sprintf( __( 'Could not update the user group: <strong>%s</strong>', 'edit-flow' ), $name ) );
517-
die( wp_kses( $change_error->get_error_message(), 'strong' ) );
517+
wp_die( wp_kses( $change_error->get_error_message(), 'strong' ) );
518518
}
519519
}
520520

0 commit comments

Comments
 (0)