Ajax
Source
File: classes/Ajax.php
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | class Ajax { public function __construct() { //Saving option add_action( 'wp_ajax_dozent_option_save' , [ $this , 'dozent_option_save' ] ); //Search Instructor add_action( 'wp_ajax_dozent_search_instructor' , [ $this , 'dozent_search_instructor' ] ); //Add Instructor add_action( 'wp_ajax_dozent_add_instructors' , [ $this , 'dozent_add_instructors' ] ); //Detach an instructor add_action( 'wp_ajax_dozent_detach_instructor' , [ $this , 'dozent_detach_instructor' ] ); //Create or Update Review add_action( 'wp_ajax_dozent_save_review' , [ $this , 'dozent_save_review' ] ); add_action( 'wp_ajax_dozent_load_more_reviews' , [ $this , 'dozent_load_more_reviews' ] ); add_action( 'wp_ajax_nopriv_dozent_load_more_reviews' , [ $this , 'dozent_load_more_reviews' ] ); add_action( 'wp_ajax_dozent_search_reviews' , [ $this , 'dozent_search_reviews' ] ); add_action( 'wp_ajax_nopriv_dozent_search_reviews' , [ $this , 'dozent_search_reviews' ] ); //Update review from dashboard add_action( 'wp_ajax_open_review_update_modal' , [ $this , 'open_review_update_modal' ] ); } public function dozent_option_save() { dozent_checking_nonce(); do_action( 'dozent_option_save_before' ); $option = ( array ) dozent_input_array_field( 'dozent_option' ); $option = apply_filters( 'dozent_option_input' , $option ); update_option( 'dozent_options' , $option ); do_action( 'dozent_option_save_after' ); wp_send_json_success( [ 'msg' => __( 'Dozent options has been saved' , 'dozent' ) ] ); } public function dozent_search_instructor() { $search_term = dozent_input_text( 'search_term' ); $course_id = dozent_input_text( 'course_id' ); $course = get_post( $course_id ); $search_args = [ 'search_term' => $search_term , 'start' => 0, 'per_page' => "-1" , ]; $attached_instructors_ids = dozent_get_attached_instructors_ids( $course ); $not_in_ids = []; if ( ! empty ( $course ->post_author ) ) { $not_in_ids [] = $course ->post_author; } if ( dozent_count( $attached_instructors_ids ) ) { $not_in_ids = array_merge ( $not_in_ids , $attached_instructors_ids ); } if ( dozent_count( $not_in_ids ) ) { $search_args [ 'not_in_ids' ] = $not_in_ids ; } $instructors = dozent_get_instructors( $search_args ); ob_start(); include_once DOZENT_ABSPATH . '/views/metabox/part/instructors-search-results.php' ; $html = ob_get_clean(); wp_send_json_success( [ 'html' => $html ] ); } public function dozent_add_instructors() { $course_id = dozent_input_text( 'course_id' ); $instructor_ids = dozent_input_array_field( 'instructors' ); do_action( 'dozent_add_instructors_before' , $instructor_ids , $course_id ); if ( dozent_count( $instructor_ids ) ) { foreach ( $instructor_ids as $instructor_id ) { dozent_instructor_attach_course( $course_id , $instructor_id ); } } do_action( 'dozent_add_instructors_after' , $instructor_ids , $course_id ); ob_start(); include_once DOZENT_ABSPATH . 'views/metabox/part/instructors-list.php' ; $instructors_list = ob_get_clean(); wp_send_json_success( [ 'message' => dozent_notice( __( 'Instructor has been added' , 'dozent' ), 'success' , false ), 'instructors_list_html' => $instructors_list , ] ); } /** * Detach an instructor from a course. * * @since DozentLMS 1.0.0 */ public function dozent_detach_instructor() { $course_id = (int) dozent_input_text( 'course_id' ); $instructor_id = (int) dozent_input_text( 'instructor_id' ); dozent_instructor_detach_course( $course_id , $instructor_id ); wp_send_json_success(); } public function dozent_save_review() { global $wpdb ; $course_id = (int) dozent_input_text( 'course_id' ); $rating_value = (int) dozent_input_text( 'rating_value' ); $review = dozent_input_textarea( 'review' ); $user_id = get_current_user_id(); $hasReview = (int) $wpdb ->get_var( "SELECT ID FROM {$wpdb->dozent_reviews} WHERE user_id = {$user_id} AND course_id = {$course_id} " ); if ( $hasReview ) { //Update review $data = apply_filters( 'dozent/course_review/update_data' , [ 'review' => $review , 'rating' => $rating_value , 'updated_at' => dozent_mysql_time(), ] ); do_action( 'dozent/course_review/update/before' , $data ); $wpdb ->update( $wpdb ->dozent_reviews, $data , [ 'ID' => $hasReview ] ); do_action( 'dozent/course_review/update/after' , $data ); } else { //Create review $data = apply_filters( 'dozent/course_review/create_data' , [ 'user_id' => $user_id , 'course_id' => $course_id , 'review_id' => 0, 'review' => $review , 'rating' => $rating_value , 'status' => 1, 'created_at' => dozent_mysql_time(), 'updated_at' => dozent_mysql_time(), ] ); do_action( 'dozent/course_review/create/before' , $data ); $wpdb ->insert( $wpdb ->dozent_reviews, $data ); do_action( 'dozent/course_review/create/after' , $data ); } //Sync Rating with course meta. _dozent_get_course_ratings_and_sync( $course_id ); wp_send_json_success( [ 'msg' => __( 'Review has been saved' , 'dozent' ) ] ); } /** * Load more reviews * * Ajax pagination of the reviews which comes from the course details page. * * * @since DozentLMS 1.0.0 * */ public function dozent_load_more_reviews() { $course_id = dozent_input_text( 'course_id' ); $per_page = dozent_input_text( 'per_page' ); $page = max( 1, dozent_input_text( 'page' ) ); $start = ( $page - 1 ) * $per_page ; //search term $review_s = dozent_input_text( 'review_s' ); $review_s_rating = (int) dozent_input_text( 'review_s_rating' ); $search_args = [ 'course_id' => $course_id , 'start' => $start , 'per_page' => $per_page ]; //search term if ( $review_s ) { $search_args [ 's' ] = $review_s ; } //Filter by rating if ( $review_s_rating ) { $search_args [ 'rating' ] = $review_s_rating ; } $reviews = dozent_get_course_reviews( $search_args ); $review_html = '' ; if ( $reviews -> count ) { foreach ( $reviews ->results as $review ) { $review_html .= dozent_individual_review_render( $review , false ); } } wp_send_json_success( [ 'html' => $review_html ] ); } public function dozent_search_reviews() { $config = json_decode( wp_unslash( dozent_input_textarea( 'config' ) ), true ); $course_ID = (int) dozent_array_get( 'course_id' , $config ); $per_page = (int) dozent_array_get( 'per_page' , $config ); $review_s = dozent_input_text( 'review_s' ); $review_s_rating = (int) dozent_input_text( 'review_s_rating' ); if ( ! $course_ID ) { wp_send_json_error(); } $search_args = [ 'course_id' => $course_ID , 'per_page' => $per_page ]; //search term if ( $review_s ) { $search_args [ 's' ] = $review_s ; } //Filter by rating if ( $review_s_rating ) { $search_args [ 'rating' ] = $review_s_rating ; } $reviews_html = dozent_reviews_render( $search_args , false ); wp_send_json_success( [ 'html' => $reviews_html ] ); } /** * Opening update modal from the dashboard * * * @since DozentLMS 1.0.0 * * @see open_review_update_modal(); * */ public function open_review_update_modal() { $review_id = dozent_input_text( 'review_id' ); $review = dozent_get_review( $review_id ); if ( ! $review ) { wp_send_json_error(); } ob_start(); ?> <div class = "modal-content" > <div class = "modal-header dozent-mb-2" > <h5 class = "modal-title" > <i class = "dicon-graduation-cap" ></i> <?php echo $review ->course_title; ?> </h5> <a href= "javascript:;" class = "dozent-modal-close" data-dismiss= "dozent-modal" > <i class = "dicon-close" ></i> </a> </div> <form action= "" class = "dozent-modal-review-form" method= "post" > <div class = "modal-body" > <div id= "review-writing-box" class = "course-review-write-box-wrap" > <input type= "hidden" name= "course_id" value= "<?php echo $review->course_id; ?>" > <?php dozent_rating_field( $review ->rating ); ?> <div class = "form-group" > <textarea name= "review" class = "form-control" rows= "4" ><?php echo $review ? $review ->review : '' ; ?></textarea> </div> </div> </div> <div class = "review-modal-footer" > <p class = "review-modal-nofity-text" > <i class = "dicon-earth" ></i> <?php _e( 'Your review will be posted publicly. Under' , 'dozent' ); ?> <strong> <?php echo $review ->user_display_name; ?> </strong> </p> <button type= "submit" class = "dozent-btn dozent-btn-primary" > <i class = "dicon-comment" ></i> <?php _e( 'Update review' , 'dozent' ); ?> </button> <button type= "button" class = "dozent-btn dozent-btn-light" data-dismiss= "dozent-modal" > <?php _e( 'Cancel' , 'dozent' ); ?> </button> </div> </form> </div> <?php $html = ob_get_clean(); wp_send_json_success( [ 'html' => $html ] ); } } |
Methods
- __construct
- dozent_add_instructors
- dozent_detach_instructor — Detach an instructor from a course.
- dozent_load_more_reviews — Load more reviews
- dozent_option_save
- dozent_save_review
- dozent_search_instructor
- dozent_search_reviews
- open_review_update_modal — Opening update modal from the dashboard