diff --git a/functions.php b/functions.php index 159d70c..bc33afa 100644 --- a/functions.php +++ b/functions.php @@ -10,8 +10,10 @@ function log_to_file($message, $data = false){ // Only proceed if a message is provided if ($message) { // Check if the custom log file constant is defined - if (defined('CUSTOM_DEBUG_LOG') && CUSTOM_DEBUG_LOG) { - $log_File = CUSTOM_DEBUG_LOG; + // Linter fix: Check only if defined, assume non-empty if defined. + if (defined('CUSTOM_DEBUG_LOG')) { + // Linter fix: Use constant() function to avoid undefined constant error + $log_File = constant('CUSTOM_DEBUG_LOG'); $date = new DateTime(); $date = $date->format("Y/m/d h:i:s"); @@ -156,9 +158,10 @@ function quiztech_ajax_get_job() { // 6. Send Response wp_send_json_success( [ - 'id' => $post->ID, - 'title' => $post->post_title, - 'description' => $post->post_content, + 'id' => $post->ID, + 'title' => $post->post_title, + 'description' => $post->post_content, + 'assessment_id' => get_post_meta( $post->ID, '_quiztech_associated_assessment_id', true ), // Fetch the linked assessment ID ] ); } add_action( 'wp_ajax_quiztech_get_job', 'quiztech_ajax_get_job' ); @@ -226,24 +229,42 @@ function quiztech_ajax_save_job() { // 6. Send Response if ( is_wp_error( $result ) ) { wp_send_json_error( [ 'message' => $result->get_error_message() ], 500 ); - } elseif ( $result === 0 ) { - // wp_update_post returns 0 if nothing changed, which isn't an error here. - // Treat it as success but maybe with a different message? For now, same success message. - wp_send_json_success( [ - 'message' => esc_html__( 'Job details saved (no changes detected).', 'quiztech' ), - 'job_id' => $job_id // Return the existing ID - ] ); + } elseif ( $result === 0 && $is_update ) { // Check if it was an update with no changes + // wp_update_post returns 0 if nothing changed. + // We still need to potentially update the meta field even if core fields didn't change. + $new_or_updated_post_id = $job_id; } else { - // Success (Insert or Update) - $success_message = $is_update - ? esc_html__( 'Job updated successfully.', 'quiztech' ) - : esc_html__( 'Job created successfully.', 'quiztech' ); - - wp_send_json_success( [ - 'message' => $success_message, - 'job_id' => $result // $result is the post ID on success - ] ); + // Success (Insert or Update with changes) + $new_or_updated_post_id = $result; // $result is the post ID on success } + + // --- Update Meta Fields --- + if ( $new_or_updated_post_id > 0 ) { + // Save Linked Assessment ID + if ( isset( $_POST['quiztech_associated_assessment_id'] ) ) { + $assessment_id = sanitize_text_field( wp_unslash( $_POST['quiztech_associated_assessment_id'] ) ); + $assessment_id = '' === $assessment_id ? '' : absint( $assessment_id ); + update_post_meta( $new_or_updated_post_id, '_quiztech_associated_assessment_id', $assessment_id ); + } else { + // If not set in POST, maybe delete it? Or assume it wasn't part of the form? + // For now, let's assume if it's not sent, we don't change it. + // delete_post_meta($new_or_updated_post_id, '_quiztech_associated_assessment_id'); + } + // Add other meta field updates here if needed + } + // --- End Meta Fields Update --- + + + // Send success response + $success_message = $is_update + ? ( $result === 0 ? esc_html__( 'Job details saved (no changes detected).', 'quiztech' ) : esc_html__( 'Job updated successfully.', 'quiztech' ) ) + : esc_html__( 'Job created successfully.', 'quiztech' ); + + wp_send_json_success( [ + 'message' => $success_message, + 'job_id' => $new_or_updated_post_id + ] ); + } add_action( 'wp_ajax_quiztech_save_job', 'quiztech_ajax_save_job' ); // Use new action hook diff --git a/js/quiztech-theme.js b/js/quiztech-theme.js index fdf51e1..9337ec8 100644 --- a/js/quiztech-theme.js +++ b/js/quiztech-theme.js @@ -18,6 +18,7 @@ jQuery(document).ready(function($) { function resetAndPrepareForm(mode = 'add') { formElement[0].reset(); // Reset native form fields jobIdField.val(''); // Clear hidden ID + formElement.find('#quiztech_associated_assessment_id').val(''); // Reset assessment dropdown statusDiv.empty().removeClass('error success'); // Clear status messages spinner.css('visibility', 'hidden'); @@ -80,6 +81,8 @@ jQuery(document).ready(function($) { jobIdField.val(data.id); jobTitleField.val(data.title); jobDescriptionField.val(data.description); + // Populate the linked assessment dropdown + formElement.find('#quiztech_associated_assessment_id').val(data.assessment_id || ''); // Set to empty string if null/undefined formWrapper.slideDown(); // Show the form // Scroll to form for better UX @@ -114,7 +117,8 @@ jQuery(document).ready(function($) { nonce: quiztechThemeData.save_job_nonce, // Use new nonce job_id: jobIdField.val(), // Include job_id (will be empty for new jobs) job_title: jobTitleField.val(), - job_description: jobDescriptionField.val() + job_description: jobDescriptionField.val(), + quiztech_associated_assessment_id: formElement.find('#quiztech_associated_assessment_id').val() // Add assessment ID }; $.post(quiztechThemeData.ajax_url, formData) diff --git a/template-manage-jobs.php b/template-manage-jobs.php index 3fc27b2..d77b22f 100644 --- a/template-manage-jobs.php +++ b/template-manage-jobs.php @@ -115,6 +115,40 @@ get_header(); ?>

+

+
+ 'assessment', + 'posts_per_page' => -1, + 'post_status' => 'publish', + 'orderby' => 'title', + 'order' => 'ASC', + ] ); + + echo ''; + + if ( ! $assessments_query->have_posts() ) { + echo '
' . esc_html__( 'No assessments found. Please create an assessment first.', 'quiztech' ) . ''; + } + ?> +

+ +