PluginProbe ʕ •ᴥ•ʔ
Tutor LMS – eLearning and online course solution / 2.1.6
Tutor LMS – eLearning and online course solution v2.1.6
3.9.14 3.9.13 3.9.12 3.9.11 trunk 1.0.0 1.0.0-alpha 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.2.0 1.2.1 1.2.11 1.2.12 1.2.13 1.2.20 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 1.7.3 1.7.4 1.7.5 1.7.6 1.7.7 1.7.8 1.7.9 1.8.0 1.8.1 1.8.10 1.8.2 1.8.3 1.8.4 1.8.5 1.8.6 1.8.7 1.8.8 1.8.9 1.9.0 1.9.1 1.9.10 1.9.11 1.9.12 1.9.13 1.9.14 1.9.15 1.9.16 1.9.2 1.9.3 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 2.0.1 2.0.10 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1.0 2.1.1 2.1.10 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.3.0 2.4.0 2.5.0 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 3.0.0 3.0.1 3.0.2 3.1.0 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.6.1 3.6.2 3.6.3 3.6.4 3.7.0 3.7.1 3.7.2 3.7.3 3.7.4 3.8.0 3.8.1 3.8.2 3.8.3 3.9.0 3.9.1 3.9.10 3.9.2 3.9.3 3.9.4 3.9.5 3.9.6 3.9.7 3.9.8 3.9.9
tutor / classes / Ajax.php
tutor / classes Last commit date
Addons.php 3 years ago Admin.php 3 years ago Ajax.php 3 years ago Announcements.php 3 years ago Assets.php 3 years ago Backend_Page_Trait.php 3 years ago Course.php 3 years ago Course_Embed.php 3 years ago Course_Filter.php 3 years ago Course_List.php 3 years ago Course_Settings_Tabs.php 3 years ago Course_Widget.php 3 years ago Custom_Validation.php 3 years ago Dashboard.php 3 years ago FormHandler.php 3 years ago Frontend.php 3 years ago Gutenberg.php 3 years ago Input.php 3 years ago Instructor.php 3 years ago Instructors_List.php 3 years ago Lesson.php 3 years ago Options_V2.php 3 years ago Post_types.php 3 years ago Private_Course_Access.php 3 years ago Q_and_A.php 3 years ago Question_Answers_List.php 3 years ago Quiz.php 3 years ago Quiz_Attempts_List.php 3 years ago RestAPI.php 3 years ago Reviews.php 3 years ago Rewrite_Rules.php 3 years ago Shortcode.php 3 years ago Student.php 3 years ago Students_List.php 3 years ago Taxonomies.php 3 years ago Template.php 3 years ago Theme_Compatibility.php 3 years ago Tools.php 3 years ago Tools_V2.php 3 years ago Tutor.php 3 years ago TutorEDD.php 3 years ago Tutor_Base.php 3 years ago Tutor_Setup.php 3 years ago Upgrader.php 3 years ago User.php 3 years ago Utils.php 3 years ago Video_Stream.php 3 years ago Withdraw.php 3 years ago Withdraw_Requests_List.php 3 years ago WooCommerce.php 3 years ago
Ajax.php
665 lines
1 <?php
2 /**
3 * Handle Ajax Request
4 *
5 * @package Tutor
6 * @author Themeum <support@themeum.com>
7 * @link https://themeum.com
8 * @since 1.0.0
9 */
10
11 namespace TUTOR;
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 use Tutor\Models\LessonModel;
18
19 /**
20 * Ajax Class
21 *
22 * @since 1.0.0
23 */
24 class Ajax {
25
26 const LOGIN_ERRORS_TRANSIENT_KEY = 'tutor_login_errors';
27 /**
28 * Constructor
29 *
30 * @since 1.0.0
31 * @return void
32 */
33 public function __construct() {
34
35 add_action( 'wp_ajax_sync_video_playback', array( $this, 'sync_video_playback' ) );
36 add_action( 'wp_ajax_nopriv_sync_video_playback', array( $this, 'sync_video_playback_noprev' ) );
37 add_action( 'wp_ajax_tutor_place_rating', array( $this, 'tutor_place_rating' ) );
38 add_action( 'wp_ajax_delete_tutor_review', array( $this, 'delete_tutor_review' ) );
39
40 add_action( 'wp_ajax_tutor_course_add_to_wishlist', array( $this, 'tutor_course_add_to_wishlist' ) );
41 add_action( 'wp_ajax_nopriv_tutor_course_add_to_wishlist', array( $this, 'tutor_course_add_to_wishlist' ) );
42
43 /**
44 * Get all addons
45 */
46 add_action( 'wp_ajax_tutor_get_all_addons', array( $this, 'tutor_get_all_addons' ) );
47
48 /**
49 * Addon Enable Disable Control
50 */
51 add_action( 'wp_ajax_addon_enable_disable', array( $this, 'addon_enable_disable' ) );
52
53 /**
54 * Ajax login
55 *
56 * @since v.1.6.3
57 */
58 add_action( 'tutor_action_tutor_user_login', array( $this, 'process_tutor_login' ) );
59
60 /**
61 * Announcement
62 *
63 * @since v.1.7.9
64 */
65 add_action( 'wp_ajax_tutor_announcement_create', array( $this, 'create_or_update_annoucement' ) );
66 add_action( 'wp_ajax_tutor_announcement_delete', array( $this, 'delete_annoucement' ) );
67 }
68
69
70
71 /**
72 * Update video information and data when necessary
73 *
74 * @since 1.0.0
75 * @return void
76 */
77 public function sync_video_playback() {
78 tutor_utils()->checking_nonce();
79
80 $user_id = get_current_user_id();
81 $post_id = Input::post( 'post_id', 0, Input::TYPE_INT );
82 $duration = Input::post( 'duration' );
83 $current_time = Input::post( 'currentTime' );
84
85 if ( ! tutor_utils()->has_enrolled_content_access( 'lesson', $post_id ) ) {
86 wp_send_json_error( array( 'message' => __( 'Access Denied', 'tutor' ) ) );
87 exit;
88 }
89
90 /**
91 * Update posts attached video
92 */
93 $video = tutor_utils()->get_video( $post_id );
94
95 if ( $duration ) {
96 $video['duration_sec'] = $duration; // Duration in sec.
97 $video['playtime'] = tutor_utils()->playtime_string( $duration );
98 $video['runtime'] = tutor_utils()->playtime_array( $duration );
99 }
100 tutor_utils()->update_video( $post_id, $video );
101
102 /**
103 * Sync Lesson Reading Info by Users
104 */
105
106 $best_watch_time = tutor_utils()->get_lesson_reading_info( $post_id, $user_id, 'video_best_watched_time' );
107 if ( $best_watch_time < $current_time ) {
108 LessonModel::update_lesson_reading_info( $post_id, $user_id, 'video_best_watched_time', $current_time );
109 }
110
111 if ( Input::post( 'is_ended', false, Input::TYPE_BOOL ) ) {
112 LessonModel::mark_lesson_complete( $post_id );
113 }
114 exit();
115 }
116
117 /**
118 * Video playback callback for noprev
119 *
120 * @since 1.0.0
121 * @return void
122 */
123 public function sync_video_playback_noprev() {
124
125 }
126
127 /**
128 * Place rating
129 *
130 * @since 1.0.0
131 * @return void
132 */
133 public function tutor_place_rating() {
134 tutor_utils()->checking_nonce();
135
136 global $wpdb;
137
138 $moderation = tutor_utils()->get_option( 'enable_course_review_moderation', false, true, true );
139 $rating = Input::post( 'tutor_rating_gen_input', 0, Input::TYPE_INT );
140 $course_id = Input::post( 'course_id' );
141 $review = Input::post( 'review', '', Input::TYPE_TEXTAREA );
142
143 $rating <= 0 ? $rating = 1 : 0;
144 $rating > 5 ? $rating = 5 : 0;
145
146 $user_id = get_current_user_id();
147 $user = get_userdata( $user_id );
148 $date = date( 'Y-m-d H:i:s', tutor_time() );
149
150 if ( ! tutor_utils()->has_enrolled_content_access( 'course', $course_id ) ) {
151 wp_send_json_error( array( 'message' => __( 'Access Denied', 'tutor' ) ) );
152 exit;
153 }
154
155 do_action( 'tutor_before_rating_placed' );
156
157 $previous_rating_id = $wpdb->get_var(
158 $wpdb->prepare(
159 "SELECT comment_ID
160 from {$wpdb->comments}
161 WHERE comment_post_ID = %d AND
162 user_id = %d AND
163 comment_type = 'tutor_course_rating'
164 LIMIT 1;",
165 $course_id,
166 $user_id
167 )
168 );
169
170 $review_id = $previous_rating_id;
171 if ( $previous_rating_id ) {
172 $wpdb->update(
173 $wpdb->comments,
174 array(
175 'comment_content' => $review,
176 'comment_approved' => $moderation ? 'hold' : 'approved',
177 'comment_date' => $date,
178 'comment_date_gmt' => get_gmt_from_date( $date ),
179 ),
180 array( 'comment_ID' => $previous_rating_id )
181 );
182
183 $rating_info = $wpdb->get_row(
184 $wpdb->prepare(
185 "SELECT * FROM {$wpdb->commentmeta}
186 WHERE comment_id = %d
187 AND meta_key = 'tutor_rating'; ",
188 $previous_rating_id
189 )
190 );
191
192 if ( $rating_info ) {
193 $wpdb->update(
194 $wpdb->commentmeta,
195 array( 'meta_value' => $rating ),
196 array(
197 'comment_id' => $previous_rating_id,
198 'meta_key' => 'tutor_rating',
199 )
200 );
201 } else {
202 $wpdb->insert(
203 $wpdb->commentmeta,
204 array(
205 'comment_id' => $previous_rating_id,
206 'meta_key' => 'tutor_rating',
207 'meta_value' => $rating,
208 )
209 );
210 }
211 } else {
212 $data = array(
213 'comment_post_ID' => esc_sql( $course_id ),
214 'comment_approved' => $moderation ? 'hold' : 'approved',
215 'comment_type' => 'tutor_course_rating',
216 'comment_date' => $date,
217 'comment_date_gmt' => get_gmt_from_date( $date ),
218 'user_id' => $user_id,
219 'comment_author' => $user->user_login,
220 'comment_agent' => 'TutorLMSPlugin',
221 );
222 if ( $review ) {
223 $data['comment_content'] = $review;
224 }
225
226 $wpdb->insert( $wpdb->comments, $data );
227 $comment_id = (int) $wpdb->insert_id;
228 $review_id = $comment_id;
229
230 if ( $comment_id ) {
231 $result = $wpdb->insert(
232 $wpdb->commentmeta,
233 array(
234 'comment_id' => $comment_id,
235 'meta_key' => 'tutor_rating',
236 'meta_value' => $rating,
237 )
238 );
239
240 do_action( 'tutor_after_rating_placed', $comment_id );
241 }
242 }
243
244 wp_send_json_success(
245 array(
246 'message' => __( 'Rating placed successsully!', 'tutor' ),
247 'review_id' => $review_id,
248 )
249 );
250 }
251
252 /**
253 * Delete a review
254 *
255 * @since 1.0.0
256 * @return void
257 */
258 public function delete_tutor_review() {
259 tutor_utils()->checking_nonce();
260
261 $review_id = Input::post( 'review_id' );
262
263 if ( ! tutor_utils()->can_user_manage( 'review', $review_id, get_current_user_id() ) ) {
264 wp_send_json_error( array( 'message' => __( 'Permissioned Denied!', 'tutor' ) ) );
265 exit;
266 }
267
268 global $wpdb;
269 $wpdb->delete( $wpdb->commentmeta, array( 'comment_id' => $review_id ) );
270 $wpdb->delete( $wpdb->comments, array( 'comment_ID' => $review_id ) );
271
272 wp_send_json_success();
273 }
274
275 /**
276 * Add course in wishlist
277 *
278 * @since 1.0.0
279 * @return void
280 */
281 public function tutor_course_add_to_wishlist() {
282 tutor_utils()->checking_nonce();
283
284 // Redirect login since only logged in user can add courses to wishlist.
285 if ( ! is_user_logged_in() ) {
286 wp_send_json_error(
287 array(
288 'redirect_to' => wp_login_url( wp_get_referer() ),
289 )
290 );
291 }
292
293 global $wpdb;
294 $user_id = get_current_user_id();
295 $course_id = Input::post( 'course_id', 0, Input::TYPE_INT );
296
297 $if_added_to_list = $wpdb->get_row(
298 $wpdb->prepare(
299 "SELECT * from {$wpdb->usermeta}
300 WHERE user_id = %d
301 AND meta_key = '_tutor_course_wishlist'
302 AND meta_value = %d;",
303 $user_id,
304 $course_id
305 )
306 );
307
308 if ( $if_added_to_list ) {
309 $wpdb->delete(
310 $wpdb->usermeta,
311 array(
312 'user_id' => $user_id,
313 'meta_key' => '_tutor_course_wishlist',
314 'meta_value' => $course_id,
315 )
316 );
317 wp_send_json_success(
318 array(
319 'status' => 'removed',
320 'message' => __( 'Course removed from wish list', 'tutor' ),
321 )
322 );
323 } else {
324 add_user_meta( $user_id, '_tutor_course_wishlist', $course_id );
325 wp_send_json_success(
326 array(
327 'status' => 'added',
328 'message' => __( 'Course added to wish list', 'tutor' ),
329 )
330 );
331 }
332 }
333
334 /**
335 * Prepare addons data
336 *
337 * @since 1.0.0
338 * @return array
339 */
340 public function prepare_addons_data() {
341 $addons = apply_filters( 'tutor_addons_lists_config', array() );
342 $plugins_data = $addons;
343
344 if ( is_array( $addons ) && count( $addons ) ) {
345 foreach ( $addons as $base_name => $addon ) {
346 $addon_config = tutor_utils()->get_addon_config( $base_name );
347 $is_enabled = (bool) tutor_utils()->avalue_dot( 'is_enable', $addon_config );
348
349 $plugins_data[ $base_name ]['is_enabled'] = $is_enabled;
350
351 $thumbnail_url = tutor()->url . 'assets/images/tutor-plugin.png';
352 if ( file_exists( $addon['path'] . 'assets/images/thumbnail.png' ) ) {
353 $thumbnail_url = $addon['url'] . 'assets/images/thumbnail.png';
354 } elseif ( file_exists( $addon['path'] . 'assets/images/thumbnail.jpg' ) ) {
355 $thumbnail_url = $addon['url'] . 'assets/images/thumbnail.jpg';
356 } elseif ( file_exists( $addon['path'] . 'assets/images/thumbnail.svg' ) ) {
357 $thumbnail_url = $addon['url'] . 'assets/images/thumbnail.svg';
358 }
359
360 $plugins_data[ $base_name ]['thumb_url'] = $thumbnail_url;
361
362 /**
363 * Checking if there any dependant plugin exists
364 */
365 $depends = tutor_utils()->array_get( 'depend_plugins', $addon );
366 $plugins_required = array();
367 if ( tutor_utils()->count( $depends ) ) {
368 foreach ( $depends as $plugin_base => $plugin_name ) {
369 if ( ! is_plugin_active( $plugin_base ) ) {
370 $plugins_required[ $plugin_base ] = $plugin_name;
371 }
372 }
373 }
374
375 $depended_plugins = array();
376 foreach ( $plugins_required as $required_plugin ) {
377 array_push( $depended_plugins, $required_plugin );
378 }
379
380 $plugins_data[ $base_name ]['plugins_required'] = $depended_plugins;
381
382 // Check if it's notifications.
383 if ( function_exists( 'tutor_notifications' ) && tutor_notifications()->basename === $base_name ) {
384
385 $required = array();
386 version_compare( PHP_VERSION, '7.2.5', '>=' ) ? 0 : $required[] = __( 'PHP 7.2.5 or greater is required', 'tutor' );
387 ! is_ssl() ? $required[] = __( 'SSL certificate', 'tutor' ) : 0;
388
389 foreach ( array( 'curl', 'gmp', 'mbstring', 'openssl' ) as $ext ) {
390 ! extension_loaded( $ext ) ? $required[] = 'PHP extension <strong>' . $ext . '</strong>' : 0;
391 }
392
393 $plugins_data[ $base_name ]['ext_required'] = $required;
394 }
395 }
396 }
397
398 $prepared_addons = array();
399 foreach ( $plugins_data as $tutor_addon ) {
400 array_push( $prepared_addons, $tutor_addon );
401 }
402
403 return $prepared_addons;
404 }
405
406 /**
407 * Get all notifications
408 *
409 * @since 1.0.0
410 * @return void
411 */
412 public function tutor_get_all_addons() {
413
414 // Check and verify the request.
415 tutor_utils()->checking_nonce();
416
417 // All good, let's proceed.
418 $all_addons = $this->prepare_addons_data();
419
420 wp_send_json_success(
421 array(
422 'addons' => $all_addons,
423 )
424 );
425 }
426
427 /**
428 * Method for enable / disable addons
429 *
430 * @since 1.0.0
431 * @return void
432 */
433 public function addon_enable_disable() {
434
435 if ( ! current_user_can( 'manage_options' ) ) {
436 wp_send_json_error( array( 'message' => __( 'Access Denied', 'tutor' ) ) );
437 }
438
439 $addons_config = maybe_unserialize( get_option( 'tutor_addons_config' ) );
440 $addon_field_names = json_decode( stripslashes( ( tutor_utils()->avalue_dot( 'addonFieldNames', $_POST ) ) ), true ); //phpcs:ignore WordPress.Security.NonceVerification.Missing
441
442 foreach ( $addon_field_names as $addon_field_name => $is_enable ) {
443 do_action( 'tutor_addon_before_enable_disable' );
444 if ( $is_enable ) {
445 do_action( "tutor_addon_before_enable_{$addon_field_name}" );
446 do_action( 'tutor_addon_before_enable', $addon_field_name );
447 $addons_config[ $addon_field_name ]['is_enable'] = 1;
448 update_option( 'tutor_addons_config', $addons_config );
449
450 do_action( 'tutor_addon_after_enable', $addon_field_name );
451 do_action( "tutor_addon_after_enable_{$addon_field_name}" );
452 } else {
453 do_action( "tutor_addon_before_disable_{$addon_field_name}" );
454 do_action( 'tutor_addon_before_disable', $addon_field_name );
455 $addons_config[ $addon_field_name ]['is_enable'] = 0;
456 update_option( 'tutor_addons_config', $addons_config );
457
458 do_action( 'tutor_addon_after_disable', $addon_field_name );
459 do_action( "tutor_addon_after_disable_{$addon_field_name}" );
460 }
461 do_action( 'tutor_addon_after_enable_disable' );
462 }
463
464 wp_send_json_success();
465 }
466
467 /**
468 * Process tutor login
469 *
470 * @since 1.6.3
471 *
472 * @since 2.1.3 Ajax removed, validation errors
473 * stores in session.
474 *
475 * @return void
476 */
477 public function process_tutor_login() {
478 $validation_error = new \WP_Error();
479
480 /**
481 * Separate nonce verification added to show nonce verification
482 * failed message in a proper way.
483 *
484 * @since 2.1.4
485 */
486 if ( ! wp_verify_nonce( $_POST[ tutor()->nonce ], tutor()->nonce_action ) ) {
487 $validation_error->add( 401, __( 'Nonce verification failed', 'tutor' ) );
488 \set_transient( self::LOGIN_ERRORS_TRANSIENT_KEY, $validation_error->get_error_messages() );
489 return;
490 }
491 //phpcs:disable WordPress.Security.NonceVerification.Missing
492
493 /**
494 * No sanitization/wp_unslash needed for log & pwd since WordPress
495 * does itself
496 *
497 * @since 2.1.3
498 *
499 * @see https://developer.wordpress.org/reference/functions/wp_signon/
500 */
501 $username = tutor_utils()->array_get( 'log', $_POST ); //phpcs:ignore
502 $password = tutor_utils()->array_get( 'pwd', $_POST ); //phpcs:ignore
503 $redirect_to = isset( $_POST['redirect_to'] ) ? esc_url_raw( wp_unslash( $_POST['redirect_to'] ) ) : '';
504 $remember = isset( $_POST['rememberme'] );
505
506 try {
507 $creds = array(
508 'user_login' => trim( $username ),
509 'user_password' => $password,
510 'remember' => $remember,
511 );
512
513 $validation_error = apply_filters( 'tutor_process_login_errors', $validation_error, $creds['user_login'], $creds['user_password'] );
514
515 if ( $validation_error->get_error_code() ) {
516 $validation_error->add(
517 $validation_error->get_error_code(),
518 $validation_error->get_error_message()
519 );
520 }
521
522 if ( empty( $creds['user_login'] ) ) {
523 $validation_error->add(
524 400,
525 __( 'Username is required.', 'tutor' )
526 );
527 }
528
529 // On multi-site, ensure user exists on current site, if not add them before allowing login.
530 if ( is_multisite() ) {
531 $user_data = get_user_by( is_email( $creds['user_login'] ) ? 'email' : 'login', $creds['user_login'] );
532
533 if ( $user_data && ! is_user_member_of_blog( $user_data->ID, get_current_blog_id() ) ) {
534 add_user_to_blog( get_current_blog_id(), $user_data->ID, 'customer' );
535 }
536 }
537
538 // Perform the login.
539 $user = wp_signon( apply_filters( 'tutor_login_credentials', $creds ), is_ssl() );
540
541 if ( is_wp_error( $user ) ) {
542 // If no error exist then add WP login error, to prevent error duplication.
543 if ( ! $validation_error->has_errors() ) {
544 $validation_error->add( 400, $user->get_error_message() );
545 }
546 } else {
547 do_action( 'tutor_after_login_success', $user->ID );
548 // Since 1.9.8 do enroll if guest attempt to enroll.
549 $course_enroll_attempt = Input::post( 'tutor_course_enroll_attempt' );
550 if ( ! empty( $course_enroll_attempt ) && is_a( $user, 'WP_User' ) ) {
551 do_action( 'tutor_do_enroll_after_login_if_attempt', $course_enroll_attempt, $user->ID );
552 }
553 wp_safe_redirect( $redirect_to );
554 exit();
555 }
556 } catch ( \Exception $e ) {
557 do_action( 'tutor_login_failed' );
558 $validation_error->add( 400, $e->getMessage() );
559 } finally {
560 // Store errors in transient data.
561 \set_transient( self::LOGIN_ERRORS_TRANSIENT_KEY, $validation_error->get_error_messages() );
562 }
563 }
564
565 /**
566 * Create/Update announcement
567 *
568 * @since 1.7.9
569 * @return void
570 */
571 public function create_or_update_annoucement() {
572 tutor_utils()->checking_nonce();
573
574 $error = array();
575 $course_id = Input::post( 'tutor_announcement_course' );
576 $announcement_title = Input::post( 'tutor_announcement_title' );
577 $announcement_summary = Input::post( 'tutor_announcement_summary', '', Input::TYPE_TEXTAREA );
578
579 // Check if user can manage this announcment.
580 if ( ! tutor_utils()->can_user_manage( 'course', $course_id ) ) {
581 wp_send_json_error( array( 'message' => __( 'Access Denied', 'tutor' ) ) );
582 }
583
584 // Set data and sanitize it.
585 $form_data = array(
586 'post_type' => 'tutor_announcements',
587 'post_title' => $announcement_title,
588 'post_content' => $announcement_summary,
589 'post_parent' => $course_id,
590 'post_status' => 'publish',
591 );
592
593 if ( Input::has( 'announcement_id' ) ) {
594 $form_data['ID'] = Input::post( 'announcement_id' );
595 }
596
597 // Validation message set.
598 if ( empty( $form_data['post_parent'] ) ) {
599 $error['post_parent'] = __( 'Course name required', 'tutor' );
600
601 }
602
603 if ( empty( $form_data['post_title'] ) ) {
604 $error['post_title'] = __( 'Announcement title required', 'tutor' );
605 }
606
607 if ( empty( $form_data['post_content'] ) ) {
608 $error['post_content'] = __( 'Announcement summary required', 'tutor' );
609
610 }
611
612 if ( empty( $form_data['post_content'] ) ) {
613 $error['post_content'] = __( 'Announcement summary required', 'tutor' );
614
615 }
616
617 // If validation fails.
618 if ( count( $error ) > 0 ) {
619 wp_send_json_error(
620 array(
621 'message' => __( 'All fields required!', 'tutor' ),
622 'fields' => $error,
623 )
624 );
625 }
626
627 // Insert or update post.
628 $post_id = wp_insert_post( $form_data );
629 if ( $post_id > 0 ) {
630 $announcement = get_post( $post_id );
631 $action_type = Input::post( 'action_type' );
632
633 do_action( 'tutor_announcements/after/save', $post_id, $announcement, $action_type );
634
635 $resp_message = 'create' === $action_type ? __( 'Announcement created successfully', 'tutor' ) : __( 'Announcement updated successfully', 'tutor' );
636 wp_send_json_success( array( 'message' => $resp_message ) );
637 }
638
639 wp_send_json_error( array( 'message' => __( 'Something Went Wrong!', 'tutor' ) ) );
640 }
641
642 /**
643 * Delete announcement
644 *
645 * @since 1.7.9
646 * @return void
647 */
648 public function delete_annoucement() {
649 tutor_utils()->checking_nonce();
650
651 $announcement_id = Input::post( 'announcement_id' );
652
653 if ( ! tutor_utils()->can_user_manage( 'announcement', $announcement_id ) ) {
654 wp_send_json_error( array( 'message' => __( 'Access Denied', 'tutor' ) ) );
655 }
656
657 $delete = wp_delete_post( $announcement_id );
658 if ( $delete ) {
659 wp_send_json_success( array( 'message' => __( 'Announcement deleted successfully', 'tutor' ) ) );
660 }
661
662 wp_send_json_error( array( 'message' => __( 'Announcement delete failed', 'tutor' ) ) );
663 }
664 }
665