ElementGenerator
9 months ago
Ajax.php
10 months ago
Backend.php
1 year ago
Editor.php
1 year ago
Frontend.php
1 year ago
Helper.php
9 months ago
Hooks.php
9 months ago
Iframe.php
1 year ago
Pages.php
9 months ago
VisibilityCondition.php
8 months ago
Ajax.php
185 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 | tutor_utils()->checking_nonce(); |
| 46 | if ( 'generate_html' === $request_method ) { |
| 47 | |
| 48 | $course_id = Input::post( 'course_id' ); |
| 49 | $droip_data = isset( $_REQUEST['droip_data'] ) ? wp_unslash( $_REQUEST['droip_data'] ) : null; //phpcs:ignore |
| 50 | $droip_data = json_decode( $droip_data, true ); |
| 51 | |
| 52 | $blocks = $droip_data['blocks']; |
| 53 | $styles = $droip_data['styles']; |
| 54 | $root = Input::sanitize( $droip_data['root'] ); |
| 55 | |
| 56 | $params = array( |
| 57 | 'blocks' => $blocks, |
| 58 | 'style_blocks' => $styles, |
| 59 | 'root' => $root, |
| 60 | 'get_variable' => false, |
| 61 | 'get_fonts' => false, |
| 62 | 'options' => array( 'post' => get_post( $course_id ) ), |
| 63 | ); |
| 64 | |
| 65 | $collection_wrapper_html_string = HelperFunctions::get_html_using_preview_script( $params ); |
| 66 | wp_send_json_success( $collection_wrapper_html_string ); |
| 67 | } |
| 68 | if ( 'enroll_course' === $request_method ) { |
| 69 | $course_id = Input::post( 'course_id' ); |
| 70 | $res = tutor_utils()->do_enroll( $course_id ); |
| 71 | wp_send_json_success( $res ); |
| 72 | } |
| 73 | if ( 'add_to_cart_course' === $request_method ) { |
| 74 | $course_id = Input::post( 'course_id' ); |
| 75 | $res = tutor_add_to_cart( $course_id ); |
| 76 | wp_send_json_success( $res ); |
| 77 | } |
| 78 | |
| 79 | if ('remove_from_cart_course' === $request_method) { |
| 80 | $res = tutor_remove_cart_item(Input::post('course_id')); |
| 81 | wp_send_json_success($res); |
| 82 | } |
| 83 | |
| 84 | if('get_user_cart_item_count' === $request_method) { |
| 85 | $cart_items = tutor_get_cart_items(); |
| 86 | $count = count($cart_items); |
| 87 | wp_send_json_success($count); |
| 88 | } |
| 89 | |
| 90 | if ( 'complete_course' === $request_method ) { |
| 91 | $course_id = Input::post( 'course_id' ); |
| 92 | $user_id = get_current_user_id(); |
| 93 | if ( ! $user_id ) { |
| 94 | wp_send_json_error( 'Please Sign-In' ); |
| 95 | } |
| 96 | CourseModel::mark_course_as_completed( $course_id, $user_id ); |
| 97 | |
| 98 | wp_send_json_success( true ); |
| 99 | } |
| 100 | |
| 101 | if ( 'add_qna' === $request_method ) { |
| 102 | $course_id = Input::post( 'course_id' ); |
| 103 | $comment_parent_id = Input::post( 'comment_parent_id' ); |
| 104 | $content = Input::post( 'content' ); |
| 105 | |
| 106 | $user = wp_get_current_user(); |
| 107 | $date = gmdate( 'Y-m-d H:i:s', tutor_time() ); |
| 108 | |
| 109 | if ( ! $user->ID ) { |
| 110 | wp_send_json_error( 'Please Sign-In' ); |
| 111 | } |
| 112 | |
| 113 | $collection_data = isset($_REQUEST['collection_data']) ? json_decode(wp_unslash($_REQUEST['collection_data']), true) : null; //phpcs:ignore |
| 114 | |
| 115 | if ( ! $content ) { |
| 116 | wp_send_json_error( 'Invalid request' ); |
| 117 | } |
| 118 | |
| 119 | $data = apply_filters( |
| 120 | 'tutor_qna_insert_data', |
| 121 | array( |
| 122 | 'comment_post_ID' => $course_id, |
| 123 | 'comment_author' => $user->user_login, |
| 124 | 'comment_date' => $date, |
| 125 | 'comment_date_gmt' => get_gmt_from_date( $date ), |
| 126 | 'comment_content' => $content, |
| 127 | 'comment_approved' => 'approved', |
| 128 | 'comment_agent' => 'TutorLMSPlugin', |
| 129 | 'comment_type' => 'tutor_q_and_a', |
| 130 | 'comment_parent' => $comment_parent_id, |
| 131 | 'user_id' => $user->ID, |
| 132 | ) |
| 133 | ); |
| 134 | |
| 135 | global $wpdb; |
| 136 | |
| 137 | $response = $wpdb->insert( $wpdb->comments, $data ); |
| 138 | |
| 139 | if ( false === $response ) { |
| 140 | wp_send_json_error( 'Request failed!' ); |
| 141 | } |
| 142 | |
| 143 | $thread = $this->get_comment( $wpdb->insert_id ); |
| 144 | |
| 145 | // comment-item.// -qna-reply. |
| 146 | $new_element_name = 0 === $comment_parent_id ? 'comment-item' : TDE_APP_PREFIX . '-qna-reply'; |
| 147 | |
| 148 | $new_element = Preview::generateQnAElement( $thread, $new_element_name, $collection_data ); |
| 149 | |
| 150 | wp_send_json_success( |
| 151 | array( |
| 152 | 'html' => $new_element, |
| 153 | 'inserted_comment_id' => $wpdb->insert_id, |
| 154 | ) |
| 155 | ); |
| 156 | } |
| 157 | |
| 158 | wp_send_json_error( 'Invalid request' ); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Get comment |
| 163 | * |
| 164 | * @param int $id comment id. |
| 165 | * @return object |
| 166 | * @since 1.0.0 |
| 167 | */ |
| 168 | private function get_comment( $id ) { |
| 169 | $comment = (object) (array) get_comment( $id ); |
| 170 | |
| 171 | if ( $comment instanceof stdClass ) { |
| 172 | $author_posts_page_link = $comment->comment_author_url; |
| 173 | |
| 174 | if ( ! $author_posts_page_link ) { |
| 175 | $author_posts_page_link = \get_author_posts_url( $comment->user_id ); |
| 176 | } |
| 177 | |
| 178 | $comment->author_profile_picture = get_avatar_url( $comment->user_id ); |
| 179 | $comment->author_posts_page_link = $author_posts_page_link; |
| 180 | } |
| 181 | |
| 182 | return $comment; |
| 183 | } |
| 184 | } |
| 185 |