Ajax


Source

File: classes/Ajax.php

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