| @@ -1,1051 +1,1051 @@ | ||
| 1 | -<?php | |
| 2 | -/** | |
| 3 | - * Common functions to manipulate with course, lesson, quiz, questions, etc... | |
| 4 | - * | |
| 5 | - * @author ThimPress | |
| 6 | - * @package LearnPress/Functions | |
| 7 | - * @version 1.0 | |
| 8 | - */ | |
| 9 | - | |
| 10 | -defined( 'ABSPATH' ) || exit(); | |
| 11 | - | |
| 12 | -/** | |
| 13 | - * Get course current on single course or course by id | |
| 14 | - * Only use learn_press_get_course() on the single page | |
| 15 | - * Another page use learn_press_get_course(id) | |
| 16 | - * | |
| 17 | - * @param int $the_course | |
| 18 | - * @since 3.0.0 | |
| 19 | - * @version 1.0.1 | |
| 20 | - * @editor tungnx | |
| 21 | - * @return bool|LP_Course|mixed | |
| 22 | - */ | |
| 23 | -function learn_press_get_course( $the_course = 0 ) { | |
| 24 | - $the_course = (int) $the_course; | |
| 25 | - | |
| 26 | - if ( 0 === $the_course ) { | |
| 27 | - $the_course = get_the_ID() ? get_the_ID() : 0; | |
| 28 | - } | |
| 29 | - | |
| 30 | - return LP_Course::get_course( $the_course ); | |
| 31 | -} | |
| 32 | - | |
| 33 | -/** | |
| 34 | - * @editor tungnx | |
| 35 | - * @modify 4.1.4.1 - comment - not use | |
| 36 | - */ | |
| 37 | -/*function learn_press_get_course_by_id( $id ) { | |
| 38 | - if ( false !== ( $courses = LP_Object_Cache::get( 'object', 'learn-press/courses' ) ) ) { | |
| 39 | - return ! empty( $courses[ $id ] ) ? $courses[ $id ] : false; | |
| 40 | - } | |
| 41 | - | |
| 42 | - return false; | |
| 43 | -}*/ | |
| 44 | - | |
| 45 | -/** | |
| 46 | - * Create nonce for course action. | |
| 47 | - * Return nonce created with format 'learn-press-$action-$course_id-course-$user_id' | |
| 48 | - * | |
| 49 | - * @param string $action [retake, purchase, enroll] | |
| 50 | - * @param int $course_id | |
| 51 | - * @param int $user_id | |
| 52 | - * | |
| 53 | - * @return string | |
| 54 | - * @since 3.0.0 | |
| 55 | - * @deprecated 4.1.6.9 | |
| 56 | - */ | |
| 57 | -/*function learn_press_create_course_action_nonce( $action, $course_id = 0, $user_id = 0 ) { | |
| 58 | - return LP_Nonce_Helper::create_course( $action, $course_id, $user_id ); | |
| 59 | -}*/ | |
| 60 | - | |
| 61 | -/** | |
| 62 | - * Verify nonce for course action. | |
| 63 | - * | |
| 64 | - * @param string $nonce | |
| 65 | - * @param string $action | |
| 66 | - * @param int $course_id | |
| 67 | - * @param int $user_id | |
| 68 | - * | |
| 69 | - * @return bool | |
| 70 | - * @since 3.0.0 | |
| 71 | - * @deprecated 4.1.6.9 | |
| 72 | - */ | |
| 73 | -/*function learn_press_verify_course_action_nonce( $nonce, $action, $course_id = 0, $user_id = 0 ) { | |
| 74 | - return LP_Nonce_Helper::verify_course( $nonce, $action, $course_id, $user_id ); | |
| 75 | -}*/ | |
| 76 | - | |
| 77 | -/** | |
| 78 | - * Get type of items are supported in course curriculum (post types). | |
| 79 | - * Default: [lp_lesson, lp_quiz] | |
| 80 | - * When all addon use hook 'learn-press/course/item-types-support', will deprecate this function | |
| 81 | - * @use CourseModel::item_types_support | |
| 82 | - * | |
| 83 | - * @return mixed | |
| 84 | - * @since 3.0.0 | |
| 85 | - * @editor tungnx | |
| 86 | - * @version 1.0.1 | |
| 87 | - * @return array | |
| 88 | - * @deprecated | |
| 89 | - */ | |
| 90 | -function learn_press_get_course_item_types( bool $return_only_value = true ): array { | |
| 91 | - return apply_filters( | |
| 92 | - 'learn-press/course-item-type', | |
| 93 | - array( LP_LESSON_CPT, LP_QUIZ_CPT ) | |
| 94 | - ); | |
| 95 | -} | |
| 96 | - | |
| 97 | -/** | |
| 98 | - * Get type of items can purchase on LP Order. | |
| 99 | - * Default: ['lp_course', 'lp_certificate'] | |
| 100 | - * | |
| 101 | - * @return mixed | |
| 102 | - * @since 3.0.0 | |
| 103 | - * | |
| 104 | - */ | |
| 105 | -function learn_press_get_item_types_can_purchase() { | |
| 106 | - return apply_filters( | |
| 107 | - 'learn-press/purchase/item-types/can-purchase', | |
| 108 | - array( LP_COURSE_CPT ) | |
| 109 | - ); | |
| 110 | -} | |
| 111 | - | |
| 112 | -/** | |
| 113 | - * Get the courses that a item is assigned to | |
| 114 | - * | |
| 115 | - * @param $item | |
| 116 | - * | |
| 117 | - * @return mixed | |
| 118 | - * @Todo - tungnx need review code to rewrite. | |
| 119 | - */ | |
| 120 | -function learn_press_get_item_courses( $item ) { | |
| 121 | - global $wpdb; | |
| 122 | - $query = $wpdb->prepare( | |
| 123 | - " | |
| 124 | - SELECT c.* | |
| 125 | - FROM {$wpdb->posts} c | |
| 126 | - INNER JOIN {$wpdb->learnpress_sections} s ON c.ID = s.section_course_id | |
| 127 | - INNER JOIN {$wpdb->learnpress_section_items} si ON si.section_id = s.section_id | |
| 128 | - WHERE si.item_id = %d | |
| 129 | - ", | |
| 130 | - $item | |
| 131 | - ); | |
| 132 | - | |
| 133 | - return $wpdb->get_results( $query ); | |
| 134 | -} | |
| 135 | - | |
| 136 | -/** | |
| 137 | - * @deprecated 4.2.7.4 | |
| 138 | - */ | |
| 139 | -/*function _learn_press_usort_terms_by_ID( $terms ) { | |
| 140 | - $version = get_bloginfo( 'version' ); | |
| 141 | - if ( version_compare( $version, '4.7', '>=' ) ) { | |
| 142 | - $terms = wp_list_sort( $terms, 'term_id' ); | |
| 143 | - } else { | |
| 144 | - usort( $terms, '_usort_terms_by_ID' ); | |
| 145 | - } | |
| 146 | - | |
| 147 | - return $terms; | |
| 148 | -}*/ | |
| 149 | - | |
| 150 | -/*function learn_press_course_post_type_link( $permalink, $post ) { | |
| 151 | - if ( $post->post_type !== 'lp_course' ) { | |
| 152 | - return $permalink; | |
| 153 | - } | |
| 154 | - | |
| 155 | - // Abort early if the placeholder rewrite tag isn't in the generated URL | |
| 156 | - if ( false === strpos( $permalink, '%' ) ) { | |
| 157 | - return $permalink; | |
| 158 | - } | |
| 159 | - | |
| 160 | - // Get the custom taxonomy terms in use by this post | |
| 161 | - $terms = get_the_terms( $post->ID, 'course_category' ); | |
| 162 | - | |
| 163 | - if ( ! empty( $terms ) ) { | |
| 164 | - $terms = _learn_press_usort_terms_by_ID( $terms ); // order by ID | |
| 165 | - $category_object = apply_filters( | |
| 166 | - 'learn_press_course_post_type_link_course_category', | |
| 167 | - $terms[0], | |
| 168 | - $terms, | |
| 169 | - $post | |
| 170 | - ); | |
| 171 | - $category_object = get_term( $category_object, 'course_category' ); | |
| 172 | - $course_category = $category_object->slug; | |
| 173 | - | |
| 174 | - if ( $parent = $category_object->parent ) { | |
| 175 | - $ancestors = get_ancestors( $category_object->term_id, 'course_category' ); | |
| 176 | - foreach ( $ancestors as $ancestor ) { | |
| 177 | - $ancestor_object = get_term( $ancestor, 'course_category' ); | |
| 178 | - $course_category = $ancestor_object->slug . '/' . $course_category; | |
| 179 | - } | |
| 180 | - } | |
| 181 | - } else { | |
| 182 | - // If no terms are assigned to this post, use a string instead (can't leave the placeholder there) | |
| 183 | - $course_category = _x( 'uncategorized', 'slug', 'learnpress' ); | |
| 184 | - } | |
| 185 | - | |
| 186 | - $find = array( | |
| 187 | - '%year%', | |
| 188 | - '%monthnum%', | |
| 189 | - '%day%', | |
| 190 | - '%hour%', | |
| 191 | - '%minute%', | |
| 192 | - '%second%', | |
| 193 | - '%post_id%', | |
| 194 | - '%category%', | |
| 195 | - '%course_category%', | |
| 196 | - ); | |
| 197 | - | |
| 198 | - $replace = array( | |
| 199 | - date_i18n( 'Y', strtotime( $post->post_date ) ), | |
| 200 | - date_i18n( 'm', strtotime( $post->post_date ) ), | |
| 201 | - date_i18n( 'd', strtotime( $post->post_date ) ), | |
| 202 | - date_i18n( 'H', strtotime( $post->post_date ) ), | |
| 203 | - date_i18n( 'i', strtotime( $post->post_date ) ), | |
| 204 | - date_i18n( 's', strtotime( $post->post_date ) ), | |
| 205 | - $post->ID, | |
| 206 | - $course_category, | |
| 207 | - $course_category, | |
| 208 | - ); | |
| 209 | - | |
| 210 | - $permalink = str_replace( $find, $replace, $permalink ); | |
| 211 | - | |
| 212 | - return $permalink; | |
| 213 | -}*/ | |
| 214 | - | |
| 215 | -//add_filter( 'post_type_link', 'learn_press_course_post_type_link', 10, 2 ); | |
| 216 | - | |
| 217 | -function learn_press_item_meta_format( $item, $nonce = '' ) { | |
| 218 | - if ( current_theme_supports( 'post-formats' ) ) { | |
| 219 | - $format = get_post_format( $item ); | |
| 220 | - if ( false === $format ) { | |
| 221 | - $format = 'standard'; | |
| 222 | - } | |
| 223 | - | |
| 224 | - // return false to hide post format | |
| 225 | - $format = apply_filters( 'learn_press_course_item_format', $format, $item ); | |
| 226 | - if ( $format ) { | |
| 227 | - printf( | |
| 228 | - '<label for="post-format-0" class="post-format-icon post-format-%s" title="%s"></label>', | |
| 229 | - $format, | |
| 230 | - ucfirst( $format ) | |
| 231 | - ); | |
| 232 | - } else { | |
| 233 | - echo esc_html( $nonce ); | |
| 234 | - } | |
| 235 | - } | |
| 236 | -} | |
| 237 | - | |
| 238 | -/** | |
| 239 | - * @deprecated 4.2.7.4 | |
| 240 | - */ | |
| 241 | -/*function learn_press_course_item_format_exclude( $format, $item ) { | |
| 242 | - if ( learn_press_get_post_type( $item ) != LP_LESSON_CPT || ( $format == 'standard' ) ) { | |
| 243 | - $format = false; | |
| 244 | - } | |
| 245 | - | |
| 246 | - return $format; | |
| 247 | -}*/ | |
| 248 | - | |
| 249 | -/** | |
| 250 | - * Get curriculum of a course | |
| 251 | - * | |
| 252 | - * @param $course_id | |
| 253 | - * | |
| 254 | - * @return mixed | |
| 255 | - * @version 1.0 | |
| 256 | - * @deprecated 4.2.7.4 | |
| 257 | - */ | |
| 258 | -/*function learn_press_get_course_curriculum( $course_id ) { | |
| 259 | - $course = learn_press_get_course( $course_id ); | |
| 260 | - | |
| 261 | - return $course->get_curriculum(); | |
| 262 | -}*/ | |
| 263 | - | |
| 264 | -/** | |
| 265 | - * Verify course access | |
| 266 | - * | |
| 267 | - * @param int $course_id | |
| 268 | - * @param int $user_id | |
| 269 | - * | |
| 270 | - * @return boolean | |
| 271 | - * @deprecated 4.1.3 | |
| 272 | - * @editor tungnx | |
| 273 | - */ | |
| 274 | -/*function learn_press_is_enrolled_course( $course_id = null, $user_id = null ) { | |
| 275 | - _deprecated_function( __FUNCTION__, '4.1.3' ); | |
| 276 | - if ( $course = learn_press_get_course( $course_id ) && $user = learn_press_get_user( $user_id ) ) { | |
| 277 | - return $user->has_enrolled_course( $course_id ); | |
| 278 | - } | |
| 279 | - | |
| 280 | - return false; | |
| 281 | -}*/ | |
| 282 | - | |
| 283 | -/** | |
| 284 | - * Detect if a course is free or not | |
| 285 | - * | |
| 286 | - * @param null $course_id | |
| 287 | - * | |
| 288 | - * @return bool | |
| 289 | - * @deprecated 4.2.7.4 | |
| 290 | - */ | |
| 291 | -/*function learn_press_is_free_course( $course_id = null ) { | |
| 292 | - if ( ! $course_id ) { | |
| 293 | - $course_id = get_the_ID(); | |
| 294 | - } | |
| 295 | - | |
| 296 | - return learn_press_get_course( $course_id )->is_free(); | |
| 297 | -}*/ | |
| 298 | - | |
| 299 | -/** | |
| 300 | - * get current status of user's course | |
| 301 | - * | |
| 302 | - * @param int $user_id | |
| 303 | - * @param int $course_id | |
| 304 | - * | |
| 305 | - * @return string | |
| 306 | - * @author Tunn | |
| 307 | - * @deprecated 4.2.7.4 | |
| 308 | - */ | |
| 309 | -/*function learn_press_get_user_course_status( $user_id = null, $course_id = null ) { | |
| 310 | - $course = learn_press_get_course( $course_id ); | |
| 311 | - $user = learn_press_get_user( $user_id ); | |
| 312 | - | |
| 313 | - if ( $course && $user ) { | |
| 314 | - return $user->get_course_status( $course_id ); | |
| 315 | - } | |
| 316 | - | |
| 317 | - return false; | |
| 318 | -}*/ | |
| 319 | - | |
| 320 | -/** | |
| 321 | - * Wrap function can-view-item of user object. | |
| 322 | - * | |
| 323 | - * @param int $item_id | |
| 324 | - * @param int $course_id | |
| 325 | - * @param int $user_id | |
| 326 | - * | |
| 327 | - * @return mixed | |
| 328 | - * @since 3.1.0 | |
| 329 | - * | |
| 330 | - */ | |
| 331 | -//function learn_press_can_view_item( $item_id, $course_id = 0, $user_id = 0 ) { | |
| 332 | -// if ( ! $user_id ) { | |
| 333 | -// $user_id = get_current_user_id(); | |
| 334 | -// } | |
| 335 | -// $user = learn_press_get_user( $user_id ); | |
| 336 | -// | |
| 337 | -// return $user->can_view_item( $item_id, $course_id ); | |
| 338 | -//} | |
| 339 | - | |
| 340 | -/** | |
| 341 | - * Get course setting is enroll required or public | |
| 342 | - * | |
| 343 | - * @param int $course_id | |
| 344 | - * | |
| 345 | - * @return boolean | |
| 346 | - * @since 0.9.5 | |
| 347 | - * @deprecated 4.2.7.4 | |
| 348 | - */ | |
| 349 | -/*function learn_press_course_enroll_required( $course_id = null ) { | |
| 350 | - $course_id = learn_press_get_course_id( $course_id ); | |
| 351 | - | |
| 352 | - $required = ( 'yes' == get_post_meta( $course_id, '_lpr_course_enrolled_require', true ) ); | |
| 353 | - | |
| 354 | - return apply_filters( 'learn_press_course_enroll_required', $required, $course_id ); | |
| 355 | -}*/ | |
| 356 | - | |
| 357 | -function learn_press_get_all_courses( $args = array() ) { | |
| 358 | - $term = ''; | |
| 359 | - $exclude = ''; | |
| 360 | - is_array( $args ) && extract( $args ); | |
| 361 | - $args = array( | |
| 362 | - 'post_type' => array( 'lp_course' ), | |
| 363 | - 'post_status' => 'publish', | |
| 364 | - 'posts_per_page' => - 1, | |
| 365 | - 's' => $term, | |
| 366 | - 'fields' => 'ids', | |
| 367 | - 'exclude' => $exclude, | |
| 368 | - ); | |
| 369 | - $args = apply_filters( 'learn_press_get_courses_args', $args ); | |
| 370 | - $posts = get_posts( $args ); | |
| 371 | - | |
| 372 | - return apply_filters( 'learn_press_get_courses', $posts, $args ); | |
| 373 | -} | |
| 374 | - | |
| 375 | -/** | |
| 376 | - * @deprecated 4.2.7.4 | |
| 377 | - */ | |
| 378 | -/*function learn_press_search_post_excerpt( $where = '' ) { | |
| 379 | - global $wp_the_query, $wpdb; | |
| 380 | - | |
| 381 | - if ( empty( $wp_the_query->query_vars['s'] ) ) { | |
| 382 | - return $where; | |
| 383 | - } | |
| 384 | - | |
| 385 | - $where = preg_replace( | |
| 386 | - "/post_title\s+LIKE\s*(\'\%[^\%]+\%\')/", | |
| 387 | - "post_title LIKE $1) OR ({$wpdb->posts}.post_excerpt LIKE $1", | |
| 388 | - $where | |
| 389 | - ); | |
| 390 | - | |
| 391 | - return $where; | |
| 392 | -}*/ | |
| 393 | - | |
| 394 | -// add_filter( 'posts_where', 'learn_press_search_post_excerpt' ); | |
| 395 | - | |
| 396 | -function learn_press_get_course_user( $course_id = null ) { | |
| 397 | - if ( ! $course_id ) { | |
| 398 | - $course_id = get_the_ID(); | |
| 399 | - } | |
| 400 | - | |
| 401 | - return learn_press_get_user( get_post_field( 'post_author', $course_id ) ); | |
| 402 | -} | |
| 403 | - | |
| 404 | -/** | |
| 405 | - * Get item types support in course curriculum. | |
| 406 | - * | |
| 407 | - * @return array | |
| 408 | - */ | |
| 409 | -function learn_press_course_get_support_item_types() { | |
| 410 | - $types = [ | |
| 411 | - LP_LESSON_CPT => __( 'Lesson', 'learnpress' ), | |
| 412 | - LP_QUIZ_CPT => __( 'Quiz', 'learnpress' ), | |
| 413 | - ]; | |
| 414 | - | |
| 415 | - return apply_filters( 'learn-press/course-support-items', $types, false ); | |
| 416 | -} | |
| 417 | - | |
| 418 | -/** | |
| 419 | - * Register new type of course item | |
| 420 | - * | |
| 421 | - * @param string $post_type - Usually is post type | |
| 422 | - * @param string $label - Name show for user | |
| 423 | - * @deprecated 4.2.7.4 | |
| 424 | - */ | |
| 425 | -/*function learn_press_course_add_support_item_type( $post_type, $label = '' ) { | |
| 426 | - if ( empty( $GLOBALS['learn_press_course_support_item_types'] ) ) { | |
| 427 | - $GLOBALS['learn_press_course_support_item_types'] = array(); | |
| 428 | - } | |
| 429 | - if ( func_num_args() == 1 && is_array( func_get_arg( 0 ) ) ) { | |
| 430 | - foreach ( func_get_arg( 0 ) as $type => $label ) { | |
| 431 | - learn_press_course_add_support_item_type( $type, $label ); | |
| 432 | - } | |
| 433 | - } elseif ( func_num_args() == 2 ) { | |
| 434 | - $GLOBALS['learn_press_course_support_item_types'][ func_get_arg( 0 ) ] = func_get_arg( 1 ); | |
| 435 | - } | |
| 436 | -}*/ | |
| 437 | - | |
| 438 | -/** | |
| 439 | - * Check if course is support an item's type. | |
| 440 | - * | |
| 441 | - * @param string $type | |
| 442 | - * | |
| 443 | - * @return bool | |
| 444 | - */ | |
| 445 | -function learn_press_is_support_course_item_type( $type ) { | |
| 446 | - $types = learn_press_course_get_support_item_types(); | |
| 447 | - | |
| 448 | - if ( is_array( $type ) ) { | |
| 449 | - $support = true; | |
| 450 | - foreach ( $type as $t ) { | |
| 451 | - $support = $support && learn_press_is_support_course_item_type( $t ); | |
| 452 | - } | |
| 453 | - } else { | |
| 454 | - $support = is_string( $type ) && $type && ! empty( $types[ $type ] ); | |
| 455 | - } | |
| 456 | - | |
| 457 | - return $support; | |
| 458 | -} | |
| 459 | - | |
| 460 | -/** | |
| 461 | - * @deprecated 4.2.7.4 | |
| 462 | - */ | |
| 463 | -/*learn_press_course_add_support_item_type( | |
| 464 | - array( | |
| 465 | - 'lp_lesson' => __( 'Lesson', 'learnpress' ), | |
| 466 | - 'lp_quiz' => __( 'Quiz', 'learnpress' ), | |
| 467 | - ) | |
| 468 | -);*/ | |
| 469 | - | |
| 470 | -function learn_press_add_course_item_feature( $type, $feature ) { | |
| 471 | - $features = array(); | |
| 472 | - if ( ! empty( $GLOBALS['learn_press_course_item_features'] ) ) { | |
| 473 | - $features = $GLOBALS['learn_press_course_item_features']; | |
| 474 | - } | |
| 475 | - | |
| 476 | - if ( empty( $features[ $type ] ) ) { | |
| 477 | - $features[ $type ] = array(); | |
| 478 | - } | |
| 479 | - | |
| 480 | - if ( array_search( $feature, $features ) === false ) { | |
| 481 | - $features[ $type ] = $feature; | |
| 482 | - } | |
| 483 | - | |
| 484 | - $GLOBALS['learn_press_course_item_features'] = $features; | |
| 485 | -} | |
| 486 | - | |
| 487 | - | |
| 488 | -function learn_press_get_course_id() { | |
| 489 | - $course_id = 0; | |
| 490 | - if ( learn_press_is_course() ) { | |
| 491 | - $course_id = get_the_ID(); | |
| 492 | - } | |
| 493 | - | |
| 494 | - return absint( $course_id ); | |
| 495 | -} | |
| 496 | - | |
| 497 | -/** | |
| 498 | - * Get the permalink of a course | |
| 499 | - * | |
| 500 | - * @param int $course_id | |
| 501 | - * | |
| 502 | - * @return string | |
| 503 | - * @since 3.0.0 | |
| 504 | - * | |
| 505 | - */ | |
| 506 | -function learn_press_get_course_permalink( $course_id = 0 ) { | |
| 507 | - if ( $course = learn_press_get_course( $course_id ) ) { | |
| 508 | - return $course->get_permalink(); | |
| 509 | - } | |
| 510 | - | |
| 511 | - return false; | |
| 512 | -} | |
| 513 | - | |
| 514 | - | |
| 515 | -/** | |
| 516 | - * Get the permalink of a item in a course | |
| 517 | - * | |
| 518 | - * @param int $course_id | |
| 519 | - * @param int $item_id | |
| 520 | - * | |
| 521 | - * @return string | |
| 522 | - * @since 3.0.0 | |
| 523 | - * Addon Assignment 4.1.1 still use | |
| 524 | - */ | |
| 525 | -function learn_press_get_course_item_permalink( int $course_id = 0, int $item_id = 0 ): string { | |
| 526 | - $course = learn_press_get_course( $course_id ); | |
| 527 | - if ( $course ) { | |
| 528 | - return $course->get_item_link( $item_id ); | |
| 529 | - } | |
| 530 | - | |
| 531 | - return ''; | |
| 532 | -} | |
| 533 | - | |
| 534 | -/** | |
| 535 | - * @deprecated 4.2.2 | |
| 536 | - */ | |
| 537 | -function learn_press_get_the_course() { | |
| 538 | - _deprecated_function( __FUNCTION__, '4.2.2', 'learn_press_get_course' ); | |
| 539 | - return learn_press_get_course(); | |
| 540 | -} | |
| 541 | - | |
| 542 | -/** | |
| 543 | - * @deprecated 4.2.7.4 | |
| 544 | - */ | |
| 545 | -/*function learn_press_get_user_question_answer( $args = '' ) { | |
| 546 | - $args = wp_parse_args( | |
| 547 | - $args, | |
| 548 | - array( | |
| 549 | - 'question_id' => 0, | |
| 550 | - 'history_id' => 0, | |
| 551 | - 'quiz_id' => 0, | |
| 552 | - 'course_id' => 0, | |
| 553 | - 'user_id' => get_current_user_id(), | |
| 554 | - ) | |
| 555 | - ); | |
| 556 | - $answered = null; | |
| 557 | - if ( $args['history_id'] ) { | |
| 558 | - $user_meta = learn_press_get_user_item_meta( $args['history_id'], 'question_answers', true ); | |
| 559 | - if ( $user_meta && array_key_exists( $args['question_id'], $user_meta ) ) { | |
| 560 | - $answered = $user_meta[ $args['question_id'] ]; | |
| 561 | - } | |
| 562 | - } elseif ( $args['quiz_id'] && $args['course_id'] ) { | |
| 563 | - $user = learn_press_get_user( $args['user_id'] ); | |
| 564 | - $history = $user->get_quiz_results( $args['quiz_id'], $args['course_id'] ); | |
| 565 | - if ( $history ) { | |
| 566 | - $user_meta = learn_press_get_user_item_meta( $history->history_id, 'question_answers', true ); | |
| 567 | - if ( $user_meta && array_key_exists( $args['question_id'], $user_meta ) ) { | |
| 568 | - $answered = $user_meta[ $args['question_id'] ]; | |
| 569 | - } | |
| 570 | - } | |
| 571 | - } | |
| 572 | - | |
| 573 | - return $answered; | |
| 574 | -}*/ | |
| 575 | - | |
| 576 | -/** | |
| 577 | - * @deprecated 4.2.7.4 | |
| 578 | - */ | |
| 579 | -/*function need_to_updating() { | |
| 580 | - ob_start(); | |
| 581 | - learn_press_display_message( 'This function need to updating' ); | |
| 582 | - | |
| 583 | - return ob_get_clean(); | |
| 584 | -}*/ | |
| 585 | - | |
| 586 | -/** | |
| 587 | - * filter section item single course | |
| 588 | - * @deprecated 4.2.7.4 | |
| 589 | - */ | |
| 590 | -/*function learn_press_get_course_sections() { | |
| 591 | - return apply_filters( | |
| 592 | - 'learn_press_get_course_sections', | |
| 593 | - array( | |
| 594 | - 'lp_lesson', | |
| 595 | - 'lp_quiz', | |
| 596 | - ) | |
| 597 | - ); | |
| 598 | -}*/ | |
| 599 | - | |
| 600 | -/** | |
| 601 | - * @deprecated 4.2.7.4 | |
| 602 | - */ | |
| 603 | -/*function lean_press_get_course_sections() { | |
| 604 | - _deprecated_function( __FUNCTION__, '2.1', 'learn_press_get_course_sections' ); | |
| 605 | - | |
| 606 | - return learn_press_get_course_sections(); | |
| 607 | -}*/ | |
| 608 | - | |
| 609 | -/** | |
| 610 | - * @deprecated 4.2.7.4 | |
| 611 | - */ | |
| 612 | -/*if ( ! function_exists( 'learn_press_get_course_item_url' ) ) { | |
| 613 | - function learn_press_get_course_item_url( $course_id = null, $item_id = null ) { | |
| 614 | - $course = learn_press_get_course( $course_id ); | |
| 615 | - | |
| 616 | - return $course ? $course->get_item_link( $item_id ) : false; | |
| 617 | - } | |
| 618 | -}*/ | |
| 619 | - | |
| 620 | -/** | |
| 621 | - * Add filter to WP comment form of lesson or quiz to output ID of current course. | |
| 622 | - * | |
| 623 | - * @param $post_id | |
| 624 | - * | |
| 625 | - * @since 3.0.10 | |
| 626 | - * | |
| 627 | - */ | |
| 628 | -function learn_press_comment_post_item_course( $post_id ) { | |
| 629 | - $course = learn_press_get_course(); | |
| 630 | - if ( ! $course ) { | |
| 631 | - return; | |
| 632 | - } | |
| 633 | - | |
| 634 | - echo sprintf( '<input type="hidden" name="comment-post-item-course" value="%d" />', $course->get_id() ); | |
| 635 | -} | |
| 636 | - | |
| 637 | -add_action( 'comment_form', 'learn_press_comment_post_item_course' ); | |
| 638 | - | |
| 639 | -function learn_press_item_comment_link( $link, $comment, $args, $cpage ) { | |
| 640 | - | |
| 641 | - $comment_post_ID = $comment->comment_post_ID; | |
| 642 | - | |
| 643 | - /** | |
| 644 | - * Validate if comment post is an item of course | |
| 645 | - */ | |
| 646 | - if ( ! learn_press_is_support_course_item_type( learn_press_get_post_type( $comment_post_ID ) ) ) { | |
| 647 | - return $link; | |
| 648 | - } | |
| 649 | - | |
| 650 | - $post_id = 0; | |
| 651 | - | |
| 652 | - /** | |
| 653 | - * Ensure there is a course | |
| 654 | - */ | |
| 655 | - if ( empty( $_POST['comment-post-item-course'] ) ) { | |
| 656 | - $course = learn_press_get_course(); | |
| 657 | - if ( $course ) { | |
| 658 | - $post_id = $course->get_id(); | |
| 659 | - } | |
| 660 | - } else { | |
| 661 | - $post_id = absint( $_POST['comment-post-item-course'] ); | |
| 662 | - } | |
| 663 | - | |
| 664 | - $course = learn_press_get_course( $post_id ); | |
| 665 | - if ( $course ) { | |
| 666 | - $link = str_replace( get_the_permalink( $comment_post_ID ), $course->get_item_link( $comment_post_ID ), $link ); | |
| 667 | - } | |
| 668 | - | |
| 669 | - return $link; | |
| 670 | -} | |
| 671 | - | |
| 672 | -add_filter( 'get_comment_link', 'learn_press_item_comment_link', 100, 4 ); | |
| 673 | - | |
| 674 | -/** | |
| 675 | - * Fix redirection invalid when SG Cache is installed | |
| 676 | - * | |
| 677 | - * @param int $comment_id | |
| 678 | - * @param string $status | |
| 679 | - * | |
| 680 | - * @since 3.0.10 | |
| 681 | - * @deprecated 4.1.6.9 | |
| 682 | - * | |
| 683 | - */ | |
| 684 | -/*function learn_press_force_refresh_course( $comment_id, $status ) { | |
| 685 | - | |
| 686 | - if ( empty( $_POST['comment-post-item-course'] ) ) { | |
| 687 | - return; | |
| 688 | - } | |
| 689 | - | |
| 690 | - $course_id = absint( $_POST['comment-post-item-course'] ); | |
| 691 | - $course = learn_press_get_course( $course_id ); | |
| 692 | - $curd = new LP_Course_CURD(); | |
| 693 | - $curd->load( $course ); | |
| 694 | -} | |
| 695 | - | |
| 696 | -add_action( 'comment_post', 'learn_press_force_refresh_course', 1000, 2 );*/ | |
| 697 | - | |
| 698 | -/** | |
| 699 | - * @deprecated 4.1.6.9 | |
| 700 | - */ | |
| 701 | -/*if ( ! function_exists( 'learn_press_get_nav_course_item_url' ) ) { | |
| 702 | - function learn_press_get_nav_course_item_url( $course_id = null, $item_id = null, $content_only = false ) { | |
| 703 | - | |
| 704 | - $course = learn_press_get_course( $course_id ); | |
| 705 | - $curriculum_items = $course->get_items();// LP_Helper::maybe_unserialize( $course->post->curriculum_items ); | |
| 706 | - $index = array_search( $item_id, $curriculum_items ); | |
| 707 | - $return = array( | |
| 708 | - 'back' => '', | |
| 709 | - 'next' => '', | |
| 710 | - ); | |
| 711 | - if ( is_array( $curriculum_items ) ) { | |
| 712 | - if ( array_key_exists( $index - 1, $curriculum_items ) ) { | |
| 713 | - $back_item = get_post( $curriculum_items[ $index - 1 ] ); | |
| 714 | - $return['back'] = array( | |
| 715 | - 'id' => $back_item->ID, | |
| 716 | - 'link' => $course->get_item_link( $curriculum_items[ $index - 1 ] ), | |
| 717 | - 'title' => $back_item->post_title, | |
| 718 | - ); | |
| 719 | - if ( $content_only ) { | |
| 720 | - $return['back']['link'] .= '?content-item-only=yes'; | |
| 721 | - } | |
| 722 | - } | |
| 723 | - if ( array_key_exists( $index + 1, $curriculum_items ) ) { | |
| 724 | - $next_item = get_post( $curriculum_items[ $index + 1 ] ); | |
| 725 | - $return['next'] = array( | |
| 726 | - 'id' => $next_item->ID, | |
| 727 | - 'link' => $course->get_item_link( $curriculum_items[ $index + 1 ] ), | |
| 728 | - 'title' => $next_item->post_title, | |
| 729 | - ); | |
| 730 | - if ( $content_only ) { | |
| 731 | - $return['next']['link'] .= '?content-item-only=yes'; | |
| 732 | - } | |
| 733 | - } | |
| 734 | - } | |
| 735 | - | |
| 736 | - return $return; | |
| 737 | - } | |
| 738 | -}*/ | |
| 739 | - | |
| 740 | -/** | |
| 741 | - * Get course id of an item by id | |
| 742 | - * | |
| 743 | - * @deprecated 4.1.6.9 | |
| 744 | - */ | |
| 745 | - | |
| 746 | -if ( ! function_exists( 'learn_press_get_item_course_id' ) ) { | |
| 747 | - | |
| 748 | - function learn_press_get_item_course_id( $post_id, $post_type ) { | |
| 749 | - _deprecated_function( __FUNCTION__, '4.1.6.9' ); | |
| 750 | - /*global $wpdb; | |
| 751 | - | |
| 752 | - // If the post is a course | |
| 753 | - if ( LP_COURSE_CPT == learn_press_get_post_type( $post_id ) ) { | |
| 754 | - return false; | |
| 755 | - } | |
| 756 | - | |
| 757 | - if ( ! $post_types = learn_press_course_get_support_item_types( true ) ) { | |
| 758 | - return false; | |
| 759 | - } | |
| 760 | - | |
| 761 | - if ( ! in_array( learn_press_get_post_type( $post_id ), $post_types ) ) { | |
| 762 | - return false; | |
| 763 | - } | |
| 764 | - | |
| 765 | - $course_id = false; | |
| 766 | - | |
| 767 | - if ( false !== ( $courses = LP_Object_Cache::get( 'item-course-ids', 'learn-press' ) ) ) { | |
| 768 | - | |
| 769 | - foreach ( $courses as $course_id => $items ) { | |
| 770 | - if ( in_array( $post_id, $items ) ) { | |
| 771 | - break; | |
| 772 | - } | |
| 773 | - $course_id = false; | |
| 774 | - } | |
| 775 | - } else { | |
| 776 | - $courses = array(); | |
| 777 | - } | |
| 778 | - | |
| 779 | - if ( false === $course_id ) { | |
| 780 | - $query = $wpdb->prepare( | |
| 781 | - " | |
| 782 | - SELECT section.section_course_id | |
| 783 | - FROM {$wpdb->learnpress_sections} AS section | |
| 784 | - INNER JOIN {$wpdb->learnpress_section_items} AS item | |
| 785 | - ON item.section_id = section.section_id | |
| 786 | - WHERE item.item_id = %d | |
| 787 | - LIMIT 1 | |
| 788 | - ", | |
| 789 | - $post_id | |
| 790 | - ); | |
| 791 | - | |
| 792 | - $course_id = apply_filters( 'learn-press/item-course-id', absint( $wpdb->get_var( $query ) ), $post_id ); | |
| 793 | - | |
| 794 | - if ( $course = learn_press_get_course( $course_id ) ) { | |
| 795 | - $courses[ $course_id ] = $course->get_items(); | |
| 796 | - } | |
| 797 | - | |
| 798 | - if ( empty( $courses[ $course_id ] ) ) { | |
| 799 | - $courses[ $course_id ] = array(); | |
| 800 | - } | |
| 801 | - | |
| 802 | - if ( ! in_array( $post_id, $courses[ $course_id ] ) ) { | |
| 803 | - $courses[ $course_id ][] = $post_id; | |
| 804 | - } | |
| 805 | - LP_Object_Cache::set( 'item-course-ids', $courses, 'learn-press' ); | |
| 806 | - | |
| 807 | - } | |
| 808 | - | |
| 809 | - return $course_id;*/ | |
| 810 | - } | |
| 811 | -} | |
| 812 | - | |
| 813 | -/** | |
| 814 | - * @editor tungnx | comment code | |
| 815 | - * @deprecated 3.2.7.5 | |
| 816 | - */ | |
| 817 | -/*function learn_press_item_sample_permalink_html( $return, $post_id, $new_title, $new_slug, $post ) { | |
| 818 | - remove_filter( 'get_sample_permalink_html', 'learn_press_item_sample_permalink_html', 10 ); | |
| 819 | - | |
| 820 | - $return = sprintf( | |
| 821 | - '<a class="button" href="%s" target="_blank">%s</a>', | |
| 822 | - learn_press_get_preview_url( $post_id ), | |
| 823 | - __( 'Preview', 'learnpress' ) | |
| 824 | - ); | |
| 825 | - | |
| 826 | - $return .= '<span>' . __( 'Permalink only available if the item is already assigned to a course.', 'learnpress' ) . '</span>'; | |
| 827 | - | |
| 828 | - return sprintf( '<div id="learn-press-box-edit-slug">%s</div>', $return ); | |
| 829 | -}*/ | |
| 830 | - | |
| 831 | -/** | |
| 832 | - * @editor tungnx | comment code | |
| 833 | - * @deprecated 3.2.7.5 | |
| 834 | - */ | |
| 835 | -/*if ( ! function_exists( 'learn_press_item_sample_permalink' ) ) { | |
| 836 | - | |
| 837 | - function learn_press_item_sample_permalink( $permalink, $post_id, $title, $name, $post ) { | |
| 838 | - if ( ! in_array( $post->post_type, learn_press_course_get_support_item_types( true ) ) ) { | |
| 839 | - return $permalink; | |
| 840 | - } | |
| 841 | - | |
| 842 | - $permalink[0] = str_replace( $post->post_name, '%pagename%', $permalink[0] ); | |
| 843 | - | |
| 844 | - if ( ! preg_match( '~^https?://~', $permalink[0] ) ) { | |
| 845 | - add_filter( 'get_sample_permalink_html', 'learn_press_item_sample_permalink_html', 10, 5 ); | |
| 846 | - } | |
| 847 | - | |
| 848 | - return $permalink; | |
| 849 | - } | |
| 850 | - | |
| 851 | -}*/ | |
| 852 | -//add_filter( 'get_sample_permalink', 'learn_press_item_sample_permalink', 10, 5 ); | |
| 853 | - | |
| 854 | -/** | |
| 855 | - * Get preview url for LP post type. | |
| 856 | - * | |
| 857 | - * @param int $post_id | |
| 858 | - * | |
| 859 | - * @return string | |
| 860 | - * @since 3.0.0 | |
| 861 | - * | |
| 862 | - */ | |
| 863 | -function learn_press_get_preview_url( $post_id ) { | |
| 864 | - return esc_url_raw( | |
| 865 | - add_query_arg( | |
| 866 | - array( | |
| 867 | - 'lp-preview' => $post_id, | |
| 868 | - '_wpnonce' => wp_create_nonce( 'lp-preview' ), | |
| 869 | - ), | |
| 870 | - trailingslashit( get_home_url() ) | |
| 871 | - ) | |
| 872 | - ); | |
| 873 | -} | |
| 874 | - | |
| 875 | -if ( ! function_exists( 'learn_press_course_item_type_link' ) ) { | |
| 876 | - /** | |
| 877 | - * Add filter to WP custom post-type-link to edit the link of item | |
| 878 | - * with the link of it's course. | |
| 879 | - * | |
| 880 | - * @updated 12 Nov 2018 | |
| 881 | - * | |
| 882 | - * @param string $post_link | |
| 883 | - * @param WP_Post $post | |
| 884 | - * @param bool $leavename | |
| 885 | - * @param bool $sample | |
| 886 | - * | |
| 887 | - * @editor tungnx | comment code | |
| 888 | - * @return string | |
| 889 | - * @deprecated 3.2.7.4 | |
| 890 | - */ | |
| 891 | - /*function learn_press_course_item_type_link( $post_link, $post, $leavename, $sample ) { | |
| 892 | - | |
| 893 | - remove_filter( 'post_type_link', 'learn_press_course_item_type_link', 10 ); | |
| 894 | - | |
| 895 | - $course = learn_press_get_course(); | |
| 896 | - | |
| 897 | - if ( ! $course && ( $course_id = learn_press_get_item_course( $post->ID ) ) ) { | |
| 898 | - $course = learn_press_get_course( $course_id ); | |
| 899 | - } | |
| 900 | - | |
| 901 | - if ( learn_press_is_support_course_item_type( $post->post_type ) ) { | |
| 902 | - // Check elementor installed and activated | |
| 903 | - if ( did_action( 'elementor/loaded' ) ) { | |
| 904 | - // do stuff for edit mode | |
| 905 | - if ( ! Elementor\Plugin::$instance->editor->is_edit_mode() ) { | |
| 906 | - if ( $course ) { | |
| 907 | - $post_link = $course->get_item_link( $post->ID ); | |
| 908 | - } else { | |
| 909 | - $post_link = learn_press_get_sample_link_course_item_url( $post->ID ); | |
| 910 | - } | |
| 911 | - } | |
| 912 | - } else { | |
| 913 | - if ( $course ) { | |
| 914 | - $post_link = $course->get_item_link( $post->ID ); | |
| 915 | - } else { | |
| 916 | - $post_link = learn_press_get_sample_link_course_item_url( $post->ID ); | |
| 917 | - } | |
| 918 | - } | |
| 919 | - } | |
| 920 | - | |
| 921 | - add_filter( 'post_type_link', 'learn_press_course_item_type_link', 10, 4 ); | |
| 922 | - | |
| 923 | - return $post_link; | |
| 924 | - }*/ | |
| 925 | -} | |
| 926 | -//add_filter( 'post_type_link', 'learn_press_course_item_type_link', 10, 4 ); | |
| 927 | - | |
| 928 | -/** | |
| 929 | - * Get grade course type can translate | |
| 930 | - * | |
| 931 | - * @param string $grade | |
| 932 | - * @param bool $echo | |
| 933 | - * | |
| 934 | - * @return mixed|void | |
| 935 | - */ | |
| 936 | -function learn_press_course_grade_html( string $grade = '', bool $echo = true ) { | |
| 937 | - switch ( $grade ) { | |
| 938 | - case 'passed': | |
| 939 | - $html = __( 'Passed', 'learnpress' ); | |
| 940 | - break; | |
| 941 | - case 'failed': | |
| 942 | - $html = __( 'Failed', 'learnpress' ); | |
| 943 | - break; | |
| 944 | - case 'in-progress': | |
| 945 | - $html = __( 'In Progress', 'learnpress' ); | |
| 946 | - break; | |
| 947 | - default: | |
| 948 | - $html = $grade; | |
| 949 | - break; | |
| 950 | - } | |
| 951 | - | |
| 952 | - // @since 3.0.0 | |
| 953 | - $html = apply_filters( 'learn-press/course/grade-html', $html, $grade ); | |
| 954 | - | |
| 955 | - if ( $echo ) { | |
| 956 | - echo wp_kses_post( $html ); | |
| 957 | - } | |
| 958 | - | |
| 959 | - return $html; | |
| 960 | -} | |
| 961 | - | |
| 962 | -/** | |
| 963 | - * @deprecated 4.2.7.4 | |
| 964 | - */ | |
| 965 | -/*function learn_press_get_course_results_tooltip( $course_id ) { | |
| 966 | - $metabox = LP_Course_Post_Type::assessment_meta_box(); | |
| 967 | - $options = $metabox['fields'][0]['options']; | |
| 968 | - $cr = get_post_meta( $course_id, '_lp_course_result', true ); | |
| 969 | - $tooltip = ! empty( $options[ $cr ] ) ? $options[ $cr ] : false; | |
| 970 | - if ( $tooltip ) { | |
| 971 | - if ( preg_match_all( '~<p.*>(.*)<\/p>~im', $tooltip, $matches ) ) { | |
| 972 | - $tooltip = $matches[1][0]; | |
| 973 | - } | |
| 974 | - } | |
| 975 | - | |
| 976 | - return $tooltip; | |
| 977 | -}*/ | |
| 978 | - | |
| 979 | -function learn_press_course_passing_condition( $value, $format, $course_id ) { | |
| 980 | - $course = learn_press_get_course( $course_id ); | |
| 981 | - $quiz_id = $course->get_final_quiz(); | |
| 982 | - $evalution = get_post_meta( $course_id, '_lp_course_result', true ); | |
| 983 | - | |
| 984 | - if ( $quiz_id && $evalution === 'evaluate_final_quiz' ) { | |
| 985 | - $quiz = learn_press_get_quiz( $quiz_id ); | |
| 986 | - $value = absint( $quiz->get_passing_grade() ); | |
| 987 | - | |
| 988 | - if ( $format ) { | |
| 989 | - $value = "{$value}%"; | |
| 990 | - } | |
| 991 | - } | |
| 992 | - | |
| 993 | - return $value; | |
| 994 | -} | |
| 995 | - | |
| 996 | -add_filter( 'learn-press/course-passing-condition', 'learn_press_course_passing_condition', 10, 3 ); | |
| 997 | - | |
| 998 | -/** | |
| 999 | - * @deprecated 4.2.7.4 | |
| 1000 | - */ | |
| 1001 | -function learn_press_remove_query_var_enrolled_course( $redirect ) { | |
| 1002 | - return esc_url_raw( remove_query_arg( 'enroll-course', $redirect ) ); | |
| 1003 | -} | |
| 1004 | - | |
| 1005 | -//add_filter( 'learn-press/enroll-course-redirect', 'learn_press_remove_query_var_enrolled_course' ); | |
| 1006 | - | |
| 1007 | -/** | |
| 1008 | - * Mark the user to know if they have just logged in | |
| 1009 | - * for some purpose. | |
| 1010 | - * | |
| 1011 | - * @since 3.0.0 | |
| 1012 | - */ | |
| 1013 | -/*function learn_press_mark_user_just_logged_in() { | |
| 1014 | - LearnPress::instance()->session->set( 'user_just_logged_in', 'yes' ); | |
| 1015 | -} | |
| 1016 | -add_action( 'wp_login', 'learn_press_mark_user_just_logged_in' );*/ | |
| 1017 | - | |
| 1018 | -function learn_press_translate_course_result_required( $course ) { | |
| 1019 | - if ( ! $course ) { | |
| 1020 | - return ''; | |
| 1021 | - } | |
| 1022 | - | |
| 1023 | - $passing_condition = $course->get_passing_condition(); | |
| 1024 | - | |
| 1025 | - $evaluate_type = $course->get_data( 'course_result', 'evaluate_lesson' ); | |
| 1026 | - switch ( $evaluate_type ) { | |
| 1027 | - case 'evaluate_lesson': | |
| 1028 | - $label = esc_html__( 'completed lessons per the total number of lessons.', 'learnpress' ); | |
| 1029 | - break; | |
| 1030 | - case 'evaluate_quiz': | |
| 1031 | - $label = esc_html__( 'passed quizzes per the total number of quizzes.', 'learnpress' ); | |
| 1032 | - break; | |
| 1033 | - case 'evaluate_final_quiz': | |
| 1034 | - $label = esc_html__( 'Final Quiz', 'learnpress' ); | |
| 1035 | - break; | |
| 1036 | - case 'evaluate_questions': | |
| 1037 | - $label = esc_html__( 'correct answers per the total number of questions.', 'learnpress' ); | |
| 1038 | - break; | |
| 1039 | - case 'evaluate_mark': | |
| 1040 | - $label = esc_html__( 'score achieved per the total score of the questions.', 'learnpress' ); | |
| 1041 | - break; | |
| 1042 | - default: | |
| 1043 | - $label = apply_filters( 'learnpress/message/evaluate/' . $evaluate_type, $evaluate_type ); | |
| 1044 | - break; | |
| 1045 | - } | |
| 1046 | - | |
| 1047 | - return apply_filters( | |
| 1048 | - 'learnpress/message/evaluate', | |
| 1049 | - wp_sprintf( '%1$s %2$s %3$s', __( 'Require', 'learnpress' ), $passing_condition . '%', $label ) | |
| 1050 | - ); | |
| 1051 | -} | |
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Common functions to manipulate with course, lesson, quiz, questions, etc... | |
| 4 | + * | |
| 5 | + * @author ThimPress | |
| 6 | + * @package LearnPress/Functions | |
| 7 | + * @version 1.0 | |
| 8 | + */ | |
| 9 | + | |
| 10 | +defined( 'ABSPATH' ) || exit(); | |
| 11 | + | |
| 12 | +/** | |
| 13 | + * Get course current on single course or course by id | |
| 14 | + * Only use learn_press_get_course() on the single page | |
| 15 | + * Another page use learn_press_get_course(id) | |
| 16 | + * | |
| 17 | + * @param int $the_course | |
| 18 | + * @since 3.0.0 | |
| 19 | + * @version 1.0.1 | |
| 20 | + * @editor tungnx | |
| 21 | + * @return bool|LP_Course|mixed | |
| 22 | + */ | |
| 23 | +function learn_press_get_course( $the_course = 0 ) { | |
| 24 | + $the_course = (int) $the_course; | |
| 25 | + | |
| 26 | + if ( 0 === $the_course ) { | |
| 27 | + $the_course = get_the_ID() ? get_the_ID() : 0; | |
| 28 | + } | |
| 29 | + | |
| 30 | + return LP_Course::get_course( $the_course ); | |
| 31 | +} | |
| 32 | + | |
| 33 | +/** | |
| 34 | + * @editor tungnx | |
| 35 | + * @modify 4.1.4.1 - comment - not use | |
| 36 | + */ | |
| 37 | +/*function learn_press_get_course_by_id( $id ) { | |
| 38 | + if ( false !== ( $courses = LP_Object_Cache::get( 'object', 'learn-press/courses' ) ) ) { | |
| 39 | + return ! empty( $courses[ $id ] ) ? $courses[ $id ] : false; | |
| 40 | + } | |
| 41 | + | |
| 42 | + return false; | |
| 43 | +}*/ | |
| 44 | + | |
| 45 | +/** | |
| 46 | + * Create nonce for course action. | |
| 47 | + * Return nonce created with format 'learn-press-$action-$course_id-course-$user_id' | |
| 48 | + * | |
| 49 | + * @param string $action [retake, purchase, enroll] | |
| 50 | + * @param int $course_id | |
| 51 | + * @param int $user_id | |
| 52 | + * | |
| 53 | + * @return string | |
| 54 | + * @since 3.0.0 | |
| 55 | + * @deprecated 4.1.6.9 | |
| 56 | + */ | |
| 57 | +/*function learn_press_create_course_action_nonce( $action, $course_id = 0, $user_id = 0 ) { | |
| 58 | + return LP_Nonce_Helper::create_course( $action, $course_id, $user_id ); | |
| 59 | +}*/ | |
| 60 | + | |
| 61 | +/** | |
| 62 | + * Verify nonce for course action. | |
| 63 | + * | |
| 64 | + * @param string $nonce | |
| 65 | + * @param string $action | |
| 66 | + * @param int $course_id | |
| 67 | + * @param int $user_id | |
| 68 | + * | |
| 69 | + * @return bool | |
| 70 | + * @since 3.0.0 | |
| 71 | + * @deprecated 4.1.6.9 | |
| 72 | + */ | |
| 73 | +/*function learn_press_verify_course_action_nonce( $nonce, $action, $course_id = 0, $user_id = 0 ) { | |
| 74 | + return LP_Nonce_Helper::verify_course( $nonce, $action, $course_id, $user_id ); | |
| 75 | +}*/ | |
| 76 | + | |
| 77 | +/** | |
| 78 | + * Get type of items are supported in course curriculum (post types). | |
| 79 | + * Default: [lp_lesson, lp_quiz] | |
| 80 | + * When all addon use hook 'learn-press/course/item-types-support', will deprecate this function | |
| 81 | + * @use CourseModel::item_types_support | |
| 82 | + * | |
| 83 | + * @return mixed | |
| 84 | + * @since 3.0.0 | |
| 85 | + * @editor tungnx | |
| 86 | + * @version 1.0.1 | |
| 87 | + * @return array | |
| 88 | + * @deprecated | |
| 89 | + */ | |
| 90 | +function learn_press_get_course_item_types( bool $return_only_value = true ): array { | |
| 91 | + return apply_filters( | |
| 92 | + 'learn-press/course-item-type', | |
| 93 | + array( LP_LESSON_CPT, LP_QUIZ_CPT ) | |
| 94 | + ); | |
| 95 | +} | |
| 96 | + | |
| 97 | +/** | |
| 98 | + * Get type of items can purchase on LP Order. | |
| 99 | + * Default: ['lp_course', 'lp_certificate'] | |
| 100 | + * | |
| 101 | + * @return mixed | |
| 102 | + * @since 3.0.0 | |
| 103 | + * | |
| 104 | + */ | |
| 105 | +function learn_press_get_item_types_can_purchase() { | |
| 106 | + return apply_filters( | |
| 107 | + 'learn-press/purchase/item-types/can-purchase', | |
| 108 | + array( LP_COURSE_CPT ) | |
| 109 | + ); | |
| 110 | +} | |
| 111 | + | |
| 112 | +/** | |
| 113 | + * Get the courses that a item is assigned to | |
| 114 | + * | |
| 115 | + * @param $item | |
| 116 | + * | |
| 117 | + * @return mixed | |
| 118 | + * @Todo - tungnx need review code to rewrite. | |
| 119 | + */ | |
| 120 | +function learn_press_get_item_courses( $item ) { | |
| 121 | + global $wpdb; | |
| 122 | + $query = $wpdb->prepare( | |
| 123 | + " | |
| 124 | + SELECT c.* | |
| 125 | + FROM {$wpdb->posts} c | |
| 126 | + INNER JOIN {$wpdb->learnpress_sections} s ON c.ID = s.section_course_id | |
| 127 | + INNER JOIN {$wpdb->learnpress_section_items} si ON si.section_id = s.section_id | |
| 128 | + WHERE si.item_id = %d | |
| 129 | + ", | |
| 130 | + $item | |
| 131 | + ); | |
| 132 | + | |
| 133 | + return $wpdb->get_results( $query ); | |
| 134 | +} | |
| 135 | + | |
| 136 | +/** | |
| 137 | + * @deprecated 4.2.7.4 | |
| 138 | + */ | |
| 139 | +/*function _learn_press_usort_terms_by_ID( $terms ) { | |
| 140 | + $version = get_bloginfo( 'version' ); | |
| 141 | + if ( version_compare( $version, '4.7', '>=' ) ) { | |
| 142 | + $terms = wp_list_sort( $terms, 'term_id' ); | |
| 143 | + } else { | |
| 144 | + usort( $terms, '_usort_terms_by_ID' ); | |
| 145 | + } | |
| 146 | + | |
| 147 | + return $terms; | |
| 148 | +}*/ | |
| 149 | + | |
| 150 | +/*function learn_press_course_post_type_link( $permalink, $post ) { | |
| 151 | + if ( $post->post_type !== 'lp_course' ) { | |
| 152 | + return $permalink; | |
| 153 | + } | |
| 154 | + | |
| 155 | + // Abort early if the placeholder rewrite tag isn't in the generated URL | |
| 156 | + if ( false === strpos( $permalink, '%' ) ) { | |
| 157 | + return $permalink; | |
| 158 | + } | |
| 159 | + | |
| 160 | + // Get the custom taxonomy terms in use by this post | |
| 161 | + $terms = get_the_terms( $post->ID, 'course_category' ); | |
| 162 | + | |
| 163 | + if ( ! empty( $terms ) ) { | |
| 164 | + $terms = _learn_press_usort_terms_by_ID( $terms ); // order by ID | |
| 165 | + $category_object = apply_filters( | |
| 166 | + 'learn_press_course_post_type_link_course_category', | |
| 167 | + $terms[0], | |
| 168 | + $terms, | |
| 169 | + $post | |
| 170 | + ); | |
| 171 | + $category_object = get_term( $category_object, 'course_category' ); | |
| 172 | + $course_category = $category_object->slug; | |
| 173 | + | |
| 174 | + if ( $parent = $category_object->parent ) { | |
| 175 | + $ancestors = get_ancestors( $category_object->term_id, 'course_category' ); | |
| 176 | + foreach ( $ancestors as $ancestor ) { | |
| 177 | + $ancestor_object = get_term( $ancestor, 'course_category' ); | |
| 178 | + $course_category = $ancestor_object->slug . '/' . $course_category; | |
| 179 | + } | |
| 180 | + } | |
| 181 | + } else { | |
| 182 | + // If no terms are assigned to this post, use a string instead (can't leave the placeholder there) | |
| 183 | + $course_category = _x( 'uncategorized', 'slug', 'learnpress' ); | |
| 184 | + } | |
| 185 | + | |
| 186 | + $find = array( | |
| 187 | + '%year%', | |
| 188 | + '%monthnum%', | |
| 189 | + '%day%', | |
| 190 | + '%hour%', | |
| 191 | + '%minute%', | |
| 192 | + '%second%', | |
| 193 | + '%post_id%', | |
| 194 | + '%category%', | |
| 195 | + '%course_category%', | |
| 196 | + ); | |
| 197 | + | |
| 198 | + $replace = array( | |
| 199 | + date_i18n( 'Y', strtotime( $post->post_date ) ), | |
| 200 | + date_i18n( 'm', strtotime( $post->post_date ) ), | |
| 201 | + date_i18n( 'd', strtotime( $post->post_date ) ), | |
| 202 | + date_i18n( 'H', strtotime( $post->post_date ) ), | |
| 203 | + date_i18n( 'i', strtotime( $post->post_date ) ), | |
| 204 | + date_i18n( 's', strtotime( $post->post_date ) ), | |
| 205 | + $post->ID, | |
| 206 | + $course_category, | |
| 207 | + $course_category, | |
| 208 | + ); | |
| 209 | + | |
| 210 | + $permalink = str_replace( $find, $replace, $permalink ); | |
| 211 | + | |
| 212 | + return $permalink; | |
| 213 | +}*/ | |
| 214 | + | |
| 215 | +//add_filter( 'post_type_link', 'learn_press_course_post_type_link', 10, 2 ); | |
| 216 | + | |
| 217 | +function learn_press_item_meta_format( $item, $nonce = '' ) { | |
| 218 | + if ( current_theme_supports( 'post-formats' ) ) { | |
| 219 | + $format = get_post_format( $item ); | |
| 220 | + if ( false === $format ) { | |
| 221 | + $format = 'standard'; | |
| 222 | + } | |
| 223 | + | |
| 224 | + // return false to hide post format | |
| 225 | + $format = apply_filters( 'learn_press_course_item_format', $format, $item ); | |
| 226 | + if ( $format ) { | |
| 227 | + printf( | |
| 228 | + '<label for="post-format-0" class="post-format-icon post-format-%s" title="%s"></label>', | |
| 229 | + $format, | |
| 230 | + ucfirst( $format ) | |
| 231 | + ); | |
| 232 | + } else { | |
| 233 | + echo esc_html( $nonce ); | |
| 234 | + } | |
| 235 | + } | |
| 236 | +} | |
| 237 | + | |
| 238 | +/** | |
| 239 | + * @deprecated 4.2.7.4 | |
| 240 | + */ | |
| 241 | +/*function learn_press_course_item_format_exclude( $format, $item ) { | |
| 242 | + if ( learn_press_get_post_type( $item ) != LP_LESSON_CPT || ( $format == 'standard' ) ) { | |
| 243 | + $format = false; | |
| 244 | + } | |
| 245 | + | |
| 246 | + return $format; | |
| 247 | +}*/ | |
| 248 | + | |
| 249 | +/** | |
| 250 | + * Get curriculum of a course | |
| 251 | + * | |
| 252 | + * @param $course_id | |
| 253 | + * | |
| 254 | + * @return mixed | |
| 255 | + * @version 1.0 | |
| 256 | + * @deprecated 4.2.7.4 | |
| 257 | + */ | |
| 258 | +/*function learn_press_get_course_curriculum( $course_id ) { | |
| 259 | + $course = learn_press_get_course( $course_id ); | |
| 260 | + | |
| 261 | + return $course->get_curriculum(); | |
| 262 | +}*/ | |
| 263 | + | |
| 264 | +/** | |
| 265 | + * Verify course access | |
| 266 | + * | |
| 267 | + * @param int $course_id | |
| 268 | + * @param int $user_id | |
| 269 | + * | |
| 270 | + * @return boolean | |
| 271 | + * @deprecated 4.1.3 | |
| 272 | + * @editor tungnx | |
| 273 | + */ | |
| 274 | +/*function learn_press_is_enrolled_course( $course_id = null, $user_id = null ) { | |
| 275 | + _deprecated_function( __FUNCTION__, '4.1.3' ); | |
| 276 | + if ( $course = learn_press_get_course( $course_id ) && $user = learn_press_get_user( $user_id ) ) { | |
| 277 | + return $user->has_enrolled_course( $course_id ); | |
| 278 | + } | |
| 279 | + | |
| 280 | + return false; | |
| 281 | +}*/ | |
| 282 | + | |
| 283 | +/** | |
| 284 | + * Detect if a course is free or not | |
| 285 | + * | |
| 286 | + * @param null $course_id | |
| 287 | + * | |
| 288 | + * @return bool | |
| 289 | + * @deprecated 4.2.7.4 | |
| 290 | + */ | |
| 291 | +/*function learn_press_is_free_course( $course_id = null ) { | |
| 292 | + if ( ! $course_id ) { | |
| 293 | + $course_id = get_the_ID(); | |
| 294 | + } | |
| 295 | + | |
| 296 | + return learn_press_get_course( $course_id )->is_free(); | |
| 297 | +}*/ | |
| 298 | + | |
| 299 | +/** | |
| 300 | + * get current status of user's course | |
| 301 | + * | |
| 302 | + * @param int $user_id | |
| 303 | + * @param int $course_id | |
| 304 | + * | |
| 305 | + * @return string | |
| 306 | + * @author Tunn | |
| 307 | + * @deprecated 4.2.7.4 | |
| 308 | + */ | |
| 309 | +/*function learn_press_get_user_course_status( $user_id = null, $course_id = null ) { | |
| 310 | + $course = learn_press_get_course( $course_id ); | |
| 311 | + $user = learn_press_get_user( $user_id ); | |
| 312 | + | |
| 313 | + if ( $course && $user ) { | |
| 314 | + return $user->get_course_status( $course_id ); | |
| 315 | + } | |
| 316 | + | |
| 317 | + return false; | |
| 318 | +}*/ | |
| 319 | + | |
| 320 | +/** | |
| 321 | + * Wrap function can-view-item of user object. | |
| 322 | + * | |
| 323 | + * @param int $item_id | |
| 324 | + * @param int $course_id | |
| 325 | + * @param int $user_id | |
| 326 | + * | |
| 327 | + * @return mixed | |
| 328 | + * @since 3.1.0 | |
| 329 | + * | |
| 330 | + */ | |
| 331 | +//function learn_press_can_view_item( $item_id, $course_id = 0, $user_id = 0 ) { | |
| 332 | +// if ( ! $user_id ) { | |
| 333 | +// $user_id = get_current_user_id(); | |
| 334 | +// } | |
| 335 | +// $user = learn_press_get_user( $user_id ); | |
| 336 | +// | |
| 337 | +// return $user->can_view_item( $item_id, $course_id ); | |
| 338 | +//} | |
| 339 | + | |
| 340 | +/** | |
| 341 | + * Get course setting is enroll required or public | |
| 342 | + * | |
| 343 | + * @param int $course_id | |
| 344 | + * | |
| 345 | + * @return boolean | |
| 346 | + * @since 0.9.5 | |
| 347 | + * @deprecated 4.2.7.4 | |
| 348 | + */ | |
| 349 | +/*function learn_press_course_enroll_required( $course_id = null ) { | |
| 350 | + $course_id = learn_press_get_course_id( $course_id ); | |
| 351 | + | |
| 352 | + $required = ( 'yes' == get_post_meta( $course_id, '_lpr_course_enrolled_require', true ) ); | |
| 353 | + | |
| 354 | + return apply_filters( 'learn_press_course_enroll_required', $required, $course_id ); | |
| 355 | +}*/ | |
| 356 | + | |
| 357 | +function learn_press_get_all_courses( $args = array() ) { | |
| 358 | + $term = ''; | |
| 359 | + $exclude = ''; | |
| 360 | + is_array( $args ) && extract( $args ); | |
| 361 | + $args = array( | |
| 362 | + 'post_type' => array( 'lp_course' ), | |
| 363 | + 'post_status' => 'publish', | |
| 364 | + 'posts_per_page' => - 1, | |
| 365 | + 's' => $term, | |
| 366 | + 'fields' => 'ids', | |
| 367 | + 'exclude' => $exclude, | |
| 368 | + ); | |
| 369 | + $args = apply_filters( 'learn_press_get_courses_args', $args ); | |
| 370 | + $posts = get_posts( $args ); | |
| 371 | + | |
| 372 | + return apply_filters( 'learn_press_get_courses', $posts, $args ); | |
| 373 | +} | |
| 374 | + | |
| 375 | +/** | |
| 376 | + * @deprecated 4.2.7.4 | |
| 377 | + */ | |
| 378 | +/*function learn_press_search_post_excerpt( $where = '' ) { | |
| 379 | + global $wp_the_query, $wpdb; | |
| 380 | + | |
| 381 | + if ( empty( $wp_the_query->query_vars['s'] ) ) { | |
| 382 | + return $where; | |
| 383 | + } | |
| 384 | + | |
| 385 | + $where = preg_replace( | |
| 386 | + "/post_title\s+LIKE\s*(\'\%[^\%]+\%\')/", | |
| 387 | + "post_title LIKE $1) OR ({$wpdb->posts}.post_excerpt LIKE $1", | |
| 388 | + $where | |
| 389 | + ); | |
| 390 | + | |
| 391 | + return $where; | |
| 392 | +}*/ | |
| 393 | + | |
| 394 | +// add_filter( 'posts_where', 'learn_press_search_post_excerpt' ); | |
| 395 | + | |
| 396 | +function learn_press_get_course_user( $course_id = null ) { | |
| 397 | + if ( ! $course_id ) { | |
| 398 | + $course_id = get_the_ID(); | |
| 399 | + } | |
| 400 | + | |
| 401 | + return learn_press_get_user( get_post_field( 'post_author', $course_id ) ); | |
| 402 | +} | |
| 403 | + | |
| 404 | +/** | |
| 405 | + * Get item types support in course curriculum. | |
| 406 | + * | |
| 407 | + * @return array | |
| 408 | + */ | |
| 409 | +function learn_press_course_get_support_item_types() { | |
| 410 | + $types = [ | |
| 411 | + LP_LESSON_CPT => __( 'Lesson', 'learnpress' ), | |
| 412 | + LP_QUIZ_CPT => __( 'Quiz', 'learnpress' ), | |
| 413 | + ]; | |
| 414 | + | |
| 415 | + return apply_filters( 'learn-press/course-support-items', $types, false ); | |
| 416 | +} | |
| 417 | + | |
| 418 | +/** | |
| 419 | + * Register new type of course item | |
| 420 | + * | |
| 421 | + * @param string $post_type - Usually is post type | |
| 422 | + * @param string $label - Name show for user | |
| 423 | + * @deprecated 4.2.7.4 | |
| 424 | + */ | |
| 425 | +/*function learn_press_course_add_support_item_type( $post_type, $label = '' ) { | |
| 426 | + if ( empty( $GLOBALS['learn_press_course_support_item_types'] ) ) { | |
| 427 | + $GLOBALS['learn_press_course_support_item_types'] = array(); | |
| 428 | + } | |
| 429 | + if ( func_num_args() == 1 && is_array( func_get_arg( 0 ) ) ) { | |
| 430 | + foreach ( func_get_arg( 0 ) as $type => $label ) { | |
| 431 | + learn_press_course_add_support_item_type( $type, $label ); | |
| 432 | + } | |
| 433 | + } elseif ( func_num_args() == 2 ) { | |
| 434 | + $GLOBALS['learn_press_course_support_item_types'][ func_get_arg( 0 ) ] = func_get_arg( 1 ); | |
| 435 | + } | |
| 436 | +}*/ | |
| 437 | + | |
| 438 | +/** | |
| 439 | + * Check if course is support an item's type. | |
| 440 | + * | |
| 441 | + * @param string $type | |
| 442 | + * | |
| 443 | + * @return bool | |
| 444 | + */ | |
| 445 | +function learn_press_is_support_course_item_type( $type ) { | |
| 446 | + $types = learn_press_course_get_support_item_types(); | |
| 447 | + | |
| 448 | + if ( is_array( $type ) ) { | |
| 449 | + $support = true; | |
| 450 | + foreach ( $type as $t ) { | |
| 451 | + $support = $support && learn_press_is_support_course_item_type( $t ); | |
| 452 | + } | |
| 453 | + } else { | |
| 454 | + $support = is_string( $type ) && $type && ! empty( $types[ $type ] ); | |
| 455 | + } | |
| 456 | + | |
| 457 | + return $support; | |
| 458 | +} | |
| 459 | + | |
| 460 | +/** | |
| 461 | + * @deprecated 4.2.7.4 | |
| 462 | + */ | |
| 463 | +/*learn_press_course_add_support_item_type( | |
| 464 | + array( | |
| 465 | + 'lp_lesson' => __( 'Lesson', 'learnpress' ), | |
| 466 | + 'lp_quiz' => __( 'Quiz', 'learnpress' ), | |
| 467 | + ) | |
| 468 | +);*/ | |
| 469 | + | |
| 470 | +function learn_press_add_course_item_feature( $type, $feature ) { | |
| 471 | + $features = array(); | |
| 472 | + if ( ! empty( $GLOBALS['learn_press_course_item_features'] ) ) { | |
| 473 | + $features = $GLOBALS['learn_press_course_item_features']; | |
| 474 | + } | |
| 475 | + | |
| 476 | + if ( empty( $features[ $type ] ) ) { | |
| 477 | + $features[ $type ] = array(); | |
| 478 | + } | |
| 479 | + | |
| 480 | + if ( array_search( $feature, $features ) === false ) { | |
| 481 | + $features[ $type ] = $feature; | |
| 482 | + } | |
| 483 | + | |
| 484 | + $GLOBALS['learn_press_course_item_features'] = $features; | |
| 485 | +} | |
| 486 | + | |
| 487 | + | |
| 488 | +function learn_press_get_course_id() { | |
| 489 | + $course_id = 0; | |
| 490 | + if ( learn_press_is_course() ) { | |
| 491 | + $course_id = get_the_ID(); | |
| 492 | + } | |
| 493 | + | |
| 494 | + return absint( $course_id ); | |
| 495 | +} | |
| 496 | + | |
| 497 | +/** | |
| 498 | + * Get the permalink of a course | |
| 499 | + * | |
| 500 | + * @param int $course_id | |
| 501 | + * | |
| 502 | + * @return string | |
| 503 | + * @since 3.0.0 | |
| 504 | + * | |
| 505 | + */ | |
| 506 | +function learn_press_get_course_permalink( $course_id = 0 ) { | |
| 507 | + if ( $course = learn_press_get_course( $course_id ) ) { | |
| 508 | + return $course->get_permalink(); | |
| 509 | + } | |
| 510 | + | |
| 511 | + return false; | |
| 512 | +} | |
| 513 | + | |
| 514 | + | |
| 515 | +/** | |
| 516 | + * Get the permalink of a item in a course | |
| 517 | + * | |
| 518 | + * @param int $course_id | |
| 519 | + * @param int $item_id | |
| 520 | + * | |
| 521 | + * @return string | |
| 522 | + * @since 3.0.0 | |
| 523 | + * Addon Assignment 4.1.1 still use | |
| 524 | + */ | |
| 525 | +function learn_press_get_course_item_permalink( int $course_id = 0, int $item_id = 0 ): string { | |
| 526 | + $course = learn_press_get_course( $course_id ); | |
| 527 | + if ( $course ) { | |
| 528 | + return $course->get_item_link( $item_id ); | |
| 529 | + } | |
| 530 | + | |
| 531 | + return ''; | |
| 532 | +} | |
| 533 | + | |
| 534 | +/** | |
| 535 | + * @deprecated 4.2.2 | |
| 536 | + */ | |
| 537 | +function learn_press_get_the_course() { | |
| 538 | + _deprecated_function( __FUNCTION__, '4.2.2', 'learn_press_get_course' ); | |
| 539 | + return learn_press_get_course(); | |
| 540 | +} | |
| 541 | + | |
| 542 | +/** | |
| 543 | + * @deprecated 4.2.7.4 | |
| 544 | + */ | |
| 545 | +/*function learn_press_get_user_question_answer( $args = '' ) { | |
| 546 | + $args = wp_parse_args( | |
| 547 | + $args, | |
| 548 | + array( | |
| 549 | + 'question_id' => 0, | |
| 550 | + 'history_id' => 0, | |
| 551 | + 'quiz_id' => 0, | |
| 552 | + 'course_id' => 0, | |
| 553 | + 'user_id' => get_current_user_id(), | |
| 554 | + ) | |
| 555 | + ); | |
| 556 | + $answered = null; | |
| 557 | + if ( $args['history_id'] ) { | |
| 558 | + $user_meta = learn_press_get_user_item_meta( $args['history_id'], 'question_answers', true ); | |
| 559 | + if ( $user_meta && array_key_exists( $args['question_id'], $user_meta ) ) { | |
| 560 | + $answered = $user_meta[ $args['question_id'] ]; | |
| 561 | + } | |
| 562 | + } elseif ( $args['quiz_id'] && $args['course_id'] ) { | |
| 563 | + $user = learn_press_get_user( $args['user_id'] ); | |
| 564 | + $history = $user->get_quiz_results( $args['quiz_id'], $args['course_id'] ); | |
| 565 | + if ( $history ) { | |
| 566 | + $user_meta = learn_press_get_user_item_meta( $history->history_id, 'question_answers', true ); | |
| 567 | + if ( $user_meta && array_key_exists( $args['question_id'], $user_meta ) ) { | |
| 568 | + $answered = $user_meta[ $args['question_id'] ]; | |
| 569 | + } | |
| 570 | + } | |
| 571 | + } | |
| 572 | + | |
| 573 | + return $answered; | |
| 574 | +}*/ | |
| 575 | + | |
| 576 | +/** | |
| 577 | + * @deprecated 4.2.7.4 | |
| 578 | + */ | |
| 579 | +/*function need_to_updating() { | |
| 580 | + ob_start(); | |
| 581 | + learn_press_display_message( 'This function need to updating' ); | |
| 582 | + | |
| 583 | + return ob_get_clean(); | |
| 584 | +}*/ | |
| 585 | + | |
| 586 | +/** | |
| 587 | + * filter section item single course | |
| 588 | + * @deprecated 4.2.7.4 | |
| 589 | + */ | |
| 590 | +/*function learn_press_get_course_sections() { | |
| 591 | + return apply_filters( | |
| 592 | + 'learn_press_get_course_sections', | |
| 593 | + array( | |
| 594 | + 'lp_lesson', | |
| 595 | + 'lp_quiz', | |
| 596 | + ) | |
| 597 | + ); | |
| 598 | +}*/ | |
| 599 | + | |
| 600 | +/** | |
| 601 | + * @deprecated 4.2.7.4 | |
| 602 | + */ | |
| 603 | +/*function lean_press_get_course_sections() { | |
| 604 | + _deprecated_function( __FUNCTION__, '2.1', 'learn_press_get_course_sections' ); | |
| 605 | + | |
| 606 | + return learn_press_get_course_sections(); | |
| 607 | +}*/ | |
| 608 | + | |
| 609 | +/** | |
| 610 | + * @deprecated 4.2.7.4 | |
| 611 | + */ | |
| 612 | +/*if ( ! function_exists( 'learn_press_get_course_item_url' ) ) { | |
| 613 | + function learn_press_get_course_item_url( $course_id = null, $item_id = null ) { | |
| 614 | + $course = learn_press_get_course( $course_id ); | |
| 615 | + | |
| 616 | + return $course ? $course->get_item_link( $item_id ) : false; | |
| 617 | + } | |
| 618 | +}*/ | |
| 619 | + | |
| 620 | +/** | |
| 621 | + * Add filter to WP comment form of lesson or quiz to output ID of current course. | |
| 622 | + * | |
| 623 | + * @param $post_id | |
| 624 | + * | |
| 625 | + * @since 3.0.10 | |
| 626 | + * | |
| 627 | + */ | |
| 628 | +function learn_press_comment_post_item_course( $post_id ) { | |
| 629 | + $course = learn_press_get_course(); | |
| 630 | + if ( ! $course ) { | |
| 631 | + return; | |
| 632 | + } | |
| 633 | + | |
| 634 | + echo sprintf( '<input type="hidden" name="comment-post-item-course" value="%d" />', $course->get_id() ); | |
| 635 | +} | |
| 636 | + | |
| 637 | +add_action( 'comment_form', 'learn_press_comment_post_item_course' ); | |
| 638 | + | |
| 639 | +function learn_press_item_comment_link( $link, $comment, $args, $cpage ) { | |
| 640 | + | |
| 641 | + $comment_post_ID = $comment->comment_post_ID; | |
| 642 | + | |
| 643 | + /** | |
| 644 | + * Validate if comment post is an item of course | |
| 645 | + */ | |
| 646 | + if ( ! learn_press_is_support_course_item_type( learn_press_get_post_type( $comment_post_ID ) ) ) { | |
| 647 | + return $link; | |
| 648 | + } | |
| 649 | + | |
| 650 | + $post_id = 0; | |
| 651 | + | |
| 652 | + /** | |
| 653 | + * Ensure there is a course | |
| 654 | + */ | |
| 655 | + if ( empty( $_POST['comment-post-item-course'] ) ) { | |
| 656 | + $course = learn_press_get_course(); | |
| 657 | + if ( $course ) { | |
| 658 | + $post_id = $course->get_id(); | |
| 659 | + } | |
| 660 | + } else { | |
| 661 | + $post_id = absint( $_POST['comment-post-item-course'] ); | |
| 662 | + } | |
| 663 | + | |
| 664 | + $course = learn_press_get_course( $post_id ); | |
| 665 | + if ( $course ) { | |
| 666 | + $link = str_replace( get_the_permalink( $comment_post_ID ), $course->get_item_link( $comment_post_ID ), $link ); | |
| 667 | + } | |
| 668 | + | |
| 669 | + return $link; | |
| 670 | +} | |
| 671 | + | |
| 672 | +add_filter( 'get_comment_link', 'learn_press_item_comment_link', 100, 4 ); | |
| 673 | + | |
| 674 | +/** | |
| 675 | + * Fix redirection invalid when SG Cache is installed | |
| 676 | + * | |
| 677 | + * @param int $comment_id | |
| 678 | + * @param string $status | |
| 679 | + * | |
| 680 | + * @since 3.0.10 | |
| 681 | + * @deprecated 4.1.6.9 | |
| 682 | + * | |
| 683 | + */ | |
| 684 | +/*function learn_press_force_refresh_course( $comment_id, $status ) { | |
| 685 | + | |
| 686 | + if ( empty( $_POST['comment-post-item-course'] ) ) { | |
| 687 | + return; | |
| 688 | + } | |
| 689 | + | |
| 690 | + $course_id = absint( $_POST['comment-post-item-course'] ); | |
| 691 | + $course = learn_press_get_course( $course_id ); | |
| 692 | + $curd = new LP_Course_CURD(); | |
| 693 | + $curd->load( $course ); | |
| 694 | +} | |
| 695 | + | |
| 696 | +add_action( 'comment_post', 'learn_press_force_refresh_course', 1000, 2 );*/ | |
| 697 | + | |
| 698 | +/** | |
| 699 | + * @deprecated 4.1.6.9 | |
| 700 | + */ | |
| 701 | +/*if ( ! function_exists( 'learn_press_get_nav_course_item_url' ) ) { | |
| 702 | + function learn_press_get_nav_course_item_url( $course_id = null, $item_id = null, $content_only = false ) { | |
| 703 | + | |
| 704 | + $course = learn_press_get_course( $course_id ); | |
| 705 | + $curriculum_items = $course->get_items();// LP_Helper::maybe_unserialize( $course->post->curriculum_items ); | |
| 706 | + $index = array_search( $item_id, $curriculum_items ); | |
| 707 | + $return = array( | |
| 708 | + 'back' => '', | |
| 709 | + 'next' => '', | |
| 710 | + ); | |
| 711 | + if ( is_array( $curriculum_items ) ) { | |
| 712 | + if ( array_key_exists( $index - 1, $curriculum_items ) ) { | |
| 713 | + $back_item = get_post( $curriculum_items[ $index - 1 ] ); | |
| 714 | + $return['back'] = array( | |
| 715 | + 'id' => $back_item->ID, | |
| 716 | + 'link' => $course->get_item_link( $curriculum_items[ $index - 1 ] ), | |
| 717 | + 'title' => $back_item->post_title, | |
| 718 | + ); | |
| 719 | + if ( $content_only ) { | |
| 720 | + $return['back']['link'] .= '?content-item-only=yes'; | |
| 721 | + } | |
| 722 | + } | |
| 723 | + if ( array_key_exists( $index + 1, $curriculum_items ) ) { | |
| 724 | + $next_item = get_post( $curriculum_items[ $index + 1 ] ); | |
| 725 | + $return['next'] = array( | |
| 726 | + 'id' => $next_item->ID, | |
| 727 | + 'link' => $course->get_item_link( $curriculum_items[ $index + 1 ] ), | |
| 728 | + 'title' => $next_item->post_title, | |
| 729 | + ); | |
| 730 | + if ( $content_only ) { | |
| 731 | + $return['next']['link'] .= '?content-item-only=yes'; | |
| 732 | + } | |
| 733 | + } | |
| 734 | + } | |
| 735 | + | |
| 736 | + return $return; | |
| 737 | + } | |
| 738 | +}*/ | |
| 739 | + | |
| 740 | +/** | |
| 741 | + * Get course id of an item by id | |
| 742 | + * | |
| 743 | + * @deprecated 4.1.6.9 | |
| 744 | + */ | |
| 745 | + | |
| 746 | +if ( ! function_exists( 'learn_press_get_item_course_id' ) ) { | |
| 747 | + | |
| 748 | + function learn_press_get_item_course_id( $post_id, $post_type ) { | |
| 749 | + _deprecated_function( __FUNCTION__, '4.1.6.9' ); | |
| 750 | + /*global $wpdb; | |
| 751 | + | |
| 752 | + // If the post is a course | |
| 753 | + if ( LP_COURSE_CPT == learn_press_get_post_type( $post_id ) ) { | |
| 754 | + return false; | |
| 755 | + } | |
| 756 | + | |
| 757 | + if ( ! $post_types = learn_press_course_get_support_item_types( true ) ) { | |
| 758 | + return false; | |
| 759 | + } | |
| 760 | + | |
| 761 | + if ( ! in_array( learn_press_get_post_type( $post_id ), $post_types ) ) { | |
| 762 | + return false; | |
| 763 | + } | |
| 764 | + | |
| 765 | + $course_id = false; | |
| 766 | + | |
| 767 | + if ( false !== ( $courses = LP_Object_Cache::get( 'item-course-ids', 'learn-press' ) ) ) { | |
| 768 | + | |
| 769 | + foreach ( $courses as $course_id => $items ) { | |
| 770 | + if ( in_array( $post_id, $items ) ) { | |
| 771 | + break; | |
| 772 | + } | |
| 773 | + $course_id = false; | |
| 774 | + } | |
| 775 | + } else { | |
| 776 | + $courses = array(); | |
| 777 | + } | |
| 778 | + | |
| 779 | + if ( false === $course_id ) { | |
| 780 | + $query = $wpdb->prepare( | |
| 781 | + " | |
| 782 | + SELECT section.section_course_id | |
| 783 | + FROM {$wpdb->learnpress_sections} AS section | |
| 784 | + INNER JOIN {$wpdb->learnpress_section_items} AS item | |
| 785 | + ON item.section_id = section.section_id | |
| 786 | + WHERE item.item_id = %d | |
| 787 | + LIMIT 1 | |
| 788 | + ", | |
| 789 | + $post_id | |
| 790 | + ); | |
| 791 | + | |
| 792 | + $course_id = apply_filters( 'learn-press/item-course-id', absint( $wpdb->get_var( $query ) ), $post_id ); | |
| 793 | + | |
| 794 | + if ( $course = learn_press_get_course( $course_id ) ) { | |
| 795 | + $courses[ $course_id ] = $course->get_items(); | |
| 796 | + } | |
| 797 | + | |
| 798 | + if ( empty( $courses[ $course_id ] ) ) { | |
| 799 | + $courses[ $course_id ] = array(); | |
| 800 | + } | |
| 801 | + | |
| 802 | + if ( ! in_array( $post_id, $courses[ $course_id ] ) ) { | |
| 803 | + $courses[ $course_id ][] = $post_id; | |
| 804 | + } | |
| 805 | + LP_Object_Cache::set( 'item-course-ids', $courses, 'learn-press' ); | |
| 806 | + | |
| 807 | + } | |
| 808 | + | |
| 809 | + return $course_id;*/ | |
| 810 | + } | |
| 811 | +} | |
| 812 | + | |
| 813 | +/** | |
| 814 | + * @editor tungnx | comment code | |
| 815 | + * @deprecated 3.2.7.5 | |
| 816 | + */ | |
| 817 | +/*function learn_press_item_sample_permalink_html( $return, $post_id, $new_title, $new_slug, $post ) { | |
| 818 | + remove_filter( 'get_sample_permalink_html', 'learn_press_item_sample_permalink_html', 10 ); | |
| 819 | + | |
| 820 | + $return = sprintf( | |
| 821 | + '<a class="button" href="%s" target="_blank">%s</a>', | |
| 822 | + learn_press_get_preview_url( $post_id ), | |
| 823 | + __( 'Preview', 'learnpress' ) | |
| 824 | + ); | |
| 825 | + | |
| 826 | + $return .= '<span>' . __( 'Permalink only available if the item is already assigned to a course.', 'learnpress' ) . '</span>'; | |
| 827 | + | |
| 828 | + return sprintf( '<div id="learn-press-box-edit-slug">%s</div>', $return ); | |
| 829 | +}*/ | |
| 830 | + | |
| 831 | +/** | |
| 832 | + * @editor tungnx | comment code | |
| 833 | + * @deprecated 3.2.7.5 | |
| 834 | + */ | |
| 835 | +/*if ( ! function_exists( 'learn_press_item_sample_permalink' ) ) { | |
| 836 | + | |
| 837 | + function learn_press_item_sample_permalink( $permalink, $post_id, $title, $name, $post ) { | |
| 838 | + if ( ! in_array( $post->post_type, learn_press_course_get_support_item_types( true ) ) ) { | |
| 839 | + return $permalink; | |
| 840 | + } | |
| 841 | + | |
| 842 | + $permalink[0] = str_replace( $post->post_name, '%pagename%', $permalink[0] ); | |
| 843 | + | |
| 844 | + if ( ! preg_match( '~^https?://~', $permalink[0] ) ) { | |
| 845 | + add_filter( 'get_sample_permalink_html', 'learn_press_item_sample_permalink_html', 10, 5 ); | |
| 846 | + } | |
| 847 | + | |
| 848 | + return $permalink; | |
| 849 | + } | |
| 850 | + | |
| 851 | +}*/ | |
| 852 | +//add_filter( 'get_sample_permalink', 'learn_press_item_sample_permalink', 10, 5 ); | |
| 853 | + | |
| 854 | +/** | |
| 855 | + * Get preview url for LP post type. | |
| 856 | + * | |
| 857 | + * @param int $post_id | |
| 858 | + * | |
| 859 | + * @return string | |
| 860 | + * @since 3.0.0 | |
| 861 | + * | |
| 862 | + */ | |
| 863 | +function learn_press_get_preview_url( $post_id ) { | |
| 864 | + return esc_url_raw( | |
| 865 | + add_query_arg( | |
| 866 | + array( | |
| 867 | + 'lp-preview' => $post_id, | |
| 868 | + '_wpnonce' => wp_create_nonce( 'lp-preview' ), | |
| 869 | + ), | |
| 870 | + trailingslashit( get_home_url() ) | |
| 871 | + ) | |
| 872 | + ); | |
| 873 | +} | |
| 874 | + | |
| 875 | +if ( ! function_exists( 'learn_press_course_item_type_link' ) ) { | |
| 876 | + /** | |
| 877 | + * Add filter to WP custom post-type-link to edit the link of item | |
| 878 | + * with the link of it's course. | |
| 879 | + * | |
| 880 | + * @updated 12 Nov 2018 | |
| 881 | + * | |
| 882 | + * @param string $post_link | |
| 883 | + * @param WP_Post $post | |
| 884 | + * @param bool $leavename | |
| 885 | + * @param bool $sample | |
| 886 | + * | |
| 887 | + * @editor tungnx | comment code | |
| 888 | + * @return string | |
| 889 | + * @deprecated 3.2.7.4 | |
| 890 | + */ | |
| 891 | + /*function learn_press_course_item_type_link( $post_link, $post, $leavename, $sample ) { | |
| 892 | + | |
| 893 | + remove_filter( 'post_type_link', 'learn_press_course_item_type_link', 10 ); | |
| 894 | + | |
| 895 | + $course = learn_press_get_course(); | |
| 896 | + | |
| 897 | + if ( ! $course && ( $course_id = learn_press_get_item_course( $post->ID ) ) ) { | |
| 898 | + $course = learn_press_get_course( $course_id ); | |
| 899 | + } | |
| 900 | + | |
| 901 | + if ( learn_press_is_support_course_item_type( $post->post_type ) ) { | |
| 902 | + // Check elementor installed and activated | |
| 903 | + if ( did_action( 'elementor/loaded' ) ) { | |
| 904 | + // do stuff for edit mode | |
| 905 | + if ( ! Elementor\Plugin::$instance->editor->is_edit_mode() ) { | |
| 906 | + if ( $course ) { | |
| 907 | + $post_link = $course->get_item_link( $post->ID ); | |
| 908 | + } else { | |
| 909 | + $post_link = learn_press_get_sample_link_course_item_url( $post->ID ); | |
| 910 | + } | |
| 911 | + } | |
| 912 | + } else { | |
| 913 | + if ( $course ) { | |
| 914 | + $post_link = $course->get_item_link( $post->ID ); | |
| 915 | + } else { | |
| 916 | + $post_link = learn_press_get_sample_link_course_item_url( $post->ID ); | |
| 917 | + } | |
| 918 | + } | |
| 919 | + } | |
| 920 | + | |
| 921 | + add_filter( 'post_type_link', 'learn_press_course_item_type_link', 10, 4 ); | |
| 922 | + | |
| 923 | + return $post_link; | |
| 924 | + }*/ | |
| 925 | +} | |
| 926 | +//add_filter( 'post_type_link', 'learn_press_course_item_type_link', 10, 4 ); | |
| 927 | + | |
| 928 | +/** | |
| 929 | + * Get grade course type can translate | |
| 930 | + * | |
| 931 | + * @param string $grade | |
| 932 | + * @param bool $echo | |
| 933 | + * | |
| 934 | + * @return mixed|void | |
| 935 | + */ | |
| 936 | +function learn_press_course_grade_html( string $grade = '', bool $echo = true ) { | |
| 937 | + switch ( $grade ) { | |
| 938 | + case 'passed': | |
| 939 | + $html = __( 'Passed', 'learnpress' ); | |
| 940 | + break; | |
| 941 | + case 'failed': | |
| 942 | + $html = __( 'Failed', 'learnpress' ); | |
| 943 | + break; | |
| 944 | + case 'in-progress': | |
| 945 | + $html = __( 'In Progress', 'learnpress' ); | |
| 946 | + break; | |
| 947 | + default: | |
| 948 | + $html = $grade; | |
| 949 | + break; | |
| 950 | + } | |
| 951 | + | |
| 952 | + // @since 3.0.0 | |
| 953 | + $html = apply_filters( 'learn-press/course/grade-html', $html, $grade ); | |
| 954 | + | |
| 955 | + if ( $echo ) { | |
| 956 | + echo wp_kses_post( $html ); | |
| 957 | + } | |
| 958 | + | |
| 959 | + return $html; | |
| 960 | +} | |
| 961 | + | |
| 962 | +/** | |
| 963 | + * @deprecated 4.2.7.4 | |
| 964 | + */ | |
| 965 | +/*function learn_press_get_course_results_tooltip( $course_id ) { | |
| 966 | + $metabox = LP_Course_Post_Type::assessment_meta_box(); | |
| 967 | + $options = $metabox['fields'][0]['options']; | |
| 968 | + $cr = get_post_meta( $course_id, '_lp_course_result', true ); | |
| 969 | + $tooltip = ! empty( $options[ $cr ] ) ? $options[ $cr ] : false; | |
| 970 | + if ( $tooltip ) { | |
| 971 | + if ( preg_match_all( '~<p.*>(.*)<\/p>~im', $tooltip, $matches ) ) { | |
| 972 | + $tooltip = $matches[1][0]; | |
| 973 | + } | |
| 974 | + } | |
| 975 | + | |
| 976 | + return $tooltip; | |
| 977 | +}*/ | |
| 978 | + | |
| 979 | +function learn_press_course_passing_condition( $value, $format, $course_id ) { | |
| 980 | + $course = learn_press_get_course( $course_id ); | |
| 981 | + $quiz_id = $course->get_final_quiz(); | |
| 982 | + $evalution = get_post_meta( $course_id, '_lp_course_result', true ); | |
| 983 | + | |
| 984 | + if ( $quiz_id && $evalution === 'evaluate_final_quiz' ) { | |
| 985 | + $quiz = learn_press_get_quiz( $quiz_id ); | |
| 986 | + $value = absint( $quiz->get_passing_grade() ); | |
| 987 | + | |
| 988 | + if ( $format ) { | |
| 989 | + $value = "{$value}%"; | |
| 990 | + } | |
| 991 | + } | |
| 992 | + | |
| 993 | + return $value; | |
| 994 | +} | |
| 995 | + | |
| 996 | +add_filter( 'learn-press/course-passing-condition', 'learn_press_course_passing_condition', 10, 3 ); | |
| 997 | + | |
| 998 | +/** | |
| 999 | + * @deprecated 4.2.7.4 | |
| 1000 | + */ | |
| 1001 | +function learn_press_remove_query_var_enrolled_course( $redirect ) { | |
| 1002 | + return esc_url_raw( remove_query_arg( 'enroll-course', $redirect ) ); | |
| 1003 | +} | |
| 1004 | + | |
| 1005 | +//add_filter( 'learn-press/enroll-course-redirect', 'learn_press_remove_query_var_enrolled_course' ); | |
| 1006 | + | |
| 1007 | +/** | |
| 1008 | + * Mark the user to know if they have just logged in | |
| 1009 | + * for some purpose. | |
| 1010 | + * | |
| 1011 | + * @since 3.0.0 | |
| 1012 | + */ | |
| 1013 | +/*function learn_press_mark_user_just_logged_in() { | |
| 1014 | + LearnPress::instance()->session->set( 'user_just_logged_in', 'yes' ); | |
| 1015 | +} | |
| 1016 | +add_action( 'wp_login', 'learn_press_mark_user_just_logged_in' );*/ | |
| 1017 | + | |
| 1018 | +function learn_press_translate_course_result_required( $course ) { | |
| 1019 | + if ( ! $course ) { | |
| 1020 | + return ''; | |
| 1021 | + } | |
| 1022 | + | |
| 1023 | + $passing_condition = $course->get_passing_condition(); | |
| 1024 | + | |
| 1025 | + $evaluate_type = $course->get_data( 'course_result', 'evaluate_lesson' ); | |
| 1026 | + switch ( $evaluate_type ) { | |
| 1027 | + case 'evaluate_lesson': | |
| 1028 | + $label = esc_html__( 'completed lessons per the total number of lessons.', 'learnpress' ); | |
| 1029 | + break; | |
| 1030 | + case 'evaluate_quiz': | |
| 1031 | + $label = esc_html__( 'passed quizzes per the total number of quizzes.', 'learnpress' ); | |
| 1032 | + break; | |
| 1033 | + case 'evaluate_final_quiz': | |
| 1034 | + $label = esc_html__( 'Final Quiz', 'learnpress' ); | |
| 1035 | + break; | |
| 1036 | + case 'evaluate_questions': | |
| 1037 | + $label = esc_html__( 'correct answers per the total number of questions.', 'learnpress' ); | |
| 1038 | + break; | |
| 1039 | + case 'evaluate_mark': | |
| 1040 | + $label = esc_html__( 'score achieved per the total score of the questions.', 'learnpress' ); | |
| 1041 | + break; | |
| 1042 | + default: | |
| 1043 | + $label = apply_filters( 'learnpress/message/evaluate/' . $evaluate_type, $evaluate_type ); | |
| 1044 | + break; | |
| 1045 | + } | |
| 1046 | + | |
| 1047 | + return apply_filters( | |
| 1048 | + 'learnpress/message/evaluate', | |
| 1049 | + wp_sprintf( '%1$s %2$s %3$s', __( 'Require', 'learnpress' ), $passing_condition . '%', $label ) | |
| 1050 | + ); | |
| 1051 | +} | |