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