PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.1
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.1
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 / class-lp-assets.php

class-lp-assets.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.4.1, at inc/class-lp-assets.php

600 lines 17.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class LP_Assets
4 *
5 * @author ThimPress
6 * @package LearnPress/Classes
7 * @version 4.0.1
8 */
9
10 use LearnPress\Services\OpenAiService;
11
12 defined( 'ABSPATH' ) || exit;
13
14 class LP_Assets extends LP_Abstract_Assets {
15 protected static $_instance;
16
17 /**
18 * Constructor
19 */
20 protected function __construct() {
21 parent::__construct();
22
23 add_action( 'wp_print_footer_scripts', array( $this, 'show_overlay' ) );
24 //Note: hook wp_head load before hook wp_enqueue_scripts
25 add_action( 'wp_head', [ $this, 'load_scripts_styles_on_head' ], - 1 );
26 }
27
28 /**
29 * Get default styles in frontend.
30 *
31 * @return array
32 */
33 protected function _get_styles(): array {
34 $is_rtl = is_rtl() ? '-rtl' : '';
35
36 $only_register_css_learnpress = false;
37 if ( wp_is_block_theme() ) {
38 if ( LP_Page_Controller::is_page_courses() ) {
39 $only_register_css_learnpress = true;
40 }
41
42 if ( LP_Page_Controller::is_page_single_course() ) {
43 global $post;
44 setup_postdata( $post );
45 $course_item = LP_Global::course_item();
46 if ( ! $course_item ) {
47 $only_register_css_learnpress = true;
48 }
49 }
50 }
51
52 $styles = apply_filters(
53 'learn-press/frontend-default-styles',
54 array(
55 'learnpress' => new LP_Asset_Key(
56 self::url( 'css/learnpress' . $is_rtl . self::$_min_assets . '.css' ),
57 array(),
58 array(
59 LP_PAGE_COURSES,
60 LP_PAGE_SINGLE_COURSE,
61 LP_PAGE_SINGLE_COURSE_CURRICULUM,
62 LP_PAGE_QUIZ,
63 LP_PAGE_QUESTION,
64 LP_PAGE_CHECKOUT,
65 LP_PAGE_BECOME_A_TEACHER,
66 LP_PAGE_PROFILE,
67 ),
68 $only_register_css_learnpress
69 ),
70 'lp-instructor' => new LP_Asset_Key(
71 self::url( 'css/instructor' . $is_rtl . self::$_min_assets . '.css' ),
72 array(),
73 array(),
74 1
75 ),
76 'lp-instructors' => new LP_Asset_Key(
77 self::url( 'css/instructors' . $is_rtl . self::$_min_assets . '.css' ),
78 [],
79 [],
80 1
81 ),
82 'learnpress-widgets' => new LP_Asset_Key(
83 self::url( 'css/widgets' . $is_rtl . self::$_min_assets . '.css' ),
84 array(),
85 array(),
86 0
87 ),
88 'lp-edit-curriculum' => new LP_Asset_Key(
89 $this->url( 'css/edit-curriculum' . $is_rtl . self::$_min_assets . '.css' ),
90 [],
91 [],
92 1
93 ),
94 'lp-edit-quiz' => new LP_Asset_Key(
95 $this->url( 'css/edit-quiz' . $is_rtl . self::$_min_assets . '.css' ),
96 [],
97 [],
98 1
99 ),
100 'lp-edit-question' => new LP_Asset_Key(
101 $this->url( 'css/edit-question' . $is_rtl . self::$_min_assets . '.css' ),
102 [],
103 [],
104 1
105 ),
106 'lp-course-builder' => new LP_Asset_Key(
107 self::url( 'css/course-builder' . $is_rtl . self::$_min_assets . '.css' ),
108 array(),
109 array( LP_PAGE_COURSE_BUILDER ),
110 0
111 ),
112 'lp-tom-select' => new LP_Asset_Key(
113 self::url( 'src/css/vendor/tom-select.min.css' ),
114 [],
115 [],
116 0
117 ),
118 'lp-ai-assistant' => new LP_Asset_Key(
119 self::url( 'css/frontend/ai-assistant' . self::$_min_assets . '.css' ),
120 array(),
121 array( LP_PAGE_SINGLE_COURSE_CURRICULUM ),
122 1
123 ),
124 )
125 );
126
127 if ( wp_is_block_theme() ) {
128 $styles['learnpress-block'] = new LP_Asset_Key(
129 self::url( 'css/learnpress-block' . $is_rtl . self::$_min_assets . '.css' ),
130 array(),
131 array(),
132 0
133 );
134 }
135
136 return $styles;
137 }
138
139 /**
140 * Set localize script data
141 *
142 * @return array
143 */
144 public function _get_script_data(): array {
145 $localize_script = [
146 'lp-global' => array(
147 //'url' => learn_press_get_current_url(),
148 'siteurl' => site_url(),
149 'ajax' => admin_url( 'admin-ajax.php' ),
150 'courses_url' => learn_press_get_page_link( 'courses' ),
151 'post_id' => get_the_ID(),
152 'user_id' => get_current_user_id(),
153 'theme' => get_stylesheet(),
154 'localize' => array(
155 'button_ok' => esc_html__( 'OK', 'learnpress' ),
156 'button_cancel' => esc_html__( 'Cancel', 'learnpress' ),
157 'button_yes' => esc_html__( 'Yes', 'learnpress' ),
158 'button_no' => esc_html__( 'No', 'learnpress' ),
159 ),
160 'lp_rest_url' => get_rest_url(),
161 //Todo: should create nonce for guest and user separation
162 'nonce' => wp_create_nonce( 'wp_rest' ),
163 'option_enable_popup_confirm_finish' => LP_Settings::get_option( 'enable_popup_confirm_finish', 'yes' ),
164 'is_course_archive' => LP_Page_Controller::is_page_courses(),
165 'lpArchiveSkeleton' => lp_archive_skeleton_get_args(),
166 'lpArchiveLoadAjax' => LP_Settings_Courses::is_ajax_load_courses() ? 1 : 0,
167 'lpArchiveNoLoadAjaxFirst' => LP_Settings_Courses::is_ajax_load_courses() && LP_Settings_Courses::is_no_load_ajax_first_courses() ? 1 : 0,
168 'lpArchivePaginationType' => LP_Settings::get_option( 'course_pagination_type' ),
169 'noLoadCoursesJs' => LP_Settings::theme_no_support_load_courses_ajax() ? 1 : 0,
170 ),
171 'lp-checkout' => array(
172 'ajaxurl' => home_url( '/' ),
173 //'user_checkout' => LP_Checkout::instance()->get_checkout_email(),
174 'i18n_processing' => esc_html__( 'Processing', 'learnpress' ),
175 'i18n_redirecting' => esc_html__( 'Redirecting', 'learnpress' ),
176 'i18n_invalid_field' => esc_html__( 'Invalid field', 'learnpress' ),
177 'i18n_unknown_error' => esc_html__( 'Unknown error', 'learnpress' ),
178 'i18n_place_order' => esc_html__( 'Place order', 'learnpress' ),
179 ),
180 'lp-profile' => array(
181 'text_upload' => __( 'Upload', 'learnpress' ),
182 'text_replace' => __( 'Replace', 'learnpress' ),
183 'text_remove' => __( 'Remove', 'learnpress' ),
184 'text_save' => __( 'Save', 'learnpress' ),
185 'avatar_dimensions' => learn_press_get_avatar_thumb_size(),
186 'default_avatar' => get_avatar_url( get_current_user_id() ),
187 ),
188 'lp-quiz' => learn_press_single_quiz_args(),
189 ];
190
191 return apply_filters( 'learnpress/frontend/localize_script', $localize_script );
192 }
193
194 /**
195 * Localize data for all page frontend.
196 *
197 * @return array
198 */
199 public function localize_data_global(): array {
200 $cover_image_dimensions = LP_Settings::get_option(
201 'cover_image_dimensions',
202 array(
203 'width' => 1290,
204 'height' => 250,
205 )
206 );
207 $aspectRatio = $cover_image_dimensions['width'] / $cover_image_dimensions['height'];
208
209 return apply_filters(
210 'learn-press/frontend/localize-data-global',
211 [
212 'site_url' => site_url(),
213 'user_id' => get_current_user_id(),
214 'theme' => get_stylesheet(),
215 'lp_rest_url' => get_rest_url(),
216 'nonce' => wp_create_nonce( 'wp_rest' ),
217 'is_course_archive' => LP_Page_Controller::is_page_courses(),
218 'courses_url' => learn_press_get_page_link( 'courses' ),
219 'urlParams' => lp_archive_skeleton_get_args(),
220 'lp_version' => LearnPress::instance()->version,
221 'lp_rest_load_ajax' => get_rest_url( null, 'lp/v1/load_content_via_ajax/' ), // @deprecated 4.3.0
222 'ajaxUrl' => admin_url( 'admin-ajax.php' ),
223 'lpAjaxUrl' => LP_Settings::url_handle_lp_ajax(),
224 'coverImageRatio' => $aspectRatio,
225 'toast' => [
226 'gravity' => 'bottom',
227 'position' => 'center',
228 'duration' => 3000,
229 'close' => 1,
230 'stopOnFocus' => 1,
231 'classPrefix' => 'lp-toast',
232 ],
233 'i18n' => [
234 'yes' => esc_html__( 'Yes' ),
235 'cancel' => esc_html__( 'Cancel' ),
236 'generate_with_ai' => esc_html__( 'Generate with AI', 'learnpress' ),
237 'confirm_close_ai' => esc_html__( 'Are you sure you want to close? Generate data will stop.', 'learnpress' ),
238 ],
239 'enable_open_ai' => OpenAiService::instance()->is_enable()
240 && ! empty( LP_Settings::get_option( 'open_ai_secret_key', '' ) ),
241 ]
242 );
243 }
244
245 /**
246 * Localize data for all page frontend.
247 *
248 * @return array
249 */
250 public function localize_data_courses(): array {
251 return apply_filters(
252 'learn-press/frontend/localize-script/courses',
253 [
254 'lpArchiveLoadAjax' => LP_Settings_Courses::is_ajax_load_courses() ? 1 : 0,
255 'lpArchiveNoLoadAjaxFirst' => LP_Settings_Courses::is_ajax_load_courses() && LP_Settings_Courses::is_no_load_ajax_first_courses() ? 1 : 0,
256 'lpArchivePaginationType' => LP_Settings::get_option( 'course_pagination_type' ),
257 'noLoadCoursesJs' => LP_Settings::theme_no_support_load_courses_ajax() ? 1 : 0,
258 ]
259 );
260 }
261
262 /**
263 * Config load scripts
264 *
265 * @return array
266 */
267 public function _get_scripts(): array {
268 $wp_js = array(
269 'jquery',
270 'wp-element',
271 'wp-compose',
272 'wp-data',
273 'wp-hooks',
274 'wp-api-fetch',
275 'lodash',
276 );
277
278 $scripts = apply_filters(
279 'learn-press/frontend-default-scripts',
280 array(
281 'vue-libs' => new LP_Asset_Key(
282 self::url( 'src/js/vendor/vue/vue_libs_special.min.js' )
283 ),
284 'lp-modal' => new LP_Asset_Key(
285 self::url( 'js/dist/frontend/modal' . self::$_min_assets . '.js' ),
286 array( 'jquery' )
287 ),
288 // lp-plugins-all use only for FE, when FE 2 release will remove it.
289 'lp-plugins-all' => new LP_Asset_Key( self::url( 'js/vendor/plugins.all.min.js' ) ),
290 'lp-global' => new LP_Asset_Key(
291 self::url( self::$_folder_source . 'js/global' . self::$_min_assets . '.js' ),
292 array( 'jquery', 'underscore', 'utils' )
293 ),
294 'lp-utils' => new LP_Asset_Key(
295 self::url( 'js/dist/utils' . self::$_min_assets . '.js' ),
296 array( 'jquery' )
297 ),
298 'lp-load-ajax' => new LP_Asset_Key(
299 self::url( 'js/dist/loadAJAX' . self::$_min_assets . '.js' ),
300 [ 'wp-hooks' ],
301 [],
302 0,
303 0,
304 '',
305 [ 'strategy' => 'async' ]
306 ),
307 'lp-checkout' => new LP_Asset_Key(
308 self::url( 'js/dist/frontend/checkout' . self::$_min_assets . '.js' ),
309 [],
310 [ LP_PAGE_CHECKOUT ],
311 0,
312 0,
313 '',
314 [ 'strategy' => 'async' ]
315 ),
316 'lp-data-controls' => new LP_Asset_Key(
317 self::url( 'js/dist/js/data-controls' . self::$_min_assets . '.js' ),
318 array_merge( $wp_js, array( 'lp-global' ) )
319 ),
320 'lp-config' => new LP_Asset_Key(
321 self::url( 'js/dist/frontend/lp-configs' . self::$_min_assets . '.js' ),
322 array_merge( $wp_js, array( 'lp-global' ) )
323 ),
324 // 'lp-lesson' => new LP_Asset_Key( self::url( self::$_folder_source .'js/frontend/lesson' . self::$_min_assets . '.js' ) ),
325 'lp-question-types' => new LP_Asset_Key(
326 self::url( 'js/dist/frontend/question-types' . self::$_min_assets . '.js' ),
327 array_merge( $wp_js, array( 'lp-global' ) ),
328 array(),
329 1,
330 1
331 ),
332 'lp-single-curriculum' => new LP_Asset_Key(
333 self::url( 'js/dist/frontend/single-curriculum' . self::$_min_assets . '.js' ),
334 array_merge(
335 $wp_js,
336 array(
337 'lp-global',
338 'lp-utils',
339 )
340 ),
341 array( LP_PAGE_SINGLE_COURSE_CURRICULUM ),
342 0,
343 1,
344 '',
345 [ 'strategy' => 'defer' ]
346 ),
347 'lp-curriculum' => new LP_Asset_Key(
348 self::url( 'js/dist/frontend/curriculum' . self::$_min_assets . '.js' ),
349 [],
350 array( LP_PAGE_SINGLE_COURSE_CURRICULUM, LP_PAGE_SINGLE_COURSE ),
351 0,
352 0,
353 '',
354 [ 'strategy' => 'async' ]
355 ),
356 'lp-quiz' => new LP_Asset_Key(
357 self::url( 'js/dist/frontend/quiz' . self::$_min_assets . '.js' ),
358 array_merge(
359 $wp_js,
360 array(
361 'wp-i18n',
362 'lp-global',
363 'lp-utils',
364 'lp-data-controls',
365 'lp-question-types',
366 'lp-modal',
367 'lp-config',
368 'lp-single-curriculum',
369 )
370 ),
371 array( LP_PAGE_QUIZ ),
372 0,
373 1,
374 '',
375 [ 'strategy' => 'defer' ]
376 ),
377 'lp-single-course' => new LP_Asset_Key(
378 self::url( 'js/dist/frontend/single-course' . self::$_min_assets . '.js' ),
379 array_merge(
380 $wp_js,
381 array(
382 'lp-global',
383 'lp-utils',
384 )
385 ),
386 array( LP_PAGE_SINGLE_COURSE ),
387 0,
388 0,
389 '',
390 [ 'strategy' => 'defer' ]
391 ),
392 /*'lp-courses' => new LP_Asset_Key(
393 self::url( 'js/dist/frontend/courses' . self::$_min_assets . '.js' ),
394 array(
395 'lp-global',
396 'wp-hooks',
397 ), // when Eduma v5.3.6 release a long time, will be remove lp-global.
398 array(),
399 1,
400 0,
401 '',
402 [ 'strategy' => 'defer' ]
403 ),*/
404 'lp-courses-v2' => new LP_Asset_Key(
405 self::url( 'js/dist/frontend/courses-v2' . self::$_min_assets . '.js' ),
406 [ 'utils', 'wp-hooks' ], // dependency utils of wp, because js is using wpCookies
407 [ LP_PAGE_COURSES ],
408 0,
409 0,
410 '',
411 [ 'strategy' => 'async' ]
412 ),
413 'lp-instructors' => new LP_Asset_Key(
414 self::url( 'js/dist/frontend/instructors' . self::$_min_assets . '.js' ),
415 [ 'lp-global' ],
416 [],
417 1,
418 0,
419 '',
420 [ 'strategy' => 'defer' ]
421 ),
422 'lp-profile' => new LP_Asset_Key(
423 self::url( 'js/dist/frontend/profile' . self::$_min_assets . '.js' ),
424 [ 'lp-load-ajax', 'wp-i18n' ],
425 array( LP_PAGE_PROFILE ),
426 0,
427 0,
428 '',
429 [ 'strategy' => 'defer' ]
430 ),
431 'lp-list-students-enrolled' => new LP_Asset_Key(
432 self::url( 'dist/js/admin/list-students-enrolled' . self::$_min_assets . '.js' ),
433 [ 'lp-load-ajax' ],
434 [ LP_PAGE_PROFILE ],
435 0,
436 0,
437 '',
438 [ 'strategy' => 'async' ]
439 ),
440 'lp-widgets' => new LP_Asset_Key(
441 self::url( 'js/dist/frontend/widgets' . self::$_min_assets . '.js' ),
442 [ 'lp-course-filter' ],
443 array(),
444 1,
445 0,
446 '',
447 [ 'strategy' => 'async' ]
448 ),
449 'lp-become-a-teacher' => new LP_Asset_Key(
450 self::url( 'js/dist/frontend/become-teacher' . self::$_min_assets . '.js' ),
451 array( 'jquery' ),
452 array( LP_PAGE_BECOME_A_TEACHER ),
453 0,
454 1,
455 '',
456 [ 'strategy' => 'defer' ]
457 ),
458 'lp-course-filter' => new LP_Asset_Key(
459 self::url( 'js/dist/frontend/course-filter' . self::$_min_assets . '.js' ),
460 array(),
461 array(),
462 1,
463 0,
464 '',
465 [ 'strategy' => 'async' ]
466 ),
467 'lp-course-builder' => new LP_Asset_Key(
468 self::url( 'js/dist/frontend/course-builder' . self::$_min_assets . '.js' ),
469 array( 'wp-tinymce', 'editor', 'lp-load-ajax' ),
470 array( LP_PAGE_COURSE_BUILDER ),
471 0,
472 0,
473 '',
474 [ 'strategy' => 'defer' ]
475 ),
476 'lp-ai-assistant' => new LP_Asset_Key(
477 self::url( 'js/dist/frontend/ai-assistant' . self::$_min_assets . '.js' ),
478 array(),
479 array( LP_PAGE_SINGLE_COURSE_CURRICULUM ),
480 1,
481 1,
482 ),
483 )
484 );
485
486 // Dequeue script 'smoothPageScroll' on item details, it makes can't scroll, when rewrite page item detail, can check to remove.
487 if ( LP_PAGE_SINGLE_COURSE_CURRICULUM === LP_Page_Controller::page_current() ||
488 LP_PAGE_QUIZ === LP_Page_Controller::page_current() ||
489 LP_PAGE_QUESTION === LP_Page_Controller::page_current() ) {
490 wp_dequeue_script( 'smoothPageScroll' );
491 }
492
493 return $scripts;
494 }
495
496 /**
497 * Load assets
498 *
499 * @author tungnx
500 * @version 1.0.1
501 * @since 3.2.8
502 */
503 public function load_scripts() {
504 $page_current = LP_Page_Controller::page_current();
505 $this->handle_js( $page_current );
506 $this->handle_style( $page_current );
507
508 do_action( 'learn-press/after-enqueue-scripts' );
509 }
510
511 /**
512 * Add javascript to head
513 * Add style to head
514 *
515 * @return void
516 */
517 public function load_scripts_styles_on_head() {
518 $this->load_scripts_on_head();
519 $this->load_styles_on_head();
520 }
521
522 /**
523 * Load scripts on head
524 * @return void
525 */
526 public function load_scripts_on_head() {
527 LP_Helper::print_inline_script_tag( 'lpData', $this->localize_data_global(), [ 'id' => 'lpData' ] );
528 LP_Helper::print_inline_script_tag( 'lpSettingCourses', $this->localize_data_courses(), [ 'id' => 'lpSettingCourses' ] );
529 }
530
531 /**
532 * Load styles on head
533 * @return void
534 */
535 public function load_styles_on_head() {
536 $max_width = esc_html( LP_Settings::get_option( 'width_container', '1290px' ) );
537 $padding_container = apply_filters( 'learn-press/container-padding-width', '1rem' );
538 $primary_color = esc_html( LP_Settings::get_option( 'primary_color' ) );
539 $secondary_color = esc_html( LP_Settings::get_option( 'secondary_color' ) );
540 ?>
541 <style id="learn-press-custom-css">
542 :root {
543 --lp-container-max-width: <?php echo $max_width; ?>;
544 --lp-cotainer-padding: <?php echo $padding_container; ?>;
545 --lp-primary-color: <?php echo ! empty( $primary_color ) ? $primary_color : '#ffb606'; ?>;
546 --lp-secondary-color: <?php echo ! empty( $secondary_color ) ? $secondary_color : '#442e66'; ?>;
547 }
548 </style>
549 <?php
550 }
551
552 /**
553 * Add lp overlay
554 *
555 * @since 3.2.8
556 * @version 1.0.1
557 * @author tungnx
558 */
559 public function show_overlay() {
560 $page_current = LP_Page_Controller::page_current();
561 if ( ! in_array(
562 $page_current,
563 array( LP_PAGE_SINGLE_COURSE_CURRICULUM, LP_PAGE_SINGLE_COURSE, LP_PAGE_QUIZ )
564 ) ) {
565 return;
566 }
567
568 if ( 'yes' !== LP_Settings::get_option( 'enable_popup_confirm_finish', 'yes' ) ) {
569 return;
570 }
571
572 echo '<div class="lp-overlay" style="display: none">';
573 learn_press_get_template( 'global/lp-modal-overlay' );
574 echo '</div>';
575 }
576
577 public static function instance() {
578 if ( is_admin() ) {
579 return null;
580 }
581
582 if ( self::$_instance == null ) {
583 self::$_instance = new self();
584 }
585
586 return self::$_instance;
587 }
588 }
589
590 /**
591 * Shortcut function to get instance of LP_Assets
592 *
593 * @return LP_Assets|null
594 */
595 function learn_press_assets() {
596 return LP_Assets::instance();
597 }
598
599 learn_press_assets();
600