" => "", ], $course ); return Template::instance()->nest_elements( $html_wrapper, $course->get_title() ); } /** * Get short description course. * * @param LP_Course|CourseModel $course * @param int $number_words * * @return string */ public function html_short_description( $course, int $number_words = 0 ): string { $html_wrapper = [ '

' => '

', ]; if ( $course instanceof LP_Course ) { $course = CourseModel::find( $course->get_id(), true ); } $short_description = $course->get_short_description(); if ( empty( $short_description ) ) { return ''; } if ( $number_words > 0 ) { $short_description = wp_trim_words( $short_description, $number_words, '...' ); } return Template::instance()->nest_elements( $html_wrapper, $short_description ); } /** * Get description course. * * @param LP_Course|CourseModel $course * * @return string */ public function html_description( $course ): string { $content = ''; if ( $course instanceof LP_Course ) { $content = $course->get_data( 'description' ); } elseif ( $course instanceof CourseModel ) { $content = $course->get_description(); } $section = apply_filters( 'learn-press/course/html-description', [ 'wrapper' => '
', 'content' => $content, 'wrapper_end' => '
', ], $course ); return Template::combine_components( $section ); } /** * Get display categories course. * * @param LP_Course|CourseModel $course * @param array $setting * * @return string * @since 4.2.6 * @version 1.0.3 */ public function html_categories( $course, array $setting = [] ): string { $html = ''; try { if ( $course instanceof LP_Course ) { $course = CourseModel::find( $course->get_id(), true ); } if ( empty( $course ) ) { return ''; } $cats = $course->get_categories(); if ( empty( $cats ) ) { return ''; } $is_link = $setting['is_link'] ?? true; $attribute_target = ! empty( $setting['new_tab'] ) ? 'target="_blank"' : ''; $cat_names = []; foreach ( $cats as $cat ) { if ( $is_link ) { $term = sprintf( '%s', get_term_link( $cat ), $attribute_target, $cat->name ); } else { $term = $cat->name; } $cat_names[] = $term; } $content = implode( ', ', $cat_names ); $section = apply_filters( 'learn-press/course/html-categories', [ 'wrapper' => '
', 'content' => $content, 'wrapper_end' => '
', ], $course, $cats, $cat_names ); $html = Template::combine_components( $section ); } catch ( Throwable $e ) { error_log( __METHOD__ . ': ' . $e->getMessage() ); } return $html; } /** * Get display tags course. * * @param LP_Course|CourseModel $course * * @return string * @since 4.2.6 * @version 1.0.2 */ public function html_tags( $course ): string { $html = ''; try { if ( $course instanceof LP_Course ) { $course = CourseModel::find( $course->get_id(), true ); } if ( empty( $course ) ) { return ''; } $tags = $course->get_tags(); if ( empty( $tags ) ) { return ''; } $tag_names = []; foreach ( $tags as $tag ) { $term = sprintf( '%s', get_term_link( $tag ), $tag->name ); $tag_names[] = $term; } $content = implode( ', ', $tag_names ); $section = apply_filters( 'learn-press/course/html-tags', [ 'wrapper' => '
', 'content' => $content, 'wrapper_end' => '
', ], $course, $tags, $tag_names ); $html = Template::combine_components( $section ); } catch ( Throwable $e ) { error_log( __METHOD__ . ': ' . $e->getMessage() ); } return $html; } /** * Get display image course. * * @param LP_Course|CourseModel $course * @param array $data ['size'] Size of image to get, Ex: [500, 300] or string 'thumbnail', 'medium', 'large', 'full' etc. * * @return string * @since 4.2.3.2 * @version 1.0.3 */ public function html_image( $course, array $data = [] ): string { $content = ''; try { $courseModel = $course; if ( $course instanceof LP_Course ) { $courseModel = CourseModel::find( $course->get_id(), true ); } if ( ! $courseModel instanceof CourseModel ) { return ''; } if ( ! empty( $data['size'] ) ) { $size_img_send = $data['size']; // If custom size, data size is type int[], like [500, 300], not [ width => 500, height => 300 ] // Convert if data is [ width => 500, height => 300 ] if ( is_array( $size_img_send ) && array_key_exists( 'width', $size_img_send ) ) { $size_img_send = [ $size_img_send['width'] ?? 500, $size_img_send['height'] ?? 300, ]; } } else { $size_img_setting = LP_Settings::get_option( 'course_thumbnail_dimensions', [] ); $size_img_send = [ $size_img_setting['width'] ?? 500, $size_img_setting['height'] ?? 300, ]; } // Check cache before get image url $cache = new \LP_Course_Cache(); $key_cache = 'image_url/' . $courseModel->get_id(); if ( is_array( $size_img_send ) && count( $size_img_send ) === 2 ) { $key_cache .= '/' . implode( 'x', $size_img_send ); } elseif ( is_string( $size_img_send ) ) { $key_cache .= '/' . $size_img_send; } // Set cache for image url $course_img_url = $cache->get_cache( $key_cache ); if ( false === $course_img_url ) { $course_img_url = $courseModel->get_image_url( $size_img_send ); $cache->set_cache( $key_cache, $course_img_url ); $cache->save_cache_keys( sprintf( 'image_urls/%s', $courseModel->get_id() ), $key_cache ); } $content = sprintf( '%s', esc_url_raw( $course_img_url ), _x( 'course thumbnail', 'no course thumbnail', 'learnpress' ) ); $section = apply_filters( 'learn-press/course/html-image', [ 'wrapper' => '
', 'content' => $content, 'wrapper_end' => '
', ], $courseModel ); $content = Template::combine_components( $section ); } catch ( Throwable $e ) { error_log( __METHOD__ . ': ' . $e->getMessage() ); } return $content; } /** * Get display instructor course. * * @param CourseModel $course * @param bool $with_avatar * * @return string * @since 4.2.5.8 * @version 1.0.2 */ public function html_instructor( $course, bool $with_avatar = false, $setting = [] ): string { $content = ''; try { $instructor = $course->get_author_model(); if ( ! $instructor ) { return ''; } $singleInstructorTemplate = SingleInstructorTemplate::instance(); $userTemplate = new UserTemplate( 'instructor' ); $is_link = $setting['is_link'] ?? true; if ( $is_link ) { $attribute_target = ! empty( $setting['new_tab'] ) ? 'target="_blank"' : ''; $link_instructor = sprintf( '%s %s', $instructor->get_url_instructor(), $attribute_target, $with_avatar ? $userTemplate->html_avatar( $instructor ) : '', $singleInstructorTemplate->html_display_name( $instructor ) ); } else { $link_instructor = sprintf( '%s %s', $with_avatar ? $userTemplate->html_avatar( $instructor ) : '', $singleInstructorTemplate->html_display_name( $instructor ) ); } $section = apply_filters( 'learn-press/course/instructor-html', [ 'wrapper' => '
', 'content' => $link_instructor, 'wrapper_end' => '
', ], $course, $instructor, $with_avatar ); $content = Template::combine_components( $section ); } catch ( Throwable $e ) { error_log( __METHOD__ . ': ' . $e->getMessage() ); } return $content; } /** * Get html regular price * * @param CourseModel $course * * @return string */ public function html_regular_price( CourseModel $course ): string { $price = learn_press_format_price( $course->get_regular_price() ); $price = apply_filters( 'learn-press/course/regular-price-html', $price, $course, $this ); return sprintf( '%s', $price ); } /** * Get display price course. * * @param LP_Course|CourseModel $course * * @return string */ public function html_price( $course ): string { $price_html = ''; if ( $course instanceof LP_Course ) { $course = CourseModel::find( $course->get_id(), true ); } if ( ! $course ) { return $price_html; } if ( $course->is_free() ) { if ( '' != $course->get_sale_price() ) { $price_html .= $this->html_regular_price( $course ); } $price_html .= sprintf( '%s', esc_html__( 'Free', 'learnpress' ) ); $price_html = apply_filters( 'learn_press_course_price_html_free', $price_html, $this ); } elseif ( $course->has_no_enroll_requirement() ) { $price_html .= ''; } else { if ( $course->has_sale_price() ) { $price_html .= $this->html_regular_price( $course ); } $price_html .= sprintf( '%s', learn_press_format_price( $course->get_price(), true ) ); $price_html = sprintf( '%1$s %2$s %3$s', $this->html_price_prefix( $course ), $price_html, $this->html_price_suffix( $course ) ); $price_html = apply_filters( 'learn_press_course_price_html', $price_html, $course->has_sale_price(), $course->get_id() ); } // @since 4.2.7 $price_html = sprintf( '%s', $price_html ); return apply_filters( 'learn-press/course/html-price', $price_html, $course ); } /** * Get deliver type * * @param CourseModel $course * * @return string */ public function html_capacity( CourseModel $course ): string { $content = ''; $html_wrapper = [ '' => '', ]; $capacity = $course->get_meta_value_by_key( CoursePostModel::META_KEY_MAX_STUDENTS, 0 ); if ( $capacity == 0 ) { $content = __( 'Unlimited', 'learnpress' ); } else { $content = sprintf( '%d %s', $capacity, _n( 'Student', 'Students', $capacity, 'learnpress' ) ); } return Template::instance()->nest_elements( $html_wrapper, $content ); } /** * Get display total student's course. * * @param LP_Course|CourseModel $course * * @return string * @since 4.2.3.4 * @version 1.0.2 */ public function html_count_student( $course ): string { if ( $course instanceof LP_Course ) { $course = CourseModel::find( $course->get_id(), true ); } if ( empty( $course ) ) { return ''; } $count_student = $course->get_total_user_enrolled_or_purchased(); $count_student += $course->get_fake_students(); $content = sprintf( '%d %s', $count_student, _n( 'Student', 'Students', $count_student, 'learnpress' ) ); $html_wrapper = [ '
' => '
', ]; return Template::instance()->nest_elements( $html_wrapper, $content ); } /** * Get display total lesson's course. * * @param LP_Course|CourseModel $course * @param string $item_type custom post type item * @param bool $show_only_number * * @return string */ public function html_count_item( $course, string $item_type, bool $show_only_number = false ): string { if ( $course instanceof LP_Course ) { $course = CourseModel::find( $course->get_id(), true ); } if ( empty( $course ) ) { return ''; } $info_total_items = $course->get_total_items(); if ( empty( $info_total_items ) ) { return ''; } $count_item = $info_total_items->{$item_type} ?? 0; if ( $show_only_number ) { $content = $count_item; } else { switch ( $item_type ) { case LP_LESSON_CPT: $content = sprintf( '%d %s', $count_item, _n( 'Lesson', 'Lessons', $count_item, 'learnpress' ) ); break; case LP_QUIZ_CPT: $content = sprintf( '%d %s', $count_item, _n( 'Quiz', 'Quizzes', $count_item, 'learnpress' ) ); break; case 'lp_assignment': $content = sprintf( '%d %s', $count_item, _n( 'Assignment', 'Assignments', $count_item, 'learnpress' ) ); break; case 'lp_h5p': $content = sprintf( '%d %s', $count_item, _n( 'H5P', 'H5Ps', $count_item, 'learnpress' ) ); break; default: $content = apply_filters( 'learn-press/single-course/i18n/count-item', $count_item, $course, $item_type ); break; } } $section = apply_filters( 'learn-press/single-course/html-count-item', [ 'wrapper' => sprintf( '
', $item_type ), 'content' => $content, 'wrapper_end' => '
', ], $course, $item_type, $count_item ); return Template::combine_components( $section ); } /** * Get html level course. * * @param LP_Course|CourseModel $course * * @return string * @since 4.2.3.5 * @version 1.0.1 */ public function html_level( $course ): string { $content = ''; try { if ( $course instanceof LP_Course ) { $course = CourseModel::find( $course->get_id(), true ); } $level = $course->get_meta_value_by_key( CoursePostModel::META_KEY_LEVEL, '' ); if ( empty( $level ) ) { $level = 'all'; } $levels = lp_course_level(); $level = $levels[ $level ] ?? $levels['all']; $html_wrapper = [ '' => '', ]; $content = Template::instance()->nest_elements( $html_wrapper, $level ); } catch ( Throwable $e ) { error_log( __METHOD__ . ': ' . $e->getMessage() ); } return $content; } /** * Get html duration course. * * @param LP_Course|CourseModel $course * * @return string * @since 4.2.3.5 * @version 1.0.0 */ public function html_duration( $course ): string { $content = ''; try { if ( $course instanceof LP_Course ) { $course = CourseModel::find( $course->get_id(), true ); } $duration = $course->get_meta_value_by_key( CoursePostModel::META_KEY_DURATION, '' ); $duration_arr = explode( ' ', $duration ); $duration_number = floatval( $duration_arr[0] ?? 0 ); $duration_type = $duration_arr[1] ?? ''; if ( empty( $duration_number ) ) { $duration_str = __( 'Lifetime', 'learnpress' ); } else { $duration_str = LP_Datetime::get_string_plural_duration( $duration_number, $duration_type ); } $html_wrapper = [ '' => '', ]; $content = Template::instance()->nest_elements( $html_wrapper, $duration_str ); } catch ( Throwable $e ) { error_log( __METHOD__ . ': ' . $e->getMessage() ); } return $content; } /** * Get feature review * * @param CourseModel $course * @param UserModel|false $userModel * @param array $data * * @return string * @since 4.2.7 * @version 1.0.2 */ public function html_feature_review( CourseModel $course, $userModel = false, array $data = [] ): string { $feature_review = $course->get_meta_value_by_key( CoursePostModel::META_KEY_FEATURED_REVIEW, '' ); if ( empty( $feature_review ) ) { return ''; } if ( $userModel instanceof UserModel ) { $userCourseModel = UserCourseModel::find( $userModel->get_id(), $course->get_id(), true ); if ( $userCourseModel ) { return ''; } } $stars = ''; foreach ( range( 1, 5 ) as $star ) { $stars .= ''; } $section = [ 'wrapper' => sprintf( '', ]; return Template::combine_components( $section ); } /** * Get button external * * @param CourseModel $courseModel * @param UserModel|false $userModel * * @return string * @since 4.2.7 * @version 1.0.3 */ public function html_btn_external( CourseModel $courseModel, $userModel ): string { $external_link = $courseModel->get_meta_value_by_key( CoursePostModel::META_KEY_EXTERNAL_LINK_BY_COURSE, '' ); if ( empty( $external_link ) ) { return ''; } // Check user has enrolled, finished or purchased course if ( $userModel instanceof UserModel ) { $userCourse = UserCourseModel::find( $userModel->get_id(), $courseModel->get_id(), true ); if ( $userCourse && ( $userCourse->has_enrolled_or_finished() || $userCourse->has_purchased() ) ) { return ''; } } $content = sprintf( '%s', esc_url_raw( $external_link ), __( 'Contact To Request', 'learnpress' ) ); return apply_filters( 'learn-press/course/html-button-external', $content, $courseModel, $userModel ); } /** * HTML button purchase course * * @param CourseModel $courseModel * @param false|UserModel $userModel * * @return string * @since 4.2.7.2 * @version 1.0.2 */ public function html_btn_purchase_course( CourseModel $courseModel, $userModel ): string { $html_btn = ''; $can_purchase = $courseModel->can_purchase( $userModel ); if ( is_wp_error( $can_purchase ) ) { $error_code_show = apply_filters( 'learn-press/course/html-button-purchase/show-messages', [] ); if ( in_array( $can_purchase->get_error_code(), $error_code_show ) && ! empty( $can_purchase->get_error_message() ) ) { $html_btn = Template::print_message( $can_purchase->get_error_message(), 'warning', false ); } } else { $html_btn = sprintf( '', __( 'Buy Now', 'learnpress' ) ); } if ( empty( $html_btn ) ) { return apply_filters( 'learn-press/course/html-button-purchase/empty', $html_btn, $courseModel, $userModel ); } $class_guest_checkout = LP_Checkout::instance()->is_enable_guest_checkout() ? 'guest_checkout' : ''; // Hook action old $html_hook_old = ''; if ( has_action( 'learn-press/after-purchase-button' ) ) { ob_start(); do_action( 'learn-press/after-purchase-button' ); $html_hook_old = ob_get_clean(); } if ( has_filter( 'learnpress/course/template/button-purchase/can-show' ) ) { $user_id = 0; if ( $userModel instanceof UserModel ) { $user_id = $userModel->get_id(); } $user_old = learn_press_get_user( $user_id ); $course_old = learn_press_get_course( $courseModel->get_id() ); $can_show = apply_filters( 'learnpress/course/template/button-purchase/can-show', true, $user_old, $course_old ); if ( ! $can_show ) { return ''; } } // End hook action old $section = apply_filters( 'learn-press/course/html-button-purchase', [ 'form' => sprintf( '
', esc_attr( $class_guest_checkout ) ), 'input' => sprintf( '', esc_attr( $courseModel->get_id() ) ), 'btn' => $html_btn, 'hook_old' => $html_hook_old, 'form_end' => '
', ], $courseModel, $userModel ); return Template::combine_components( $section ); } /** * HTML button enroll course * * @param CourseModel $courseModel * @param false|UserModel $userModel * * @return string * @since 4.2.7.3 * @version 1.0.0 */ public function html_btn_enroll_course( CourseModel $courseModel, $userModel ): string { $html_btn = ''; $can_enroll = $courseModel->can_enroll( $userModel ); if ( is_wp_error( $can_enroll ) ) { $error_code_show = apply_filters( 'learn-press/course/html-button-enroll/show-messages', [ 'course_is_no_required_enroll_not_login', 'course_out_of_stock' ] ); if ( in_array( $can_enroll->get_error_code(), $error_code_show ) && ! empty( $can_enroll->get_error_message() ) ) { $html_btn = Template::print_message( $can_enroll->get_error_message(), 'warning', false ); } } else { $html_btn = sprintf( '', __( 'Start Now', 'learnpress' ) ); } if ( empty( $html_btn ) ) { return $html_btn; } // Hook old $html_hook_before_old = ''; if ( has_action( 'learn-press/before-enroll-button' ) ) { ob_start(); do_action( 'learn-press/before-enroll-button' ); $html_hook_before_old = ob_get_clean(); } $html_hook_after_old = ''; if ( has_action( 'learn-press/after-enroll-button' ) ) { ob_start(); do_action( 'learn-press/after-enroll-button' ); $html_hook_after_old = ob_get_clean(); } if ( has_filter( 'learnpress/course/template/button-enroll/can-show' ) ) { $user_id = 0; if ( $userModel instanceof UserModel ) { $user_id = $userModel->get_id(); } $user_old = learn_press_get_user( $user_id ); $course_old = learn_press_get_course( $courseModel->get_id() ); $can_show = apply_filters( 'learnpress/course/template/button-enroll/can-show', true, $user_old, $course_old ); if ( ! $can_show ) { return ''; } } // End hook old $section = apply_filters( 'learn-press/course/html-button-enroll', [ 'form' => '
', 'hook_before_old' => $html_hook_before_old, 'input' => sprintf( '', esc_attr( $courseModel->get_id() ) ), 'btn' => $html_btn, 'hook_after_old' => $html_hook_after_old, 'form_end' => '
', ], $courseModel, $userModel ); return Template::combine_components( $section ); } /** * Sidebar * * @param CourseModel $courseModel * @param array $data * * @return void * @version 1.0.1 * @since 4.2.7 */ public function html_sidebar( CourseModel $courseModel, array $data = [] ): string { $html = ''; try { if ( is_active_sidebar( 'course-sidebar' ) ) { ob_start(); dynamic_sidebar( 'course-sidebar' ); $sidebar_content = ob_get_clean(); $section = apply_filters( 'learn-press/course/html-sidebar', [ 'wrapper' => sprintf( '
', esc_attr( $data['lp_display_on'] ?? '' ) ), 'content' => $sidebar_content, 'wrapper_end' => '
', ], $courseModel ); $html = Template::combine_components( $section ); } } catch ( Throwable $e ) { error_log( $e->getMessage() ); } return $html; } /** * HTML meta faqs * * @param CourseModel $courseModel * @param array $data * * @return string * @since 4.2.7.2 * @version 1.0.1 */ public function html_faqs( CourseModel $courseModel, array $data = [] ): string { $html = ''; try { $show_heading = $data['show_heading'] ?? true; $faqs = $courseModel->get_meta_value_by_key( CoursePostModel::META_KEY_FAQS, [] ); if ( ! is_array( $faqs ) && ! is_object( $faqs ) ) { return ''; } if ( empty( $faqs ) ) { return ''; } foreach ( $faqs as $faq ) { $title = $faq[0]; $description = $faq[1]; if ( empty( $title ) || empty( $description ) ) { continue; } $key = uniqid(); $section_item = [ 'input_checkbox' => sprintf( '', $key ), 'wrapper' => '
', 'title' => sprintf( '', $key, $title ), 'content' => '
', 'content_inner' => '
', 'content_main' => wp_kses_post( $description ), 'content_inner_end' => '
', 'content_end' => '
', 'wrapper_end' => '
', ]; $html .= Template::combine_components( $section_item ); } $section = apply_filters( 'learn-press/course/html-faqs', [ 'wrapper' => '
', 'title' => $show_heading ? sprintf( '

%s

', __( 'FAQs', 'learnpress' ) ) : '', 'content' => $html, 'wrapper_end' => '
', ], $courseModel ); $html = Template::combine_components( $section ); } catch ( Throwable $e ) { error_log( $e->getMessage() ); } return $html; } /** * HTML struct course box extra * * @param CourseModel $courseModel * @param $title * @param string $html_list * * @return string * @since 4.2.7.2 * @version 1.0.0 */ public function html_course_box_extra( CourseModel $courseModel, $title, string $html_list ): string { if ( empty( $html_list ) ) { return ''; } $section = apply_filters( 'learn-press/course/html-course-box-extra', [ 'wrapper' => '
', 'title' => sprintf( '

%s

', $title ), 'content' => '
', 'content_inner' => '
', 'list' => $html_list, 'content_inner_end' => '
', 'content_end' => '
', 'wrapper_end' => '
', ], $courseModel ); return Template::combine_components( $section ); } /** * HTML meta requirements * * @param CourseModel $courseModel * * @return string * @since 4.2.7.2 * @version 1.0.0 */ public function html_requirements( CourseModel $courseModel ): string { $html = ''; try { $requirements = $courseModel->get_meta_value_by_key( CoursePostModel::META_KEY_REQUIREMENTS, [] ); if ( ! is_array( $requirements ) && ! is_object( $requirements ) ) { return ''; } if ( empty( $requirements ) ) { return ''; } $html_lis = ''; foreach ( $requirements as $requirement ) { $html_lis .= sprintf( '
  • %s
  • ', $requirement ); } $section_list = [ 'wrapper' => '', ]; $section = apply_filters( 'learn-press/course/html-requirements', [ 'wrapper' => '
    ', 'title' => sprintf( '

    %s

    ', __( 'Requirements', 'learnpress' ) ), 'content' => Template::combine_components( $section_list ), 'wrapper_end' => '
    ', ], $courseModel, $requirements ); $html = Template::combine_components( $section ); } catch ( Throwable $e ) { error_log( $e->getMessage() ); } return $html; } /** * HTML meta features * * @param CourseModel $courseModel * * @return string * @since 4.2.7.2 * @version 1.0.0 */ public function html_features( CourseModel $courseModel ): string { $html = ''; try { $features = $courseModel->get_meta_value_by_key( CoursePostModel::META_KEY_FEATURES, [] ); if ( ! is_array( $features ) && ! is_object( $features ) ) { return ''; } if ( empty( $features ) ) { return ''; } $html_lis = ''; foreach ( $features as $feature ) { $html_lis .= sprintf( '
  • %s
  • ', $feature ); } $section_list = [ 'wrapper' => '', ]; $section = apply_filters( 'learn-press/course/html-features', [ 'wrapper' => '
    ', 'title' => sprintf( '

    %s

    ', __( 'Features', 'learnpress' ) ), 'content' => Template::combine_components( $section_list ), 'wrapper_end' => '
    ', ], $courseModel, $features ); $html = Template::combine_components( $section ); } catch ( Throwable $e ) { error_log( $e->getMessage() ); } return $html; } /** * HTML meta target * * @param CourseModel $courseModel * * @return string * @since 4.2.7.2 * @version 1.0.0 */ public function html_target( CourseModel $courseModel ): string { $html = ''; try { $targets = $courseModel->get_meta_value_by_key( CoursePostModel::META_KEY_TARGET, [] ); if ( ! is_array( $targets ) && ! is_object( $targets ) ) { return ''; } if ( empty( $targets ) ) { return ''; } $html_lis = ''; foreach ( $targets as $target ) { $html_lis .= sprintf( '
  • %s
  • ', $target ); } $section_list = [ 'wrapper' => '', ]; $section = apply_filters( 'learn-press/course/html-target', [ 'wrapper' => '
    ', 'title' => sprintf( '

    %s

    ', __( 'Target audiences', 'learnpress' ) ), 'content' => Template::combine_components( $section_list ), 'wrapper_end' => '
    ', ], $courseModel, $targets ); $html = Template::combine_components( $section ); } catch ( Throwable $e ) { error_log( $e->getMessage() ); } return $html; } /** * HTML material * * @param CourseModel $courseModel * @param UserModel|false $userModel * @param array $data * * @return string * @since 4.2.7.2 * @version 1.0.4 */ public function html_material( CourseModel $courseModel, $userModel = false, array $data = [] ): string { $html = ''; try { $show_heading = $data['show_heading'] ?? true; $can_show = false; if ( $userModel instanceof UserModel ) { if ( $courseModel->check_user_is_author( $userModel ) || user_can( $userModel->get_id(), ADMIN_ROLE ) ) { $can_show = true; } else { $userCourseModel = UserCourseModel::find( $userModel->get_id(), $courseModel->get_id(), true ); if ( $userCourseModel && ( $userCourseModel->has_enrolled_or_finished() || $userCourseModel->has_purchased() ) ) { $can_show = true; } } } elseif ( $courseModel->has_no_enroll_requirement() ) { $can_show = true; } $can_show = apply_filters( 'learn-press/course-material/can-show', $can_show, $courseModel, $userModel ); $file_per_page = LP_Settings::get_option( 'material_file_per_page', - 1 ); $count_files = LP_Material_Files_DB::getInstance()->get_total( $courseModel->get_id() ); if ( ! $can_show || $file_per_page == 0 || $count_files <= 0 ) { return $html; } ob_start(); do_action( 'learn-press/course-material/layout', [] ); $html_content = ob_get_clean(); $section = [ 'wrapper' => '
    ', 'title' => $show_heading ? sprintf( '

    %s

    ', __( 'Course Material', 'learnpress' ) ) : '', 'content' => $html_content, 'wrapper_end' => '
    ', ]; $html = Template::combine_components( $section ); } catch ( Throwable $e ) { error_log( __METHOD__ . ': ' . $e->getMessage() ); } return $html; } /** * Get html level course. * * @param LP_Course|CourseModel $course * * @return string * @since 4.2.7.5 * @version 1.0.1 */ public function html_price_prefix( $course ): string { $html = ''; try { if ( $course instanceof LP_Course ) { $course = CourseModel::find( $course->get_id(), true ); if ( ! $course instanceof CourseModel ) { return $html; } } $price_prefix_str = $course->get_meta_value_by_key( CoursePostModel::META_KEY_PRICE_PREFIX, '' ); if ( empty( $price_prefix_str ) ) { return $html; } $html = sprintf( '%s', $price_prefix_str ); } catch ( Throwable $e ) { error_log( __METHOD__ . ': ' . $e->getMessage() ); } return $html; } /** * Get html level course. * * @param LP_Course|CourseModel $course * * @return string * @since 4.2.7.5 * @version 1.0.0 */ public function html_price_suffix( $course ): string { $html = ''; try { $price_suffix_str = $course->get_meta_value_by_key( CoursePostModel::META_KEY_PRICE_SUFFIX, '' ); if ( empty( $price_suffix_str ) ) { return $html; } $html = sprintf( '%s', $price_suffix_str ); } catch ( Throwable $e ) { error_log( __METHOD__ . ': ' . $e->getMessage() ); } return $html; } /** * Get html featured course. * * @param CourseModel $courseModel * * @return string * @since 4.2.9 * @version 1.0.0 */ public function html_featured( CourseModel $courseModel ): string { $html = ''; try { $is_featured = $courseModel->get_meta_value_by_key( CoursePostModel::META_KEY_FEATURED, 'no' ); if ( $is_featured !== 'yes' ) { return $html; } $html = sprintf( '%s', __( 'Featured', 'learnpress' ) ); } catch ( Throwable $e ) { LP_Debug::error_log( $e ); } return $html; } /** * GET HTML comment default of WP * * @param CourseModel $courseModel * @param false|UserModel $userModel * * @return string * @since 4.2.7.6 * @version 1.0.0 */ public function html_comment( CourseModel $courseModel, $userModel = false ): string { $args = [ 'id_url' => 'course-comments', 'course_id' => $courseModel->get_id(), 'user_id' => $userModel instanceof UserModel ? $userModel->get_id() : 0, ]; $callBack = [ 'class' => __CLASS__, 'method' => 'render_html_comments', ]; $html = TemplateAJAX::load_content_via_ajax( $args, $callBack ); $section_comment = apply_filters( 'learn-press/single-course/html-comment', [ 'wrapper' => '
    ', 'content' => $html, 'wrapper_end' => '
    ', ], $courseModel, $userModel ); return Template::combine_components( $section_comment ); } /** * Render HTML comments of course. * * @param array $data ['course_id'] * * @return stdClass * @since 4.2.7.6 * @version 1.0.0 */ public static function render_html_comments( array $data ): stdClass { $response = new stdClass(); $response->content = ''; try { $course_id = $data['course_id'] ?? 0; $user_id = $data['user_id'] ?? 0; if ( empty( $course_id ) ) { return $response; } global $withcomments, $post; $post = get_post( $course_id ); if ( $post->comment_status !== 'open' ) { return $response; } else { $withcomments = true; } $withcomments = apply_filters( 'learn-press/single-course/render-html-comment/is-show', $withcomments, $post, $user_id ); //$post->comment_count = 0; ob_start(); add_filter( 'deprecated_file_trigger_error', '__return_false' ); comments_template(); remove_filter( 'deprecated_file_trigger_error', '__return_false' ); $html = ob_get_clean(); $response->content = $html; } catch ( Throwable $e ) { $response->content = Template::print_message( $e->getMessage(), 'error', false ); } return $response; } /** * Get HTML curriculum of course. * * @param CourseModel $courseModel * @param UserModel|false $userModel * @param LessonPostModel|QuizPostModel|PostModel|mixed $itemModelCurrent * * @return string * @since 4.2.7.6 * @version 1.0.3 */ public function html_curriculum( CourseModel $courseModel, $userModel, $itemModelCurrent = false ): string { $html = ''; try { // Get current item viewing /** * @var $lp_course_item LP_Course_Item * @var $post \WP_Post */ global $lp_course_item, $post; /** * @var $itemModelCurrent LessonPostModel|QuizPostModel|AssignmentPostModel|PostModel... */ $itemModelCurrent = $courseModel->get_item_model( $post->ID, $post->post_type ); if ( $lp_course_item ) { $itemModelCurrent = $courseModel->get_item_model( $lp_course_item->get_id(), $lp_course_item->get_item_type() ); } if ( $itemModelCurrent ) { $item_types = CourseModel::item_types_support(); // Check item type is support if ( in_array( $itemModelCurrent->post_type, $item_types ) ) { $this->currentItemModel = $itemModelCurrent; } } // End get current item viewing $section_items = $courseModel->get_section_items(); $html = Template::print_message( esc_html__( 'There are no items in the curriculum yet.', 'learnpress' ), 'info', false ); if ( empty( $section_items ) ) { return $html; } wp_enqueue_script( 'lp-curriculum' ); $li_section_items = ''; foreach ( $section_items as $section_item ) { $li_section_items .= $this->render_html_section_item( $courseModel, $userModel, $section_item ); } $section = apply_filters( 'learn-press/course/html-curriculum', [ 'wrapper' => '
    ', 'title' => sprintf( '

    %s

    ', esc_html__( 'Curriculum', 'learnpress' ) ), 'curriculum_info' => '
    ', 'curriculum_info_left' => '', 'curriculum_info_right' => '
    ', 'expand_all' => sprintf( '%s', esc_html__( 'Expand all sections', 'learnpress' ) ), 'collapse_all' => sprintf( '%s', esc_html__( 'Collapse all sections', 'learnpress' ) ), 'curriculum_info_right_end' => '
    ', 'curriculum_info_end' => '
    ', 'curriculum' => '
    ', 'sections' => '', 'curriculum_end' => '
    ', 'wrapper_end' => '
    ', ], $courseModel, $userModel, $itemModelCurrent ); $html = Template::combine_components( $section ); } catch ( Throwable $e ) { error_log( __METHOD__ . ': ' . $e->getMessage() ); } return $html; } /** * Render HTML section item * * @param CourseModel $courseModel * @param UserModel|false $userModel * @param $section_item object {section_id, section_name, section_description, items[{item_id, item_order, item_type, title, preview}]} * * @return string * @since 4.2.7.6 * @version 1.0.1 */ public function render_html_section_item( CourseModel $courseModel, $userModel, $section_item ): string { if ( ! $section_item instanceof stdClass ) { return ''; } $section_id = $section_item->section_id ?? 0; $section_name = $section_item->section_name ?? ''; $section_description = $section_item->section_description ?? ''; $section_order = $section_item->section_order ?? 1; $items = $section_item->items ?? []; $html_section_description = ''; if ( ! empty( $section_description ) ) { $html_section_description = sprintf( '
    %s
    ', wp_kses_post( $section_description ) ); } $section_header = [ 'start' => '
    ', 'toggle' => '
    ', 'info' => '
    ', 'title' => sprintf( '
    %s
    ', wp_kses_post( $section_name ) ), 'description' => $html_section_description, 'info_end' => '
    ', 'count_items' => sprintf( '
    %d
    ', count( $items ) ), 'end' => '
    ', ]; $li_items = ''; foreach ( $items as $item ) { $li_items .= $this->render_html_course_item( $courseModel, $userModel, $item, $section_item ); } $section_items = [ 'start' => '', ]; $curriculum_display_setting = LP_Settings::get_option( 'curriculum_display', 'expand_first_section' ); $class_section_toggle = ''; if ( $this->currentItemModel ) { $current_section = $courseModel->get_section_of_item( $this->currentItemModel->get_id() ); if ( $current_section != $section_id ) { $class_section_toggle = 'lp-collapse'; } } else { if ( $curriculum_display_setting === 'collapse_all' ) { $class_section_toggle = 'lp-collapse'; } elseif ( $curriculum_display_setting === 'expand_first_section' ) { if ( $section_order > 1 ) { $class_section_toggle = 'lp-collapse'; } } } $class_section_toggle = apply_filters( 'learn-press/course/html-section-item/class-section-toggle', $class_section_toggle, $courseModel, $userModel, $section_item ); $section_item = apply_filters( 'learn-press/course/html-section-item', [ 'start' => sprintf( '
  • ', $class_section_toggle, $section_id ), 'header' => Template::combine_components( $section_header ), 'items' => Template::combine_components( $section_items ), 'end' => '
  • ', ], $courseModel, $userModel, $section_item ); return Template::combine_components( $section_item ); } /** * @param CourseModel $courseModel * @param UserModel|false $userModel * @param $item stdClass {item_id, item_order, item_type, title, preview} * @param $section_item stdClass {section_id, section_name, section_description, items[{item_id, item_order, item_type, title, preview}]} * * @return string * @since 4.2.7.6 * @version 1.0.2 */ public function render_html_course_item( CourseModel $courseModel, $userModel, $item, $section_item ): string { $html = ''; if ( ! $item instanceof stdClass ) { return $html; } $item_id = (int) ( $item->item_id ?? $item->id ?? 0 ); $item_order = $item->item_order ?? $item->order ?? 0; $item_type = $item->item_type ?? $item->type ?? ''; $title = $item->title ?? ''; $has_preview = $item->preview ?? ''; $class_current = ''; if ( $this->currentItemModel ) { $current_item_id = $this->currentItemModel->get_id(); if ( $current_item_id == $item_id ) { $class_current = 'current'; } } //LP_Course_Item::get_item( $item_id, $course->get_id() ); $itemModel = $courseModel->get_item_model( $item_id, $item_type ); if ( empty( $itemModel ) ) { return $html; } $link_item = $courseModel->get_item_link( $item_id, $item_type ); $item_duration = ''; $html_item_duration = ''; if ( is_callable( [ $itemModel, 'get_duration' ] ) ) { $item_duration = $itemModel->get_duration(); } else { $item_duration = get_post_meta( $item_id, '_lp_duration', true ); } $duration_arr = explode( ' ', $item_duration ); $duration_number = floatval( $duration_arr[0] ?? 0 ); $duration_type = $duration_arr[1] ?? ''; if ( $duration_number > 0 ) { $item_duration_plural = LP_Datetime::get_string_plural_duration( $duration_number, $duration_type ); $html_item_duration = sprintf( '%s', $item_duration_plural ); } // Count question of quiz $html_item_count_questions = ''; if ( $item_type === LP_QUIZ_CPT ) { $quizPostModel = QuizPostModel::find( $item_id, true ); if ( $quizPostModel instanceof QuizPostModel ) { $question_count = $quizPostModel->count_questions(); $html_item_duration .= sprintf( '%s', sprintf( _n( '%d Question', '%d Questions', $question_count, 'learnpress' ), $question_count ) ); } } $user_item_status_ico_flag = 'locked'; $user_attended_course = false; if ( $userModel instanceof UserModel ) { $userCourseModel = UserCourseModel::find( $userModel->get_id(), $courseModel->get_id(), true ); if ( $userCourseModel && $userCourseModel->get_status() !== UserItemModel::STATUS_CANCEL && $userCourseModel->get_status() !== UserCourseModel::STATUS_PURCHASED ) { $user_attended_course = true; // Check status of item's course $userCourseItem = $userCourseModel->get_item_attend( $item_id, $item_type ); if ( ! $userCourseItem instanceof UserItemModel ) { $user_item_status_ico_flag = UserItemModel::GRADUATION_IN_PROGRESS; } else { $user_item_status_ico_flag = $userCourseItem->get_status(); $user_item_graduation = $userCourseItem->get_graduation(); if ( ! empty( $user_item_graduation ) ) { $user_item_status_ico_flag = $user_item_graduation . ' completed'; } if ( empty( $user_item_status_ico_flag ) ) { $user_item_status_ico_flag = UserItemModel::GRADUATION_IN_PROGRESS; } } if ( $user_item_status_ico_flag === UserItemModel::GRADUATION_IN_PROGRESS ) { if ( $userCourseModel->get_time_remaining() === 0 ) { $user_item_status_ico_flag = 'locked'; } elseif ( $userCourseModel->get_status() === UserItemModel::STATUS_FINISHED && $courseModel->enable_block_when_finished() ) { $user_item_status_ico_flag = 'locked'; } } } } if ( $has_preview && ! $user_attended_course ) { $user_item_status_ico_flag = 'preview'; } if ( $courseModel->has_no_enroll_requirement() && ! $user_attended_course && ! $userModel ) { $user_item_status_ico_flag = UserItemModel::GRADUATION_IN_PROGRESS; } $user_item_status_ico_flag = apply_filters( 'learn-press/course/item/status-ico-flag', $user_item_status_ico_flag, $courseModel, $userModel, $item, $section_item ); $html_item_status = sprintf( '
    ', $user_item_status_ico_flag ); $html_item_right = []; if ( $html_item_count_questions != '' || $html_item_duration != '' ) { $html_item_right = [ 'item_right' => '
    ', 'question_count' => $html_item_count_questions, 'duration' => $html_item_duration, 'item_right_end' => '
    ', ]; } $section_item = apply_filters( 'learn-press/course/html-course-item', [ 'start' => sprintf( '
  • ', $class_current, $item_id, $item_order, $item_type ), 'link' => sprintf( '', esc_url_raw( $link_item ) ), 'item_info' => '
    ', 'icon' => sprintf( '', esc_attr( $item_type ) ), 'item_order' => sprintf( '%s.%s', $section_item->section_order, $item_order ), 'item_info_end' => '
    ', 'item_content' => '
    ', 'item_left' => '
    ', 'title' => sprintf( '
    %s
    ', wp_kses_post( $title ) ), 'item_left_end' => '
    ', 'item_right' => Template::combine_components( $html_item_right ), 'item_content_end' => '
    ', 'status' => $html_item_status, 'link_end' => '
    ', 'end' => '
  • ', ], $courseModel, $userModel, $item, $section_item ); return Template::combine_components( $section_item ); } /** * Render string to data content * * @param CourseModel $course * @param string $data_content * * @return string */ public function render_data( CourseModel $course, string $data_content = '' ): string { $author_of_course = $course->get_author_model(); $singleInstructorTemplate = SingleInstructorTemplate::instance(); // render count items $pattern_count_items = '/{{course_count_.*?}}/'; preg_match_all( $pattern_count_items, $data_content, $matches_count_items ); if ( ! empty( $matches_count_items ) ) { $items = $matches_count_items[0]; foreach ( $items as $item ) { $method = str_replace( [ '{{', '}}' ], '', $item ); $post_type_item = str_replace( 'course_count_', '', $method ); $data_content = str_replace( $item, $this->html_count_item( $course, $post_type_item ), $data_content ); } } return str_replace( [ '{{course_id}}', '{{course_title}}', '{{course_image}}', '{{course_url}}', '{{course_short_description}}', '{{course_price}}', '{{course_categories}}', '{{course_count_student}}', '{{course_author_display_name}}', '{{course_author_url}}', '{{course_author_avatar}}', ], [ $course->get_id(), $this->html_title( $course ), $this->html_image( $course ), $course->get_permalink(), $this->html_short_description( $course ), $this->html_price( $course ), $this->html_categories( $course ), $this->html_count_student( $course ), $singleInstructorTemplate->html_display_name( $author_of_course ), $author_of_course->get_url_instructor(), $singleInstructorTemplate->html_avatar( $author_of_course ), ], $data_content ); } /** * Get text button read more of course on list courses. * * @param CourseModel $courseModel * * @return string * @since 4.3.4 * @version 1.0.0 */ public static function text_button_course( CourseModel $courseModel ): string { $btn_text = __( 'Enroll Now', 'learnpress' ); $userModel = UserModel::find( get_current_user_id(), true ); $userCourseModel = null; if ( $userModel instanceof UserModel ) { $userCourseModel = UserCourseModel::find( $userModel->get_id(), $courseModel->get_id(), true ); } if ( $userCourseModel instanceof UserCourseModel ) { if ( $userCourseModel->has_enrolled() ) { $btn_text = __( 'Continue Learning', 'learnpress' ); } elseif ( $userCourseModel->has_purchased() ) { $btn_text = __( 'Start Learning', 'learnpress' ); } else { $btn_text = __( 'View Detail', 'learnpress' ); } } else { if ( $courseModel->has_no_enroll_requirement() ) { $btn_text = __( 'Start Learning', 'learnpress' ); } elseif ( ! $courseModel->is_free() ) { $btn_text = __( 'Buy Now', 'learnpress' ); } elseif ( ! empty( $courseModel->get_external_link() ) ) { $btn_text = __( 'View Detail', 'learnpress' ); } } return $btn_text; } }