PluginProbe ʕ •ᴥ•ʔ
Tutor LMS – eLearning and online course solution / 2.5.0
Tutor LMS – eLearning and online course solution v2.5.0
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 2 years ago Admin.php 2 years ago Ajax.php 2 years ago Announcements.php 3 years ago Assets.php 2 years ago Backend_Page_Trait.php 3 years ago Course.php 2 years ago Course_Embed.php 3 years ago Course_Filter.php 2 years ago Course_List.php 2 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 2 years ago Frontend.php 2 years ago Gutenberg.php 3 years ago Input.php 3 years ago Instructor.php 2 years ago Instructors_List.php 2 years ago Lesson.php 2 years ago Options_V2.php 2 years ago Post_types.php 2 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 2 years ago Quiz_Attempts_List.php 2 years ago RestAPI.php 3 years ago Reviews.php 3 years ago Rewrite_Rules.php 2 years ago Shortcode.php 2 years ago Student.php 2 years ago Students_List.php 3 years ago Taxonomies.php 3 years ago Template.php 2 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 2 years ago Tutor_Base.php 2 years ago Tutor_Setup.php 3 years ago Upgrader.php 2 years ago User.php 2 years ago Utils.php 2 years ago Video_Stream.php 3 years ago WhatsNew.php 2 years ago Withdraw.php 2 years ago Withdraw_Requests_List.php 3 years ago WooCommerce.php 2 years ago
Ajax.php
675 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() ); //phpcs:ignore
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 /**
399 * Keep same sorting order.
400 *
401 * @since 2.2.4
402 */
403 $free_addon_list = apply_filters( 'tutor_pro_addons_lists_for_display', array() );
404 $prepared_addons = array();
405
406 foreach ( $free_addon_list as $addon_name => $addon ) {
407 $key = "tutor-pro/addons/{$addon_name}/{$addon_name}.php";
408 if ( isset( $plugins_data[ $key ] ) ) {
409 $prepared_addons[] = $plugins_data[ $key ];
410 }
411 }
412
413 return $prepared_addons;
414 }
415
416 /**
417 * Get all notifications
418 *
419 * @since 1.0.0
420 * @return void
421 */
422 public function tutor_get_all_addons() {
423
424 // Check and verify the request.
425 tutor_utils()->checking_nonce();
426
427 // All good, let's proceed.
428 $all_addons = $this->prepare_addons_data();
429
430 wp_send_json_success(
431 array(
432 'addons' => $all_addons,
433 )
434 );
435 }
436
437 /**
438 * Method for enable / disable addons
439 *
440 * @since 1.0.0
441 * @return void
442 */
443 public function addon_enable_disable() {
444
445 if ( ! current_user_can( 'manage_options' ) ) {
446 wp_send_json_error( array( 'message' => __( 'Access Denied', 'tutor' ) ) );
447 }
448
449 $addons_config = maybe_unserialize( get_option( 'tutor_addons_config' ) );
450 $addon_field_names = json_decode( stripslashes( ( tutor_utils()->avalue_dot( 'addonFieldNames', $_POST ) ) ), true ); //phpcs:ignore WordPress.Security.NonceVerification.Missing
451
452 foreach ( $addon_field_names as $addon_field_name => $is_enable ) {
453 do_action( 'tutor_addon_before_enable_disable' );
454 if ( $is_enable ) {
455 do_action( "tutor_addon_before_enable_{$addon_field_name}" );
456 do_action( 'tutor_addon_before_enable', $addon_field_name );
457 $addons_config[ $addon_field_name ]['is_enable'] = 1;
458 update_option( 'tutor_addons_config', $addons_config );
459
460 do_action( 'tutor_addon_after_enable', $addon_field_name );
461 do_action( "tutor_addon_after_enable_{$addon_field_name}" );
462 } else {
463 do_action( "tutor_addon_before_disable_{$addon_field_name}" );
464 do_action( 'tutor_addon_before_disable', $addon_field_name );
465 $addons_config[ $addon_field_name ]['is_enable'] = 0;
466 update_option( 'tutor_addons_config', $addons_config );
467
468 do_action( 'tutor_addon_after_disable', $addon_field_name );
469 do_action( "tutor_addon_after_disable_{$addon_field_name}" );
470 }
471 do_action( 'tutor_addon_after_enable_disable' );
472 }
473
474 wp_send_json_success();
475 }
476
477 /**
478 * Process tutor login
479 *
480 * @since 1.6.3
481 *
482 * @since 2.1.3 Ajax removed, validation errors
483 * stores in session.
484 *
485 * @return void
486 */
487 public function process_tutor_login() {
488 $validation_error = new \WP_Error();
489
490 /**
491 * Separate nonce verification added to show nonce verification
492 * failed message in a proper way.
493 *
494 * @since 2.1.4
495 */
496 if ( ! wp_verify_nonce( $_POST[ tutor()->nonce ], tutor()->nonce_action ) ) { //phpcs:ignore
497 $validation_error->add( 401, __( 'Nonce verification failed', 'tutor' ) );
498 \set_transient( self::LOGIN_ERRORS_TRANSIENT_KEY, $validation_error->get_error_messages() );
499 return;
500 }
501 //phpcs:disable WordPress.Security.NonceVerification.Missing
502
503 /**
504 * No sanitization/wp_unslash needed for log & pwd since WordPress
505 * does itself
506 *
507 * @since 2.1.3
508 *
509 * @see https://developer.wordpress.org/reference/functions/wp_signon/
510 */
511 $username = tutor_utils()->array_get( 'log', $_POST ); //phpcs:ignore
512 $password = tutor_utils()->array_get( 'pwd', $_POST ); //phpcs:ignore
513 $redirect_to = isset( $_POST['redirect_to'] ) ? esc_url_raw( wp_unslash( $_POST['redirect_to'] ) ) : '';
514 $remember = isset( $_POST['rememberme'] );
515
516 try {
517 $creds = array(
518 'user_login' => trim( $username ),
519 'user_password' => $password,
520 'remember' => $remember,
521 );
522
523 $validation_error = apply_filters( 'tutor_process_login_errors', $validation_error, $creds['user_login'], $creds['user_password'] );
524
525 if ( $validation_error->get_error_code() ) {
526 $validation_error->add(
527 $validation_error->get_error_code(),
528 $validation_error->get_error_message()
529 );
530 }
531
532 if ( empty( $creds['user_login'] ) ) {
533 $validation_error->add(
534 400,
535 __( 'Username is required.', 'tutor' )
536 );
537 }
538
539 // On multi-site, ensure user exists on current site, if not add them before allowing login.
540 if ( is_multisite() ) {
541 $user_data = get_user_by( is_email( $creds['user_login'] ) ? 'email' : 'login', $creds['user_login'] );
542
543 if ( $user_data && ! is_user_member_of_blog( $user_data->ID, get_current_blog_id() ) ) {
544 add_user_to_blog( get_current_blog_id(), $user_data->ID, 'customer' );
545 }
546 }
547
548 // Perform the login.
549 $user = wp_signon( apply_filters( 'tutor_login_credentials', $creds ), is_ssl() );
550
551 if ( is_wp_error( $user ) ) {
552 // If no error exist then add WP login error, to prevent error duplication.
553 if ( ! $validation_error->has_errors() ) {
554 $validation_error->add( 400, $user->get_error_message() );
555 }
556 } else {
557 do_action( 'tutor_after_login_success', $user->ID );
558 // Since 1.9.8 do enroll if guest attempt to enroll.
559 $course_enroll_attempt = Input::post( 'tutor_course_enroll_attempt' );
560 if ( ! empty( $course_enroll_attempt ) && is_a( $user, 'WP_User' ) ) {
561 do_action( 'tutor_do_enroll_after_login_if_attempt', $course_enroll_attempt, $user->ID );
562 }
563 wp_safe_redirect( $redirect_to );
564 exit();
565 }
566 } catch ( \Exception $e ) {
567 do_action( 'tutor_login_failed' );
568 $validation_error->add( 400, $e->getMessage() );
569 } finally {
570 // Store errors in transient data.
571 \set_transient( self::LOGIN_ERRORS_TRANSIENT_KEY, $validation_error->get_error_messages() );
572 }
573 }
574
575 /**
576 * Create/Update announcement
577 *
578 * @since 1.7.9
579 * @return void
580 */
581 public function create_or_update_annoucement() {
582 tutor_utils()->checking_nonce();
583
584 $error = array();
585 $course_id = Input::post( 'tutor_announcement_course' );
586 $announcement_title = Input::post( 'tutor_announcement_title' );
587 $announcement_summary = Input::post( 'tutor_announcement_summary', '', Input::TYPE_TEXTAREA );
588
589 // Check if user can manage this announcment.
590 if ( ! tutor_utils()->can_user_manage( 'course', $course_id ) ) {
591 wp_send_json_error( array( 'message' => __( 'Access Denied', 'tutor' ) ) );
592 }
593
594 // Set data and sanitize it.
595 $form_data = array(
596 'post_type' => 'tutor_announcements',
597 'post_title' => $announcement_title,
598 'post_content' => $announcement_summary,
599 'post_parent' => $course_id,
600 'post_status' => 'publish',
601 );
602
603 if ( Input::has( 'announcement_id' ) ) {
604 $form_data['ID'] = Input::post( 'announcement_id' );
605 }
606
607 // Validation message set.
608 if ( empty( $form_data['post_parent'] ) ) {
609 $error['post_parent'] = __( 'Course name required', 'tutor' );
610
611 }
612
613 if ( empty( $form_data['post_title'] ) ) {
614 $error['post_title'] = __( 'Announcement title required', 'tutor' );
615 }
616
617 if ( empty( $form_data['post_content'] ) ) {
618 $error['post_content'] = __( 'Announcement summary required', 'tutor' );
619
620 }
621
622 if ( empty( $form_data['post_content'] ) ) {
623 $error['post_content'] = __( 'Announcement summary required', 'tutor' );
624
625 }
626
627 // If validation fails.
628 if ( count( $error ) > 0 ) {
629 wp_send_json_error(
630 array(
631 'message' => __( 'All fields required!', 'tutor' ),
632 'fields' => $error,
633 )
634 );
635 }
636
637 // Insert or update post.
638 $post_id = wp_insert_post( $form_data );
639 if ( $post_id > 0 ) {
640 $announcement = get_post( $post_id );
641 $action_type = Input::post( 'action_type' );
642
643 do_action( 'tutor_announcements/after/save', $post_id, $announcement, $action_type );
644
645 $resp_message = 'create' === $action_type ? __( 'Announcement created successfully', 'tutor' ) : __( 'Announcement updated successfully', 'tutor' );
646 wp_send_json_success( array( 'message' => $resp_message ) );
647 }
648
649 wp_send_json_error( array( 'message' => __( 'Something Went Wrong!', 'tutor' ) ) );
650 }
651
652 /**
653 * Delete announcement
654 *
655 * @since 1.7.9
656 * @return void
657 */
658 public function delete_annoucement() {
659 tutor_utils()->checking_nonce();
660
661 $announcement_id = Input::post( 'announcement_id' );
662
663 if ( ! tutor_utils()->can_user_manage( 'announcement', $announcement_id ) ) {
664 wp_send_json_error( array( 'message' => __( 'Access Denied', 'tutor' ) ) );
665 }
666
667 $delete = wp_delete_post( $announcement_id );
668 if ( $delete ) {
669 wp_send_json_success( array( 'message' => __( 'Announcement deleted successfully', 'tutor' ) ) );
670 }
671
672 wp_send_json_error( array( 'message' => __( 'Announcement delete failed', 'tutor' ) ) );
673 }
674 }
675