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_Settings_Tabs.php
5 years ago
Course_Widget.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
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
379 lines
| 1 | <?php |
| 2 | namespace TUTOR; |
| 3 | |
| 4 | if ( ! defined( 'ABSPATH' ) ) |
| 5 | exit; |
| 6 | |
| 7 | class Ajax{ |
| 8 | public function __construct() { |
| 9 | add_action('wp_ajax_sync_video_playback', array($this, 'sync_video_playback')); |
| 10 | add_action('wp_ajax_nopriv_sync_video_playback', array($this, 'sync_video_playback_noprev')); |
| 11 | add_action('wp_ajax_tutor_place_rating', array($this, 'tutor_place_rating')); |
| 12 | |
| 13 | add_action('wp_ajax_tutor_ask_question', array($this, 'tutor_ask_question')); |
| 14 | add_action('wp_ajax_tutor_add_answer', array($this, 'tutor_add_answer')); |
| 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 | * Addon Enable Disable Control |
| 21 | */ |
| 22 | add_action('wp_ajax_addon_enable_disable', array($this, 'addon_enable_disable')); |
| 23 | |
| 24 | /** |
| 25 | * Update Rating/review |
| 26 | * @since v.1.4.0 |
| 27 | */ |
| 28 | add_action('wp_ajax_tutor_load_edit_review_modal', array($this, 'tutor_load_edit_review_modal')); |
| 29 | add_action('wp_ajax_tutor_update_review_modal', array($this, 'tutor_update_review_modal')); |
| 30 | |
| 31 | /** |
| 32 | * Ajax login |
| 33 | * @since v.1.6.3 |
| 34 | */ |
| 35 | add_action('wp_ajax_nopriv_tutor_user_login', array($this, 'process_ajax_login')); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Update video information and data when necessary |
| 40 | * |
| 41 | * @since v.1.0.0 |
| 42 | */ |
| 43 | public function sync_video_playback(){ |
| 44 | tutor_utils()->checking_nonce(); |
| 45 | |
| 46 | $duration = sanitize_text_field($_POST['duration']); |
| 47 | $currentTime = sanitize_text_field($_POST['currentTime']); |
| 48 | $post_id = sanitize_text_field($_POST['post_id']); |
| 49 | |
| 50 | /** |
| 51 | * Update posts attached video |
| 52 | */ |
| 53 | $video = tutor_utils()->get_video($post_id); |
| 54 | |
| 55 | if ($duration) { |
| 56 | $video['duration_sec'] = $duration; //secs |
| 57 | $video['playtime'] = tutor_utils()->playtime_string( $duration ); |
| 58 | $video['runtime'] = tutor_utils()->playtime_array( $duration ); |
| 59 | } |
| 60 | tutor_utils()->update_video($post_id, $video); |
| 61 | |
| 62 | /** |
| 63 | * Sync Lesson Reading Info by Users |
| 64 | */ |
| 65 | |
| 66 | $user_id = get_current_user_id(); |
| 67 | |
| 68 | $best_watch_time = tutor_utils()->get_lesson_reading_info($post_id, $user_id, 'video_best_watched_time'); |
| 69 | if ($best_watch_time < $currentTime){ |
| 70 | tutor_utils()->update_lesson_reading_info($post_id, $user_id, 'video_best_watched_time', $currentTime); |
| 71 | } |
| 72 | |
| 73 | if (tutor_utils()->avalue_dot('is_ended', $_POST)){ |
| 74 | tutor_utils()->mark_lesson_complete($post_id); |
| 75 | } |
| 76 | exit(); |
| 77 | } |
| 78 | |
| 79 | public function sync_video_playback_noprev(){ |
| 80 | |
| 81 | } |
| 82 | |
| 83 | |
| 84 | public function tutor_place_rating(){ |
| 85 | global $wpdb; |
| 86 | |
| 87 | //TODO: Check nonce |
| 88 | |
| 89 | $rating = sanitize_text_field(tutor_utils()->avalue_dot('rating', $_POST)); |
| 90 | $course_id = sanitize_text_field(tutor_utils()->avalue_dot('course_id', $_POST)); |
| 91 | $review = wp_kses_post(tutor_utils()->avalue_dot('review', $_POST)); |
| 92 | |
| 93 | $user_id = get_current_user_id(); |
| 94 | $user = get_userdata($user_id); |
| 95 | $date = date("Y-m-d H:i:s", tutor_time()); |
| 96 | |
| 97 | do_action('tutor_before_rating_placed'); |
| 98 | |
| 99 | $previous_rating_id = $wpdb->get_var("select comment_ID from {$wpdb->comments} WHERE comment_post_ID={$course_id} AND user_id = {$user_id} AND comment_type = 'tutor_course_rating' LIMIT 1;"); |
| 100 | |
| 101 | $review_ID = $previous_rating_id; |
| 102 | if ( $previous_rating_id){ |
| 103 | $wpdb->update( $wpdb->comments, array('comment_content' => $review), |
| 104 | array('comment_ID' => $previous_rating_id) |
| 105 | ); |
| 106 | |
| 107 | $rating_info = $wpdb->get_row("SELECT * FROM {$wpdb->commentmeta} WHERE comment_id = {$previous_rating_id} AND meta_key = 'tutor_rating'; "); |
| 108 | if ($rating_info){ |
| 109 | $wpdb->update( $wpdb->commentmeta, array('meta_value' => $rating), array('comment_id' => $previous_rating_id, 'meta_key' => 'tutor_rating') ); |
| 110 | }else{ |
| 111 | $wpdb->insert( $wpdb->commentmeta, array('comment_id' => $previous_rating_id, 'meta_key' => 'tutor_rating', 'meta_value' => $rating) ); |
| 112 | } |
| 113 | }else{ |
| 114 | $data = array( |
| 115 | 'comment_post_ID' => $course_id, |
| 116 | 'comment_approved' => 'approved', |
| 117 | 'comment_type' => 'tutor_course_rating', |
| 118 | 'comment_date' => $date, |
| 119 | 'comment_date_gmt' => get_gmt_from_date($date), |
| 120 | 'user_id' => $user_id, |
| 121 | 'comment_author' => $user->user_login, |
| 122 | 'comment_agent' => 'TutorLMSPlugin', |
| 123 | ); |
| 124 | if ($review){ |
| 125 | $data['comment_content'] = $review; |
| 126 | } |
| 127 | |
| 128 | $wpdb->insert($wpdb->comments, $data); |
| 129 | $comment_id = (int) $wpdb->insert_id; |
| 130 | $review_ID = $comment_id; |
| 131 | |
| 132 | if ($comment_id && $rating){ |
| 133 | $result = $wpdb->insert( $wpdb->commentmeta, array( |
| 134 | 'comment_id' => $comment_id, |
| 135 | 'meta_key' => 'tutor_rating', |
| 136 | 'meta_value' => $rating |
| 137 | ) ); |
| 138 | |
| 139 | do_action('tutor_after_rating_placed', $comment_id); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | $data = array('msg' => __('Rating placed success', 'tutor'), 'review_id' => $review_ID, 'review' => $review); |
| 144 | wp_send_json_success($data); |
| 145 | } |
| 146 | |
| 147 | public function tutor_ask_question(){ |
| 148 | tutor_utils()->checking_nonce(); |
| 149 | |
| 150 | global $wpdb; |
| 151 | |
| 152 | $course_id = (int) sanitize_text_field($_POST['tutor_course_id']); |
| 153 | $question_title = sanitize_text_field($_POST['question_title']); |
| 154 | $question = wp_kses_post($_POST['question']); |
| 155 | |
| 156 | if (empty($question) || empty($question_title)){ |
| 157 | wp_send_json_error(__('Empty question title or body', 'tutor')); |
| 158 | } |
| 159 | |
| 160 | $user_id = get_current_user_id(); |
| 161 | $user = get_userdata($user_id); |
| 162 | $date = date("Y-m-d H:i:s", tutor_time()); |
| 163 | |
| 164 | do_action('tutor_before_add_question', $course_id); |
| 165 | $data = apply_filters('tutor_add_question_data', array( |
| 166 | 'comment_post_ID' => $course_id, |
| 167 | 'comment_author' => $user->user_login, |
| 168 | 'comment_date' => $date, |
| 169 | 'comment_date_gmt' => get_gmt_from_date($date), |
| 170 | 'comment_content' => $question, |
| 171 | 'comment_approved' => 'waiting_for_answer', |
| 172 | 'comment_agent' => 'TutorLMSPlugin', |
| 173 | 'comment_type' => 'tutor_q_and_a', |
| 174 | 'user_id' => $user_id, |
| 175 | )); |
| 176 | |
| 177 | $wpdb->insert($wpdb->comments, $data); |
| 178 | $comment_id = (int) $wpdb->insert_id; |
| 179 | |
| 180 | if ($comment_id){ |
| 181 | $result = $wpdb->insert( $wpdb->commentmeta, array( |
| 182 | 'comment_id' => $comment_id, |
| 183 | 'meta_key' => 'tutor_question_title', |
| 184 | 'meta_value' => $question_title |
| 185 | ) ); |
| 186 | } |
| 187 | do_action('tutor_after_add_question', $course_id, $comment_id); |
| 188 | |
| 189 | wp_send_json_success(__('Question has been added successfully', 'tutor')); |
| 190 | } |
| 191 | |
| 192 | |
| 193 | public function tutor_add_answer(){ |
| 194 | tutor_utils()->checking_nonce(); |
| 195 | global $wpdb; |
| 196 | |
| 197 | $answer = wp_kses_post($_POST['answer']); |
| 198 | if ( ! $answer){ |
| 199 | wp_send_json_error(__('Please write answer', 'tutor')); |
| 200 | } |
| 201 | |
| 202 | $question_id = (int) sanitize_text_field($_POST['question_id']); |
| 203 | $question = tutor_utils()->get_qa_question($question_id); |
| 204 | |
| 205 | $user_id = get_current_user_id(); |
| 206 | $user = get_userdata($user_id); |
| 207 | $date = date("Y-m-d H:i:s", tutor_time()); |
| 208 | |
| 209 | do_action('tutor_before_answer_to_question'); |
| 210 | $data = apply_filters('tutor_add_answer_data', array( |
| 211 | 'comment_post_ID' => $question->comment_post_ID, |
| 212 | 'comment_author' => $user->user_login, |
| 213 | 'comment_date' => $date, |
| 214 | 'comment_date_gmt' => get_gmt_from_date($date), |
| 215 | 'comment_content' => $answer, |
| 216 | 'comment_approved' => 'approved', |
| 217 | 'comment_agent' => 'TutorLMSPlugin', |
| 218 | 'comment_type' => 'tutor_q_and_a', |
| 219 | 'comment_parent' => $question_id, |
| 220 | 'user_id' => $user_id, |
| 221 | )); |
| 222 | |
| 223 | $wpdb->insert($wpdb->comments, $data); |
| 224 | $comment_id = (int) $wpdb->insert_id; |
| 225 | do_action('tutor_after_answer_to_question', $comment_id); |
| 226 | |
| 227 | wp_send_json_success(__('Answer has been added successfully', 'tutor')); |
| 228 | } |
| 229 | |
| 230 | |
| 231 | public function tutor_course_add_to_wishlist(){ |
| 232 | $course_id = (int) sanitize_text_field($_POST['course_id']); |
| 233 | if ( ! is_user_logged_in()){ |
| 234 | wp_send_json_error(array('redirect_to' => wp_login_url( wp_get_referer() ) ) ); |
| 235 | } |
| 236 | global $wpdb; |
| 237 | |
| 238 | $user_id = get_current_user_id(); |
| 239 | $if_added_to_list = $wpdb->get_row("select * from {$wpdb->usermeta} WHERE user_id = {$user_id} AND meta_key = '_tutor_course_wishlist' AND meta_value = {$course_id} ;"); |
| 240 | |
| 241 | if ( $if_added_to_list){ |
| 242 | $wpdb->delete($wpdb->usermeta, array('user_id' => $user_id, 'meta_key' => '_tutor_course_wishlist', 'meta_value' => $course_id )); |
| 243 | wp_send_json_success(array('status' => 'removed', 'msg' => __('Course removed from wish list', 'tutor'))); |
| 244 | }else{ |
| 245 | add_user_meta($user_id, '_tutor_course_wishlist', $course_id); |
| 246 | wp_send_json_success(array('status' => 'added', 'msg' => __('Course added to wish list', 'tutor'))); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Method for enable / disable addons |
| 252 | */ |
| 253 | public function addon_enable_disable(){ |
| 254 | $addonsConfig = maybe_unserialize(get_option('tutor_addons_config')); |
| 255 | |
| 256 | $isEnable = (bool) sanitize_text_field(tutor_utils()->avalue_dot('isEnable', $_POST)); |
| 257 | $addonFieldName = sanitize_text_field(tutor_utils()->avalue_dot('addonFieldName', $_POST)); |
| 258 | |
| 259 | do_action('tutor_addon_before_enable_disable'); |
| 260 | if ($isEnable){ |
| 261 | do_action("tutor_addon_before_enable_{$addonFieldName}"); |
| 262 | do_action('tutor_addon_before_enable', $addonFieldName); |
| 263 | $addonsConfig[$addonFieldName]['is_enable'] = 1; |
| 264 | update_option('tutor_addons_config', $addonsConfig); |
| 265 | |
| 266 | do_action('tutor_addon_after_enable', $addonFieldName); |
| 267 | do_action("tutor_addon_after_enable_{$addonFieldName}"); |
| 268 | }else{ |
| 269 | do_action("tutor_addon_before_disable_{$addonFieldName}"); |
| 270 | do_action('tutor_addon_before_disable', $addonFieldName); |
| 271 | $addonsConfig[$addonFieldName]['is_enable'] = 0; |
| 272 | update_option('tutor_addons_config', $addonsConfig); |
| 273 | |
| 274 | do_action('tutor_addon_after_disable', $addonFieldName); |
| 275 | do_action("tutor_addon_after_disable_{$addonFieldName}"); |
| 276 | } |
| 277 | |
| 278 | do_action('tutor_addon_after_enable_disable'); |
| 279 | wp_send_json_success(); |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Load review edit form |
| 284 | * @since v.1.4.0 |
| 285 | */ |
| 286 | public function tutor_load_edit_review_modal(){ |
| 287 | tutor_utils()->checking_nonce(); |
| 288 | |
| 289 | $review_id = (int) sanitize_text_field(tutils()->array_get('review_id', $_POST)); |
| 290 | $rating = tutils()->get_rating_by_id($review_id); |
| 291 | |
| 292 | ob_start(); |
| 293 | tutor_load_template('dashboard.reviews.edit-review-form', array('rating' => $rating)); |
| 294 | $output = ob_get_clean(); |
| 295 | |
| 296 | wp_send_json_success(array('output' => $output)); |
| 297 | } |
| 298 | |
| 299 | public function tutor_update_review_modal(){ |
| 300 | global $wpdb; |
| 301 | |
| 302 | tutor_utils()->checking_nonce(); |
| 303 | |
| 304 | $review_id = (int) sanitize_text_field(tutils()->array_get('review_id', $_POST)); |
| 305 | $rating = sanitize_text_field(tutor_utils()->avalue_dot('rating', $_POST)); |
| 306 | $review = wp_kses_post(tutor_utils()->avalue_dot('review', $_POST)); |
| 307 | |
| 308 | $is_exists = $wpdb->get_var("select comment_ID from {$wpdb->comments} WHERE comment_ID={$review_id} AND comment_type = 'tutor_course_rating' ;"); |
| 309 | |
| 310 | if ( $is_exists) { |
| 311 | $wpdb->update( $wpdb->comments, array( 'comment_content' => $review ), |
| 312 | array( 'comment_ID' => $review_id ) |
| 313 | ); |
| 314 | $wpdb->update( $wpdb->commentmeta, array( 'meta_value' => $rating ), |
| 315 | array( 'comment_id' => $review_id, 'meta_key' => 'tutor_rating' ) |
| 316 | ); |
| 317 | |
| 318 | wp_send_json_success(); |
| 319 | } |
| 320 | wp_send_json_error(); |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * Process ajax login |
| 325 | * @since v.1.6.3 |
| 326 | */ |
| 327 | public function process_ajax_login(){ |
| 328 | tutils()->checking_nonce(); |
| 329 | |
| 330 | $username = tutils()->array_get('log', $_POST); |
| 331 | $password = tutils()->array_get('pwd', $_POST); |
| 332 | $redirect_to = tutils()->array_get('redirect_to', $_POST); |
| 333 | |
| 334 | try { |
| 335 | $creds = array( |
| 336 | 'user_login' => trim( wp_unslash( $username ) ), // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 337 | 'user_password' => $password, // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash |
| 338 | 'remember' => isset( $_POST['rememberme'] ), // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 339 | ); |
| 340 | |
| 341 | $validation_error = new \WP_Error(); |
| 342 | $validation_error = apply_filters( 'tutor_process_login_errors', $validation_error, $creds['user_login'], $creds['user_password'] ); |
| 343 | |
| 344 | if ( $validation_error->get_error_code() ) { |
| 345 | wp_send_json_error( '<strong>' . __( 'ERROR:', 'tutor' ) . '</strong> ' . $validation_error->get_error_message() ); |
| 346 | } |
| 347 | |
| 348 | if ( empty( $creds['user_login'] ) ) { |
| 349 | wp_send_json_error( '<strong>' . __( 'ERROR:', 'tutor' ) . '</strong> ' . __( 'Username is required.', 'tutor' ) ); |
| 350 | } |
| 351 | |
| 352 | // On multisite, ensure user exists on current site, if not add them before allowing login. |
| 353 | if ( is_multisite() ) { |
| 354 | $user_data = get_user_by( is_email( $creds['user_login'] ) ? 'email' : 'login', $creds['user_login'] ); |
| 355 | |
| 356 | if ( $user_data && ! is_user_member_of_blog( $user_data->ID, get_current_blog_id() ) ) { |
| 357 | add_user_to_blog( get_current_blog_id(), $user_data->ID, 'customer' ); |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | // Perform the login. |
| 362 | $user = wp_signon( apply_filters( 'tutor_login_credentials', $creds ), is_ssl() ); |
| 363 | |
| 364 | if ( is_wp_error( $user ) ) { |
| 365 | $message = $user->get_error_message(); |
| 366 | $message = str_replace( '<strong>' . esc_html( $creds['user_login'] ) . '</strong>', '<strong>' . esc_html( $creds['user_login'] ) . '</strong>', $message ); |
| 367 | |
| 368 | wp_send_json_error( $message ); |
| 369 | } else { |
| 370 | wp_send_json_success([ |
| 371 | 'redirect' => apply_filters('tutor_login_redirect_url', $redirect_to) |
| 372 | ]); |
| 373 | } |
| 374 | } catch ( \Exception $e ) { |
| 375 | wp_send_json_error( apply_filters( 'login_errors', $e->getMessage()) ); |
| 376 | do_action( 'tutor_login_failed' ); |
| 377 | } |
| 378 | } |
| 379 | } |