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.1 3.6.2 3.6.3 3.6.4 3.7.0 3.7.1 3.7.2 3.7.3 3.7.4 3.8.0 3.8.1 3.8.2 3.8.3 3.9.0 3.9.1 3.9.10 3.9.2 3.9.3 3.9.4 3.9.5 3.9.6 3.9.7 3.9.8 3.9.9
tutor / restapi / REST_Course.php
tutor / restapi Last commit date
REST_Author.php 2 years ago REST_Course.php 2 years ago REST_Course_Announcement.php 2 years ago REST_Lesson.php 2 years ago REST_Quiz.php 2 years ago REST_Rating.php 2 years ago REST_Response.php 2 years ago REST_Topic.php 2 years ago RestAuth.php 2 years ago
REST_Course.php
404 lines
1 <?php
2 /**
3 * Manage Course API
4 *
5 * @package Tutor\RestAPI
6 * @author Themeum <support@themeum.com>
7 * @link https://themeum.com
8 * @since 1.7.1
9 */
10
11 namespace TUTOR;
12
13 use WP_REST_Request;
14 use WP_Query;
15
16 if ( ! defined( 'ABSPATH' ) ) {
17 exit;
18 }
19
20 /**
21 * Rest_Course class
22 */
23 class REST_Course {
24
25 use REST_Response;
26
27
28 /**
29 * The post type associated with the course handler.
30 *
31 * @since 1.7.1
32 *
33 * @var string $post_type The post type for courses.
34 */
35 private $post_type;
36
37 /**
38 * The taxonomy for course categories.
39 *
40 * @since 1.7.1
41 *
42 * @var string $course_cat_tax The taxonomy for course categories.
43 */
44 private $course_cat_tax = 'course-category';
45
46 /**
47 * The taxonomy for course tags.
48 *
49 * @since 1.7.1
50 *
51 * @var string $course_tag_tax The taxonomy for course tags.
52 */
53 private $course_tag_tax = 'course-tag';
54
55 /**
56 * Constructor for the Tutor_Course_Handler class.
57 *
58 * Initializes the post type property.
59 *
60 * @since 1.7.1
61 */
62 public function __construct() {
63 $this->post_type = tutor()->course_post_type;
64 }
65
66 /**
67 * Course read API handler
68 *
69 * Get course list along with pagination, categories, tags
70 * author details, reviews
71 *
72 * @param WP_REST_Request $request request data.
73 *
74 * @return WP_REST_Response
75 */
76 public function course( WP_REST_Request $request ) {
77 $order = sanitize_text_field( $request->get_param( 'order' ) );
78 $orderby = sanitize_text_field( $request->get_param( 'orderby' ) );
79 $paged = sanitize_text_field( $request->get_param( 'paged' ) );
80
81 $args = array(
82 'post_type' => $this->post_type,
83 'post_status' => 'publish',
84 'posts_per_page' => 10,
85 'paged' => $paged ? $paged : 1,
86 'order' => $order ? $order : 'ASC',
87 'orderby' => $orderby ? $orderby : 'title',
88 );
89
90 $args = apply_filters( 'tutor_rest_course_query_args', $args );
91
92 $query = new WP_Query( $args );
93
94 // if post found.
95 if ( count( $query->posts ) > 0 ) {
96 // unset filter property.
97 array_map(
98 function( $post ) {
99 unset( $post->filter );
100 },
101 $query->posts
102 );
103
104 $data = array(
105 'posts' => array(),
106 'total_course' => $query->found_posts,
107 'total_page' => $query->max_num_pages,
108 );
109
110 foreach ( $query->posts as $post ) {
111 $category = wp_get_post_terms( $post->ID, $this->course_cat_tax );
112
113 $tag = wp_get_post_terms( $post->ID, $this->course_tag_tax );
114
115 $author = get_userdata( $post->post_author );
116
117 if ( $author ) {
118 // Unset user pass & key.
119 unset( $author->data->user_pass );
120 unset( $author->data->user_activation_key );
121 }
122
123 is_a( $author, 'WP_User' ) ? $post->post_author = $author->data : new \stdClass();
124
125 $thumbnail_size = apply_filters( 'tutor_rest_course_thumbnail_size', 'post-thumbnail' );
126 $post->thumbnail_url = get_the_post_thumbnail_url( $post->ID, $thumbnail_size );
127
128 $post->additional_info = $this->course_additional_info( $post->ID );
129
130 $post->ratings = tutor_utils()->get_course_rating( $post->ID );
131
132 $post->course_category = $category;
133
134 $post->course_tag = $tag;
135
136 $post = apply_filters( 'tutor_rest_course_single_post', $post );
137 array_push( $data['posts'], $post );
138 }
139
140 $response = array(
141 'status_code' => 'success',
142 'message' => __( 'Course retrieved successfully', 'tutor' ),
143 'data' => $data,
144 );
145
146 return self::send( $response );
147 }
148
149 $response = array(
150 'status_code' => 'not_found',
151 'message' => __( 'Course not found', 'tutor' ),
152 'data' => array(),
153 );
154
155 return self::send( $response );
156 }
157
158 /**
159 * Course Details API handler
160 *
161 * @since 1.7.1
162 *
163 * @param WP_REST_Request $request request params.
164 *
165 * @return WP_REST_Response
166 */
167 public function course_detail( WP_REST_Request $request ) {
168 $post_id = $request->get_param( 'id' );
169
170 $detail = $this->course_additional_info( $post_id );
171 if ( $detail ) {
172 $response = array(
173 'status_code' => 'course_detail',
174 'message' => __( 'Course detail retrieved successfully', 'tutor' ),
175 'data' => $detail,
176 );
177 return self::send( $response );
178 }
179 $response = array(
180 'status_code' => 'course_detail',
181 'message' => __( 'Detail not found for given ID', 'tutor' ),
182 'data' => array(),
183 );
184
185 return self::send( $response );
186 }
187
188 /**
189 * Get course additional info
190 *
191 * @since 2.6.1
192 *
193 * @param integer $post_id post id.
194 *
195 * @return array
196 */
197 public function course_additional_info( int $post_id ) {
198 $detail = array(
199
200 'course_settings' => get_post_meta( $post_id, '_tutor_course_settings', false ),
201
202 'course_price_type' => get_post_meta( $post_id, '_tutor_course_price_type', false ),
203
204 'course_duration' => get_post_meta( $post_id, '_course_duration', false ),
205
206 'course_level' => get_post_meta( $post_id, '_tutor_course_level', false ),
207
208 'course_benefits' => get_post_meta( $post_id, '_tutor_course_benefits', false ),
209
210 'course_requirements' => get_post_meta( $post_id, '_tutor_course_requirements', false ),
211
212 'course_target_audience' => get_post_meta( $post_id, '_tutor_course_target_audience', false ),
213
214 'course_material_includes' => get_post_meta( $post_id, '_tutor_course_material_includes', false ),
215
216 'video' => get_post_meta( $post_id, '_video', false ),
217
218 'disable_qa' => get_post_meta( $post_id, '_tutor_enable_qa', true ) != 'yes',
219 );
220
221 return apply_filters( 'tutor_course_additional_info', $detail );
222
223 }
224
225 /**
226 * Get Course by Terms API handler
227 *
228 * @since 1.7.1
229 *
230 * @param WP_REST_Request $request request params.
231 *
232 * @return WP_REST_Response
233 */
234 public function course_by_terms( WP_REST_Request $request ) {
235 $post_fields = $request->get_params();
236 $validate_err = $this->validate_terms( $post_fields );
237
238 // check array or not.
239 if ( count( $validate_err ) > 0 ) {
240 $response = array(
241 'status_code' => 'validation_error',
242 'message' => $validate_err,
243 'data' => array(),
244 );
245
246 return self::send( $response );
247 }
248
249 // sanitize terms.
250 $categories = sanitize_term( $request['categories'], $this->course_cat_tax, $context = 'db' );
251
252 $tags = sanitize_term( $request['tags'], $this->course_tag_tax, $context = 'db' );
253
254 $args = array(
255 'post_type' => $this->post_type,
256 'tax_query' => array(
257 'relation' => 'OR',
258 array(
259 'taxonomy' => $this->course_cat_tax,
260 'field' => 'name',
261 'terms' => $categories,
262 ),
263 array(
264 'taxonomy' => $this->course_tag_tax,
265 'field' => 'name',
266 'terms' => $tags,
267
268 ),
269 ),
270 );
271
272 $args = apply_filters( 'tutor_rest_course_by_terms_query_args', $args );
273 $query = new WP_Query( $args );
274
275 if ( count( $query->posts ) > 0 ) {
276 // unset filter property.
277 array_map(
278 function( $post ) {
279 unset( $post->filter );
280 },
281 $query->posts
282 );
283
284 $response = array(
285 'status_code' => 'success',
286 'message' => __( 'Course retrieved successfully', 'tutor' ),
287 'data' => $query->posts,
288 );
289
290 return self::send( $response );
291 }
292
293 $response = array(
294 'status_code' => 'not_found',
295 'message' => __( 'Course not found for given terms', 'tutor' ),
296 'data' => array(),
297 );
298 return self::send( $response );
299 }
300
301 /**
302 * Validate terms
303 *
304 * @since 1.7.1
305 *
306 * @param array $post post array.
307 *
308 * @return array validation errors.
309 */
310 public function validate_terms( array $post ) {
311 $categories = $post['categories'];
312 $tags = $post['tags'];
313
314 $error = array();
315
316 if ( ! is_array( $categories ) ) {
317 array_push( $error, __( 'Categories field is not an array', 'tutor' ) );
318 }
319
320 if ( ! is_array( $tags ) ) {
321 array_push( $error, __( 'Tags field is not an array', 'tutor' ) );
322 }
323
324 return $error;
325 }
326
327 /**
328 * Course sort by price API handler
329 *
330 * @since 1.7.1
331 *
332 * @param WP_REST_Request $request request params.
333 *
334 * @return WP_REST_Response
335 */
336 public function course_sort_by_price( WP_REST_Request $request ) {
337 $order = $request->get_param( 'order' );
338 $paged = $request->get_param( 'page' );
339
340 $order = sanitize_text_field( $order );
341 $paged = sanitize_text_field( $paged );
342
343 $args = array(
344 'post_type' => 'product',
345 'post_status' => 'publish',
346 'posts_per_page' => 10,
347 'paged' => $paged ? $paged : 1,
348
349 'meta_key' => '_regular_price',
350 'orderby' => 'meta_value_num',
351 'order' => $order,
352 'meta_query' => array(
353 'relation' => 'AND',
354 array(
355 'key' => '_tutor_product',
356 'value' => 'yes',
357
358 ),
359 ),
360 );
361 $args = apply_filters( 'tutor_rest_course_sort_by_price_args', $args );
362
363 $query = new WP_Query( $args );
364
365 if ( count( $query->posts ) > 0 ) {
366 // unset filter property.
367 array_map(
368 function( $post ) {
369 unset( $post->filter );
370 },
371 $query->posts
372 );
373
374 $posts = array();
375
376 foreach ( $query->posts as $post ) {
377 $post->price = get_post_meta( $post->ID, '_regular_price', true );
378 array_push( $posts, $post );
379 }
380
381 $data = array(
382 'posts' => $posts,
383 'total_course' => $query->found_posts,
384 'total_page' => $query->max_num_pages,
385 );
386
387 $response = array(
388 'status_code' => 'success',
389 'message' => __( 'Course retrieved successfully', 'tutor' ),
390 'data' => $data,
391 );
392
393 return self::send( $response );
394 }
395
396 $response = array(
397 'status' => 'not_found',
398 'message' => __( 'Course not found', 'tutor' ),
399 'data' => array(),
400 );
401 return self::send( $response );
402 }
403 }
404