| 1 |
<?php |
| 2 |
/** |
| 3 |
* Template hooks Archive Package. |
| 4 |
* |
| 5 |
* @since 4.2.3.2 |
| 6 |
* @version 1.0.4 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace LearnPress\TemplateHooks\Course; |
| 10 |
|
| 11 |
defined( 'ABSPATH' ) || exit; |
| 12 |
|
| 13 |
use Exception; |
| 14 |
use LearnPress\Helpers\Singleton; |
| 15 |
use LearnPress\Helpers\Template; |
| 16 |
use LearnPress\Models\Courses; |
| 17 |
use LearnPress\Models\ListCourseCategories; |
| 18 |
use LearnPress\Models\UserModel; |
| 19 |
use LP_Course_Filter; |
| 20 |
use LP_Debug; |
| 21 |
use LP_Request; |
| 22 |
use Throwable; |
| 23 |
|
| 24 |
class FilterCourseTemplate { |
| 25 |
use Singleton; |
| 26 |
|
| 27 |
public function init() { |
| 28 |
add_action( 'learn-press/filter-courses/layout', [ $this, 'sections' ] ); |
| 29 |
//add_action( 'wp_head', [ $this, 'add_internal_scripts_to_head' ] ); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Sections of template filter courses. |
| 34 |
* |
| 35 |
* @param array $data |
| 36 |
* |
| 37 |
* @return void |
| 38 |
* @uses self::html_category |
| 39 |
* |
| 40 |
* @since 4.2.3.2 |
| 41 |
* @version 1.0.1 |
| 42 |
*/ |
| 43 |
public function sections( array $data = [] ) { |
| 44 |
wp_enqueue_script( 'lp-course-filter' ); |
| 45 |
|
| 46 |
try { |
| 47 |
if ( ! isset( $data['fields'] ) ) { |
| 48 |
$data['fields'] = [ |
| 49 |
'search', |
| 50 |
'price', |
| 51 |
'category', |
| 52 |
'tag', |
| 53 |
'author', |
| 54 |
'level', |
| 55 |
'type', |
| 56 |
'btn_submit', |
| 57 |
'btn_reset', |
| 58 |
]; |
| 59 |
|
| 60 |
$data = apply_filters( 'learn-press/filter-courses/data', $data ); |
| 61 |
} elseif ( is_string( $data['fields'] ) ) { |
| 62 |
$data['fields'] = explode( ',', $data['fields'] ); |
| 63 |
} |
| 64 |
|
| 65 |
if ( ! is_array( $data['fields'] ) ) { |
| 66 |
throw new Exception( 'Fields must be array' ); |
| 67 |
} |
| 68 |
|
| 69 |
if ( isset( $data['fields']['btn_submit'] ) ) { |
| 70 |
$data['fields'][] = 'btn_submit'; |
| 71 |
} |
| 72 |
|
| 73 |
$sections = []; |
| 74 |
foreach ( $data['fields'] as $field ) { |
| 75 |
if ( is_callable( [ $this, 'html_' . $field ] ) ) { |
| 76 |
$sections[ $field ] = $this->{'html_' . $field}( $data ); |
| 77 |
} else { // For custom field. |
| 78 |
do_action_ref_array( |
| 79 |
'learn-press/filter-courses/sections/field/html', |
| 80 |
[ |
| 81 |
&$sections, |
| 82 |
$field, |
| 83 |
$data, |
| 84 |
] |
| 85 |
); |
| 86 |
} |
| 87 |
} |
| 88 |
// Add button Done for mobile if not has btn submit. |
| 89 |
if ( ! in_array( 'btn_submit', $data['fields'] ) ) { |
| 90 |
$sections['btn_done'] = $this->html_btn_done( $data ); |
| 91 |
} |
| 92 |
|
| 93 |
$class_wrapper_form = $data['class_wrapper_form'] ?? 'lp-form-course-filter'; |
| 94 |
|
| 95 |
$wrapper = apply_filters( |
| 96 |
'lp/filter-courses/sections/wrapper', |
| 97 |
[ |
| 98 |
'wrapper' => sprintf( |
| 99 |
'<form class="%s">', |
| 100 |
esc_attr( $class_wrapper_form ) |
| 101 |
), |
| 102 |
'sections' => Template::combine_components( $sections ), |
| 103 |
'close' => sprintf( |
| 104 |
'<div class="lp-form-course-filter__close">%s<i class="lp-icon-close"></i></div>', |
| 105 |
esc_html__( 'Close', 'learnpress' ) |
| 106 |
), |
| 107 |
'wrapper_end' => '</form>', |
| 108 |
], |
| 109 |
$data |
| 110 |
); |
| 111 |
|
| 112 |
echo Template::combine_components( $wrapper ); |
| 113 |
} catch ( Throwable $e ) { |
| 114 |
LP_Debug::error_log( $e ); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Get html item. |
| 120 |
* |
| 121 |
* @param string $title |
| 122 |
* @param string $content |
| 123 |
* |
| 124 |
* @return string |
| 125 |
*/ |
| 126 |
public function html_item( string $title = '', string $content = '' ): string { |
| 127 |
try { |
| 128 |
$title_html = sprintf( |
| 129 |
'<div class="lp-form-course-filter__title">%s</div>', |
| 130 |
$title |
| 131 |
); |
| 132 |
$content_html = sprintf( |
| 133 |
'<div class="lp-form-course-filter__content">%s</div>', |
| 134 |
$content |
| 135 |
); |
| 136 |
$sections = apply_filters( |
| 137 |
'lp/filter-courses/item/sections', |
| 138 |
[ |
| 139 |
'title' => $title_html, |
| 140 |
'content' => $content_html, |
| 141 |
], |
| 142 |
$title, |
| 143 |
$content |
| 144 |
); |
| 145 |
|
| 146 |
$wrapper = apply_filters( |
| 147 |
'lp/filter-courses/item/wrapper', |
| 148 |
[ |
| 149 |
'wrapper' => '<div class="lp-form-course-filter__item">', |
| 150 |
'content' => Template::combine_components( $sections ), |
| 151 |
'wrapper_end' => '</div>', |
| 152 |
] |
| 153 |
); |
| 154 |
|
| 155 |
$content = Template::combine_components( $wrapper ); |
| 156 |
} catch ( Throwable $e ) { |
| 157 |
LP_Debug::error_log( $e ); |
| 158 |
} |
| 159 |
|
| 160 |
return $content; |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Get html search. |
| 165 |
* |
| 166 |
* @param array $data |
| 167 |
* |
| 168 |
* @return string |
| 169 |
*/ |
| 170 |
public function html_search( array $data = [] ): string { |
| 171 |
$content = ''; |
| 172 |
|
| 173 |
try { |
| 174 |
$this->check_param_url_has_lang( $data ); |
| 175 |
$value = LP_Request::get_param( 'c_search' ); |
| 176 |
$value = isset( $data['params_url'] ) ? ( $data['params_url']['c_search'] ?? $value ) : $value; |
| 177 |
$content = sprintf( |
| 178 |
'<input type="text" name="c_search" placeholder="%s" value="%s" class="%s" data-search-suggest="%d">', |
| 179 |
esc_html__( 'Search Course', 'learnpress' ), |
| 180 |
esc_attr( $value ), |
| 181 |
esc_attr( 'lp-course-filter-search' ), |
| 182 |
esc_attr( $data['search_suggestion'] ?? 1 ) |
| 183 |
); |
| 184 |
$content .= '<span class="lp-loading-circle lp-loading-no-css hide"></span>'; |
| 185 |
|
| 186 |
$sections = [ |
| 187 |
'wrapper' => '<div class="lp-course-filter-search-field">', |
| 188 |
'content' => $content, |
| 189 |
'wrapper_end' => '</div>', |
| 190 |
'result' => '<div class="lp-course-filter-search-result"></div>', |
| 191 |
]; |
| 192 |
|
| 193 |
$content = Template::combine_components( $sections ); |
| 194 |
|
| 195 |
$content = $this->html_item( esc_html__( 'Search', 'learnpress' ), $content ); |
| 196 |
|
| 197 |
$wrapper = apply_filters( |
| 198 |
'lp/filter-courses/sections/search/wrapper', |
| 199 |
[ |
| 200 |
'wrapper' => '<div class="lp-course-filter-search">', |
| 201 |
'content' => $content, |
| 202 |
'wrapper_end' => '</div>', |
| 203 |
], |
| 204 |
$data |
| 205 |
); |
| 206 |
|
| 207 |
$content = Template::combine_components( $wrapper ); |
| 208 |
} catch ( Throwable $e ) { |
| 209 |
LP_Debug::error_log( $e ); |
| 210 |
} |
| 211 |
|
| 212 |
return $content; |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Get html price. |
| 217 |
* |
| 218 |
* @param array $data |
| 219 |
* |
| 220 |
* @return string |
| 221 |
* @since 4.2.3.2 |
| 222 |
* @version 1.0.3 |
| 223 |
*/ |
| 224 |
public function html_price( array $data = [] ): string { |
| 225 |
$content = ''; |
| 226 |
|
| 227 |
try { |
| 228 |
$this->check_param_url_has_lang( $data ); |
| 229 |
$params_url = $data['params_url'] ?? []; |
| 230 |
$data_selected = $params_url['sort_by'] ?? ''; |
| 231 |
$data_selected = explode( ',', $data_selected ); |
| 232 |
$hide_count_zero = $data['hide_count_zero'] ?? 1; |
| 233 |
|
| 234 |
// Get number courses free |
| 235 |
$filter_courses_free = new LP_Course_Filter(); |
| 236 |
$this->handle_filter_params_before_query( $filter_courses_free, $params_url ); |
| 237 |
// Not count include sort by price. |
| 238 |
$filter_courses_free->sort_by = []; |
| 239 |
$count_courses_free = Courses::count_course_free( $filter_courses_free ); |
| 240 |
|
| 241 |
// Get number courses has price |
| 242 |
$filter_courses_price = new LP_Course_Filter(); |
| 243 |
$this->handle_filter_params_before_query( $filter_courses_price, $params_url ); |
| 244 |
$filter_courses_price->query_count = true; |
| 245 |
$filter_courses_price->sort_by = [ 'on_paid' ]; |
| 246 |
$count_courses_paid = 0; |
| 247 |
Courses::get_courses( $filter_courses_price, $count_courses_paid ); |
| 248 |
|
| 249 |
$fields = apply_filters( |
| 250 |
'learn-press/filter-courses/price/fields', |
| 251 |
[ |
| 252 |
'free' => [ |
| 253 |
'label' => __( 'Free', 'learnpress' ), |
| 254 |
'count' => $count_courses_free, |
| 255 |
], |
| 256 |
'paid' => [ |
| 257 |
'label' => __( 'Paid', 'learnpress' ), |
| 258 |
'count' => $count_courses_paid, |
| 259 |
], |
| 260 |
] |
| 261 |
); |
| 262 |
|
| 263 |
foreach ( $fields as $key => $field ) { |
| 264 |
$value = "on_{$key}"; |
| 265 |
$disabled = $field['count'] > 0 ? '' : 'disabled'; |
| 266 |
if ( ! empty( $disabled ) && $hide_count_zero ) { |
| 267 |
continue; |
| 268 |
} |
| 269 |
$checked = in_array( $value, $data_selected ) && empty( $disabled ) ? 'checked' : ''; |
| 270 |
$input = sprintf( |
| 271 |
'<input name="sort_by" type="checkbox" value="%1$s" %2$s %3$s>', |
| 272 |
esc_attr( $value ), |
| 273 |
esc_attr( $checked ), |
| 274 |
esc_attr( $disabled ) |
| 275 |
); |
| 276 |
$label = sprintf( '<label for="">%s</label>', wp_kses_post( $field['label'] ) ); |
| 277 |
$count = sprintf( '<span class="count">%s</span>', esc_html( $field['count'] ) ); |
| 278 |
|
| 279 |
$sections = apply_filters( |
| 280 |
'lp/filter-courses/price/sections', |
| 281 |
[ |
| 282 |
'input' => $input, |
| 283 |
'label' => $label, |
| 284 |
'count' => $count, |
| 285 |
], |
| 286 |
$field, |
| 287 |
$data |
| 288 |
); |
| 289 |
|
| 290 |
$wrapper = [ |
| 291 |
'wrapper' => '<div class="lp-course-filter__field">', |
| 292 |
'content' => Template::combine_components( $sections ), |
| 293 |
'wrapper_end' => '</div>', |
| 294 |
]; |
| 295 |
|
| 296 |
$content .= Template::combine_components( $wrapper ); |
| 297 |
} |
| 298 |
|
| 299 |
$content = $this->html_item( esc_html__( 'Price', 'learnpress' ), $content ); |
| 300 |
} catch ( Throwable $e ) { |
| 301 |
error_log( __METHOD__ . ': ' . $e->getMessage() ); |
| 302 |
} |
| 303 |
|
| 304 |
return $content; |
| 305 |
} |
| 306 |
|
| 307 |
/** |
| 308 |
* Get html course list categories. |
| 309 |
* |
| 310 |
* @param array $data |
| 311 |
* |
| 312 |
* @return string |
| 313 |
* @since 4.2.3.2 |
| 314 |
* @version 1.0.4 |
| 315 |
*/ |
| 316 |
public function html_category( array $data = [] ): string { |
| 317 |
$content = ''; |
| 318 |
|
| 319 |
try { |
| 320 |
$this->check_param_url_has_lang( $data ); |
| 321 |
$params_url = $data['params_url'] ?? []; |
| 322 |
$data_selected = $params_url['term_id'] ?? ''; |
| 323 |
$data_selected = explode( ',', $data_selected ); |
| 324 |
$data['data_selected'] = $data_selected; |
| 325 |
$parent_cat_id = 0; |
| 326 |
if ( isset( $params_url['page_term_id_current'] ) ) { |
| 327 |
$category_current_id = $params_url['page_term_id_current']; |
| 328 |
$category_current = get_term_by( 'id', $category_current_id, LP_COURSE_CATEGORY_TAX ); |
| 329 |
|
| 330 |
if ( ! empty( $category_current ) ) { |
| 331 |
$parent_cat_id = $category_current_id; |
| 332 |
$content .= $this->html_field_category( $category_current->term_id, $category_current->name, $data ); |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
// For subcategories. |
| 337 |
ob_start(); |
| 338 |
$data['level_current'] = 0; |
| 339 |
$data['parent_term_id'] = $parent_cat_id; |
| 340 |
$this->html_struct_categories( $data ); |
| 341 |
$content .= ob_get_clean(); |
| 342 |
|
| 343 |
$content = $this->html_item( esc_html__( 'Categories', 'learnpress' ), $content ); |
| 344 |
|
| 345 |
$wrapper = apply_filters( |
| 346 |
'lp/filter-courses/sections/category/wrapper', |
| 347 |
[ |
| 348 |
'wrapper' => '<div class="lp-course-filter-category">', |
| 349 |
'content' => $content, |
| 350 |
'wrapper_end' => '</div>', |
| 351 |
], |
| 352 |
$data |
| 353 |
); |
| 354 |
|
| 355 |
$content = Template::combine_components( $wrapper ); |
| 356 |
} catch ( Throwable $e ) { |
| 357 |
error_log( __METHOD__ . ': ' . $e->getMessage() ); |
| 358 |
} |
| 359 |
|
| 360 |
return $content; |
| 361 |
} |
| 362 |
|
| 363 |
/** |
| 364 |
* Get list categories course. |
| 365 |
* |
| 366 |
* @param array $args |
| 367 |
* |
| 368 |
* @return void |
| 369 |
* @since 4.2.6.5 |
| 370 |
* @version 1.0.0 |
| 371 |
*/ |
| 372 |
public function html_struct_categories( array $args = [] ) { |
| 373 |
$level_current = $args['level_current'] ?? 0; |
| 374 |
$number_level_category = $args['number_level_category'] ?? 2; |
| 375 |
$parent_term_id = $args['parent_term_id'] ?? 0; |
| 376 |
|
| 377 |
if ( $level_current >= $number_level_category ) { |
| 378 |
return; |
| 379 |
} |
| 380 |
|
| 381 |
$terms = ListCourseCategories::get_all_categories_id_name( [ 'parent' => $parent_term_id ] ); |
| 382 |
if ( empty( $terms ) ) { |
| 383 |
return; |
| 384 |
} |
| 385 |
|
| 386 |
$class_wrapper = 'lp-cate-parent'; |
| 387 |
if ( $level_current > 0 ) { |
| 388 |
$class_wrapper = 'lp-cate-child'; |
| 389 |
} |
| 390 |
|
| 391 |
echo sprintf( '<div class="%s">', esc_attr( $class_wrapper ) ); |
| 392 |
foreach ( $terms as $term_id => $term_name ) { |
| 393 |
echo sprintf( '<div class="lp-cat-%s">', esc_attr( $term_id ) ); |
| 394 |
echo $this->html_field_category( $term_id, $term_name, $args ); |
| 395 |
|
| 396 |
$args['level_current'] = $level_current + 1; |
| 397 |
$args['parent_term_id'] = $term_id; |
| 398 |
$this->html_struct_categories( $args ); |
| 399 |
echo '</div>'; |
| 400 |
} |
| 401 |
echo '</div>'; |
| 402 |
} |
| 403 |
|
| 404 |
/** |
| 405 |
* Return string html a field category. |
| 406 |
* |
| 407 |
* @param int $category_id |
| 408 |
* @param string $category_name |
| 409 |
* @param array $args |
| 410 |
* |
| 411 |
* @return false|string |
| 412 |
* @since 4.2.6.5 |
| 413 |
* @version 1.0.0 |
| 414 |
*/ |
| 415 |
public function html_field_category( int $category_id, string $category_name, array $args = [] ) { |
| 416 |
$count_courses = 0; |
| 417 |
$filter = new LP_Course_Filter(); |
| 418 |
$filter->query_count = true; |
| 419 |
$filter->only_fields = [ 'DISTINCT(ID)' ]; |
| 420 |
$this->handle_filter_params_before_query( $filter, $args['params_url'] ?? [] ); |
| 421 |
$filter->term_ids = [ $category_id ]; |
| 422 |
//$filter->debug_string_query = true; |
| 423 |
Courses::get_courses( $filter, $count_courses ); |
| 424 |
|
| 425 |
$disabled = $count_courses > 0 ? '' : 'disabled'; |
| 426 |
if ( ! empty( $disabled ) && ( $args['hide_count_zero'] ?? 1 ) ) { |
| 427 |
return ''; |
| 428 |
} |
| 429 |
|
| 430 |
$checked = in_array( $category_id, $args['data_selected'] ?? [] ) && empty( $disabled ) ? 'checked' : ''; |
| 431 |
$input = sprintf( |
| 432 |
'<input name="term_id" type="checkbox" value="%s" %s %s>', |
| 433 |
esc_attr( $category_id ), |
| 434 |
esc_attr( $checked ), |
| 435 |
esc_attr( $disabled ) |
| 436 |
); |
| 437 |
$label = sprintf( '<label for="">%s</label>', wp_kses_post( $category_name ) ); |
| 438 |
$count = sprintf( '<span class="count">%s</span>', esc_html( $count_courses ) ); |
| 439 |
|
| 440 |
$sections = apply_filters( |
| 441 |
'lp/filter-courses/course-category/sections', |
| 442 |
[ |
| 443 |
'start' => '<div class="lp-course-filter__field">', |
| 444 |
'input' => $input, |
| 445 |
'label' => $label, |
| 446 |
'count' => $count, |
| 447 |
'end' => '</div>', |
| 448 |
], |
| 449 |
$category_id, |
| 450 |
$category_name, |
| 451 |
$args |
| 452 |
); |
| 453 |
|
| 454 |
return Template::combine_components( $sections ); |
| 455 |
} |
| 456 |
|
| 457 |
/** |
| 458 |
* Get html course tag. |
| 459 |
* |
| 460 |
* @param array $data |
| 461 |
* |
| 462 |
* @return string |
| 463 |
* @since 4.2.3.2 |
| 464 |
* @version 1.0.3 |
| 465 |
*/ |
| 466 |
public function html_tag( array $data = [] ): string { |
| 467 |
$content = ''; |
| 468 |
|
| 469 |
try { |
| 470 |
$this->check_param_url_has_lang( $data ); |
| 471 |
$params_url = $data['params_url'] ?? []; |
| 472 |
$data_selected = $params_url['tag_id'] ?? ''; |
| 473 |
$data_selected = explode( ',', $data_selected ); |
| 474 |
$hide_count_zero = $data['hide_count_zero'] ?? 1; |
| 475 |
// Check has in tag page. |
| 476 |
if ( isset( $params_url['page_tag_id_current'] ) && |
| 477 |
empty( $params_url['tag_id'] ) ) { |
| 478 |
$data_selected[] = $params_url['page_tag_id_current']; |
| 479 |
} |
| 480 |
|
| 481 |
$terms = get_terms( |
| 482 |
[ |
| 483 |
'taxonomy' => LP_COURSE_TAXONOMY_TAG, |
| 484 |
'hide_empty' => true, |
| 485 |
'count' => false, |
| 486 |
] |
| 487 |
); |
| 488 |
|
| 489 |
if ( empty( $terms ) ) { |
| 490 |
return $content; |
| 491 |
} |
| 492 |
|
| 493 |
foreach ( $terms as $term ) { |
| 494 |
$value = $term->term_id; |
| 495 |
$filter = new LP_Course_Filter(); |
| 496 |
$filter->query_count = true; |
| 497 |
$this->handle_filter_params_before_query( $filter, $params_url ); |
| 498 |
$filter->tag_ids = [ $value ]; |
| 499 |
|
| 500 |
$count_courses = 0; |
| 501 |
Courses::get_courses( $filter, $count_courses ); |
| 502 |
$disabled = $count_courses > 0 ? '' : 'disabled'; |
| 503 |
if ( ! empty( $disabled ) && $hide_count_zero ) { |
| 504 |
continue; |
| 505 |
} |
| 506 |
$checked = in_array( $value, $data_selected ) && empty( $disabled ) ? 'checked' : ''; |
| 507 |
$input = sprintf( |
| 508 |
'<input name="tag_id" type="checkbox" value="%s" %s %s>', |
| 509 |
esc_attr( $value ), |
| 510 |
esc_attr( $checked ), |
| 511 |
esc_attr( $disabled ) |
| 512 |
); |
| 513 |
$label = sprintf( '<label for="">%s</label>', wp_kses_post( $term->name ) ); |
| 514 |
$count = sprintf( '<span class="count">%s</span>', esc_html( $count_courses ) ); |
| 515 |
|
| 516 |
$sections = apply_filters( |
| 517 |
'lp/filter-courses/course-tag/sections', |
| 518 |
[ |
| 519 |
'input' => $input, |
| 520 |
'label' => $label, |
| 521 |
'count' => $count, |
| 522 |
], |
| 523 |
$term, |
| 524 |
$data |
| 525 |
); |
| 526 |
|
| 527 |
$wrapper = [ |
| 528 |
'wrapper' => '<div class="lp-course-filter__field">', |
| 529 |
'content' => Template::combine_components( $sections ), |
| 530 |
'wrapper_end' => '</div>', |
| 531 |
]; |
| 532 |
|
| 533 |
$content .= Template::combine_components( $wrapper ); |
| 534 |
} |
| 535 |
|
| 536 |
$content = $this->html_item( esc_html__( 'Tags', 'learnpress' ), $content ); |
| 537 |
} catch ( Throwable $e ) { |
| 538 |
error_log( __METHOD__ . ': ' . $e->getMessage() ); |
| 539 |
} |
| 540 |
|
| 541 |
return $content; |
| 542 |
} |
| 543 |
|
| 544 |
/** |
| 545 |
* Get html course tag. |
| 546 |
* |
| 547 |
* @param array $data |
| 548 |
* |
| 549 |
* @return string |
| 550 |
* @since 4.2.3.2 |
| 551 |
* @version 1.0.4 |
| 552 |
*/ |
| 553 |
public function html_author( array $data = [] ): string { |
| 554 |
$content = ''; |
| 555 |
|
| 556 |
try { |
| 557 |
$this->check_param_url_has_lang( $data ); |
| 558 |
$params_url = $data['params_url'] ?? []; |
| 559 |
$data_selected = $params_url['c_authors'] ?? ''; |
| 560 |
$data_selected = explode( ',', $data_selected ); |
| 561 |
$hide_count_zero = $data['hide_count_zero'] ?? 1; |
| 562 |
$instructors = get_users( |
| 563 |
array( |
| 564 |
'role__in' => [ LP_TEACHER_ROLE, ADMIN_ROLE ], |
| 565 |
'fields' => array( 'ID' ), |
| 566 |
) |
| 567 |
); |
| 568 |
|
| 569 |
foreach ( $instructors as $instructorObj ) { |
| 570 |
$userModel = UserModel::find( $instructorObj->ID, true ); |
| 571 |
if ( ! $userModel instanceof UserModel ) { |
| 572 |
continue; |
| 573 |
} |
| 574 |
|
| 575 |
$instructor_id = $userModel->get_id(); |
| 576 |
$total_course_of_instructor = 0; |
| 577 |
|
| 578 |
$filter = new LP_Course_Filter(); |
| 579 |
$filter->query_count = true; |
| 580 |
$filter->only_fields = [ 'DISTINCT(ID)' ]; |
| 581 |
$this->handle_filter_params_before_query( $filter, $params_url ); |
| 582 |
$filter->post_authors = [ $instructor_id ]; |
| 583 |
Courses::get_courses( $filter, $total_course_of_instructor ); |
| 584 |
|
| 585 |
$value = $instructor_id; |
| 586 |
$disabled = $total_course_of_instructor > 0 ? '' : 'disabled'; |
| 587 |
if ( ! empty( $disabled ) && $hide_count_zero ) { |
| 588 |
continue; |
| 589 |
} |
| 590 |
$checked = in_array( $value, $data_selected ) && empty( $disabled ) ? 'checked' : ''; |
| 591 |
$input = sprintf( |
| 592 |
'<input name="c_authors" type="checkbox" value="%s" %s %s>', |
| 593 |
esc_attr( $value ), |
| 594 |
esc_attr( $checked ), |
| 595 |
esc_attr( $disabled ) |
| 596 |
); |
| 597 |
$label = sprintf( '<label for="">%s</label>', esc_html( $userModel->get_display_name() ) ); |
| 598 |
$count = sprintf( '<span class="count">%s</span>', esc_html( $total_course_of_instructor ) ); |
| 599 |
|
| 600 |
$sections = apply_filters( |
| 601 |
'lp/filter-courses/author/sections', |
| 602 |
[ |
| 603 |
'input' => $input, |
| 604 |
'label' => $label, |
| 605 |
'count' => $count, |
| 606 |
], |
| 607 |
$userModel, |
| 608 |
$total_course_of_instructor, |
| 609 |
$data |
| 610 |
); |
| 611 |
|
| 612 |
$wrapper = [ |
| 613 |
'wrapper' => '<div class="lp-course-filter__field">', |
| 614 |
'content' => Template::combine_components( $sections ), |
| 615 |
'wrapper_end' => '</div>', |
| 616 |
]; |
| 617 |
|
| 618 |
$content .= Template::combine_components( $wrapper ); |
| 619 |
} |
| 620 |
|
| 621 |
$content = $this->html_item( esc_html__( 'Author', 'learnpress' ), $content ); |
| 622 |
} catch ( Throwable $e ) { |
| 623 |
LP_Debug::error_log( $e ); |
| 624 |
} |
| 625 |
|
| 626 |
return $content; |
| 627 |
} |
| 628 |
|
| 629 |
/** |
| 630 |
* Get html course tag. |
| 631 |
* |
| 632 |
* @param array $data |
| 633 |
* |
| 634 |
* @return string |
| 635 |
* @since 4.2.3.2 |
| 636 |
* @version 1.0.4 |
| 637 |
*/ |
| 638 |
public function html_level( array $data = [] ): string { |
| 639 |
$content = ''; |
| 640 |
|
| 641 |
try { |
| 642 |
$this->check_param_url_has_lang( $data ); |
| 643 |
$params_url = $data['params_url'] ?? []; |
| 644 |
$data_selected = $params_url['c_level'] ?? ''; |
| 645 |
$data_selected = explode( ',', $data_selected ); |
| 646 |
$fields = lp_course_level(); |
| 647 |
$hide_count_zero = $data['hide_count_zero'] ?? 1; |
| 648 |
|
| 649 |
foreach ( $fields as $key => $field ) { |
| 650 |
$value = $key; |
| 651 |
if ( empty( $key ) ) { |
| 652 |
$value = 'all'; |
| 653 |
} |
| 654 |
|
| 655 |
$filter = new LP_Course_Filter(); |
| 656 |
$this->handle_filter_params_before_query( $filter, $params_url ); |
| 657 |
$filter->only_fields = [ 'DISTINCT(ID)' ]; |
| 658 |
$filter->query_count = true; |
| 659 |
$filter->levels = [ $key ]; |
| 660 |
if ( $key === 'all' ) { |
| 661 |
$filter->levels[] = ''; |
| 662 |
} |
| 663 |
|
| 664 |
$total_courses = 0; |
| 665 |
Courses::get_courses( $filter, $total_courses ); |
| 666 |
|
| 667 |
$disabled = $total_courses > 0 ? '' : 'disabled'; |
| 668 |
if ( ! empty( $disabled ) && $hide_count_zero ) { |
| 669 |
continue; |
| 670 |
} |
| 671 |
$checked = in_array( $value, $data_selected ) && empty( $disabled ) ? 'checked' : ''; |
| 672 |
$input = sprintf( |
| 673 |
'<input name="c_level" type="checkbox" value="%1$s" %2$s %3$s>', |
| 674 |
esc_attr( $value ), |
| 675 |
esc_attr( $checked ), |
| 676 |
esc_attr( $disabled ) |
| 677 |
); |
| 678 |
$label = sprintf( '<label for="">%s</label>', esc_html( $field ) ); |
| 679 |
$count = sprintf( '<span class="count">%s</span>', esc_html( $total_courses ) ); |
| 680 |
|
| 681 |
$sections = apply_filters( |
| 682 |
'lp/filter-courses/levels/sections', |
| 683 |
[ |
| 684 |
'input' => $input, |
| 685 |
'label' => $label, |
| 686 |
'count' => $count, |
| 687 |
], |
| 688 |
$field, |
| 689 |
$value, |
| 690 |
$data |
| 691 |
); |
| 692 |
|
| 693 |
$wrapper = [ |
| 694 |
'wrapper' => '<div class="lp-course-filter__field">', |
| 695 |
'content' => Template::combine_components( $sections ), |
| 696 |
'wrapper_end' => '</div>', |
| 697 |
]; |
| 698 |
|
| 699 |
$content .= Template::combine_components( $wrapper ); |
| 700 |
} |
| 701 |
|
| 702 |
$content = $this->html_item( esc_html__( 'Levels', 'learnpress' ), $content ); |
| 703 |
} catch ( Throwable $e ) { |
| 704 |
error_log( __METHOD__ . ': ' . $e->getMessage() ); |
| 705 |
} |
| 706 |
|
| 707 |
return $content; |
| 708 |
} |
| 709 |
|
| 710 |
/** |
| 711 |
* Get HTML fields type (online/offline) |
| 712 |
* |
| 713 |
* @param array $data |
| 714 |
* |
| 715 |
* @return string |
| 716 |
* @since 4.2.8.2 |
| 717 |
* @version 1.0.0 |
| 718 |
*/ |
| 719 |
public function html_type( array $data = [] ): string { |
| 720 |
$content = ''; |
| 721 |
try { |
| 722 |
$this->check_param_url_has_lang( $data ); |
| 723 |
$params_url = $data['params_url'] ?? []; |
| 724 |
$data_selected = $params_url['c_type'] ?? ''; |
| 725 |
$data_selected = explode( ',', $data_selected ); |
| 726 |
$filter_types = apply_filters( |
| 727 |
'learn-press/filter-courses/type/fields', |
| 728 |
array( |
| 729 |
'online' => __( 'Online', 'learnpress' ), |
| 730 |
'offline' => __( 'Offline', 'learnpress' ), |
| 731 |
) |
| 732 |
); |
| 733 |
foreach ( $filter_types as $key => $type ) { |
| 734 |
$checked = in_array( $key, $data_selected ) ? 'checked' : ''; |
| 735 |
$input = sprintf( |
| 736 |
'<input name="c_type" type="checkbox" value="%s" %s>', |
| 737 |
esc_attr( $key ), |
| 738 |
esc_attr( $checked ) |
| 739 |
); |
| 740 |
$label = sprintf( '<label for="">%s</label>', esc_html( $type ) ); |
| 741 |
$sections = apply_filters( |
| 742 |
'lp/filter-courses/type/sections', |
| 743 |
[ |
| 744 |
'input' => $input, |
| 745 |
'label' => $label, |
| 746 |
], |
| 747 |
$type, |
| 748 |
$key, |
| 749 |
$data |
| 750 |
); |
| 751 |
|
| 752 |
$wrapper = [ |
| 753 |
'wrapper' => '<div class="lp-course-filter__field">', |
| 754 |
'content' => Template::combine_components( $sections ), |
| 755 |
'wrapper_end' => '</div>', |
| 756 |
]; |
| 757 |
|
| 758 |
$content .= Template::combine_components( $wrapper ); |
| 759 |
} |
| 760 |
|
| 761 |
$content = $this->html_item( esc_html__( 'Type', 'learnpress' ), $content ); |
| 762 |
} catch ( Throwable $e ) { |
| 763 |
LP_Debug::error_log( $e ); |
| 764 |
} |
| 765 |
|
| 766 |
return $content; |
| 767 |
} |
| 768 |
|
| 769 |
/** |
| 770 |
* Get html button submit filter. |
| 771 |
* |
| 772 |
* @param array $data |
| 773 |
* |
| 774 |
* @return string |
| 775 |
*/ |
| 776 |
public function html_btn_submit( array $data = [] ): string { |
| 777 |
return sprintf( |
| 778 |
'<button type="submit" class="course-filter-submit">%s</button>', |
| 779 |
esc_html__( 'Apply', 'learnpress' ) |
| 780 |
); |
| 781 |
} |
| 782 |
|
| 783 |
/** |
| 784 |
* Get html button Done - for mobile when not show btn submit. |
| 785 |
* |
| 786 |
* @param array $data |
| 787 |
* |
| 788 |
* @return string |
| 789 |
*/ |
| 790 |
public function html_btn_done( array $data = [] ): string { |
| 791 |
return sprintf( |
| 792 |
'<button type="submit" class="course-filter-submit lp-btn-done lp-hidden">%s</button>', |
| 793 |
esc_html__( 'Done', 'learnpress' ) |
| 794 |
); |
| 795 |
} |
| 796 |
|
| 797 |
/** |
| 798 |
* Get html button reset filter. |
| 799 |
* |
| 800 |
* @param array $data |
| 801 |
* |
| 802 |
* @return string |
| 803 |
*/ |
| 804 |
public function html_btn_reset( array $data = [] ): string { |
| 805 |
return sprintf( |
| 806 |
'<button class="course-filter-reset">%s</button>', |
| 807 |
esc_html__( 'Reset', 'learnpress' ) |
| 808 |
); |
| 809 |
} |
| 810 |
|
| 811 |
/** |
| 812 |
* Get html button reset filter. |
| 813 |
* |
| 814 |
* @param array $data |
| 815 |
* |
| 816 |
* @return string |
| 817 |
* @since 4.2.7.6 |
| 818 |
* @version 1.0.0 |
| 819 |
*/ |
| 820 |
public function html_btn_filter_mobile( array $data = [] ): string { |
| 821 |
$count = $data['count_fields_selected'] ?? ''; |
| 822 |
if ( $count === '(0)' ) { |
| 823 |
$count = ''; |
| 824 |
} |
| 825 |
|
| 826 |
$section = [ |
| 827 |
'wrapper' => '<div class="course-filter-btn-mobile">', |
| 828 |
'icon' => '<span class="lp-icon lp-icon-filter"></span>', |
| 829 |
'count_fields_selected' => sprintf( |
| 830 |
'<span class="course-filter-count-fields-selected">%s</span>', |
| 831 |
esc_html( $count ) |
| 832 |
), |
| 833 |
'wrapper_end' => '</div>', |
| 834 |
]; |
| 835 |
|
| 836 |
return Template::combine_components( $section ); |
| 837 |
} |
| 838 |
|
| 839 |
/** |
| 840 |
* Set params from url for filter. |
| 841 |
* |
| 842 |
* @param LP_Course_Filter $filter |
| 843 |
* @param array $params_url |
| 844 |
* |
| 845 |
* @return void |
| 846 |
*/ |
| 847 |
public function handle_filter_params_before_query( LP_Course_Filter &$filter, array $params_url = [] ) { |
| 848 |
Courses::handle_params_for_query_courses( $filter, $params_url ); |
| 849 |
|
| 850 |
// Check has in category page. |
| 851 |
if ( isset( $params_url['page_term_id_current'] ) && |
| 852 |
empty( $params_url['term_id'] ) ) { |
| 853 |
$filter->term_ids[] = $params_url['page_term_id_current']; |
| 854 |
} elseif ( isset( $params_url['page_tag_id_current'] ) |
| 855 |
&& empty( $params_url['tag_id'] ) ) { |
| 856 |
$filter->tag_ids[] = $params_url['page_tag_id_current']; |
| 857 |
} |
| 858 |
} |
| 859 |
|
| 860 |
/** |
| 861 |
* Check param url has lang for multiple lang. |
| 862 |
* |
| 863 |
* @param array $data |
| 864 |
* |
| 865 |
* @return void |
| 866 |
* @since 4.2.5.7 |
| 867 |
* @version 1.0.0 |
| 868 |
*/ |
| 869 |
public function check_param_url_has_lang( array $data = [] ) { |
| 870 |
$params_url = $data['params_url'] ?? []; |
| 871 |
if ( isset( $params_url['lang'] ) ) { |
| 872 |
$_REQUEST['lang'] = sanitize_text_field( $params_url['lang'] ); |
| 873 |
} |
| 874 |
} |
| 875 |
} |
| 876 |
|