theme-compatibility
4 years ago
tinymce_translate.php
4 years ago
tutor-general-functions.php
3 years ago
tutor-template-functions.php
4 years ago
tutor-template-hook.php
4 years ago
tutor-general-functions.php
1047 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; |
| 4 | } |
| 5 | |
| 6 | /** |
| 7 | * Tutor input sanitization |
| 8 | */ |
| 9 | |
| 10 | if ( ! function_exists( 'tutor_sanitize_data' ) ) { |
| 11 | /** |
| 12 | * Escaping for Sanitize data. |
| 13 | * |
| 14 | * @since 1.9.13 |
| 15 | * |
| 16 | * @param string $input. |
| 17 | * @param string $type. |
| 18 | * @return string |
| 19 | */ |
| 20 | function tutor_sanitize_data( $input = null, $type = null ) { |
| 21 | $array = array(); |
| 22 | $object = new stdClass(); |
| 23 | |
| 24 | if ( is_string( $input ) ) { |
| 25 | |
| 26 | if ( 'textarea' == $type ) { |
| 27 | $input = sanitize_textarea_field( $input ); |
| 28 | } elseif ( 'kses' == $type ) { |
| 29 | $input = wp_kses_post( $input ); |
| 30 | } else { |
| 31 | $input = sanitize_text_field( $input ); |
| 32 | } |
| 33 | |
| 34 | return $input; |
| 35 | |
| 36 | } elseif ( is_object( $input ) && count( get_object_vars( $input ) ) ) { |
| 37 | |
| 38 | foreach ( $input as $key => $value ) { |
| 39 | if ( is_object( $value ) ) { |
| 40 | $object->$key = tutor_sanitize_data( $value ); |
| 41 | } else { |
| 42 | $key = sanitize_text_field( $key ); |
| 43 | $value = sanitize_text_field( $value ); |
| 44 | $object->$key = $value; |
| 45 | } |
| 46 | } |
| 47 | return $object; |
| 48 | } elseif ( is_array( $input ) && count( $input ) ) { |
| 49 | foreach ( $input as $key => $value ) { |
| 50 | if ( is_array( $value ) ) { |
| 51 | $array[ $key ] = tutor_sanitize_data( $value ); |
| 52 | } else { |
| 53 | $key = sanitize_text_field( $key ); |
| 54 | $value = sanitize_text_field( $value ); |
| 55 | $array[ $key ] = $value; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | return $array; |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | if ( ! function_exists( 'tutor_placeholder_img_src' ) ) { |
| 65 | function tutor_placeholder_img_src() { |
| 66 | $src = tutor()->url . 'assets/images/placeholder.svg'; |
| 67 | return apply_filters( 'tutor_placeholder_img_src', $src ); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @return string |
| 73 | * |
| 74 | * Get course categories selecting UI |
| 75 | * |
| 76 | * @since v.1.3.4 |
| 77 | */ |
| 78 | |
| 79 | if ( ! function_exists( 'tutor_course_categories_dropdown' ) ) { |
| 80 | function tutor_course_categories_dropdown( $post_ID = 0, $args = array() ) { |
| 81 | |
| 82 | $default = array( |
| 83 | 'classes' => '', |
| 84 | 'name' => 'tax_input[course-category]', |
| 85 | 'multiple' => true, |
| 86 | ); |
| 87 | |
| 88 | $args = apply_filters( 'tutor_course_categories_dropdown_args', array_merge( $default, $args ) ); |
| 89 | |
| 90 | $multiple_select = ''; |
| 91 | |
| 92 | if ( tutor_utils()->array_get( 'multiple', $args ) ) { |
| 93 | if ( isset( $args['name'] ) ) { |
| 94 | $args['name'] = $args['name'] . '[]'; |
| 95 | } |
| 96 | $multiple_select = "multiple='multiple'"; |
| 97 | } |
| 98 | |
| 99 | extract( $args ); |
| 100 | |
| 101 | $classes = (array) $classes; |
| 102 | $classes = implode( ' ', $classes ); |
| 103 | |
| 104 | $categories = tutor_utils()->get_course_categories(); |
| 105 | |
| 106 | $output = ''; |
| 107 | $output .= '<select name="' . $name . '" ' . $multiple_select . ' class="' . $classes . '" data-placeholder="' . __( 'Search Course Category. ex. Design, Development, Business', 'tutor' ) . '">'; |
| 108 | $output .= '<option value="">' . __( 'Select a category', 'tutor' ) . '</option>'; |
| 109 | $output .= _generate_categories_dropdown_option( $post_ID, $categories, $args ); |
| 110 | $output .= '</select>'; |
| 111 | |
| 112 | return $output; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * @return string |
| 118 | * |
| 119 | * Get course tags selecting UI |
| 120 | * |
| 121 | * @since v.1.3.4 |
| 122 | */ |
| 123 | |
| 124 | if ( ! function_exists( 'tutor_course_tags_dropdown' ) ) { |
| 125 | function tutor_course_tags_dropdown( $post_ID = 0, $args = array() ) { |
| 126 | |
| 127 | $default = array( |
| 128 | 'classes' => '', |
| 129 | 'name' => 'tax_input[course-tag]', |
| 130 | 'multiple' => true, |
| 131 | ); |
| 132 | |
| 133 | $args = apply_filters( 'tutor_course_tags_dropdown_args', array_merge( $default, $args ) ); |
| 134 | |
| 135 | $multiple_select = ''; |
| 136 | |
| 137 | if ( tutor_utils()->array_get( 'multiple', $args ) ) { |
| 138 | if ( isset( $args['name'] ) ) { |
| 139 | $args['name'] = $args['name'] . '[]'; |
| 140 | } |
| 141 | $multiple_select = "multiple='multiple'"; |
| 142 | } |
| 143 | |
| 144 | extract( $args ); |
| 145 | |
| 146 | $classes = (array) $classes; |
| 147 | $classes = implode( ' ', $classes ); |
| 148 | |
| 149 | $tags = tutor_utils()->get_course_tags(); |
| 150 | |
| 151 | $output = ''; |
| 152 | $output .= '<select name=' . $name . ' ' . $multiple_select . ' class="' . $classes . '" data-placeholder="' . __( 'Search Course Tags. ex. Design, Development, Business', 'tutor' ) . '">'; |
| 153 | $output .= '<option value="">' . __( 'Select a tag', 'tutor' ) . '</option>'; |
| 154 | $output .= _generate_tags_dropdown_option( $post_ID, $tags, $args ); |
| 155 | $output .= '</select>'; |
| 156 | |
| 157 | return $output; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * @param $categories |
| 163 | * @param string $parent_name |
| 164 | * |
| 165 | * @return string |
| 166 | * |
| 167 | * Get selecting options, recursive supports |
| 168 | * |
| 169 | * @since v.1.3.4 |
| 170 | */ |
| 171 | |
| 172 | if ( ! function_exists( '_generate_categories_dropdown_option' ) ) { |
| 173 | function _generate_categories_dropdown_option( $post_ID = 0, $categories = array(), $args = array(), $depth = 0 ) { |
| 174 | $output = ''; |
| 175 | |
| 176 | if ( ! tutor_utils()->count( $categories ) ) { |
| 177 | return $output; |
| 178 | } |
| 179 | |
| 180 | if ( ! is_numeric( $post_ID ) || $post_ID < 1 ) { |
| 181 | return $output; |
| 182 | } |
| 183 | |
| 184 | foreach ( $categories as $category_id => $category ) { |
| 185 | if ( ! $category->parent ) { |
| 186 | $depth = 0; |
| 187 | } |
| 188 | |
| 189 | $childrens = tutor_utils()->array_get( 'children', $category ); |
| 190 | $has_in_term = has_term( $category->term_id, 'course-category', $post_ID ); |
| 191 | |
| 192 | $depth_seperator = ''; |
| 193 | if ( $depth ) { |
| 194 | for ( $depth_i = 0; $depth_i < $depth; $depth_i++ ) { |
| 195 | $depth_seperator .= '-'; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | $output .= '<option value="' . $category->term_id . '" ' . selected( $has_in_term, true, false ) . '> ' . $depth_seperator . ' ' . $category->name . '</option>'; |
| 200 | |
| 201 | if ( tutor_utils()->count( $childrens ) ) { |
| 202 | $depth++; |
| 203 | $output .= _generate_categories_dropdown_option( $post_ID, $childrens, $args, $depth ); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | return $output; |
| 208 | } |
| 209 | } |
| 210 | /** |
| 211 | * @param $tags |
| 212 | * @param string $parent_name |
| 213 | * |
| 214 | * @return string |
| 215 | * |
| 216 | * Get selecting options, recursive supports |
| 217 | * |
| 218 | * @since v.1.3.4 |
| 219 | */ |
| 220 | |
| 221 | if ( ! function_exists( '_generate_tags_dropdown_option' ) ) { |
| 222 | function _generate_tags_dropdown_option( $post_ID = 0, $tags = array(), $args = array(), $depth = 0 ) { |
| 223 | $output = ''; |
| 224 | |
| 225 | if ( ! tutor_utils()->count( $tags ) ) { |
| 226 | return $output; |
| 227 | } |
| 228 | |
| 229 | if ( ! is_numeric( $post_ID ) || $post_ID < 1 ) { |
| 230 | return $output; |
| 231 | } |
| 232 | |
| 233 | foreach ( $tags as $tag ) { |
| 234 | |
| 235 | $has_in_term = has_term( $tag->term_id, 'course-tag', $post_ID ); |
| 236 | |
| 237 | $output .= '<option value="' . $tag->name . '" ' . selected( $has_in_term, true, false ) . '>' . $tag->name . '</option>'; |
| 238 | |
| 239 | } |
| 240 | |
| 241 | return $output; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * @param array $args |
| 247 | * |
| 248 | * @return string |
| 249 | * |
| 250 | * Generate course categories checkbox |
| 251 | * @since v.1.3.4 |
| 252 | */ |
| 253 | |
| 254 | if ( ! function_exists( 'tutor_course_categories_checkbox' ) ) { |
| 255 | function tutor_course_categories_checkbox( $post_ID = 0, $args = array() ) { |
| 256 | $default = array( |
| 257 | 'name' => 'tax_input[course-category]', |
| 258 | ); |
| 259 | |
| 260 | $args = apply_filters( 'tutor_course_categories_checkbox_args', array_merge( $default, $args ) ); |
| 261 | |
| 262 | if ( isset( $args['name'] ) ) { |
| 263 | $args['name'] = $args['name'] . '[]'; |
| 264 | } |
| 265 | |
| 266 | extract( $args ); |
| 267 | |
| 268 | $categories = tutor_utils()->get_course_categories(); |
| 269 | $output = ''; |
| 270 | $output .= __tutor_generate_categories_checkbox( $post_ID, $categories, $args ); |
| 271 | |
| 272 | return $output; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * @param array $args |
| 278 | * |
| 279 | * @return string |
| 280 | * |
| 281 | * Generate course tags checkbox |
| 282 | * @since v.1.3.4 |
| 283 | */ |
| 284 | |
| 285 | if ( ! function_exists( 'tutor_course_tags_checkbox' ) ) { |
| 286 | function tutor_course_tags_checkbox( $post_ID = 0, $args = array() ) { |
| 287 | $default = array( |
| 288 | 'name' => 'tax_input[course-tag]', |
| 289 | ); |
| 290 | |
| 291 | $args = apply_filters( 'tutor_course_tags_checkbox_args', array_merge( $default, $args ) ); |
| 292 | |
| 293 | if ( isset( $args['name'] ) ) { |
| 294 | $args['name'] = $args['name'] . '[]'; |
| 295 | } |
| 296 | |
| 297 | extract( $args ); |
| 298 | |
| 299 | $tags = tutor_utils()->get_course_tags(); |
| 300 | $output = ''; |
| 301 | $output .= __tutor_generate_tags_checkbox( $post_ID, $tags, $args ); |
| 302 | |
| 303 | return $output; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * @param $categories |
| 309 | * @param string $parent_name |
| 310 | * @param array $args |
| 311 | * |
| 312 | * @return string |
| 313 | * |
| 314 | * Internal function to generate course categories checkbox |
| 315 | * |
| 316 | * @since v.1.3.4 |
| 317 | */ |
| 318 | if ( ! function_exists( '__tutor_generate_categories_checkbox' ) ) { |
| 319 | function __tutor_generate_categories_checkbox( $post_ID = 0, $categories = array(), $args = array() ) { |
| 320 | |
| 321 | $output = ''; |
| 322 | $input_name = tutor_utils()->array_get( 'name', $args ); |
| 323 | |
| 324 | if ( tutor_utils()->count( $categories ) ) { |
| 325 | $output .= '<ul class="tax-input-course-category">'; |
| 326 | foreach ( $categories as $category_id => $category ) { |
| 327 | $childrens = tutor_utils()->array_get( 'children', $category ); |
| 328 | $has_in_term = has_term( $category->term_id, 'course-category', $post_ID ); |
| 329 | |
| 330 | $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>'; |
| 331 | |
| 332 | if ( tutor_utils()->count( $childrens ) ) { |
| 333 | $output .= __tutor_generate_categories_checkbox( $post_ID, $childrens, $args ); |
| 334 | } |
| 335 | $output .= ' </li>'; |
| 336 | } |
| 337 | $output .= '</ul>'; |
| 338 | } |
| 339 | |
| 340 | return $output; |
| 341 | |
| 342 | } |
| 343 | } |
| 344 | /** |
| 345 | * @param $tags |
| 346 | * @param string $parent_name |
| 347 | * @param array $args |
| 348 | * |
| 349 | * @return string |
| 350 | * |
| 351 | * Internal function to generate course tags checkbox |
| 352 | * |
| 353 | * @since v.1.3.4 |
| 354 | */ |
| 355 | if ( ! function_exists( '__tutor_generate_tags_checkbox' ) ) { |
| 356 | function __tutor_generate_tags_checkbox( $post_ID = 0, $tags = array(), $args = array() ) { |
| 357 | |
| 358 | $output = ''; |
| 359 | $input_name = tutor_utils()->array_get( 'name', $args ); |
| 360 | |
| 361 | if ( tutor_utils()->count( $tags ) ) { |
| 362 | $output .= '<ul class="tax-input-course-tag">'; |
| 363 | foreach ( $tags as $tag ) { |
| 364 | $has_in_term = has_term( $tag->term_id, 'course-tag', $post_ID ); |
| 365 | |
| 366 | $output .= '<li class="tax-input-course-tag-item tax-input-course-tag-item-' . $tag->term_id . '"><label class="course-tag-checkbox"> <input type="checkbox" name="' . $input_name . '" value="' . $tag->term_id . '" ' . checked( $has_in_term, true, false ) . ' /> <span>' . $tag->name . '</span> </label>'; |
| 367 | |
| 368 | $output .= ' </li>'; |
| 369 | } |
| 370 | $output .= '</ul>'; |
| 371 | } |
| 372 | |
| 373 | return $output; |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | * @param string $content |
| 379 | * @param string $title |
| 380 | * |
| 381 | * @return string |
| 382 | * |
| 383 | * Wrap course builder sections within div for frontend |
| 384 | * |
| 385 | * @since v.1.3.4 |
| 386 | */ |
| 387 | |
| 388 | if ( ! function_exists( 'course_builder_section_wrap' ) ) { |
| 389 | function course_builder_section_wrap( $content = '', $title = '', $echo = true ) { |
| 390 | ob_start(); |
| 391 | ?> |
| 392 | <div class="tutor-course-builder-section"> |
| 393 | <div class="tutor-course-builder-section-title"> |
| 394 | <span class="tutor-fs-5 tutor-fw-bold tutor-color-secondary"> |
| 395 | <i class="tutor-icon-angle-up" area-hidden="true"></i> |
| 396 | <span><?php echo $title; ?></span> |
| 397 | </span> |
| 398 | </div> |
| 399 | <div class="tutor-course-builder-section-content"> |
| 400 | <?php echo $content; ?> |
| 401 | </div> |
| 402 | </div> |
| 403 | <?php |
| 404 | $html = ob_get_clean(); |
| 405 | |
| 406 | if ( $echo ) { |
| 407 | echo tutor_kses_html( $html ); |
| 408 | } else { |
| 409 | return $html; |
| 410 | } |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | |
| 415 | if ( ! function_exists( 'get_tutor_header' ) ) { |
| 416 | function get_tutor_header( $fullScreen = false ) { |
| 417 | $enable_spotlight_mode = tutor_utils()->get_option( 'enable_spotlight_mode' ); |
| 418 | |
| 419 | if ( $enable_spotlight_mode || $fullScreen ) { |
| 420 | ?> |
| 421 | <!doctype html> |
| 422 | <html <?php language_attributes(); ?>> |
| 423 | |
| 424 | <head> |
| 425 | <meta charset="<?php bloginfo( 'charset' ); ?>" /> |
| 426 | <meta name="viewport" content="width=device-width, initial-scale=1" /> |
| 427 | <link rel="profile" href="https://gmpg.org/xfn/11" /> |
| 428 | <?php wp_head(); ?> |
| 429 | </head> |
| 430 | |
| 431 | <body <?php body_class(); ?>> |
| 432 | <div id="tutor-page-wrap" class="tutor-site-wrap site"> |
| 433 | <?php |
| 434 | } else { |
| 435 | tutor_utils()->tutor_custom_header(); |
| 436 | } |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | if ( ! function_exists( 'get_tutor_footer' ) ) { |
| 441 | function get_tutor_footer( $fullScreen = false ) { |
| 442 | $enable_spotlight_mode = tutor_utils()->get_option( 'enable_spotlight_mode' ); |
| 443 | if ( $enable_spotlight_mode || $fullScreen ) { |
| 444 | ?> |
| 445 | </div> |
| 446 | <?php wp_footer(); ?> |
| 447 | |
| 448 | </body> |
| 449 | |
| 450 | </html> |
| 451 | <?php |
| 452 | } else { |
| 453 | tutor_utils()->tutor_custom_footer(); |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | /** |
| 459 | * @param null $key |
| 460 | * @param bool $default |
| 461 | * |
| 462 | * @return array|bool|mixed |
| 463 | * |
| 464 | * Get tutor option by this helper function |
| 465 | * |
| 466 | * @since v.1.3.6 |
| 467 | */ |
| 468 | if ( ! function_exists( 'get_tutor_option' ) ) { |
| 469 | function get_tutor_option( $key = null, $default = false ) { |
| 470 | return tutor_utils()->get_option( $key, $default ); |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | /** |
| 475 | * @param null $key |
| 476 | * @param bool $value |
| 477 | * |
| 478 | * Update tutor option by this helper function |
| 479 | * |
| 480 | * @since v.1.3.6 |
| 481 | */ |
| 482 | if ( ! function_exists( 'update_tutor_option' ) ) { |
| 483 | function update_tutor_option( $key = null, $value = false ) { |
| 484 | tutor_utils()->update_option( $key, $value ); |
| 485 | } |
| 486 | } |
| 487 | /** |
| 488 | * @param int $course_id |
| 489 | * @param null $key |
| 490 | * @param bool $default |
| 491 | * |
| 492 | * @return array|bool|mixed |
| 493 | * |
| 494 | * Get tutor course settings by course ID |
| 495 | * |
| 496 | * @since v.1.4.1 |
| 497 | */ |
| 498 | if ( ! function_exists( 'get_tutor_course_settings' ) ) { |
| 499 | function get_tutor_course_settings( $course_id = 0, $key = null, $default = false ) { |
| 500 | return tutor_utils()->get_course_settings( $course_id, $key, $default ); |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | /** |
| 505 | * @param int $lesson_id |
| 506 | * @param null $key |
| 507 | * @param bool $default |
| 508 | * |
| 509 | * @return array|bool|mixed |
| 510 | * |
| 511 | * Get lesson content drip settings |
| 512 | */ |
| 513 | |
| 514 | if ( ! function_exists( 'get_item_content_drip_settings' ) ) { |
| 515 | function get_item_content_drip_settings( $lesson_id = 0, $key = null, $default = false ) { |
| 516 | return tutor_utils()->get_item_content_drip_settings( $lesson_id, $key, $default ); |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | /** |
| 521 | * @param null $msg |
| 522 | * @param string $type |
| 523 | * @param bool $echo |
| 524 | * |
| 525 | * @return string |
| 526 | * |
| 527 | * Print Alert by tutor_alert() |
| 528 | * |
| 529 | * @since v.1.4.1 |
| 530 | */ |
| 531 | if ( ! function_exists( 'tutor_alert' ) ) { |
| 532 | function tutor_alert( $msg = null, $type = 'warning', $echo = true ) { |
| 533 | if ( ! $msg ) { |
| 534 | |
| 535 | if ( $type === 'any' ) { |
| 536 | if ( ! $msg ) { |
| 537 | $type = 'warning'; |
| 538 | $msg = tutor_flash_get( $type ); |
| 539 | } |
| 540 | if ( ! $msg ) { |
| 541 | $type = 'danger'; |
| 542 | $msg = tutor_flash_get( $type ); |
| 543 | } |
| 544 | if ( ! $msg ) { |
| 545 | $type = 'success'; |
| 546 | $msg = tutor_flash_get( $type ); |
| 547 | } |
| 548 | } else { |
| 549 | $msg = tutor_flash_get( $type ); |
| 550 | } |
| 551 | } |
| 552 | if ( ! $msg ) { |
| 553 | return $msg; |
| 554 | } |
| 555 | |
| 556 | $html = '<div class="asas tutor-alert tutor-' . esc_attr( $type ) . '"> |
| 557 | <div class="tutor-alert-text"> |
| 558 | <span class="tutor-alert-icon tutor-fs-4 tutor-icon-circle-info tutor-mr-12"></span> |
| 559 | <span>' . wp_kses( $msg, array( 'div', 'span' ) ) . '</span> |
| 560 | </div> |
| 561 | </div>'; |
| 562 | if ( $echo ) { |
| 563 | echo tutor_kses_html( $html ); |
| 564 | } |
| 565 | return $html; |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | |
| 570 | /** |
| 571 | * @param bool $echo |
| 572 | * |
| 573 | * Simply call tutor_nonce_field() to generate nonce field |
| 574 | * |
| 575 | * @since v.1.4.2 |
| 576 | */ |
| 577 | |
| 578 | if ( ! function_exists( 'tutor_nonce_field' ) ) { |
| 579 | function tutor_nonce_field( $echo = true ) { |
| 580 | wp_nonce_field( tutor()->nonce_action, tutor()->nonce, $echo ); |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | /** |
| 585 | * @param null $key |
| 586 | * @param string $message |
| 587 | * |
| 588 | * Set Flash Message |
| 589 | */ |
| 590 | |
| 591 | if ( ! function_exists( 'tutor_flash_set' ) ) { |
| 592 | function tutor_flash_set( $key = null, $message = '' ) { |
| 593 | if ( ! $key ) { |
| 594 | return; |
| 595 | } |
| 596 | // ensure session is started |
| 597 | if ( session_status() !== PHP_SESSION_ACTIVE ) { |
| 598 | session_start(); |
| 599 | } |
| 600 | $_SESSION[ $key ] = $message; |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | /** |
| 605 | * @param null $key |
| 606 | * |
| 607 | * @return array|bool|mixed|null |
| 608 | * |
| 609 | * @since v.1.4.2 |
| 610 | * |
| 611 | * Get flash message |
| 612 | */ |
| 613 | |
| 614 | if ( ! function_exists( 'tutor_flash_get' ) ) { |
| 615 | function tutor_flash_get( $key = null ) { |
| 616 | if ( $key ) { |
| 617 | // ensure session is started |
| 618 | if ( session_status() !== PHP_SESSION_ACTIVE ) { |
| 619 | @session_start(); |
| 620 | } |
| 621 | if ( empty( $_SESSION ) ) { |
| 622 | return null; |
| 623 | } |
| 624 | $message = tutor_utils()->array_get( $key, $_SESSION ); |
| 625 | if ( $message ) { |
| 626 | unset( $_SESSION[ $key ] ); |
| 627 | } |
| 628 | return $message; |
| 629 | } |
| 630 | return $key; |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | if ( ! function_exists( 'tutor_redirect_back' ) ) { |
| 635 | /** |
| 636 | * @param null $url |
| 637 | * |
| 638 | * Redirect to back or a specific URL and terminate |
| 639 | * |
| 640 | * @since v.1.4.3 |
| 641 | */ |
| 642 | function tutor_redirect_back( $url = null ) { |
| 643 | if ( ! $url ) { |
| 644 | $url = tutor_utils()->referer(); |
| 645 | } |
| 646 | wp_safe_redirect( $url ); |
| 647 | exit(); |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | /** |
| 652 | * @param string $action |
| 653 | * @param bool $echo |
| 654 | * |
| 655 | * @return string |
| 656 | * |
| 657 | * @since v.1.4.3 |
| 658 | */ |
| 659 | |
| 660 | if ( ! function_exists( 'tutor_action_field' ) ) { |
| 661 | function tutor_action_field( $action = '', $echo = true ) { |
| 662 | $output = ''; |
| 663 | if ( $action ) { |
| 664 | $output = '<input type="hidden" name="tutor_action" value="' . $action . '">'; |
| 665 | } |
| 666 | |
| 667 | if ( $echo ) { |
| 668 | echo tutor_kses_html( $output ); |
| 669 | } else { |
| 670 | return $output; |
| 671 | } |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | /** |
| 676 | * @return int|string |
| 677 | * |
| 678 | * Return current Time from WordPress time |
| 679 | * |
| 680 | * @since v.1.4.3 |
| 681 | */ |
| 682 | |
| 683 | if ( ! function_exists( 'tutor_time' ) ) { |
| 684 | function tutor_time() { |
| 685 | // return current_time( 'timestamp' ); |
| 686 | return time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ); |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | /** |
| 691 | * Toggle maintenance mode for the site. |
| 692 | * |
| 693 | * Creates/deletes the maintenance file to enable/disable maintenance mode. |
| 694 | * |
| 695 | * @since v.1.4.6 |
| 696 | * |
| 697 | * @global WP_Filesystem_Base $wp_filesystem Subclass |
| 698 | * |
| 699 | * @param bool $enable True to enable maintenance mode, false to disable. |
| 700 | */ |
| 701 | if ( ! function_exists( 'tutor_maintenance_mode' ) ) { |
| 702 | function tutor_maintenance_mode( $enable = false ) { |
| 703 | $file = ABSPATH . '.tutor_maintenance'; |
| 704 | if ( $enable ) { |
| 705 | // Create maintenance file to signal that we are upgrading |
| 706 | $maintenance_string = '<?php $upgrading = ' . time() . '; ?>'; |
| 707 | |
| 708 | if ( ! file_exists( $file ) ) { |
| 709 | file_put_contents( $file, $maintenance_string ); |
| 710 | } |
| 711 | } else { |
| 712 | if ( file_exists( $file ) ) { |
| 713 | unlink( $file ); |
| 714 | } |
| 715 | } |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | /** |
| 720 | * @return bool |
| 721 | * |
| 722 | * Check if the current page is course single page |
| 723 | * |
| 724 | * @since v.1.6.0 |
| 725 | */ |
| 726 | |
| 727 | if ( ! function_exists( 'is_single_course' ) ) { |
| 728 | function is_single_course( $check_spotlight = false ) { |
| 729 | global $wp_query; |
| 730 | $course_post_type = tutor()->course_post_type; |
| 731 | |
| 732 | $post_types = array( $course_post_type ); |
| 733 | if ( $check_spotlight ) { |
| 734 | $post_types = array_merge( |
| 735 | $post_types, |
| 736 | array( |
| 737 | 'lesson', |
| 738 | 'tutor_quiz', |
| 739 | 'tutor_assignments', |
| 740 | 'tutor_zoom_meeting', |
| 741 | ) |
| 742 | ); |
| 743 | } |
| 744 | |
| 745 | if ( is_single() && ! empty( $wp_query->query['post_type'] ) && in_array( $wp_query->query['post_type'], $post_types ) ) { |
| 746 | return true; |
| 747 | } |
| 748 | return false; |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | /** |
| 753 | * Require wp_date form return js date format. |
| 754 | * this is helpful for date picker |
| 755 | * |
| 756 | * @return string |
| 757 | * |
| 758 | * @since 1.9.7 |
| 759 | */ |
| 760 | if ( ! function_exists( 'tutor_js_date_format_against_wp' ) ) { |
| 761 | function tutor_js_date_format_against_wp() { |
| 762 | $wp_date_format = get_option( 'date_format' ); |
| 763 | $default_format = 'Y-M-d'; |
| 764 | |
| 765 | $formats = array( |
| 766 | 'Y-m-d' => 'Y-M-d', |
| 767 | 'm/d/Y' => 'M-d-Y', |
| 768 | 'd/m/Y' => 'd-M-Y', |
| 769 | 'F j, Y' => 'MMMM d, yyyy', |
| 770 | 'j F Y' => 'MMMM d, yyyy', |
| 771 | ); |
| 772 | return isset( $formats[ $wp_date_format ] ) ? $formats[ $wp_date_format ] : $default_format; |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | /** |
| 777 | * Convert date to desire format |
| 778 | * |
| 779 | * NOTE: mysql query use formated date from here |
| 780 | * that's why date_i18n need to be ignore |
| 781 | * |
| 782 | * @param $format string |
| 783 | * |
| 784 | * @param $date string |
| 785 | * |
| 786 | * @return string ( date ) |
| 787 | */ |
| 788 | if ( ! function_exists( 'tutor_get_formated_date' ) ) { |
| 789 | function tutor_get_formated_date( string $require_format, string $user_date ) { |
| 790 | $date = date_create( str_replace( '/', '-', $user_date ) ); |
| 791 | if ( is_a( $date, 'DateTime' ) ) { |
| 792 | $formatted_date = date_format( $date, $require_format ); |
| 793 | } else { |
| 794 | $formatted_date = date( $require_format, strtotime( $user_date ) ); |
| 795 | } |
| 796 | return $formatted_date; |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | /** |
| 801 | * Get translated date |
| 802 | * |
| 803 | * @since v2.0.2 |
| 804 | * |
| 805 | * @param string $date date in string from to translate & format. |
| 806 | * @param string $format optional date format, default is wp date time format. |
| 807 | * |
| 808 | * @return string translated date |
| 809 | */ |
| 810 | if ( ! function_exists( 'tutor_i18n_get_formated_date' ) ) { |
| 811 | function tutor_i18n_get_formated_date( string $date, string $format = '' ) { |
| 812 | if ( '' === $format ) { |
| 813 | $format = get_option( 'date_format' ) . ' ' . get_option( 'time_format' ); |
| 814 | } |
| 815 | return date_i18n( $format, strtotime( $date ) ); |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | if ( ! function_exists( '_tutor_search_by_title_only' ) ) { |
| 820 | /** |
| 821 | * Search SQL filter for matching against post title only. |
| 822 | * |
| 823 | * @link http://wordpress.stackexchange.com/a/11826/1685 |
| 824 | * |
| 825 | * @param string $search |
| 826 | * @param WP_Query $wp_query |
| 827 | */ |
| 828 | function _tutor_search_by_title_only( $search, $wp_query ) { |
| 829 | if ( ! empty( $search ) && ! empty( $wp_query->query_vars['search_terms'] ) ) { |
| 830 | global $wpdb; |
| 831 | |
| 832 | $q = $wp_query->query_vars; |
| 833 | $n = ! empty( $q['exact'] ) ? '' : '%'; |
| 834 | |
| 835 | $search = array(); |
| 836 | |
| 837 | foreach ( (array) $q['search_terms'] as $term ) { |
| 838 | $search[] = $wpdb->prepare( "$wpdb->posts.post_title LIKE %s", $n . $wpdb->esc_like( $term ) . $n ); |
| 839 | } |
| 840 | |
| 841 | if ( ! is_user_logged_in() ) { |
| 842 | $search[] = "$wpdb->posts.post_password = ''"; |
| 843 | } |
| 844 | |
| 845 | $search = ' AND ' . implode( ' AND ', $search ); |
| 846 | } |
| 847 | |
| 848 | return $search; |
| 849 | } |
| 850 | } |
| 851 | |
| 852 | if ( ! function_exists( 'pr' ) ) { |
| 853 | /** |
| 854 | * Function to print_r |
| 855 | * |
| 856 | * @param array $var . |
| 857 | * @return array |
| 858 | */ |
| 859 | function pr( $var ) { |
| 860 | $template = PHP_SAPI !== 'cli' && PHP_SAPI !== 'phpdbg' ? '<pre class="pr">%s</pre>' : "\n%s\n\n"; |
| 861 | printf( $template, trim( print_r( $var, true ) ) ); |
| 862 | |
| 863 | return $var; |
| 864 | } |
| 865 | } |
| 866 | |
| 867 | if ( ! function_exists( 'tutor_vd' ) ) { |
| 868 | /** |
| 869 | * Function to var_dump |
| 870 | * |
| 871 | * @param array $var . |
| 872 | * @return array |
| 873 | */ |
| 874 | function tutor_vd( $var ) { |
| 875 | $template = PHP_SAPI !== 'cli' && PHP_SAPI !== 'phpdbg' ? '<pre class="pr">%s</pre>' : "\n%s\n\n"; |
| 876 | printf( $template, trim( var_dump( $var, true ) ) ); |
| 877 | |
| 878 | return $var; |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | |
| 883 | |
| 884 | if ( ! function_exists( 'get_request' ) ) { |
| 885 | /** |
| 886 | * Function to get_request |
| 887 | * |
| 888 | * @param array $var . |
| 889 | * @return array |
| 890 | */ |
| 891 | function get_request( $var ) { |
| 892 | return isset( $_REQUEST[ $var ] ) ? sanitize_text_field( $_REQUEST[ $var ] ) : false; |
| 893 | |
| 894 | } |
| 895 | } |
| 896 | |
| 897 | if ( ! function_exists( 'tutor_kses_allowed_html' ) ) { |
| 898 | function tutor_kses_allowed_html( $allowed_tags, $context ) { |
| 899 | $tags = array( 'input', 'style', 'script', 'select', 'form', 'option', 'optgroup', 'iframe', 'bdi', 'source' ); |
| 900 | $atts = array( 'min', 'max', 'maxlength', 'type', 'method', 'enctype', 'action', 'selected', 'class', 'id', 'disabled', 'checked', 'readonly', 'name', 'aria-*', 'style', 'role', 'placeholder', 'value', 'data-*', 'src', 'width', 'height', 'frameborder', 'allow', 'fullscreen', 'title', 'multiple' ); |
| 901 | |
| 902 | foreach ( $tags as $tag ) { |
| 903 | $tag_attrs = array(); |
| 904 | |
| 905 | foreach ( $atts as $att ) { |
| 906 | $tag_attrs[ $att ] = true; |
| 907 | } |
| 908 | |
| 909 | $allowed_tags[ $tag ] = $tag_attrs; |
| 910 | } |
| 911 | |
| 912 | return $allowed_tags; |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | if ( ! function_exists( 'tutor_kses_allowed_css' ) ) { |
| 917 | function tutor_kses_allowed_css( $styles ) { |
| 918 | $styles[] = 'display'; |
| 919 | $styles[] = '--progress-value'; |
| 920 | return $styles; |
| 921 | } |
| 922 | } |
| 923 | |
| 924 | if ( ! function_exists( 'tutor_kses_html' ) ) { |
| 925 | function tutor_kses_html( $content ) { |
| 926 | |
| 927 | return $content; |
| 928 | add_filter( 'wp_kses_allowed_html', 'tutor_kses_allowed_html', 10, 2 ); |
| 929 | add_filter( 'safe_style_css', 'tutor_kses_allowed_css' ); |
| 930 | |
| 931 | $content = preg_replace( '/<!--(.|\s)*?-->/', '', $content ); |
| 932 | $content = wp_kses_post( $content ); |
| 933 | $content = str_replace( '&', '&', $content ); |
| 934 | |
| 935 | remove_filter( 'safe_style_css', 'tutor_kses_allowed_css' ); |
| 936 | remove_filter( 'wp_kses_allowed_html', 'tutor_kses_allowed_html' ); |
| 937 | |
| 938 | return $content; |
| 939 | } |
| 940 | } |
| 941 | |
| 942 | /** |
| 943 | * @return array |
| 944 | * |
| 945 | * Get all Withdraw Methods available on this system |
| 946 | * |
| 947 | * @since v.1.5.7 |
| 948 | */ |
| 949 | if ( ! function_exists( 'get_tutor_all_withdrawal_methods' ) ) { |
| 950 | function get_tutor_all_withdrawal_methods() { |
| 951 | return apply_filters( 'tutor_withdrawal_methods_all', array() ); |
| 952 | } |
| 953 | } |
| 954 | |
| 955 | |
| 956 | if ( ! function_exists( 'tutor_log' ) ) { |
| 957 | function tutor_log() { |
| 958 | $arg_list = func_get_args(); |
| 959 | |
| 960 | foreach ($arg_list as $data) { |
| 961 | ob_start(); |
| 962 | var_dump( $data ); |
| 963 | error_log( ob_get_clean() ); |
| 964 | } |
| 965 | } |
| 966 | } |
| 967 | |
| 968 | if ( ! function_exists( 'tutor_wc_price_currency_format' ) ) { |
| 969 | function tutor_wc_price_currency_format( $amount ) { |
| 970 | |
| 971 | $symbol = get_woocommerce_currency_symbol(); |
| 972 | $position = get_option( 'woocommerce_currency_pos', 'left' ); |
| 973 | |
| 974 | switch ( $position ) { |
| 975 | case 'left': |
| 976 | $amount = $symbol . $amount; |
| 977 | break; |
| 978 | case 'left_space': |
| 979 | $amount = $symbol . ' ' . $amount; |
| 980 | break; |
| 981 | |
| 982 | case 'right': |
| 983 | $amount = $amount . $symbol; |
| 984 | break; |
| 985 | case 'right_space': |
| 986 | $amount = $amount . ' ' . $symbol; |
| 987 | break; |
| 988 | |
| 989 | default: |
| 990 | $amount = $symbol . $amount; |
| 991 | break; |
| 992 | } |
| 993 | |
| 994 | return $amount; |
| 995 | } |
| 996 | } |
| 997 | |
| 998 | if ( ! function_exists( 'tutor_meta_box_wrapper' ) ) { |
| 999 | /** |
| 1000 | * Tutor meta box wrapper |
| 1001 | * |
| 1002 | * @since v2.0.2 |
| 1003 | * |
| 1004 | * @param string $id id of meta box. |
| 1005 | * @param string $title meta box title. |
| 1006 | * @param mixed $callback callback function that meta box will call. |
| 1007 | * @param string $screen which screen meta box should appear. |
| 1008 | * @param string $context optional param. Where meta box should appear. |
| 1009 | * @param string $priority optional. |
| 1010 | * @param string $custom_class optional. If provide it will add this class along |
| 1011 | * with div id. |
| 1012 | * |
| 1013 | * @return void if class provided then filter hook will return class. |
| 1014 | */ |
| 1015 | function tutor_meta_box_wrapper( |
| 1016 | $id, |
| 1017 | $title, |
| 1018 | $callback, |
| 1019 | $screen, |
| 1020 | $context = 'advanced', |
| 1021 | $priority = 'default', |
| 1022 | $custom_class = '' |
| 1023 | ) { |
| 1024 | add_meta_box( |
| 1025 | $id, |
| 1026 | $title, |
| 1027 | $callback, |
| 1028 | $screen, |
| 1029 | $context, |
| 1030 | $priority |
| 1031 | ); |
| 1032 | if ( '' !== $custom_class ) { |
| 1033 | $post_type = tutor()->course_post_type; |
| 1034 | add_filter( |
| 1035 | "postbox_classes_{$post_type}_{$id}", |
| 1036 | function( $classes ) use ( $custom_class ) { |
| 1037 | if ( ! in_array( $custom_class, $classes ) ) { |
| 1038 | $classes[] = $custom_class; |
| 1039 | } |
| 1040 | return $classes; |
| 1041 | } |
| 1042 | ); |
| 1043 | } |
| 1044 | } |
| 1045 | } |
| 1046 | |
| 1047 |