From a626e008bb181519aea74cad125fd26ec1b09c8e Mon Sep 17 00:00:00 2001 From: Ruben Ramirez Date: Fri, 4 Apr 2025 05:46:55 -0500 Subject: [PATCH] Fix: Return and use correct token value for invitation email link --- src/Admin/JobMetaboxes.php | 10 ++++++---- src/Includes/Invitations.php | 8 ++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/Admin/JobMetaboxes.php b/src/Admin/JobMetaboxes.php index e31ce22..bf64a9b 100644 --- a/src/Admin/JobMetaboxes.php +++ b/src/Admin/JobMetaboxes.php @@ -115,13 +115,15 @@ class JobMetaboxes { } $invitations = new Invitations(); - $token_or_error = $invitations->create_invitation( $job_id, $assessment_id, $applicant_email ); + $invitation_data = $invitations->create_invitation( $job_id, $assessment_id, $applicant_email ); - if ( is_wp_error( $token_or_error ) ) { - wp_send_json_error( [ 'message' => $token_or_error->get_error_message() ] ); + if ( is_wp_error( $invitation_data ) ) { + wp_send_json_error( [ 'message' => $invitation_data->get_error_message() ] ); + } elseif ( !is_array($invitation_data) || !isset($invitation_data['token']) ) { + wp_send_json_error( [ 'message' => __( 'Failed to retrieve invitation token after creation.', 'quiztech' ) ] ); } - $token = $token_or_error; + $token = $invitation_data['token']; $email_sent = $invitations->send_invitation_email( $applicant_email, $token, [ 'job_title' => get_the_title( $job_id ) ] ); // Pass job title if ( $email_sent ) { diff --git a/src/Includes/Invitations.php b/src/Includes/Invitations.php index 519d994..8a584c7 100644 --- a/src/Includes/Invitations.php +++ b/src/Includes/Invitations.php @@ -24,10 +24,10 @@ class Invitations { * @param int $job_id The ID of the job associated with the invitation. * @param int $assessment_id The ID of the assessment associated with the invitation. * @param string $applicant_email The email address of the applicant being invited. - * @return int|\WP_Error The database ID of the new invitation record on success, or \WP_Error on failure. + * @return array|\WP_Error An array ['id' => int, 'token' => string] on success, or \WP_Error on failure. */ public function create_invitation( $job_id, $assessment_id, $applicant_email ) { - global $wpdb; + global $wpdb; $table_name = $wpdb->prefix . 'quiztech_invitations'; $token = $this->generate_unique_token(); @@ -59,8 +59,8 @@ class Invitations { return new \WP_Error( 'invitation_db_error', __( 'Could not save the invitation record.', 'quiztech' ), [ 'status' => 500 ] ); } - // Return the DB ID of the inserted record, not the token, as ID is used elsewhere - return $wpdb->insert_id; + // Return both the ID and the token + return ['id' => $wpdb->insert_id, 'token' => $token]; } /**