diff --git a/src/wp-admin/includes/privacy-tools.php b/src/wp-admin/includes/privacy-tools.php index e7b949842ffd1..9487b33db5221 100644 --- a/src/wp-admin/includes/privacy-tools.php +++ b/src/wp-admin/includes/privacy-tools.php @@ -166,7 +166,17 @@ function _wp_personal_data_handle_actions() { } if ( 'pending' === $status ) { - wp_send_user_request( $request_id ); + $send_request_result = wp_send_user_request( $request_id ); + + if ( is_wp_error( $send_request_result ) ) { + add_settings_error( + 'username_or_email_for_privacy_request', + 'username_or_email_for_privacy_request', + $send_request_result->get_error_message(), + 'error' + ); + break; + } $message = __( 'Confirmation request initiated successfully.' ); } elseif ( 'confirmed' === $status ) { diff --git a/tests/phpunit/tests/privacy/wpPersonalDataHandleActions.php b/tests/phpunit/tests/privacy/wpPersonalDataHandleActions.php new file mode 100644 index 0000000000000..385dc1e62fdad --- /dev/null +++ b/tests/phpunit/tests/privacy/wpPersonalDataHandleActions.php @@ -0,0 +1,95 @@ +set_up_add_request_post_data( 'requester@example.com' ); + + // Cause `wp_mail()` to return false. + add_filter( 'wp_mail_from', '__return_empty_string' ); + + _wp_personal_data_handle_actions(); + + $errors = get_settings_errors( 'username_or_email_for_privacy_request' ); + + $this->assertNotEmpty( $errors, 'An error should be recorded when the confirmation email fails to send.' ); + $this->assertSame( 'error', $errors[0]['type'] ); + $this->assertSame( 'Unable to send personal data export confirmation email.', $errors[0]['message'] ); + } + + /** + * A successfully sent confirmation email should still report success. + * + * @ticket 54442 + */ + public function test_should_add_success_message_when_confirmation_email_sends() { + $this->set_up_add_request_post_data( 'requester@example.com' ); + + _wp_personal_data_handle_actions(); + + $errors = get_settings_errors( 'username_or_email_for_privacy_request' ); + + $this->assertNotEmpty( $errors ); + $this->assertSame( 'success', $errors[0]['type'] ); + $this->assertSame( 'Confirmation request initiated successfully.', $errors[0]['message'] ); + } +}