Wishlist::dozent_wishlist()


Source

File: classes/Wishlist.php

	public function dozent_wishlist() {
		$course_id = (int) dozent_input_text( 'course_id' );
		if ( ! is_user_logged_in() ) {
			$redirect_url = wp_login_url( get_the_permalink( $course_id ) );

			wp_send_json_error( [ 'status' => 'login_require', 'redirect_to' => $redirect_url ] );
		}

		$user_id = get_current_user_id();

		$is_added = $this->has_in_wishlist( $course_id, $user_id );

		$btn_text = sprintf( __( ' %s Removed from Wishlist', 'dozent' ), '<i class="dicon-heart"></i>' );

		if ( $is_added ) {
			$btn_text = sprintf( __( ' %s Add to Wishlist', 'dozent' ), '<i class="dicon-heart-o"></i>' );
			delete_user_meta( $user_id, '_dozent_course_wishlist', $course_id );

			wp_send_json_success( [
				'status'   => 'removed',
				'msg'      => __( 'Course removed from wish list', 'dozent' ),
				'btn_text' => $btn_text,
			] );
		} else {
			add_user_meta( $user_id, '_dozent_course_wishlist', $course_id );
			wp_send_json_success( [
				'status'   => 'added',
				'msg'      => __( 'Course added to wish list', 'dozent' ),
				'btn_text' => $btn_text,
			] );
		}

	}