PluginProbe ʕ •ᴥ•ʔ
Tutor LMS – eLearning and online course solution / 2.6.0
Tutor LMS – eLearning and online course solution v2.6.0
3.9.15 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 Permalink.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 2 years ago TutorEDD.php 2 years ago Tutor_Base.php 2 years ago Tutor_Setup.php 2 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
687 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 $form_data = json_decode( Input::post( 'addonFieldNames' ) );
450 $before_config = get_option( 'tutor_addons_config' );
451 $addons_config = array();
452
453 foreach ( $form_data as $addon_field_name => $is_enable ) {
454
455 $before_status = ! isset( $before_config[ $addon_field_name ] ) ? 0 : $before_config[ $addon_field_name ]['is_enable'];
456 $after_status = $is_enable ? 1 : 0;
457
458 if ( $before_status !== $after_status ) {
459 do_action( 'tutor_addon_before_enable_disable' );
460 if ( $is_enable ) {
461 do_action( "tutor_addon_before_enable_{$addon_field_name}" );
462 do_action( 'tutor_addon_before_enable', $addon_field_name );
463
464 $addons_config[ $addon_field_name ]['is_enable'] = 1;
465 update_option( 'tutor_addons_config', $addons_config );
466
467 do_action( 'tutor_addon_after_enable', $addon_field_name );
468 do_action( "tutor_addon_after_enable_{$addon_field_name}" );
469 } else {
470 do_action( "tutor_addon_before_disable_{$addon_field_name}" );
471 do_action( 'tutor_addon_before_disable', $addon_field_name );
472
473 $addons_config[ $addon_field_name ]['is_enable'] = 0;
474 update_option( 'tutor_addons_config', $addons_config );
475
476 do_action( 'tutor_addon_after_disable', $addon_field_name );
477 do_action( "tutor_addon_after_disable_{$addon_field_name}" );
478 }
479 do_action( 'tutor_addon_after_enable_disable' );
480 } else {
481 $addons_config[ $addon_field_name ]['is_enable'] = $after_status;
482 update_option( 'tutor_addons_config', $addons_config );
483 }
484 }
485
486 wp_send_json_success();
487 }
488
489 /**
490 * Process tutor login
491 *
492 * @since 1.6.3
493 *
494 * @since 2.1.3 Ajax removed, validation errors
495 * stores in session.
496 *
497 * @return void
498 */
499 public function process_tutor_login() {
500 $validation_error = new \WP_Error();
501
502 /**
503 * Separate nonce verification added to show nonce verification
504 * failed message in a proper way.
505 *
506 * @since 2.1.4
507 */
508 if ( ! wp_verify_nonce( $_POST[ tutor()->nonce ], tutor()->nonce_action ) ) { //phpcs:ignore
509 $validation_error->add( 401, __( 'Nonce verification failed', 'tutor' ) );
510 \set_transient( self::LOGIN_ERRORS_TRANSIENT_KEY, $validation_error->get_error_messages() );
511 return;
512 }
513 //phpcs:disable WordPress.Security.NonceVerification.Missing
514
515 /**
516 * No sanitization/wp_unslash needed for log & pwd since WordPress
517 * does itself
518 *
519 * @since 2.1.3
520 *
521 * @see https://developer.wordpress.org/reference/functions/wp_signon/
522 */
523 $username = tutor_utils()->array_get( 'log', $_POST ); //phpcs:ignore
524 $password = tutor_utils()->array_get( 'pwd', $_POST ); //phpcs:ignore
525 $redirect_to = isset( $_POST['redirect_to'] ) ? esc_url_raw( wp_unslash( $_POST['redirect_to'] ) ) : '';
526 $remember = isset( $_POST['rememberme'] );
527
528 try {
529 $creds = array(
530 'user_login' => trim( $username ),
531 'user_password' => $password,
532 'remember' => $remember,
533 );
534
535 $validation_error = apply_filters( 'tutor_process_login_errors', $validation_error, $creds['user_login'], $creds['user_password'] );
536
537 if ( $validation_error->get_error_code() ) {
538 $validation_error->add(
539 $validation_error->get_error_code(),
540 $validation_error->get_error_message()
541 );
542 }
543
544 if ( empty( $creds['user_login'] ) ) {
545 $validation_error->add(
546 400,
547 __( 'Username is required.', 'tutor' )
548 );
549 }
550
551 // On multi-site, ensure user exists on current site, if not add them before allowing login.
552 if ( is_multisite() ) {
553 $user_data = get_user_by( is_email( $creds['user_login'] ) ? 'email' : 'login', $creds['user_login'] );
554
555 if ( $user_data && ! is_user_member_of_blog( $user_data->ID, get_current_blog_id() ) ) {
556 add_user_to_blog( get_current_blog_id(), $user_data->ID, 'customer' );
557 }
558 }
559
560 // Perform the login.
561 $user = wp_signon( apply_filters( 'tutor_login_credentials', $creds ), is_ssl() );
562
563 if ( is_wp_error( $user ) ) {
564 // If no error exist then add WP login error, to prevent error duplication.
565 if ( ! $validation_error->has_errors() ) {
566 $validation_error->add( 400, $user->get_error_message() );
567 }
568 } else {
569 do_action( 'tutor_after_login_success', $user->ID );
570 // Since 1.9.8 do enroll if guest attempt to enroll.
571 $course_enroll_attempt = Input::post( 'tutor_course_enroll_attempt' );
572 if ( ! empty( $course_enroll_attempt ) && is_a( $user, 'WP_User' ) ) {
573 do_action( 'tutor_do_enroll_after_login_if_attempt', $course_enroll_attempt, $user->ID );
574 }
575 wp_safe_redirect( $redirect_to );
576 exit();
577 }
578 } catch ( \Exception $e ) {
579 do_action( 'tutor_login_failed' );
580 $validation_error->add( 400, $e->getMessage() );
581 } finally {
582 // Store errors in transient data.
583 \set_transient( self::LOGIN_ERRORS_TRANSIENT_KEY, $validation_error->get_error_messages() );
584 }
585 }
586
587 /**
588 * Create/Update announcement
589 *
590 * @since 1.7.9
591 * @return void
592 */
593 public function create_or_update_annoucement() {
594 tutor_utils()->checking_nonce();
595
596 $error = array();
597 $course_id = Input::post( 'tutor_announcement_course' );
598 $announcement_title = Input::post( 'tutor_announcement_title' );
599 $announcement_summary = Input::post( 'tutor_announcement_summary', '', Input::TYPE_TEXTAREA );
600
601 // Check if user can manage this announcment.
602 if ( ! tutor_utils()->can_user_manage( 'course', $course_id ) ) {
603 wp_send_json_error( array( 'message' => __( 'Access Denied', 'tutor' ) ) );
604 }
605
606 // Set data and sanitize it.
607 $form_data = array(
608 'post_type' => 'tutor_announcements',
609 'post_title' => $announcement_title,
610 'post_content' => $announcement_summary,
611 'post_parent' => $course_id,
612 'post_status' => 'publish',
613 );
614
615 if ( Input::has( 'announcement_id' ) ) {
616 $form_data['ID'] = Input::post( 'announcement_id' );
617 }
618
619 // Validation message set.
620 if ( empty( $form_data['post_parent'] ) ) {
621 $error['post_parent'] = __( 'Course name required', 'tutor' );
622
623 }
624
625 if ( empty( $form_data['post_title'] ) ) {
626 $error['post_title'] = __( 'Announcement title required', 'tutor' );
627 }
628
629 if ( empty( $form_data['post_content'] ) ) {
630 $error['post_content'] = __( 'Announcement summary required', 'tutor' );
631
632 }
633
634 if ( empty( $form_data['post_content'] ) ) {
635 $error['post_content'] = __( 'Announcement summary required', 'tutor' );
636
637 }
638
639 // If validation fails.
640 if ( count( $error ) > 0 ) {
641 wp_send_json_error(
642 array(
643 'message' => __( 'All fields required!', 'tutor' ),
644 'fields' => $error,
645 )
646 );
647 }
648
649 // Insert or update post.
650 $post_id = wp_insert_post( $form_data );
651 if ( $post_id > 0 ) {
652 $announcement = get_post( $post_id );
653 $action_type = Input::post( 'action_type' );
654
655 do_action( 'tutor_announcements/after/save', $post_id, $announcement, $action_type );
656
657 $resp_message = 'create' === $action_type ? __( 'Announcement created successfully', 'tutor' ) : __( 'Announcement updated successfully', 'tutor' );
658 wp_send_json_success( array( 'message' => $resp_message ) );
659 }
660
661 wp_send_json_error( array( 'message' => __( 'Something Went Wrong!', 'tutor' ) ) );
662 }
663
664 /**
665 * Delete announcement
666 *
667 * @since 1.7.9
668 * @return void
669 */
670 public function delete_annoucement() {
671 tutor_utils()->checking_nonce();
672
673 $announcement_id = Input::post( 'announcement_id' );
674
675 if ( ! tutor_utils()->can_user_manage( 'announcement', $announcement_id ) ) {
676 wp_send_json_error( array( 'message' => __( 'Access Denied', 'tutor' ) ) );
677 }
678
679 $delete = wp_delete_post( $announcement_id );
680 if ( $delete ) {
681 wp_send_json_success( array( 'message' => __( 'Announcement deleted successfully', 'tutor' ) ) );
682 }
683
684 wp_send_json_error( array( 'message' => __( 'Announcement delete failed', 'tutor' ) ) );
685 }
686 }
687