Fix: Return and use correct token value for invitation email link

This commit is contained in:
Ruben Ramirez 2025-04-04 05:46:55 -05:00
parent 742630778c
commit a626e008bb
2 changed files with 10 additions and 8 deletions

View file

@ -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 ) {

View file

@ -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];
}
/**