diff --git a/.agent/skills/core-development-skill/SKILL.md b/.agents/skills/core-development-skill/SKILL.md similarity index 99% rename from .agent/skills/core-development-skill/SKILL.md rename to .agents/skills/core-development-skill/SKILL.md index b5f71641be..1421698b09 100644 --- a/.agent/skills/core-development-skill/SKILL.md +++ b/.agents/skills/core-development-skill/SKILL.md @@ -1,3 +1,8 @@ +--- +name: core-development-skill +description: This skill governs all development, bug fixing, and feature work inside the **Tutor LMS** plugin codebase (`github.com/themeum/tutor`). It covers project structure, namespace conventions, coding patterns, PHPDoc standards, available helpers/utilities, security rules, and AI agent instructions. +--- + # Tutor LMS — Core Development Skill ## Overview diff --git a/.agent/skills/wpcs-security-skill/SKILL.md b/.agents/skills/wpcs-security-skill/SKILL.md similarity index 99% rename from .agent/skills/wpcs-security-skill/SKILL.md rename to .agents/skills/wpcs-security-skill/SKILL.md index 51ebe7845f..4c98fa5a22 100644 --- a/.agent/skills/wpcs-security-skill/SKILL.md +++ b/.agents/skills/wpcs-security-skill/SKILL.md @@ -1,3 +1,8 @@ +--- +name: wpcs-security-skill +description: Enforces strict WordPress coding standards (WPCS) and security best practices for PHP, JS, CSS, and HTML. +--- + # WordPress Coding Standards & Security Skill ## Overview diff --git a/classes/Post_types.php b/classes/Post_types.php index 4cca4b1b6e..0c8c5278a9 100644 --- a/classes/Post_types.php +++ b/classes/Post_types.php @@ -15,6 +15,7 @@ use Tutor\Core\Input; use Tutor\Models\CourseModel; +use Tutor\Models\EnrollmentModel; /** * Register Tutor's post types @@ -540,6 +541,32 @@ public function register_tutor_enrolled_post_types() { ), ); register_post_type( tutor()->enrollment_post_type, $args ); + + register_post_status( + EnrollmentModel::STATUS_COMPLETED, + array( + 'label' => _x( 'Completed', 'post status', 'tutor' ), + 'public' => false, + 'exclude_from_search' => false, + 'show_in_admin_all_list' => false, + 'show_in_admin_status_list' => false, + // translators: %s: count of posts in this status. + 'label_count' => _n_noop( 'Completed (%s)', 'Completed (%s)', 'tutor' ), + ) + ); + + register_post_status( + EnrollmentModel::STATUS_CANCEL, + array( + 'label' => _x( 'Cancel', 'post status', 'tutor' ), + 'public' => false, + 'exclude_from_search' => false, + 'show_in_admin_all_list' => false, + 'show_in_admin_status_list' => false, + // translators: %s: count of posts in this status. + 'label_count' => _n_noop( 'Cancel (%s)', 'Cancel (%s)', 'tutor' ), + ) + ); } /** diff --git a/models/CourseModel.php b/models/CourseModel.php index de58431157..7780850e2d 100644 --- a/models/CourseModel.php +++ b/models/CourseModel.php @@ -10,12 +10,15 @@ namespace Tutor\Models; +defined( 'ABSPATH' ) || exit; + use Tutor\Helpers\DateTimeHelper; use TUTOR\Icon; use TUTOR\Course; use TUTOR\Lesson; use Tutor\Ecommerce\Tax; use InvalidArgumentException; +use Tutor\Core\DB; use Tutor\Helpers\QueryHelper; use TUTOR\User; use TUTOR_ASSIGNMENTS\Assignments; @@ -162,45 +165,23 @@ public static function get_post_types( $post ) { * @return array|null|object */ public static function get_courses( $excludes = array(), $post_status = array( 'publish' ) ) { - global $wpdb; - $excludes = (array) $excludes; - $exclude_query = ''; + $excludes = array_values( array_filter( array_map( 'intval', (array) $excludes ), fn( $id ) => $id > 0 ) ); + $post_status = array_intersect( $post_status, self::get_status_list() ); - if ( count( $excludes ) ) { - $exclude_query = implode( "','", $excludes ); + if ( empty( $post_status ) ) { + $post_status = array( 'publish' ); } - $post_status = array_map( - function ( $element ) { - return "'" . $element . "'"; - }, - $post_status - ); - - $post_status = implode( ',', $post_status ); - $course_post_type = tutor()->course_post_type; - - //phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared - $query = $wpdb->get_results( - $wpdb->prepare( - "SELECT ID, - post_author, - post_title, - post_name, - post_status, - menu_order - FROM {$wpdb->posts} - WHERE post_status IN ({$post_status}) - AND ID NOT IN('$exclude_query') - AND post_type = %s; - ", - $course_post_type + return get_posts( + array( + 'posts_per_page' => -1, + 'post_status' => $post_status, + 'post_type' => self::POST_TYPE, + 'post__not_in' => $excludes, + 'fields' => array( 'ID', 'post_author', 'post_title', 'post_name', 'post_status', 'menu_order' ), ) ); - //phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared - - return $query; } /** @@ -977,7 +958,7 @@ public static function get_paid_courses( array $args = array() ) { ); // Check if the current user is an admin. - if ( ! current_user_can( 'administrator' ) ) { + if ( ! current_user_can( 'manage_options' ) ) { $args['author'] = $current_user->ID; } @@ -1414,7 +1395,7 @@ public static function get_course_dropdown_options() { ), ); - $courses = current_user_can( 'administrator' ) ? self::get_courses() : self::get_courses_by_instructor(); + $courses = current_user_can( 'manage_options' ) ? self::get_courses() : self::get_courses_by_instructor(); if ( ! empty( $courses ) ) { foreach ( $courses as $course ) { $course_options[] = array( diff --git a/models/EnrollmentModel.php b/models/EnrollmentModel.php index f7fcdd6c3d..91db5a5be0 100644 --- a/models/EnrollmentModel.php +++ b/models/EnrollmentModel.php @@ -10,7 +10,10 @@ namespace Tutor\Models; +defined( 'ABSPATH' ) || exit; + use Tutor\Cache\TutorCache; +use Tutor\Core\DB; use TUTOR\Course; use Tutor\Helpers\QueryHelper; use TUTOR\User; @@ -282,35 +285,37 @@ public static function get_enrolment_by_enrol_id( $enrol_id = 0 ) { * @return object|mixed */ public static function get_enrolled_data( $user_id = 0, $course_id = 0, $status = self::STATUS_COMPLETED ) { - global $wpdb; - $status_clause = $status ? $wpdb->prepare( 'AND post_status = %s ', $status ) : ''; - // If course ID provided, it will return single row data. + $user_id = absint( $user_id ); + $course_id = absint( $course_id ); + + if ( ! empty( $status ) && ! in_array( $status, array( self::STATUS_COMPLETED, self::STATUS_CANCEL, self::STATUS_PENDING ), true ) ) { + return null; + } + + $args = array( + 'posts_per_page' => -1, + 'post_type' => self::POST_TYPE, + 'post_status' => array( self::STATUS_COMPLETED, self::STATUS_CANCEL, self::STATUS_PENDING ), + ); + + if ( $user_id > 0 ) { + $args['author'] = $user_id; + } + + if ( $course_id > 0 && in_array( get_post_type( $course_id ), array( tutor()->course_post_type, tutor()->bundle_post_type ), true ) ) { + $args['post_parent'] = $course_id; + } + + if ( ! empty( $status ) ) { + $args['post_status'] = $status; + } + if ( $course_id > 0 ) { - return $wpdb->get_row( - $wpdb->prepare( - "SELECT * FROM {$wpdb->posts} - WHERE post_type = %s - AND post_parent = %d - {$status_clause} - AND post_author = %d;", - self::POST_TYPE, - $course_id, - $user_id - ) - ); - } else { - // Return all enrolled data by user ID. - return $wpdb->get_results( - $wpdb->prepare( - "SELECT * FROM {$wpdb->posts} - WHERE post_type = %s - {$status_clause} - AND post_author = %d;", - self::POST_TYPE, - $user_id - ) - ); + $posts = get_posts( $args ); + return ! empty( $posts ) ? $posts[0] : null; } + + return get_posts( $args ); } /** diff --git a/models/OrderModel.php b/models/OrderModel.php index e1070751ab..e9cc8db2dc 100644 --- a/models/OrderModel.php +++ b/models/OrderModel.php @@ -10,12 +10,11 @@ namespace Tutor\Models; +defined( 'ABSPATH' ) || exit; + use DateTime; use Exception; use Tutor\Cache\TutorCache; -use Tutor\Components\Badge; -use Tutor\Components\Button; -use Tutor\Components\Constants\Variant; use TUTOR\Earnings; use Tutor\Ecommerce\BillingController; use Tutor\Ecommerce\CheckoutController; @@ -25,7 +24,6 @@ use Tutor\Helpers\DateTimeHelper; use Tutor\Helpers\QueryHelper; use TUTOR\User; -use TutorPro\Ecommerce\Invoice; /** * OrderModel Class @@ -421,6 +419,7 @@ public function create_order( array $data ) { } } } catch ( \Throwable $th ) { + $wpdb->query( 'ROLLBACK' ); throw new \Exception( $th->getMessage() ); } } diff --git a/models/QuizModel.php b/models/QuizModel.php index fd88311e02..9be7f9a1e8 100644 --- a/models/QuizModel.php +++ b/models/QuizModel.php @@ -10,8 +10,11 @@ namespace Tutor\Models; +defined( 'ABSPATH' ) || exit; + use Tutor\Cache\TutorCache; use Tutor\Components\Badge; +use Tutor\Core\DB; use TUTOR\Course_List; use Tutor\Helpers\DateTimeHelper; use Tutor\Helpers\QueryHelper; @@ -968,27 +971,21 @@ public static function get_answers_by_quiz_question( $question_id, $rand = false * @return array|null|object */ public static function get_quiz_answers_by_attempt_id( $attempt_id, $add_index = false ) { - global $wpdb; - $ids = is_array( $attempt_id ) ? $attempt_id : array( $attempt_id ); - $ids_in = implode( ',', $ids ); + $ids = array_values( array_filter( array_map( 'intval', (array) $attempt_id ), fn( $id ) => $id > 0 ) ); - if ( empty( $ids_in ) ) { + if ( empty( $ids ) ) { // Prevent empty. return array(); } - //phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared - $results = $wpdb->get_results( - "SELECT answers.*, - question.* - FROM {$wpdb->prefix}tutor_quiz_attempt_answers answers - LEFT JOIN {$wpdb->prefix}tutor_quiz_questions question - ON answers.question_id = question.question_id - WHERE answers.quiz_attempt_id IN ({$ids_in}) - ORDER BY attempt_answer_id ASC;" - ); - //phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared + $results = DB::table( 'tutor_quiz_attempt_answers' ) + ->as( 'answers' ) + ->select( 'answers.*', 'question.*' ) + ->join( 'tutor_quiz_questions as question', 'answers.question_id', '=', 'question.question_id' ) + ->where_in( 'answers.quiz_attempt_id', $ids ) + ->order_by( 'answers.attempt_answer_id' ) + ->get(); if ( $add_index ) { $new_array = array(); @@ -1396,7 +1393,17 @@ public static function get_attempt_date( $attempt_data, string $format = '' ): s public static function get_quiz_attempt_timing( $attempt_data ) { $attempt_duration = ''; $attempt_duration_taken = ''; - $attempt_info = @unserialize( $attempt_data->attempt_info ); + $raw_info = $attempt_data->attempt_info ?? ''; + $attempt_info = ''; + + if ( is_array( $raw_info ) ) { + $attempt_info = $raw_info; + } elseif ( is_string( $raw_info ) && is_serialized( $raw_info ) ) { + $attempt_info = tutor_unserialize( $raw_info ); + } else { + $attempt_info = array(); + } + if ( is_array( $attempt_info ) ) { // Allowed duration. if ( isset( $attempt_info['time_limit'] ) ) { @@ -1521,33 +1528,26 @@ public function get_first_or_last_attempt( int $quiz_id, int $user_id = 0, strin */ public static function get_quiz_count_by_course( $course_id ) { global $wpdb; + $course_ids = array_values( array_filter( array_map( 'intval', (array) $course_id ), fn( $id ) => $id > 0 ) ); + if ( empty( $course_ids ) ) { + return 0; + } - $and_clause = is_array( $course_id ) && count( $course_id ) ? ' AND post_parent IN (' . QueryHelper::prepare_in_clause( $course_id ) . ')' : "AND post_parent = $course_id"; + $placeholders = implode( ', ', array_fill( 0, count( $course_ids ), '%d' ) ); //phpcs:disable - $count = $wpdb->get_var( + return (int) $wpdb->get_var( $wpdb->prepare( - "SELECT - COUNT(ID) - FROM {$wpdb->posts} - WHERE post_parent IN - (SELECT - ID - FROM {$wpdb->posts} - WHERE post_type = %s - {$and_clause} - AND post_status = %s - ) - AND post_type = %s - AND post_status = %s", - 'topics', - 'publish', - 'tutor_quiz', - 'publish' + "SELECT COUNT(ID) FROM {$wpdb->posts} + WHERE post_parent IN ( + SELECT ID FROM {$wpdb->posts} + WHERE post_type = 'topics' AND post_parent IN ({$placeholders}) AND post_status = 'publish' + ) + AND post_type = %s AND post_status = 'publish'", + array( ...$course_ids, tutor()->quiz_post_type ) ) ); //phpcs:enable - return $count ? $count : 0; } /** diff --git a/models/UserModel.php b/models/UserModel.php index 678c6a09de..71f2f356b8 100644 --- a/models/UserModel.php +++ b/models/UserModel.php @@ -60,6 +60,7 @@ public function get_users_list( array $args ) { public function get_unenrolled_users( $object_id, $search_clause = array(), $limit = 10, $offset = 0 ) { global $wpdb; + $object_id = absint( $object_id ); $primary_table = "{$wpdb->users} AS u"; $joining_tables = array( array( diff --git a/tests/phpunit/CourseModelTest.php b/tests/phpunit/CourseModelTest.php index 0113b41554..2d34a7503e 100644 --- a/tests/phpunit/CourseModelTest.php +++ b/tests/phpunit/CourseModelTest.php @@ -1,14 +1,15 @@ post->create( + array( + 'post_type' => tutor()->course_post_type, + 'post_title' => 'Test Course', + 'post_status' => 'publish', + ) + ); + $course2 = self::factory()->post->create( + array( + 'post_type' => tutor()->course_post_type, + 'post_title' => 'Test Course 2', + 'post_status' => 'draft', + ) + ); + + $courses = CourseModel::get_courses(); + + $this->assertEquals( 1, count( $courses ) ); + + $courses = CourseModel::get_courses( array(), array( 'publish', 'draft' ) ); + + $this->assertEquals( 2, count( $courses ) ); + + $courses = CourseModel::get_courses( array( $course2 ) ); + + $this->assertEquals( 1, count( $courses ) ); + $this->assertEquals( $course1, $courses[0]->ID ); } } diff --git a/tests/phpunit/EnrollmentModelTest.php b/tests/phpunit/EnrollmentModelTest.php new file mode 100644 index 0000000000..ae663011bc --- /dev/null +++ b/tests/phpunit/EnrollmentModelTest.php @@ -0,0 +1,170 @@ +post->create( + array( + 'post_type' => 'courses', + 'post_status' => 'publish', + 'post_author' => $user_id, + ) + ); + $enroll_id_1 = self::factory()->post->create( + array( + 'post_type' => EnrollmentModel::POST_TYPE, + 'post_status' => EnrollmentModel::STATUS_COMPLETED, + 'post_author' => $user_id, + 'post_parent' => $course_id, + ) + ); + $enroll_id_2 = self::factory()->post->create( + array( + 'post_type' => EnrollmentModel::POST_TYPE, + 'post_status' => EnrollmentModel::STATUS_COMPLETED, + 'post_author' => $user_id, + 'post_parent' => $course_id, + ) + ); + + $enroll_id_3 = self::factory()->post->create( + array( + 'post_type' => EnrollmentModel::POST_TYPE, + 'post_status' => EnrollmentModel::STATUS_COMPLETED, + 'post_author' => $user_id, + 'post_parent' => $course_id, + ) + ); + + $total_enrollments = EnrollmentModel::get_enrolled_data( $user_id, null ); + + $this->assertEquals( 3, count( $total_enrollments ) ); + + $enrollment = EnrollmentModel::delete_enrollment_record( $user_id, $course_id ); + $this->assertTrue( $enrollment ); + + $total_enrollments = EnrollmentModel::get_enrolled_data( $user_id, $course_id ); + $this->assertEquals( null, $total_enrollments ); + } + + /** + * Test get enrolled data. + * + * @return void + */ + public function test_get_enrolled_data() { + $user_id = 1; + $course_id = self::factory()->post->create( + array( + 'post_type' => 'courses', + 'post_status' => 'publish', + 'post_author' => $user_id, + ) + ); + + $enroll_id_1 = self::factory()->post->create( + array( + 'post_type' => EnrollmentModel::POST_TYPE, + 'post_status' => EnrollmentModel::STATUS_COMPLETED, + 'post_author' => $user_id, + 'post_parent' => $course_id, + ) + ); + + self::factory()->post->create( + array( + 'post_type' => EnrollmentModel::POST_TYPE, + 'post_status' => EnrollmentModel::STATUS_PENDING, + 'post_author' => $user_id, + 'post_parent' => $course_id, + ) + ); + + self::factory()->post->create( + array( + 'post_type' => EnrollmentModel::POST_TYPE, + 'post_status' => EnrollmentModel::STATUS_CANCEL, + 'post_author' => $user_id, + 'post_parent' => $course_id, + ) + ); + + $enrollments = EnrollmentModel::get_enrolled_data( $user_id, $course_id ); + + $this->assertEquals( $enroll_id_1, $enrollments->ID ); + + $enrollments = EnrollmentModel::get_enrolled_data( $user_id ); + + $this->assertEquals( 1, count( $enrollments ) ); + } + + /** + * Test update enroll data. + * + * @return void + */ + public function test_update_enroll_data() { + $user_id = 1; + $course_id = self::factory()->post->create( + array( + 'post_type' => 'courses', + 'post_status' => 'publish', + 'post_author' => $user_id, + ) + ); + + $enroll_id_1 = self::factory()->post->create( + array( + 'post_type' => EnrollmentModel::POST_TYPE, + 'post_status' => EnrollmentModel::STATUS_COMPLETED, + 'post_author' => $user_id, + 'post_parent' => $course_id, + ) + ); + + $update = EnrollmentModel::update_enrollments( EnrollmentModel::STATUS_CANCEL, array( $enroll_id_1 ), false ); + + $this->assertTrue( $update ); + + $updated_enrollment = EnrollmentModel::get_enrolled_data( $user_id, $course_id, EnrollmentModel::STATUS_CANCEL ); + $this->assertEquals( EnrollmentModel::STATUS_CANCEL, $updated_enrollment->post_status ); + + $enroll_id_2 = self::factory()->post->create( + array( + 'post_type' => EnrollmentModel::POST_TYPE, + 'post_status' => EnrollmentModel::STATUS_COMPLETED, + 'post_author' => $user_id, + 'post_parent' => $course_id, + ) + ); + + $update = EnrollmentModel::update_enrollments( EnrollmentModel::STATUS_PENDING, array( $enroll_id_1, $enroll_id_2 ), false ); + + $this->assertTrue( $update ); + + $updated_enrollment = EnrollmentModel::get_enrolled_data( $user_id, $course_id, EnrollmentModel::STATUS_PENDING ); + $this->assertEquals( EnrollmentModel::STATUS_PENDING, $updated_enrollment->post_status ); + $this->assertEquals( EnrollmentModel::STATUS_PENDING, $updated_enrollment->post_status ); + } +} diff --git a/tests/phpunit/QuizModelTest.php b/tests/phpunit/QuizModelTest.php new file mode 100644 index 0000000000..b34a415ed9 --- /dev/null +++ b/tests/phpunit/QuizModelTest.php @@ -0,0 +1,59 @@ +post->create( + array( + 'post_type' => tutor()->course_post_type, + 'post_status' => 'publish', + 'post_title' => 'Test Course', + ) + ); + + $topic_id = self::factory()->post->create( + array( + 'post_type' => tutor()->topics_post_type, + 'post_status' => 'publish', + 'post_parent' => $course_id, + 'post_title' => 'Topic', + 'comment_status' => 'closed', + ) + ); + + $quiz_id = self::factory()->post->create( + array( + 'post_type' => tutor()->quiz_post_type, + 'post_status' => 'publish', + 'post_parent' => $topic_id, + 'post_title' => 'Quiz', + ) + ); + + $count = QuizModel::get_quiz_count_by_course( $course_id ); + + $this->assertEquals( 1, $count ); + } +}