Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/tutor-pro/addons/course-bundle/src/Ajax.php

Keine Baseline-Datei – Diff nur gegen leer.
Zur Liste
1 -
1 + <?php
2 + /**
3 + * Handle Ajax Request.
4 + *
5 + * @package TutorPro\CourseBundle
6 + * @author Themeum <support@themeum.com>
7 + * @link https://themeum.com
8 + * @since 2.2.0
9 + */
10 +
11 + namespace TutorPro\CourseBundle;
12 +
13 + use TUTOR\Course;
14 + use Tutor\Helpers\HttpHelper;
15 + use TUTOR\Input;
16 + use Tutor\Models\CourseModel;
17 + use Tutor\Traits\JsonResponse;
18 + use TutorPro\CourseBundle\CustomPosts\CourseBundle;
19 + use TutorPro\CourseBundle\CustomPosts\ManagePostMeta;
20 + use TutorPro\CourseBundle\Models\BundleModel;
21 +
22 + /**
23 + * Ajax Class.
24 + *
25 + * @since 2.2.0
26 + */
27 + class Ajax {
28 +
29 + use JsonResponse;
30 +
31 + /**
32 + * Register hooks.
33 + *
34 + * @since 2.2.0
35 + *
36 + * @return void
37 + */
38 + public function __construct() {
39 + add_action( 'wp_ajax_tutor_get_bundle_list', array( $this, 'ajax_get_bundle_list' ) );
40 + add_action( 'wp_ajax_tutor_add_remove_course_to_bundle', array( $this, 'add_remove_course_to_bundle' ) );
41 + add_action( 'wp_ajax_tutor_create_course_bundle', array( $this, 'ajax_create_course_bundle' ) );
42 + add_action( 'wp_ajax_tutor_get_course_bundle_data', array( $this, 'ajax_get_bundle_data' ) );
43 + }
44 +
45 + /**
46 + * Get course list
47 + *
48 + * @since 3.6.0
49 + *
50 + *
51 + * Refactor the arguments & response as per new design
52 + *
53 + * @return void
54 + */
55 + public function ajax_get_bundle_list() {
56 + // Validate nonce.
57 + tutor_utils()->checking_nonce();
58 +
59 + // Check user permission.
60 + if ( ! Utils::current_user_can_create_bundle() ) {
61 + $this->json_response(
62 + tutor_utils()->error_message(),
63 + null,
64 + HttpHelper::STATUS_UNAUTHORIZED
65 + );
66 + }
67 +
68 + $limit = Input::post( 'limit', 10, Input::TYPE_INT );
69 + $offset = Input::post( 'offset', 0, Input::TYPE_INT );
70 + $post_status = Input::post( 'post_status', null );
71 + $search_term = '';
72 +
73 + $filter = json_decode( wp_unslash( $_POST['filter'] ) ); //phpcs:ignore --sanitized already
74 + if ( ! empty( $filter ) && property_exists( $filter, 'search' ) ) {
75 + $search_term = Input::sanitize( $filter->search );
76 + }
77 +
78 + $args = array(
79 + 'post_status' => is_null( $post_status ) ? 'publish' : $post_status,
80 + 'posts_per_page' => $limit,
81 + 'offset' => $offset,
82 + 's' => $search_term,
83 + );
84 +
85 + $exclude = Input::post( 'exclude', array(), Input::TYPE_ARRAY );
86 + if ( count( $exclude ) ) {
87 + $exclude = array_filter(
88 + $exclude,
89 + function ( $id ) {
90 + return is_numeric( $id );
91 + }
92 + );
93 + $args['post__not_in'] = $exclude;
94 + }
95 +
96 + $bundles = BundleModel::get_bundle_list( $args );
97 +
98 + $response = array(
99 + 'results' => array(),
100 + 'total_items' => 0,
101 + );
102 +
103 + $response['total_items'] = is_a( $bundles, 'WP_Query' ) ? $bundles->found_posts : 0;
104 +
105 + if ( is_a( $bundles, 'WP_Query' ) && $bundles->have_posts() ) {
106 + $bundles = $bundles->get_posts();
107 + foreach ( $bundles as $bundle ) {
108 + $response['results'][] = array(
109 + 'id' => $bundle->ID,
110 + 'title' => $bundle->post_title,
111 + 'image' => get_tutor_course_thumbnail_src( 'post-thumbnail', $bundle->ID ),
112 + 'total_courses' => BundleModel::get_total_courses_in_bundle( $bundle->ID ),
113 + );
114 + }
115 + }
116 +
117 + $this->json_response(
118 + __( 'Bundle list retrieved successfully!', 'tutor-pro' ),
119 + $response
120 + );
121 + }
122 +
123 + /**
124 + * Get course bundle data
125 + *
126 + * All the course bundle related data will be returned.
127 + *
128 + * @since 3.7.1 Allow admin to add/remove course from bundle
129 + * regardless of enrollments
130 + *
131 + * @since 2.2.0
132 + *
133 + * @return void send wp_json response
134 + */
135 + public function add_remove_course_to_bundle() {
136 + // Validate nonce.
137 + tutor_utils()->checking_nonce();
138 +
139 + // Post data.
140 + $bundle_id = Input::post( 'ID', 0, Input::TYPE_INT );
141 + $course_ids = Input::post( 'course_ids', array(), Input::TYPE_ARRAY );
142 + $user_action = Input::post( 'user_action', '', Input::TYPE_STRING );
143 +
144 + // Check user permission.
145 + if ( ! Utils::current_user_can_update_bundle( $bundle_id ) ) {
146 + $this->json_response(
147 + tutor_utils()->error_message(),
148 + null,
149 + HttpHelper::STATUS_UNAUTHORIZED
150 + );
151 + }
152 +
153 + if ( ! $bundle_id || CourseBundle::POST_TYPE !== get_post_type( $bundle_id ) ) {
154 + $this->json_response(
155 + tutor_utils()->error_message( 'validation_error' ),
156 + __( 'Invalid bundle id or post type', 'tutor-pro' ),
157 + HttpHelper::STATUS_BAD_REQUEST
158 + );
159 + }
160 +
161 + if ( ! empty( $course_ids ) ) {
162 + // Remove course from bundle if user action is remove.
163 + if ( 'remove_course' === $user_action ) {
164 + BundleModel::remove_course_from_bundle( $course_ids[0], $bundle_id );
165 + } else {
166 + // Otherwise add courses to the bundle.
167 + $existing_course_ids = BundleModel::get_bundle_course_ids( $bundle_id );
168 + $course_ids_to_add = array_diff( $course_ids, $existing_course_ids );
169 + if ( ! empty( $course_ids_to_add ) ) {
170 + $updated_course_ids = array_merge( $existing_course_ids, $course_ids_to_add );
171 + $update = BundleModel::update_bundle_course_ids( $bundle_id, $updated_course_ids );
172 +
173 + // If bundle course update failed.
174 + if ( ! $update ) {
175 + $this->json_response(
176 + __( 'Course could not added to the bundle.', 'tutor-pro' ),
177 + null,
178 + HttpHelper::STATUS_INTERNAL_SERVER_ERROR
179 + );
180 + }
181 +
182 + // Do action.
183 + do_action( 'tutor_course_bundle_course_added', $bundle_id, $course_ids );
184 + } else {
185 + $this->json_response(
186 + __( 'Course already added to the bundle.', 'tutor-pro' ),
187 + null,
188 + HttpHelper::STATUS_BAD_REQUEST
189 + );
190 + }
191 + }
192 + } else {
193 + $this->json_response(
194 + __( 'No courses found to add to the bundle.', 'tutor-pro' ),
195 + null,
196 + HttpHelper::STATUS_BAD_REQUEST
197 + );
198 + }
199 +
200 + $bundle_data = BundleModel::get_bundle_data( $bundle_id );
201 +
202 + $regular_price = BundleModel::get_bundle_regular_price( $bundle_id );
203 + $sale_price = tutor_utils()->get_raw_course_price( $bundle_id )->sale_price;
204 +
205 + if ( $sale_price > $regular_price ) {
206 +
207 + update_post_meta( $bundle_id, Course::COURSE_SALE_PRICE_META, 0 );
208 + $bundle_data['subtotal_sale_price'] = '';
209 + $bundle_data['subtotal_raw_sale_price'] = '';
210 +
211 + }
212 +
213 + $this->json_response(
214 + 'remove_course' === $user_action ?
215 + __( 'Course removed from the bundle.', 'tutor-pro' ) :
216 + __( 'Course added to the bundle.', 'tutor-pro' ),
217 + $bundle_data
218 + );
219 + }
220 +
221 + /**
222 + * Create course bundle.
223 + *
224 + * @since 3.2.0
225 + *
226 + * @return void send wp_json response
227 + */
228 + public function ajax_create_course_bundle() {
229 +
230 + tutor_utils()->check_nonce();
231 + if ( ! Utils::current_user_can_create_bundle() ) {
232 + $this->json_response(
233 + tutor_utils()->error_message(),
234 + null,
235 + HttpHelper::STATUS_UNAUTHORIZED
236 + );
237 + }
238 +
239 + $post = Input::sanitize_array(
240 + $_POST,//phpcs:ignore
241 + array(
242 + 'post_content' => 'wp_kses_post',
243 + 'course_benefits' => 'sanitize_textarea_field',
244 + )
245 + );
246 +
247 + $post['post_type'] = CourseBundle::POST_TYPE;
248 + $post['post_title'] = Input::post( 'post_title', __( 'New Bundle', 'tutor-pro' ), Input::TYPE_STRING );
249 + $post['post_name'] = Input::post( 'post_name', 'new-bundle', Input::TYPE_STRING );
250 + $sale_price = Input::post( 'sale_price', 0, Input::TYPE_NUMERIC );
251 +
252 + if ( isset( $post['ID'] ) ) {
253 +
254 + if ( CourseBundle::POST_TYPE !== get_post_type( $post['ID'] ) ) {
255 + $this->json_response(
256 + __( 'Invalid bundle id or post type', 'tutor-pro' ),
257 + null,
258 + HttpHelper::STATUS_BAD_REQUEST
259 + );
260 + }
261 +
262 + $bundle_id = $post['ID'];
263 + $regular_price = BundleModel::get_bundle_regular_price( $bundle_id );
264 + if ( $sale_price > $regular_price ) {
265 + $this->json_response(
266 + __( 'Sale price can not be greater than regular price', 'tutor-pro' ),
267 + null,
268 + HttpHelper::STATUS_BAD_REQUEST
269 + );
270 + }
271 +
272 + $post_author = get_post_field( 'post_author', $bundle_id );
273 +
274 + $post['post_author'] = $post_author;
275 + }
276 +
277 + $insert = wp_insert_post( $post );
278 + if ( is_wp_error( $insert ) ) {
279 + $this->json_response(
280 + $insert->get_error_message(),
281 + null,
282 + HttpHelper::STATUS_INTERNAL_SERVER_ERROR
283 + );
284 + }
285 +
286 + if ( isset( $post['course_ids'] ) ) {
287 + BundleModel::update_bundle_course_ids( $insert, $post['course_ids'] );
288 + }
289 +
290 + if ( isset( $post['thumbnail_id'] ) ) {
291 + set_post_thumbnail( $insert, $post['thumbnail_id'] );
292 + }
293 +
294 + if ( isset( $post['tax_on_single'] ) ) {
295 + update_post_meta( $insert, Course::TAX_ON_SINGLE_META, $post['tax_on_single'] );
296 + }
297 +
298 + if ( isset( $post['tax_on_subscription'] ) ) {
299 + update_post_meta( $insert, Course::TAX_ON_SUBSCRIPTION_META, $post['tax_on_subscription'] );
300 + }
301 +
302 + if ( isset( $post['source'] ) && 'frontend' === $post['source'] ) {
303 + $edit_url = Utils::construct_front_url( 'edit', $insert );
304 + } else {
305 + $edit_url = Utils::construct_page_url( 'edit', $insert );
306 + }
307 +
308 + do_action( 'tutor_save_bundle', $insert, $post );
309 +
310 + $this->json_response(
311 + __( 'Course Bundle updated successfully', 'tutor-pro' ),
312 + $edit_url,
313 + );
314 + }
315 +
316 + /**
317 + * Get course bundle data.
318 + *
319 + * @since 3.2.0
320 + *
321 + * @return void send wp_json response
322 + */
323 + public function ajax_get_bundle_data() {
324 + tutor_utils()->checking_nonce();
325 + $bundle_id = Input::post( 'bundle_id', 0, Input::TYPE_INT );
326 + if ( ! $bundle_id ) {
327 + $this->json_response(
328 + __( 'Invalid bundle id', 'tutor-pro' ),
329 + null,
330 + HttpHelper::STATUS_BAD_REQUEST
331 + );
332 + }
333 +
334 + $bundle_course = get_post( $bundle_id );
335 + $bundle_data = BundleModel::get_bundle_data( $bundle_id );
336 + if ( is_a( $bundle_course, 'WP_Post' ) ) {
337 + $bundle_course->post_name = urldecode( $bundle_course->post_name );
338 + $bundle_course->details = $bundle_data;
339 + } else {
340 + $this->json_response(
341 + __( 'Invalid bundle id', 'tutor-pro' ),
342 + null,
343 + HttpHelper::STATUS_BAD_REQUEST
344 + );
345 + }
346 +
347 + ! empty( get_post_meta( $bundle_id, ManagePostMeta::BUNDLE_RIBBON_META_KEY, true ) ) ? $bundle_course->ribbon_type = get_post_meta( $bundle_id, ManagePostMeta::BUNDLE_RIBBON_META_KEY, true ) : null;
348 + ! empty( get_post_meta( $bundle_id, Course::COURSE_SELLING_OPTION_META, true ) ) ? $bundle_course->course_selling_option = get_post_meta( $bundle_id, Course::COURSE_SELLING_OPTION_META, true ) : null;
349 +
350 + $editors = tutor_utils()->get_editor_list( $bundle_id );
351 +
352 + $bundle_course->course_benefits = get_post_meta( $bundle_id, CourseModel::BENEFITS_META_KEY, true );
353 + $bundle_course->preview_link = get_preview_post_link( $bundle_id );
354 + $bundle_course->thumbnail_id = get_post_meta( $bundle_id, '_thumbnail_id', true );
355 + $bundle_course->thumbnail = get_the_post_thumbnail_url( $bundle_id );
356 + $bundle_course->total_enrolled = BundleModel::get_total_bundle_sold( $bundle_id );
357 + $bundle_course->editors = array_values( $editors );
358 + $bundle_course->editor_used = tutor_utils()->get_editor_used( $bundle_id );
359 +
360 + $tax_on_single = get_post_meta( $bundle_id, Course::TAX_ON_SINGLE_META, true );
361 + $tax_on_subscription = get_post_meta( $bundle_id, Course::TAX_ON_SUBSCRIPTION_META, true );
362 +
363 + $bundle_course->tax_collection = array(
364 + 'tax_on_single' => '' === $tax_on_single ? '1' : $tax_on_single,
365 + 'tax_on_subscription' => '' === $tax_on_subscription ? '1' : $tax_on_subscription,
366 + );
367 +
368 + $bundle_course = apply_filters( 'tutor_bundle_details_response', $bundle_course );
369 +
370 + $this->json_response(
371 + __( 'Success', 'tutor-pro' ),
372 + $bundle_course
373 + );
374 + }
375 + }
376 +