| @@ -6,8 +6,12 @@ | ||
| 6 | 6 | * @package LearnPress/Classes |
| 7 | 7 | * @version 4.0.0 |
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | +use LearnPress\Models\CourseModel; | |
| 11 | +use LearnPress\TemplateHooks\Course\SingleCourseTemplate; | |
| 12 | +use LearnPress\TemplateHooks\Instructor\SingleInstructorTemplate; | |
| 13 | + | |
| 10 | 14 | defined( 'ABSPATH' ) || exit(); |
| 11 | 15 | |
| 12 | 16 | if ( ! function_exists( 'LP_Abstract_Course' ) ) { |
| 13 | 17 | |
| @@ -161,8 +165,14 @@ | ||
| 161 | 165 | if ( metadata_exists( 'post', $this->get_id(), '_lp_regular_price' ) ) { |
| 162 | 166 | $regular_price = get_post_meta( $id, '_lp_regular_price', true ); |
| 163 | 167 | } |
| 164 | 168 | |
| 169 | + $block_course_finished = get_post_meta( | |
| 170 | + $id, | |
| 171 | + '_lp_block_finished', | |
| 172 | + true | |
| 173 | + ); | |
| 174 | + | |
| 165 | 175 | $this->_set_data( |
| 166 | 176 | array( |
| 167 | 177 | 'status' => $post_object->post_status, |
| 168 | 178 | 'no_required_enroll' => get_post_meta( $id, '_lp_no_required_enroll', true ), |
| @@ -185,15 +195,12 @@ | ||
| 185 | 195 | $id, |
| 186 | 196 | '_lp_block_expire_duration', |
| 187 | 197 | true |
| 188 | 198 | ), |
| 189 | - 'block_course_finished' => get_post_meta( | |
| 190 | - $id, | |
| 191 | - '_lp_block_finished', | |
| 192 | - true | |
| 193 | - ), | |
| 199 | + 'block_course_finished' => $block_course_finished ? $block_course_finished : 'yes', | |
| 194 | 200 | 'allow_repurchase' => get_post_meta( $id, '_lp_allow_course_repurchase', true ), |
| 195 | 201 | 'allow_repurchase_course_option' => get_post_meta( $id, '_lp_course_repurchase_option', true ), |
| 202 | + 'excerpt' => $post_object->post_excerpt, | |
| 196 | 203 | ) |
| 197 | 204 | ); |
| 198 | 205 | } |
| 199 | 206 | |
| @@ -199,9 +206,9 @@ | ||
| 199 | 206 | |
| 200 | 207 | /** |
| 201 | 208 | * Load course curriculum. |
| 202 | 209 | * |
| 203 | - * @depecated 4.1.6.9 | |
| 210 | + * @deprecated 4.1.6.9 | |
| 204 | 211 | */ |
| 205 | 212 | public function load_curriculum() { |
| 206 | 213 | _deprecated_function( __FUNCTION__, '4.1.6.9' ); |
| 207 | 214 | /*$item_ids = array(); |
| @@ -297,13 +304,9 @@ | ||
| 297 | 304 | $url = get_the_post_thumbnail_url( $parent_id, $size ); |
| 298 | 305 | } |
| 299 | 306 | |
| 300 | 307 | if ( ! $url ) { |
| 301 | - if ( 'course_thumbnail' == $size ) { | |
| 302 | - $url = LP()->image( 'no-image.png' ); | |
| 303 | - } else { | |
| 304 | - $url = LP()->image( 'placeholder-500x300' ); | |
| 305 | - } | |
| 308 | + $url = LearnPress::instance()->image( 'no-image.png' ); | |
| 306 | 309 | } |
| 307 | 310 | |
| 308 | 311 | return apply_filters( 'learn-press/course-thumbnail-url', $url, $this->get_id(), $size ); |
| 309 | 312 | } |
| @@ -331,56 +334,39 @@ | ||
| 331 | 334 | return 'publish' === get_post_status( $this->get_id() ); |
| 332 | 335 | } |
| 333 | 336 | |
| 334 | 337 | /** |
| 335 | - * Check if this course is required enroll or not. | |
| 338 | + * Check this course is required enroll. | |
| 336 | 339 | * |
| 337 | - * @param mixed | |
| 338 | - * | |
| 339 | 340 | * @return bool |
| 340 | - * @depecated 4.0.0 | |
| 341 | 341 | */ |
| 342 | - public function is_required_enroll() { | |
| 342 | + public function is_required_enroll(): bool { | |
| 343 | + return ! $this->is_no_required_enroll(); | |
| 344 | + } | |
| 343 | 345 | |
| 344 | - // $return = $this->get_data( 'required_enroll' ) == 'yes'; | |
| 345 | - // @deprecated | |
| 346 | - // $return = apply_filters( 'learn_press_course_required_enroll', $return, $this ); | |
| 346 | + /** | |
| 347 | + * @deprecated 4.2.0 | |
| 348 | + */ | |
| 349 | + public function is_require_enrollment() { | |
| 350 | + _deprecated_function( __METHOD__, '4.2.0' ); | |
| 351 | + return ! $this->is_no_required_enroll(); | |
| 352 | + } | |
| 347 | 353 | |
| 348 | - /** | |
| 349 | - * Since version 4.0.0 feature 'no require enroll' has deprecated | |
| 350 | - */ | |
| 351 | - $return = true; | |
| 352 | - | |
| 353 | - return apply_filters( 'learn-press/course-require-enrollment', $return, $this->get_id() ); | |
| 354 | - } | |
| 355 | 354 | /** |
| 356 | 355 | * Check if this course is required enroll or not. |
| 357 | 356 | * |
| 358 | - * @param mixed | |
| 359 | 357 | * @author hungkv |
| 360 | 358 | * @since 4.0.5 |
| 361 | 359 | * @return bool |
| 362 | - * @version 1.0.0 | |
| 360 | + * @version 1.0.1 | |
| 363 | 361 | */ |
| 364 | 362 | public function is_no_required_enroll(): bool { |
| 365 | - $return = false; | |
| 366 | - if ( $this->get_data( 'no_required_enroll', 'no' ) == 'yes' && ! is_user_logged_in() ) { | |
| 367 | - $return = true; | |
| 368 | - } | |
| 369 | - return apply_filters( 'learn-press/course/require-enrollment', $return, $this->get_id() ); | |
| 363 | + return $this->get_data( 'no_required_enroll', 'no' ) === 'yes' && ! is_user_logged_in(); | |
| 370 | 364 | } |
| 371 | 365 | |
| 372 | 366 | /** |
| 373 | - * @return mixed | |
| 374 | - * @deprecated | |
| 367 | + * @deprecated 4.1.6.9 | |
| 375 | 368 | */ |
| 376 | - public function is_require_enrollment() { | |
| 377 | - return $this->is_required_enroll(); | |
| 378 | - } | |
| 379 | - | |
| 380 | - /** | |
| 381 | - * @depecated 4.1.6.9 | |
| 382 | - */ | |
| 383 | 369 | /*public function get_item_types( $group = false ) { |
| 384 | 370 | $cache_key = $group ? 'course-item-group-types' : 'course-item-types'; |
| 385 | 371 | $items = LP_Object_Cache::get( 'course-' . $this->get_id(), "learn-press/{$cache_key}" ); |
| 386 | 372 | $items = false; |
| @@ -452,9 +438,9 @@ | ||
| 452 | 438 | /** |
| 453 | 439 | * Get item is viewing in single course. |
| 454 | 440 | * |
| 455 | 441 | * @return LP_Course_Item |
| 456 | - * @depecated 4.1.6.9 | |
| 442 | + * @deprecated 4.1.6.9 | |
| 457 | 443 | */ |
| 458 | 444 | public function get_viewing_item() { |
| 459 | 445 | _deprecated_function( __FUNCTION__, '4.1.6.9' ); |
| 460 | 446 | //return apply_filters( 'learn-press/single-course-viewing-item', $this->_viewing_item, $this->get_id() ); |
| @@ -464,9 +450,9 @@ | ||
| 464 | 450 | * Get total students fake. |
| 465 | 451 | * |
| 466 | 452 | * @return int |
| 467 | 453 | */ |
| 468 | - public function get_fake_students() : int { | |
| 454 | + public function get_fake_students(): int { | |
| 469 | 455 | return absint( $this->get_data( 'fake_students', 0 ) ); |
| 470 | 456 | } |
| 471 | 457 | |
| 472 | 458 | /** |
| @@ -488,56 +474,8 @@ | ||
| 488 | 474 | return apply_filters( 'learn-press/course/users-enrolled', $enrolled, $this ); |
| 489 | 475 | } |
| 490 | 476 | |
| 491 | 477 | /** |
| 492 | - * Output html for students enrolled counter | |
| 493 | - * | |
| 494 | - * @return string | |
| 495 | - * @editor tungnx | |
| 496 | - * @modify 4.1.3 | |
| 497 | - */ | |
| 498 | - /*public function get_students_html() { | |
| 499 | - $output = ''; | |
| 500 | - $count = $this->get_users_enrolled(); | |
| 501 | - | |
| 502 | - if ( $count ) { | |
| 503 | - $user = learn_press_get_current_user(); | |
| 504 | - | |
| 505 | - if ( $user->has_enrolled_course( $this->get_id() ) ) { | |
| 506 | - if ( $count == 1 ) { | |
| 507 | - $output .= esc_html__( 'You enrolled', 'learnpress' ); | |
| 508 | - } else { | |
| 509 | - $output .= sprintf( | |
| 510 | - _nx( | |
| 511 | - 'You and one student enrolled', | |
| 512 | - 'You and <span class="course-students-number">%1$s</span> students enrolled', | |
| 513 | - intval( $count - 1 ), | |
| 514 | - 'students-html', | |
| 515 | - 'learnpress' | |
| 516 | - ), | |
| 517 | - $count - 1 | |
| 518 | - ); | |
| 519 | - } | |
| 520 | - } else { | |
| 521 | - $output = sprintf( | |
| 522 | - _nx( | |
| 523 | - 'One student enrolled', | |
| 524 | - '<span class="course-students-number">%1$s</span> students enrolled', | |
| 525 | - $count, | |
| 526 | - 'students-html', | |
| 527 | - 'learnpress' | |
| 528 | - ), | |
| 529 | - $count | |
| 530 | - ); | |
| 531 | - } | |
| 532 | - } else { | |
| 533 | - $output = esc_html__( 'No student enrolled', 'learnpress' ); | |
| 534 | - } | |
| 535 | - | |
| 536 | - return apply_filters( 'learn-press/students-enrolled-html', $output, $this->get_id() ); | |
| 537 | - }*/ | |
| 538 | - | |
| 539 | - /** | |
| 540 | 478 | * @param string $field |
| 541 | 479 | * |
| 542 | 480 | * @return LP_User|mixed |
| 543 | 481 | */ |
| @@ -567,33 +505,43 @@ | ||
| 567 | 505 | return apply_filters( 'learn-press/course/instructor-name', $name, $this->get_id() ); |
| 568 | 506 | } |
| 569 | 507 | |
| 570 | 508 | /** |
| 509 | + * Get instructor html of course. | |
| 510 | + * | |
| 571 | 511 | * @param int|bool $with_avatar |
| 572 | 512 | * @param string $link_class |
| 573 | 513 | * |
| 574 | 514 | * @return string |
| 575 | 515 | */ |
| 576 | - public function get_instructor_html( $with_avatar = false, $link_class = '' ) { | |
| 577 | - $instructor = $this->get_instructor_name(); | |
| 516 | + public function get_instructor_html( $with_avatar = false, $link_class = '' ): string { | |
| 517 | + $html = ''; | |
| 578 | 518 | |
| 579 | - $html = sprintf( | |
| 580 | - '<a href="%s"%s>%s<span>%s</span></a>', | |
| 581 | - learn_press_user_profile_link( get_post_field( 'post_author', $this->get_id() ) ), | |
| 582 | - $link_class ? sprintf( 'class="%s"', $link_class ) : '', | |
| 583 | - $with_avatar ? get_avatar( | |
| 584 | - $this->get_instructor( 'id' ), | |
| 585 | - $with_avatar === true ? 48 : $with_avatar | |
| 586 | - ) : '', | |
| 587 | - $instructor | |
| 588 | - ); | |
| 519 | + try { | |
| 520 | + $instructor = $this->get_author(); | |
| 521 | + if ( ! $instructor ) { | |
| 522 | + return ''; | |
| 523 | + } | |
| 589 | 524 | |
| 590 | - return apply_filters( | |
| 591 | - 'learn_press_course_instructor_html', | |
| 592 | - $html, | |
| 593 | - get_post_field( 'post_author', $this->get_id() ), | |
| 594 | - $this->get_id() | |
| 595 | - ); | |
| 525 | + $singleInstructorTemplate = SingleInstructorTemplate::instance(); | |
| 526 | + | |
| 527 | + $html = apply_filters( | |
| 528 | + 'learn-press/course/instructor-html', | |
| 529 | + sprintf( | |
| 530 | + '<a href="%s"%s>%s %s</a>', | |
| 531 | + $instructor->get_url_instructor(), | |
| 532 | + $link_class ? sprintf( 'class="%s"', $link_class ) : '', | |
| 533 | + $with_avatar ? $instructor->get_profile_picture() : '', | |
| 534 | + $singleInstructorTemplate->html_display_name( $instructor ) | |
| 535 | + ), | |
| 536 | + $instructor, | |
| 537 | + $singleInstructorTemplate | |
| 538 | + ); | |
| 539 | + } catch ( Throwable $e ) { | |
| 540 | + error_log( $e->getMessage() ); | |
| 541 | + } | |
| 542 | + | |
| 543 | + return $html; | |
| 596 | 544 | } |
| 597 | 545 | |
| 598 | 546 | /** |
| 599 | 547 | * Check if a course is Free |
| @@ -613,9 +561,9 @@ | ||
| 613 | 561 | public function get_sale_price() { |
| 614 | 562 | $sale_price_value = $this->get_data( 'sale_price', '' ); |
| 615 | 563 | |
| 616 | 564 | if ( '' !== $sale_price_value ) { |
| 617 | - return floatval( $sale_price_value ); | |
| 565 | + return apply_filters( 'learn-press/course/sale-price', floatval( $sale_price_value ), $this->get_id() ); | |
| 618 | 566 | } |
| 619 | 567 | |
| 620 | 568 | return $sale_price_value; |
| 621 | 569 | } |
| @@ -637,11 +585,14 @@ | ||
| 637 | 585 | } |
| 638 | 586 | |
| 639 | 587 | // Check in days sale |
| 640 | 588 | if ( $has_sale_price && '' !== $start_date && '' !== $end_date ) { |
| 641 | - $now = strtotime( get_date_from_gmt( gmdate( 'Y-m-d H:i:s', time() ), 'Y-m-d H:i:s' ) ); | |
| 642 | - $end = strtotime( $end_date ); | |
| 643 | - $start = strtotime( $start_date ); | |
| 589 | + $nowObj = new LP_Datetime(); | |
| 590 | + // Compare via timezone WP | |
| 591 | + $nowStr = $nowObj->toSql( true ); | |
| 592 | + $now = strtotime( $nowStr ); | |
| 593 | + $end = strtotime( $end_date ); | |
| 594 | + $start = strtotime( $start_date ); | |
| 644 | 595 | |
| 645 | 596 | $has_sale_price = $now >= $start && $now <= $end; |
| 646 | 597 | } |
| 647 | 598 | |
| @@ -669,9 +620,9 @@ | ||
| 669 | 620 | */ |
| 670 | 621 | public function get_regular_price_html() { |
| 671 | 622 | $price = learn_press_format_price( $this->get_regular_price(), true ); |
| 672 | 623 | |
| 673 | - return apply_filters( 'learn-press/course/regular-price', $price, $this->get_id() ); | |
| 624 | + return apply_filters( 'learn-press/course/regular-price-html', $price, $this->get_id() ); | |
| 674 | 625 | } |
| 675 | 626 | |
| 676 | 627 | /** |
| 677 | 628 | * Get the price of course. |
| @@ -684,18 +635,20 @@ | ||
| 684 | 635 | |
| 685 | 636 | if ( false === $price ) { |
| 686 | 637 | if ( $this->has_sale_price() ) { |
| 687 | 638 | $price = $this->get_sale_price(); |
| 688 | - // Add key _lp_course_is_sale for query | |
| 689 | - update_post_meta( $this->get_id(), '_lp_course_is_sale', 1 ); | |
| 639 | + // Add key _lp_course_is_sale for query - Todo: Check performance, need write function get all courses, and set on Admin, on background | |
| 640 | + //update_post_meta( $this->get_id(), '_lp_course_is_sale', 1 ); | |
| 690 | 641 | } else { |
| 691 | 642 | // Delete key _lp_course_is_sale |
| 692 | - delete_post_meta( $this->get_id(), '_lp_course_is_sale' ); | |
| 643 | + //delete_post_meta( $this->get_id(), '_lp_course_is_sale' ); | |
| 693 | 644 | $price = $this->get_regular_price(); |
| 694 | 645 | } |
| 695 | 646 | |
| 696 | - // For case set sale by days range | |
| 697 | - update_post_meta( $this->get_id(), '_lp_price', $price ); | |
| 647 | + // Save price only on page Single Course | |
| 648 | + /*if ( LP_PAGE_SINGLE_COURSE === LP_Page_Controller::page_current() ) { | |
| 649 | + update_post_meta( $this->get_id(), '_lp_price', $price ); | |
| 650 | + }*/ | |
| 698 | 651 | |
| 699 | 652 | LP_Course_Cache::cache_load_first( 'set', $key_cache, $price ); |
| 700 | 653 | } |
| 701 | 654 | |
| @@ -706,12 +659,12 @@ | ||
| 706 | 659 | * Get html course price |
| 707 | 660 | * |
| 708 | 661 | * @author tungnx |
| 709 | 662 | * @since 4.1.5 |
| 710 | - * @version 1.0.0 | |
| 711 | - * @return mixed|void | |
| 663 | + * @version 1.0.2 | |
| 664 | + * @return string | |
| 712 | 665 | */ |
| 713 | - public function get_course_price_html() { | |
| 666 | + public function get_course_price_html(): string { | |
| 714 | 667 | $price_html = ''; |
| 715 | 668 | |
| 716 | 669 | if ( $this->is_free() ) { |
| 717 | 670 | if ( is_float( $this->get_sale_price() ) ) { |
| @@ -719,18 +672,28 @@ | ||
| 719 | 672 | } |
| 720 | 673 | |
| 721 | 674 | $price_html .= sprintf( '<span class="free">%s</span>', esc_html__( 'Free', 'learnpress' ) ); |
| 722 | 675 | $price_html = apply_filters( 'learn_press_course_price_html_free', $price_html, $this ); |
| 676 | + } elseif ( $this->get_data( 'no_required_enroll', 'no' ) === 'yes' ) { | |
| 677 | + $price_html .= ''; | |
| 723 | 678 | } else { |
| 724 | 679 | if ( $this->has_sale_price() ) { |
| 725 | 680 | $price_html .= sprintf( '<span class="origin-price">%s</span>', $this->get_regular_price_html() ); |
| 726 | 681 | } |
| 727 | 682 | |
| 728 | - $price_html .= sprintf( '<span class="price">%s</span>', learn_press_format_price( $this->get_price(), true ) ); | |
| 729 | - $price_html = apply_filters( 'learn_press_course_price_html', $price_html, $this->has_sale_price(), $this->get_id() ); | |
| 683 | + $price_html .= sprintf( '<span class="price">%s</span>', learn_press_format_price( $this->get_price(), true ) ); | |
| 684 | + $course = CourseModel::find( $this->get_id(), true ); | |
| 685 | + $singleCourseTemplate = SingleCourseTemplate::instance(); | |
| 686 | + $price_html = sprintf( | |
| 687 | + '%1$s %2$s %3$s', | |
| 688 | + $singleCourseTemplate->html_price_prefix( $course ), | |
| 689 | + $price_html, | |
| 690 | + $singleCourseTemplate->html_price_suffix( $course ) | |
| 691 | + ); | |
| 692 | + $price_html = apply_filters( 'learn_press_course_price_html', $price_html, $this->has_sale_price(), $this->get_id() ); | |
| 730 | 693 | } |
| 731 | 694 | |
| 732 | - return $price_html; | |
| 695 | + return sprintf( '<span class="course-item-price">%s</span>', $price_html ); | |
| 733 | 696 | } |
| 734 | 697 | |
| 735 | 698 | /** |
| 736 | 699 | * Get the price of course with html |
| @@ -858,20 +821,20 @@ | ||
| 858 | 821 | * |
| 859 | 822 | * @return mixed |
| 860 | 823 | */ |
| 861 | 824 | public function is_purchasable() { |
| 862 | - $is_purchasable = $this->exists() && $this->is_required_enroll() && get_post_status( $this->get_id() ) == 'publish'; | |
| 825 | + $is_purchasable = $this->exists() && ! $this->is_no_required_enroll() && get_post_status( $this->get_id() ) == 'publish'; | |
| 863 | 826 | |
| 864 | - // @deprecated | |
| 865 | - $is_purchasable = apply_filters( 'learn_press_item_is_purchasable', $is_purchasable, $this->get_id() ); | |
| 866 | - | |
| 867 | 827 | return apply_filters( 'learn-press/is-purchasable', $is_purchasable, $this->get_id() ); |
| 868 | 828 | } |
| 869 | 829 | |
| 870 | 830 | /** |
| 871 | - * Check if students have enrolled or purchased course is reached. | |
| 831 | + * Check if students have purchased course is reached. | |
| 832 | + * For case check course can purchase. | |
| 872 | 833 | * |
| 873 | 834 | * @return mixed |
| 835 | + * @since 3.0.0 | |
| 836 | + * @version 1.0.1 | |
| 874 | 837 | */ |
| 875 | 838 | public function is_in_stock() { |
| 876 | 839 | $in_stock = true; |
| 877 | 840 | $max_allowed = $this->get_max_students(); |
| @@ -883,8 +846,27 @@ | ||
| 883 | 846 | return apply_filters( 'learn-press/is-in-stock', $in_stock, $this->get_id() ); |
| 884 | 847 | } |
| 885 | 848 | |
| 886 | 849 | /** |
| 850 | + * Check max student can enroll. | |
| 851 | + * For case check course can enroll. | |
| 852 | + * | |
| 853 | + * @return mixed | |
| 854 | + * @since 4.2.5.7 | |
| 855 | + * @version 1.0.0 | |
| 856 | + */ | |
| 857 | + public function is_in_stock_enroll() { | |
| 858 | + $in_stock = true; | |
| 859 | + $max_allowed = $this->get_max_students(); | |
| 860 | + | |
| 861 | + if ( $max_allowed ) { | |
| 862 | + $in_stock = $max_allowed > $this->get_total_user_enrolled_or_purchased(); | |
| 863 | + } | |
| 864 | + | |
| 865 | + return apply_filters( 'learn-press/is-in-stock', $in_stock, $this->get_id() ); | |
| 866 | + } | |
| 867 | + | |
| 868 | + /** | |
| 887 | 869 | * Get max students can enroll to course. |
| 888 | 870 | * |
| 889 | 871 | * @return int |
| 890 | 872 | * |
| @@ -904,24 +886,16 @@ | ||
| 904 | 886 | * and add the fake value if both are set. |
| 905 | 887 | * |
| 906 | 888 | * @return int |
| 907 | 889 | * @editor tungnx |
| 890 | + * @version 1.0.1 | |
| 891 | + * @since 3.0.0 | |
| 908 | 892 | * @Todo: view and rewrite this function |
| 909 | 893 | */ |
| 910 | 894 | public function count_students(): int { |
| 911 | - $key_cache = "{$this->get_id()}/total-students"; | |
| 912 | - $total = LP_Course_Cache::instance()->get_cache( $key_cache ); | |
| 895 | + $total = $this->get_total_user_enrolled_or_purchased(); | |
| 896 | + $total += $this->get_fake_students(); | |
| 913 | 897 | |
| 914 | - if ( $total ) { | |
| 915 | - return $total; | |
| 916 | - } | |
| 917 | - | |
| 918 | - $lp_course_db = LP_Course_DB::getInstance(); | |
| 919 | - $total = $lp_course_db->get_total_user_enrolled( $this->get_id() ); | |
| 920 | - $total += $this->get_fake_students(); | |
| 921 | - | |
| 922 | - LP_Course_Cache::instance()->set_cache( $key_cache, $total, 6 * HOUR_IN_SECONDS ); | |
| 923 | - | |
| 924 | 898 | return $total; |
| 925 | 899 | } |
| 926 | 900 | |
| 927 | 901 | /** |
| @@ -926,32 +900,83 @@ | ||
| 926 | 900 | |
| 927 | 901 | /** |
| 928 | 902 | * Get total user enrolled |
| 929 | 903 | * |
| 904 | + * @since 4.1.4 | |
| 905 | + * @version 1.0.1 | |
| 930 | 906 | * @return int |
| 931 | 907 | */ |
| 932 | 908 | public function get_total_user_enrolled(): int { |
| 933 | - $lp_course_db = LP_Course_DB::getInstance(); | |
| 934 | - return $lp_course_db->get_total_user_enrolled( $this->get_id() ); | |
| 909 | + $total = 0; | |
| 910 | + $lp_course_cache = new LP_Course_Cache( true ); | |
| 911 | + | |
| 912 | + try { | |
| 913 | + $total = $lp_course_cache->get_total_students_enrolled( $this->get_id() ); | |
| 914 | + if ( false !== $total ) { | |
| 915 | + return $total; | |
| 916 | + } | |
| 917 | + | |
| 918 | + $lp_course_db = LP_Course_DB::getInstance(); | |
| 919 | + $total = $lp_course_db->get_total_user_enrolled( $this->get_id() ); | |
| 920 | + $lp_course_cache->set_total_students_enrolled( $this->get_id(), $total ); | |
| 921 | + } catch ( Throwable $e ) { | |
| 922 | + error_log( $e->getMessage() ); | |
| 923 | + } | |
| 924 | + | |
| 925 | + return $total; | |
| 935 | 926 | } |
| 936 | 927 | |
| 937 | 928 | /** |
| 938 | - * Get total user enrolled or finished | |
| 929 | + * Get total user enrolled include fake | |
| 930 | + * @since 4.2.2 | |
| 931 | + * @return int | |
| 932 | + */ | |
| 933 | + /*public function get_total_user_enrolled_include_fake() { | |
| 934 | + $total_user_enrolled = 0; | |
| 935 | + $key_cache = "{$this->get_id()}/total-students-include-fake"; | |
| 936 | + | |
| 937 | + try { | |
| 938 | + $total_user_enrolled = LP_Course_Cache::cache_load_first( 'get', $key_cache ); | |
| 939 | + | |
| 940 | + if ( false === $total_user_enrolled ) { | |
| 941 | + $lp_course_db = LP_Course_DB::getInstance(); | |
| 942 | + $total_user_enrolled = $lp_course_db->get_total_user_enrolled( $this->get_id() ); | |
| 943 | + $total_user_enrolled += $this->get_fake_students(); | |
| 944 | + | |
| 945 | + LP_Course_Cache::cache_load_first( 'set', $key_cache, $total_user_enrolled ); | |
| 946 | + } | |
| 947 | + } catch ( Throwable $e ) { | |
| 948 | + error_log( $e->getMessage() ); | |
| 949 | + } | |
| 950 | + | |
| 951 | + return $total_user_enrolled; | |
| 952 | + }*/ | |
| 953 | + | |
| 954 | + /** | |
| 955 | + * Get total user enrolled, purchased or finished | |
| 939 | 956 | * |
| 957 | + * @since 4.1.4 | |
| 958 | + * @version 1.0.1 | |
| 940 | 959 | * @return int |
| 941 | 960 | */ |
| 942 | 961 | public function get_total_user_enrolled_or_purchased(): int { |
| 943 | - $key_cache = "{$this->get_id()}/total-students-attended"; | |
| 944 | - $total_user_enrolled = LP_Course_Cache::cache_load_first( 'get', $key_cache ); | |
| 962 | + $total = 0; | |
| 963 | + $lp_course_cache = new LP_Course_Cache( true ); | |
| 945 | 964 | |
| 946 | - if ( false === $total_user_enrolled ) { | |
| 947 | - $lp_course_db = LP_Course_DB::getInstance(); | |
| 948 | - $total_user_enrolled = $lp_course_db->get_total_user_enrolled_or_purchased( $this->get_id() ); | |
| 965 | + try { | |
| 966 | + $total = $lp_course_cache->get_total_students_enrolled_or_purchased( $this->get_id() ); | |
| 967 | + if ( false !== $total ) { | |
| 968 | + return $total; | |
| 969 | + } | |
| 949 | 970 | |
| 950 | - LP_Course_Cache::cache_load_first( 'set', $key_cache, $total_user_enrolled ); | |
| 971 | + $lp_course_db = LP_Course_DB::getInstance(); | |
| 972 | + $total = $lp_course_db->get_total_user_enrolled_or_purchased( $this->get_id() ); | |
| 973 | + $lp_course_cache->set_total_students_enrolled_or_purchased( $this->get_id(), $total ); | |
| 974 | + } catch ( Throwable $e ) { | |
| 975 | + error_log( $e->getMessage() ); | |
| 951 | 976 | } |
| 952 | 977 | |
| 953 | - return $total_user_enrolled; | |
| 978 | + return $total; | |
| 954 | 979 | } |
| 955 | 980 | |
| 956 | 981 | /** |
| 957 | 982 | * @param string|array $statuses |
| @@ -1031,9 +1056,9 @@ | ||
| 1031 | 1056 | * @param string $context |
| 1032 | 1057 | * |
| 1033 | 1058 | * @return array|mixed|string |
| 1034 | 1059 | */ |
| 1035 | - public function get_passing_condition( $format = false, $context = '' ) { | |
| 1060 | + public function get_passing_condition( $format = false ) { | |
| 1036 | 1061 | $value = absint( $this->get_data( 'passing_condition' ) ); |
| 1037 | 1062 | |
| 1038 | 1063 | if ( $format ) { |
| 1039 | 1064 | $value = "{$value}%"; |
| @@ -1038,9 +1063,9 @@ | ||
| 1038 | 1063 | if ( $format ) { |
| 1039 | 1064 | $value = "{$value}%"; |
| 1040 | 1065 | } |
| 1041 | 1066 | |
| 1042 | - return 'edit' === $context ? $value : apply_filters( | |
| 1067 | + return apply_filters( | |
| 1043 | 1068 | 'learn-press/course-passing-condition', |
| 1044 | 1069 | $value, |
| 1045 | 1070 | $format, |
| 1046 | 1071 | $this->get_id() |
| @@ -1066,18 +1091,17 @@ | ||
| 1066 | 1091 | |
| 1067 | 1092 | $slug_prefixes = apply_filters( |
| 1068 | 1093 | 'learn-press/course/custom-item-prefixes', |
| 1069 | 1094 | array( |
| 1070 | - LP_QUIZ_CPT => sanitize_title_with_dashes( LP_Settings::instance()->get( 'quiz_slug', 'quizzes' ) ), | |
| 1071 | - LP_LESSON_CPT => sanitize_title_with_dashes( LP_Settings::instance()->get( 'lesson_slug', 'lessons' ) ), | |
| 1095 | + LP_QUIZ_CPT => sanitize_title_with_dashes( LP_Settings::get_option( 'quiz_slug', 'quizzes' ) ), | |
| 1096 | + LP_LESSON_CPT => sanitize_title_with_dashes( LP_Settings::get_option( 'lesson_slug', 'lessons' ) ), | |
| 1072 | 1097 | ), |
| 1073 | 1098 | $this->get_id() |
| 1074 | 1099 | ); |
| 1075 | 1100 | |
| 1076 | 1101 | $slug_prefix = trailingslashit( $slug_prefixes[ $item_type ] ?? '' ); |
| 1102 | + $item_link = trailingslashit( $course_permalink . $slug_prefix . $item_slug ); | |
| 1077 | 1103 | |
| 1078 | - $item_link = $course_permalink . $slug_prefix . $item_slug; | |
| 1079 | - | |
| 1080 | 1104 | return apply_filters( 'learn-press/course/item-link', $item_link, $item_id, $this ); |
| 1081 | 1105 | } |
| 1082 | 1106 | |
| 1083 | 1107 | /** |
| @@ -1104,9 +1128,9 @@ | ||
| 1104 | 1128 | * |
| 1105 | 1129 | * @param LP_Course_Item|LP_User_Item|int $item |
| 1106 | 1130 | * |
| 1107 | 1131 | * @return mixed |
| 1108 | - * @depecated 4.1.6.9 | |
| 1132 | + * @deprecated 4.1.6.9 | |
| 1109 | 1133 | */ |
| 1110 | 1134 | /*public function get_item_position( $item ) { |
| 1111 | 1135 | $items = $this->get_items(); |
| 1112 | 1136 | |
| @@ -1138,9 +1162,9 @@ | ||
| 1138 | 1162 | */ |
| 1139 | 1163 | public function get_next_item( $args = null ) { |
| 1140 | 1164 | $item_nav = $this->get_item_nav(); |
| 1141 | 1165 | |
| 1142 | - return apply_filters( 'learn-press/course/next-item', $item_nav[2], $this->get_id(), $args ); | |
| 1166 | + return apply_filters( 'learn-press/course/next-item', $item_nav ? $item_nav[2] : 0, $this->get_id(), $args ); | |
| 1143 | 1167 | } |
| 1144 | 1168 | |
| 1145 | 1169 | /** |
| 1146 | 1170 | * Get item standing before the item is viewing. |
| @@ -1150,8 +1174,11 @@ | ||
| 1150 | 1174 | * @return int |
| 1151 | 1175 | */ |
| 1152 | 1176 | public function get_prev_item( $args = null ) { |
| 1153 | 1177 | $item_nav = $this->get_item_nav(); |
| 1178 | + if ( ! is_array( $item_nav ) || empty( $item_nav ) ) { | |
| 1179 | + return 0; | |
| 1180 | + } | |
| 1154 | 1181 | |
| 1155 | 1182 | return apply_filters( 'learn-press/course/prev-item', $item_nav[0], $this->get_id(), $args ); |
| 1156 | 1183 | } |
| 1157 | 1184 | |
| @@ -1185,9 +1212,9 @@ | ||
| 1185 | 1212 | $user = learn_press_get_current_user(); |
| 1186 | 1213 | $pos_tmp = $pos; |
| 1187 | 1214 | |
| 1188 | 1215 | while ( $pos_tmp < $max ) { |
| 1189 | - $pos_tmp ++; | |
| 1216 | + ++$pos_tmp; | |
| 1190 | 1217 | |
| 1191 | 1218 | if ( ! $viewable ) { |
| 1192 | 1219 | $next_id = $item_ids[ $pos_tmp ]; |
| 1193 | 1220 | |
| @@ -1197,9 +1224,9 @@ | ||
| 1197 | 1224 | |
| 1198 | 1225 | $pos_tmp = $pos; |
| 1199 | 1226 | |
| 1200 | 1227 | while ( $pos_tmp > 0 ) { |
| 1201 | - $pos_tmp --; | |
| 1228 | + --$pos_tmp; | |
| 1202 | 1229 | |
| 1203 | 1230 | if ( ! $viewable ) { |
| 1204 | 1231 | $prev_id = $item_ids[ $pos_tmp ]; |
| 1205 | 1232 | |
| @@ -1248,9 +1275,8 @@ | ||
| 1248 | 1275 | return false; |
| 1249 | 1276 | } |
| 1250 | 1277 | |
| 1251 | 1278 | public function get_prev_item_html( $args = null ) { |
| 1252 | - | |
| 1253 | 1279 | } |
| 1254 | 1280 | |
| 1255 | 1281 | public function get_preview_items() { |
| 1256 | 1282 | return LP_Object_Cache::get( 'course-' . $this->get_id(), 'learn-press/course-preview-items' ); |
| @@ -1314,9 +1340,9 @@ | ||
| 1314 | 1340 | |
| 1315 | 1341 | if ( $start_time + $duration > $now ) { |
| 1316 | 1342 | $remain = $start_time + $duration - $now; |
| 1317 | 1343 | $remain = learn_press_seconds_to_weeks( $remain ); |
| 1318 | - $html = sprintf( __( 'This course will end within next %s', 'learnpress' ), $remain ); | |
| 1344 | + $html = sprintf( __( 'This course will end within the next %s', 'learnpress' ), $remain ); | |
| 1319 | 1345 | } |
| 1320 | 1346 | } |
| 1321 | 1347 | |
| 1322 | 1348 | return $html; |
| @@ -1432,6 +1458,17 @@ | ||
| 1432 | 1458 | $this->get_id(), |
| 1433 | 1459 | $this |
| 1434 | 1460 | ); |
| 1435 | 1461 | } |
| 1462 | + | |
| 1463 | + /** | |
| 1464 | + * [get_downloadable_material get all material files of this course and lesson of this course] | |
| 1465 | + * @return [array] [array of material files or empty array] | |
| 1466 | + * @deprecated 4.2.6.4 | |
| 1467 | + */ | |
| 1468 | + /*public function get_downloadable_material(): array { | |
| 1469 | + $material = LP_Material_Files_DB::getInstance(); | |
| 1470 | + $materials = $material->get_material_by_item_id( $this->get_id(), 1 ); | |
| 1471 | + return apply_filters( 'learn-press/course-materials', $materials, $this->get_id() ); | |
| 1472 | + }*/ | |
| 1436 | 1473 | } |
| 1437 | 1474 | } |