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 / class-lp-assets.php

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

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