Addons.php
6 years ago
Admin.php
6 years ago
Ajax.php
6 years ago
Assets.php
6 years ago
Course.php
6 years ago
Course_Settings_Tabs.php
6 years ago
Course_Widget.php
6 years ago
Dashboard.php
6 years ago
Email.php
6 years ago
FormHandler.php
6 years ago
Gutenberg.php
6 years ago
Instructor.php
6 years ago
Instructors_List.php
6 years ago
Lesson.php
6 years ago
Options.php
6 years ago
Post_types.php
6 years ago
Q_and_A.php
6 years ago
Question_Answers_List.php
6 years ago
Quiz.php
6 years ago
Quiz_Attempts_List.php
6 years ago
Rewrite_Rules.php
6 years ago
Shortcode.php
6 years ago
Student.php
6 years ago
Students_List.php
6 years ago
Taxonomies.php
6 years ago
Template.php
6 years ago
Theme_Compatibility.php
6 years ago
Tools.php
6 years ago
Tutor.php
6 years ago
TutorEDD.php
6 years ago
Tutor_Base.php
6 years ago
Tutor_List_Table.php
6 years ago
Upgrader.php
6 years ago
User.php
6 years ago
Utils.php
6 years ago
Video_Stream.php
6 years ago
Withdraw.php
6 years ago
Withdraw_Requests_List.php
6 years ago
WooCommerce.php
6 years ago
Ajax.php
318 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 | /** |
| 33 | * Update video information and data when necessary |
| 34 | * |
| 35 | * @since v.1.0.0 |
| 36 | */ |
| 37 | public function sync_video_playback(){ |
| 38 | tutor_utils()->checking_nonce(); |
| 39 | |
| 40 | $duration = sanitize_text_field($_POST['duration']); |
| 41 | $currentTime = sanitize_text_field($_POST['currentTime']); |
| 42 | $post_id = sanitize_text_field($_POST['post_id']); |
| 43 | |
| 44 | /** |
| 45 | * Update posts attached video |
| 46 | */ |
| 47 | $video = tutor_utils()->get_video($post_id); |
| 48 | |
| 49 | if ($duration) { |
| 50 | $video['duration_sec'] = $duration; //secs |
| 51 | $video['playtime'] = tutor_utils()->playtime_string( $duration ); |
| 52 | $video['runtime'] = tutor_utils()->playtime_array( $duration ); |
| 53 | } |
| 54 | tutor_utils()->update_video($post_id, $video); |
| 55 | |
| 56 | /** |
| 57 | * Sync Lesson Reading Info by Users |
| 58 | */ |
| 59 | |
| 60 | $user_id = get_current_user_id(); |
| 61 | |
| 62 | $best_watch_time = tutor_utils()->get_lesson_reading_info($post_id, $user_id, 'video_best_watched_time'); |
| 63 | if ($best_watch_time < $currentTime){ |
| 64 | tutor_utils()->update_lesson_reading_info($post_id, $user_id, 'video_best_watched_time', $currentTime); |
| 65 | } |
| 66 | |
| 67 | if (tutor_utils()->avalue_dot('is_ended', $_POST)){ |
| 68 | tutor_utils()->mark_lesson_complete($post_id); |
| 69 | } |
| 70 | exit(); |
| 71 | } |
| 72 | |
| 73 | public function sync_video_playback_noprev(){ |
| 74 | |
| 75 | } |
| 76 | |
| 77 | |
| 78 | public function tutor_place_rating(){ |
| 79 | global $wpdb; |
| 80 | |
| 81 | //TODO: Check nonce |
| 82 | |
| 83 | $rating = sanitize_text_field(tutor_utils()->avalue_dot('rating', $_POST)); |
| 84 | $course_id = sanitize_text_field(tutor_utils()->avalue_dot('course_id', $_POST)); |
| 85 | $review = wp_kses_post(tutor_utils()->avalue_dot('review', $_POST)); |
| 86 | |
| 87 | $user_id = get_current_user_id(); |
| 88 | $user = get_userdata($user_id); |
| 89 | $date = date("Y-m-d H:i:s", tutor_time()); |
| 90 | |
| 91 | do_action('tutor_before_rating_placed'); |
| 92 | |
| 93 | $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;"); |
| 94 | |
| 95 | $review_ID = $previous_rating_id; |
| 96 | if ( $previous_rating_id){ |
| 97 | $wpdb->update( $wpdb->comments, array('comment_content' => $review), |
| 98 | array('comment_ID' => $previous_rating_id) |
| 99 | ); |
| 100 | |
| 101 | $rating_info = $wpdb->get_row("SELECT * FROM {$wpdb->commentmeta} WHERE comment_id = {$previous_rating_id} AND meta_key = 'tutor_rating'; "); |
| 102 | if ($rating_info){ |
| 103 | $wpdb->update( $wpdb->commentmeta, array('meta_value' => $rating), array('comment_id' => $previous_rating_id, 'meta_key' => 'tutor_rating') ); |
| 104 | }else{ |
| 105 | $wpdb->insert( $wpdb->commentmeta, array('comment_id' => $previous_rating_id, 'meta_key' => 'tutor_rating', 'meta_value' => $rating) ); |
| 106 | } |
| 107 | }else{ |
| 108 | $data = array( |
| 109 | 'comment_post_ID' => $course_id, |
| 110 | 'comment_approved' => 'approved', |
| 111 | 'comment_type' => 'tutor_course_rating', |
| 112 | 'comment_date' => $date, |
| 113 | 'comment_date_gmt' => get_gmt_from_date($date), |
| 114 | 'user_id' => $user_id, |
| 115 | 'comment_author' => $user->user_login, |
| 116 | 'comment_agent' => 'TutorLMSPlugin', |
| 117 | ); |
| 118 | if ($review){ |
| 119 | $data['comment_content'] = $review; |
| 120 | } |
| 121 | |
| 122 | $wpdb->insert($wpdb->comments, $data); |
| 123 | $comment_id = (int) $wpdb->insert_id; |
| 124 | $review_ID = $comment_id; |
| 125 | |
| 126 | if ($comment_id && $rating){ |
| 127 | $result = $wpdb->insert( $wpdb->commentmeta, array( |
| 128 | 'comment_id' => $comment_id, |
| 129 | 'meta_key' => 'tutor_rating', |
| 130 | 'meta_value' => $rating |
| 131 | ) ); |
| 132 | |
| 133 | do_action('tutor_after_rating_placed', $comment_id); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | $data = array('msg' => __('Rating placed success', 'tutor'), 'review_id' => $review_ID, 'review' => $review); |
| 138 | wp_send_json_success($data); |
| 139 | } |
| 140 | |
| 141 | public function tutor_ask_question(){ |
| 142 | tutor_utils()->checking_nonce(); |
| 143 | |
| 144 | global $wpdb; |
| 145 | |
| 146 | $course_id = (int) sanitize_text_field($_POST['tutor_course_id']); |
| 147 | $question_title = sanitize_text_field($_POST['question_title']); |
| 148 | $question = wp_kses_post($_POST['question']); |
| 149 | |
| 150 | if (empty($question) || empty($question_title)){ |
| 151 | wp_send_json_error(__('Empty question title or body', 'tutor')); |
| 152 | } |
| 153 | |
| 154 | $user_id = get_current_user_id(); |
| 155 | $user = get_userdata($user_id); |
| 156 | $date = date("Y-m-d H:i:s", tutor_time()); |
| 157 | |
| 158 | do_action('tutor_before_add_question', $course_id); |
| 159 | $data = apply_filters('tutor_add_question_data', array( |
| 160 | 'comment_post_ID' => $course_id, |
| 161 | 'comment_author' => $user->user_login, |
| 162 | 'comment_date' => $date, |
| 163 | 'comment_date_gmt' => get_gmt_from_date($date), |
| 164 | 'comment_content' => $question, |
| 165 | 'comment_approved' => 'waiting_for_answer', |
| 166 | 'comment_agent' => 'TutorLMSPlugin', |
| 167 | 'comment_type' => 'tutor_q_and_a', |
| 168 | 'user_id' => $user_id, |
| 169 | )); |
| 170 | |
| 171 | $wpdb->insert($wpdb->comments, $data); |
| 172 | $comment_id = (int) $wpdb->insert_id; |
| 173 | |
| 174 | if ($comment_id){ |
| 175 | $result = $wpdb->insert( $wpdb->commentmeta, array( |
| 176 | 'comment_id' => $comment_id, |
| 177 | 'meta_key' => 'tutor_question_title', |
| 178 | 'meta_value' => $question_title |
| 179 | ) ); |
| 180 | } |
| 181 | do_action('tutor_after_add_question', $course_id, $comment_id); |
| 182 | |
| 183 | wp_send_json_success(__('Question has been added successfully', 'tutor')); |
| 184 | } |
| 185 | |
| 186 | |
| 187 | public function tutor_add_answer(){ |
| 188 | tutor_utils()->checking_nonce(); |
| 189 | global $wpdb; |
| 190 | |
| 191 | $answer = wp_kses_post($_POST['answer']); |
| 192 | if ( ! $answer){ |
| 193 | wp_send_json_error(__('Please write answer', 'tutor')); |
| 194 | } |
| 195 | |
| 196 | $question_id = (int) sanitize_text_field($_POST['question_id']); |
| 197 | $question = tutor_utils()->get_qa_question($question_id); |
| 198 | |
| 199 | $user_id = get_current_user_id(); |
| 200 | $user = get_userdata($user_id); |
| 201 | $date = date("Y-m-d H:i:s", tutor_time()); |
| 202 | |
| 203 | do_action('tutor_before_answer_to_question'); |
| 204 | $data = apply_filters('tutor_add_answer_data', array( |
| 205 | 'comment_post_ID' => $question->comment_post_ID, |
| 206 | 'comment_author' => $user->user_login, |
| 207 | 'comment_date' => $date, |
| 208 | 'comment_date_gmt' => get_gmt_from_date($date), |
| 209 | 'comment_content' => $answer, |
| 210 | 'comment_approved' => 'approved', |
| 211 | 'comment_agent' => 'TutorLMSPlugin', |
| 212 | 'comment_type' => 'tutor_q_and_a', |
| 213 | 'comment_parent' => $question_id, |
| 214 | 'user_id' => $user_id, |
| 215 | )); |
| 216 | |
| 217 | $wpdb->insert($wpdb->comments, $data); |
| 218 | $comment_id = (int) $wpdb->insert_id; |
| 219 | do_action('tutor_after_answer_to_question', $comment_id); |
| 220 | |
| 221 | wp_send_json_success(__('Answer has been added successfully', 'tutor')); |
| 222 | } |
| 223 | |
| 224 | |
| 225 | public function tutor_course_add_to_wishlist(){ |
| 226 | $course_id = (int) sanitize_text_field($_POST['course_id']); |
| 227 | if ( ! is_user_logged_in()){ |
| 228 | wp_send_json_error(array('redirect_to' => wp_login_url( wp_get_referer() ) ) ); |
| 229 | } |
| 230 | global $wpdb; |
| 231 | |
| 232 | $user_id = get_current_user_id(); |
| 233 | $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} ;"); |
| 234 | |
| 235 | if ( $if_added_to_list){ |
| 236 | $wpdb->delete($wpdb->usermeta, array('user_id' => $user_id, 'meta_key' => '_tutor_course_wishlist', 'meta_value' => $course_id )); |
| 237 | wp_send_json_success(array('status' => 'removed', 'msg' => __('Course removed from wish list', 'tutor'))); |
| 238 | }else{ |
| 239 | add_user_meta($user_id, '_tutor_course_wishlist', $course_id); |
| 240 | wp_send_json_success(array('status' => 'added', 'msg' => __('Course added to wish list', 'tutor'))); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Method for enable / disable addons |
| 246 | */ |
| 247 | public function addon_enable_disable(){ |
| 248 | $addonsConfig = maybe_unserialize(get_option('tutor_addons_config')); |
| 249 | |
| 250 | $isEnable = (bool) sanitize_text_field(tutor_utils()->avalue_dot('isEnable', $_POST)); |
| 251 | $addonFieldName = sanitize_text_field(tutor_utils()->avalue_dot('addonFieldName', $_POST)); |
| 252 | |
| 253 | do_action('tutor_addon_before_enable_disable'); |
| 254 | if ($isEnable){ |
| 255 | do_action("tutor_addon_before_enable_{$addonFieldName}"); |
| 256 | do_action('tutor_addon_before_enable', $addonFieldName); |
| 257 | $addonsConfig[$addonFieldName]['is_enable'] = 1; |
| 258 | update_option('tutor_addons_config', $addonsConfig); |
| 259 | |
| 260 | do_action('tutor_addon_after_enable', $addonFieldName); |
| 261 | do_action("tutor_addon_after_enable_{$addonFieldName}"); |
| 262 | }else{ |
| 263 | do_action("tutor_addon_before_disable_{$addonFieldName}"); |
| 264 | do_action('tutor_addon_before_disable', $addonFieldName); |
| 265 | $addonsConfig[$addonFieldName]['is_enable'] = 0; |
| 266 | update_option('tutor_addons_config', $addonsConfig); |
| 267 | |
| 268 | do_action('tutor_addon_after_disable', $addonFieldName); |
| 269 | do_action("tutor_addon_after_disable_{$addonFieldName}"); |
| 270 | } |
| 271 | |
| 272 | do_action('tutor_addon_after_enable_disable'); |
| 273 | wp_send_json_success(); |
| 274 | } |
| 275 | |
| 276 | |
| 277 | /** |
| 278 | * Load review edit form |
| 279 | * @since v.1.4.0 |
| 280 | */ |
| 281 | public function tutor_load_edit_review_modal(){ |
| 282 | tutor_utils()->checking_nonce(); |
| 283 | |
| 284 | $review_id = (int) sanitize_text_field(tutils()->array_get('review_id', $_POST)); |
| 285 | $rating = tutils()->get_rating_by_id($review_id); |
| 286 | |
| 287 | ob_start(); |
| 288 | tutor_load_template('dashboard.reviews.edit-review-form', array('rating' => $rating)); |
| 289 | $output = ob_get_clean(); |
| 290 | |
| 291 | wp_send_json_success(array('output' => $output)); |
| 292 | } |
| 293 | |
| 294 | public function tutor_update_review_modal(){ |
| 295 | global $wpdb; |
| 296 | |
| 297 | tutor_utils()->checking_nonce(); |
| 298 | |
| 299 | $review_id = (int) sanitize_text_field(tutils()->array_get('review_id', $_POST)); |
| 300 | $rating = sanitize_text_field(tutor_utils()->avalue_dot('rating', $_POST)); |
| 301 | $review = wp_kses_post(tutor_utils()->avalue_dot('review', $_POST)); |
| 302 | |
| 303 | $is_exists = $wpdb->get_var("select comment_ID from {$wpdb->comments} WHERE comment_ID={$review_id} AND comment_type = 'tutor_course_rating' ;"); |
| 304 | |
| 305 | if ( $is_exists) { |
| 306 | $wpdb->update( $wpdb->comments, array( 'comment_content' => $review ), |
| 307 | array( 'comment_ID' => $review_id ) |
| 308 | ); |
| 309 | $wpdb->update( $wpdb->commentmeta, array( 'meta_value' => $rating ), |
| 310 | array( 'comment_id' => $review_id, 'meta_key' => 'tutor_rating' ) |
| 311 | ); |
| 312 | |
| 313 | wp_send_json_success(); |
| 314 | } |
| 315 | wp_send_json_error(); |
| 316 | } |
| 317 | |
| 318 | } |