| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class LP_Assets |
| 4 |
* |
| 5 |
* @author ThimPress |
| 6 |
* @package LearnPress/Classes |
| 7 |
* @version 4.0.1 |
| 8 |
*/ |
| 9 |
|
| 10 |
defined( 'ABSPATH' ) || exit; |
| 11 |
|
| 12 |
class LP_Assets extends LP_Abstract_Assets { |
| 13 |
protected static $_instance; |
| 14 |
|
| 15 |
/** |
| 16 |
* Constructor |
| 17 |
*/ |
| 18 |
protected function __construct() { |
| 19 |
parent::__construct(); |
| 20 |
|
| 21 |
add_action( 'wp_print_footer_scripts', array( $this, 'show_overlay' ) ); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Get default styles in frontend. |
| 26 |
* |
| 27 |
* @return array |
| 28 |
*/ |
| 29 |
protected function _get_styles(): array { |
| 30 |
$is_rtl = is_rtl() ? '-rtl' : ''; |
| 31 |
|
| 32 |
return apply_filters( |
| 33 |
'learn-press/frontend-default-styles', |
| 34 |
array( |
| 35 |
'font-awesome-5-all' => new LP_Asset_Key( |
| 36 |
self::url( 'src/css/vendor/font-awesome-5.min.css' ), |
| 37 |
array(), |
| 38 |
array() |
| 39 |
), |
| 40 |
'learnpress' => new LP_Asset_Key( |
| 41 |
self::url( 'css/learnpress' . $is_rtl . self::$_min_assets . '.css' ), |
| 42 |
array( 'font-awesome-5-all' ), |
| 43 |
array( LP_PAGE_COURSES, LP_PAGE_SINGLE_COURSE, LP_PAGE_SINGLE_COURSE_CURRICULUM, LP_PAGE_QUIZ, LP_PAGE_QUESTION, LP_PAGE_CHECKOUT, LP_PAGE_BECOME_A_TEACHER, LP_PAGE_PROFILE ), |
| 44 |
0 |
| 45 |
), |
| 46 |
'learnpress-widgets' => new LP_Asset_Key( |
| 47 |
self::url( 'css/widgets' . $is_rtl . self::$_min_assets . '.css' ), |
| 48 |
array(), |
| 49 |
array(), |
| 50 |
0 |
| 51 |
), |
| 52 |
) |
| 53 |
); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Set localize script data |
| 58 |
* |
| 59 |
* @return array |
| 60 |
*/ |
| 61 |
public function _get_script_data(): array { |
| 62 |
$localize_script = [ |
| 63 |
'lp-global' => array( |
| 64 |
'url' => learn_press_get_current_url(), |
| 65 |
'siteurl' => site_url(), |
| 66 |
'ajax' => admin_url( 'admin-ajax.php' ), |
| 67 |
'courses_url' => learn_press_get_page_link( 'courses' ), |
| 68 |
'post_id' => get_the_ID(), |
| 69 |
'user_id' => get_current_user_id(), // use: course-progress. |
| 70 |
'theme' => get_stylesheet(), |
| 71 |
'localize' => array( |
| 72 |
'button_ok' => esc_html__( 'OK', 'learnpress' ), |
| 73 |
'button_cancel' => esc_html__( 'Cancel', 'learnpress' ), |
| 74 |
'button_yes' => esc_html__( 'Yes', 'learnpress' ), |
| 75 |
'button_no' => esc_html__( 'No', 'learnpress' ), |
| 76 |
), |
| 77 |
'lp_rest_url' => get_rest_url(), |
| 78 |
'nonce' => wp_create_nonce( 'wp_rest' ), |
| 79 |
'option_enable_popup_confirm_finish' => LP_Settings::get_option( 'enable_popup_confirm_finish', 'yes' ), |
| 80 |
'is_course_archive' => learn_press_is_courses(), |
| 81 |
'lpArchiveSkeleton' => lp_archive_skeleton_get_args(), |
| 82 |
), |
| 83 |
'lp-checkout' => array( |
| 84 |
'ajaxurl' => home_url( '/' ), |
| 85 |
'user_checkout' => LP()->checkout()->get_checkout_email(), |
| 86 |
'i18n_processing' => esc_html__( 'Processing', 'learnpress' ), |
| 87 |
'i18n_redirecting' => esc_html__( 'Redirecting', 'learnpress' ), |
| 88 |
'i18n_invalid_field' => esc_html__( 'Invalid field', 'learnpress' ), |
| 89 |
'i18n_unknown_error' => esc_html__( 'Unknown error', 'learnpress' ), |
| 90 |
'i18n_place_order' => esc_html__( 'Place order', 'learnpress' ), |
| 91 |
), |
| 92 |
'lp-profile-user' => array( |
| 93 |
'processing' => esc_html__( 'Processing', 'learnpress' ), |
| 94 |
'redirecting' => esc_html__( 'Redirecting', 'learnpress' ), |
| 95 |
'avatar_size' => learn_press_get_avatar_thumb_size(), |
| 96 |
), |
| 97 |
//'lp-course' => learn_press_single_course_args(), |
| 98 |
'lp-quiz' => learn_press_single_quiz_args(), |
| 99 |
]; |
| 100 |
|
| 101 |
return apply_filters( 'learnpress/frontend/localize_script', $localize_script ); |
| 102 |
|
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Config load scripts |
| 107 |
* |
| 108 |
* @return array |
| 109 |
*/ |
| 110 |
public function _get_scripts(): array { |
| 111 |
$wp_js = array( |
| 112 |
'jquery', |
| 113 |
'wp-element', |
| 114 |
'wp-compose', |
| 115 |
'wp-data', |
| 116 |
'wp-hooks', |
| 117 |
'wp-api-fetch', |
| 118 |
'lodash', |
| 119 |
); |
| 120 |
|
| 121 |
$scripts = apply_filters( |
| 122 |
'learn-press/frontend-default-scripts', |
| 123 |
array( |
| 124 |
'vue-libs' => new LP_Asset_Key( |
| 125 |
self::url( 'src/js/vendor/vue/vue_libs_special.min.js' ) |
| 126 |
), |
| 127 |
'lp-modal' => new LP_Asset_Key( |
| 128 |
self::url( 'js/dist/frontend/modal' . self::$_min_assets . '.js' ), |
| 129 |
array( 'jquery' ) |
| 130 |
), |
| 131 |
// lp-plugins-all use only for FE, when FE 2 release will remove it. |
| 132 |
'lp-plugins-all' => new LP_Asset_Key( self::url( 'js/vendor/plugins.all.min.js' ) ), |
| 133 |
'lp-global' => new LP_Asset_Key( |
| 134 |
self::url( self::$_folder_source . 'js/global' . self::$_min_assets . '.js' ), |
| 135 |
array( 'jquery', 'underscore', 'utils' ) |
| 136 |
), |
| 137 |
'lp-utils' => new LP_Asset_Key( |
| 138 |
self::url( 'js/dist/utils' . self::$_min_assets . '.js' ), |
| 139 |
array( 'jquery' ) |
| 140 |
), |
| 141 |
'lp-checkout' => new LP_Asset_Key( |
| 142 |
self::url( 'js/dist/frontend/checkout' . self::$_min_assets . '.js' ), |
| 143 |
array( 'lp-global', 'lp-utils', 'wp-api-fetch', 'jquery' ), |
| 144 |
array( LP_PAGE_CHECKOUT ), |
| 145 |
0, |
| 146 |
1 |
| 147 |
), |
| 148 |
'lp-data-controls' => new LP_Asset_Key( |
| 149 |
self::url( 'js/dist/js/data-controls' . self::$_min_assets . '.js' ), |
| 150 |
array_merge( $wp_js, array( 'lp-global' ) ) |
| 151 |
), |
| 152 |
'lp-config' => new LP_Asset_Key( |
| 153 |
self::url( 'js/dist/frontend/lp-configs' . self::$_min_assets . '.js' ), |
| 154 |
array_merge( $wp_js, array( 'lp-global' ) ) |
| 155 |
), |
| 156 |
// 'lp-lesson' => new LP_Asset_Key( self::url( self::$_folder_source .'js/frontend/lesson' . self::$_min_assets . '.js' ) ), |
| 157 |
'lp-question-types' => new LP_Asset_Key( |
| 158 |
self::url( 'js/dist/frontend/question-types' . self::$_min_assets . '.js' ), |
| 159 |
array_merge( $wp_js, array( 'lp-global' ) ), |
| 160 |
array(), |
| 161 |
1, |
| 162 |
1 |
| 163 |
), |
| 164 |
'lp-single-curriculum' => new LP_Asset_Key( |
| 165 |
self::url( 'js/dist/frontend/single-curriculum' . self::$_min_assets . '.js' ), |
| 166 |
array_merge( |
| 167 |
$wp_js, |
| 168 |
array( |
| 169 |
'lp-global', |
| 170 |
'lp-utils', |
| 171 |
) |
| 172 |
), |
| 173 |
array( LP_PAGE_SINGLE_COURSE_CURRICULUM ), |
| 174 |
0, |
| 175 |
1 |
| 176 |
), |
| 177 |
'lp-quiz' => new LP_Asset_Key( |
| 178 |
self::url( 'js/dist/frontend/quiz' . self::$_min_assets . '.js' ), |
| 179 |
array_merge( |
| 180 |
$wp_js, |
| 181 |
array( |
| 182 |
'wp-i18n', |
| 183 |
'lp-global', |
| 184 |
'lp-utils', |
| 185 |
'lp-data-controls', |
| 186 |
'lp-question-types', |
| 187 |
'lp-modal', |
| 188 |
'lp-config', |
| 189 |
'lp-single-curriculum', |
| 190 |
) |
| 191 |
), |
| 192 |
array( LP_PAGE_QUIZ ), |
| 193 |
0, |
| 194 |
1 |
| 195 |
), |
| 196 |
'lp-single-course' => new LP_Asset_Key( |
| 197 |
self::url( 'js/dist/frontend/single-course' . self::$_min_assets . '.js' ), |
| 198 |
array_merge( |
| 199 |
$wp_js, |
| 200 |
array( |
| 201 |
'lp-global', |
| 202 |
'lp-utils', |
| 203 |
) |
| 204 |
), |
| 205 |
array( LP_PAGE_SINGLE_COURSE ), |
| 206 |
0, |
| 207 |
1 |
| 208 |
), |
| 209 |
'lp-courses' => new LP_Asset_Key( |
| 210 |
self::url( 'js/dist/frontend/courses' . self::$_min_assets . '.js' ), |
| 211 |
array( 'lp-global', 'lp-utils', 'wp-hooks' ), |
| 212 |
array( LP_PAGE_COURSES ), |
| 213 |
0, |
| 214 |
0 |
| 215 |
), |
| 216 |
'lp-profile' => new LP_Asset_Key( |
| 217 |
self::url( 'js/dist/frontend/profile' . self::$_min_assets . '.js' ), |
| 218 |
array_merge( |
| 219 |
$wp_js, |
| 220 |
array( 'wp-i18n', 'lp-utils' ) |
| 221 |
), |
| 222 |
array( LP_PAGE_PROFILE ), |
| 223 |
0, |
| 224 |
1 |
| 225 |
), |
| 226 |
'lp-widgets' => new LP_Asset_Key( |
| 227 |
self::url( 'js/dist/frontend/widgets' . self::$_min_assets . '.js' ), |
| 228 |
array_merge( |
| 229 |
$wp_js, |
| 230 |
array( 'wp-i18n' ) |
| 231 |
), |
| 232 |
array(), |
| 233 |
1, |
| 234 |
1 |
| 235 |
), |
| 236 |
'lp-become-a-teacher' => new LP_Asset_Key( |
| 237 |
self::url( 'js/dist/frontend/become-teacher' . self::$_min_assets . '.js' ), |
| 238 |
array( 'jquery', 'lp-utils' ), |
| 239 |
array( LP_PAGE_BECOME_A_TEACHER ), |
| 240 |
0, |
| 241 |
1 |
| 242 |
), |
| 243 |
) |
| 244 |
); |
| 245 |
|
| 246 |
// Dequeue script 'smoothPageScroll' on item details, it makes can't scroll, when rewrite page item detail, can check to remove. |
| 247 |
if ( LP_PAGE_SINGLE_COURSE_CURRICULUM === LP_Page_Controller::page_current() || |
| 248 |
LP_PAGE_QUIZ === LP_Page_Controller::page_current() || |
| 249 |
LP_PAGE_QUESTION === LP_Page_Controller::page_current() ) { |
| 250 |
wp_dequeue_script( 'smoothPageScroll' ); |
| 251 |
} |
| 252 |
|
| 253 |
return $scripts; |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* Load assets |
| 258 |
* |
| 259 |
* @author tungnx |
| 260 |
* @version 1.0.1 |
| 261 |
* @since 3.2.8 |
| 262 |
*/ |
| 263 |
public function load_scripts() { |
| 264 |
$page_current = LP_Page_Controller::page_current(); |
| 265 |
$this->handle_js( $page_current ); |
| 266 |
$this->handle_style( $page_current ); |
| 267 |
|
| 268 |
do_action( 'learn-press/after-enqueue-scripts' ); |
| 269 |
} |
| 270 |
|
| 271 |
/** |
| 272 |
* Add lp overlay |
| 273 |
* |
| 274 |
* @since 3.2.8 |
| 275 |
* @version 1.0.1 |
| 276 |
* @author tungnx |
| 277 |
*/ |
| 278 |
public function show_overlay() { |
| 279 |
$page_current = LP_Page_Controller::page_current(); |
| 280 |
if ( ! in_array( |
| 281 |
$page_current, |
| 282 |
array( LP_PAGE_SINGLE_COURSE_CURRICULUM, LP_PAGE_SINGLE_COURSE, LP_PAGE_QUIZ ) |
| 283 |
) ) { |
| 284 |
return; |
| 285 |
} |
| 286 |
|
| 287 |
if ( 'yes' !== LP_Settings::get_option( 'enable_popup_confirm_finish', 'yes' ) ) { |
| 288 |
return; |
| 289 |
} |
| 290 |
|
| 291 |
echo '<div class="lp-overlay">'; |
| 292 |
apply_filters( 'learnpress/modal-dialog', learn_press_get_template( 'global/lp-modal-overlay' ) ); |
| 293 |
echo '</div>'; |
| 294 |
} |
| 295 |
|
| 296 |
public static function instance() { |
| 297 |
if ( is_admin() ) { |
| 298 |
return null; |
| 299 |
} |
| 300 |
|
| 301 |
if ( self::$_instance == null ) { |
| 302 |
self::$_instance = new self(); |
| 303 |
} |
| 304 |
|
| 305 |
return self::$_instance; |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
/** |
| 310 |
* Shortcut function to get instance of LP_Assets |
| 311 |
* |
| 312 |
* @return LP_Assets|null |
| 313 |
*/ |
| 314 |
function learn_press_assets() { |
| 315 |
return LP_Assets::instance(); |
| 316 |
} |
| 317 |
|
| 318 |
learn_press_assets(); |
| 319 |
|
| 320 |
|