'new', 'course_id' => $course_id, 'title' => '', 'description' => '', ) ); $html = ob_get_clean(); wp_send_json_success( array( 'html' => $html ) ); } /** * Render markup for a new (unsaved) lesson card within a curriculum section. * * @return void Sends a JSON success response containing the rendered HTML and exits. */ public static function add_lesson() { check_ajax_referer( 'aimslms_curriculum', 'nonce' ); if ( ! current_user_can( 'edit_posts' ) ) { wp_send_json_error( 'Permission denied' ); return; } $section_id = isset( $_POST['section_id'] ) ? intval( $_POST['section_id'] ) : 0; $lesson_id = 'new'; ob_start(); ?>
$html ) ); } /** * Handle AJAX request to create or update a curriculum section along with its lessons. * * @return void Sends a JSON success/error response and exits. */ public static function save_section() { check_ajax_referer( 'aimslms_curriculum', 'nonce' ); if ( ! current_user_can( 'edit_posts' ) ) { wp_send_json_error( 'Permission denied' ); } $title = isset( $_POST['title'] ) ? sanitize_text_field( wp_unslash( $_POST['title'] ) ) : ''; $description = isset( $_POST['description'] ) ? wp_kses_post( wp_unslash( $_POST['description'] ) ) : ''; $raw_lessons = array(); if ( ! empty( $_POST['lessons'] ) && is_array( $_POST['lessons'] ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Each lesson field is individually sanitized per its type (sanitize_text_field/wp_kses_post) at its point of use below; blanket-sanitizing here would strip legitimate HTML from lesson content. $raw_lessons = wp_unslash( $_POST['lessons'] ); } // SECTION TITLE VALIDATION. if ( empty( $title ) || ! trim( $title ) ) { wp_send_json_error( 'Section title is required.' ); } // LESSON TITLE VALIDATION. if ( ! empty( $raw_lessons ) ) { foreach ( $raw_lessons as $index => $lesson ) { if ( empty( $lesson['title'] ) || ! trim( $lesson['title'] ) ) { wp_send_json_error( sprintf( 'Lesson title is required (Lesson #%d).', $index + 1 ) ); } } } global $wpdb; $course_id = isset( $_POST['course_id'] ) ? intval( $_POST['course_id'] ) : 0; $section_id = isset( $_POST['section_id'] ) ? sanitize_text_field( wp_unslash( $_POST['section_id'] ) ) : ''; if ( 'new' !== $section_id ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table, no WP API alternative. $wpdb->update( $wpdb->prefix . 'aimslms_course_sections', array( 'title' => $title, 'description' => $description, ), array( 'id' => $section_id ), array( '%s', '%s' ), array( '%d' ) ); } else { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table, no WP API alternative. $next_sort_order = (int) $wpdb->get_var( $wpdb->prepare( "SELECT MAX(sort_order) FROM {$wpdb->prefix}aimslms_course_sections WHERE course_id = %d", $course_id ) ); // If no lesson yet. $next_sort_order = $next_sort_order ? ( $next_sort_order + 1 ) : 1; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery -- Custom table, no WP API alternative. $wpdb->insert( $wpdb->prefix . 'aimslms_course_sections', array( 'course_id' => intval( $course_id ), 'title' => $title, 'description' => $description, 'sort_order' => $next_sort_order, ), array( '%d', '%s', '%s', '%d' ), ); $section_id = $wpdb->insert_id; } $lesson_id_map = array(); if ( ! empty( $raw_lessons ) ) { foreach ( $raw_lessons as $lesson ) { $lesson_id = $lesson['id']; $lesson_temp_key = $lesson['temp_key']; $material_url = ''; // Handle file upload. if ( isset( $_FILES['lesson_files']['name'][ $lesson_temp_key ] ) ) { $file = array( 'name' => sanitize_file_name( wp_unslash( $_FILES['lesson_files']['name'][ $lesson_temp_key ] ) ), 'type' => isset( $_FILES['lesson_files']['type'][ $lesson_temp_key ] ) ? sanitize_text_field( wp_unslash( $_FILES['lesson_files']['type'][ $lesson_temp_key ] ) ) : '', // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Internal PHP temp file path, not user-editable text; sanitizing would corrupt it. 'tmp_name' => isset( $_FILES['lesson_files']['tmp_name'][ $lesson_temp_key ] ) ? $_FILES['lesson_files']['tmp_name'][ $lesson_temp_key ] : '', 'error' => isset( $_FILES['lesson_files']['error'][ $lesson_temp_key ] ) ? (int) $_FILES['lesson_files']['error'][ $lesson_temp_key ] : UPLOAD_ERR_NO_FILE, 'size' => isset( $_FILES['lesson_files']['size'][ $lesson_temp_key ] ) ? (int) $_FILES['lesson_files']['size'][ $lesson_temp_key ] : 0, ); $allowed_types = array( 'application/pdf', 'text/plain', ); if ( strpos( $file['type'], 'video/' ) === 0 ) { $allowed_types[] = $file['type']; } if ( ! in_array( $file['type'], $allowed_types, true ) ) { wp_send_json_error( 'Invalid file type' ); } require_once ABSPATH . 'wp-admin/includes/file.php'; $upload = wp_handle_upload( $file, array( 'test_form' => false ) ); if ( isset( $upload['url'] ) ) { $material_url = esc_url_raw( $upload['url'] ); } } $is_update = ( 'new' !== $lesson_id ); $lesson_slug = sanitize_title( $lesson['title'] ); $original_slug = $lesson_slug; $i = 1; if ( $is_update ) { // Get existing slug. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table, no WP API alternative. $existing_slug = $wpdb->get_var( $wpdb->prepare( "SELECT slug FROM {$wpdb->prefix}aimslms_course_lessons WHERE id = %d", $lesson_id ) ); // If title not changed → keep slug. if ( $existing_slug === $lesson_slug ) { $lesson_slug = $existing_slug; } else { // Title changed → ensure uniqueness (exclude current lesson). // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table, no WP API alternative. while ( $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->prefix}aimslms_course_lessons WHERE slug = %s AND course_id = %d AND id != %d", $lesson_slug, $course_id, $lesson_id ) ) ) { $lesson_slug = $original_slug . '-' . ( $i++ ); } } } else { // New lesson → always enforce uniqueness. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table, no WP API alternative. while ( $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->prefix}aimslms_course_lessons WHERE slug = %s AND course_id = %d", $lesson_slug, $course_id ) ) ) { $lesson_slug = $original_slug . '-' . ( $i++ ); } } $data = array( 'course_id' => intval( $course_id ), 'section_id' => intval( $section_id ), 'title' => sanitize_text_field( $lesson['title'] ), 'slug' => sanitize_text_field( $lesson_slug ), 'content' => wp_kses_post( $lesson['content'] ), 'duration' => sanitize_text_field( $lesson['duration'] ), ); if ( ! empty( $material_url ) ) { $data['upload_file'] = $material_url; } if ( 'new' !== $lesson_id ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table, no WP API alternative. $wpdb->update( $wpdb->prefix . 'aimslms_course_lessons', $data, array( 'id' => $lesson_id ), array( '%s', '%s', '%s', '%s' ), array( '%d' ) ); } else { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table, no WP API alternative. $next_sort_order = (int) $wpdb->get_var( $wpdb->prepare( "SELECT MAX(sort_order) FROM {$wpdb->prefix}aimslms_course_lessons WHERE section_id = %d", $section_id ) ); // If no lesson yet. $next_sort_order = $next_sort_order ? ( $next_sort_order + 1 ) : 1; $data['sort_order'] = $next_sort_order; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery -- Custom table, no WP API alternative. $wpdb->insert( $wpdb->prefix . 'aimslms_course_lessons', $data, array( '%s', '%s', '%s', '%s' ), array( '%d' ) ); $new_lesson_id = $wpdb->insert_id; // MAP temp_key → real ID. if ( ! empty( $lesson['temp_key'] ) ) { $lesson_id_map[ $lesson['temp_key'] ] = $new_lesson_id; } } } } // Total sections for this course. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table, no WP API alternative. $sections_count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->prefix}aimslms_course_sections WHERE course_id = %d", $course_id ) ); // Total lessons for this section. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table, no WP API alternative. $lessons_count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->prefix}aimslms_course_lessons WHERE section_id = %d", $section_id ) ); wp_send_json_success( array( 'message' => 'Section updated successfully', 'section_id' => $section_id, 'lesson_map' => $lesson_id_map, 'section_count' => $sections_count ?? 0, 'lesson_count' => $lessons_count ?? 0, ) ); } /** * Handle AJAX request to delete a curriculum section and its lessons. * * @return void Sends a JSON success response and exits. */ public static function delete_section() { check_ajax_referer( 'aimslms_curriculum', 'nonce' ); if ( ! current_user_can( 'edit_posts' ) ) { wp_send_json_error( 'Permission denied' ); } global $wpdb; $course_id = isset( $_POST['course_id'] ) ? intval( $_POST['course_id'] ) : 0; $section_id = isset( $_POST['section_id'] ) ? intval( $_POST['section_id'] ) : 0; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table, no WP API alternative. $wpdb->delete( $wpdb->prefix . 'aimslms_course_lessons', array( 'section_id' => $section_id, ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table, no WP API alternative. $wpdb->delete( $wpdb->prefix . 'aimslms_course_sections', array( 'id' => $section_id, ) ); // Total sections for this course. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table, no WP API alternative. $sections_count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->prefix}aimslms_course_sections WHERE course_id = %d", $course_id ) ); // Total lessons for this section. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table, no WP API alternative. $lessons_count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->prefix}aimslms_course_lessons WHERE section_id = %d", $section_id ) ); wp_send_json_success( array( 'message' => 'Section deleted successfully', 'section_id' => $section_id, 'section_count' => $sections_count ?? 0, ) ); wp_send_json_success(); } /** * Handle AJAX request to delete a single curriculum lesson. * * @return void Sends a JSON success response and exits. */ public static function delete_lesson() { check_ajax_referer( 'aimslms_curriculum', 'nonce' ); if ( ! current_user_can( 'edit_posts' ) ) { wp_send_json_error( 'Permission denied' ); } global $wpdb; $section_id = isset( $_POST['section_id'] ) ? intval( $_POST['section_id'] ) : 0; $lesson_id = isset( $_POST['lesson_id'] ) ? intval( $_POST['lesson_id'] ) : 0; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table, no WP API alternative. $wpdb->delete( $wpdb->prefix . 'aimslms_course_lessons', array( 'id' => $lesson_id, ) ); // Total lessons for this section. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table, no WP API alternative. $lessons_count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->prefix}aimslms_course_lessons WHERE section_id = %d", $section_id ) ); wp_send_json_success( array( 'message' => 'Lesson deleted successfully', 'section_id' => $section_id, 'lesson_count' => $lessons_count ?? 0, ) ); wp_send_json_success(); } /** * Handle AJAX request to persist the new sort order of curriculum sections. * * @return void Sends a JSON success/error response and exits. */ public static function update_section_order() { check_ajax_referer( 'aimslms_curriculum', 'nonce' ); if ( ! current_user_can( 'edit_posts' ) ) { wp_send_json_error(); } global $wpdb; if ( empty( $_POST['order'] ) ) { wp_send_json_error(); } $order = map_deep( wp_unslash( $_POST['order'] ), 'sanitize_text_field' ); foreach ( $order as $item ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table, no WP API alternative. $wpdb->update( $wpdb->prefix . 'aimslms_course_sections', array( 'sort_order' => intval( $item['position'] ) ), array( 'id' => intval( $item['id'] ) ), array( '%d' ), array( '%d' ) ); } wp_send_json_success(); } /** * Handle AJAX request to persist the new sort order of lessons within a section. * * @return void Sends a JSON success/error response and exits. */ public static function update_lesson_order() { check_ajax_referer( 'aimslms_curriculum', 'nonce' ); if ( ! current_user_can( 'edit_posts' ) ) { wp_send_json_error(); } global $wpdb; if ( empty( $_POST['order'] ) || empty( $_POST['section_id'] ) ) { wp_send_json_error(); } $order = map_deep( wp_unslash( $_POST['order'] ), 'sanitize_text_field' ); foreach ( $order as $item ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table, no WP API alternative. $wpdb->update( $wpdb->prefix . 'aimslms_course_lessons', array( 'sort_order' => intval( $item['position'] ) ), array( 'id' => intval( $item['id'] ) ), array( '%d' ), array( '%d' ) ); } wp_send_json_success(); } /** * Handle AJAX request to record a learner's video playback progress for a lesson. * * @return void Sends a JSON success response and exits. */ public static function save_video_progress() { check_ajax_referer( 'aimslms_public_nonce', 'nonce' ); $course_id = isset( $_POST['course_id'] ) ? absint( $_POST['course_id'] ) : 0; $lesson_id = isset( $_POST['lesson_id'] ) ? absint( $_POST['lesson_id'] ) : 0; $time = isset( $_POST['time'] ) ? absint( $_POST['time'] ) : 0; $percentage = isset( $_POST['percentage'] ) ? absint( $_POST['percentage'] ) : 0; $completed = aimslms_check_lesson_status( $course_id, $lesson_id ); if ( 1 !== $completed ) { AIMSLMS_Progress_Tracker::save( array( 'lesson_id' => $lesson_id, 'course_id' => $course_id, 'time' => $time, 'percentage' => $percentage, ) ); } wp_send_json_success(); } /** * Handle AJAX request to mark a lesson as completed for the current user. * * @return void Sends a JSON success/error response and exits. */ public static function aimslms_mark_lesson_complete() { check_ajax_referer( 'aimslms_public_nonce', 'nonce' ); if ( ! is_user_logged_in() ) { wp_send_json_error( 'User not logged in' ); } $user_id = get_current_user_id(); $course_id = isset( $_POST['course_id'] ) ? intval( $_POST['course_id'] ) : 0; $lesson_id = isset( $_POST['lesson_id'] ) ? intval( $_POST['lesson_id'] ) : 0; // Mark lesson complete. AIMSLMS_Progress_Tracker::mark_complete( array( 'lesson_id' => $lesson_id, 'course_id' => $course_id, 'completed' => 1, ) ); // Get UPDATED progress after completion. $progress = aimslms_get_course_progress_data( $course_id ); // Get next lesson. $next_lesson_data = null; if ( function_exists( 'aimslms_get_prev_next_lesson' ) ) { [ $prev, $next ] = aimslms_get_prev_next_lesson( $course_id, $lesson_id ); if ( $next && aimslms_is_lesson_unlocked( $user_id, $course_id, $next->id ) ) { $next_lesson_data = array( 'id' => $next->id, 'title' => $next->title, 'url' => aimslms_get_lesson_url( $course_id, $next->id ), ); } } wp_send_json_success( array( 'lesson_id' => $lesson_id, 'completed' => intval( $progress['completed'] ), 'total' => intval( $progress['total'] ), 'percentage' => intval( $progress['percentage'] ), 'next' => $next_lesson_data, ) ); } /** * Handle AJAX request for admin dashboard chart data (enrollments, sales, or revenue over time). * * @return void Sends the chart labels/values as JSON and exits. */ public static function aimslms_get_chart_data() { if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) { wp_die(); } check_ajax_referer( 'aimslms_dashboard_chart', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => 'Permission denied' ), 403 ); } global $wpdb; $type = isset( $_GET['type'] ) ? sanitize_text_field( wp_unslash( $_GET['type'] ) ) : ''; $range = isset( $_GET['range'] ) ? sanitize_text_field( wp_unslash( $_GET['range'] ) ) : 'daily'; $where = ''; /* --------------------------------- * TABLE & DATE COLUMN MAPPING --------------------------------- */ switch ( $type ) { case 'enrollment': $table = $wpdb->prefix . 'aimslms_course_enrollments'; $date_column = 'enrollment_date'; $value_field = 'COUNT(id)'; $where = "AND status = 'active'"; break; case 'sales': $table = $wpdb->prefix . 'aimslms_course_orders'; $date_column = 'created_at'; $value_field = 'COUNT(id)'; $where = "AND order_status = 'completed'"; break; case 'revenue': default: $table = $wpdb->prefix . 'aimslms_course_payment'; $date_column = 'transaction_date'; // (spelling kept as-is) $value_field = 'SUM(defult_course_price)'; $where = "AND status = 'completed'"; break; } /* --------------------------------- * DATE GROUPING FORMAT --------------------------------- */ if ( 'yearly' === $range ) { $format = '%Y'; $limit = 10; } elseif ( 'monthly' === $range ) { $format = '%Y-%m'; $limit = 12; } else { $format = '%Y-%m-%d'; $limit = 30; } /* --------------------------------- * QUERY (STRICT-MODE SAFE) --------------------------------- */ // phpcs:disable WordPress.DB.PreparedSQL -- $table/$date_column/$value_field/$where/$limit are all assigned from hardcoded values in the switch/if-else above (keyed off an allowlisted $type/$range), never from raw request input; %s placeholders above cover the only real user-influenced values ($format). $sql = $wpdb->prepare( " SELECT DATE_FORMAT($date_column, %s) AS label, $value_field AS total FROM $table WHERE $date_column IS NOT NULL $where GROUP BY DATE_FORMAT($date_column, %s) ORDER BY label ASC LIMIT $limit ", $format, $format ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table, no WP API alternative. $rows = $wpdb->get_results( $sql ); // phpcs:enable WordPress.DB.PreparedSQL $results = array(); foreach ( $rows as $row ) { $results[ $row->label ] = (float) $row->total; } $labels = array(); $values = array(); $days = ( 'yearly' === $range ) ? 10 : ( ( 'monthly' === $range ) ? 12 : 30 ); for ( $i = $days - 1; $i >= 0; $i-- ) { if ( 'yearly' === $range ) { $date = gmdate( 'Y', strtotime( "-$i years" ) ); } elseif ( 'monthly' === $range ) { $date = gmdate( 'Y-m', strtotime( "-$i months" ) ); } else { $date = gmdate( 'Y-m-d', strtotime( "-$i days" ) ); } $labels[] = $date; $values[] = $results[ $date ] ?? 0; // fill missing with 0. } /* --------------------------------- * RETURN JSON (NO ECHO / PRINT) --------------------------------- */ wp_send_json( array( 'labels' => $labels, 'values' => $values, ) ); } } /* ====================== AJAX HOOKS ====================== */ add_action( 'wp_ajax_aimslms_add_section', array( 'AIMSLMS_Ajax_Handlers', 'add_section' ) ); add_action( 'wp_ajax_aimslms_add_lesson', array( 'AIMSLMS_Ajax_Handlers', 'add_lesson' ) ); add_action( 'wp_ajax_aimslms_save_section', array( 'AIMSLMS_Ajax_Handlers', 'save_section' ) ); add_action( 'wp_ajax_aimslms_delete_section', array( 'AIMSLMS_Ajax_Handlers', 'delete_section' ) ); add_action( 'wp_ajax_aimslms_delete_lesson', array( 'AIMSLMS_Ajax_Handlers', 'delete_lesson' ) ); add_action( 'wp_ajax_aimslms_update_section_order', array( 'AIMSLMS_Ajax_Handlers', 'update_section_order' ) ); add_action( 'wp_ajax_aimslms_update_lesson_order', array( 'AIMSLMS_Ajax_Handlers', 'update_lesson_order' ) ); add_action( 'wp_ajax_aimslms_save_video_progress', array( 'AIMSLMS_Ajax_Handlers', 'save_video_progress' ) ); add_action( 'wp_ajax_aimslms_mark_lesson_complete', array( 'AIMSLMS_Ajax_Handlers', 'aimslms_mark_lesson_complete' ) ); add_action( 'wp_ajax_aimslms_get_chart_data', array( 'AIMSLMS_Ajax_Handlers', 'aimslms_get_chart_data' ) );