Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
3ac9990
`/models` files security audit and fixes
Anindra123 Oct 1, 2026
7704e7d
Fix: typcast to int before placing it in string interpolation for query
Anindra123 Oct 1, 2026
0b0936c
Security Fixes
Anindra123 Oct 1, 2026
13cff07
Merge branch '4.1.1' into security/models
Anindra123 Oct 2, 2026
1813746
Fix: Query hardened for the several model class
Anindra123 Oct 2, 2026
9e185bb
updated abspath check to use shorthand syntax
Anindra123 Oct 6, 2026
a9c9e69
updated abspath check to use shorthand syntax
Anindra123 Oct 6, 2026
5305d1e
Merge pull request #3050 from themeum/models/UserModel
shewa12 Oct 7, 2026
84b34a2
Merge branch '4.2.0' into security/models
Anindra123 Oct 8, 2026
9796bf9
added given review changes
Anindra123 Oct 8, 2026
2c02474
Merge branch '4.2.0' into security/models
Anindra123 Oct 8, 2026
914cba3
Merge branch '4.2.0' into security/models
Anindra123 Oct 14, 2026
1b6007a
Merge branch 'security/models' into models/QuizModel
Anindra123 Oct 14, 2026
38f791f
added requested changes
Anindra123 Oct 14, 2026
016ddd5
Merge branch '4.2.0' into security/models
Anindra123 Oct 14, 2026
ac52486
Merge branch 'security/models' into models/QuizModel
Anindra123 Oct 14, 2026
9b62dbd
added requested changes
Anindra123 Oct 14, 2026
9f1cbda
Merge branch '4.2.0' into security/models
Anindra123 Oct 14, 2026
b97134f
Merge branch 'security/models' into models/fixes
Anindra123 Oct 14, 2026
1b0db45
updated array_filter for negative values and string values
Anindra123 Oct 14, 2026
51d7e4c
Merge branch 'security/models' into models/QuizModel
Anindra123 Oct 14, 2026
f10970f
added negative value check on filter
Anindra123 Oct 14, 2026
a0b0058
Merge branch '4.2.0' into security/models
Anindra123 Oct 22, 2026
16fcecf
Merge branch 'security/models' into models/fixes
Anindra123 Oct 22, 2026
2453f46
Merge branch 'security/models' into models/QuizModel
Anindra123 Oct 22, 2026
8437b12
Merge pull request #3055 from themeum/models/fixes
Anindra123 Oct 22, 2026
e23bcb3
Merge pull request #3053 from themeum/models/QuizModel
Anindra123 Oct 22, 2026
7590083
updated to use fluent query and added test cases
Anindra123 Oct 23, 2026
e8e0588
updated quiz model queries and added unit test
Anindra123 Oct 23, 2026
8991354
added fluent query in
Anindra123 Oct 23, 2026
67c455e
removed unused import
Anindra123 Oct 23, 2026
b931bd9
reverted subquery to original
Anindra123 Oct 26, 2026
aa1928f
Merge branch '4.2.0' into security/models
Anindra123 Oct 27, 2026
edff9ef
Updated Enrollment model query to use DB and added unit tests
Anindra123 Oct 27, 2026
78399b3
Added BillingModel unit test
Anindra123 Oct 27, 2026
f753ae2
update billing model to use DB and added test cases
Anindra123 Oct 27, 2026
26e46dc
Merge branch 'dev' into security/models
Anindra123 Oct 28, 2026
272fbd0
Merge branch '4.2.1' into chore/models/security-fixes
Anindra123 Oct 29, 2026
bc9028b
reverted query to original
Anindra123 Oct 29, 2026
81dcb12
removed billing model test case
Anindra123 Oct 29, 2026
8c8d7b2
Merge branch '4.2.2' into chore/models/security-fixes
Anindra123 Nov 11, 2026
1107826
Added requested changes
Anindra123 Nov 11, 2026
ac595f0
updated query to use get_posts and registered custom post statuses
Anindra123 Nov 11, 2026
04d0ff1
updated get_quiz_attempt_timing logic
Anindra123 Nov 11, 2026
17ca954
added custom post status during enrollment post type registration
Anindra123 Nov 11, 2026
465f8e9
updated custom post status registration
Anindra123 Nov 11, 2026
640cf87
updated custom post status registration
Anindra123 Nov 11, 2026
cec6ac2
Merge branch '4.2.2' into chore/models/security-fixes
Anindra123 Nov 13, 2026
f21c856
added requested changes
Anindra123 Nov 13, 2026
d287b0e
Merge branch '4.3.0' into chore/models/security-fixes
Anindra123 Nov 18, 2026
0b8ed19
Merge branch '4.3.0' into chore/models/security-fixes
Anindra123 Dec 2, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
27 changes: 27 additions & 0 deletions classes/Post_types.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Tutor\Core\Input;
use Tutor\Models\CourseModel;
use Tutor\Models\EnrollmentModel;

