tab_list_lessons(); $popup_template = sprintf( '', TemplateAJAX::load_content_via_ajax( [ 'id_url' => 'builder-popup-lesson-list', 'lesson_id' => 0, 'html_no_load_ajax_first' => sprintf( '
%s
', esc_html__( 'Loading...', 'learnpress' ) ), ], [ 'class' => BuilderPopupTemplate::class, 'method' => 'render_lesson_popup', ] ) ); $tab = [ 'wrapper' => '
', 'header' => $this->html_header(), 'filter_bar' => $this->html_filter_bar(), 'lessons' => $list_lesson, 'popup_template' => $popup_template, 'wrapper_end' => '
', ]; echo Template::combine_components( $tab ); } public function html_header(): string { $section = [ 'wrapper' => '
', 'title' => sprintf( '

%s

', __( 'Lessons', 'learnpress' ) ), 'actions' => sprintf( '
%s
', CourseBuilderTemplate::instance()->html_btn_add_new() ), 'wrapper_end' => '
', ]; return Template::combine_components( $section ); } public function html_filter_bar(): string { $section = [ 'wrapper_action' => '
', 'search_lesson' => $this->html_search(), 'wrapper_action_end' => '
', ]; return Template::combine_components( $section ); } public function html_search() { $args = lp_archive_skeleton_get_args(); $link_tab = CourseBuilder::get_tab_link( 'lessons' ); $search = [ 'wrapper' => sprintf( '
', $link_tab ), 'search_lesson' => '', 'input' => sprintf( '', __( 'Search', 'learnpress' ), esc_attr( $args['c_search'] ?? '' ) ), 'wrapper_end' => '
', ]; return Template::combine_components( $search ); } public function tab_list_lessons(): string { $content = ''; try { // Query lessons of user $param = lp_archive_skeleton_get_args(); $param['id_url'] = 'tab-list-lessons'; $query_args = array( 'post_type' => LP_LESSON_CPT, 'post_status' => array( 'publish', 'private', 'draft', 'pending', 'trash' ), 'posts_per_page' => 12, 'paged' => $GLOBALS['wp_query']->get( 'paged', 1 ) ? $GLOBALS['wp_query']->get( 'paged', 1 ) : 1, 's' => ! empty( $param['c_search'] ) ? sanitize_text_field( $param['c_search'] ) : '', ); $user_id = get_current_user_id(); $userModel = UserModel::find( $user_id, true ); if ( ! $userModel instanceof UserModel ) { return ''; } if ( ! current_user_can( ADMIN_ROLE ) ) { $query_args['author'] = $user_id; } $query = new WP_Query(); $result = $query->query( $query_args ); $total_lessons = $query->found_posts ?? 0; if ( $total_lessons < 1 ) { unset( $query_args['paged'] ); $count_query = new WP_Query(); $count_query->query( $query_args ); $total_lessons = $count_query->found_posts; } $lessons = array(); if ( $query->have_posts() ) { while ( $query->have_posts() ) { $query->the_post(); $lesson_model = LessonPostModel::find( get_the_ID(), true ); $lessons[] = $lesson_model; } } wp_reset_postdata(); if ( ! empty( $lessons ) ) { $html_lessons = $this->list_lessons( $lessons ); } else { $html_lessons = Template::print_message( sprintf( __( 'No lessons found', 'learnpress' ) ), 'info', false ); } $total_pages = \LP_Database::get_total_pages( $query_args['posts_per_page'], $total_lessons ); $link_tab = CourseBuilder::get_tab_link( 'lessons' ); $data_pagination = [ 'paged' => max( 1, $query_args['paged'] ?? 1 ), 'total_pages' => $total_pages, 'base' => trailingslashit( $link_tab ) . 'page/%#%', 'format' => '', ]; $pagination = Template::instance()->html_pagination( $data_pagination ); $sections = apply_filters( 'learn-press/course-builder/lessons/sections', [ 'wrapper' => '
', 'lessons' => $html_lessons, 'wrapper_end' => '
', 'pagination' => $pagination, ], $lessons, $userModel ); $content = Template::combine_components( $sections ); } catch ( Throwable $e ) { error_log( __METHOD__ . ': ' . $e->getMessage() ); } return $content; } /** * Display list lessons. * * @param $lessons * * @return string */ public function list_lessons( $lessons ): string { $content = ''; try { $html_list_lesson = ''; foreach ( $lessons as $lesson_model ) { $html_list_lesson .= self::render_lesson( $lesson_model ); } $header = '
'; $header .= sprintf( '%s', __( 'Lesson Title', 'learnpress' ) ); $header .= sprintf( '%s', __( 'Courses', 'learnpress' ) ); $header .= sprintf( '%s', __( 'Create Date', 'learnpress' ) ); $header .= sprintf( '%s', __( 'Status', 'learnpress' ) ); $header .= sprintf( '%s', __( 'Preview', 'learnpress' ) ); $header .= sprintf( '%s', __( 'Actions', 'learnpress' ) ); $header .= '
'; $sections = [ 'header' => $header, 'wrapper' => '', ]; $content = Template::combine_components( $sections ); } catch ( Throwable $e ) { error_log( __METHOD__ . ': ' . $e->getMessage() ); } return $content; } /** * Render lesson in course builder * * @param $lesson * @param array $settings * * @return string * @since 4.3.0 * @version 1.0.0 */ public static function render_lesson( LessonPostModel $lesson_model, array $settings = [] ): string { $author = get_user_by( 'ID', $lesson_model->post_author ); $author_name = $author && isset( $author->display_name ) ? $author->display_name : '--'; $lesson = array( 'id' => $lesson_model->get_id(), 'title' => $lesson_model->post_title, 'status' => $lesson_model->post_status, 'courses' => BuilderEditLessonTemplate::instance()->get_assigned( $lesson_model->get_id() ), 'author' => $author_name, 'preview' => get_post_meta( $lesson_model->get_id(), '_lp_preview', true ), 'date_modified' => lp_jwt_prepare_date_response( $lesson_model->post_date_gmt ), ); try { $html_courses = ''; $assigned = '--'; if ( ! empty( $lesson['courses'] ) ) { $courses = is_array( $lesson['courses'] ) && isset( $lesson['courses']['id'] ) ? array( $lesson['courses'] ) : $lesson['courses']; $course_htmls = array(); foreach ( $courses as $course ) { $course_id = $course['id'] ?? 0; $course_title = $course['title'] ?? ''; if ( $course_id && $course_title ) { $course_link = BuilderCourseTemplate::instance()->get_link_edit( $course_id ); $course_htmls[] = sprintf( '%s', esc_url( $course_link ), esc_html( $course_title ) ); } } if ( ! empty( $course_htmls ) ) { $assigned = implode( ', ', $course_htmls ); } } $html_courses = sprintf( '
%s: %s
', __( 'Assigned', 'learnpress' ), $assigned ); $status = $lesson_model->post_status ?? ''; $html_content = apply_filters( 'learn-press/course-builder/list-lessons/item/section/bottom', [ 'title' => sprintf( '

', $lesson_model->get_id(), $lesson['title'] ), 'courses' => $html_courses, 'date' => sprintf( '%s', ! empty( $lesson['date_modified'] ) ? date_i18n( 'm/d/Y', strtotime( $lesson['date_modified'] ) ) : '--' ), 'lesson_status' => ! empty( $status ) ? sprintf( '%1$s', $status ) : '', 'preview' => sprintf( '', $lesson['id'], __( 'Toggle preview', 'learnpress' ), $lesson['preview'] === 'yes' ? 'lp-icon-eye' : 'lp-icon-eye-slash' ), ], $lesson, $settings ); $edit_icon = LP_WP_Filesystem::get_icon_svg( 'ico-cb-edit.svg' ); $more_actions_icon = LP_WP_Filesystem::get_icon_svg( 'ico-cb-more.svg' ); $html_action = apply_filters( 'learn-press/course-builder/list-lessons/item/action', [ 'wrapper' => '
', 'edit' => $status !== PostModel::STATUS_TRASH ? sprintf( '
', $lesson['id'], $edit_icon, __( 'Edit', 'learnpress' ) ) : '', 'action_expanded_button' => sprintf( '', $more_actions_icon ), 'action_expanded_wrapper' => '', 'wrapper_end' => '
', ], $lesson, $settings ); $section = apply_filters( 'learn-press/course-builder/list-lessons/item-li', [ 'wrapper_li' => '
  • ', 'wrapper_div' => sprintf( '
    ', $lesson['id'], $status ), 'lesson_info' => Template::combine_components( $html_content ), 'lesson_action' => Template::combine_components( $html_action ), 'wrapper_div_end' => '
    ', 'wrapper_li_end' => '
  • ', ], $lesson, $settings ); $html_item = Template::combine_components( $section ); } catch ( Throwable $e ) { $html_item = $e->getMessage(); } return $html_item; } }