theme-compatibility
5 years ago
tutor-general-functions.php
5 years ago
tutor-template-functions.php
5 years ago
tutor-template-hook.php
5 years ago
tutor-general-functions.php
579 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) |
| 3 | exit; |
| 4 | |
| 5 | /** |
| 6 | * Tutor general Functions |
| 7 | */ |
| 8 | |
| 9 | if ( ! function_exists('tutor_withdrawal_methods')){ |
| 10 | function tutor_withdrawal_methods(){ |
| 11 | $withdraw = new \TUTOR\Withdraw(); |
| 12 | |
| 13 | return $withdraw->available_withdraw_methods; |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * @return array |
| 19 | * |
| 20 | * Get all Withdraw Methods available on this system |
| 21 | * |
| 22 | * @since v.1.5.7 |
| 23 | */ |
| 24 | |
| 25 | if ( ! function_exists('get_tutor_all_withdrawal_methods')){ |
| 26 | function get_tutor_all_withdrawal_methods(){ |
| 27 | $withdraw = new \TUTOR\Withdraw(); |
| 28 | |
| 29 | return $withdraw->withdraw_methods; |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | if ( ! function_exists('tutor_placeholder_img_src')) { |
| 34 | function tutor_placeholder_img_src() { |
| 35 | $src = tutor()->url . 'assets/images/placeholder.jpg'; |
| 36 | return apply_filters( 'tutor_placeholder_img_src', $src ); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * @return string |
| 42 | * |
| 43 | * Get course categories selecting UI |
| 44 | * |
| 45 | * @since v.1.3.4 |
| 46 | */ |
| 47 | |
| 48 | if ( ! function_exists('tutor_course_categories_dropdown')){ |
| 49 | function tutor_course_categories_dropdown($post_ID = 0, $args = array()){ |
| 50 | |
| 51 | $default = array( |
| 52 | 'classes' => '', |
| 53 | 'name' => 'tax_input[course-category]', |
| 54 | 'multiple' => true, |
| 55 | ); |
| 56 | |
| 57 | $args = apply_filters('tutor_course_categories_dropdown_args', array_merge($default, $args)); |
| 58 | |
| 59 | $multiple_select = ''; |
| 60 | |
| 61 | if (tutor_utils()->array_get('multiple', $args)){ |
| 62 | if (isset($args['name'])){ |
| 63 | $args['name'] = $args['name'].'[]'; |
| 64 | } |
| 65 | $multiple_select = "multiple='multiple'"; |
| 66 | } |
| 67 | |
| 68 | extract($args); |
| 69 | |
| 70 | $classes = (array) $classes; |
| 71 | $classes = implode(' ', $classes); |
| 72 | |
| 73 | $categories = tutor_utils()->get_course_categories(); |
| 74 | |
| 75 | $output = ''; |
| 76 | $output .= "<select name='{$name}' {$multiple_select} class='{$classes}' data-placeholder='". __('Search Course Category. ex. Design, Development, Business', 'tutor') ."'>"; |
| 77 | $output .= "<option value=''>". __('Select a category', 'tutor') ."</option>"; |
| 78 | $output .= _generate_categories_dropdown_option($post_ID, $categories, $args); |
| 79 | $output .= "</select>"; |
| 80 | |
| 81 | return $output; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * @param $categories |
| 87 | * @param string $parent_name |
| 88 | * |
| 89 | * @return string |
| 90 | * |
| 91 | * Get selecting options, recursive supports |
| 92 | * |
| 93 | * @since v.1.3.4 |
| 94 | */ |
| 95 | |
| 96 | if ( ! function_exists('_generate_categories_dropdown_option')){ |
| 97 | function _generate_categories_dropdown_option($post_ID = 0, $categories, $args = array(), $depth = 0){ |
| 98 | $output = ''; |
| 99 | |
| 100 | if (tutor_utils()->count($categories)) { |
| 101 | foreach ( $categories as $category_id => $category ) { |
| 102 | if ( ! $category->parent){ |
| 103 | $depth = 0; |
| 104 | } |
| 105 | |
| 106 | $childrens = tutor_utils()->array_get( 'children', $category ); |
| 107 | $has_in_term = has_term( $category->term_id, 'course-category', $post_ID ); |
| 108 | |
| 109 | $depth_seperator = ''; |
| 110 | if ($depth){ |
| 111 | for ($depth_i = 0; $depth_i < $depth; $depth_i++){ |
| 112 | $depth_seperator.='-'; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | $output .= "<option value='{$category->term_id}' ".selected($has_in_term, true, false)." > {$depth_seperator} {$category->name}</option> "; |
| 117 | |
| 118 | if ( tutor_utils()->count( $childrens ) ) { |
| 119 | $depth++; |
| 120 | $output .= _generate_categories_dropdown_option($post_ID,$childrens, $args, $depth); |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | return $output; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * @param array $args |
| 130 | * |
| 131 | * @return string |
| 132 | * |
| 133 | * Generate course categories checkbox |
| 134 | * @since v.1.3.4 |
| 135 | */ |
| 136 | |
| 137 | if ( ! function_exists('tutor_course_categories_checkbox')){ |
| 138 | function tutor_course_categories_checkbox($post_ID = 0, $args = array()){ |
| 139 | $default = array( |
| 140 | 'name' => 'tax_input[course-category]', |
| 141 | ); |
| 142 | |
| 143 | $args = apply_filters('tutor_course_categories_checkbox_args', array_merge($default, $args)); |
| 144 | |
| 145 | if (isset($args['name'])){ |
| 146 | $args['name'] = $args['name'].'[]'; |
| 147 | } |
| 148 | |
| 149 | extract($args); |
| 150 | |
| 151 | $categories = tutor_utils()->get_course_categories(); |
| 152 | $output = ''; |
| 153 | $output .= __tutor_generate_categories_checkbox($post_ID, $categories, $args); |
| 154 | |
| 155 | return $output; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * @param $categories |
| 161 | * @param string $parent_name |
| 162 | * @param array $args |
| 163 | * |
| 164 | * @return string |
| 165 | * |
| 166 | * Internal function to generate course categories checkbox |
| 167 | * |
| 168 | * @since v.1.3.4 |
| 169 | */ |
| 170 | if ( ! function_exists('__tutor_generate_categories_checkbox')){ |
| 171 | function __tutor_generate_categories_checkbox($post_ID = 0, $categories, $args = array()){ |
| 172 | $output = ''; |
| 173 | $input_name = tutor_utils()->array_get('name', $args); |
| 174 | |
| 175 | if (tutor_utils()->count($categories)) { |
| 176 | $output .= "<ul class='tax-input-course-category'>"; |
| 177 | foreach ( $categories as $category_id => $category ) { |
| 178 | $childrens = tutor_utils()->array_get( 'children', $category ); |
| 179 | $has_in_term = has_term( $category->term_id, 'course-category', $post_ID ); |
| 180 | |
| 181 | $output .= "<li class='tax-input-course-category-item tax-input-course-category-item-{$category->term_id} '><label class='course-category-checkbox'> <input type='checkbox' name='{$input_name}' value='{$category->term_id}' ".checked($has_in_term, true, false)." /> <span>{$category->name}</span> </label>"; |
| 182 | |
| 183 | if ( tutor_utils()->count( $childrens ) ) { |
| 184 | $output .= __tutor_generate_categories_checkbox($post_ID,$childrens, $args); |
| 185 | } |
| 186 | $output .= " </li>"; |
| 187 | } |
| 188 | $output .= "</ul>"; |
| 189 | } |
| 190 | return $output; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * @param string $content |
| 196 | * @param string $title |
| 197 | * |
| 198 | * @return string |
| 199 | * |
| 200 | * Wrap course builder sections within div for frontend |
| 201 | * |
| 202 | * @since v.1.3.4 |
| 203 | */ |
| 204 | |
| 205 | if ( ! function_exists('course_builder_section_wrap')) { |
| 206 | function course_builder_section_wrap( $content = '', $title = '', $echo = true ) { |
| 207 | ob_start(); |
| 208 | ?> |
| 209 | <div class="tutor-course-builder-section"> |
| 210 | <div class="tutor-course-builder-section-title"> |
| 211 | <h3><i class="tutor-icon-down"></i> <span><?php echo $title; ?></span></h3> |
| 212 | </div> |
| 213 | <div class="tutor-course-builder-section-content"> |
| 214 | <?php echo $content; ?> |
| 215 | </div> |
| 216 | </div> |
| 217 | <?php |
| 218 | $html = ob_get_clean(); |
| 219 | |
| 220 | if ($echo){ |
| 221 | echo $html; |
| 222 | }else{ |
| 223 | return $html; |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | |
| 229 | if ( ! function_exists('get_tutor_header')){ |
| 230 | function get_tutor_header($fullScreen = false){ |
| 231 | $enable_spotlight_mode = tutor_utils()->get_option('enable_spotlight_mode'); |
| 232 | |
| 233 | if ($enable_spotlight_mode || $fullScreen){ |
| 234 | ?> |
| 235 | <!doctype html> |
| 236 | <html <?php language_attributes(); ?>> |
| 237 | <head> |
| 238 | <meta charset="<?php bloginfo( 'charset' ); ?>" /> |
| 239 | <meta name="viewport" content="width=device-width, initial-scale=1" /> |
| 240 | <link rel="profile" href="https://gmpg.org/xfn/11" /> |
| 241 | <?php wp_head(); ?> |
| 242 | </head> |
| 243 | <body <?php body_class(); ?>> |
| 244 | <div id="tutor-page-wrap" class="tutor-site-wrap site"> |
| 245 | <?php |
| 246 | }else{ |
| 247 | get_header(); |
| 248 | } |
| 249 | |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | if (! function_exists('get_tutor_footer')){ |
| 254 | function get_tutor_footer($fullScreen = false){ |
| 255 | $enable_spotlight_mode = tutor_utils()->get_option('enable_spotlight_mode'); |
| 256 | if ($enable_spotlight_mode || $fullScreen){ |
| 257 | ?> |
| 258 | </div> |
| 259 | <?php wp_footer(); ?> |
| 260 | |
| 261 | </body> |
| 262 | </html> |
| 263 | <?php |
| 264 | }else{ |
| 265 | get_footer(); |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * @param int $parent_id |
| 272 | * @param array $level_categories |
| 273 | * |
| 274 | * Generate Courses categories for Paid Memberships Pro |
| 275 | * |
| 276 | * @since v.1.3.6 |
| 277 | */ |
| 278 | if ( ! function_exists('generate_categories_for_pmpro')) { |
| 279 | function generate_categories_for_pmpro( $parent_id = 0, $level_categories = array() ) { |
| 280 | $args = array( |
| 281 | 'taxonomy' => 'course-category', |
| 282 | 'parent' => $parent_id, |
| 283 | 'hide_empty' => false, |
| 284 | ); |
| 285 | $cats = get_categories( apply_filters( 'course_categories_pmpro_args', $args ) ); |
| 286 | if ( $cats ) { |
| 287 | foreach ( $cats as $cat ) { |
| 288 | $name = 'membershipcategory_' . $cat->term_id; |
| 289 | if ( ! empty( $level_categories ) ) { |
| 290 | $checked = checked( in_array( $cat->term_id, $level_categories ), true, false ); |
| 291 | } else { |
| 292 | $checked = ''; |
| 293 | } |
| 294 | echo "<ul><li class=membershipcategory><input type=checkbox name={$name} id={$name} value=yes {$checked}><label for={$name}>{$cat->name}</label>"; |
| 295 | generate_categories_for_pmpro( $cat->term_id, $level_categories ); |
| 296 | echo '</li></ul>'; |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * @param null $key |
| 304 | * @param bool $default |
| 305 | * |
| 306 | * @return array|bool|mixed |
| 307 | * |
| 308 | * Get tutor option by this helper function |
| 309 | * |
| 310 | * @since v.1.3.6 |
| 311 | */ |
| 312 | if ( ! function_exists('get_tutor_option')){ |
| 313 | function get_tutor_option($key = null, $default = false){ |
| 314 | return tutils()->get_option($key, $default); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * @param null $key |
| 320 | * @param bool $value |
| 321 | * |
| 322 | * Update tutor option by this helper function |
| 323 | * |
| 324 | * @since v.1.3.6 |
| 325 | */ |
| 326 | if ( ! function_exists('update_tutor_option')){ |
| 327 | function update_tutor_option($key = null, $value = false){ |
| 328 | tutils()->update_option($key, $value); |
| 329 | } |
| 330 | } |
| 331 | /** |
| 332 | * @param int $course_id |
| 333 | * @param null $key |
| 334 | * @param bool $default |
| 335 | * |
| 336 | * @return array|bool|mixed |
| 337 | * |
| 338 | * Get tutor course settings by course ID |
| 339 | * |
| 340 | * @since v.1.4.1 |
| 341 | */ |
| 342 | if ( ! function_exists('get_tutor_course_settings')) { |
| 343 | function get_tutor_course_settings( $course_id = 0, $key = null, $default = false ) { |
| 344 | return tutils()->get_course_settings( $course_id, $key, $default ); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * @param int $lesson_id |
| 350 | * @param null $key |
| 351 | * @param bool $default |
| 352 | * |
| 353 | * @return array|bool|mixed |
| 354 | * |
| 355 | * Get lesson content drip settings |
| 356 | */ |
| 357 | |
| 358 | if ( ! function_exists('get_item_content_drip_settings')){ |
| 359 | function get_item_content_drip_settings($lesson_id = 0, $key = null, $default = false){ |
| 360 | return tutils()->get_item_content_drip_settings( $lesson_id, $key, $default ); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * @param null $msg |
| 366 | * @param string $type |
| 367 | * @param bool $echo |
| 368 | * |
| 369 | * @return string |
| 370 | * |
| 371 | * Print Alert by tutor_alert() |
| 372 | * |
| 373 | * @since v.1.4.1 |
| 374 | */ |
| 375 | if ( ! function_exists('tutor_alert')){ |
| 376 | function tutor_alert($msg = null, $type = 'warning', $echo = true){ |
| 377 | if ( ! $msg){ |
| 378 | |
| 379 | if ($type === 'any'){ |
| 380 | if ( ! $msg){ |
| 381 | $type = 'warning'; |
| 382 | $msg = tutor_flash_get($type); |
| 383 | } |
| 384 | if ( ! $msg){ |
| 385 | $type = 'danger'; |
| 386 | $msg = tutor_flash_get($type); |
| 387 | } |
| 388 | if ( ! $msg){ |
| 389 | $type = 'success'; |
| 390 | $msg = tutor_flash_get($type); |
| 391 | } |
| 392 | |
| 393 | }else{ |
| 394 | $msg = tutor_flash_get($type); |
| 395 | } |
| 396 | |
| 397 | |
| 398 | } |
| 399 | if ( ! $msg){ |
| 400 | return $msg; |
| 401 | } |
| 402 | |
| 403 | $html = "<div class='tutor-alert tutor-alert-{$type}'>{$msg}</div>"; |
| 404 | if ($echo){ |
| 405 | echo $html; |
| 406 | } |
| 407 | return $html; |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | |
| 412 | /** |
| 413 | * @param bool $echo |
| 414 | * |
| 415 | * Simply call tutor_nonce_field() to generate nonce field |
| 416 | * |
| 417 | * @since v.1.4.2 |
| 418 | */ |
| 419 | |
| 420 | if ( ! function_exists('tutor_nonce_field')) { |
| 421 | function tutor_nonce_field( $echo = true ) { |
| 422 | wp_nonce_field( tutor()->nonce_action, tutor()->nonce, $echo ); |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | /** |
| 427 | * @param null $key |
| 428 | * @param string $message |
| 429 | * |
| 430 | * Set Flash Message |
| 431 | */ |
| 432 | |
| 433 | if ( ! function_exists('tutor_flash_set')) { |
| 434 | function tutor_flash_set( $key = null, $message = '' ) { |
| 435 | if ( ! $key ) { |
| 436 | return; |
| 437 | } |
| 438 | // ensure session is started |
| 439 | if ( session_status() !== PHP_SESSION_ACTIVE ) { |
| 440 | session_start(); |
| 441 | } |
| 442 | $_SESSION[ $key ] = $message; |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | /** |
| 447 | * @param null $key |
| 448 | * |
| 449 | * @return array|bool|mixed|null |
| 450 | * |
| 451 | * @since v.1.4.2 |
| 452 | * |
| 453 | * Get flash message |
| 454 | */ |
| 455 | |
| 456 | if ( ! function_exists('tutor_flash_get')) { |
| 457 | function tutor_flash_get( $key = null ) { |
| 458 | if ( $key ) { |
| 459 | // ensure session is started |
| 460 | if ( session_status() !== PHP_SESSION_ACTIVE ) { |
| 461 | @session_start(); |
| 462 | } |
| 463 | if ( empty( $_SESSION ) ) { |
| 464 | return null; |
| 465 | } |
| 466 | $message = tutils()->array_get( $key, $_SESSION ); |
| 467 | if ( $message ) { |
| 468 | unset( $_SESSION[ $key ] ); |
| 469 | } |
| 470 | return $message; |
| 471 | } |
| 472 | return $key; |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | if ( ! function_exists('tutor_redirect_back')) { |
| 477 | /** |
| 478 | * @param null $url |
| 479 | * |
| 480 | * Redirect to back or a specific URL and terminate |
| 481 | * |
| 482 | * @since v.1.4.3 |
| 483 | */ |
| 484 | function tutor_redirect_back( $url = null ) { |
| 485 | if ( ! $url ) { |
| 486 | $url = tutils()->referer(); |
| 487 | } |
| 488 | wp_safe_redirect( $url ); |
| 489 | exit(); |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | /** |
| 494 | * @param string $action |
| 495 | * @param bool $echo |
| 496 | * |
| 497 | * @return string |
| 498 | * |
| 499 | * @since v.1.4.3 |
| 500 | */ |
| 501 | |
| 502 | if ( ! function_exists('tutor_action_field')) { |
| 503 | function tutor_action_field( $action = '', $echo = true ) { |
| 504 | $output = ''; |
| 505 | if ( $action ) { |
| 506 | $output = "<input type='hidden' name='tutor_action' value='{$action}'>"; |
| 507 | } |
| 508 | |
| 509 | if ( $echo ) { |
| 510 | echo $output; |
| 511 | } else { |
| 512 | return $output; |
| 513 | } |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | /** |
| 518 | * @return int|string |
| 519 | * |
| 520 | * Return current Time from wordpress time |
| 521 | * |
| 522 | * @since v.1.4.3 |
| 523 | */ |
| 524 | |
| 525 | if ( ! function_exists('tutor_time')) { |
| 526 | function tutor_time() { |
| 527 | //return current_time( 'timestamp' ); |
| 528 | return time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ); |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | /** |
| 533 | * Toggle maintenance mode for the site. |
| 534 | * |
| 535 | * Creates/deletes the maintenance file to enable/disable maintenance mode. |
| 536 | * |
| 537 | * @since v.1.4.6 |
| 538 | * |
| 539 | * @global WP_Filesystem_Base $wp_filesystem Subclass |
| 540 | * |
| 541 | * @param bool $enable True to enable maintenance mode, false to disable. |
| 542 | */ |
| 543 | if ( ! function_exists('tutor_maintenance_mode')) { |
| 544 | function tutor_maintenance_mode( $enable = false ) { |
| 545 | $file = ABSPATH . '.tutor_maintenance'; |
| 546 | if ( $enable ) { |
| 547 | // Create maintenance file to signal that we are upgrading |
| 548 | $maintenance_string = '<?php $upgrading = ' . time() . '; ?>'; |
| 549 | |
| 550 | if ( ! file_exists($file)){ |
| 551 | file_put_contents($file, $maintenance_string); |
| 552 | } |
| 553 | } else{ |
| 554 | if (file_exists($file)){ |
| 555 | unlink($file); |
| 556 | } |
| 557 | } |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | /** |
| 562 | * @return bool |
| 563 | * |
| 564 | * Check if the current page is course single page |
| 565 | * |
| 566 | * @since v.1.6.0 |
| 567 | */ |
| 568 | |
| 569 | if ( ! function_exists('is_single_course')) { |
| 570 | function is_single_course(){ |
| 571 | global $wp_query; |
| 572 | $course_post_type = tutor()->course_post_type; |
| 573 | |
| 574 | if (is_single() && !empty($wp_query->query['post_type']) && $wp_query->query['post_type'] === $course_post_type) { |
| 575 | return true; |
| 576 | } |
| 577 | return false; |
| 578 | } |
| 579 | } |