/**
* Register Tutor's post types
Expand Down Expand Up @@ -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 <span class="count">(%s)</span>', 'Completed <span class="count">(%s)</span>', '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 <span class="count">(%s)</span>', 'Cancel <span class="count">(%s)</span>', 'tutor' ),
)
);
}

/**
Expand Down
51 changes: 16 additions & 35 deletions models/CourseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -162,45 +165,23 @@
* @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;
}

/**
Expand Down Expand Up @@ -977,7 +958,7 @@
);

// Check if the current user is an admin.
if ( ! current_user_can( 'administrator' ) ) {
if ( ! current_user_can( 'manage_options' ) ) {
$args['author'] = $current_user->ID;
}

Expand Down Expand Up @@ -1414,7 +1395,7 @@
),
);

$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(
Expand Down Expand Up @@ -1758,7 +1739,7 @@
$args = wp_parse_args( $args, $defaults );

if ( ! $args['course_id'] || ! $args['current_post_id'] || ! $args['current_post_type'] ) {
throw new InvalidArgumentException( __( 'Invalid argument passed', 'tutor' ) );

Check failure on line 1742 in models/CourseModel.php

View workflow job for this annotation

GitHub Actions / WPCS

All output should be run through an escaping function (see the Security sections in the WordPress Developer Handbooks), found '__'.
}

$has_access = false;
Expand Down
59 changes: 32 additions & 27 deletions models/EnrollmentModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Comment thread
Anindra123 marked this conversation as resolved.
Resolved
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 );
}

/**
Expand Down
7 changes: 3 additions & 4 deletions models/OrderModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,7 +24,6 @@
use Tutor\Helpers\DateTimeHelper;
use Tutor\Helpers\QueryHelper;
use TUTOR\User;
use TutorPro\Ecommerce\Invoice;

/**
* OrderModel Class
Expand Down Expand Up @@ -421,7 +419,8 @@
}
}
} catch ( \Throwable $th ) {
$wpdb->query( 'ROLLBACK' );
throw new \Exception( $th->getMessage() );

Check failure on line 423 in models/OrderModel.php

View workflow job for this annotation

GitHub Actions / WPCS

All output should be run through an escaping function (see the Security sections in the WordPress Developer Handbooks), found '$th'.
}
}

Expand Down Expand Up @@ -1095,7 +1094,7 @@
//phpcs:enable

if ( $wpdb->last_error ) {
throw new \Exception( $wpdb->last_error );

Check failure on line 1097 in models/OrderModel.php

View workflow job for this annotation

GitHub Actions / WPCS

All output should be run through an escaping function (see the Security sections in the WordPress Developer Handbooks), found '$wpdb'.
} else {
$response['results'] = $results;
$response['total_count'] = is_array( $results ) && count( $results ) ? (int) $wpdb->get_var( 'SELECT FOUND_ROWS()' ) : 0;
Expand Down Expand Up @@ -1718,7 +1717,7 @@
if ( ! is_null( $sale_price ) && $sale_price < $regular_price ) {
$item_subtotal = $sale_price;
$item_total = $sale_price;
} else {

Check failure on line 1720 in models/OrderModel.php

View workflow job for this annotation

GitHub Actions / WPCS

If control structure block found as the only statement within an &quot;else&quot; block. Use elseif instead.
// If there's a discount, apply it to the total price.
if ( ! is_null( $discount_price ) && $discount_price >= 0 ) {
$item_total = max( 0, $discount_price ); // Ensure total doesn't go below 0.
Expand All @@ -1743,7 +1742,7 @@
if ( ! is_null( $sale_price ) && $sale_price < $regular_price ) {
$item_subtotal = $sale_price;
$item_total = $sale_price;
} else {

Check failure on line 1745 in models/OrderModel.php

View workflow job for this annotation

GitHub Actions / WPCS

If control structure block found as the only statement within an &quot;else&quot; block. Use elseif instead.
// If there's a discount, apply it to the total price.
if ( ! is_null( $discount_price ) && $discount_price >= 0 ) {
$item_total = max( 0, $discount_price ); // Ensure total doesn't go below 0.
Expand Down
72 changes: 36 additions & 36 deletions models/QuizModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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'] ) ) {
Expand Down Expand Up @@ -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;
}

/**
Expand Down
1 change: 1 addition & 0 deletions models/UserModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Loading
Loading