ElementGenerator
1 year ago
Ajax.php
1 year ago
Backend.php
1 year ago
Editor.php
1 year ago
Frontend.php
1 year ago
Helper.php
1 year ago
Iframe.php
1 year ago
Pages.php
1 year ago
Ajax.php
150 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 stdClass; |
| 11 | use Tutor\Models\CourseModel; |
| 12 | use TutorLMSDroip\ElementGenerator\Preview; |
| 13 | |
| 14 | if ( ! defined( 'ABSPATH' ) ) { |
| 15 | exit; // Exit if accessed directly. |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Class Ajax |
| 20 | * This class is used to define all ajax functions. |
| 21 | * |
| 22 | * @package TutorLMSDroip |
| 23 | * @since 1.0.0 |
| 24 | */ |
| 25 | class Ajax { |
| 26 | |
| 27 | /** |
| 28 | * Class constructor |
| 29 | */ |
| 30 | public function __construct() { |
| 31 | add_action( 'wp_ajax_tutor_handle_api_calls', array( $this, 'tutor_handle_api_calls' ) ); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Handle api calls |
| 36 | * |
| 37 | * @since 1.0.0 |
| 38 | */ |
| 39 | public function tutor_handle_api_calls() { |
| 40 | // Helper::verify_nonce( 'wp_rest' ); |
| 41 | //phpcs:ignore WordPress.Security.NonceVerification.Missing,WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput.MissingUnslash,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 42 | $request_method = sanitize_text_field( isset( $_REQUEST['method'] ) ? $_REQUEST['method'] : null ); |
| 43 | if ( 'enroll_course' === $request_method ) { |
| 44 | tutor_utils()->checking_nonce(); |
| 45 | //phpcs:ignore WordPress.Security.NonceVerification.Missing,WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput.MissingUnslash,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 46 | $course_id = sanitize_text_field( isset( $_REQUEST['course_id'] ) ? $_REQUEST['course_id'] : null ); |
| 47 | $res = tutor_utils()->do_enroll( $course_id ); |
| 48 | wp_send_json_success( $res ); |
| 49 | } |
| 50 | |
| 51 | if ( 'complete_course' === $request_method ) { |
| 52 | tutor_utils()->checking_nonce(); |
| 53 | |
| 54 | //phpcs:ignore WordPress.Security.NonceVerification.Missing,WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput.MissingUnslash,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 55 | $course_id = sanitize_text_field( isset( $_REQUEST['course_id'] ) ? $_REQUEST['course_id'] : null ); |
| 56 | $user_id = get_current_user_id(); |
| 57 | if ( ! $user_id ) { |
| 58 | wp_send_json_error( 'Please Sign-In' ); |
| 59 | } |
| 60 | CourseModel::mark_course_as_completed( $course_id, $user_id ); |
| 61 | |
| 62 | wp_send_json_success( true ); |
| 63 | } |
| 64 | |
| 65 | if ( 'add_qna' === $request_method ) { |
| 66 | tutor_utils()->checking_nonce(); |
| 67 | |
| 68 | //phpcs:ignore WordPress.Security.NonceVerification.Missing,WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput.MissingUnslash,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 69 | $course_id = sanitize_text_field( isset( $_REQUEST['course_id'] ) ? $_REQUEST['course_id'] : null ); |
| 70 | //phpcs:ignore WordPress.Security.NonceVerification.Missing,WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput.MissingUnslash,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 71 | $comment_parent_id = sanitize_text_field( isset( $_REQUEST['comment_parent_id'] ) ? $_REQUEST['comment_parent_id'] : null ); |
| 72 | //phpcs:ignore WordPress.Security.NonceVerification.Missing,WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput.MissingUnslash,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 73 | $content = sanitize_text_field( isset( $_REQUEST['content'] ) ? $_REQUEST['content'] : null ); |
| 74 | $user = wp_get_current_user(); |
| 75 | $date = gmdate( 'Y-m-d H:i:s', tutor_time() ); |
| 76 | |
| 77 | //phpcs:ignore WordPress.Security.NonceVerification.Missing,WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput.MissingUnslash,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 78 | $collection_data = json_decode( stripslashes( isset( $_REQUEST['collection_data'] ) ? $_REQUEST['collection_data'] : null ), true ); |
| 79 | |
| 80 | if ( ! $content ) { |
| 81 | wp_send_json_error( 'Invalid request' ); |
| 82 | } |
| 83 | |
| 84 | $data = apply_filters( |
| 85 | 'tutor_qna_insert_data', |
| 86 | array( |
| 87 | 'comment_post_ID' => $course_id, |
| 88 | 'comment_author' => $user->user_login, |
| 89 | 'comment_date' => $date, |
| 90 | 'comment_date_gmt' => get_gmt_from_date( $date ), |
| 91 | 'comment_content' => $content, |
| 92 | 'comment_approved' => 'approved', |
| 93 | 'comment_agent' => 'TutorLMSPlugin', |
| 94 | 'comment_type' => 'tutor_q_and_a', |
| 95 | 'comment_parent' => $comment_parent_id, |
| 96 | 'user_id' => $user->ID, |
| 97 | ) |
| 98 | ); |
| 99 | |
| 100 | global $wpdb; |
| 101 | |
| 102 | $response = $wpdb->insert( $wpdb->comments, $data ); |
| 103 | |
| 104 | if ( false === $response ) { |
| 105 | wp_send_json_error( 'Request failed!' ); |
| 106 | } |
| 107 | |
| 108 | $thread = $this->get_comment( $wpdb->insert_id ); |
| 109 | |
| 110 | // comment-item.// -qna-reply. |
| 111 | $new_element_name = 0 === $comment_parent_id ? 'comment-item' : TDE_APP_PREFIX . '-qna-reply'; |
| 112 | |
| 113 | $new_element = Preview::generateQnAElement( $thread, $new_element_name, $collection_data ); |
| 114 | |
| 115 | wp_send_json_success( |
| 116 | array( |
| 117 | 'html' => $new_element, |
| 118 | 'inserted_comment_id' => $wpdb->insert_id, |
| 119 | ) |
| 120 | ); |
| 121 | } |
| 122 | |
| 123 | wp_send_json_error( 'Invalid request' ); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Get comment |
| 128 | * |
| 129 | * @param int $id comment id. |
| 130 | * @return object |
| 131 | * @since 1.0.0 |
| 132 | */ |
| 133 | private function get_comment( $id ) { |
| 134 | $comment = (object) (array) get_comment( $id ); |
| 135 | |
| 136 | if ( $comment instanceof stdClass ) { |
| 137 | $author_posts_page_link = $comment->comment_author_url; |
| 138 | |
| 139 | if ( ! $author_posts_page_link ) { |
| 140 | $author_posts_page_link = \get_author_posts_url( $comment->user_id ); |
| 141 | } |
| 142 | |
| 143 | $comment->author_profile_picture = get_avatar_url( $comment->user_id ); |
| 144 | $comment->author_posts_page_link = $author_posts_page_link; |
| 145 | } |
| 146 | |
| 147 | return $comment; |
| 148 | } |
| 149 | } |
| 150 |