Addons.php
5 years ago
Admin.php
5 years ago
Ajax.php
5 years ago
Assets.php
5 years ago
Course.php
5 years ago
Course_Filter.php
5 years ago
Course_Settings_Tabs.php
5 years ago
Course_Widget.php
5 years ago
Custom_Validation.php
5 years ago
Dashboard.php
5 years ago
Email.php
5 years ago
FormHandler.php
5 years ago
Frontend.php
5 years ago
Gutenberg.php
5 years ago
Instructor.php
5 years ago
Instructors_List.php
5 years ago
Lesson.php
5 years ago
Options.php
5 years ago
Post_types.php
5 years ago
Private_Course_Access.php
5 years ago
Q_and_A.php
5 years ago
Question_Answers_List.php
5 years ago
Quiz.php
5 years ago
Quiz_Attempts_List.php
5 years ago
RestAPI.php
5 years ago
Rewrite_Rules.php
5 years ago
Shortcode.php
5 years ago
Student.php
5 years ago
Students_List.php
5 years ago
Taxonomies.php
5 years ago
Template.php
5 years ago
Theme_Compatibility.php
5 years ago
Tools.php
5 years ago
Tutor.php
5 years ago
TutorEDD.php
5 years ago
Tutor_Base.php
5 years ago
Tutor_List_Table.php
5 years ago
Tutor_Setup.php
5 years ago
Upgrader.php
5 years ago
User.php
5 years ago
Utils.php
5 years ago
Video_Stream.php
5 years ago
Withdraw.php
5 years ago
Withdraw_Requests_List.php
5 years ago
WooCommerce.php
5 years ago
Ajax.php
540 lines
| 1 | <?php |
| 2 | namespace TUTOR; |
| 3 | |
| 4 | if ( ! defined( 'ABSPATH' ) ) |
| 5 | exit; |
| 6 | |
| 7 | class Ajax{ |
| 8 | public function __construct() { |
| 9 | |
| 10 | add_action('wp_ajax_sync_video_playback', array($this, 'sync_video_playback')); |
| 11 | add_action('wp_ajax_nopriv_sync_video_playback', array($this, 'sync_video_playback_noprev')); |
| 12 | add_action('wp_ajax_tutor_place_rating', array($this, 'tutor_place_rating')); |
| 13 | |
| 14 | add_action('wp_ajax_tutor_ask_question', array($this, 'tutor_ask_question')); |
| 15 | add_action('wp_ajax_tutor_add_answer', array($this, 'tutor_add_answer')); |
| 16 | |
| 17 | add_action('wp_ajax_tutor_course_add_to_wishlist', array($this, 'tutor_course_add_to_wishlist')); |
| 18 | add_action('wp_ajax_nopriv_tutor_course_add_to_wishlist', array($this, 'tutor_course_add_to_wishlist')); |
| 19 | |
| 20 | /** |
| 21 | * Addon Enable Disable Control |
| 22 | */ |
| 23 | add_action('wp_ajax_addon_enable_disable', array($this, 'addon_enable_disable')); |
| 24 | |
| 25 | /** |
| 26 | * Update Rating/review |
| 27 | * @since v.1.4.0 |
| 28 | */ |
| 29 | add_action('wp_ajax_tutor_load_edit_review_modal', array($this, 'tutor_load_edit_review_modal')); |
| 30 | add_action('wp_ajax_tutor_update_review_modal', array($this, 'tutor_update_review_modal')); |
| 31 | |
| 32 | /** |
| 33 | * Ajax login |
| 34 | * @since v.1.6.3 |
| 35 | */ |
| 36 | add_action('wp_ajax_nopriv_tutor_user_login', array($this, 'process_ajax_login')); |
| 37 | |
| 38 | /** |
| 39 | * Announcement |
| 40 | * @since v.1.7.9 |
| 41 | */ |
| 42 | add_action("wp_ajax_tutor_announcement_create", array($this,'create_or_update_annoucement')); |
| 43 | add_action("wp_ajax_tutor_announcement_delete", array($this,'delete_annoucement')); |
| 44 | } |
| 45 | |
| 46 | |
| 47 | |
| 48 | /** |
| 49 | * Update video information and data when necessary |
| 50 | * |
| 51 | * @since v.1.0.0 |
| 52 | */ |
| 53 | public function sync_video_playback(){ |
| 54 | tutor_utils()->checking_nonce(); |
| 55 | |
| 56 | $user_id = get_current_user_id(); |
| 57 | $post_id = isset($_POST['post_id']) ? sanitize_text_field($_POST['post_id']) : 0; |
| 58 | $duration = sanitize_text_field($_POST['duration']); |
| 59 | $currentTime = sanitize_text_field($_POST['currentTime']); |
| 60 | |
| 61 | if(!tutils()->has_enrolled_content_access('lesson', $post_id)) { |
| 62 | wp_send_json_error(array('message'=>__('Access Denied', 'tutor'))); |
| 63 | exit; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Update posts attached video |
| 68 | */ |
| 69 | $video = tutor_utils()->get_video($post_id); |
| 70 | |
| 71 | if ($duration) { |
| 72 | $video['duration_sec'] = $duration; //secs |
| 73 | $video['playtime'] = tutor_utils()->playtime_string( $duration ); |
| 74 | $video['runtime'] = tutor_utils()->playtime_array( $duration ); |
| 75 | } |
| 76 | tutor_utils()->update_video($post_id, $video); |
| 77 | |
| 78 | /** |
| 79 | * Sync Lesson Reading Info by Users |
| 80 | */ |
| 81 | |
| 82 | $best_watch_time = tutor_utils()->get_lesson_reading_info($post_id, $user_id, 'video_best_watched_time'); |
| 83 | if ($best_watch_time < $currentTime){ |
| 84 | tutor_utils()->update_lesson_reading_info($post_id, $user_id, 'video_best_watched_time', $currentTime); |
| 85 | } |
| 86 | |
| 87 | if (tutor_utils()->avalue_dot('is_ended', $_POST)){ |
| 88 | tutor_utils()->mark_lesson_complete($post_id); |
| 89 | } |
| 90 | exit(); |
| 91 | } |
| 92 | |
| 93 | public function sync_video_playback_noprev(){ |
| 94 | |
| 95 | } |
| 96 | |
| 97 | |
| 98 | public function tutor_place_rating(){ |
| 99 | global $wpdb; |
| 100 | |
| 101 | tutils()->checking_nonce(); |
| 102 | |
| 103 | $rating = sanitize_text_field(tutor_utils()->avalue_dot('rating', $_POST)); |
| 104 | $course_id = sanitize_text_field(tutor_utils()->avalue_dot('course_id', $_POST)); |
| 105 | $review = wp_kses_post(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(!tutils()->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($wpdb->prepare("select comment_ID from {$wpdb->comments} WHERE comment_post_ID = %d AND user_id = %d AND comment_type = 'tutor_course_rating' LIMIT 1;", $course_id, $user_id)); |
| 122 | |
| 123 | $review_ID = $previous_rating_id; |
| 124 | if ( $previous_rating_id){ |
| 125 | $wpdb->update( $wpdb->comments, array('comment_content' => esc_sql( $review ) ), |
| 126 | array('comment_ID' => $previous_rating_id) |
| 127 | ); |
| 128 | |
| 129 | $rating_info = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->commentmeta} WHERE comment_id = %d AND meta_key = 'tutor_rating'; ", $previous_rating_id)); |
| 130 | if ($rating_info){ |
| 131 | $wpdb->update( $wpdb->commentmeta, array('meta_value' => $rating), array('comment_id' => $previous_rating_id, 'meta_key' => 'tutor_rating') ); |
| 132 | }else{ |
| 133 | $wpdb->insert( $wpdb->commentmeta, array('comment_id' => $previous_rating_id, 'meta_key' => 'tutor_rating', 'meta_value' => $rating) ); |
| 134 | } |
| 135 | }else{ |
| 136 | $data = array( |
| 137 | 'comment_post_ID' => esc_sql( $course_id ) , |
| 138 | 'comment_approved' => 'approved', |
| 139 | 'comment_type' => 'tutor_course_rating', |
| 140 | 'comment_date' => $date, |
| 141 | 'comment_date_gmt' => get_gmt_from_date($date), |
| 142 | 'user_id' => $user_id, |
| 143 | 'comment_author' => $user->user_login, |
| 144 | 'comment_agent' => 'TutorLMSPlugin', |
| 145 | ); |
| 146 | if ($review){ |
| 147 | $data['comment_content'] = $review; |
| 148 | } |
| 149 | |
| 150 | $wpdb->insert($wpdb->comments, $data); |
| 151 | $comment_id = (int) $wpdb->insert_id; |
| 152 | $review_ID = $comment_id; |
| 153 | |
| 154 | if ($comment_id){ |
| 155 | $result = $wpdb->insert( $wpdb->commentmeta, array( |
| 156 | 'comment_id' => $comment_id, |
| 157 | 'meta_key' => 'tutor_rating', |
| 158 | 'meta_value' => $rating |
| 159 | ) ); |
| 160 | |
| 161 | do_action('tutor_after_rating_placed', $comment_id); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | $data = array('msg' => __('Rating placed success', 'tutor'), 'review_id' => $review_ID, 'review' => $review); |
| 166 | wp_send_json_success($data); |
| 167 | } |
| 168 | |
| 169 | public function tutor_ask_question(){ |
| 170 | tutor_utils()->checking_nonce(); |
| 171 | |
| 172 | global $wpdb; |
| 173 | |
| 174 | $course_id = (int) sanitize_text_field($_POST['tutor_course_id']); |
| 175 | $question_title = sanitize_text_field($_POST['question_title']); |
| 176 | $question = wp_kses_post($_POST['question']); |
| 177 | |
| 178 | if(!tutils()->has_enrolled_content_access('course', $course_id)) { |
| 179 | wp_send_json_error(array('message'=>__('Access Denied', 'tutor'))); |
| 180 | exit; |
| 181 | } |
| 182 | |
| 183 | if (empty($question) || empty($question_title)){ |
| 184 | wp_send_json_error(__('Empty question title or body', 'tutor')); |
| 185 | } |
| 186 | |
| 187 | $user_id = get_current_user_id(); |
| 188 | $user = get_userdata($user_id); |
| 189 | $date = date("Y-m-d H:i:s", tutor_time()); |
| 190 | |
| 191 | do_action('tutor_before_add_question', $course_id); |
| 192 | $data = apply_filters('tutor_add_question_data', array( |
| 193 | 'comment_post_ID' => $course_id, |
| 194 | 'comment_author' => $user->user_login, |
| 195 | 'comment_date' => $date, |
| 196 | 'comment_date_gmt' => get_gmt_from_date($date), |
| 197 | 'comment_content' => $question, |
| 198 | 'comment_approved' => 'waiting_for_answer', |
| 199 | 'comment_agent' => 'TutorLMSPlugin', |
| 200 | 'comment_type' => 'tutor_q_and_a', |
| 201 | 'user_id' => $user_id, |
| 202 | )); |
| 203 | |
| 204 | $wpdb->insert($wpdb->comments, $data); |
| 205 | $comment_id = (int) $wpdb->insert_id; |
| 206 | |
| 207 | if ($comment_id){ |
| 208 | $result = $wpdb->insert( $wpdb->commentmeta, array( |
| 209 | 'comment_id' => $comment_id, |
| 210 | 'meta_key' => 'tutor_question_title', |
| 211 | 'meta_value' => $question_title |
| 212 | ) ); |
| 213 | } |
| 214 | do_action('tutor_after_add_question', $course_id, $comment_id); |
| 215 | |
| 216 | wp_send_json_success(__('Question has been added successfully', 'tutor')); |
| 217 | } |
| 218 | |
| 219 | |
| 220 | public function tutor_add_answer(){ |
| 221 | tutor_utils()->checking_nonce(); |
| 222 | global $wpdb; |
| 223 | |
| 224 | $answer = wp_kses_post($_POST['answer']); |
| 225 | if ( ! $answer){ |
| 226 | wp_send_json_error(__('Please write answer', 'tutor')); |
| 227 | } |
| 228 | |
| 229 | $question_id = (int) sanitize_text_field($_POST['question_id']); |
| 230 | $question = tutor_utils()->get_qa_question($question_id); |
| 231 | |
| 232 | $user_id = get_current_user_id(); |
| 233 | $user = get_userdata($user_id); |
| 234 | $date = date("Y-m-d H:i:s", tutor_time()); |
| 235 | |
| 236 | if(!tutils()->has_enrolled_content_access('qa_question', $question_id)) { |
| 237 | wp_send_json_error(array('message'=>__('Access Denied', 'tutor'))); |
| 238 | exit; |
| 239 | } |
| 240 | |
| 241 | do_action('tutor_before_answer_to_question'); |
| 242 | $data = apply_filters('tutor_add_answer_data', array( |
| 243 | 'comment_post_ID' => $question->comment_post_ID, |
| 244 | 'comment_author' => $user->user_login, |
| 245 | 'comment_date' => $date, |
| 246 | 'comment_date_gmt' => get_gmt_from_date($date), |
| 247 | 'comment_content' => $answer, |
| 248 | 'comment_approved' => 'approved', |
| 249 | 'comment_agent' => 'TutorLMSPlugin', |
| 250 | 'comment_type' => 'tutor_q_and_a', |
| 251 | 'comment_parent' => $question_id, |
| 252 | 'user_id' => $user_id, |
| 253 | )); |
| 254 | |
| 255 | $wpdb->insert($wpdb->comments, $data); |
| 256 | $comment_id = (int) $wpdb->insert_id; |
| 257 | do_action('tutor_after_answer_to_question', $comment_id); |
| 258 | |
| 259 | wp_send_json_success(__('Answer has been added successfully', 'tutor')); |
| 260 | } |
| 261 | |
| 262 | |
| 263 | public function tutor_course_add_to_wishlist(){ |
| 264 | tutils()->checking_nonce(); |
| 265 | |
| 266 | $course_id = (int) sanitize_text_field($_POST['course_id']); |
| 267 | if ( ! is_user_logged_in()){ |
| 268 | wp_send_json_error(array('redirect_to' => wp_login_url( wp_get_referer() ) ) ); |
| 269 | } |
| 270 | global $wpdb; |
| 271 | |
| 272 | $user_id = get_current_user_id(); |
| 273 | $if_added_to_list = $wpdb->get_row($wpdb->prepare("SELECT * from {$wpdb->usermeta} WHERE user_id = %d AND meta_key = '_tutor_course_wishlist' AND meta_value = %d;", $user_id, $course_id)); |
| 274 | |
| 275 | if ( $if_added_to_list){ |
| 276 | $wpdb->delete($wpdb->usermeta, array('user_id' => $user_id, 'meta_key' => '_tutor_course_wishlist', 'meta_value' => $course_id )); |
| 277 | wp_send_json_success(array('status' => 'removed', 'msg' => __('Course removed from wish list', 'tutor'))); |
| 278 | }else{ |
| 279 | add_user_meta($user_id, '_tutor_course_wishlist', $course_id); |
| 280 | wp_send_json_success(array('status' => 'added', 'msg' => __('Course added to wish list', 'tutor'))); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Method for enable / disable addons |
| 286 | */ |
| 287 | public function addon_enable_disable(){ |
| 288 | |
| 289 | if(!current_user_can( 'manage_options' )) { |
| 290 | wp_send_json_error( array('message'=>__('Access Denied', 'tutor')) ); |
| 291 | } |
| 292 | |
| 293 | $addonsConfig = maybe_unserialize(get_option('tutor_addons_config')); |
| 294 | |
| 295 | $isEnable = (bool) sanitize_text_field(tutor_utils()->avalue_dot('isEnable', $_POST)); |
| 296 | $addonFieldName = sanitize_text_field(tutor_utils()->avalue_dot('addonFieldName', $_POST)); |
| 297 | |
| 298 | do_action('tutor_addon_before_enable_disable'); |
| 299 | if ($isEnable){ |
| 300 | do_action("tutor_addon_before_enable_{$addonFieldName}"); |
| 301 | do_action('tutor_addon_before_enable', $addonFieldName); |
| 302 | $addonsConfig[$addonFieldName]['is_enable'] = 1; |
| 303 | update_option('tutor_addons_config', $addonsConfig); |
| 304 | |
| 305 | do_action('tutor_addon_after_enable', $addonFieldName); |
| 306 | do_action("tutor_addon_after_enable_{$addonFieldName}"); |
| 307 | }else{ |
| 308 | do_action("tutor_addon_before_disable_{$addonFieldName}"); |
| 309 | do_action('tutor_addon_before_disable', $addonFieldName); |
| 310 | $addonsConfig[$addonFieldName]['is_enable'] = 0; |
| 311 | update_option('tutor_addons_config', $addonsConfig); |
| 312 | |
| 313 | do_action('tutor_addon_after_disable', $addonFieldName); |
| 314 | do_action("tutor_addon_after_disable_{$addonFieldName}"); |
| 315 | } |
| 316 | |
| 317 | do_action('tutor_addon_after_enable_disable'); |
| 318 | wp_send_json_success(); |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * Load review edit form |
| 323 | * @since v.1.4.0 |
| 324 | */ |
| 325 | public function tutor_load_edit_review_modal(){ |
| 326 | tutor_utils()->checking_nonce(); |
| 327 | |
| 328 | $review_id = (int) sanitize_text_field(tutils()->array_get('review_id', $_POST)); |
| 329 | $rating = tutils()->get_rating_by_id($review_id); |
| 330 | |
| 331 | if(!tutils()->has_enrolled_content_access('review', $review_id)) { |
| 332 | wp_send_json_error(array('message'=>__('Access Denied', 'tutor'))); |
| 333 | exit; |
| 334 | } |
| 335 | |
| 336 | ob_start(); |
| 337 | tutor_load_template('dashboard.reviews.edit-review-form', array('rating' => $rating)); |
| 338 | $output = ob_get_clean(); |
| 339 | |
| 340 | wp_send_json_success(array('output' => $output)); |
| 341 | } |
| 342 | |
| 343 | public function tutor_update_review_modal(){ |
| 344 | global $wpdb; |
| 345 | |
| 346 | tutor_utils()->checking_nonce(); |
| 347 | |
| 348 | $review_id = (int) sanitize_text_field(tutils()->array_get('review_id', $_POST)); |
| 349 | $rating = sanitize_text_field(tutor_utils()->avalue_dot('rating', $_POST)); |
| 350 | $review = wp_kses_post(tutor_utils()->avalue_dot('review', $_POST)); |
| 351 | |
| 352 | if(!tutils()->has_enrolled_content_access('review', $review_id)) { |
| 353 | wp_send_json_error(array('message'=>__('Access Denied', 'tutor'))); |
| 354 | exit; |
| 355 | } |
| 356 | |
| 357 | $is_exists = $wpdb->get_var($wpdb->prepare("SELECT comment_ID from {$wpdb->comments} WHERE comment_ID=%d AND comment_type = 'tutor_course_rating' ;", $review_id)); |
| 358 | |
| 359 | if ( $is_exists) { |
| 360 | $wpdb->update( $wpdb->comments, array( 'comment_content' => $review ), |
| 361 | array( 'comment_ID' => $review_id ) |
| 362 | ); |
| 363 | $wpdb->update( $wpdb->commentmeta, array( 'meta_value' => $rating ), |
| 364 | array( 'comment_id' => $review_id, 'meta_key' => 'tutor_rating' ) |
| 365 | ); |
| 366 | |
| 367 | do_action('tutor_after_review_update', $review_id, $is_exists); |
| 368 | |
| 369 | wp_send_json_success(); |
| 370 | } |
| 371 | wp_send_json_error(); |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * Process ajax login |
| 376 | * @since v.1.6.3 |
| 377 | */ |
| 378 | public function process_ajax_login(){ |
| 379 | tutils()->checking_nonce(); |
| 380 | |
| 381 | $username = tutils()->array_get('log', $_POST); |
| 382 | $password = tutils()->array_get('pwd', $_POST); |
| 383 | $redirect_to = tutils()->array_get('redirect_to', $_POST); |
| 384 | |
| 385 | try { |
| 386 | $creds = array( |
| 387 | 'user_login' => trim( wp_unslash( $username ) ), // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 388 | 'user_password' => $password, // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash |
| 389 | 'remember' => isset( $_POST['rememberme'] ), // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 390 | ); |
| 391 | |
| 392 | $validation_error = new \WP_Error(); |
| 393 | $validation_error = apply_filters( 'tutor_process_login_errors', $validation_error, $creds['user_login'], $creds['user_password'] ); |
| 394 | |
| 395 | if ( $validation_error->get_error_code() ) { |
| 396 | wp_send_json_error( '<strong>' . __( 'ERROR:', 'tutor' ) . '</strong> ' . $validation_error->get_error_message() ); |
| 397 | } |
| 398 | |
| 399 | if ( empty( $creds['user_login'] ) ) { |
| 400 | wp_send_json_error( '<strong>' . __( 'ERROR:', 'tutor' ) . '</strong> ' . __( 'Username is required.', 'tutor' ) ); |
| 401 | } |
| 402 | |
| 403 | // On multisite, ensure user exists on current site, if not add them before allowing login. |
| 404 | if ( is_multisite() ) { |
| 405 | $user_data = get_user_by( is_email( $creds['user_login'] ) ? 'email' : 'login', $creds['user_login'] ); |
| 406 | |
| 407 | if ( $user_data && ! is_user_member_of_blog( $user_data->ID, get_current_blog_id() ) ) { |
| 408 | add_user_to_blog( get_current_blog_id(), $user_data->ID, 'customer' ); |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | // Perform the login. |
| 413 | $user = wp_signon( apply_filters( 'tutor_login_credentials', $creds ), is_ssl() ); |
| 414 | |
| 415 | if ( is_wp_error( $user ) ) { |
| 416 | $message = $user->get_error_message(); |
| 417 | $message = str_replace( '<strong>' . esc_html( $creds['user_login'] ) . '</strong>', '<strong>' . esc_html( $creds['user_login'] ) . '</strong>', $message ); |
| 418 | |
| 419 | wp_send_json_error( $message ); |
| 420 | } else { |
| 421 | wp_send_json_success([ |
| 422 | 'redirect' => apply_filters('tutor_login_redirect_url', $redirect_to) |
| 423 | ]); |
| 424 | } |
| 425 | } catch ( \Exception $e ) { |
| 426 | wp_send_json_error( apply_filters( 'login_errors', $e->getMessage()) ); |
| 427 | do_action( 'tutor_login_failed' ); |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | /** |
| 432 | * Create/Update announcement |
| 433 | * @since v.1.7.9 |
| 434 | */ |
| 435 | public function create_or_update_annoucement() { |
| 436 | //prepare alert message |
| 437 | $create_success_msg = __("Announcement created successfully",'tutor'); |
| 438 | $update_success_msg = __("Announcement updated successfully",'tutor'); |
| 439 | $create_fail_msg = __("Announcement creation failed",'tutor'); |
| 440 | $update_fail_msg = __("Announcement update failed",'tutor'); |
| 441 | |
| 442 | $error = array(); |
| 443 | $response = array(); |
| 444 | tutils()->checking_nonce(); |
| 445 | |
| 446 | $course_id = sanitize_text_field($_POST['tutor_announcement_course']); |
| 447 | $announcement_title = sanitize_text_field($_POST['tutor_announcement_title']); |
| 448 | $announcement_summary = sanitize_textarea_field($_POST['tutor_announcement_summary']); |
| 449 | |
| 450 | if(!tutils()->can_user_manage('course', $course_id)) { |
| 451 | wp_send_json_error( array('message'=>__('Access Denied', 'tutor')) ); |
| 452 | } |
| 453 | |
| 454 | //set data and sanitize it |
| 455 | $form_data = array( |
| 456 | 'post_type' => 'tutor_announcements', |
| 457 | 'post_title' => $announcement_title, |
| 458 | 'post_content' => $announcement_summary, |
| 459 | 'post_parent' => $course_id, |
| 460 | 'post_status' => 'publish' |
| 461 | ); |
| 462 | |
| 463 | if (isset($_POST['announcement_id'])) { |
| 464 | $form_data['ID'] = sanitize_text_field($_POST['announcement_id']); |
| 465 | } |
| 466 | |
| 467 | //validation message set |
| 468 | if (empty($form_data['post_parent'])) { |
| 469 | $error['post_parent'] = __('Course name required','tutor'); |
| 470 | |
| 471 | } |
| 472 | |
| 473 | if (empty($form_data['post_title'])) { |
| 474 | $error['post_title'] = __('Announcement title required','tutor'); |
| 475 | } |
| 476 | |
| 477 | if (empty($form_data['post_content'])) { |
| 478 | $error['post_content'] = __('Announcement summary required','tutor'); |
| 479 | |
| 480 | } |
| 481 | |
| 482 | if (count($error)>0) { |
| 483 | $response['status'] = 'validation_error'; |
| 484 | $response['message'] = $error; |
| 485 | wp_send_json($response); |
| 486 | } else { |
| 487 | //insert or update post |
| 488 | $post_id = wp_insert_post($form_data); |
| 489 | if ($post_id > 0) { |
| 490 | $announcement = get_post($post_id); |
| 491 | $action_type = sanitize_textarea_field($_POST['action_type']); |
| 492 | $response['status'] = 'success'; |
| 493 | //set reponse message as per action type |
| 494 | $response['message'] = ($action_type == 'create') ? $create_success_msg : $update_success_msg; |
| 495 | |
| 496 | do_action('tutor_announcements/after/save', $post_id, $announcement, $action_type ); |
| 497 | |
| 498 | wp_send_json($response); |
| 499 | } else { |
| 500 | //failure message |
| 501 | $response['status'] = 'fail'; |
| 502 | if($_POST['action_type'] == 'create'){ |
| 503 | $response['message'] = $create_fail_msg; |
| 504 | } |
| 505 | if($_POST['action_type'] == 'update'){ |
| 506 | $response['message'] = $update_fail_msg; |
| 507 | } |
| 508 | wp_send_json($response); |
| 509 | } |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | /** |
| 514 | * Delete announcement |
| 515 | * @since v.1.7.9 |
| 516 | */ |
| 517 | public function delete_annoucement() { |
| 518 | $announcement_id = sanitize_text_field($_POST['announcement_id']); |
| 519 | tutils()->checking_nonce(); |
| 520 | |
| 521 | if(!tutils()->can_user_manage('announcement', $announcement_id)) { |
| 522 | wp_send_json_error( array('message'=>__('Access Denied', 'tutor')) ); |
| 523 | } |
| 524 | |
| 525 | $delete = wp_delete_post($announcement_id); |
| 526 | if ($delete) { |
| 527 | $response = array( |
| 528 | 'status' => 'success', |
| 529 | 'message' => __('Announcement deleted successfully','tutor') |
| 530 | ); |
| 531 | wp_send_json($response); |
| 532 | } else { |
| 533 | $response = array( |
| 534 | 'status' => 'fail', |
| 535 | 'message' => __('Announcement delete failed','tutor') |
| 536 | ); |
| 537 | wp_send_json($response); |
| 538 | } |
| 539 | } |
| 540 | } |