admin_url( 'admin-ajax.php' ), 'add_job_nonce' => wp_create_nonce( 'quiztech_add_new_job_action' ), 'send_invite_nonce' => wp_create_nonce( 'quiztech_send_job_invite_action' ), 'error_generic' => esc_html__( 'An error occurred. Please try again.', 'quiztech' ), 'error_permissions' => esc_html__( 'You do not have permission to perform this action.', 'quiztech' ), 'error_missing_assessment' => esc_html__( 'Error: No assessment is linked to this job.', 'quiztech' ), 'invite_sent_success' => esc_html__( 'Invite sent successfully!', 'quiztech' ), 'job_added_success' => esc_html__( 'Job added successfully!', 'quiztech' ), ]); } // --- Enqueue script specifically for Assessment Builder --- if ( is_page_template( 'template-assessment-builder.php' ) ) { $builder_script_path = get_stylesheet_directory() . '/js/quiztech-assessment-builder.js'; $builder_script_url = get_stylesheet_directory_uri() . '/js/quiztech-assessment-builder.js'; $builder_version = file_exists( $builder_script_path ) ? filemtime( $builder_script_path ) : '1.0'; wp_enqueue_script( 'quiztech-builder-script', // Unique handle $builder_script_url, array( 'jquery', 'jquery-ui-sortable' ), // Depends on jQuery and Sortable for drag/drop later $builder_version, true // Load in footer ); // Localize data specifically for the builder script wp_localize_script( 'quiztech-builder-script', 'quiztechBuilderData', [ 'ajax_url' => admin_url( 'admin-ajax.php' ), 'fetch_nonce' => wp_create_nonce( 'quiztech_fetch_library_questions_action' ), // TODO: Verify action name in AJAX handler 'save_nonce' => wp_create_nonce( 'quiztech_save_assessment_action' ), // TODO: Verify action name in AJAX handler 'error_generic' => esc_html__( 'An error occurred. Please try again.', 'quiztech' ), 'loading_questions' => esc_html__( 'Loading questions...', 'quiztech' ), 'saving_assessment' => esc_html__( 'Saving assessment...', 'quiztech' ), 'assessment_saved' => esc_html__( 'Assessment saved successfully!', 'quiztech' ), // Add more localized strings as needed ]); } } add_action( 'wp_enqueue_scripts', 'quiztech_theme_enqueue_scripts' ); /** * AJAX handler for adding a new job post from the frontend. */ function quiztech_ajax_add_new_job() { // 1. Verify Nonce check_ajax_referer( 'quiztech_add_new_job_action', 'nonce' ); // 2. Check Capabilities (adjust capability if needed) if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( [ 'message' => esc_html__( 'Insufficient permissions.', 'quiztech' ) ], 403 ); } // 3. Sanitize Input $job_title = isset( $_POST['job_title'] ) ? sanitize_text_field( wp_unslash( $_POST['job_title'] ) ) : ''; $job_description = isset( $_POST['job_description'] ) ? sanitize_textarea_field( wp_unslash( $_POST['job_description'] ) ) : ''; if ( empty( $job_title ) ) { wp_send_json_error( [ 'message' => esc_html__( 'Job title cannot be empty.', 'quiztech' ) ], 400 ); } // 4. Create Post $post_data = [ 'post_title' => $job_title, 'post_content' => $job_description, 'post_status' => 'publish', // Or 'draft' if preferred 'post_author' => get_current_user_id(), 'post_type' => 'job', ]; $post_id = wp_insert_post( $post_data, true ); // Pass true to return WP_Error on failure // 5. Send Response if ( is_wp_error( $post_id ) ) { wp_send_json_error( [ 'message' => $post_id->get_error_message() ], 500 ); } else { // Optionally return HTML for the new row, or just success and let JS handle refresh/update wp_send_json_success( [ 'message' => esc_html__( 'Job created successfully.', 'quiztech' ), 'post_id' => $post_id ] ); } } add_action( 'wp_ajax_add_new_job', 'quiztech_ajax_add_new_job' ); /** * AJAX handler for sending a job invite from the frontend. */ function quiztech_ajax_send_job_invite() { // 1. Verify Nonce check_ajax_referer( 'quiztech_send_job_invite_action', 'nonce' ); // 2. Check Capabilities (adjust capability if needed) if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( [ 'message' => esc_html__( 'Insufficient permissions.', 'quiztech' ) ], 403 ); } // 3. Sanitize Input $job_id = isset( $_POST['job_id'] ) ? absint( $_POST['job_id'] ) : 0; $applicant_email = isset( $_POST['applicant_email'] ) ? sanitize_email( wp_unslash( $_POST['applicant_email'] ) ) : ''; if ( ! $job_id || ! is_email( $applicant_email ) ) { wp_send_json_error( [ 'message' => esc_html__( 'Invalid job ID or email address.', 'quiztech' ) ], 400 ); } // 4. Check if Job exists and belongs to user (optional extra check) $job_post = get_post( $job_id ); if ( ! $job_post || $job_post->post_type !== 'job' || $job_post->post_author != get_current_user_id() ) { wp_send_json_error( [ 'message' => esc_html__( 'Invalid job specified.', 'quiztech' ) ], 404 ); } // 5. Get Linked Assessment ID $assessment_id = get_post_meta( $job_id, '_quiztech_linked_assessment_id', true ); if ( empty( $assessment_id ) ) { wp_send_json_error( [ 'message' => esc_html__( 'No assessment is linked to this job. Please edit the job and link an assessment.', 'quiztech' ) ], 400 ); } $assessment_id = absint( $assessment_id ); // 6. Use the Invitation Class (ensure plugin is active and class exists) if ( ! class_exists( 'Quiztech\\AssessmentPlatform\\Includes\\Invitations' ) ) { wp_send_json_error( [ 'message' => esc_html__( 'Invitation system is unavailable.', 'quiztech' ) ], 500 ); } try { $invitations = new \Quiztech\AssessmentPlatform\Includes\Invitations(); $result = $invitations->create_invitation( $job_id, $assessment_id, $applicant_email ); if ( is_wp_error( $result ) ) { wp_send_json_error( [ 'message' => $result->get_error_message() ], 500 ); } elseif ( $result === false ) { // Generic failure from create_invitation if no WP_Error wp_send_json_error( [ 'message' => esc_html__( 'Failed to create invitation.', 'quiztech' ) ], 500 ); } else { // Success! $result contains the token. Now send the email. $token = $result; $email_sent = $invitations->send_invitation_email( $applicant_email, $token, [ 'job_title' => get_the_title( $job_id ) ] ); if ( $email_sent ) { wp_send_json_success( [ 'message' => esc_html__( 'Invitation sent successfully.', 'quiztech' ) ] ); } else { // Invitation created, but email failed. wp_send_json_error( [ 'message' => esc_html__( 'Invitation created, but the email could not be sent. Please check email settings.', 'quiztech' ) ], 500 ); } } } catch ( \Exception $e ) { // Catch any unexpected exceptions error_log( 'Quiztech Send Invite Error: ' . $e->getMessage() ); wp_send_json_error( [ 'message' => esc_html__( 'An unexpected error occurred while sending the invitation.', 'quiztech' ) ], 500 ); } } add_action( 'wp_ajax_send_job_invite', 'quiztech_ajax_send_job_invite' );