| @@ -1,1679 +1,1686 @@ | ||
| 1 | -<?php | |
| 2 | -/** | |
| 3 | - * All functions for LearnPress template | |
| 4 | - * | |
| 5 | - * @author ThimPress | |
| 6 | - * @package LearnPress/Functions | |
| 7 | - * @version 1.0 | |
| 8 | - */ | |
| 9 | - | |
| 10 | -use LearnPress\Helpers\Template; | |
| 11 | -use LearnPress\Models\CourseModel; | |
| 12 | -use LearnPress\Models\CoursePostModel; | |
| 13 | -use LearnPress\Models\UserItems\UserCourseModel; | |
| 14 | -use LearnPress\Models\UserModel; | |
| 15 | -use LearnPress\TemplateHooks\Course\CourseMaterialTemplate; | |
| 16 | -use LearnPress\TemplateHooks\Course\SingleCourseTemplate; | |
| 17 | - | |
| 18 | -if ( ! defined( 'ABSPATH' ) ) { | |
| 19 | - exit; | |
| 20 | -} | |
| 21 | - | |
| 22 | -if ( ! function_exists( 'learn_press_add_course_buttons' ) ) { | |
| 23 | - function learn_press_add_course_buttons() { | |
| 24 | - _deprecated_function( __FUNCTION__, '4.3.2.4' ); | |
| 25 | - } | |
| 26 | -} | |
| 27 | - | |
| 28 | -if ( ! function_exists( 'learn_press_remove_course_buttons' ) ) { | |
| 29 | - function learn_press_remove_course_buttons() { | |
| 30 | - _deprecated_function( __FUNCTION__, '4.3.2.4' ); | |
| 31 | - } | |
| 32 | -} | |
| 33 | - | |
| 34 | -if ( ! function_exists( 'learn_press_get_course_tabs' ) ) { | |
| 35 | - /** | |
| 36 | - * Return an array of tabs display in single course page. | |
| 37 | - * | |
| 38 | - * @return array | |
| 39 | - * @throws Exception | |
| 40 | - */ | |
| 41 | - function learn_press_get_course_tabs() { | |
| 42 | - $courseModel = CourseModel::find( get_the_ID(), true ); | |
| 43 | - if ( ! $courseModel ) { | |
| 44 | - return []; | |
| 45 | - } | |
| 46 | - | |
| 47 | - $userModel = UserModel::find( get_current_user_id(), true ); | |
| 48 | - | |
| 49 | - $defaults = []; | |
| 50 | - $defaults['overview'] = [ | |
| 51 | - 'title' => esc_html__( 'Overview', 'learnpress' ), | |
| 52 | - 'priority' => 10, | |
| 53 | - 'callback' => LearnPress::instance()->template( 'course' )->callback( 'single-course/tabs/overview.php' ), | |
| 54 | - ]; | |
| 55 | - $defaults['curriculum'] = [ | |
| 56 | - 'title' => esc_html__( 'Curriculum', 'learnpress' ), | |
| 57 | - 'priority' => 30, | |
| 58 | - 'callback' => function () use ( $courseModel, $userModel ) { | |
| 59 | - $singleCourseTemplate = SingleCourseTemplate::instance(); | |
| 60 | - echo $singleCourseTemplate->html_curriculum( $courseModel, $userModel ); | |
| 61 | - }, | |
| 62 | - ]; | |
| 63 | - $defaults['instructor'] = [ | |
| 64 | - 'title' => esc_html__( 'Instructor', 'learnpress' ), | |
| 65 | - 'priority' => 40, | |
| 66 | - 'callback' => LearnPress::instance()->template( 'course' )->callback( 'single-course/tabs/instructor.php' ), | |
| 67 | - ]; | |
| 68 | - | |
| 69 | - $faq = $courseModel->get_meta_value_by_key( CoursePostModel::META_KEY_FAQS, [] ); | |
| 70 | - if ( ! empty( $faq ) ) { | |
| 71 | - $defaults['faqs'] = [ | |
| 72 | - 'title' => esc_html__( 'FAQs', 'learnpress' ), | |
| 73 | - 'priority' => 50, | |
| 74 | - 'callback' => function () use ( $courseModel ) { | |
| 75 | - $singleCourseTemplate = SingleCourseTemplate::instance(); | |
| 76 | - echo $singleCourseTemplate->html_faqs( $courseModel, [ 'show_heading' => false ] ); | |
| 77 | - }, | |
| 78 | - ]; | |
| 79 | - } | |
| 80 | - | |
| 81 | - $singleCourseTemplate = SingleCourseTemplate::instance(); | |
| 82 | - $html_material = $singleCourseTemplate->html_material( $courseModel, $userModel, [ 'show_heading' => false ] ); | |
| 83 | - if ( ! empty( $html_material ) ) { | |
| 84 | - $defaults['materials'] = [ | |
| 85 | - 'title' => esc_html__( 'Materials', 'learnpress' ), | |
| 86 | - 'priority' => 45, | |
| 87 | - 'callback' => function () use ( $html_material ) { | |
| 88 | - echo $html_material; | |
| 89 | - }, | |
| 90 | - ]; | |
| 91 | - } | |
| 92 | - | |
| 93 | - // @since 4.2.8.7.1 update new parameter $courseModel | |
| 94 | - $tabs = apply_filters( 'learn-press/course-tabs', $defaults, $courseModel ); | |
| 95 | - | |
| 96 | - if ( ! empty( $tabs ) ) { | |
| 97 | - $has_tab_active = false; | |
| 98 | - | |
| 99 | - // sort tabs by priority, from low to high | |
| 100 | - uasort( | |
| 101 | - $tabs, | |
| 102 | - function ( $a, $b ) { | |
| 103 | - $priority1 = $a['priority'] ?? 1; | |
| 104 | - $priority2 = $b['priority'] ?? 2; | |
| 105 | - | |
| 106 | - return ( $priority1 < $priority2 ) ? - 1 : 1; | |
| 107 | - } | |
| 108 | - ); | |
| 109 | - | |
| 110 | - foreach ( $tabs as $k => $v ) { | |
| 111 | - $v['id'] = $v['id'] ?? 'tab-' . $k; | |
| 112 | - if ( ! empty( $v['active'] ) ) { | |
| 113 | - $has_tab_active = true; | |
| 114 | - } | |
| 115 | - | |
| 116 | - $tabs[ $k ] = $v; | |
| 117 | - } | |
| 118 | - | |
| 119 | - // Check if there is no tab active, set first tab active | |
| 120 | - if ( ! $has_tab_active ) { | |
| 121 | - // set first tab active | |
| 122 | - $first_key = array_key_first( $tabs ); | |
| 123 | - if ( $first_key ) { | |
| 124 | - $tabs[ $first_key ]['active'] = true; | |
| 125 | - } | |
| 126 | - } | |
| 127 | - } | |
| 128 | - | |
| 129 | - return $tabs; | |
| 130 | - } | |
| 131 | -} | |
| 132 | - | |
| 133 | -if ( ! function_exists( 'learn_press_content_item_summary_question' ) ) { | |
| 134 | - function learn_press_content_item_summary_question() { | |
| 135 | - $quiz = LP_Global::course_item_quiz(); | |
| 136 | - $question = $quiz->get_viewing_question(); | |
| 137 | - | |
| 138 | - if ( $question ) { | |
| 139 | - $course = learn_press_get_course(); | |
| 140 | - $user = learn_press_get_current_user(); | |
| 141 | - $answered = false; | |
| 142 | - $course_data = $user->get_course_data( $course->get_id() ); | |
| 143 | - $user_quiz = $course_data->get_item_quiz( $quiz->get_id() ); | |
| 144 | - | |
| 145 | - if ( $user_quiz ) { | |
| 146 | - $answered = $user_quiz->get_question_answer( $question->get_id() ); | |
| 147 | - $question->show_correct_answers( | |
| 148 | - $user->has_checked_answer( | |
| 149 | - $question->get_id(), | |
| 150 | - $quiz->get_id(), | |
| 151 | - $course->get_id() | |
| 152 | - ) ? 'yes' : false | |
| 153 | - ); | |
| 154 | - $question->disable_answers( $user_quiz->get_status() == 'completed' ? 'yes' : false ); | |
| 155 | - } | |
| 156 | - | |
| 157 | - $question->render( $answered ); | |
| 158 | - } | |
| 159 | - } | |
| 160 | -} | |
| 161 | - | |
| 162 | - | |
| 163 | -if ( ! function_exists( 'learn_press_content_item_body_class' ) ) { | |
| 164 | - // Add more assets into page that displaying content of an item | |
| 165 | - add_filter( 'body_class', 'learn_press_content_item_body_class', 10 ); | |
| 166 | - | |
| 167 | - function learn_press_content_item_body_class( $classes ) { | |
| 168 | - global $lp_course_item; | |
| 169 | - | |
| 170 | - if ( wp_is_mobile() ) { | |
| 171 | - $sidebar_toggle_class = 'lp-sidebar-toggle__close'; | |
| 172 | - } else { | |
| 173 | - $sidebar_toggle_class = learn_press_cookie_get( 'sidebar-toggle' ) ? 'lp-sidebar-toggle__close' : 'lp-sidebar-toggle__open'; | |
| 174 | - } | |
| 175 | - | |
| 176 | - if ( $lp_course_item ) { | |
| 177 | - $classes[] = 'course-item-popup'; | |
| 178 | - $classes[] = 'viewing-course-item'; | |
| 179 | - $classes[] = 'viewing-course-item-' . $lp_course_item->get_id(); | |
| 180 | - $classes[] = 'course-item-' . $lp_course_item->get_item_type(); | |
| 181 | - $classes[] = $sidebar_toggle_class; | |
| 182 | - } | |
| 183 | - | |
| 184 | - return $classes; | |
| 185 | - } | |
| 186 | -} | |
| 187 | - | |
| 188 | -if ( ! function_exists( 'learn_press_content_item_edit_links' ) ) { | |
| 189 | - /** | |
| 190 | - * Add edit links for course item question to admin bar. | |
| 191 | - */ | |
| 192 | - function learn_press_content_item_edit_links() { | |
| 193 | - global $wp_admin_bar, $post, $lp_course_item, $lp_quiz_question; | |
| 194 | - | |
| 195 | - if ( ! ( ! is_admin() && is_user_logged_in() ) ) { | |
| 196 | - return; | |
| 197 | - } | |
| 198 | - | |
| 199 | - /** | |
| 200 | - * Edit link for lesson/quiz or any other course's item. | |
| 201 | - */ | |
| 202 | - if ( $lp_course_item ) { | |
| 203 | - $post_type_object = get_post_type_object( $lp_course_item->get_item_type() ); | |
| 204 | - | |
| 205 | - if ( $post_type_object && current_user_can( | |
| 206 | - 'edit_post', | |
| 207 | - $lp_course_item->get_id() | |
| 208 | - ) && $post_type_object->show_in_admin_bar && get_edit_post_link( $lp_course_item->get_id() ) ) { | |
| 209 | - $type = get_post_type( $lp_course_item->get_id() ); | |
| 210 | - | |
| 211 | - if ( apply_filters( 'learn-press/edit-admin-bar-button', true, $lp_course_item ) ) { | |
| 212 | - $wp_admin_bar->add_menu( | |
| 213 | - array( | |
| 214 | - 'id' => 'edit-' . $type, | |
| 215 | - 'title' => $post_type_object->labels->edit_item, | |
| 216 | - 'href' => get_edit_post_link( $lp_course_item->get_id() ), | |
| 217 | - ) | |
| 218 | - ); | |
| 219 | - } | |
| 220 | - } | |
| 221 | - } | |
| 222 | - | |
| 223 | - /** | |
| 224 | - * Edit link for quiz's question. | |
| 225 | - */ | |
| 226 | - if ( $lp_quiz_question ) { | |
| 227 | - $post_type_object = get_post_type_object( $lp_quiz_question->get_item_type() ); | |
| 228 | - $edit_post_link = get_edit_post_link( $lp_quiz_question->get_id() ); | |
| 229 | - | |
| 230 | - if ( $post_type_object && current_user_can( | |
| 231 | - 'edit_post', | |
| 232 | - $lp_quiz_question->get_id() | |
| 233 | - ) && $post_type_object->show_in_admin_bar && $edit_post_link ) { | |
| 234 | - $type = get_post_type( $lp_quiz_question->get_id() ); | |
| 235 | - $wp_admin_bar->add_menu( | |
| 236 | - array( | |
| 237 | - 'id' => 'edit-' . $type, | |
| 238 | - 'title' => $post_type_object->labels->edit_item, | |
| 239 | - 'href' => $edit_post_link, | |
| 240 | - ) | |
| 241 | - ); | |
| 242 | - } | |
| 243 | - } | |
| 244 | - } | |
| 245 | -} | |
| 246 | -add_filter( 'admin_bar_menu', 'learn_press_content_item_edit_links', 90 ); | |
| 247 | - | |
| 248 | -if ( ! function_exists( 'learn_press_single_quiz_args' ) ) { | |
| 249 | - function learn_press_single_quiz_args() { | |
| 250 | - $args = array(); | |
| 251 | - | |
| 252 | - if ( LP_PAGE_QUIZ !== LP_Page_Controller::page_current() ) { | |
| 253 | - return $args; | |
| 254 | - } | |
| 255 | - | |
| 256 | - $quiz = LP_Global::course_item_quiz(); | |
| 257 | - $course = learn_press_get_course(); | |
| 258 | - $user = learn_press_get_current_user(); | |
| 259 | - if ( $quiz && $course ) { | |
| 260 | - $course_id = $course->get_id(); | |
| 261 | - //$user_quiz = $user->get_item_data( $quiz->get_id(), $course_id ); | |
| 262 | - | |
| 263 | - /*if ( $user_quiz ) { | |
| 264 | - $remaining_time = $user_quiz->get_time_remaining(); | |
| 265 | - } else { | |
| 266 | - $remaining_time = false; | |
| 267 | - }*/ | |
| 268 | - | |
| 269 | - $args = array( | |
| 270 | - 'id' => $quiz->get_id(), | |
| 271 | - //'totalTime' => -1, | |
| 272 | - //'remainingTime' => $remaining_time ? $remaining_time->get() : $quiz->get_duration()->get(), | |
| 273 | - 'status' => $user->get_item_status( $quiz->get_id(), $course_id ), | |
| 274 | - 'checkNorequizenroll' => $course->is_no_required_enroll(), | |
| 275 | - 'navigationPosition' => LP_Settings::get_option( 'navigation_position', 'yes' ), | |
| 276 | - ); | |
| 277 | - } | |
| 278 | - | |
| 279 | - return apply_filters( 'learn-press/localize_script/quiz', $args, $user, $course, $quiz ); | |
| 280 | - } | |
| 281 | -} | |
| 282 | - | |
| 283 | -if ( ! function_exists( 'learn_press_single_document_title_parts' ) ) { | |
| 284 | - /** | |
| 285 | - * Custom document title depending on LP current page. | |
| 286 | - * E.g: Single course, profile, etc... | |
| 287 | - * | |
| 288 | - * @param array $title | |
| 289 | - * | |
| 290 | - * @return array | |
| 291 | - * @since 3.0.0 | |
| 292 | - * @version 3.0.1 | |
| 293 | - */ | |
| 294 | - function learn_press_single_document_title_parts( $title ) { | |
| 295 | - if ( learn_press_is_course() ) { | |
| 296 | - $item = LP_Global::course_item(); | |
| 297 | - | |
| 298 | - if ( $item ) { | |
| 299 | - $title['title'] = join( | |
| 300 | - ' ', | |
| 301 | - apply_filters( | |
| 302 | - 'learn-press/document-course-title-parts', | |
| 303 | - array( | |
| 304 | - $title['title'], | |
| 305 | - ' → ', | |
| 306 | - $item->get_title(), | |
| 307 | - ) | |
| 308 | - ) | |
| 309 | - ); | |
| 310 | - } | |
| 311 | - } elseif ( learn_press_is_courses() ) { | |
| 312 | - if ( learn_press_is_search() ) { | |
| 313 | - $title['title'] = esc_html__( 'Course Search Results', 'learnpress' ); | |
| 314 | - } else { | |
| 315 | - $title['title'] = esc_html__( 'Courses', 'learnpress' ); | |
| 316 | - } | |
| 317 | - } elseif ( LP_Page_Controller::is_page_profile() ) { | |
| 318 | - $profile = LP_Profile::instance(); | |
| 319 | - $tab_slug = $profile->get_current_tab(); | |
| 320 | - $tab = $profile->get_tab_at( $tab_slug ); | |
| 321 | - $page_id = learn_press_get_page_id( 'profile' ); | |
| 322 | - | |
| 323 | - if ( $page_id ) { | |
| 324 | - $page_title = get_the_title( $page_id ); | |
| 325 | - } else { | |
| 326 | - $page_title = ''; | |
| 327 | - } | |
| 328 | - | |
| 329 | - if ( $tab instanceof LP_Profile_Tab ) { | |
| 330 | - $title['title'] = join( | |
| 331 | - ' ', | |
| 332 | - apply_filters( | |
| 333 | - 'learn-press/document-profile-title-parts', | |
| 334 | - array( | |
| 335 | - $page_title, | |
| 336 | - '→', | |
| 337 | - $tab->get( 'title' ), | |
| 338 | - ) | |
| 339 | - ) | |
| 340 | - ); | |
| 341 | - } | |
| 342 | - } | |
| 343 | - | |
| 344 | - return $title; | |
| 345 | - } | |
| 346 | -} | |
| 347 | - | |
| 348 | -// @deprecated 4.2.7.3 | |
| 349 | -/*if ( ! function_exists( 'learn_press_course_item_class' ) ) { | |
| 350 | - function learn_press_course_item_class( $item_id, $course_id = 0, $class = null ) { | |
| 351 | - switch ( get_post_type( $item_id ) ) { | |
| 352 | - case 'lp_lesson': | |
| 353 | - learn_press_course_lesson_class( $item_id, $course_id, $class ); | |
| 354 | - break; | |
| 355 | - case 'lp_quiz': | |
| 356 | - learn_press_course_quiz_class( $item_id, $course_id, $class ); | |
| 357 | - break; | |
| 358 | - } | |
| 359 | - } | |
| 360 | -}*/ | |
| 361 | - | |
| 362 | -// @deprecated 4.2.7.3 | |
| 363 | -//if ( ! function_exists( 'learn_press_course_lesson_class' ) ) { | |
| 364 | -// /** | |
| 365 | -// * The class of lesson in course curriculum | |
| 366 | -// * | |
| 367 | -// * @param int $lesson_id | |
| 368 | -// * @param int $course_id | |
| 369 | -// * @param array|string $class | |
| 370 | -// * @param boolean $echo | |
| 371 | -// * | |
| 372 | -// * @return mixed | |
| 373 | -// */ | |
| 374 | -// function learn_press_course_lesson_class( $lesson_id = null, $course_id = 0, $class = null, $echo = true ) { | |
| 375 | -// $user = learn_press_get_current_user(); | |
| 376 | -// | |
| 377 | -// if ( ! $course_id ) { | |
| 378 | -// $course_id = get_the_ID(); | |
| 379 | -// } | |
| 380 | -// | |
| 381 | -// $course = learn_press_get_course( $course_id ); | |
| 382 | -// if ( ! $course ) { | |
| 383 | -// return ''; | |
| 384 | -// } | |
| 385 | -// | |
| 386 | -// if ( is_string( $class ) && $class ) { | |
| 387 | -// $class = preg_split( '!\s+!', $class ); | |
| 388 | -// } else { | |
| 389 | -// $class = array(); | |
| 390 | -// } | |
| 391 | -// | |
| 392 | -// $classes = array( | |
| 393 | -// 'course-lesson course-item course-item-' . $lesson_id, | |
| 394 | -// ); | |
| 395 | -// | |
| 396 | -// $user = learn_press_get_current_user(); | |
| 397 | -// $status = $user->get_item_status( $lesson_id ); | |
| 398 | -// | |
| 399 | -// if ( $status ) { | |
| 400 | -// $classes[] = "item-has-status item-{$status}"; | |
| 401 | -// } | |
| 402 | -// | |
| 403 | -// if ( $lesson_id && $course->is_current_item( $lesson_id ) ) { | |
| 404 | -// $classes[] = 'item-current'; | |
| 405 | -// } | |
| 406 | -// | |
| 407 | -// if ( learn_press_is_course() ) { | |
| 408 | -// if ( $course->is_free() ) { | |
| 409 | -// $classes[] = 'free-item'; | |
| 410 | -// } | |
| 411 | -// } | |
| 412 | -// | |
| 413 | -// $lesson = LP_Lesson::get_lesson( $lesson_id ); | |
| 414 | -// | |
| 415 | -// if ( $lesson && $lesson->is_preview() ) { | |
| 416 | -// $classes[] = 'preview-item'; | |
| 417 | -// } | |
| 418 | -// | |
| 419 | -// $classes = array_unique( array_merge( $classes, $class ) ); | |
| 420 | -// | |
| 421 | -// if ( $echo ) { | |
| 422 | -// echo 'class="' . implode( ' ', $classes ) . '"'; | |
| 423 | -// } | |
| 424 | -// | |
| 425 | -// return $classes; | |
| 426 | -// } | |
| 427 | -//} | |
| 428 | - | |
| 429 | -// @deprecated 4.2.7.3 | |
| 430 | -//if ( ! function_exists( 'learn_press_course_quiz_class' ) ) { | |
| 431 | -// /** | |
| 432 | -// * The class of lesson in course curriculum | |
| 433 | -// * | |
| 434 | -// * @param int $quiz_id | |
| 435 | -// * @param int $course_id | |
| 436 | -// * @param string|array $class | |
| 437 | -// * @param boolean $echo | |
| 438 | -// * | |
| 439 | -// * @return mixed | |
| 440 | -// */ | |
| 441 | -// function learn_press_course_quiz_class( $quiz_id = null, $course_id = 0, $class = null, $echo = true ) { | |
| 442 | -// $user = learn_press_get_current_user(); | |
| 443 | -// | |
| 444 | -// if ( ! $course_id ) { | |
| 445 | -// $course_id = get_the_ID(); | |
| 446 | -// } | |
| 447 | -// | |
| 448 | -// if ( is_string( $class ) && $class ) { | |
| 449 | -// $class = preg_split( '!\s+!', $class ); | |
| 450 | -// } else { | |
| 451 | -// $class = array(); | |
| 452 | -// } | |
| 453 | -// | |
| 454 | -// $course = learn_press_get_course( $course_id ); | |
| 455 | -// | |
| 456 | -// if ( ! $course ) { | |
| 457 | -// return ''; | |
| 458 | -// } | |
| 459 | -// | |
| 460 | -// $classes = array( | |
| 461 | -// 'course-quiz course-item course-item-' . $quiz_id, | |
| 462 | -// ); | |
| 463 | -// | |
| 464 | -// $status = $user->get_item_status( $quiz_id ); | |
| 465 | -// | |
| 466 | -// if ( $status ) { | |
| 467 | -// $classes[] = "item-has-status item-{$status}"; | |
| 468 | -// } | |
| 469 | -// | |
| 470 | -// if ( $quiz_id && $course->is_current_item( $quiz_id ) ) { | |
| 471 | -// $classes[] = 'item-current'; | |
| 472 | -// } | |
| 473 | -// | |
| 474 | -// /* | |
| 475 | -// if ( $user->can_view_item( $quiz_id, $course_id )->flag ) { | |
| 476 | -// $classes[] = 'viewable'; | |
| 477 | -// }*/ | |
| 478 | -// | |
| 479 | -// if ( $course->get_evaluation_type() == 'evaluate_final_quiz' && $course->is_final_quiz( $quiz_id ) ) { | |
| 480 | -// $classes[] = 'final-quiz'; | |
| 481 | -// } | |
| 482 | -// | |
| 483 | -// $classes = array_unique( array_merge( $classes, $class ) ); | |
| 484 | -// | |
| 485 | -// if ( $echo ) { | |
| 486 | -// echo 'class="' . implode( ' ', $classes ) . '"'; | |
| 487 | -// } | |
| 488 | -// | |
| 489 | -// return $classes; | |
| 490 | -// } | |
| 491 | -//} | |
| 492 | - | |
| 493 | -if ( ! function_exists( 'learn_press_course_class' ) ) { | |
| 494 | - /** | |
| 495 | - * Append new class to course post type | |
| 496 | - * | |
| 497 | - * @param $classes | |
| 498 | - * @param $class | |
| 499 | - * @param $post_id | |
| 500 | - * | |
| 501 | - * @return string | |
| 502 | - */ | |
| 503 | - function learn_press_course_class( $classes, $class, $post_id = '' ) { | |
| 504 | - if ( is_learnpress() ) { | |
| 505 | - $classes = (array) $classes; | |
| 506 | - } | |
| 507 | - | |
| 508 | - if ( $post_id === 0 ) { | |
| 509 | - $classes[] = 'page type-page'; | |
| 510 | - } | |
| 511 | - | |
| 512 | - if ( ! $post_id || 'lp_course' !== get_post_type( $post_id ) ) { | |
| 513 | - return $classes; | |
| 514 | - } | |
| 515 | - | |
| 516 | - $classes[] = 'course'; | |
| 517 | - | |
| 518 | - return apply_filters( 'learn_press_course_class', $classes ); | |
| 519 | - } | |
| 520 | -} | |
| 521 | - | |
| 522 | -function learn_press_setup_user() { | |
| 523 | - $GLOBALS['lp_user'] = learn_press_get_current_user(); | |
| 524 | -} | |
| 525 | - | |
| 526 | -//add_action( 'init', 'learn_press_setup_user', 1000 ); | |
| 527 | - | |
| 528 | -/** | |
| 529 | - * Display a message immediately with out push into queue | |
| 530 | - * | |
| 531 | - * @param $message | |
| 532 | - * @param string $type | |
| 533 | - * | |
| 534 | - * @Todo tungnx review code. | |
| 535 | - */ | |
| 536 | - | |
| 537 | -function learn_press_display_message( $message, $type = 'success' ) { | |
| 538 | - $messages = learn_press_session_get( learn_press_session_message_id() ); | |
| 539 | - learn_press_session_set( learn_press_session_message_id(), null ); | |
| 540 | - | |
| 541 | - // add new notice and display | |
| 542 | - learn_press_add_message( $message, $type ); | |
| 543 | - echo learn_press_get_messages( true ); | |
| 544 | - | |
| 545 | - // store back messages | |
| 546 | - learn_press_session_set( learn_press_session_message_id(), $messages ); | |
| 547 | -} | |
| 548 | - | |
| 549 | -/** | |
| 550 | - * Returns all notices added | |
| 551 | - * | |
| 552 | - * @param bool $clear | |
| 553 | - * | |
| 554 | - * @return string | |
| 555 | - */ | |
| 556 | -function learn_press_get_messages( $clear = false ) { | |
| 557 | - ob_start(); | |
| 558 | - learn_press_print_messages( $clear ); | |
| 559 | - | |
| 560 | - return ob_get_clean(); | |
| 561 | -} | |
| 562 | - | |
| 563 | -/** | |
| 564 | - * Add new message into queue for displaying. | |
| 565 | - * | |
| 566 | - * @param string $message | |
| 567 | - * @param string $type | |
| 568 | - * @param array $options | |
| 569 | - * @param int|bool $current_user . @since 3.0.9 - add for current user only | |
| 570 | - * | |
| 571 | - * @deprecated 4.2.2.1 | |
| 572 | - */ | |
| 573 | -function learn_press_add_message( $message, $type = 'success', $options = array(), $current_user = true ) { | |
| 574 | - if ( is_string( $options ) ) { | |
| 575 | - $options = array( 'id' => $options ); | |
| 576 | - } | |
| 577 | - | |
| 578 | - $options = wp_parse_args( | |
| 579 | - $options, | |
| 580 | - array( | |
| 581 | - 'id' => '', | |
| 582 | - ) | |
| 583 | - ); | |
| 584 | - | |
| 585 | - if ( $current_user ) { | |
| 586 | - if ( true === $current_user ) { | |
| 587 | - $current_user = get_current_user_id(); | |
| 588 | - } | |
| 589 | - } | |
| 590 | - | |
| 591 | - $key = "messages{$current_user}"; | |
| 592 | - | |
| 593 | - $messages = learn_press_session_get( $key ); | |
| 594 | - | |
| 595 | - if ( empty( $messages[ $type ] ) ) { | |
| 596 | - $messages[ $type ] = array(); | |
| 597 | - } | |
| 598 | - | |
| 599 | - $messages[ $type ][ $options['id'] ] = array( | |
| 600 | - 'content' => $message, | |
| 601 | - 'options' => $options, | |
| 602 | - ); | |
| 603 | - | |
| 604 | - learn_press_session_set( $key, $messages ); | |
| 605 | -} | |
| 606 | - | |
| 607 | -function learn_press_get_message( $message, $type = 'success' ) { | |
| 608 | - ob_start(); | |
| 609 | - learn_press_display_message( $message, $type ); | |
| 610 | - $message = ob_get_clean(); | |
| 611 | - | |
| 612 | - return $message; | |
| 613 | -} | |
| 614 | - | |
| 615 | -/** | |
| 616 | - * Set LP message to COOKIE. | |
| 617 | - * | |
| 618 | - * @param array $message_data ['status' => 'success/warning/error', 'content' => 'Message content'] | |
| 619 | - * | |
| 620 | - * @return void | |
| 621 | - * @version 1.0.0 | |
| 622 | - * @since 4.2.0 | |
| 623 | - */ | |
| 624 | -function learn_press_set_message( array $message_data = [] ) { | |
| 625 | - if ( ! isset( $message_data ['status'] ) ) { | |
| 626 | - error_log( 'Message data must have status' ); | |
| 627 | - | |
| 628 | - return; | |
| 629 | - } | |
| 630 | - if ( ! isset( $message_data ['content'] ) ) { | |
| 631 | - error_log( 'Message data must have content' ); | |
| 632 | - | |
| 633 | - return; | |
| 634 | - } | |
| 635 | - | |
| 636 | - $customer_id = LP_Session_Handler::instance()->get_customer_id(); | |
| 637 | - $customer_message = [ $customer_id => $message_data ]; | |
| 638 | - update_option( 'lp-customer-message', $customer_message ); | |
| 639 | -} | |
| 640 | - | |
| 641 | -/** | |
| 642 | - * Show message only one time. | |
| 643 | - * @return void | |
| 644 | - * @version 1.0.0 | |
| 645 | - * @since 4.2.0 | |
| 646 | - */ | |
| 647 | -function learn_press_show_message() { | |
| 648 | - try { | |
| 649 | - $customer_id = LP_Session_Handler::instance()->get_customer_id(); | |
| 650 | - $message_data = get_option( 'lp-customer-message' ) ?? []; | |
| 651 | - $customer_message = $message_data[ $customer_id ] ?? ''; | |
| 652 | - if ( ! $customer_message ) { | |
| 653 | - return; | |
| 654 | - } | |
| 655 | - | |
| 656 | - unset( $message_data[ $customer_id ] ); | |
| 657 | - update_option( 'lp-customer-message', $message_data ); | |
| 658 | - //delete_option( 'lp-message' ); | |
| 659 | - Template::instance()->get_frontend_template( 'global/lp-message.php', compact( 'customer_message' ) ); | |
| 660 | - } catch ( Throwable $e ) { | |
| 661 | - error_log( $e->getMessage() ); | |
| 662 | - } | |
| 663 | -} | |
| 664 | - | |
| 665 | -add_action( 'learn-press/before-course-item-content', 'learn_press_show_message', 50 ); | |
| 666 | - | |
| 667 | -/** | |
| 668 | - * Remove message added into queue by id and/or type. | |
| 669 | - * | |
| 670 | - * @param string $id | |
| 671 | - * @param string|array $type | |
| 672 | - * | |
| 673 | - * @since 3.0.0 | |
| 674 | - */ | |
| 675 | -function learn_press_remove_message( $id = '', $type = '' ) { | |
| 676 | - $groups = learn_press_session_get( learn_press_session_message_id() ); | |
| 677 | - | |
| 678 | - if ( ! $groups ) { | |
| 679 | - return; | |
| 680 | - } | |
| 681 | - | |
| 682 | - settype( $type, 'array' ); | |
| 683 | - | |
| 684 | - if ( $id ) { | |
| 685 | - foreach ( $groups as $message_type => $messages ) { | |
| 686 | - if ( ! sizeof( $type ) ) { | |
| 687 | - if ( isset( $groups[ $message_type ][ $id ] ) ) { | |
| 688 | - unset( $groups[ $message_type ][ $id ] ); | |
| 689 | - } | |
| 690 | - } elseif ( in_array( $message_type, $type ) ) { | |
| 691 | - if ( isset( $groups[ $message_type ][ $id ] ) ) { | |
| 692 | - unset( $groups[ $message_type ][ $id ] ); | |
| 693 | - } | |
| 694 | - } | |
| 695 | - } | |
| 696 | - } elseif ( sizeof( $type ) ) { | |
| 697 | - foreach ( $type as $t ) { | |
| 698 | - if ( isset( $groups[ $t ] ) ) { | |
| 699 | - unset( $groups[ $t ] ); | |
| 700 | - } | |
| 701 | - } | |
| 702 | - } else { | |
| 703 | - $groups = array(); | |
| 704 | - } | |
| 705 | - | |
| 706 | - learn_press_session_set( learn_press_session_message_id(), $groups ); | |
| 707 | -} | |
| 708 | - | |
| 709 | -/** | |
| 710 | - * Print out the message stored in the queue | |
| 711 | - * | |
| 712 | - * @param bool | |
| 713 | - */ | |
| 714 | -function learn_press_print_messages( $clear = true ) { | |
| 715 | - $messages = learn_press_session_get( learn_press_session_message_id() ); | |
| 716 | - learn_press_get_template( 'global/message.php', array( 'messages' => $messages ) ); | |
| 717 | - | |
| 718 | - if ( $clear ) { | |
| 719 | - learn_press_session_set( learn_press_session_message_id(), array() ); | |
| 720 | - } | |
| 721 | -} | |
| 722 | - | |
| 723 | -function learn_press_session_message_id() { | |
| 724 | - return 'messages' . get_current_user_id(); | |
| 725 | -} | |
| 726 | - | |
| 727 | -if ( ! function_exists( 'learn_press_page_title' ) ) { | |
| 728 | - | |
| 729 | - /** | |
| 730 | - * learn_press_page_title function. | |
| 731 | - * | |
| 732 | - * @param boolean $echo | |
| 733 | - * | |
| 734 | - * @return string | |
| 735 | - */ | |
| 736 | - function learn_press_page_title( bool $echo = false ): string { | |
| 737 | - $page_title = ''; | |
| 738 | - | |
| 739 | - if ( is_search() ) { | |
| 740 | - // Comment by tungnx | |
| 741 | - /*$page_title = sprintf( __( 'Search Results for: “%s”', 'learnpress' ), get_search_query() ); | |
| 742 | - | |
| 743 | - if ( get_query_var( 'paged' ) ) { | |
| 744 | - $page_title .= sprintf( __( ' – Page %s', 'learnpress' ), get_query_var( 'paged' ) ); | |
| 745 | - }*/ | |
| 746 | - } elseif ( is_tax() ) { | |
| 747 | - $page_title = single_term_title( '', false ); | |
| 748 | - } else { | |
| 749 | - $courses_page_id = learn_press_get_page_id( 'courses' ); | |
| 750 | - $page_title = get_the_title( $courses_page_id ); | |
| 751 | - } | |
| 752 | - | |
| 753 | - return apply_filters( 'learn_press_page_title', $page_title ); | |
| 754 | - } | |
| 755 | -} | |
| 756 | - | |
| 757 | -/** | |
| 758 | - * Get template part. | |
| 759 | - * | |
| 760 | - * @param string $slug | |
| 761 | - * @param string $name | |
| 762 | - * | |
| 763 | - * @return string | |
| 764 | - */ | |
| 765 | -function learn_press_get_template_part( $slug, $name = '' ) { | |
| 766 | - $template = ''; | |
| 767 | - | |
| 768 | - if ( $name ) { | |
| 769 | - $template = locate_template( | |
| 770 | - array( | |
| 771 | - "{$slug}-{$name}.php", | |
| 772 | - learn_press_template_path() . "/{$slug}-{$name}.php", | |
| 773 | - ) | |
| 774 | - ); | |
| 775 | - } | |
| 776 | - | |
| 777 | - // Get default slug-name.php | |
| 778 | - if ( ! $template && $name && file_exists( LP_PLUGIN_PATH . "/templates/{$slug}-{$name}.php" ) ) { | |
| 779 | - $template = LP_PLUGIN_PATH . "/templates/{$slug}-{$name}.php"; | |
| 780 | - } | |
| 781 | - | |
| 782 | - // If template file doesn't exist, look in yourtheme/slug.php and yourtheme/learnpress/slug.php | |
| 783 | - if ( ! $template ) { | |
| 784 | - $template = locate_template( array( "{$slug}.php", learn_press_template_path() . "/{$slug}.php" ) ); | |
| 785 | - } | |
| 786 | - // override path of child theme in parent theme - Fix for eduma by tuanta | |
| 787 | - $file_ct_in_pr = apply_filters( 'learn_press_child_in_parrent_template_path', '' ); | |
| 788 | - if ( $file_ct_in_pr && $name ) { | |
| 789 | - $template_child = locate_template( | |
| 790 | - array( | |
| 791 | - "{$slug}-{$name}.php", | |
| 792 | - 'lp-child-path/' . learn_press_template_path() . '/' . $file_ct_in_pr . "/{$slug}-{$name}.php", | |
| 793 | - ) | |
| 794 | - ); | |
| 795 | - if ( $template_child && file_exists( $template_child ) ) { | |
| 796 | - $template = $template_child; | |
| 797 | - } | |
| 798 | - // check in child theme if have filter learn_press_child_in_parrent_template_path | |
| 799 | - | |
| 800 | - $check_child_theme = get_stylesheet_directory() . '/' . learn_press_template_path() . "{$slug}-{$name}.php"; | |
| 801 | - if ( $check_child_theme && file_exists( $check_child_theme ) ) { | |
| 802 | - $template = $check_child_theme; | |
| 803 | - } | |
| 804 | - } | |
| 805 | - | |
| 806 | - // Allow 3rd party plugin filter template file from their plugin | |
| 807 | - if ( $template ) { | |
| 808 | - $template = apply_filters( 'learn_press_get_template_part', $template, $slug, $name ); | |
| 809 | - } | |
| 810 | - if ( $template && file_exists( $template ) ) { | |
| 811 | - load_template( $template, false ); | |
| 812 | - } | |
| 813 | - | |
| 814 | - return $template; | |
| 815 | -} | |
| 816 | - | |
| 817 | -/** | |
| 818 | - * Get other templates passing attributes and including the file. | |
| 819 | - * | |
| 820 | - * @param string $template_name . | |
| 821 | - * @param array $args (default: array()) . | |
| 822 | - * @param string $template_path (default: ''). | |
| 823 | - * @param string $default_path (default: ''). | |
| 824 | - * | |
| 825 | - * @return void | |
| 826 | - */ | |
| 827 | -function learn_press_get_template( $template_name = '', $args = array(), $template_path = '', $default_path = '' ) { | |
| 828 | - if ( $args && is_array( $args ) ) { | |
| 829 | - extract( $args ); | |
| 830 | - } | |
| 831 | - | |
| 832 | - if ( false === strpos( $template_name, '.php' ) ) { | |
| 833 | - $template_name .= '.php'; | |
| 834 | - } | |
| 835 | - | |
| 836 | - $located = learn_press_locate_template( $template_name, $template_path, $default_path ); | |
| 837 | - | |
| 838 | - if ( ! file_exists( $located ) ) { | |
| 839 | - _doing_it_wrong( __FUNCTION__, sprintf( '<code>%s</code> does not exist.', $located ), '2.1' ); | |
| 840 | - | |
| 841 | - $log = sprintf( 'TEMPLATE MISSING: Template %s doesn\'t exists.', $template_name ); | |
| 842 | - error_log( $log ); | |
| 843 | - | |
| 844 | - if ( LP_Debug::is_debug() ) { | |
| 845 | - echo sprintf( '<span title="%s" class="learn-press-template-warning"></span>', $log ); | |
| 846 | - } | |
| 847 | - | |
| 848 | - return; | |
| 849 | - } | |
| 850 | - | |
| 851 | - // Allow 3rd party plugin filter template file from their plugin | |
| 852 | - $located = apply_filters( | |
| 853 | - 'learn_press_get_template', | |
| 854 | - $located, | |
| 855 | - $template_name, | |
| 856 | - $args, | |
| 857 | - $template_path, | |
| 858 | - $default_path | |
| 859 | - ); | |
| 860 | - if ( $located != '' ) { | |
| 861 | - do_action( 'learn_press_before_template_part', $template_name, $template_path, $located, $args ); | |
| 862 | - | |
| 863 | - include $located; | |
| 864 | - | |
| 865 | - do_action( 'learn_press_after_template_part', $template_name, $template_path, $located, $args ); | |
| 866 | - } | |
| 867 | -} | |
| 868 | - | |
| 869 | -/** | |
| 870 | - * Get template content | |
| 871 | - * | |
| 872 | - * @param $template_name | |
| 873 | - * @param array $args | |
| 874 | - * @param string $template_path | |
| 875 | - * @param string $default_path | |
| 876 | - * | |
| 877 | - * @return string | |
| 878 | - * @uses learn_press_get_template(); | |
| 879 | - */ | |
| 880 | -function learn_press_get_template_content( $template_name, $args = array(), $template_path = '', $default_path = '' ) { | |
| 881 | - ob_start(); | |
| 882 | - learn_press_get_template( $template_name, $args, $template_path, $default_path ); | |
| 883 | - | |
| 884 | - return ob_get_clean(); | |
| 885 | -} | |
| 886 | - | |
| 887 | -/** | |
| 888 | - * Locate a template and return the path for inclusion. | |
| 889 | - * | |
| 890 | - * @param string $template_name | |
| 891 | - * @param string $template_path (default: '') | |
| 892 | - * @param string $default_path (default: '') | |
| 893 | - * | |
| 894 | - * @return string | |
| 895 | - */ | |
| 896 | -function learn_press_locate_template( $template_name, $template_path = '', $default_path = '' ) { | |
| 897 | - if ( ! $template_path ) { | |
| 898 | - $template_path = learn_press_template_path(); | |
| 899 | - } | |
| 900 | - | |
| 901 | - if ( ! $default_path ) { | |
| 902 | - $default_path = LP_PLUGIN_PATH . 'templates/'; | |
| 903 | - } | |
| 904 | - | |
| 905 | - /** | |
| 906 | - * Disable override templates in theme by default since LP 4.0.0 | |
| 907 | - */ | |
| 908 | - if ( learn_press_override_templates() ) { | |
| 909 | - $template = locate_template( | |
| 910 | - array( | |
| 911 | - trailingslashit( $template_path ) . $template_name, | |
| 912 | - $template_name, | |
| 913 | - ) | |
| 914 | - ); | |
| 915 | - // override path of child theme in parent theme - Fix for eduma by tuanta | |
| 916 | - $file_ct_in_pr = apply_filters( 'learn_press_child_in_parrent_template_path', '' ); | |
| 917 | - if ( $file_ct_in_pr ) { | |
| 918 | - $template_child = locate_template( | |
| 919 | - array( | |
| 920 | - trailingslashit( 'lp-child-path/' . $template_path . '/' . $file_ct_in_pr ) . $template_name, | |
| 921 | - $template_name, | |
| 922 | - ) | |
| 923 | - ); | |
| 924 | - if ( $template_child && file_exists( $template_child ) ) { | |
| 925 | - $template = $template_child; | |
| 926 | - } | |
| 927 | - // check in child theme if have filter learn_press_child_in_parrent_template_path | |
| 928 | - $check_child_theme = get_stylesheet_directory() . '/' . trailingslashit( $template_path ) . $template_name; | |
| 929 | - if ( $check_child_theme && file_exists( $check_child_theme ) ) { | |
| 930 | - $template = $check_child_theme; | |
| 931 | - } | |
| 932 | - } | |
| 933 | - } | |
| 934 | - if ( ! isset( $template ) || ! $template ) { | |
| 935 | - $template = trailingslashit( $default_path ) . $template_name; | |
| 936 | - } | |
| 937 | - | |
| 938 | - return apply_filters( 'learn_press_locate_template', $template, $template_name, $template_path ); | |
| 939 | -} | |
| 940 | - | |
| 941 | -/** | |
| 942 | - * Returns the name of folder contains override template files in theme | |
| 943 | - * | |
| 944 | - * @return string | |
| 945 | - * @since 3.0.0 | |
| 946 | - * @version 1.0.1 | |
| 947 | - */ | |
| 948 | -function learn_press_template_path(): string { | |
| 949 | - $lp_folder_name_override = apply_filters( 'learn_press_template_path', LP_PLUGIN_FOLDER_NAME ); | |
| 950 | - if ( ! is_string( $lp_folder_name_override ) ) { | |
| 951 | - $lp_folder_name_override = LP_PLUGIN_FOLDER_NAME; | |
| 952 | - } | |
| 953 | - | |
| 954 | - return $lp_folder_name_override; | |
| 955 | -} | |
| 956 | - | |
| 957 | -/** | |
| 958 | - * Disable override templates in theme by default | |
| 959 | - * | |
| 960 | - * @return bool | |
| 961 | - * @since 4.0.0 | |
| 962 | - */ | |
| 963 | -function learn_press_override_templates() { | |
| 964 | - return apply_filters( 'learn-press/override-templates', false ); | |
| 965 | -} | |
| 966 | - | |
| 967 | -/** | |
| 968 | - * Get html view path for admin to display | |
| 969 | - * | |
| 970 | - * @param $name | |
| 971 | - * @param $plugin_file | |
| 972 | - * | |
| 973 | - * @return mixed | |
| 974 | - */ | |
| 975 | -function learn_press_get_admin_view( $name, $plugin_file = null ) { | |
| 976 | - if ( ! preg_match( '/\.(html|php)$/', $name ) ) { | |
| 977 | - $name .= '.php'; | |
| 978 | - } | |
| 979 | - if ( $plugin_file ) { | |
| 980 | - $view = dirname( $plugin_file ) . '/inc/admin/views/' . $name; | |
| 981 | - } else { | |
| 982 | - $view = LearnPress::instance()->plugin_path( 'inc/admin/views/' . $name ); | |
| 983 | - } | |
| 984 | - | |
| 985 | - return apply_filters( 'learn_press_admin_view', $view, $name ); | |
| 986 | -} | |
| 987 | - | |
| 988 | -function learn_press_admin_view_content( $name, $args = array() ) { | |
| 989 | - return learn_press_admin_view( $name, $args, false, true ); | |
| 990 | -} | |
| 991 | - | |
| 992 | -/** | |
| 993 | - * Find a full path of a view and display the content in admin | |
| 994 | - * | |
| 995 | - * @param $name | |
| 996 | - * @param array $args | |
| 997 | - * @param bool|false $include_once | |
| 998 | - * @param bool | |
| 999 | - * | |
| 1000 | - * @return bool | |
| 1001 | - */ | |
| 1002 | -function learn_press_admin_view( $name, $args = array(), $include_once = false, $return = false ) { | |
| 1003 | - $view = learn_press_get_admin_view( $name, ! empty( $args['plugin_file'] ) ? $args['plugin_file'] : null ); | |
| 1004 | - | |
| 1005 | - if ( file_exists( $view ) ) { | |
| 1006 | - | |
| 1007 | - ob_start(); | |
| 1008 | - | |
| 1009 | - is_array( $args ) && extract( $args ); | |
| 1010 | - | |
| 1011 | - do_action( 'learn_press_before_display_admin_view', $name, $args ); | |
| 1012 | - | |
| 1013 | - if ( $include_once ) { | |
| 1014 | - include_once $view; | |
| 1015 | - } else { | |
| 1016 | - include $view; | |
| 1017 | - } | |
| 1018 | - | |
| 1019 | - do_action( 'learn_press_after_display_admin_view', $name, $args ); | |
| 1020 | - $output = ob_get_clean(); | |
| 1021 | - | |
| 1022 | - if ( ! $return ) { | |
| 1023 | - learn_press_echo_vuejs_write_on_php( $output ); | |
| 1024 | - } | |
| 1025 | - | |
| 1026 | - return $return ? $output : true; | |
| 1027 | - } | |
| 1028 | - | |
| 1029 | - return false; | |
| 1030 | -} | |
| 1031 | - | |
| 1032 | -if ( ! function_exists( 'learn_press_is_404' ) ) { | |
| 1033 | - /** | |
| 1034 | - * Set header is 404 | |
| 1035 | - * @deprecated 4.2.8 | |
| 1036 | - */ | |
| 1037 | - function learn_press_is_404() { | |
| 1038 | - _deprecated_function( __FUNCTION__, '4.2.8', 'LP_Page_Controller::set_page_404' ); | |
| 1039 | - | |
| 1040 | - return; | |
| 1041 | - global $wp_query; | |
| 1042 | - $wp_query->set_404(); | |
| 1043 | - status_header( 404 ); | |
| 1044 | - } | |
| 1045 | -} | |
| 1046 | - | |
| 1047 | -if ( ! function_exists( 'learn_press_404_page' ) ) { | |
| 1048 | - /** | |
| 1049 | - * Display 404 page | |
| 1050 | - * | |
| 1051 | - * @deprecated 4.2.8 | |
| 1052 | - */ | |
| 1053 | - function learn_press_404_page() { | |
| 1054 | - _deprecated_function( __FUNCTION__, '4.2.8', 'LP_Page_Controller::set_page_404' ); | |
| 1055 | - | |
| 1056 | - return; | |
| 1057 | - learn_press_is_404(); | |
| 1058 | - } | |
| 1059 | -} | |
| 1060 | - | |
| 1061 | -if ( ! function_exists( 'learn_press_generate_template_information' ) ) { | |
| 1062 | - function learn_press_generate_template_information( $template_name, $template_path, $located, $args ) { | |
| 1063 | - $debug = learn_press_get_request( 'show-template-location' ); | |
| 1064 | - if ( $debug == 'on' ) { | |
| 1065 | - echo '<!-- Template Location:' . str_replace( array( LP_PLUGIN_PATH, ABSPATH ), '', $located ) . ' -->'; | |
| 1066 | - } | |
| 1067 | - } | |
| 1068 | -} | |
| 1069 | - | |
| 1070 | -if ( ! function_exists( 'learn_press_course_remaining_time' ) ) { | |
| 1071 | - /** | |
| 1072 | - * Show the time remain of a course | |
| 1073 | - */ | |
| 1074 | - function learn_press_course_remaining_time() { | |
| 1075 | - $user = learn_press_get_current_user(); | |
| 1076 | - if ( ! $user->has_finished_course( get_the_ID() ) && $text = learn_press_get_course( get_the_ID() )->get_user_duration_html( $user->get_id() ) ) { | |
| 1077 | - learn_press_display_message( $text ); | |
| 1078 | - } | |
| 1079 | - } | |
| 1080 | -} | |
| 1081 | - | |
| 1082 | -if ( ! function_exists( 'learn_press_item_meta_type' ) ) { | |
| 1083 | - function learn_press_item_meta_type( $course, $item ) { | |
| 1084 | - ?> | |
| 1085 | - | |
| 1086 | - <?php if ( $item->post_type == 'lp_quiz' ) { ?> | |
| 1087 | - | |
| 1088 | - <span class="lp-label lp-label-quiz"><?php _e( 'Quiz', 'learnpress' ); ?></span> | |
| 1089 | - | |
| 1090 | - <?php if ( $course->final_quiz == $item->ID ) { ?> | |
| 1091 | - | |
| 1092 | - <span class="lp-label lp-label-final"><?php _e( 'Final', 'learnpress' ); ?></span> | |
| 1093 | - | |
| 1094 | - <?php } ?> | |
| 1095 | - | |
| 1096 | - <?php } elseif ( $item->post_type == 'lp_lesson' ) { ?> | |
| 1097 | - | |
| 1098 | - <span class="lp-label lp-label-lesson"><?php _e( 'Lesson', 'learnpress' ); ?></span> | |
| 1099 | - <?php if ( get_post_meta( $item->ID, '_lp_preview', true ) == 'yes' ) { ?> | |
| 1100 | - | |
| 1101 | - <span class="lp-label lp-label-preview"><?php _e( 'Preview', 'learnpress' ); ?></span> | |
| 1102 | - | |
| 1103 | - <?php } ?> | |
| 1104 | - | |
| 1105 | - <?php } else { ?> | |
| 1106 | - | |
| 1107 | - <?php do_action( 'learn_press_item_meta_type', $course, $item ); ?> | |
| 1108 | - | |
| 1109 | - <?php | |
| 1110 | - } | |
| 1111 | - } | |
| 1112 | -} | |
| 1113 | - | |
| 1114 | -if ( ! function_exists( 'learn_press_sort_course_tabs' ) ) { | |
| 1115 | - function learn_press_sort_course_tabs( $tabs = array() ) { | |
| 1116 | - uasort( $tabs, 'learn_press_sort_list_by_priority_callback' ); | |
| 1117 | - | |
| 1118 | - return $tabs; | |
| 1119 | - } | |
| 1120 | -} | |
| 1121 | - | |
| 1122 | -if ( ! function_exists( 'learn_press_get_profile_display_name' ) ) { | |
| 1123 | - /** | |
| 1124 | - * Get Display name publicly as in Profile page | |
| 1125 | - */ | |
| 1126 | - function learn_press_get_profile_display_name( $user ) { | |
| 1127 | - if ( empty( $user ) ) { | |
| 1128 | - return ''; | |
| 1129 | - } | |
| 1130 | - | |
| 1131 | - $id = ''; | |
| 1132 | - | |
| 1133 | - if ( $user instanceof LP_Abstract_User ) { | |
| 1134 | - $id = $user->get_id(); | |
| 1135 | - } elseif ( $user instanceof WP_User ) { | |
| 1136 | - $id = $user->ID; | |
| 1137 | - } elseif ( is_numeric( $user ) ) { | |
| 1138 | - $id = $user; | |
| 1139 | - } | |
| 1140 | - | |
| 1141 | - if ( ! isset( $id ) ) { | |
| 1142 | - return ''; | |
| 1143 | - } | |
| 1144 | - | |
| 1145 | - $info = get_userdata( $id ); | |
| 1146 | - | |
| 1147 | - return $info ? $info->display_name : ''; | |
| 1148 | - } | |
| 1149 | -} | |
| 1150 | - | |
| 1151 | -function learn_press_is_content_item_only() { | |
| 1152 | - return ! empty( $_REQUEST['content-item-only'] ); | |
| 1153 | -} | |
| 1154 | - | |
| 1155 | -function learn_press_label_html( $label, $type = '' ) { | |
| 1156 | - ?> | |
| 1157 | - <span class="lp-label label-<?php echo esc_attr( $type ? $type : $label ); ?>"> | |
| 1158 | - <?php echo esc_html( $label ); ?> | |
| 1159 | - </span> | |
| 1160 | - <?php | |
| 1161 | -} | |
| 1162 | - | |
| 1163 | -/** | |
| 1164 | - * @param LP_Quiz $item | |
| 1165 | - * | |
| 1166 | - * @deprecated 4.2.3.5 | |
| 1167 | - */ | |
| 1168 | -function learn_press_quiz_meta_final( $item ) { | |
| 1169 | - _deprecated_function( __METHOD__, '4.2.3.5' ); | |
| 1170 | - | |
| 1171 | - return; | |
| 1172 | - $course = learn_press_get_course(); | |
| 1173 | - | |
| 1174 | - if ( ! $course->is_final_quiz( $item->get_id() ) ) { | |
| 1175 | - return; | |
| 1176 | - } | |
| 1177 | - echo '<span class="item-meta final-quiz">' . esc_html__( 'Final', 'learnpress' ) . '</span>'; | |
| 1178 | -} | |
| 1179 | - | |
| 1180 | -function learn_press_comments_template_query_args( $comment_args ) { | |
| 1181 | - $post_type = get_post_type( $comment_args['post_id'] ); | |
| 1182 | - | |
| 1183 | - if ( $post_type == LP_COURSE_CPT ) { | |
| 1184 | - $comment_args['type__not_in'] = 'review'; | |
| 1185 | - } | |
| 1186 | - | |
| 1187 | - return $comment_args; | |
| 1188 | -} | |
| 1189 | - | |
| 1190 | -add_filter( 'comments_template_query_args', 'learn_press_comments_template_query_args' ); | |
| 1191 | - | |
| 1192 | -function learn_press_comment_form_logged_in( $html_login ) { | |
| 1193 | - if ( get_post_type() == LP_COURSE_CPT || get_post_type() == LP_LESSON_CPT ) { | |
| 1194 | - $html_login = ''; | |
| 1195 | - } | |
| 1196 | - | |
| 1197 | - return $html_login; | |
| 1198 | -} | |
| 1199 | - | |
| 1200 | -add_filter( 'comment_form_logged_in', 'learn_press_comment_form_logged_in' ); | |
| 1201 | - | |
| 1202 | -function learn_press_comment_form_defaults( $defaults ) { | |
| 1203 | - if ( get_post_type() == LP_COURSE_CPT || get_post_type() == LP_LESSON_CPT ) { | |
| 1204 | - $defaults['comment_notes_before'] = ''; | |
| 1205 | - } | |
| 1206 | - | |
| 1207 | - return $defaults; | |
| 1208 | -} | |
| 1209 | - | |
| 1210 | -add_filter( 'comment_form_defaults', 'learn_press_comment_form_defaults' ); | |
| 1211 | - | |
| 1212 | -function learn_press_filter_get_comments_number( $count, $post_id = 0 ) { | |
| 1213 | - global $wpdb; | |
| 1214 | - | |
| 1215 | - if ( ! $post_id ) { | |
| 1216 | - $post_id = get_the_ID(); | |
| 1217 | - if ( ! $post_id ) { | |
| 1218 | - return $count; | |
| 1219 | - } | |
| 1220 | - } | |
| 1221 | - | |
| 1222 | - if ( get_post_type( $post_id ) == LP_COURSE_CPT ) { | |
| 1223 | - $sql = $wpdb->prepare( | |
| 1224 | - ' SELECT count(*) ' | |
| 1225 | - . " FROM {$wpdb->comments} " | |
| 1226 | - . ' WHERE comment_post_ID = %d ' | |
| 1227 | - . ' AND comment_approved = 1 ' | |
| 1228 | - . ' AND comment_type != %s ', | |
| 1229 | - $post_id, | |
| 1230 | - 'review' | |
| 1231 | - ); | |
| 1232 | - | |
| 1233 | - $count = $wpdb->get_var( $sql ); | |
| 1234 | - } | |
| 1235 | - | |
| 1236 | - return $count; | |
| 1237 | -} | |
| 1238 | - | |
| 1239 | -add_filter( 'get_comments_number', 'learn_press_filter_get_comments_number' ); | |
| 1240 | - | |
| 1241 | -/** | |
| 1242 | - * Add custom classes to body tag class name | |
| 1243 | - * | |
| 1244 | - * @param array $classes | |
| 1245 | - * | |
| 1246 | - * @return array | |
| 1247 | - * | |
| 1248 | - * @since 3.0.0 | |
| 1249 | - * @Todo tungnx review to remove | |
| 1250 | - * @deprecated 4.2.3 | |
| 1251 | - */ | |
| 1252 | -function learn_press_body_classes( $classes ) { | |
| 1253 | - if ( is_learnpress() ) { | |
| 1254 | - $classes[] = get_stylesheet(); | |
| 1255 | - $classes[] = 'learnpress'; | |
| 1256 | - $classes[] = 'learnpress-page'; | |
| 1257 | - } | |
| 1258 | - | |
| 1259 | - return $classes; | |
| 1260 | -} | |
| 1261 | - | |
| 1262 | -//add_filter( 'body_class', 'learn_press_body_classes', 10 ); | |
| 1263 | - | |
| 1264 | -/** | |
| 1265 | - * Return true if user is learning a course | |
| 1266 | - * | |
| 1267 | - * @param int $course_id | |
| 1268 | - * | |
| 1269 | - * @return bool | |
| 1270 | - * @since 3.0 | |
| 1271 | - * @version 1.0.1 | |
| 1272 | - */ | |
| 1273 | -function learn_press_is_learning_course( int $course_id = 0 ): bool { | |
| 1274 | - $is_learning = false; | |
| 1275 | - $user = learn_press_get_current_user(); | |
| 1276 | - $course = learn_press_get_course( $course_id ); | |
| 1277 | - if ( ! $course ) { | |
| 1278 | - return $is_learning; | |
| 1279 | - } | |
| 1280 | - | |
| 1281 | - if ( $user ) { | |
| 1282 | - return $user->has_enrolled_or_finished( $course_id ); | |
| 1283 | - } | |
| 1284 | - | |
| 1285 | - if ( $course->is_no_required_enroll() ) { | |
| 1286 | - $is_learning = true; | |
| 1287 | - } | |
| 1288 | - | |
| 1289 | - return apply_filters( 'lp/is-learning-course', $is_learning, $course_id ); | |
| 1290 | -} | |
| 1291 | - | |
| 1292 | -function learn_press_content_item_summary_class( $more = '', $echo = true ) { | |
| 1293 | - $classes = array( 'content-item-summary' ); | |
| 1294 | - $classes = LP_Helper::merge_class( $classes, $more ); | |
| 1295 | - $classes = apply_filters( 'learn-press/content-item-summary-class', $classes ); | |
| 1296 | - $output = 'class="' . esc_attr( join( ' ', $classes ) ) . '"'; | |
| 1297 | - | |
| 1298 | - if ( $echo ) { | |
| 1299 | - echo wp_kses_post( $output ); | |
| 1300 | - } | |
| 1301 | - | |
| 1302 | - return $output; | |
| 1303 | -} | |
| 1304 | - | |
| 1305 | -/** | |
| 1306 | - * @deprecated 4.2.3.1 | |
| 1307 | - */ | |
| 1308 | -function learn_press_content_item_summary_classes( $classes ) { | |
| 1309 | - _deprecated_function( __FUNCTION__, '4.2.3.1' ); | |
| 1310 | - $item = LP_Global::course_item(); | |
| 1311 | - | |
| 1312 | - if ( ! $item ) { | |
| 1313 | - return $classes; | |
| 1314 | - } | |
| 1315 | - | |
| 1316 | - if ( $item->get_post_type() !== LP_LESSON_CPT ) { | |
| 1317 | - return $classes; | |
| 1318 | - } | |
| 1319 | - | |
| 1320 | - return $classes; | |
| 1321 | -} | |
| 1322 | - | |
| 1323 | -function learn_press_maybe_load_comment_js() { | |
| 1324 | - $item = LP_Global::course_item(); | |
| 1325 | - | |
| 1326 | - if ( $item ) { | |
| 1327 | - wp_enqueue_script( 'comment-reply' ); | |
| 1328 | - } | |
| 1329 | -} | |
| 1330 | - | |
| 1331 | -add_action( 'wp_enqueue_scripts', 'learn_press_maybe_load_comment_js' ); | |
| 1332 | - | |
| 1333 | -/** | |
| 1334 | - * Get list layouts archive course setting | |
| 1335 | - * | |
| 1336 | - * @editor tungnx | |
| 1337 | - * @modify 4.1.3 | |
| 1338 | - */ | |
| 1339 | -function learn_press_courses_layouts() { | |
| 1340 | - return apply_filters( | |
| 1341 | - 'learnpress/archive-courses-layouts', | |
| 1342 | - [ | |
| 1343 | - 'grid' => __( 'Grid', 'learnpress' ), | |
| 1344 | - 'list' => __( 'List', 'learnpress' ), | |
| 1345 | - ] | |
| 1346 | - ); | |
| 1347 | -} | |
| 1348 | - | |
| 1349 | -/** | |
| 1350 | - * Get layout template for archive course page. | |
| 1351 | - * | |
| 1352 | - * @return mixed | |
| 1353 | - * @since 3.3.0 | |
| 1354 | - * @editor tungnx | |
| 1355 | - * @modify 4.1.3 | |
| 1356 | - */ | |
| 1357 | -function learn_press_get_courses_layout() { | |
| 1358 | - $layout = LP_Request::get_cookie( 'courses-layout' ); | |
| 1359 | - | |
| 1360 | - if ( ! $layout ) { | |
| 1361 | - $layout = LP_Settings::get_option( 'archive_courses_layout', 'list' ); | |
| 1362 | - } | |
| 1363 | - | |
| 1364 | - return $layout; | |
| 1365 | -} | |
| 1366 | - | |
| 1367 | -function learn_press_register_sidebars() { | |
| 1368 | - register_sidebar( | |
| 1369 | - array( | |
| 1370 | - 'name' => esc_html__( 'Course Sidebar', 'learnpress' ), | |
| 1371 | - 'id' => 'course-sidebar', | |
| 1372 | - 'description' => esc_html__( 'Widgets in this area will be shown in a single course', 'learnpress' ), | |
| 1373 | - 'before_widget' => '<div id="%1$s" class="widget %2$s">', | |
| 1374 | - 'after_widget' => '</div>', | |
| 1375 | - 'before_title' => '<h3 class="widget-title">', | |
| 1376 | - 'after_title' => '</h3>', | |
| 1377 | - ) | |
| 1378 | - ); | |
| 1379 | - register_sidebar( | |
| 1380 | - array( | |
| 1381 | - 'name' => esc_html__( 'All Courses', 'learnpress' ), | |
| 1382 | - 'id' => 'archive-courses-sidebar', | |
| 1383 | - 'description' => esc_html__( 'Widgets in this area will be shown on all course pages', 'learnpress' ), | |
| 1384 | - 'before_widget' => '<div id="%1$s" class="widget %2$s">', | |
| 1385 | - 'after_widget' => '</div>', | |
| 1386 | - 'before_title' => '<h3 class="widget-title">', | |
| 1387 | - 'after_title' => '</h3>', | |
| 1388 | - ) | |
| 1389 | - ); | |
| 1390 | -} | |
| 1391 | - | |
| 1392 | -add_action( 'widgets_init', 'learn_press_register_sidebars' ); | |
| 1393 | - | |
| 1394 | -function learn_press_setup_theme() { | |
| 1395 | - $support = array( | |
| 1396 | - 'widgets' => array( | |
| 1397 | - // Place three core-defined widgets in the sidebar area. | |
| 1398 | - 'course-sidebar' => array( | |
| 1399 | - 'xxx' => array( 'lp-widget-course-progress' ), | |
| 1400 | - 'yyy' => array( 'lp-widget-course-info' ), | |
| 1401 | - ), | |
| 1402 | - ), | |
| 1403 | - ); | |
| 1404 | - | |
| 1405 | - add_theme_support( 'starter-content', $support ); | |
| 1406 | -} | |
| 1407 | - | |
| 1408 | -add_action( 'after_setup_theme', 'learn_press_setup_theme' ); | |
| 1409 | - | |
| 1410 | -/** | |
| 1411 | - * @param LP_Question $question | |
| 1412 | - * @param array $args | |
| 1413 | - * | |
| 1414 | - * @return array | |
| 1415 | - * @since 4.x.x | |
| 1416 | - */ | |
| 1417 | -function learn_press_get_question_options_for_js( $question, $args = array() ) { | |
| 1418 | - $args = wp_parse_args( | |
| 1419 | - $args, | |
| 1420 | - array( | |
| 1421 | - 'cryptoJsAes' => false, | |
| 1422 | - 'include_is_true' => true, | |
| 1423 | - 'answer' => false, | |
| 1424 | - ) | |
| 1425 | - ); | |
| 1426 | - | |
| 1427 | - if ( $args['cryptoJsAes'] ) { | |
| 1428 | - $options = array_values( $question->get_answer_options() ); | |
| 1429 | - | |
| 1430 | - $key = uniqid(); | |
| 1431 | - $options = array( | |
| 1432 | - 'data' => cryptoJsAesEncrypt( $key, wp_json_encode( $options ) ), | |
| 1433 | - 'key' => $key, | |
| 1434 | - ); | |
| 1435 | - } else { | |
| 1436 | - $exclude_option_key = array( 'question_id', 'order' ); | |
| 1437 | - if ( ! $args['include_is_true'] ) { | |
| 1438 | - $exclude_option_key[] = 'is_true'; | |
| 1439 | - } | |
| 1440 | - | |
| 1441 | - $options = array_values( | |
| 1442 | - $question->get_answer_options( | |
| 1443 | - array( | |
| 1444 | - 'exclude' => $exclude_option_key, | |
| 1445 | - 'map' => array( 'question_answer_id' => 'uid' ), | |
| 1446 | - 'answer' => $args['answer'], | |
| 1447 | - ) | |
| 1448 | - ) | |
| 1449 | - ); | |
| 1450 | - } | |
| 1451 | - | |
| 1452 | - return $options; | |
| 1453 | -} | |
| 1454 | - | |
| 1455 | -function learn_press_custom_excerpt_length( $length ) { | |
| 1456 | - return 20; | |
| 1457 | -} | |
| 1458 | - | |
| 1459 | -/** | |
| 1460 | - * Get post meta with key _lp_duration and translate. | |
| 1461 | - * | |
| 1462 | - * @param int $post_id | |
| 1463 | - * @param string $default | |
| 1464 | - * | |
| 1465 | - * @return string | |
| 1466 | - * @since 4.0.0 | |
| 1467 | - */ | |
| 1468 | -function learn_press_get_post_translated_duration( $post_id, $default = '' ) { | |
| 1469 | - $duration = get_post_meta( $post_id, '_lp_duration', true ); | |
| 1470 | - | |
| 1471 | - $duration_arr = explode( ' ', $duration ); | |
| 1472 | - $duration_str = ''; | |
| 1473 | - | |
| 1474 | - if ( count( $duration_arr ) > 1 ) { | |
| 1475 | - $duration_number = $duration_arr[0]; | |
| 1476 | - $duration_text = $duration_arr[1]; | |
| 1477 | - | |
| 1478 | - switch ( strtolower( $duration_text ) ) { | |
| 1479 | - case 'minute': | |
| 1480 | - $duration_str = sprintf( | |
| 1481 | - _n( '%s minute', '%s minutes', $duration_number, 'learnpress' ), | |
| 1482 | - $duration_number | |
| 1483 | - ); | |
| 1484 | - break; | |
| 1485 | - case 'hour': | |
| 1486 | - $duration_str = sprintf( | |
| 1487 | - _n( '%s hour', '%s hours', $duration_number, 'learnpress' ), | |
| 1488 | - $duration_number | |
| 1489 | - ); | |
| 1490 | - break; | |
| 1491 | - case 'day': | |
| 1492 | - $duration_str = sprintf( _n( '%s day', '%s days', $duration_number, 'learnpress' ), $duration_number ); | |
| 1493 | - break; | |
| 1494 | - case 'week': | |
| 1495 | - $duration_str = sprintf( | |
| 1496 | - _n( '%s week', '%s weeks', $duration_number, 'learnpress' ), | |
| 1497 | - $duration_number | |
| 1498 | - ); | |
| 1499 | - break; | |
| 1500 | - default: | |
| 1501 | - $duration_str = $duration; | |
| 1502 | - } | |
| 1503 | - } | |
| 1504 | - | |
| 1505 | - return empty( absint( $duration ) ) ? $default : $duration_str; | |
| 1506 | -} | |
| 1507 | - | |
| 1508 | -/** | |
| 1509 | - * Get level post meta. | |
| 1510 | - * | |
| 1511 | - * @param int $post_id | |
| 1512 | - * | |
| 1513 | - * @return string | |
| 1514 | - */ | |
| 1515 | -function learn_press_get_post_level( $post_id ) { | |
| 1516 | - $level = get_post_meta( $post_id, '_lp_level', true ); | |
| 1517 | - | |
| 1518 | - return apply_filters( | |
| 1519 | - 'learn-press/level-label', | |
| 1520 | - ! empty( $level ) ? lp_course_level()[ $level ] : esc_html__( 'All levels', 'learnpress' ), | |
| 1521 | - $post_id | |
| 1522 | - ); | |
| 1523 | -} | |
| 1524 | - | |
| 1525 | -function lp_course_level() { | |
| 1526 | - return apply_filters( | |
| 1527 | - 'lp/template/function/course/level', | |
| 1528 | - array( | |
| 1529 | - 'all' => esc_html__( 'All levels', 'learnpress' ), | |
| 1530 | - 'beginner' => esc_html__( 'Beginner', 'learnpress' ), | |
| 1531 | - 'intermediate' => esc_html__( 'Intermediate', 'learnpress' ), | |
| 1532 | - 'expert' => esc_html__( 'Expert', 'learnpress' ), | |
| 1533 | - ) | |
| 1534 | - ); | |
| 1535 | -} | |
| 1536 | - | |
| 1537 | -/** | |
| 1538 | - * Get slug for logout action in user profile. | |
| 1539 | - * | |
| 1540 | - * @return string | |
| 1541 | - * @since 4.0.0 | |
| 1542 | - */ | |
| 1543 | -function learn_press_profile_logout_slug() { | |
| 1544 | - return apply_filters( 'learn-press/profile-logout-slug', 'lp-logout' ); | |
| 1545 | -} | |
| 1546 | - | |
| 1547 | -function lp_get_email_content( $format, $meta = array(), $field = array() ) { | |
| 1548 | - if ( $meta && isset( $meta[ $format ] ) ) { | |
| 1549 | - $content = stripslashes( $meta[ $format ] ); | |
| 1550 | - } else { | |
| 1551 | - $template = ! empty( $field["template_{$format}"] ) ? $field["template_{$format}"] : null; | |
| 1552 | - $template_file = $field['template_base'] . $template; | |
| 1553 | - $content = LP_WP_Filesystem::instance()->file_get_contents( $template_file ); | |
| 1554 | - } | |
| 1555 | - | |
| 1556 | - return $content; | |
| 1557 | -} | |
| 1558 | - | |
| 1559 | -function lp_skeleton_animation_html( $count_li = 3, $width = 'random', $styleli = '', $styleul = '' ) { | |
| 1560 | - ?> | |
| 1561 | - <ul class="lp-skeleton-animation" style="<?php echo esc_attr( $styleul ); ?>"> | |
| 1562 | - <?php for ( $i = 0; $i < absint( $count_li ); $i ++ ) : ?> | |
| 1563 | - <li style="width: <?php echo esc_attr( $width === 'random' ? wp_rand( 90, 100 ) . '%' : $width ); ?>; <?php echo ! empty( $styleli ) ? $styleli : ''; ?>"></li> | |
| 1564 | - <?php endfor; ?> | |
| 1565 | - </ul> | |
| 1566 | - | |
| 1567 | - <?php | |
| 1568 | -} | |
| 1569 | - | |
| 1570 | -add_filter( 'lp_format_page_content', 'wptexturize' ); | |
| 1571 | -add_filter( 'lp_format_page_content', 'convert_smilies' ); | |
| 1572 | -add_filter( 'lp_format_page_content', 'convert_chars' ); | |
| 1573 | -add_filter( 'lp_format_page_content', 'wpautop' ); | |
| 1574 | -add_filter( 'lp_format_page_content', 'shortcode_unautop' ); | |
| 1575 | -add_filter( 'lp_format_page_content', 'prepend_attachment' ); | |
| 1576 | -add_filter( 'lp_format_page_content', 'do_shortcode', 11 ); | |
| 1577 | -add_filter( 'lp_format_page_content', array( $GLOBALS['wp_embed'], 'run_shortcode' ), 8 ); | |
| 1578 | - | |
| 1579 | -if ( function_exists( 'do_blocks' ) ) { | |
| 1580 | - add_filter( 'lp_format_page_content', 'do_blocks', 9 ); | |
| 1581 | -} | |
| 1582 | - | |
| 1583 | -function lp_format_page_content( $raw_string ) { | |
| 1584 | - return apply_filters( 'lp_format_page_content', $raw_string ); | |
| 1585 | -} | |
| 1586 | - | |
| 1587 | -if ( ! function_exists( 'lp_profile_page_content' ) ) { | |
| 1588 | - function lp_profile_page_content() { | |
| 1589 | - $profile_id = learn_press_get_page_id( 'profile' ); | |
| 1590 | - | |
| 1591 | - if ( $profile_id ) { | |
| 1592 | - $profile_page = get_post( $profile_id ); | |
| 1593 | - | |
| 1594 | - // remove_shortcode( 'learn_press_profile' ); | |
| 1595 | - $description = lp_format_page_content( wp_kses_post( $profile_page->post_content ) ); | |
| 1596 | - | |
| 1597 | - if ( $description ) { | |
| 1598 | - echo '<div class="lp-profile-page__content">' . $description . '</div>'; | |
| 1599 | - } | |
| 1600 | - } | |
| 1601 | - } | |
| 1602 | -} | |
| 1603 | - | |
| 1604 | -if ( ! function_exists( 'lp_archive_course_page_content' ) ) { | |
| 1605 | - add_action( 'lp/template/archive-course/description', 'lp_archive_course_page_content' ); | |
| 1606 | - | |
| 1607 | - function lp_archive_course_page_content() { | |
| 1608 | - if ( is_search() ) { | |
| 1609 | - return; | |
| 1610 | - } | |
| 1611 | - | |
| 1612 | - if ( is_post_type_archive( LP_COURSE_CPT ) | |
| 1613 | - && in_array( absint( get_query_var( 'paged' ) ), array( 0, 1 ), true ) ) { | |
| 1614 | - $profile_id = learn_press_get_page_id( 'courses' ); | |
| 1615 | - | |
| 1616 | - if ( $profile_id ) { | |
| 1617 | - $profile_page = get_post( $profile_id ); | |
| 1618 | - | |
| 1619 | - $description = lp_format_page_content( wp_kses_post( $profile_page->post_content ) ); | |
| 1620 | - if ( $description ) { | |
| 1621 | - echo '<div class="lp-course-page__content">' . $description . '</div>'; | |
| 1622 | - } | |
| 1623 | - } | |
| 1624 | - } | |
| 1625 | - } | |
| 1626 | -} | |
| 1627 | - | |
| 1628 | -if ( ! function_exists( 'lp_taxonomy_archive_course_description' ) ) { | |
| 1629 | - add_action( 'lp/template/archive-course/description', 'lp_taxonomy_archive_course_description' ); | |
| 1630 | - | |
| 1631 | - function lp_taxonomy_archive_course_description() { | |
| 1632 | - | |
| 1633 | - if ( learn_press_is_course_tax() && 0 === absint( get_query_var( 'paged' ) ) ) { | |
| 1634 | - $term = get_queried_object(); | |
| 1635 | - | |
| 1636 | - if ( $term && ! empty( $term->description ) ) { | |
| 1637 | - echo '<div class="lp-archive-course-term-description">' . lp_format_page_content( wp_kses_post( $term->description ) ) . '</div>'; | |
| 1638 | - } | |
| 1639 | - } | |
| 1640 | - } | |
| 1641 | -} | |
| 1642 | - | |
| 1643 | -/** | |
| 1644 | - * Params to query courses on Archive Course. | |
| 1645 | - * | |
| 1646 | - * @return array | |
| 1647 | - */ | |
| 1648 | -function lp_archive_skeleton_get_args(): array { | |
| 1649 | - $args = []; | |
| 1650 | - | |
| 1651 | - if ( ! empty( $_GET ) ) { | |
| 1652 | - $args = $_GET; | |
| 1653 | - } | |
| 1654 | - | |
| 1655 | - global $wp_query; | |
| 1656 | - if ( ! empty( $wp_query->get( 'paged' ) ) ) { | |
| 1657 | - $args['paged'] = $wp_query->get( 'paged' ); | |
| 1658 | - } | |
| 1659 | - | |
| 1660 | - if ( learn_press_is_course_category() || learn_press_is_course_tag() ) { | |
| 1661 | - $cat = get_queried_object(); | |
| 1662 | - | |
| 1663 | - // Info of page | |
| 1664 | - if ( learn_press_is_course_category() ) { | |
| 1665 | - $args['page_term_id_current'] = $cat->term_id; | |
| 1666 | - } elseif ( learn_press_is_course_tag() ) { | |
| 1667 | - $args['page_tag_id_current'] = $cat->term_id; | |
| 1668 | - } | |
| 1669 | - | |
| 1670 | - $args['page_term_url'] = get_term_link( $cat ); | |
| 1671 | - } | |
| 1672 | - | |
| 1673 | - $args = apply_filters( 'lp/template/archive-course/skeleton/args', $args ); | |
| 1674 | - if ( ! is_array( $args ) ) { | |
| 1675 | - $args = []; | |
| 1676 | - } | |
| 1677 | - | |
| 1678 | - return LP_Helper::sanitize_params_submitted( $args, 'sanitize_text_field' ); | |
| 1679 | -} | |
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * All functions for LearnPress template | |
| 4 | + * | |
| 5 | + * @author ThimPress | |
| 6 | + * @package LearnPress/Functions | |
| 7 | + * @version 1.0 | |
| 8 | + */ | |
| 9 | + | |
| 10 | +use LearnPress\Helpers\Template; | |
| 11 | +use LearnPress\Models\CourseModel; | |
| 12 | +use LearnPress\Models\CoursePostModel; | |
| 13 | +use LearnPress\Models\UserItems\UserCourseModel; | |
| 14 | +use LearnPress\Models\UserModel; | |
| 15 | +use LearnPress\TemplateHooks\Course\CourseMaterialTemplate; | |
| 16 | +use LearnPress\TemplateHooks\Course\SingleCourseTemplate; | |
| 17 | + | |
| 18 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 19 | + exit; | |
| 20 | +} | |
| 21 | + | |
| 22 | +if ( ! function_exists( 'learn_press_add_course_buttons' ) ) { | |
| 23 | + function learn_press_add_course_buttons() { | |
| 24 | + _deprecated_function( __FUNCTION__, '4.3.2.4' ); | |
| 25 | + } | |
| 26 | +} | |
| 27 | + | |
| 28 | +if ( ! function_exists( 'learn_press_remove_course_buttons' ) ) { | |
| 29 | + function learn_press_remove_course_buttons() { | |
| 30 | + _deprecated_function( __FUNCTION__, '4.3.2.4' ); | |
| 31 | + } | |
| 32 | +} | |
| 33 | + | |
| 34 | +if ( ! function_exists( 'learn_press_get_course_tabs' ) ) { | |
| 35 | + /** | |
| 36 | + * Return an array of tabs display in single course page. | |
| 37 | + * | |
| 38 | + * @return array | |
| 39 | + * @throws Exception | |
| 40 | + */ | |
| 41 | + function learn_press_get_course_tabs() { | |
| 42 | + $courseModel = CourseModel::find( get_the_ID(), true ); | |
| 43 | + if ( ! $courseModel ) { | |
| 44 | + return []; | |
| 45 | + } | |
| 46 | + | |
| 47 | + $userModel = UserModel::find( get_current_user_id(), true ); | |
| 48 | + | |
| 49 | + $defaults = []; | |
| 50 | + $defaults['overview'] = [ | |
| 51 | + 'title' => esc_html__( 'Overview', 'learnpress' ), | |
| 52 | + 'priority' => 10, | |
| 53 | + 'callback' => LearnPress::instance()->template( 'course' )->callback( 'single-course/tabs/overview.php' ), | |
| 54 | + ]; | |
| 55 | + $defaults['curriculum'] = [ | |
| 56 | + 'title' => esc_html__( 'Curriculum', 'learnpress' ), | |
| 57 | + 'priority' => 30, | |
| 58 | + 'callback' => function () use ( $courseModel, $userModel ) { | |
| 59 | + $singleCourseTemplate = SingleCourseTemplate::instance(); | |
| 60 | + echo $singleCourseTemplate->html_curriculum( $courseModel, $userModel ); | |
| 61 | + }, | |
| 62 | + ]; | |
| 63 | + $defaults['instructor'] = [ | |
| 64 | + 'title' => esc_html__( 'Instructor', 'learnpress' ), | |
| 65 | + 'priority' => 40, | |
| 66 | + 'callback' => LearnPress::instance()->template( 'course' )->callback( 'single-course/tabs/instructor.php' ), | |
| 67 | + ]; | |
| 68 | + | |
| 69 | + $faq = $courseModel->get_meta_value_by_key( CoursePostModel::META_KEY_FAQS, [] ); | |
| 70 | + if ( ! empty( $faq ) ) { | |
| 71 | + $defaults['faqs'] = [ | |
| 72 | + 'title' => esc_html__( 'FAQs', 'learnpress' ), | |
| 73 | + 'priority' => 50, | |
| 74 | + 'callback' => function () use ( $courseModel ) { | |
| 75 | + $singleCourseTemplate = SingleCourseTemplate::instance(); | |
| 76 | + echo $singleCourseTemplate->html_faqs( $courseModel, [ 'show_heading' => false ] ); | |
| 77 | + }, | |
| 78 | + ]; | |
| 79 | + } | |
| 80 | + | |
| 81 | + $singleCourseTemplate = SingleCourseTemplate::instance(); | |
| 82 | + $html_material = $singleCourseTemplate->html_material( $courseModel, $userModel, [ 'show_heading' => false ] ); | |
| 83 | + if ( ! empty( $html_material ) ) { | |
| 84 | + $defaults['materials'] = [ | |
| 85 | + 'title' => esc_html__( 'Materials', 'learnpress' ), | |
| 86 | + 'priority' => 45, | |
| 87 | + 'callback' => function () use ( $html_material ) { | |
| 88 | + echo $html_material; | |
| 89 | + }, | |
| 90 | + ]; | |
| 91 | + } | |
| 92 | + | |
| 93 | + // @since 4.2.8.7.1 update new parameter $courseModel | |
| 94 | + $tabs = apply_filters( 'learn-press/course-tabs', $defaults, $courseModel ); | |
| 95 | + | |
| 96 | + if ( ! empty( $tabs ) ) { | |
| 97 | + $has_tab_active = false; | |
| 98 | + | |
| 99 | + // sort tabs by priority, from low to high | |
| 100 | + uasort( | |
| 101 | + $tabs, | |
| 102 | + function ( $a, $b ) { | |
| 103 | + $priority1 = $a['priority'] ?? 1; | |
| 104 | + $priority2 = $b['priority'] ?? 2; | |
| 105 | + | |
| 106 | + return ( $priority1 < $priority2 ) ? - 1 : 1; | |
| 107 | + } | |
| 108 | + ); | |
| 109 | + | |
| 110 | + foreach ( $tabs as $k => $v ) { | |
| 111 | + $v['id'] = $v['id'] ?? 'tab-' . $k; | |
| 112 | + if ( ! empty( $v['active'] ) ) { | |
| 113 | + $has_tab_active = true; | |
| 114 | + } | |
| 115 | + | |
| 116 | + $tabs[ $k ] = $v; | |
| 117 | + } | |
| 118 | + | |
| 119 | + // Check if there is no tab active, set first tab active | |
| 120 | + if ( ! $has_tab_active ) { | |
| 121 | + // set first tab active | |
| 122 | + $first_key = array_key_first( $tabs ); | |
| 123 | + if ( $first_key ) { | |
| 124 | + $tabs[ $first_key ]['active'] = true; | |
| 125 | + } | |
| 126 | + } | |
| 127 | + } | |
| 128 | + | |
| 129 | + return $tabs; | |
| 130 | + } | |
| 131 | +} | |
| 132 | + | |
| 133 | +if ( ! function_exists( 'learn_press_content_item_summary_question' ) ) { | |
| 134 | + function learn_press_content_item_summary_question() { | |
| 135 | + $quiz = LP_Global::course_item_quiz(); | |
| 136 | + $question = $quiz->get_viewing_question(); | |
| 137 | + | |
| 138 | + if ( $question ) { | |
| 139 | + $course = learn_press_get_course(); | |
| 140 | + $user = learn_press_get_current_user(); | |
| 141 | + $answered = false; | |
| 142 | + $course_data = $user->get_course_data( $course->get_id() ); | |
| 143 | + $user_quiz = $course_data->get_item_quiz( $quiz->get_id() ); | |
| 144 | + | |
| 145 | + if ( $user_quiz ) { | |
| 146 | + $answered = $user_quiz->get_question_answer( $question->get_id() ); | |
| 147 | + $question->show_correct_answers( | |
| 148 | + $user->has_checked_answer( | |
| 149 | + $question->get_id(), | |
| 150 | + $quiz->get_id(), | |
| 151 | + $course->get_id() | |
| 152 | + ) ? 'yes' : false | |
| 153 | + ); | |
| 154 | + $question->disable_answers( $user_quiz->get_status() == 'completed' ? 'yes' : false ); | |
| 155 | + } | |
| 156 | + | |
| 157 | + $question->render( $answered ); | |
| 158 | + } | |
| 159 | + } | |
| 160 | +} | |
| 161 | + | |
| 162 | + | |
| 163 | +if ( ! function_exists( 'learn_press_content_item_body_class' ) ) { | |
| 164 | + // Add more assets into page that displaying content of an item | |
| 165 | + add_filter( 'body_class', 'learn_press_content_item_body_class', 10 ); | |
| 166 | + | |
| 167 | + function learn_press_content_item_body_class( $classes ) { | |
| 168 | + global $lp_course_item; | |
| 169 | + | |
| 170 | + if ( wp_is_mobile() ) { | |
| 171 | + $sidebar_toggle_class = 'lp-sidebar-toggle__close'; | |
| 172 | + } else { | |
| 173 | + $sidebar_toggle_class = learn_press_cookie_get( 'sidebar-toggle' ) ? 'lp-sidebar-toggle__close' : 'lp-sidebar-toggle__open'; | |
| 174 | + } | |
| 175 | + | |
| 176 | + if ( $lp_course_item ) { | |
| 177 | + $classes[] = 'course-item-popup'; | |
| 178 | + $classes[] = 'viewing-course-item'; | |
| 179 | + $classes[] = 'viewing-course-item-' . $lp_course_item->get_id(); | |
| 180 | + $classes[] = 'course-item-' . $lp_course_item->get_item_type(); | |
| 181 | + $classes[] = $sidebar_toggle_class; | |
| 182 | + } | |
| 183 | + | |
| 184 | + return $classes; | |
| 185 | + } | |
| 186 | +} | |
| 187 | + | |
| 188 | +if ( ! function_exists( 'learn_press_content_item_edit_links' ) ) { | |
| 189 | + /** | |
| 190 | + * Add edit links for course item question to admin bar. | |
| 191 | + */ | |
| 192 | + function learn_press_content_item_edit_links() { | |
| 193 | + global $wp_admin_bar, $post, $lp_course_item, $lp_quiz_question; | |
| 194 | + | |
| 195 | + if ( ! ( ! is_admin() && is_user_logged_in() ) ) { | |
| 196 | + return; | |
| 197 | + } | |
| 198 | + | |
| 199 | + /** | |
| 200 | + * Edit link for lesson/quiz or any other course's item. | |
| 201 | + */ | |
| 202 | + if ( $lp_course_item ) { | |
| 203 | + $post_type_object = get_post_type_object( $lp_course_item->get_item_type() ); | |
| 204 | + | |
| 205 | + if ( $post_type_object && current_user_can( | |
| 206 | + 'edit_post', | |
| 207 | + $lp_course_item->get_id() | |
| 208 | + ) && $post_type_object->show_in_admin_bar && get_edit_post_link( $lp_course_item->get_id() ) ) { | |
| 209 | + $type = get_post_type( $lp_course_item->get_id() ); | |
| 210 | + | |
| 211 | + if ( apply_filters( 'learn-press/edit-admin-bar-button', true, $lp_course_item ) ) { | |
| 212 | + $wp_admin_bar->add_menu( | |
| 213 | + array( | |
| 214 | + 'id' => 'edit-' . $type, | |
| 215 | + 'title' => $post_type_object->labels->edit_item, | |
| 216 | + 'href' => get_edit_post_link( $lp_course_item->get_id() ), | |
| 217 | + ) | |
| 218 | + ); | |
| 219 | + } | |
| 220 | + } | |
| 221 | + } | |
| 222 | + | |
| 223 | + /** | |
| 224 | + * Edit link for quiz's question. | |
| 225 | + */ | |
| 226 | + if ( $lp_quiz_question ) { | |
| 227 | + $post_type_object = get_post_type_object( $lp_quiz_question->get_item_type() ); | |
| 228 | + $edit_post_link = get_edit_post_link( $lp_quiz_question->get_id() ); | |
| 229 | + | |
| 230 | + if ( $post_type_object && current_user_can( | |
| 231 | + 'edit_post', | |
| 232 | + $lp_quiz_question->get_id() | |
| 233 | + ) && $post_type_object->show_in_admin_bar && $edit_post_link ) { | |
| 234 | + $type = get_post_type( $lp_quiz_question->get_id() ); | |
| 235 | + $wp_admin_bar->add_menu( | |
| 236 | + array( | |
| 237 | + 'id' => 'edit-' . $type, | |
| 238 | + 'title' => $post_type_object->labels->edit_item, | |
| 239 | + 'href' => $edit_post_link, | |
| 240 | + ) | |
| 241 | + ); | |
| 242 | + } | |
| 243 | + } | |
| 244 | + } | |
| 245 | +} | |
| 246 | +add_filter( 'admin_bar_menu', 'learn_press_content_item_edit_links', 90 ); | |
| 247 | + | |
| 248 | +if ( ! function_exists( 'learn_press_single_quiz_args' ) ) { | |
| 249 | + function learn_press_single_quiz_args() { | |
| 250 | + $args = array(); | |
| 251 | + | |
| 252 | + if ( LP_PAGE_QUIZ !== LP_Page_Controller::page_current() ) { | |
| 253 | + return $args; | |
| 254 | + } | |
| 255 | + | |
| 256 | + $quiz = LP_Global::course_item_quiz(); | |
| 257 | + $course = learn_press_get_course(); | |
| 258 | + $user = learn_press_get_current_user(); | |
| 259 | + if ( $quiz && $course ) { | |
| 260 | + $course_id = $course->get_id(); | |
| 261 | + //$user_quiz = $user->get_item_data( $quiz->get_id(), $course_id ); | |
| 262 | + | |
| 263 | + /*if ( $user_quiz ) { | |
| 264 | + $remaining_time = $user_quiz->get_time_remaining(); | |
| 265 | + } else { | |
| 266 | + $remaining_time = false; | |
| 267 | + }*/ | |
| 268 | + | |
| 269 | + $args = array( | |
| 270 | + 'id' => $quiz->get_id(), | |
| 271 | + //'totalTime' => -1, | |
| 272 | + //'remainingTime' => $remaining_time ? $remaining_time->get() : $quiz->get_duration()->get(), | |
| 273 | + 'status' => $user->get_item_status( $quiz->get_id(), $course_id ), | |
| 274 | + 'checkNorequizenroll' => $course->is_no_required_enroll(), | |
| 275 | + 'navigationPosition' => LP_Settings::get_option( 'navigation_position', 'yes' ), | |
| 276 | + ); | |
| 277 | + } | |
| 278 | + | |
| 279 | + return apply_filters( 'learn-press/localize_script/quiz', $args, $user, $course, $quiz ); | |
| 280 | + } | |
| 281 | +} | |
| 282 | + | |
| 283 | +if ( ! function_exists( 'learn_press_single_document_title_parts' ) ) { | |
| 284 | + /** | |
| 285 | + * Custom document title depending on LP current page. | |
| 286 | + * E.g: Single course, profile, etc... | |
| 287 | + * | |
| 288 | + * @param array $title | |
| 289 | + * | |
| 290 | + * @return array | |
| 291 | + * @since 3.0.0 | |
| 292 | + * @version 3.0.1 | |
| 293 | + */ | |
| 294 | + function learn_press_single_document_title_parts( $title ) { | |
| 295 | + if ( learn_press_is_course() ) { | |
| 296 | + $item = LP_Global::course_item(); | |
| 297 | + | |
| 298 | + if ( $item ) { | |
| 299 | + $title['title'] = join( | |
| 300 | + ' ', | |
| 301 | + apply_filters( | |
| 302 | + 'learn-press/document-course-title-parts', | |
| 303 | + array( | |
| 304 | + $title['title'], | |
| 305 | + ' → ', | |
| 306 | + $item->get_title(), | |
| 307 | + ) | |
| 308 | + ) | |
| 309 | + ); | |
| 310 | + } | |
| 311 | + } elseif ( learn_press_is_courses() ) { | |
| 312 | + if ( learn_press_is_search() ) { | |
| 313 | + $title['title'] = esc_html__( 'Course Search Results', 'learnpress' ); | |
| 314 | + } else { | |
| 315 | + $title['title'] = esc_html__( 'Courses', 'learnpress' ); | |
| 316 | + } | |
| 317 | + } elseif ( LP_Page_Controller::is_page_profile() ) { | |
| 318 | + $profile = LP_Profile::instance(); | |
| 319 | + $tab_slug = $profile->get_current_tab(); | |
| 320 | + $tab = $profile->get_tab_at( $tab_slug ); | |
| 321 | + $page_id = learn_press_get_page_id( 'profile' ); | |
| 322 | + | |
| 323 | + if ( $page_id ) { | |
| 324 | + $page_title = get_the_title( $page_id ); | |
| 325 | + } else { | |
| 326 | + $page_title = ''; | |
| 327 | + } | |
| 328 | + | |
| 329 | + if ( $tab instanceof LP_Profile_Tab ) { | |
| 330 | + $title['title'] = join( | |
| 331 | + ' ', | |
| 332 | + apply_filters( | |
| 333 | + 'learn-press/document-profile-title-parts', | |
| 334 | + array( | |
| 335 | + $page_title, | |
| 336 | + '→', | |
| 337 | + $tab->get( 'title' ), | |
| 338 | + ) | |
| 339 | + ) | |
| 340 | + ); | |
| 341 | + } | |
| 342 | + } | |
| 343 | + | |
| 344 | + return $title; | |
| 345 | + } | |
| 346 | +} | |
| 347 | + | |
| 348 | +// @deprecated 4.2.7.3 | |
| 349 | +/*if ( ! function_exists( 'learn_press_course_item_class' ) ) { | |
| 350 | + function learn_press_course_item_class( $item_id, $course_id = 0, $class = null ) { | |
| 351 | + switch ( get_post_type( $item_id ) ) { | |
| 352 | + case 'lp_lesson': | |
| 353 | + learn_press_course_lesson_class( $item_id, $course_id, $class ); | |
| 354 | + break; | |
| 355 | + case 'lp_quiz': | |
| 356 | + learn_press_course_quiz_class( $item_id, $course_id, $class ); | |
| 357 | + break; | |
| 358 | + } | |
| 359 | + } | |
| 360 | +}*/ | |
| 361 | + | |
| 362 | +// @deprecated 4.2.7.3 | |
| 363 | +//if ( ! function_exists( 'learn_press_course_lesson_class' ) ) { | |
| 364 | +// /** | |
| 365 | +// * The class of lesson in course curriculum | |
| 366 | +// * | |
| 367 | +// * @param int $lesson_id | |
| 368 | +// * @param int $course_id | |
| 369 | +// * @param array|string $class | |
| 370 | +// * @param boolean $echo | |
| 371 | +// * | |
| 372 | +// * @return mixed | |
| 373 | +// */ | |
| 374 | +// function learn_press_course_lesson_class( $lesson_id = null, $course_id = 0, $class = null, $echo = true ) { | |
| 375 | +// $user = learn_press_get_current_user(); | |
| 376 | +// | |
| 377 | +// if ( ! $course_id ) { | |
| 378 | +// $course_id = get_the_ID(); | |
| 379 | +// } | |
| 380 | +// | |
| 381 | +// $course = learn_press_get_course( $course_id ); | |
| 382 | +// if ( ! $course ) { | |
| 383 | +// return ''; | |
| 384 | +// } | |
| 385 | +// | |
| 386 | +// if ( is_string( $class ) && $class ) { | |
| 387 | +// $class = preg_split( '!\s+!', $class ); | |
| 388 | +// } else { | |
| 389 | +// $class = array(); | |
| 390 | +// } | |
| 391 | +// | |
| 392 | +// $classes = array( | |
| 393 | +// 'course-lesson course-item course-item-' . $lesson_id, | |
| 394 | +// ); | |
| 395 | +// | |
| 396 | +// $user = learn_press_get_current_user(); | |
| 397 | +// $status = $user->get_item_status( $lesson_id ); | |
| 398 | +// | |
| 399 | +// if ( $status ) { | |
| 400 | +// $classes[] = "item-has-status item-{$status}"; | |
| 401 | +// } | |
| 402 | +// | |
| 403 | +// if ( $lesson_id && $course->is_current_item( $lesson_id ) ) { | |
| 404 | +// $classes[] = 'item-current'; | |
| 405 | +// } | |
| 406 | +// | |
| 407 | +// if ( learn_press_is_course() ) { | |
| 408 | +// if ( $course->is_free() ) { | |
| 409 | +// $classes[] = 'free-item'; | |
| 410 | +// } | |
| 411 | +// } | |
| 412 | +// | |
| 413 | +// $lesson = LP_Lesson::get_lesson( $lesson_id ); | |
| 414 | +// | |
| 415 | +// if ( $lesson && $lesson->is_preview() ) { | |
| 416 | +// $classes[] = 'preview-item'; | |
| 417 | +// } | |
| 418 | +// | |
| 419 | +// $classes = array_unique( array_merge( $classes, $class ) ); | |
| 420 | +// | |
| 421 | +// if ( $echo ) { | |
| 422 | +// echo 'class="' . implode( ' ', $classes ) . '"'; | |
| 423 | +// } | |
| 424 | +// | |
| 425 | +// return $classes; | |
| 426 | +// } | |
| 427 | +//} | |
| 428 | + | |
| 429 | +// @deprecated 4.2.7.3 | |
| 430 | +//if ( ! function_exists( 'learn_press_course_quiz_class' ) ) { | |
| 431 | +// /** | |
| 432 | +// * The class of lesson in course curriculum | |
| 433 | +// * | |
| 434 | +// * @param int $quiz_id | |
| 435 | +// * @param int $course_id | |
| 436 | +// * @param string|array $class | |
| 437 | +// * @param boolean $echo | |
| 438 | +// * | |
| 439 | +// * @return mixed | |
| 440 | +// */ | |
| 441 | +// function learn_press_course_quiz_class( $quiz_id = null, $course_id = 0, $class = null, $echo = true ) { | |
| 442 | +// $user = learn_press_get_current_user(); | |
| 443 | +// | |
| 444 | +// if ( ! $course_id ) { | |
| 445 | +// $course_id = get_the_ID(); | |
| 446 | +// } | |
| 447 | +// | |
| 448 | +// if ( is_string( $class ) && $class ) { | |
| 449 | +// $class = preg_split( '!\s+!', $class ); | |
| 450 | +// } else { | |
| 451 | +// $class = array(); | |
| 452 | +// } | |
| 453 | +// | |
| 454 | +// $course = learn_press_get_course( $course_id ); | |
| 455 | +// | |
| 456 | +// if ( ! $course ) { | |
| 457 | +// return ''; | |
| 458 | +// } | |
| 459 | +// | |
| 460 | +// $classes = array( | |
| 461 | +// 'course-quiz course-item course-item-' . $quiz_id, | |
| 462 | +// ); | |
| 463 | +// | |
| 464 | +// $status = $user->get_item_status( $quiz_id ); | |
| 465 | +// | |
| 466 | +// if ( $status ) { | |
| 467 | +// $classes[] = "item-has-status item-{$status}"; | |
| 468 | +// } | |
| 469 | +// | |
| 470 | +// if ( $quiz_id && $course->is_current_item( $quiz_id ) ) { | |
| 471 | +// $classes[] = 'item-current'; | |
| 472 | +// } | |
| 473 | +// | |
| 474 | +// /* | |
| 475 | +// if ( $user->can_view_item( $quiz_id, $course_id )->flag ) { | |
| 476 | +// $classes[] = 'viewable'; | |
| 477 | +// }*/ | |
| 478 | +// | |
| 479 | +// if ( $course->get_evaluation_type() == 'evaluate_final_quiz' && $course->is_final_quiz( $quiz_id ) ) { | |
| 480 | +// $classes[] = 'final-quiz'; | |
| 481 | +// } | |
| 482 | +// | |
| 483 | +// $classes = array_unique( array_merge( $classes, $class ) ); | |
| 484 | +// | |
| 485 | +// if ( $echo ) { | |
| 486 | +// echo 'class="' . implode( ' ', $classes ) . '"'; | |
| 487 | +// } | |
| 488 | +// | |
| 489 | +// return $classes; | |
| 490 | +// } | |
| 491 | +//} | |
| 492 | + | |
| 493 | +if ( ! function_exists( 'learn_press_course_class' ) ) { | |
| 494 | + /** | |
| 495 | + * Append new class to course post type | |
| 496 | + * | |
| 497 | + * @param $classes | |
| 498 | + * @param $class | |
| 499 | + * @param $post_id | |
| 500 | + * | |
| 501 | + * @return string | |
| 502 | + */ | |
| 503 | + function learn_press_course_class( $classes, $class, $post_id = '' ) { | |
| 504 | + if ( is_learnpress() ) { | |
| 505 | + $classes = (array) $classes; | |
| 506 | + } | |
| 507 | + | |
| 508 | + if ( $post_id === 0 ) { | |
| 509 | + $classes[] = 'page type-page'; | |
| 510 | + } | |
| 511 | + | |
| 512 | + if ( ! $post_id || 'lp_course' !== get_post_type( $post_id ) ) { | |
| 513 | + return $classes; | |
| 514 | + } | |
| 515 | + | |
| 516 | + $classes[] = 'course'; | |
| 517 | + | |
| 518 | + return apply_filters( 'learn_press_course_class', $classes ); | |
| 519 | + } | |
| 520 | +} | |
| 521 | + | |
| 522 | +function learn_press_setup_user() { | |
| 523 | + $GLOBALS['lp_user'] = learn_press_get_current_user(); | |
| 524 | +} | |
| 525 | + | |
| 526 | +//add_action( 'init', 'learn_press_setup_user', 1000 ); | |
| 527 | + | |
| 528 | +/** | |
| 529 | + * Display a message immediately with out push into queue | |
| 530 | + * | |
| 531 | + * @param $message | |
| 532 | + * @param string $type | |
| 533 | + * | |
| 534 | + * @Todo tungnx review code. | |
| 535 | + */ | |
| 536 | + | |
| 537 | +function learn_press_display_message( $message, $type = 'success' ) { | |
| 538 | + $messages = learn_press_session_get( learn_press_session_message_id() ); | |
| 539 | + learn_press_session_set( learn_press_session_message_id(), null ); | |
| 540 | + | |
| 541 | + // add new notice and display | |
| 542 | + learn_press_add_message( $message, $type ); | |
| 543 | + echo learn_press_get_messages( true ); | |
| 544 | + | |
| 545 | + // store back messages | |
| 546 | + learn_press_session_set( learn_press_session_message_id(), $messages ); | |
| 547 | +} | |
| 548 | + | |
| 549 | +/** | |
| 550 | + * Returns all notices added | |
| 551 | + * | |
| 552 | + * @param bool $clear | |
| 553 | + * | |
| 554 | + * @return string | |
| 555 | + */ | |
| 556 | +function learn_press_get_messages( $clear = false ) { | |
| 557 | + ob_start(); | |
| 558 | + learn_press_print_messages( $clear ); | |
| 559 | + | |
| 560 | + return ob_get_clean(); | |
| 561 | +} | |
| 562 | + | |
| 563 | +/** | |
| 564 | + * Add new message into queue for displaying. | |
| 565 | + * | |
| 566 | + * @param string $message | |
| 567 | + * @param string $type | |
| 568 | + * @param array $options | |
| 569 | + * @param int|bool $current_user . @since 3.0.9 - add for current user only | |
| 570 | + * | |
| 571 | + * @deprecated 4.2.2.1 | |
| 572 | + */ | |
| 573 | +function learn_press_add_message( $message, $type = 'success', $options = array(), $current_user = true ) { | |
| 574 | + if ( is_string( $options ) ) { | |
| 575 | + $options = array( 'id' => $options ); | |
| 576 | + } | |
| 577 | + | |
| 578 | + $options = wp_parse_args( | |
| 579 | + $options, | |
| 580 | + array( | |
| 581 | + 'id' => '', | |
| 582 | + ) | |
| 583 | + ); | |
| 584 | + | |
| 585 | + if ( $current_user ) { | |
| 586 | + if ( true === $current_user ) { | |
| 587 | + $current_user = get_current_user_id(); | |
| 588 | + } | |
| 589 | + } | |
| 590 | + | |
| 591 | + $key = "messages{$current_user}"; | |
| 592 | + | |
| 593 | + $messages = learn_press_session_get( $key ); | |
| 594 | + | |
| 595 | + if ( empty( $messages[ $type ] ) ) { | |
| 596 | + $messages[ $type ] = array(); | |
| 597 | + } | |
| 598 | + | |
| 599 | + $messages[ $type ][ $options['id'] ] = array( | |
| 600 | + 'content' => $message, | |
| 601 | + 'options' => $options, | |
| 602 | + ); | |
| 603 | + | |
| 604 | + learn_press_session_set( $key, $messages ); | |
| 605 | +} | |
| 606 | + | |
| 607 | +function learn_press_get_message( $message, $type = 'success' ) { | |
| 608 | + ob_start(); | |
| 609 | + learn_press_display_message( $message, $type ); | |
| 610 | + $message = ob_get_clean(); | |
| 611 | + | |
| 612 | + return $message; | |
| 613 | +} | |
| 614 | + | |
| 615 | +/** | |
| 616 | + * Set LP message to COOKIE. | |
| 617 | + * | |
| 618 | + * @param array $message_data ['status' => 'success/warning/error', 'content' => 'Message content'] | |
| 619 | + * | |
| 620 | + * @return void | |
| 621 | + * @version 1.0.0 | |
| 622 | + * @since 4.2.0 | |
| 623 | + */ | |
| 624 | +function learn_press_set_message( array $message_data = [] ) { | |
| 625 | + if ( ! isset( $message_data ['status'] ) ) { | |
| 626 | + error_log( 'Message data must have status' ); | |
| 627 | + | |
| 628 | + return; | |
| 629 | + } | |
| 630 | + if ( ! isset( $message_data ['content'] ) ) { | |
| 631 | + error_log( 'Message data must have content' ); | |
| 632 | + | |
| 633 | + return; | |
| 634 | + } | |
| 635 | + | |
| 636 | + $customer_id = LP_Session_Handler::instance()->get_customer_id(); | |
| 637 | + $customer_message = [ $customer_id => $message_data ]; | |
| 638 | + update_option( 'lp-customer-message', $customer_message ); | |
| 639 | +} | |
| 640 | + | |
| 641 | +/** | |
| 642 | + * Show message only one time. | |
| 643 | + * @return void | |
| 644 | + * @version 1.0.1 | |
| 645 | + * @since 4.2.0 | |
| 646 | + */ | |
| 647 | +function learn_press_show_message() { | |
| 648 | + try { | |
| 649 | + $customer_id = LP_Session_Handler::instance()->get_customer_id(); | |
| 650 | + $message_data = get_option( 'lp-customer-message' ) ?? []; | |
| 651 | + $customer_message = $message_data[ $customer_id ] ?? ''; | |
| 652 | + if ( ! $customer_message ) { | |
| 653 | + return; | |
| 654 | + } | |
| 655 | + | |
| 656 | + unset( $message_data[ $customer_id ] ); | |
| 657 | + update_option( 'lp-customer-message', $message_data ); | |
| 658 | + //delete_option( 'lp-message' ); | |
| 659 | + //Template::instance()->get_frontend_template( 'global/lp-message.php', compact( 'customer_message' ) ); | |
| 660 | + Template::print_message( | |
| 661 | + $customer_message['content'] ?? '', | |
| 662 | + $customer_message['status'] ?? '' | |
| 663 | + ); | |
| 664 | + } catch ( Throwable $e ) { | |
| 665 | + error_log( $e->getMessage() ); | |
| 666 | + } | |
| 667 | +} | |
| 668 | + | |
| 669 | +add_action( 'learn-press/before-course-item-content', 'learn_press_show_message', 50 ); | |
| 670 | + | |
| 671 | +/** | |
| 672 | + * Remove message added into queue by id and/or type. | |
| 673 | + * | |
| 674 | + * @param string $id | |
| 675 | + * @param string|array $type | |
| 676 | + * | |
| 677 | + * @since 3.0.0 | |
| 678 | + */ | |
| 679 | +function learn_press_remove_message( $id = '', $type = '' ) { | |
| 680 | + $groups = learn_press_session_get( learn_press_session_message_id() ); | |
| 681 | + | |
| 682 | + if ( ! $groups ) { | |
| 683 | + return; | |
| 684 | + } | |
| 685 | + | |
| 686 | + settype( $type, 'array' ); | |
| 687 | + | |
| 688 | + if ( $id ) { | |
| 689 | + foreach ( $groups as $message_type => $messages ) { | |
| 690 | + if ( ! sizeof( $type ) ) { | |
| 691 | + if ( isset( $groups[ $message_type ][ $id ] ) ) { | |
| 692 | + unset( $groups[ $message_type ][ $id ] ); | |
| 693 | + } | |
| 694 | + } elseif ( in_array( $message_type, $type ) ) { | |
| 695 | + if ( isset( $groups[ $message_type ][ $id ] ) ) { | |
| 696 | + unset( $groups[ $message_type ][ $id ] ); | |
| 697 | + } | |
| 698 | + } | |
| 699 | + } | |
| 700 | + } elseif ( sizeof( $type ) ) { | |
| 701 | + foreach ( $type as $t ) { | |
| 702 | + if ( isset( $groups[ $t ] ) ) { | |
| 703 | + unset( $groups[ $t ] ); | |
| 704 | + } | |
| 705 | + } | |
| 706 | + } else { | |
| 707 | + $groups = array(); | |
| 708 | + } | |
| 709 | + | |
| 710 | + learn_press_session_set( learn_press_session_message_id(), $groups ); | |
| 711 | +} | |
| 712 | + | |
| 713 | +/** | |
| 714 | + * Print out the message stored in the queue | |
| 715 | + * | |
| 716 | + * @param bool | |
| 717 | + */ | |
| 718 | +function learn_press_print_messages( $clear = true ) { | |
| 719 | + $messages = learn_press_session_get( learn_press_session_message_id() ); | |
| 720 | + learn_press_get_template( 'global/message.php', array( 'messages' => $messages ) ); | |
| 721 | + | |
| 722 | + if ( $clear ) { | |
| 723 | + learn_press_session_set( learn_press_session_message_id(), array() ); | |
| 724 | + } | |
| 725 | +} | |
| 726 | + | |
| 727 | +function learn_press_session_message_id() { | |
| 728 | + return 'messages' . get_current_user_id(); | |
| 729 | +} | |
| 730 | + | |
| 731 | +if ( ! function_exists( 'learn_press_page_title' ) ) { | |
| 732 | + | |
| 733 | + /** | |
| 734 | + * learn_press_page_title function. | |
| 735 | + * | |
| 736 | + * @param boolean $echo | |
| 737 | + * | |
| 738 | + * @return string | |
| 739 | + */ | |
| 740 | + function learn_press_page_title( bool $echo = false ): string { | |
| 741 | + $page_title = ''; | |
| 742 | + | |
| 743 | + if ( is_search() ) { | |
| 744 | + // Comment by tungnx | |
| 745 | + /*$page_title = sprintf( __( 'Search Results for: “%s”', 'learnpress' ), get_search_query() ); | |
| 746 | + | |
| 747 | + if ( get_query_var( 'paged' ) ) { | |
| 748 | + $page_title .= sprintf( __( ' – Page %s', 'learnpress' ), get_query_var( 'paged' ) ); | |
| 749 | + }*/ | |
| 750 | + } elseif ( is_tax() ) { | |
| 751 | + $page_title = single_term_title( '', false ); | |
| 752 | + } else { | |
| 753 | + $courses_page_id = learn_press_get_page_id( 'courses' ); | |
| 754 | + $page_title = get_the_title( $courses_page_id ); | |
| 755 | + } | |
| 756 | + | |
| 757 | + return apply_filters( 'learn_press_page_title', $page_title ); | |
| 758 | + } | |
| 759 | +} | |
| 760 | + | |
| 761 | +/** | |
| 762 | + * Get template part. | |
| 763 | + * | |
| 764 | + * @param string $slug | |
| 765 | + * @param string $name | |
| 766 | + * | |
| 767 | + * @return string | |
| 768 | + */ | |
| 769 | +function learn_press_get_template_part( $slug, $name = '' ) { | |
| 770 | + $template = ''; | |
| 771 | + | |
| 772 | + if ( $name ) { | |
| 773 | + $template = locate_template( | |
| 774 | + array( | |
| 775 | + "{$slug}-{$name}.php", | |
| 776 | + learn_press_template_path() . "/{$slug}-{$name}.php", | |
| 777 | + ) | |
| 778 | + ); | |
| 779 | + } | |
| 780 | + | |
| 781 | + // Get default slug-name.php | |
| 782 | + if ( ! $template && $name && file_exists( LP_PLUGIN_PATH . "/templates/{$slug}-{$name}.php" ) ) { | |
| 783 | + $template = LP_PLUGIN_PATH . "/templates/{$slug}-{$name}.php"; | |
| 784 | + } | |
| 785 | + | |
| 786 | + // If template file doesn't exist, look in yourtheme/slug.php and yourtheme/learnpress/slug.php | |
| 787 | + if ( ! $template ) { | |
| 788 | + $template = locate_template( array( "{$slug}.php", learn_press_template_path() . "/{$slug}.php" ) ); | |
| 789 | + } | |
| 790 | + // override path of child theme in parent theme - Fix for eduma by tuanta | |
| 791 | + $file_ct_in_pr = apply_filters( 'learn_press_child_in_parrent_template_path', '' ); | |
| 792 | + if ( $file_ct_in_pr && $name ) { | |
| 793 | + $template_child = locate_template( | |
| 794 | + array( | |
| 795 | + "{$slug}-{$name}.php", | |
| 796 | + 'lp-child-path/' . learn_press_template_path() . '/' . $file_ct_in_pr . "/{$slug}-{$name}.php", | |
| 797 | + ) | |
| 798 | + ); | |
| 799 | + if ( $template_child && file_exists( $template_child ) ) { | |
| 800 | + $template = $template_child; | |
| 801 | + } | |
| 802 | + // check in child theme if have filter learn_press_child_in_parrent_template_path | |
| 803 | + | |
| 804 | + $check_child_theme = get_stylesheet_directory() . '/' . learn_press_template_path() . "{$slug}-{$name}.php"; | |
| 805 | + if ( $check_child_theme && file_exists( $check_child_theme ) ) { | |
| 806 | + $template = $check_child_theme; | |
| 807 | + } | |
| 808 | + } | |
| 809 | + | |
| 810 | + // Allow 3rd party plugin filter template file from their plugin | |
| 811 | + if ( $template ) { | |
| 812 | + $template = apply_filters( 'learn_press_get_template_part', $template, $slug, $name ); | |
| 813 | + } | |
| 814 | + if ( $template && file_exists( $template ) ) { | |
| 815 | + load_template( $template, false ); | |
| 816 | + } | |
| 817 | + | |
| 818 | + return $template; | |
| 819 | +} | |
| 820 | + | |
| 821 | +/** | |
| 822 | + * Get other templates passing attributes and including the file. | |
| 823 | + * | |
| 824 | + * @param string $template_name . | |
| 825 | + * @param array $args (default: array()) . | |
| 826 | + * @param string $template_path (default: ''). | |
| 827 | + * @param string $default_path (default: ''). | |
| 828 | + * | |
| 829 | + * @return void | |
| 830 | + */ | |
| 831 | +function learn_press_get_template( $template_name = '', $args = array(), $template_path = '', $default_path = '' ) { | |
| 832 | + if ( $args && is_array( $args ) ) { | |
| 833 | + extract( $args ); | |
| 834 | + } | |
| 835 | + | |
| 836 | + if ( false === strpos( $template_name, '.php' ) ) { | |
| 837 | + $template_name .= '.php'; | |
| 838 | + } | |
| 839 | + | |
| 840 | + $located = learn_press_locate_template( $template_name, $template_path, $default_path ); | |
| 841 | + | |
| 842 | + if ( ! file_exists( $located ) ) { | |
| 843 | + _doing_it_wrong( __FUNCTION__, sprintf( '<code>%s</code> does not exist.', $located ), '2.1' ); | |
| 844 | + | |
| 845 | + $log = sprintf( 'TEMPLATE MISSING: Template %s doesn\'t exists.', $template_name ); | |
| 846 | + error_log( $log ); | |
| 847 | + | |
| 848 | + if ( LP_Debug::is_debug() ) { | |
| 849 | + echo sprintf( '<span title="%s" class="learn-press-template-warning"></span>', $log ); | |
| 850 | + } | |
| 851 | + | |
| 852 | + return; | |
| 853 | + } | |
| 854 | + | |
| 855 | + // Allow 3rd party plugin filter template file from their plugin | |
| 856 | + $located = apply_filters( | |
| 857 | + 'learn_press_get_template', | |
| 858 | + $located, | |
| 859 | + $template_name, | |
| 860 | + $args, | |
| 861 | + $template_path, | |
| 862 | + $default_path | |
| 863 | + ); | |
| 864 | + if ( $located != '' ) { | |
| 865 | + do_action( 'learn_press_before_template_part', $template_name, $template_path, $located, $args ); | |
| 866 | + | |
| 867 | + include $located; | |
| 868 | + | |
| 869 | + do_action( 'learn_press_after_template_part', $template_name, $template_path, $located, $args ); | |
| 870 | + } | |
| 871 | +} | |
| 872 | + | |
| 873 | +/** | |
| 874 | + * Get template content | |
| 875 | + * | |
| 876 | + * @param $template_name | |
| 877 | + * @param array $args | |
| 878 | + * @param string $template_path | |
| 879 | + * @param string $default_path | |
| 880 | + * | |
| 881 | + * @return string | |
| 882 | + * @uses learn_press_get_template(); | |
| 883 | + */ | |
| 884 | +function learn_press_get_template_content( $template_name, $args = array(), $template_path = '', $default_path = '' ) { | |
| 885 | + ob_start(); | |
| 886 | + learn_press_get_template( $template_name, $args, $template_path, $default_path ); | |
| 887 | + | |
| 888 | + return ob_get_clean(); | |
| 889 | +} | |
| 890 | + | |
| 891 | +/** | |
| 892 | + * Locate a template and return the path for inclusion. | |
| 893 | + * | |
| 894 | + * @param string $template_name | |
| 895 | + * @param string $template_path (default: '') | |
| 896 | + * @param string $default_path (default: '') | |
| 897 | + * | |
| 898 | + * @return string | |
| 899 | + */ | |
| 900 | +function learn_press_locate_template( $template_name, $template_path = '', $default_path = '' ) { | |
| 901 | + if ( ! $template_path ) { | |
| 902 | + $template_path = learn_press_template_path(); | |
| 903 | + } | |
| 904 | + | |
| 905 | + if ( ! $default_path ) { | |
| 906 | + $default_path = LP_PLUGIN_PATH . 'templates/'; | |
| 907 | + } | |
| 908 | + | |
| 909 | + /** | |
| 910 | + * Disable override templates in theme by default since LP 4.0.0 | |
| 911 | + */ | |
| 912 | + if ( learn_press_override_templates() ) { | |
| 913 | + $template = locate_template( | |
| 914 | + array( | |
| 915 | + trailingslashit( $template_path ) . $template_name, | |
| 916 | + $template_name, | |
| 917 | + ) | |
| 918 | + ); | |
| 919 | + // override path of child theme in parent theme - Fix for eduma by tuanta | |
| 920 | + $file_ct_in_pr = apply_filters( 'learn_press_child_in_parrent_template_path', '' ); | |
| 921 | + if ( $file_ct_in_pr ) { | |
| 922 | + $template_child = locate_template( | |
| 923 | + array( | |
| 924 | + trailingslashit( 'lp-child-path/' . $template_path . '/' . $file_ct_in_pr ) . $template_name, | |
| 925 | + $template_name, | |
| 926 | + ) | |
| 927 | + ); | |
| 928 | + if ( $template_child && file_exists( $template_child ) ) { | |
| 929 | + $template = $template_child; | |
| 930 | + } | |
| 931 | + // check in child theme if have filter learn_press_child_in_parrent_template_path | |
| 932 | + $check_child_theme = get_stylesheet_directory() . '/' . trailingslashit( $template_path ) . $template_name; | |
| 933 | + if ( $check_child_theme && file_exists( $check_child_theme ) ) { | |
| 934 | + $template = $check_child_theme; | |
| 935 | + } | |
| 936 | + } | |
| 937 | + } | |
| 938 | + if ( ! isset( $template ) || ! $template ) { | |
| 939 | + $template = trailingslashit( $default_path ) . $template_name; | |
| 940 | + } | |
| 941 | + | |
| 942 | + return apply_filters( 'learn_press_locate_template', $template, $template_name, $template_path ); | |
| 943 | +} | |
| 944 | + | |
| 945 | +/** | |
| 946 | + * Returns the name of folder contains override template files in theme | |
| 947 | + * | |
| 948 | + * @return string | |
| 949 | + * @since 3.0.0 | |
| 950 | + * @version 1.0.1 | |
| 951 | + */ | |
| 952 | +function learn_press_template_path(): string { | |
| 953 | + $lp_folder_name_override = apply_filters( 'learn_press_template_path', LP_PLUGIN_FOLDER_NAME ); | |
| 954 | + if ( ! is_string( $lp_folder_name_override ) ) { | |
| 955 | + $lp_folder_name_override = LP_PLUGIN_FOLDER_NAME; | |
| 956 | + } | |
| 957 | + | |
| 958 | + return $lp_folder_name_override; | |
| 959 | +} | |
| 960 | + | |
| 961 | +/** | |
| 962 | + * Disable override templates in theme by default | |
| 963 | + * | |
| 964 | + * @return bool | |
| 965 | + * @since 4.0.0 | |
| 966 | + */ | |
| 967 | +function learn_press_override_templates() { | |
| 968 | + return apply_filters( 'learn-press/override-templates', false ); | |
| 969 | +} | |
| 970 | + | |
| 971 | +/** | |
| 972 | + * Get html view path for admin to display | |
| 973 | + * | |
| 974 | + * @param $name | |
| 975 | + * @param $plugin_file | |
| 976 | + * | |
| 977 | + * @return mixed | |
| 978 | + */ | |
| 979 | +function learn_press_get_admin_view( $name, $plugin_file = null ) { | |
| 980 | + if ( ! preg_match( '/\.(html|php)$/', $name ) ) { | |
| 981 | + $name .= '.php'; | |
| 982 | + } | |
| 983 | + if ( $plugin_file ) { | |
| 984 | + $view = dirname( $plugin_file ) . '/inc/admin/views/' . $name; | |
| 985 | + } else { | |
| 986 | + $view = LearnPress::instance()->plugin_path( 'inc/admin/views/' . $name ); | |
| 987 | + } | |
| 988 | + | |
| 989 | + return apply_filters( 'learn_press_admin_view', $view, $name ); | |
| 990 | +} | |
| 991 | + | |
| 992 | +function learn_press_admin_view_content( $name, $args = array() ) { | |
| 993 | + return learn_press_admin_view( $name, $args, false, true ); | |
| 994 | +} | |
| 995 | + | |
| 996 | +/** | |
| 997 | + * Find a full path of a view and display the content in admin | |
| 998 | + * | |
| 999 | + * @param $name | |
| 1000 | + * @param array $args | |
| 1001 | + * @param bool|false $include_once | |
| 1002 | + * @param bool | |
| 1003 | + * | |
| 1004 | + * @return bool | |
| 1005 | + */ | |
| 1006 | +function learn_press_admin_view( $name, $args = array(), $include_once = false, $return = false ) { | |
| 1007 | + $view = learn_press_get_admin_view( $name, ! empty( $args['plugin_file'] ) ? $args['plugin_file'] : null ); | |
| 1008 | + | |
| 1009 | + if ( file_exists( $view ) ) { | |
| 1010 | + | |
| 1011 | + ob_start(); | |
| 1012 | + | |
| 1013 | + is_array( $args ) && extract( $args ); | |
| 1014 | + | |
| 1015 | + do_action( 'learn_press_before_display_admin_view', $name, $args ); | |
| 1016 | + | |
| 1017 | + if ( $include_once ) { | |
| 1018 | + include_once $view; | |
| 1019 | + } else { | |
| 1020 | + include $view; | |
| 1021 | + } | |
| 1022 | + | |
| 1023 | + do_action( 'learn_press_after_display_admin_view', $name, $args ); | |
| 1024 | + $output = ob_get_clean(); | |
| 1025 | + | |
| 1026 | + if ( ! $return ) { | |
| 1027 | + learn_press_echo_vuejs_write_on_php( $output ); | |
| 1028 | + } | |
| 1029 | + | |
| 1030 | + return $return ? $output : true; | |
| 1031 | + } | |
| 1032 | + | |
| 1033 | + return false; | |
| 1034 | +} | |
| 1035 | + | |
| 1036 | +if ( ! function_exists( 'learn_press_is_404' ) ) { | |
| 1037 | + /** | |
| 1038 | + * Set header is 404 | |
| 1039 | + * @deprecated 4.2.8 | |
| 1040 | + */ | |
| 1041 | + function learn_press_is_404() { | |
| 1042 | + _deprecated_function( __FUNCTION__, '4.2.8', 'LP_Page_Controller::set_page_404' ); | |
| 1043 | + | |
| 1044 | + return; | |
| 1045 | + global $wp_query; | |
| 1046 | + $wp_query->set_404(); | |
| 1047 | + status_header( 404 ); | |
| 1048 | + } | |
| 1049 | +} | |
| 1050 | + | |
| 1051 | +if ( ! function_exists( 'learn_press_404_page' ) ) { | |
| 1052 | + /** | |
| 1053 | + * Display 404 page | |
| 1054 | + * | |
| 1055 | + * @deprecated 4.2.8 | |
| 1056 | + */ | |
| 1057 | + function learn_press_404_page() { | |
| 1058 | + _deprecated_function( __FUNCTION__, '4.2.8', 'LP_Page_Controller::set_page_404' ); | |
| 1059 | + | |
| 1060 | + return; | |
| 1061 | + learn_press_is_404(); | |
| 1062 | + } | |
| 1063 | +} | |
| 1064 | + | |
| 1065 | +if ( ! function_exists( 'learn_press_generate_template_information' ) ) { | |
| 1066 | + function learn_press_generate_template_information( $template_name, $template_path, $located, $args ) { | |
| 1067 | + $debug = learn_press_get_request( 'show-template-location' ); | |
| 1068 | + if ( $debug == 'on' ) { | |
| 1069 | + echo '<!-- Template Location:' . str_replace( array( LP_PLUGIN_PATH, ABSPATH ), '', $located ) . ' -->'; | |
| 1070 | + } | |
| 1071 | + } | |
| 1072 | +} | |
| 1073 | + | |
| 1074 | +if ( ! function_exists( 'learn_press_course_remaining_time' ) ) { | |
| 1075 | + /** | |
| 1076 | + * Show the time remain of a course | |
| 1077 | + */ | |
| 1078 | + function learn_press_course_remaining_time() { | |
| 1079 | + $user = learn_press_get_current_user(); | |
| 1080 | + if ( ! $user->has_finished_course( get_the_ID() ) && $text = learn_press_get_course( get_the_ID() )->get_user_duration_html( $user->get_id() ) ) { | |
| 1081 | + learn_press_display_message( $text ); | |
| 1082 | + } | |
| 1083 | + } | |
| 1084 | +} | |
| 1085 | + | |
| 1086 | +if ( ! function_exists( 'learn_press_item_meta_type' ) ) { | |
| 1087 | + function learn_press_item_meta_type( $course, $item ) { | |
| 1088 | + ?> | |
| 1089 | + | |
| 1090 | + <?php if ( $item->post_type == 'lp_quiz' ) { ?> | |
| 1091 | + | |
| 1092 | + <span class="lp-label lp-label-quiz"><?php _e( 'Quiz', 'learnpress' ); ?></span> | |
| 1093 | + | |
| 1094 | + <?php if ( $course->final_quiz == $item->ID ) { ?> | |
| 1095 | + | |
| 1096 | + <span class="lp-label lp-label-final"><?php _e( 'Final', 'learnpress' ); ?></span> | |
| 1097 | + | |
| 1098 | + <?php } ?> | |
| 1099 | + | |
| 1100 | + <?php } elseif ( $item->post_type == 'lp_lesson' ) { ?> | |
| 1101 | + | |
| 1102 | + <span class="lp-label lp-label-lesson"><?php _e( 'Lesson', 'learnpress' ); ?></span> | |
| 1103 | + <?php if ( get_post_meta( $item->ID, '_lp_preview', true ) == 'yes' ) { ?> | |
| 1104 | + | |
| 1105 | + <span class="lp-label lp-label-preview"><?php _e( 'Preview', 'learnpress' ); ?></span> | |
| 1106 | + | |
| 1107 | + <?php } ?> | |
| 1108 | + | |
| 1109 | + <?php } else { ?> | |
| 1110 | + | |
| 1111 | + <?php do_action( 'learn_press_item_meta_type', $course, $item ); ?> | |
| 1112 | + | |
| 1113 | + <?php | |
| 1114 | + } | |
| 1115 | + } | |
| 1116 | +} | |
| 1117 | + | |
| 1118 | +/** | |
| 1119 | + * @deprecated 4.4.5 | |
| 1120 | + */ | |
| 1121 | +/*if ( ! function_exists( 'learn_press_sort_course_tabs' ) ) { | |
| 1122 | + function learn_press_sort_course_tabs( $tabs = array() ) { | |
| 1123 | + uasort( $tabs, 'learn_press_sort_list_by_priority_callback' ); | |
| 1124 | + | |
| 1125 | + return $tabs; | |
| 1126 | + } | |
| 1127 | +}*/ | |
| 1128 | + | |
| 1129 | +if ( ! function_exists( 'learn_press_get_profile_display_name' ) ) { | |
| 1130 | + /** | |
| 1131 | + * Get Display name publicly as in Profile page | |
| 1132 | + */ | |
| 1133 | + function learn_press_get_profile_display_name( $user ) { | |
| 1134 | + if ( empty( $user ) ) { | |
| 1135 | + return ''; | |
| 1136 | + } | |
| 1137 | + | |
| 1138 | + $id = ''; | |
| 1139 | + | |
| 1140 | + if ( $user instanceof LP_Abstract_User ) { | |
| 1141 | + $id = $user->get_id(); | |
| 1142 | + } elseif ( $user instanceof WP_User ) { | |
| 1143 | + $id = $user->ID; | |
| 1144 | + } elseif ( is_numeric( $user ) ) { | |
| 1145 | + $id = $user; | |
| 1146 | + } | |
| 1147 | + | |
| 1148 | + if ( ! isset( $id ) ) { | |
| 1149 | + return ''; | |
| 1150 | + } | |
| 1151 | + | |
| 1152 | + $info = get_userdata( $id ); | |
| 1153 | + | |
| 1154 | + return $info ? $info->display_name : ''; | |
| 1155 | + } | |
| 1156 | +} | |
| 1157 | + | |
| 1158 | +function learn_press_is_content_item_only() { | |
| 1159 | + return ! empty( $_REQUEST['content-item-only'] ); | |
| 1160 | +} | |
| 1161 | + | |
| 1162 | +function learn_press_label_html( $label, $type = '' ) { | |
| 1163 | + ?> | |
| 1164 | + <span class="lp-label label-<?php echo esc_attr( $type ? $type : $label ); ?>"> | |
| 1165 | + <?php echo esc_html( $label ); ?> | |
| 1166 | + </span> | |
| 1167 | + <?php | |
| 1168 | +} | |
| 1169 | + | |
| 1170 | +/** | |
| 1171 | + * @param LP_Quiz $item | |
| 1172 | + * | |
| 1173 | + * @deprecated 4.2.3.5 | |
| 1174 | + */ | |
| 1175 | +function learn_press_quiz_meta_final( $item ) { | |
| 1176 | + _deprecated_function( __METHOD__, '4.2.3.5' ); | |
| 1177 | + | |
| 1178 | + return; | |
| 1179 | + $course = learn_press_get_course(); | |
| 1180 | + | |
| 1181 | + if ( ! $course->is_final_quiz( $item->get_id() ) ) { | |
| 1182 | + return; | |
| 1183 | + } | |
| 1184 | + echo '<span class="item-meta final-quiz">' . esc_html__( 'Final', 'learnpress' ) . '</span>'; | |
| 1185 | +} | |
| 1186 | + | |
| 1187 | +function learn_press_comments_template_query_args( $comment_args ) { | |
| 1188 | + $post_type = get_post_type( $comment_args['post_id'] ); | |
| 1189 | + | |
| 1190 | + if ( $post_type == LP_COURSE_CPT ) { | |
| 1191 | + $comment_args['type__not_in'] = 'review'; | |
| 1192 | + } | |
| 1193 | + | |
| 1194 | + return $comment_args; | |
| 1195 | +} | |
| 1196 | + | |
| 1197 | +add_filter( 'comments_template_query_args', 'learn_press_comments_template_query_args' ); | |
| 1198 | + | |
| 1199 | +function learn_press_comment_form_logged_in( $html_login ) { | |
| 1200 | + if ( get_post_type() == LP_COURSE_CPT || get_post_type() == LP_LESSON_CPT ) { | |
| 1201 | + $html_login = ''; | |
| 1202 | + } | |
| 1203 | + | |
| 1204 | + return $html_login; | |
| 1205 | +} | |
| 1206 | + | |
| 1207 | +add_filter( 'comment_form_logged_in', 'learn_press_comment_form_logged_in' ); | |
| 1208 | + | |
| 1209 | +function learn_press_comment_form_defaults( $defaults ) { | |
| 1210 | + if ( get_post_type() == LP_COURSE_CPT || get_post_type() == LP_LESSON_CPT ) { | |
| 1211 | + $defaults['comment_notes_before'] = ''; | |
| 1212 | + } | |
| 1213 | + | |
| 1214 | + return $defaults; | |
| 1215 | +} | |
| 1216 | + | |
| 1217 | +add_filter( 'comment_form_defaults', 'learn_press_comment_form_defaults' ); | |
| 1218 | + | |
| 1219 | +function learn_press_filter_get_comments_number( $count, $post_id = 0 ) { | |
| 1220 | + global $wpdb; | |
| 1221 | + | |
| 1222 | + if ( ! $post_id ) { | |
| 1223 | + $post_id = get_the_ID(); | |
| 1224 | + if ( ! $post_id ) { | |
| 1225 | + return $count; | |
| 1226 | + } | |
| 1227 | + } | |
| 1228 | + | |
| 1229 | + if ( get_post_type( $post_id ) == LP_COURSE_CPT ) { | |
| 1230 | + $sql = $wpdb->prepare( | |
| 1231 | + ' SELECT count(*) ' | |
| 1232 | + . " FROM {$wpdb->comments} " | |
| 1233 | + . ' WHERE comment_post_ID = %d ' | |
| 1234 | + . ' AND comment_approved = 1 ' | |
| 1235 | + . ' AND comment_type != %s ', | |
| 1236 | + $post_id, | |
| 1237 | + 'review' | |
| 1238 | + ); | |
| 1239 | + | |
| 1240 | + $count = $wpdb->get_var( $sql ); | |
| 1241 | + } | |
| 1242 | + | |
| 1243 | + return $count; | |
| 1244 | +} | |
| 1245 | + | |
| 1246 | +add_filter( 'get_comments_number', 'learn_press_filter_get_comments_number' ); | |
| 1247 | + | |
| 1248 | +/** | |
| 1249 | + * Add custom classes to body tag class name | |
| 1250 | + * | |
| 1251 | + * @param array $classes | |
| 1252 | + * | |
| 1253 | + * @return array | |
| 1254 | + * | |
| 1255 | + * @since 3.0.0 | |
| 1256 | + * @Todo tungnx review to remove | |
| 1257 | + * @deprecated 4.2.3 | |
| 1258 | + */ | |
| 1259 | +function learn_press_body_classes( $classes ) { | |
| 1260 | + if ( is_learnpress() ) { | |
| 1261 | + $classes[] = get_stylesheet(); | |
| 1262 | + $classes[] = 'learnpress'; | |
| 1263 | + $classes[] = 'learnpress-page'; | |
| 1264 | + } | |
| 1265 | + | |
| 1266 | + return $classes; | |
| 1267 | +} | |
| 1268 | + | |
| 1269 | +//add_filter( 'body_class', 'learn_press_body_classes', 10 ); | |
| 1270 | + | |
| 1271 | +/** | |
| 1272 | + * Return true if user is learning a course | |
| 1273 | + * | |
| 1274 | + * @param int $course_id | |
| 1275 | + * | |
| 1276 | + * @return bool | |
| 1277 | + * @since 3.0 | |
| 1278 | + * @version 1.0.1 | |
| 1279 | + */ | |
| 1280 | +function learn_press_is_learning_course( int $course_id = 0 ): bool { | |
| 1281 | + $is_learning = false; | |
| 1282 | + $user = learn_press_get_current_user(); | |
| 1283 | + $course = learn_press_get_course( $course_id ); | |
| 1284 | + if ( ! $course ) { | |
| 1285 | + return $is_learning; | |
| 1286 | + } | |
| 1287 | + | |
| 1288 | + if ( $user ) { | |
| 1289 | + return $user->has_enrolled_or_finished( $course_id ); | |
| 1290 | + } | |
| 1291 | + | |
| 1292 | + if ( $course->is_no_required_enroll() ) { | |
| 1293 | + $is_learning = true; | |
| 1294 | + } | |
| 1295 | + | |
| 1296 | + return apply_filters( 'lp/is-learning-course', $is_learning, $course_id ); | |
| 1297 | +} | |
| 1298 | + | |
| 1299 | +function learn_press_content_item_summary_class( $more = '', $echo = true ) { | |
| 1300 | + $classes = array( 'content-item-summary' ); | |
| 1301 | + $classes = LP_Helper::merge_class( $classes, $more ); | |
| 1302 | + $classes = apply_filters( 'learn-press/content-item-summary-class', $classes ); | |
| 1303 | + $output = 'class="' . esc_attr( join( ' ', $classes ) ) . '"'; | |
| 1304 | + | |
| 1305 | + if ( $echo ) { | |
| 1306 | + echo wp_kses_post( $output ); | |
| 1307 | + } | |
| 1308 | + | |
| 1309 | + return $output; | |
| 1310 | +} | |
| 1311 | + | |
| 1312 | +/** | |
| 1313 | + * @deprecated 4.2.3.1 | |
| 1314 | + */ | |
| 1315 | +function learn_press_content_item_summary_classes( $classes ) { | |
| 1316 | + _deprecated_function( __FUNCTION__, '4.2.3.1' ); | |
| 1317 | + $item = LP_Global::course_item(); | |
| 1318 | + | |
| 1319 | + if ( ! $item ) { | |
| 1320 | + return $classes; | |
| 1321 | + } | |
| 1322 | + | |
| 1323 | + if ( $item->get_post_type() !== LP_LESSON_CPT ) { | |
| 1324 | + return $classes; | |
| 1325 | + } | |
| 1326 | + | |
| 1327 | + return $classes; | |
| 1328 | +} | |
| 1329 | + | |
| 1330 | +function learn_press_maybe_load_comment_js() { | |
| 1331 | + $item = LP_Global::course_item(); | |
| 1332 | + | |
| 1333 | + if ( $item ) { | |
| 1334 | + wp_enqueue_script( 'comment-reply' ); | |
| 1335 | + } | |
| 1336 | +} | |
| 1337 | + | |
| 1338 | +add_action( 'wp_enqueue_scripts', 'learn_press_maybe_load_comment_js' ); | |
| 1339 | + | |
| 1340 | +/** | |
| 1341 | + * Get list layouts archive course setting | |
| 1342 | + * | |
| 1343 | + * @editor tungnx | |
| 1344 | + * @modify 4.1.3 | |
| 1345 | + */ | |
| 1346 | +function learn_press_courses_layouts() { | |
| 1347 | + return apply_filters( | |
| 1348 | + 'learnpress/archive-courses-layouts', | |
| 1349 | + [ | |
| 1350 | + 'grid' => __( 'Grid', 'learnpress' ), | |
| 1351 | + 'list' => __( 'List', 'learnpress' ), | |
| 1352 | + ] | |
| 1353 | + ); | |
| 1354 | +} | |
| 1355 | + | |
| 1356 | +/** | |
| 1357 | + * Get layout template for archive course page. | |
| 1358 | + * | |
| 1359 | + * @return mixed | |
| 1360 | + * @since 3.3.0 | |
| 1361 | + * @editor tungnx | |
| 1362 | + * @modify 4.1.3 | |
| 1363 | + */ | |
| 1364 | +function learn_press_get_courses_layout() { | |
| 1365 | + $layout = LP_Request::get_cookie( 'courses-layout' ); | |
| 1366 | + | |
| 1367 | + if ( ! $layout ) { | |
| 1368 | + $layout = LP_Settings::get_option( 'archive_courses_layout', 'list' ); | |
| 1369 | + } | |
| 1370 | + | |
| 1371 | + return $layout; | |
| 1372 | +} | |
| 1373 | + | |
| 1374 | +function learn_press_register_sidebars() { | |
| 1375 | + register_sidebar( | |
| 1376 | + array( | |
| 1377 | + 'name' => esc_html__( 'Course Sidebar', 'learnpress' ), | |
| 1378 | + 'id' => 'course-sidebar', | |
| 1379 | + 'description' => esc_html__( 'Widgets in this area will be shown in a single course', 'learnpress' ), | |
| 1380 | + 'before_widget' => '<div id="%1$s" class="widget %2$s">', | |
| 1381 | + 'after_widget' => '</div>', | |
| 1382 | + 'before_title' => '<h3 class="widget-title">', | |
| 1383 | + 'after_title' => '</h3>', | |
| 1384 | + ) | |
| 1385 | + ); | |
| 1386 | + register_sidebar( | |
| 1387 | + array( | |
| 1388 | + 'name' => esc_html__( 'All Courses', 'learnpress' ), | |
| 1389 | + 'id' => 'archive-courses-sidebar', | |
| 1390 | + 'description' => esc_html__( 'Widgets in this area will be shown on all course pages', 'learnpress' ), | |
| 1391 | + 'before_widget' => '<div id="%1$s" class="widget %2$s">', | |
| 1392 | + 'after_widget' => '</div>', | |
| 1393 | + 'before_title' => '<h3 class="widget-title">', | |
| 1394 | + 'after_title' => '</h3>', | |
| 1395 | + ) | |
| 1396 | + ); | |
| 1397 | +} | |
| 1398 | + | |
| 1399 | +add_action( 'widgets_init', 'learn_press_register_sidebars' ); | |
| 1400 | + | |
| 1401 | +function learn_press_setup_theme() { | |
| 1402 | + $support = array( | |
| 1403 | + 'widgets' => array( | |
| 1404 | + // Place three core-defined widgets in the sidebar area. | |
| 1405 | + 'course-sidebar' => array( | |
| 1406 | + 'xxx' => array( 'lp-widget-course-progress' ), | |
| 1407 | + 'yyy' => array( 'lp-widget-course-info' ), | |
| 1408 | + ), | |
| 1409 | + ), | |
| 1410 | + ); | |
| 1411 | + | |
| 1412 | + add_theme_support( 'starter-content', $support ); | |
| 1413 | +} | |
| 1414 | + | |
| 1415 | +add_action( 'after_setup_theme', 'learn_press_setup_theme' ); | |
| 1416 | + | |
| 1417 | +/** | |
| 1418 | + * @param LP_Question $question | |
| 1419 | + * @param array $args | |
| 1420 | + * | |
| 1421 | + * @return array | |
| 1422 | + * @since 4.x.x | |
| 1423 | + */ | |
| 1424 | +function learn_press_get_question_options_for_js( $question, $args = array() ) { | |
| 1425 | + $args = wp_parse_args( | |
| 1426 | + $args, | |
| 1427 | + array( | |
| 1428 | + 'cryptoJsAes' => false, | |
| 1429 | + 'include_is_true' => false, | |
| 1430 | + 'answer' => false, | |
| 1431 | + ) | |
| 1432 | + ); | |
| 1433 | + | |
| 1434 | + if ( $args['cryptoJsAes'] ) { | |
| 1435 | + $options = array_values( $question->get_answer_options() ); | |
| 1436 | + | |
| 1437 | + $key = uniqid(); | |
| 1438 | + $options = array( | |
| 1439 | + 'data' => cryptoJsAesEncrypt( $key, wp_json_encode( $options ) ), | |
| 1440 | + 'key' => $key, | |
| 1441 | + ); | |
| 1442 | + } else { | |
| 1443 | + $exclude_option_key = array( 'question_id', 'order' ); | |
| 1444 | + if ( ! $args['include_is_true'] ) { | |
| 1445 | + $exclude_option_key[] = 'is_true'; | |
| 1446 | + } | |
| 1447 | + | |
| 1448 | + $options = array_values( | |
| 1449 | + $question->get_answer_options( | |
| 1450 | + array( | |
| 1451 | + 'exclude' => $exclude_option_key, | |
| 1452 | + 'map' => array( 'question_answer_id' => 'uid' ), | |
| 1453 | + 'answer' => $args['answer'], | |
| 1454 | + ) | |
| 1455 | + ) | |
| 1456 | + ); | |
| 1457 | + } | |
| 1458 | + | |
| 1459 | + return $options; | |
| 1460 | +} | |
| 1461 | + | |
| 1462 | +function learn_press_custom_excerpt_length( $length ) { | |
| 1463 | + return 20; | |
| 1464 | +} | |
| 1465 | + | |
| 1466 | +/** | |
| 1467 | + * Get post meta with key _lp_duration and translate. | |
| 1468 | + * | |
| 1469 | + * @param int $post_id | |
| 1470 | + * @param string $default | |
| 1471 | + * | |
| 1472 | + * @return string | |
| 1473 | + * @since 4.0.0 | |
| 1474 | + */ | |
| 1475 | +function learn_press_get_post_translated_duration( $post_id, $default = '' ) { | |
| 1476 | + $duration = get_post_meta( $post_id, '_lp_duration', true ); | |
| 1477 | + | |
| 1478 | + $duration_arr = explode( ' ', $duration ); | |
| 1479 | + $duration_str = ''; | |
| 1480 | + | |
| 1481 | + if ( count( $duration_arr ) > 1 ) { | |
| 1482 | + $duration_number = $duration_arr[0]; | |
| 1483 | + $duration_text = $duration_arr[1]; | |
| 1484 | + | |
| 1485 | + switch ( strtolower( $duration_text ) ) { | |
| 1486 | + case 'minute': | |
| 1487 | + $duration_str = sprintf( | |
| 1488 | + _n( '%s minute', '%s minutes', $duration_number, 'learnpress' ), | |
| 1489 | + $duration_number | |
| 1490 | + ); | |
| 1491 | + break; | |
| 1492 | + case 'hour': | |
| 1493 | + $duration_str = sprintf( | |
| 1494 | + _n( '%s hour', '%s hours', $duration_number, 'learnpress' ), | |
| 1495 | + $duration_number | |
| 1496 | + ); | |
| 1497 | + break; | |
| 1498 | + case 'day': | |
| 1499 | + $duration_str = sprintf( _n( '%s day', '%s days', $duration_number, 'learnpress' ), $duration_number ); | |
| 1500 | + break; | |
| 1501 | + case 'week': | |
| 1502 | + $duration_str = sprintf( | |
| 1503 | + _n( '%s week', '%s weeks', $duration_number, 'learnpress' ), | |
| 1504 | + $duration_number | |
| 1505 | + ); | |
| 1506 | + break; | |
| 1507 | + default: | |
| 1508 | + $duration_str = $duration; | |
| 1509 | + } | |
| 1510 | + } | |
| 1511 | + | |
| 1512 | + return empty( absint( $duration ) ) ? $default : $duration_str; | |
| 1513 | +} | |
| 1514 | + | |
| 1515 | +/** | |
| 1516 | + * Get level post meta. | |
| 1517 | + * | |
| 1518 | + * @param int $post_id | |
| 1519 | + * | |
| 1520 | + * @return string | |
| 1521 | + */ | |
| 1522 | +function learn_press_get_post_level( $post_id ) { | |
| 1523 | + $level = get_post_meta( $post_id, '_lp_level', true ); | |
| 1524 | + | |
| 1525 | + return apply_filters( | |
| 1526 | + 'learn-press/level-label', | |
| 1527 | + ! empty( $level ) ? lp_course_level()[ $level ] : esc_html__( 'All levels', 'learnpress' ), | |
| 1528 | + $post_id | |
| 1529 | + ); | |
| 1530 | +} | |
| 1531 | + | |
| 1532 | +function lp_course_level() { | |
| 1533 | + return apply_filters( | |
| 1534 | + 'lp/template/function/course/level', | |
| 1535 | + array( | |
| 1536 | + 'all' => esc_html__( 'All levels', 'learnpress' ), | |
| 1537 | + 'beginner' => esc_html__( 'Beginner', 'learnpress' ), | |
| 1538 | + 'intermediate' => esc_html__( 'Intermediate', 'learnpress' ), | |
| 1539 | + 'expert' => esc_html__( 'Expert', 'learnpress' ), | |
| 1540 | + ) | |
| 1541 | + ); | |
| 1542 | +} | |
| 1543 | + | |
| 1544 | +/** | |
| 1545 | + * Get slug for logout action in user profile. | |
| 1546 | + * | |
| 1547 | + * @return string | |
| 1548 | + * @since 4.0.0 | |
| 1549 | + */ | |
| 1550 | +function learn_press_profile_logout_slug() { | |
| 1551 | + return apply_filters( 'learn-press/profile-logout-slug', 'lp-logout' ); | |
| 1552 | +} | |
| 1553 | + | |
| 1554 | +function lp_get_email_content( $format, $meta = array(), $field = array() ) { | |
| 1555 | + if ( $meta && isset( $meta[ $format ] ) ) { | |
| 1556 | + $content = stripslashes( $meta[ $format ] ); | |
| 1557 | + } else { | |
| 1558 | + $template = ! empty( $field[ "template_{$format}" ] ) ? $field[ "template_{$format}" ] : null; | |
| 1559 | + $template_file = $field['template_base'] . $template; | |
| 1560 | + $content = LP_WP_Filesystem::instance()->file_get_contents( $template_file ); | |
| 1561 | + } | |
| 1562 | + | |
| 1563 | + return $content; | |
| 1564 | +} | |
| 1565 | + | |
| 1566 | +function lp_skeleton_animation_html( $count_li = 3, $width = 'random', $styleli = '', $styleul = '' ) { | |
| 1567 | + ?> | |
| 1568 | + <ul class="lp-skeleton-animation" style="<?php echo esc_attr( $styleul ); ?>"> | |
| 1569 | + <?php for ( $i = 0; $i < absint( $count_li ); $i ++ ) : ?> | |
| 1570 | + <li style="width: <?php echo esc_attr( $width === 'random' ? wp_rand( 90, 100 ) . '%' : $width ); ?>; <?php echo ! empty( $styleli ) ? $styleli : ''; ?>"></li> | |
| 1571 | + <?php endfor; ?> | |
| 1572 | + </ul> | |
| 1573 | + | |
| 1574 | + <?php | |
| 1575 | +} | |
| 1576 | + | |
| 1577 | +add_filter( 'lp_format_page_content', 'wptexturize' ); | |
| 1578 | +add_filter( 'lp_format_page_content', 'convert_smilies' ); | |
| 1579 | +add_filter( 'lp_format_page_content', 'convert_chars' ); | |
| 1580 | +add_filter( 'lp_format_page_content', 'wpautop' ); | |
| 1581 | +add_filter( 'lp_format_page_content', 'shortcode_unautop' ); | |
| 1582 | +add_filter( 'lp_format_page_content', 'prepend_attachment' ); | |
| 1583 | +add_filter( 'lp_format_page_content', 'do_shortcode', 11 ); | |
| 1584 | +add_filter( 'lp_format_page_content', array( $GLOBALS['wp_embed'], 'run_shortcode' ), 8 ); | |
| 1585 | + | |
| 1586 | +if ( function_exists( 'do_blocks' ) ) { | |
| 1587 | + add_filter( 'lp_format_page_content', 'do_blocks', 9 ); | |
| 1588 | +} | |
| 1589 | + | |
| 1590 | +function lp_format_page_content( $raw_string ) { | |
| 1591 | + return apply_filters( 'lp_format_page_content', $raw_string ); | |
| 1592 | +} | |
| 1593 | + | |
| 1594 | +if ( ! function_exists( 'lp_profile_page_content' ) ) { | |
| 1595 | + function lp_profile_page_content() { | |
| 1596 | + $profile_id = learn_press_get_page_id( 'profile' ); | |
| 1597 | + | |
| 1598 | + if ( $profile_id ) { | |
| 1599 | + $profile_page = get_post( $profile_id ); | |
| 1600 | + | |
| 1601 | + // remove_shortcode( 'learn_press_profile' ); | |
| 1602 | + $description = lp_format_page_content( wp_kses_post( $profile_page->post_content ) ); | |
| 1603 | + | |
| 1604 | + if ( $description ) { | |
| 1605 | + echo '<div class="lp-profile-page__content">' . $description . '</div>'; | |
| 1606 | + } | |
| 1607 | + } | |
| 1608 | + } | |
| 1609 | +} | |
| 1610 | + | |
| 1611 | +if ( ! function_exists( 'lp_archive_course_page_content' ) ) { | |
| 1612 | + add_action( 'lp/template/archive-course/description', 'lp_archive_course_page_content' ); | |
| 1613 | + | |
| 1614 | + function lp_archive_course_page_content() { | |
| 1615 | + if ( is_search() ) { | |
| 1616 | + return; | |
| 1617 | + } | |
| 1618 | + | |
| 1619 | + if ( is_post_type_archive( LP_COURSE_CPT ) | |
| 1620 | + && in_array( absint( get_query_var( 'paged' ) ), array( 0, 1 ), true ) ) { | |
| 1621 | + $profile_id = learn_press_get_page_id( 'courses' ); | |
| 1622 | + | |
| 1623 | + if ( $profile_id ) { | |
| 1624 | + $profile_page = get_post( $profile_id ); | |
| 1625 | + | |
| 1626 | + $description = lp_format_page_content( wp_kses_post( $profile_page->post_content ) ); | |
| 1627 | + if ( $description ) { | |
| 1628 | + echo '<div class="lp-course-page__content">' . $description . '</div>'; | |
| 1629 | + } | |
| 1630 | + } | |
| 1631 | + } | |
| 1632 | + } | |
| 1633 | +} | |
| 1634 | + | |
| 1635 | +if ( ! function_exists( 'lp_taxonomy_archive_course_description' ) ) { | |
| 1636 | + add_action( 'lp/template/archive-course/description', 'lp_taxonomy_archive_course_description' ); | |
| 1637 | + | |
| 1638 | + function lp_taxonomy_archive_course_description() { | |
| 1639 | + | |
| 1640 | + if ( learn_press_is_course_tax() && 0 === absint( get_query_var( 'paged' ) ) ) { | |
| 1641 | + $term = get_queried_object(); | |
| 1642 | + | |
| 1643 | + if ( $term && ! empty( $term->description ) ) { | |
| 1644 | + echo '<div class="lp-archive-course-term-description">' . lp_format_page_content( wp_kses_post( $term->description ) ) . '</div>'; | |
| 1645 | + } | |
| 1646 | + } | |
| 1647 | + } | |
| 1648 | +} | |
| 1649 | + | |
| 1650 | +/** | |
| 1651 | + * Params to query courses on Archive Course. | |
| 1652 | + * | |
| 1653 | + * @return array | |
| 1654 | + */ | |
| 1655 | +function lp_archive_skeleton_get_args(): array { | |
| 1656 | + $args = []; | |
| 1657 | + | |
| 1658 | + if ( ! empty( $_GET ) ) { | |
| 1659 | + $args = $_GET; | |
| 1660 | + } | |
| 1661 | + | |
| 1662 | + global $wp_query; | |
| 1663 | + if ( ! empty( $wp_query->get( 'paged' ) ) ) { | |
| 1664 | + $args['paged'] = $wp_query->get( 'paged' ); | |
| 1665 | + } | |
| 1666 | + | |
| 1667 | + if ( learn_press_is_course_category() || learn_press_is_course_tag() ) { | |
| 1668 | + $cat = get_queried_object(); | |
| 1669 | + | |
| 1670 | + // Info of page | |
| 1671 | + if ( learn_press_is_course_category() ) { | |
| 1672 | + $args['page_term_id_current'] = $cat->term_id; | |
| 1673 | + } elseif ( learn_press_is_course_tag() ) { | |
| 1674 | + $args['page_tag_id_current'] = $cat->term_id; | |
| 1675 | + } | |
| 1676 | + | |
| 1677 | + $args['page_term_url'] = get_term_link( $cat ); | |
| 1678 | + } | |
| 1679 | + | |
| 1680 | + $args = apply_filters( 'lp/template/archive-course/skeleton/args', $args ); | |
| 1681 | + if ( ! is_array( $args ) ) { | |
| 1682 | + $args = []; | |
| 1683 | + } | |
| 1684 | + | |
| 1685 | + return LP_Helper::sanitize_params_submitted( $args, 'sanitize_text_field' ); | |
| 1686 | +} | |