PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.3.8
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.3.8
4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 4.2.1 All 138 releases
learnpress / inc / TemplateHooks / CourseBuilder / Lesson / BuilderListLessonsTemplate.php

BuilderListLessonsTemplate.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.3.8, at inc/TemplateHooks/CourseBuilder/Lesson/BuilderListLessonsTemplate.php

375 lines 13.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Template hooks Tab Course in Course Builder.
4 *
5 * @since 4.3.0
6 * @version 1.0.0
7 */
8
9 namespace LearnPress\TemplateHooks\CourseBuilder\Lesson;
10
11 use LearnPress\CourseBuilder\CourseBuilder;
12 use LearnPress\Helpers\Singleton;
13 use LearnPress\Helpers\Template;
14 use LearnPress\Models\LessonPostModel;
15 use LearnPress\Models\PostModel;
16 use LearnPress\Models\UserModel;
17 use LearnPress\TemplateHooks\CourseBuilder\BuilderPopupTemplate;
18 use LearnPress\TemplateHooks\CourseBuilder\Course\BuilderCourseTemplate;
19 use LearnPress\TemplateHooks\CourseBuilder\CourseBuilderTemplate;
20 use LearnPress\TemplateHooks\TemplateAJAX;
21 use LP_WP_Filesystem;
22 use Throwable;
23 use WP_Query;
24
25 class BuilderListLessonsTemplate {
26 use Singleton;
27
28 public function init() {
29 add_action( 'learn-press/course-builder/list-lessons/layout', [ $this, 'layout' ] );
30 }
31
32 public function layout( array $data = [] ) {
33 $list_lesson = $this->tab_list_lessons();
34 $popup_template = sprintf(
35 '<script type="text/template" id="lp-tmpl-builder-popup-lesson-list"><div class="lp-builder-popup-overlay"></div><div class="lp-builder-popup lp-builder-popup--loading">%s</div></script>',
36 TemplateAJAX::load_content_via_ajax(
37 [
38 'id_url' => 'builder-popup-lesson-list',
39 'lesson_id' => 0,
40 'html_no_load_ajax_first' => sprintf(
41 '<div class="lp-builder-popup__loader"><div class="lp-loading-circle"></div><span>%s</span></div>',
42 esc_html__( 'Loading...', 'learnpress' )
43 ),
44 ],
45 [
46 'class' => BuilderPopupTemplate::class,
47 'method' => 'render_lesson_popup',
48 ]
49 )
50 );
51
52 $tab = [
53 'wrapper' => '<div class="cb-tab-lesson">',
54 'header' => $this->html_header(),
55 'filter_bar' => $this->html_filter_bar(),
56 'lessons' => $list_lesson,
57 'popup_template' => $popup_template,
58 'wrapper_end' => '</div>',
59 ];
60
61 echo Template::combine_components( $tab );
62 }
63
64 public function html_header(): string {
65 $section = [
66 'wrapper' => '<div class="cb-tab-header">',
67 'title' => sprintf( '<h2 class="lp-cb-tab__title">%s</h2>', __( 'Lessons', 'learnpress' ) ),
68 'actions' => sprintf(
69 '<div class="cb-tab-header-actions" style="display:flex;align-items:center;gap:8px;">%s</div>',
70 CourseBuilderTemplate::instance()->html_btn_add_new()
71 ),
72 'wrapper_end' => '</div>',
73 ];
74
75 return Template::combine_components( $section );
76 }
77
78 public function html_filter_bar(): string {
79 $section = [
80 'wrapper_action' => '<div class="cb-tab-lesson__action">',
81 'search_lesson' => $this->html_search(),
82 'wrapper_action_end' => '</div>',
83 ];
84
85 return Template::combine_components( $section );
86 }
87
88 public function html_search() {
89 $args = lp_archive_skeleton_get_args();
90 $link_tab = CourseBuilder::get_tab_link( 'lessons' );
91
92 $search = [
93 'wrapper' => sprintf( '<form class="cb-search-form" method="get" action="%s">', $link_tab ),
94 'search_lesson' => '<button class="lp-button cb-search-btn" type="submit"> <i class="lp-icon-search"> </i></button>',
95 'input' => sprintf( '<input class="cb-input-search-lesson" type="search" placeholder="%s" name="c_search" value="%s">', __( 'Search', 'learnpress' ), $args['c_search'] ?? '' ),
96 'wrapper_end' => '</form>',
97 ];
98
99 return Template::combine_components( $search );
100 }
101
102 public function tab_list_lessons(): string {
103 $content = '';
104
105 try {
106 // Query lessons of user
107 $param = lp_archive_skeleton_get_args();
108 $param['id_url'] = 'tab-list-lessons';
109
110 $query_args = array(
111 'post_type' => LP_LESSON_CPT,
112 'post_status' => array( 'publish', 'private', 'draft', 'pending', 'trash' ),
113 'posts_per_page' => 12,
114 'paged' => $GLOBALS['wp_query']->get( 'paged', 1 ) ? $GLOBALS['wp_query']->get( 'paged', 1 ) : 1,
115 's' => ! empty( $param['c_search'] ) ? sanitize_text_field( $param['c_search'] ) : '',
116
117 );
118
119 $user_id = get_current_user_id();
120 $userModel = UserModel::find( $user_id, true );
121 if ( ! $userModel instanceof UserModel ) {
122 return '';
123 }
124
125 if ( ! current_user_can( ADMIN_ROLE ) ) {
126 $query_args['author'] = $user_id;
127 }
128
129 $query = new WP_Query();
130 $result = $query->query( $query_args );
131 $total_lessons = $query->found_posts ?? 0;
132
133 if ( $total_lessons < 1 ) {
134 unset( $query_args['paged'] );
135 $count_query = new WP_Query();
136 $count_query->query( $query_args );
137 $total_lessons = $count_query->found_posts;
138 }
139
140 $lessons = array();
141
142 if ( $query->have_posts() ) {
143 while ( $query->have_posts() ) {
144 $query->the_post();
145
146 $lesson_model = LessonPostModel::find( get_the_ID(), true );
147 $lessons[] = $lesson_model;
148 }
149 }
150 wp_reset_postdata();
151
152 if ( ! empty( $lessons ) ) {
153 $html_lessons = $this->list_lessons( $lessons );
154 } else {
155 $html_lessons = Template::print_message(
156 sprintf( __( 'No lessons found', 'learnpress' ) ),
157 'info',
158 false
159 );
160 }
161
162 $total_pages = \LP_Database::get_total_pages( $query_args['posts_per_page'], $total_lessons );
163 $link_tab = CourseBuilder::get_tab_link( 'lessons' );
164 $data_pagination = [
165 'paged' => max( 1, $query_args['paged'] ?? 1 ),
166 'total_pages' => $total_pages,
167 'base' => trailingslashit( $link_tab ) . 'page/%#%',
168 'format' => '',
169 ];
170
171 $pagination = Template::instance()->html_pagination( $data_pagination );
172
173 $sections = apply_filters(
174 'learn-press/course-builder/lessons/sections',
175 [
176 'wrapper' => '<div class="courses-builder__lesson-tab learn-press-lessons">',
177 'lessons' => $html_lessons,
178 'wrapper_end' => '</div>',
179 'pagination' => $pagination,
180 ],
181 $lessons,
182 $userModel
183 );
184
185 $content = Template::combine_components( $sections );
186 } catch ( Throwable $e ) {
187 error_log( __METHOD__ . ': ' . $e->getMessage() );
188 }
189
190 return $content;
191 }
192
193 /**
194 * Display list lessons.
195 *
196 * @param $lessons
197 *
198 * @return string
199 */
200 public function list_lessons( $lessons ): string {
201 $content = '';
202
203 try {
204 $html_list_lesson = '';
205 foreach ( $lessons as $lesson_model ) {
206 $html_list_lesson .= self::render_lesson( $lesson_model );
207 }
208
209 $header = '<div class="cb-list-table-header">';
210 $header .= sprintf( '<span>%s</span>', __( 'Lesson Title', 'learnpress' ) );
211 $header .= sprintf( '<span>%s</span>', __( 'Courses', 'learnpress' ) );
212 $header .= sprintf( '<span>%s</span>', __( 'Create Date', 'learnpress' ) );
213 $header .= sprintf( '<span>%s</span>', __( 'Status', 'learnpress' ) );
214 $header .= sprintf( '<span>%s</span>', __( 'Preview', 'learnpress' ) );
215 $header .= sprintf( '<span>%s</span>', __( 'Actions', 'learnpress' ) );
216 $header .= '</div>';
217
218 $sections = [
219 'header' => $header,
220 'wrapper' => '<ul class="cb-list-lesson">',
221 'list_lesson' => $html_list_lesson,
222 'wrapper_end' => '</ul>',
223 ];
224
225 $content = Template::combine_components( $sections );
226 } catch ( Throwable $e ) {
227 error_log( __METHOD__ . ': ' . $e->getMessage() );
228 }
229
230 return $content;
231 }
232
233 /**
234 * Render lesson in course builder
235 *
236 * @param $lesson
237 * @param array $settings
238 *
239 * @return string
240 * @since 4.3.0
241 * @version 1.0.0
242 */
243 public static function render_lesson( LessonPostModel $lesson_model, array $settings = [] ): string {
244 $author = get_user_by( 'ID', $lesson_model->post_author );
245 $author_name = $author && isset( $author->display_name ) ? $author->display_name : '--';
246
247 $lesson = array(
248 'id' => $lesson_model->get_id(),
249 'title' => $lesson_model->post_title,
250 'status' => $lesson_model->post_status,
251 'courses' => BuilderEditLessonTemplate::instance()->get_assigned( $lesson_model->get_id() ),
252 'author' => $author_name,
253 'preview' => get_post_meta( $lesson_model->get_id(), '_lp_preview', true ),
254 'date_modified' => lp_jwt_prepare_date_response( $lesson_model->post_date_gmt ),
255 );
256
257 try {
258 $html_courses = '';
259 $assigned = '--';
260 if ( ! empty( $lesson['courses'] ) ) {
261 $courses = is_array( $lesson['courses'] ) && isset( $lesson['courses']['id'] )
262 ? array( $lesson['courses'] )
263 : $lesson['courses'];
264
265 $course_htmls = array();
266 foreach ( $courses as $course ) {
267 $course_id = $course['id'] ?? 0;
268 $course_title = $course['title'] ?? '';
269
270 if ( $course_id && $course_title ) {
271 $course_link = BuilderCourseTemplate::instance()->get_link_edit( $course_id );
272 $course_htmls[] = sprintf(
273 '<a href="%s" target="_blank">%s</a>',
274 esc_url( $course_link ),
275 esc_html( $course_title )
276 );
277 }
278 }
279
280 if ( ! empty( $course_htmls ) ) {
281 $assigned = implode( ', ', $course_htmls );
282 }
283 }
284
285 $html_courses = sprintf(
286 '<div class="lesson-assigned-courses"><span class="label">%s:</span> %s</div>',
287 __( 'Assigned', 'learnpress' ),
288 $assigned
289 );
290
291 $status = $lesson_model->post_status ?? '';
292
293 $html_content = apply_filters(
294 'learn-press/course-builder/list-lessons/item/section/bottom',
295 [
296 'title' => sprintf(
297 '<h3 class="wap-lesson-title"><button data-popup-lesson="%1$s" data-popup-type="lesson" data-popup-id="%1$s" data-template="#lp-tmpl-builder-popup-lesson-list">%2$s</button></h3>',
298 $lesson_model->get_id(),
299 $lesson['title']
300 ),
301 'courses' => $html_courses,
302 'date' => sprintf( '<span class="lesson__date">%s</span>', ! empty( $lesson['date_modified'] ) ? date_i18n( 'm/d/Y', strtotime( $lesson['date_modified'] ) ) : '--' ),
303 'lesson_status' => ! empty( $status ) ? sprintf( '<span class="lesson-status %1$s">%1$s</span>', $status ) : '<span></span>',
304 'preview' => sprintf(
305 '<span class="lesson__preview lp-button lp-btn-set-preview-item" data-id="%s" title="%s"><a class="%s"></a></span>',
306 $lesson['id'],
307 __( 'Toggle preview', 'learnpress' ),
308 $lesson['preview'] === 'yes' ? 'lp-icon-eye' : 'lp-icon-eye-slash'
309 ),
310 ],
311 $lesson,
312 $settings
313 );
314
315 $edit_icon = LP_WP_Filesystem::get_icon_svg( 'ico-cb-edit.svg' );
316 $more_actions_icon = LP_WP_Filesystem::get_icon_svg( 'ico-cb-more.svg' );
317
318 $html_action = apply_filters(
319 'learn-press/course-builder/list-lessons/item/action',
320 [
321 'wrapper' => '<div class="lesson-action">',
322 'edit' => $status !== PostModel::STATUS_TRASH ? sprintf(
323 '<div class="lesson-action-editor"><button class="lp-button btn-edit-lesson lesson-edit-permalink" data-popup-lesson="%1$s" data-popup-type="lesson" data-popup-id="%1$s" data-template="#lp-tmpl-builder-popup-lesson-list">%2$s %3$s</button></div>',
324 $lesson['id'],
325 $edit_icon,
326 __( 'Edit', 'learnpress' )
327 ) : '',
328 'action_expanded_button' => sprintf(
329 '<button type="button" class="lp-button lesson-action-expanded">%s</button>',
330 $more_actions_icon
331 ),
332 'action_expanded_wrapper' => '<div style="display:none;" class="lesson-action-expanded__items">',
333 'action_expanded_duplicate' => sprintf( '<span class="lp-button lesson-action-expanded__duplicate" data-title="%s" data-content="%s">%s</span>', __( 'Are you sure?', 'learnpress' ), __( 'Are you sure you want to duplicate this lesson?', 'learnpress' ), __( 'Duplicate', 'learnpress' ) ),
334 'action_expanded_publish' => sprintf( '<span class="lp-button lesson-action-expanded__publish">%s</span>', __( 'Publish', 'learnpress' ) ),
335 'action_expanded_trash' => sprintf(
336 '<span class="lp-button lesson-action-expanded__trash"%s>%s</span>',
337 $status === 'trash' ? ' style="display:none"' : '',
338 __( 'Trash', 'learnpress' )
339 ),
340 'action_expanded_restore' => sprintf(
341 '<span class="lp-button lesson-action-expanded__restore"%s>%s</span>',
342 $status !== 'trash' ? ' style="display:none"' : '',
343 __( 'Restore', 'learnpress' )
344 ),
345 'action_expanded_delete' => sprintf( '<span class="lp-button lesson-action-expanded__delete" data-title="%s" data-content="%s">%s</span>', __( 'Are you sure?', 'learnpress' ), __( 'Are you sure you want to delete this lesson? This action cannot be undone.', 'learnpress' ), __( 'Delete', 'learnpress' ) ),
346 'action_expanded_wrapper_end' => '</div>',
347 'wrapper_end' => '</div>',
348 ],
349 $lesson,
350 $settings
351 );
352
353 $section = apply_filters(
354 'learn-press/course-builder/list-lessons/item-li',
355 [
356 'wrapper_li' => '<li class="lesson">',
357 'wrapper_div' => sprintf( '<div class="lesson-item" data-lesson-id="%s" data-status="%s">', $lesson['id'], $status ),
358 'lesson_info' => Template::combine_components( $html_content ),
359 'lesson_action' => Template::combine_components( $html_action ),
360 'wrapper_div_end' => '</div>',
361 'wrapper_li_end' => '</li>',
362 ],
363 $lesson,
364 $settings
365 );
366
367 $html_item = Template::combine_components( $section );
368 } catch ( Throwable $e ) {
369 $html_item = $e->getMessage();
370 }
371
372 return $html_item;
373 }
374 }
375