Skip to content

Commit

Permalink
= 4.2.6.5 =
Browse files Browse the repository at this point in the history
~ Added: load courses of subcategories for Parent Category.
  • Loading branch information
tungnxt89 committed Apr 15, 2024
1 parent 102a9d2 commit 02f3dcf
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
8 changes: 8 additions & 0 deletions config/settings/course.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@
),
'css' => 'min-width: 50px; width: 50px;',
),
array(
'title' => esc_html__( 'Load courses of subcategory', 'learnpress' ),
'desc' => esc_html__( 'Load courses of subcategories on Parent Category without tick Parent.', 'learnpress' ),
'id' => 'get_courses_of_subcategory',
'default' => 'no',
'type' => 'checkbox',
'css' => 'min-width: 50px; width: 50px;',
),
array(
'title' => esc_html__( 'Loading ajax Courses', 'learnpress' ),
'desc' => __( 'On/Off <i>loading ajax courses on the Course Archive page </i>.', 'learnpress' ),
Expand Down
46 changes: 43 additions & 3 deletions inc/Databases/class-lp-course-db.php
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ public function get_total_items( int $course_id = 0 ) {
$query_count = $this->wpdb->prepare( 'SUM(s.section_course_id = %d) AS count_items,', $course_id );

foreach ( $item_types as $item_type ) {
++$i;
++ $i;
if ( $i == $count_item_types ) {
$query_count .= $this->wpdb->prepare( 'SUM(s.section_course_id = %d AND si.item_type = %s) AS %s', $course_id, $item_type, $item_type );
} else {
Expand Down Expand Up @@ -503,8 +503,14 @@ public function get_courses( LP_Course_Filter $filter, int &$total_rows = 0 ) {
$filter->join[] = "INNER JOIN $this->tb_term_relationships AS r_term ON p.ID = r_term.object_id";
$filter->join[] = "INNER JOIN $this->tb_term_taxonomy AS tx ON r_term.term_taxonomy_id = tx.term_taxonomy_id";

$term_ids_format = LP_Helper::db_format_array( $filter->term_ids, '%d' );
$filter->where[] = $this->wpdb->prepare( 'AND tx.term_id IN (' . $term_ids_format . ')', $filter->term_ids );
if ( LP_Settings::get_option( 'get_courses_of_subcategory' ) !== 'yes' ) {
$term_ids_format = esc_sql( join( ',', $filter->term_ids ) );
$filter->where[] = $this->wpdb->prepare( 'AND tx.term_id IN (' . $term_ids_format . ')' );
} else {
$term_all = $this->recursion_sub_categories( $filter->term_ids );
$term_ids_format = esc_sql( join( ',', $term_all ) );
$filter->where[] = "AND tx.term_id IN ($term_ids_format)";
}
$filter->where[] = $this->wpdb->prepare( 'AND tx.taxonomy = %s', LP_COURSE_CATEGORY_TAX );
}

Expand Down Expand Up @@ -773,6 +779,40 @@ public function count_courses_of_author( int $author_id, array $status = [] ): L

return apply_filters( 'lp/user/course/query/filter/count-courses-of-author', $filter_course );
}

/**
* Get child categories of category and add to query OR
*
* @param array $term_ids
*
* @return array
* @throws Exception
* @version 1.0.0
* @since 4.2.6.5
*/
public function recursion_sub_categories( array $term_ids ): array {
$total_found = 0;
$term_ids_format = esc_sql( join( ',', $term_ids ) );
$filter_sub_category = new LP_Filter();
$filter_sub_category->collection = $this->tb_term_taxonomy;
$filter_sub_category->collection_alias = 'tx';
$filter_sub_category->field_count = 'tx.term_id';
$filter_sub_category->only_fields = [ 'term_id' ];
$filter_sub_category->where[] = "AND tx.parent IN ($term_ids_format)";
$query_sub_category = $this->execute( $filter_sub_category, $total_found );
$term_sub_ids = [];

if ( $total_found > 0 ) {
foreach ( $query_sub_category as $term_id ) {
$term_sub_ids[] = $term_id->term_id;
}

$term_sub_idss = $this->recursion_sub_categories( $term_sub_ids );
$term_sub_ids = array_merge( $term_sub_ids, $term_sub_idss );
}

return array_merge( $term_ids, $term_sub_ids );
}
}

LP_Course_DB::getInstance();
2 changes: 1 addition & 1 deletion inc/TemplateHooks/Instructor/ListInstructorsTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected function __construct() {
}

public function add_internal_scripts_to_head() {
if ( ! LP_Page_Controller::is_page_instructors() ) {
if ( ! class_exists( 'LP_Page_Controller' ) || ! LP_Page_Controller::is_page_instructors() ) {
return;
}

Expand Down

0 comments on commit 02f3dcf

Please # to comment.