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