Addons.php
4 years ago
Admin.php
4 years ago
Ajax.php
3 years ago
Announcements.php
4 years ago
Assets.php
3 years ago
Backend_Page_Trait.php
4 years ago
Course.php
3 years ago
Course_Filter.php
4 years ago
Course_List.php
3 years ago
Course_Settings_Tabs.php
4 years ago
Course_Widget.php
4 years ago
Custom_Validation.php
4 years ago
Dashboard.php
3 years ago
FormHandler.php
4 years ago
Frontend.php
3 years ago
Gutenberg.php
4 years ago
Input.php
3 years ago
Instructor.php
4 years ago
Instructors_List.php
3 years ago
Lesson.php
4 years ago
Options_V2.php
3 years ago
Post_types.php
4 years ago
Private_Course_Access.php
4 years ago
Q_and_A.php
3 years ago
Question_Answers_List.php
4 years ago
Quiz.php
3 years ago
Quiz_Attempts_List.php
3 years ago
RestAPI.php
4 years ago
Reviews.php
4 years ago
Rewrite_Rules.php
4 years ago
Shortcode.php
4 years ago
Student.php
4 years ago
Students_List.php
4 years ago
Taxonomies.php
4 years ago
Template.php
3 years ago
Theme_Compatibility.php
5 years ago
Tools.php
3 years ago
Tools_V2.php
4 years ago
Tutor.php
3 years ago
TutorEDD.php
4 years ago
Tutor_Base.php
5 years ago
Tutor_List_Table.php
4 years ago
Tutor_Setup.php
3 years ago
Upgrader.php
4 years ago
User.php
4 years ago
Utils.php
3 years ago
Video_Stream.php
4 years ago
Withdraw.php
3 years ago
Withdraw_Requests_List.php
4 years ago
WooCommerce.php
3 years ago
Ajax.php
582 lines
| 1 | <?php |
| 2 | namespace TUTOR; |
| 3 | |
| 4 | if ( ! defined( 'ABSPATH' ) ) { |
| 5 | exit; |
| 6 | } |
| 7 | |
| 8 | class Ajax { |
| 9 | public function __construct() { |
| 10 | |
| 11 | add_action( 'wp_ajax_sync_video_playback', array( $this, 'sync_video_playback' ) ); |
| 12 | add_action( 'wp_ajax_nopriv_sync_video_playback', array( $this, 'sync_video_playback_noprev' ) ); |
| 13 | add_action( 'wp_ajax_tutor_place_rating', array( $this, 'tutor_place_rating' ) ); |
| 14 | add_action( 'wp_ajax_delete_tutor_review', array( $this, 'delete_tutor_review' ) ); |
| 15 | |
| 16 | add_action( 'wp_ajax_tutor_course_add_to_wishlist', array( $this, 'tutor_course_add_to_wishlist' ) ); |
| 17 | add_action( 'wp_ajax_nopriv_tutor_course_add_to_wishlist', array( $this, 'tutor_course_add_to_wishlist' ) ); |
| 18 | |
| 19 | /** |
| 20 | * Get all addons |
| 21 | */ |
| 22 | add_action( 'wp_ajax_tutor_get_all_addons', array( $this, 'tutor_get_all_addons' ) ); |
| 23 | |
| 24 | /** |
| 25 | * Addon Enable Disable Control |
| 26 | */ |
| 27 | add_action( 'wp_ajax_addon_enable_disable', array( $this, 'addon_enable_disable' ) ); |
| 28 | |
| 29 | /** |
| 30 | * Ajax login |
| 31 | * |
| 32 | * @since v.1.6.3 |
| 33 | */ |
| 34 | add_action( 'wp_ajax_nopriv_tutor_user_login', array( $this, 'process_ajax_login' ) ); |
| 35 | |
| 36 | /** |
| 37 | * Announcement |
| 38 | * |
| 39 | * @since v.1.7.9 |
| 40 | */ |
| 41 | add_action( 'wp_ajax_tutor_announcement_create', array( $this, 'create_or_update_annoucement' ) ); |
| 42 | add_action( 'wp_ajax_tutor_announcement_delete', array( $this, 'delete_annoucement' ) ); |
| 43 | } |
| 44 | |
| 45 | |
| 46 | |
| 47 | /** |
| 48 | * Update video information and data when necessary |
| 49 | * |
| 50 | * @since v.1.0.0 |
| 51 | */ |
| 52 | public function sync_video_playback() { |
| 53 | tutor_utils()->checking_nonce(); |
| 54 | |
| 55 | $user_id = get_current_user_id(); |
| 56 | $post_id = isset( $_POST['post_id'] ) ? sanitize_text_field( $_POST['post_id'] ) : 0; |
| 57 | $duration = sanitize_text_field( $_POST['duration'] ); |
| 58 | $currentTime = sanitize_text_field( $_POST['currentTime'] ); |
| 59 | |
| 60 | if ( ! tutor_utils()->has_enrolled_content_access( 'lesson', $post_id ) ) { |
| 61 | wp_send_json_error( array( 'message' => __( 'Access Denied', 'tutor' ) ) ); |
| 62 | exit; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Update posts attached video |
| 67 | */ |
| 68 | $video = tutor_utils()->get_video( $post_id ); |
| 69 | |
| 70 | if ( $duration ) { |
| 71 | $video['duration_sec'] = $duration; // secs |
| 72 | $video['playtime'] = tutor_utils()->playtime_string( $duration ); |
| 73 | $video['runtime'] = tutor_utils()->playtime_array( $duration ); |
| 74 | } |
| 75 | tutor_utils()->update_video( $post_id, $video ); |
| 76 | |
| 77 | /** |
| 78 | * Sync Lesson Reading Info by Users |
| 79 | */ |
| 80 | |
| 81 | $best_watch_time = tutor_utils()->get_lesson_reading_info( $post_id, $user_id, 'video_best_watched_time' ); |
| 82 | if ( $best_watch_time < $currentTime ) { |
| 83 | tutor_utils()->update_lesson_reading_info( $post_id, $user_id, 'video_best_watched_time', $currentTime ); |
| 84 | } |
| 85 | |
| 86 | if ( tutor_utils()->avalue_dot( 'is_ended', $_POST ) ) { |
| 87 | tutor_utils()->mark_lesson_complete( $post_id ); |
| 88 | } |
| 89 | exit(); |
| 90 | } |
| 91 | |
| 92 | public function sync_video_playback_noprev() { |
| 93 | |
| 94 | } |
| 95 | |
| 96 | |
| 97 | public function tutor_place_rating() { |
| 98 | global $wpdb; |
| 99 | |
| 100 | tutor_utils()->checking_nonce(); |
| 101 | |
| 102 | $moderation= tutor_utils()->get_option('enable_course_review_moderation', false, true, true); |
| 103 | $rating = sanitize_text_field( tutor_utils()->avalue_dot( 'tutor_rating_gen_input', $_POST ) ); |
| 104 | $course_id = sanitize_text_field( tutor_utils()->avalue_dot( 'course_id', $_POST ) ); |
| 105 | $review = sanitize_textarea_field( tutor_utils()->avalue_dot( 'review', $_POST ) ); |
| 106 | |
| 107 | ! $rating ? $rating = 0 : 0; |
| 108 | $rating > 5 ? $rating = 5 : 0; |
| 109 | |
| 110 | $user_id = get_current_user_id(); |
| 111 | $user = get_userdata( $user_id ); |
| 112 | $date = date( 'Y-m-d H:i:s', tutor_time() ); |
| 113 | |
| 114 | if ( ! tutor_utils()->has_enrolled_content_access( 'course', $course_id ) ) { |
| 115 | wp_send_json_error( array( 'message' => __( 'Access Denied', 'tutor' ) ) ); |
| 116 | exit; |
| 117 | } |
| 118 | |
| 119 | do_action( 'tutor_before_rating_placed' ); |
| 120 | |
| 121 | $previous_rating_id = $wpdb->get_var( |
| 122 | $wpdb->prepare( |
| 123 | "SELECT comment_ID |
| 124 | from {$wpdb->comments} |
| 125 | WHERE comment_post_ID = %d AND |
| 126 | user_id = %d AND |
| 127 | comment_type = 'tutor_course_rating' |
| 128 | LIMIT 1;", |
| 129 | $course_id, |
| 130 | $user_id |
| 131 | ) |
| 132 | ); |
| 133 | |
| 134 | $review_ID = $previous_rating_id; |
| 135 | if ( $previous_rating_id ) { |
| 136 | $wpdb->update( |
| 137 | $wpdb->comments, |
| 138 | array( |
| 139 | 'comment_content' => $review, |
| 140 | 'comment_approved' => $moderation ? 'hold' : 'approved', |
| 141 | 'comment_date' => $date, |
| 142 | 'comment_date_gmt' => get_gmt_from_date( $date ), |
| 143 | ), |
| 144 | array( 'comment_ID' => $previous_rating_id ) |
| 145 | ); |
| 146 | |
| 147 | $rating_info = $wpdb->get_row( $wpdb->prepare( |
| 148 | "SELECT * FROM {$wpdb->commentmeta} |
| 149 | WHERE comment_id = %d |
| 150 | AND meta_key = 'tutor_rating'; ", |
| 151 | $previous_rating_id |
| 152 | ) ); |
| 153 | |
| 154 | if ( $rating_info ) { |
| 155 | $wpdb->update( |
| 156 | $wpdb->commentmeta, |
| 157 | array( 'meta_value' => $rating ), |
| 158 | array( |
| 159 | 'comment_id' => $previous_rating_id, |
| 160 | 'meta_key' => 'tutor_rating', |
| 161 | ) |
| 162 | ); |
| 163 | } else { |
| 164 | $wpdb->insert( |
| 165 | $wpdb->commentmeta, |
| 166 | array( |
| 167 | 'comment_id' => $previous_rating_id, |
| 168 | 'meta_key' => 'tutor_rating', |
| 169 | 'meta_value' => $rating, |
| 170 | ) |
| 171 | ); |
| 172 | } |
| 173 | } else { |
| 174 | $data = array( |
| 175 | 'comment_post_ID' => esc_sql( $course_id ), |
| 176 | 'comment_approved' => $moderation ? 'hold' : 'approved', |
| 177 | 'comment_type' => 'tutor_course_rating', |
| 178 | 'comment_date' => $date, |
| 179 | 'comment_date_gmt' => get_gmt_from_date( $date ), |
| 180 | 'user_id' => $user_id, |
| 181 | 'comment_author' => $user->user_login, |
| 182 | 'comment_agent' => 'TutorLMSPlugin', |
| 183 | ); |
| 184 | if ( $review ) { |
| 185 | $data['comment_content'] = $review; |
| 186 | } |
| 187 | |
| 188 | $wpdb->insert( $wpdb->comments, $data ); |
| 189 | $comment_id = (int) $wpdb->insert_id; |
| 190 | $review_ID = $comment_id; |
| 191 | |
| 192 | if ( $comment_id ) { |
| 193 | $result = $wpdb->insert( |
| 194 | $wpdb->commentmeta, |
| 195 | array( |
| 196 | 'comment_id' => $comment_id, |
| 197 | 'meta_key' => 'tutor_rating', |
| 198 | 'meta_value' => $rating, |
| 199 | ) |
| 200 | ); |
| 201 | |
| 202 | do_action( 'tutor_after_rating_placed', $comment_id ); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | wp_send_json_success( |
| 207 | array( |
| 208 | 'message' => __( 'Rating placed successsully!', 'tutor' ), |
| 209 | 'review_id' => $review_ID, |
| 210 | ) |
| 211 | ); |
| 212 | } |
| 213 | |
| 214 | public function delete_tutor_review() { |
| 215 | tutor_utils()->checking_nonce(); |
| 216 | |
| 217 | $review_id = sanitize_text_field( tutor_utils()->array_get( 'review_id', $_POST ) ); |
| 218 | |
| 219 | if ( ! tutor_utils()->can_user_manage( 'review', $review_id, get_current_user_id() ) ) { |
| 220 | wp_send_json_error( array( 'message' => __( 'Permissioned Denied!', 'tutor' ) ) ); |
| 221 | exit; |
| 222 | } |
| 223 | |
| 224 | global $wpdb; |
| 225 | $wpdb->delete( $wpdb->commentmeta, array( 'comment_id' => $review_id ) ); |
| 226 | $wpdb->delete( $wpdb->comments, array( 'comment_ID' => $review_id ) ); |
| 227 | |
| 228 | wp_send_json_success(); |
| 229 | } |
| 230 | |
| 231 | public function tutor_course_add_to_wishlist() { |
| 232 | tutor_utils()->checking_nonce(); |
| 233 | |
| 234 | // Redirect login since only logged in user can add courses to wishlist |
| 235 | if ( ! is_user_logged_in() ) { |
| 236 | wp_send_json_error( |
| 237 | array( |
| 238 | 'redirect_to' => wp_login_url( wp_get_referer() ), |
| 239 | ) |
| 240 | ); |
| 241 | } |
| 242 | |
| 243 | global $wpdb; |
| 244 | $user_id = get_current_user_id(); |
| 245 | $course_id = (int) sanitize_text_field( $_POST['course_id'] ); |
| 246 | |
| 247 | $if_added_to_list = $wpdb->get_row( |
| 248 | $wpdb->prepare( |
| 249 | "SELECT * from {$wpdb->usermeta} |
| 250 | WHERE user_id = %d |
| 251 | AND meta_key = '_tutor_course_wishlist' |
| 252 | AND meta_value = %d;", |
| 253 | $user_id, |
| 254 | $course_id |
| 255 | ) |
| 256 | ); |
| 257 | |
| 258 | if ( $if_added_to_list ) { |
| 259 | $wpdb->delete( |
| 260 | $wpdb->usermeta, |
| 261 | array( |
| 262 | 'user_id' => $user_id, |
| 263 | 'meta_key' => '_tutor_course_wishlist', |
| 264 | 'meta_value' => $course_id, |
| 265 | ) |
| 266 | ); |
| 267 | wp_send_json_success( |
| 268 | array( |
| 269 | 'status' => 'removed', |
| 270 | 'message' => __( 'Course removed from wish list', 'tutor' ), |
| 271 | ) |
| 272 | ); |
| 273 | } else { |
| 274 | add_user_meta( $user_id, '_tutor_course_wishlist', $course_id ); |
| 275 | wp_send_json_success( |
| 276 | array( |
| 277 | 'status' => 'added', |
| 278 | 'message' => __( 'Course added to wish list', 'tutor' ), |
| 279 | ) |
| 280 | ); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Prepare addons data |
| 286 | */ |
| 287 | public function prepare_addons_data() { |
| 288 | $addons = apply_filters( 'tutor_addons_lists_config', array() ); |
| 289 | $plugins_data = $addons; |
| 290 | |
| 291 | if ( is_array( $addons ) && count( $addons ) ) { |
| 292 | foreach ( $addons as $base_name => $addon ) { |
| 293 | $addon_config = tutor_utils()->get_addon_config( $base_name ); |
| 294 | $is_enabled = (bool) tutor_utils()->avalue_dot( 'is_enable', $addon_config ); |
| 295 | |
| 296 | $plugins_data[ $base_name ]['is_enabled'] = $is_enabled; |
| 297 | |
| 298 | $thumbnail_url = tutor()->url . 'assets/images/tutor-plugin.png'; |
| 299 | if ( file_exists( $addon['path'] . 'assets/images/thumbnail.png' ) ) { |
| 300 | $thumbnail_url = $addon['url'] . 'assets/images/thumbnail.png'; |
| 301 | } elseif ( file_exists( $addon['path'] . 'assets/images/thumbnail.jpg' ) ) { |
| 302 | $thumbnail_url = $addon['url'] . 'assets/images/thumbnail.jpg'; |
| 303 | } elseif ( file_exists( $addon['path'] . 'assets/images/thumbnail.svg' ) ) { |
| 304 | $thumbnail_url = $addon['url'] . 'assets/images/thumbnail.svg'; |
| 305 | } |
| 306 | |
| 307 | $plugins_data[ $base_name ]['thumb_url'] = $thumbnail_url; |
| 308 | |
| 309 | /** |
| 310 | * Checking if there any dependant plugin exists |
| 311 | */ |
| 312 | $depends = tutor_utils()->array_get( 'depend_plugins', $addon ); |
| 313 | $plugins_required = array(); |
| 314 | if ( tutor_utils()->count( $depends ) ) { |
| 315 | foreach ( $depends as $plugin_base => $plugin_name ) { |
| 316 | if ( ! is_plugin_active( $plugin_base ) ) { |
| 317 | $plugins_required[ $plugin_base ] = $plugin_name; |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | $depended_plugins = array(); |
| 323 | foreach ( $plugins_required as $required_plugin ) { |
| 324 | array_push( $depended_plugins, $required_plugin ); |
| 325 | } |
| 326 | |
| 327 | $plugins_data[ $base_name ]['plugins_required'] = $depended_plugins; |
| 328 | |
| 329 | // Check if it's notifications. |
| 330 | if ( function_exists( 'tutor_notifications' ) && $base_name == tutor_notifications()->basename ) { |
| 331 | |
| 332 | $required = array(); |
| 333 | version_compare( PHP_VERSION, '7.2.5', '>=' ) ? 0 : $required[] = __( 'PHP 7.2.5 or greater is required', 'tutor' ); |
| 334 | ! is_ssl() ? $required[] = __( 'SSL certificate', 'tutor' ) : 0; |
| 335 | |
| 336 | foreach ( array( 'curl', 'gmp', 'mbstring', 'openssl' ) as $ext ) { |
| 337 | ! extension_loaded( $ext ) ? $required[] = 'PHP extension <strong>' . $ext . '</strong>' : 0; |
| 338 | } |
| 339 | |
| 340 | $plugins_data[ $base_name ]['ext_required'] = $required; |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | $prepared_addons = array(); |
| 346 | foreach ( $plugins_data as $tutor_addon ) { |
| 347 | array_push( $prepared_addons, $tutor_addon ); |
| 348 | } |
| 349 | |
| 350 | return $prepared_addons; |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * Get all notifications |
| 355 | */ |
| 356 | public function tutor_get_all_addons() { |
| 357 | |
| 358 | // Check and verify the request. |
| 359 | tutor_utils()->checking_nonce(); |
| 360 | |
| 361 | // All good, let's proceed. |
| 362 | $all_addons = $this->prepare_addons_data(); |
| 363 | |
| 364 | wp_send_json_success( |
| 365 | array( |
| 366 | 'addons' => $all_addons, |
| 367 | ) |
| 368 | ); |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * Method for enable / disable addons |
| 373 | */ |
| 374 | public function addon_enable_disable() { |
| 375 | |
| 376 | if ( ! current_user_can( 'manage_options' ) ) { |
| 377 | wp_send_json_error( array( 'message' => __( 'Access Denied', 'tutor' ) ) ); |
| 378 | } |
| 379 | |
| 380 | $addonsConfig = maybe_unserialize( get_option( 'tutor_addons_config' ) ); |
| 381 | |
| 382 | // $isEnable = (bool) sanitize_text_field( tutor_utils()->avalue_dot( 'isEnable', $_POST ) ); |
| 383 | // $addonFieldName = sanitize_text_field( tutor_utils()->avalue_dot( 'addonFieldName', $_POST ) ); |
| 384 | $addonFieldNames = json_decode( stripslashes( ( tutor_utils()->avalue_dot( 'addonFieldNames', $_POST ) ) ), true ); |
| 385 | |
| 386 | foreach ( $addonFieldNames as $addonFieldName => $isEnable ) { |
| 387 | do_action( 'tutor_addon_before_enable_disable' ); |
| 388 | if ( $isEnable ) { |
| 389 | do_action( "tutor_addon_before_enable_{$addonFieldName}" ); |
| 390 | do_action( 'tutor_addon_before_enable', $addonFieldName ); |
| 391 | $addonsConfig[ $addonFieldName ]['is_enable'] = 1; |
| 392 | update_option( 'tutor_addons_config', $addonsConfig ); |
| 393 | |
| 394 | do_action( 'tutor_addon_after_enable', $addonFieldName ); |
| 395 | do_action( "tutor_addon_after_enable_{$addonFieldName}" ); |
| 396 | } else { |
| 397 | do_action( "tutor_addon_before_disable_{$addonFieldName}" ); |
| 398 | do_action( 'tutor_addon_before_disable', $addonFieldName ); |
| 399 | $addonsConfig[ $addonFieldName ]['is_enable'] = 0; |
| 400 | update_option( 'tutor_addons_config', $addonsConfig ); |
| 401 | |
| 402 | do_action( 'tutor_addon_after_disable', $addonFieldName ); |
| 403 | do_action( "tutor_addon_after_disable_{$addonFieldName}" ); |
| 404 | } |
| 405 | do_action( 'tutor_addon_after_enable_disable' ); |
| 406 | } |
| 407 | |
| 408 | wp_send_json_success(); |
| 409 | } |
| 410 | |
| 411 | /** |
| 412 | * Process ajax login |
| 413 | * |
| 414 | * @since v.1.6.3 |
| 415 | */ |
| 416 | public function process_ajax_login() { |
| 417 | tutor_utils()->checking_nonce(); |
| 418 | |
| 419 | $username = tutor_utils()->array_get( 'log', $_POST ); |
| 420 | $password = tutor_utils()->array_get( 'pwd', $_POST ); |
| 421 | $redirect_to = tutor_utils()->array_get( 'redirect_to', $_POST ); |
| 422 | |
| 423 | try { |
| 424 | $creds = array( |
| 425 | 'user_login' => trim( wp_unslash( $username ) ), |
| 426 | 'user_password' => $password, |
| 427 | 'remember' => isset( $_POST['rememberme'] ), |
| 428 | ); |
| 429 | |
| 430 | $validation_error = new \WP_Error(); |
| 431 | $validation_error = apply_filters( 'tutor_process_login_errors', $validation_error, $creds['user_login'], $creds['user_password'] ); |
| 432 | |
| 433 | if ( $validation_error->get_error_code() ) { |
| 434 | wp_send_json_error( |
| 435 | array( |
| 436 | 'message' => $validation_error->get_error_message(), |
| 437 | ) |
| 438 | ); |
| 439 | } |
| 440 | |
| 441 | if ( empty( $creds['user_login'] ) ) { |
| 442 | wp_send_json_error( |
| 443 | array( |
| 444 | 'message' => __( 'Username is required.', 'tutor' ), |
| 445 | ) |
| 446 | ); |
| 447 | } |
| 448 | |
| 449 | // On multisite, ensure user exists on current site, if not add them before allowing login. |
| 450 | if ( is_multisite() ) { |
| 451 | $user_data = get_user_by( is_email( $creds['user_login'] ) ? 'email' : 'login', $creds['user_login'] ); |
| 452 | |
| 453 | if ( $user_data && ! is_user_member_of_blog( $user_data->ID, get_current_blog_id() ) ) { |
| 454 | add_user_to_blog( get_current_blog_id(), $user_data->ID, 'customer' ); |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | // Perform the login. |
| 459 | $user = wp_signon( apply_filters( 'tutor_login_credentials', $creds ), is_ssl() ); |
| 460 | |
| 461 | if ( is_wp_error( $user ) ) { |
| 462 | wp_send_json_error( |
| 463 | array( |
| 464 | 'message' => $user->get_error_message(), |
| 465 | ) |
| 466 | ); |
| 467 | } else { |
| 468 | do_action( 'tutor_after_login_success', $user->ID ); |
| 469 | // since 1.9.8 do enroll if guest attempt to enroll |
| 470 | if ( ! empty( $_POST['tutor_course_enroll_attempt'] ) && is_a( $user, 'WP_User' ) ) { |
| 471 | do_action( 'tutor_do_enroll_after_login_if_attempt', $_POST['tutor_course_enroll_attempt'], $user->ID ); |
| 472 | } |
| 473 | wp_send_json_success( |
| 474 | array( |
| 475 | 'redirect_to' => apply_filters( 'tutor_login_redirect_url', $redirect_to, $user ), |
| 476 | ) |
| 477 | ); |
| 478 | } |
| 479 | } catch ( \Exception $e ) { |
| 480 | do_action( 'tutor_login_failed' ); |
| 481 | wp_send_json_error( apply_filters( 'login_errors', $e->getMessage() ) ); |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | /** |
| 486 | * Create/Update announcement |
| 487 | * |
| 488 | * @since v.1.7.9 |
| 489 | */ |
| 490 | public function create_or_update_annoucement() { |
| 491 | tutor_utils()->checking_nonce(); |
| 492 | |
| 493 | $error = array(); |
| 494 | $course_id = sanitize_text_field( $_POST['tutor_announcement_course'] ); |
| 495 | $announcement_title = sanitize_text_field( $_POST['tutor_announcement_title'] ); |
| 496 | $announcement_summary = sanitize_textarea_field( $_POST['tutor_announcement_summary'] ); |
| 497 | |
| 498 | // Check if user can manage this announcment |
| 499 | if ( ! tutor_utils()->can_user_manage( 'course', $course_id ) ) { |
| 500 | wp_send_json_error( array( 'message' => __( 'Access Denied', 'tutor' ) ) ); |
| 501 | } |
| 502 | |
| 503 | // set data and sanitize it |
| 504 | $form_data = array( |
| 505 | 'post_type' => 'tutor_announcements', |
| 506 | 'post_title' => $announcement_title, |
| 507 | 'post_content' => $announcement_summary, |
| 508 | 'post_parent' => $course_id, |
| 509 | 'post_status' => 'publish', |
| 510 | ); |
| 511 | |
| 512 | if ( isset( $_POST['announcement_id'] ) ) { |
| 513 | $form_data['ID'] = sanitize_text_field( $_POST['announcement_id'] ); |
| 514 | } |
| 515 | |
| 516 | // validation message set |
| 517 | if ( empty( $form_data['post_parent'] ) ) { |
| 518 | $error['post_parent'] = __( 'Course name required', 'tutor' ); |
| 519 | |
| 520 | } |
| 521 | |
| 522 | if ( empty( $form_data['post_title'] ) ) { |
| 523 | $error['post_title'] = __( 'Announcement title required', 'tutor' ); |
| 524 | } |
| 525 | |
| 526 | if ( empty( $form_data['post_content'] ) ) { |
| 527 | $error['post_content'] = __( 'Announcement summary required', 'tutor' ); |
| 528 | |
| 529 | } |
| 530 | |
| 531 | if ( empty( $form_data['post_content'] ) ) { |
| 532 | $error['post_content'] = __( 'Announcement summary required', 'tutor' ); |
| 533 | |
| 534 | } |
| 535 | |
| 536 | // If validation fails |
| 537 | if ( count( $error ) > 0 ) { |
| 538 | wp_send_json_error( |
| 539 | array( |
| 540 | 'message' => __( 'All fields required!', 'tutor' ), |
| 541 | 'fields' => $error, |
| 542 | ) |
| 543 | ); |
| 544 | } |
| 545 | |
| 546 | // insert or update post |
| 547 | $post_id = wp_insert_post( $form_data ); |
| 548 | if ( $post_id > 0 ) { |
| 549 | $announcement = get_post( $post_id ); |
| 550 | $action_type = sanitize_textarea_field( $_POST['action_type'] ); |
| 551 | |
| 552 | do_action( 'tutor_announcements/after/save', $post_id, $announcement, $action_type ); |
| 553 | |
| 554 | $resp_message = $action_type == 'create' ? __( 'Announcement created successfully', 'tutor' ) : __( 'Announcement updated successfully', 'tutor' ); |
| 555 | wp_send_json_success( array( 'message' => $resp_message ) ); |
| 556 | } |
| 557 | |
| 558 | wp_send_json_error( array( 'message' => __( 'Something Went Wrong!', 'tutor' ) ) ); |
| 559 | } |
| 560 | |
| 561 | /** |
| 562 | * Delete announcement |
| 563 | * |
| 564 | * @since v.1.7.9 |
| 565 | */ |
| 566 | public function delete_annoucement() { |
| 567 | $announcement_id = sanitize_text_field( $_POST['announcement_id'] ); |
| 568 | tutor_utils()->checking_nonce(); |
| 569 | |
| 570 | if ( ! tutor_utils()->can_user_manage( 'announcement', $announcement_id ) ) { |
| 571 | wp_send_json_error( array( 'message' => __( 'Access Denied', 'tutor' ) ) ); |
| 572 | } |
| 573 | |
| 574 | $delete = wp_delete_post( $announcement_id ); |
| 575 | if ( $delete ) { |
| 576 | wp_send_json_success( array( 'message' => __( 'Announcement deleted successfully', 'tutor' ) ) ); |
| 577 | } |
| 578 | |
| 579 | wp_send_json_error( array( 'message' => __( 'Announcement delete failed', 'tutor' ) ) ); |
| 580 | } |
| 581 | } |
| 582 |