ElementGenerator
2 months ago
Ajax.php
1 week ago
Backend.php
1 year ago
Editor.php
1 year ago
Frontend.php
1 year ago
Helper.php
9 months ago
Hooks.php
4 months ago
Iframe.php
1 year ago
Pages.php
9 months ago
VisibilityCondition.php
4 months ago
Ajax.php
220 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Preview script for html markup generator |
| 4 | * |
| 5 | * @package tutor-droip-elements |
| 6 | */ |
| 7 | |
| 8 | namespace TutorLMSDroip; |
| 9 | |
| 10 | use Droip\HelperFunctions; |
| 11 | use stdClass; |
| 12 | use TutorLMSDroip\ElementGenerator\Preview; |
| 13 | use TUTOR\Input; |
| 14 | use Tutor\Models\CourseModel; |
| 15 | |
| 16 | if ( ! defined( 'ABSPATH' ) ) { |
| 17 | exit; // Exit if accessed directly. |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * Class Ajax |
| 22 | * This class is used to define all ajax functions. |
| 23 | * |
| 24 | * @package TutorLMSDroip |
| 25 | * @since 1.0.0 |
| 26 | */ |
| 27 | class Ajax { |
| 28 | |
| 29 | |
| 30 | /** |
| 31 | * Class constructor |
| 32 | */ |
| 33 | public function __construct() { |
| 34 | add_action( 'wp_ajax_tutor_handle_api_calls', array( $this, 'tutor_handle_api_calls' ) ); |
| 35 | add_action( 'wp_ajax_nopriv_tutor_handle_api_calls', array( $this, 'tutor_handle_api_calls' ) ); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Handle api calls |
| 40 | * |
| 41 | * @since 1.0.0 |
| 42 | */ |
| 43 | public function tutor_handle_api_calls() { |
| 44 | $request_method = Input::post( 'method' ); |
| 45 | $is_user_logged_in = is_user_logged_in(); |
| 46 | |
| 47 | tutor_utils()->checking_nonce(); |
| 48 | |
| 49 | if ( 'generate_html' === $request_method ) { |
| 50 | |
| 51 | $course_id = Input::post( 'course_id' ); |
| 52 | $droip_data = isset( $_REQUEST['droip_data'] ) ? wp_unslash( $_REQUEST['droip_data'] ) : null; //phpcs:ignore |
| 53 | $droip_data = json_decode( $droip_data, true ); |
| 54 | |
| 55 | $blocks = $droip_data['blocks']; |
| 56 | $styles = $droip_data['styles']; |
| 57 | $root = Input::sanitize( $droip_data['root'] ); |
| 58 | |
| 59 | $params = array( |
| 60 | 'blocks' => $blocks, |
| 61 | 'style_blocks' => $styles, |
| 62 | 'root' => $root, |
| 63 | 'get_variable' => false, |
| 64 | 'get_fonts' => false, |
| 65 | 'options' => array( 'post' => get_post( $course_id ) ), |
| 66 | ); |
| 67 | |
| 68 | $collection_wrapper_html_string = HelperFunctions::get_html_using_preview_script( $params ); |
| 69 | wp_send_json_success( $collection_wrapper_html_string ); |
| 70 | } |
| 71 | if ( 'enroll_course' === $request_method && $is_user_logged_in ) { |
| 72 | $course_id = Input::post( 'course_id' ); |
| 73 | |
| 74 | $course = get_post( $course_id ); |
| 75 | |
| 76 | if ( ! $course || ! is_object( $course ) || $course->post_status !== 'publish' ) { |
| 77 | wp_send_json_error( 'Course not found!' ); |
| 78 | } |
| 79 | |
| 80 | if ( tutor_utils()->is_course_purchasable( $course_id ) ) { |
| 81 | wp_send_json_error( 'You cannot enroll in this course without purchasing it first.' ); |
| 82 | } |
| 83 | |
| 84 | $res = tutor_utils()->do_enroll( $course_id ); |
| 85 | |
| 86 | wp_send_json_success( $res ); |
| 87 | } |
| 88 | |
| 89 | if ( 'add_to_cart_course' === $request_method ) { |
| 90 | $course_id = Input::post( 'course_id' ); |
| 91 | $res = tutor_add_to_cart( $course_id ); |
| 92 | |
| 93 | // check is user logged in or not |
| 94 | if (! is_user_logged_in() ) { |
| 95 | $res['redirect'] = true; |
| 96 | $res['data'] = wp_login_url( wp_get_referer() ); |
| 97 | } |
| 98 | |
| 99 | wp_send_json_success( $res ); |
| 100 | } |
| 101 | |
| 102 | if ('remove_from_cart_course' === $request_method) { |
| 103 | $res = tutor_remove_cart_item(Input::post('course_id')); |
| 104 | wp_send_json_success($res); |
| 105 | } |
| 106 | |
| 107 | if('get_user_cart_item_count' === $request_method) { |
| 108 | $cart_items = tutor_get_cart_items(); |
| 109 | $count = count($cart_items); |
| 110 | wp_send_json_success($count); |
| 111 | } |
| 112 | |
| 113 | if ( 'complete_course' === $request_method && $is_user_logged_in ) { |
| 114 | $course_id = Input::post( 'course_id' ); |
| 115 | |
| 116 | $is_enrolled = tutor_utils()->is_enrolled($course_id); |
| 117 | |
| 118 | if ( ! $is_enrolled ) { |
| 119 | wp_send_json_error( 'You are not allowed to complete this course.' ); |
| 120 | } |
| 121 | |
| 122 | $user_id = get_current_user_id(); |
| 123 | |
| 124 | CourseModel::mark_course_as_completed( $course_id, $user_id ); |
| 125 | |
| 126 | wp_send_json_success( true ); |
| 127 | } |
| 128 | |
| 129 | if ( 'add_qna' === $request_method && $is_user_logged_in ) { |
| 130 | $course_id = Input::post( 'course_id' ); |
| 131 | |
| 132 | $is_enrolled = tutor_utils()->is_enrolled($course_id); |
| 133 | |
| 134 | if ( ! $is_enrolled ) { |
| 135 | wp_send_json_error( 'You are not allowed to add Q&A to this course.' ); |
| 136 | } |
| 137 | |
| 138 | $comment_parent_id = Input::post( 'comment_parent_id' ); |
| 139 | $content = Input::post( 'content' ); |
| 140 | |
| 141 | $user = wp_get_current_user(); |
| 142 | $date = gmdate( 'Y-m-d H:i:s', tutor_time() ); |
| 143 | |
| 144 | if ( ! $user->ID ) { |
| 145 | wp_send_json_error( 'Please Sign-In' ); |
| 146 | } |
| 147 | |
| 148 | $collection_data = isset($_REQUEST['collection_data']) ? json_decode(wp_unslash($_REQUEST['collection_data']), true) : null; //phpcs:ignore |
| 149 | |
| 150 | if ( ! $content ) { |
| 151 | wp_send_json_error( 'Invalid request' ); |
| 152 | } |
| 153 | |
| 154 | $data = apply_filters( |
| 155 | 'tutor_qna_insert_data', |
| 156 | array( |
| 157 | 'comment_post_ID' => $course_id, |
| 158 | 'comment_author' => $user->user_login, |
| 159 | 'comment_date' => $date, |
| 160 | 'comment_date_gmt' => get_gmt_from_date( $date ), |
| 161 | 'comment_content' => $content, |
| 162 | 'comment_approved' => 'approved', |
| 163 | 'comment_agent' => 'TutorLMSPlugin', |
| 164 | 'comment_type' => 'tutor_q_and_a', |
| 165 | 'comment_parent' => $comment_parent_id, |
| 166 | 'user_id' => $user->ID, |
| 167 | ) |
| 168 | ); |
| 169 | |
| 170 | global $wpdb; |
| 171 | |
| 172 | $response = $wpdb->insert( $wpdb->comments, $data ); |
| 173 | |
| 174 | if ( false === $response ) { |
| 175 | wp_send_json_error( 'Request failed!' ); |
| 176 | } |
| 177 | |
| 178 | $thread = $this->get_comment( $wpdb->insert_id ); |
| 179 | |
| 180 | // comment-item.// -qna-reply. |
| 181 | $new_element_name = 0 === $comment_parent_id ? 'comment-item' : TDE_APP_PREFIX . '-qna-reply'; |
| 182 | |
| 183 | $new_element = Preview::generateQnAElement( $thread, $new_element_name, $collection_data ); |
| 184 | |
| 185 | wp_send_json_success( |
| 186 | array( |
| 187 | 'html' => $new_element, |
| 188 | 'inserted_comment_id' => $wpdb->insert_id, |
| 189 | ) |
| 190 | ); |
| 191 | } |
| 192 | |
| 193 | wp_send_json_error( 'Invalid request' ); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Get comment |
| 198 | * |
| 199 | * @param int $id comment id. |
| 200 | * @return object |
| 201 | * @since 1.0.0 |
| 202 | */ |
| 203 | private function get_comment( $id ) { |
| 204 | $comment = (object) (array) get_comment( $id ); |
| 205 | |
| 206 | if ( $comment instanceof stdClass ) { |
| 207 | $author_posts_page_link = $comment->comment_author_url; |
| 208 | |
| 209 | if ( ! $author_posts_page_link ) { |
| 210 | $author_posts_page_link = \get_author_posts_url( $comment->user_id ); |
| 211 | } |
| 212 | |
| 213 | $comment->author_profile_picture = get_avatar_url( $comment->user_id ); |
| 214 | $comment->author_posts_page_link = $author_posts_page_link; |
| 215 | } |
| 216 | |
| 217 | return $comment; |
| 218 | } |
| 219 | } |
| 220 |