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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 1 year ago REST_Rating.php 2 years ago REST_Response.php 2 years ago REST_Topic.php 2 years ago RestAuth.php 2 months ago
REST_Course.php
345 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 $categories = null;
81 if ( isset( $request['categories'] ) ) {
82 $categories = sanitize_term( explode( ',', $request['categories'] ), $this->course_cat_tax, $context = 'db' );
83 }
84 $tags = null;
85 if ( isset( $request['tags'] ) ) {
86 $tags = sanitize_term( explode( ',', $request['tags'] ), $this->course_tag_tax, $context = 'db' );
87 }
88
89 $post_per_page = tutor_utils()->get_option( 'pagination_per_page' );
90
91 $args = array(
92 'post_type' => $this->post_type,
93 'post_status' => 'publish',
94 'posts_per_page' => $post_per_page,
95 'paged' => $paged ? $paged : 1,
96 'order' => $order ? $order : 'ASC',
97 'orderby' => $orderby ? $orderby : 'title',
98 );
99
100 if ( isset( $categories ) || isset( $tags ) ) {
101 $args['tax_query'] = array(
102 'relation' => 'OR',
103 array(
104 'taxonomy' => $this->course_cat_tax,
105 'field' => 'name',
106 'terms' => $categories,
107 ),
108 array(
109 'taxonomy' => $this->course_tag_tax,
110 'field' => 'name',
111 'terms' => $tags,
112
113 ),
114 );
115 }
116
117 if ( 'price' === $orderby ) {
118 $args['post_type'] = 'product';
119 $args['meta_key'] = '_regular_price';
120 $args['meta_query'] = array(
121 'relation' => 'AND',
122 array(
123 'key' => '_tutor_product',
124 'value' => 'yes',
125 ),
126 );
127 }
128
129 $args = apply_filters( 'tutor_rest_course_query_args', $args );
130
131 $query = new WP_Query( $args );
132
133 // if post found.
134 if ( count( $query->posts ) > 0 ) {
135 // unset filter property.
136 array_map(
137 function ( $post ) {
138 unset( $post->filter );
139 },
140 $query->posts
141 );
142
143 $data = array(
144 'posts' => array(),
145 'total_course' => $query->found_posts,
146 'total_page' => $query->max_num_pages,
147 );
148
149 foreach ( $query->posts as $post ) {
150 $category = wp_get_post_terms( $post->ID, $this->course_cat_tax );
151
152 $tag = wp_get_post_terms( $post->ID, $this->course_tag_tax );
153
154 $author = get_userdata( $post->post_author );
155
156 if ( $author ) {
157 // Unset user pass & key.
158 unset( $author->data->user_pass );
159 unset( $author->data->user_activation_key );
160 }
161
162 is_a( $author, 'WP_User' ) ? $post->post_author = $author->data : new \stdClass();
163
164 $thumbnail_size = apply_filters( 'tutor_rest_course_thumbnail_size', 'post-thumbnail' );
165 $post->thumbnail_url = get_the_post_thumbnail_url( $post->ID, $thumbnail_size );
166
167 $post->additional_info = $this->course_additional_info( $post->ID );
168
169 $post->ratings = tutor_utils()->get_course_rating( $post->ID );
170
171 $post->course_category = $category;
172
173 $post->course_tag = $tag;
174
175 $post->price = get_post_meta( $post->ID, '_regular_price', true );
176
177 $post = apply_filters( 'tutor_rest_course_single_post', $post );
178
179 array_push( $data['posts'], $post );
180 }
181
182 $response = array(
183 'code' => 'success',
184 'message' => __( 'Course retrieved successfully', 'tutor' ),
185 'data' => $data,
186 );
187
188 return self::send( $response );
189 }
190
191 $response = array(
192 'code' => 'not_found',
193 'message' => __( 'Course not found', 'tutor' ),
194 'data' => array(),
195 );
196
197 return self::send( $response );
198 }
199
200 /**
201 * Course Details API handler
202 *
203 * @since 1.7.1
204 *
205 * @param WP_REST_Request $request request params.
206 *
207 * @return WP_REST_Response
208 */
209 public function course_detail( WP_REST_Request $request ) {
210 $post_id = $request->get_param( 'id' );
211
212 $detail = $this->course_additional_info( $post_id );
213 if ( $detail ) {
214 $response = array(
215 'code' => 'course_detail',
216 'message' => __( 'Course detail retrieved successfully', 'tutor' ),
217 'data' => $detail,
218 );
219 return self::send( $response );
220 }
221 $response = array(
222 'code' => 'course_detail',
223 'message' => __( 'Detail not found for given ID', 'tutor' ),
224 'data' => array(),
225 );
226
227 return self::send( $response );
228 }
229
230 /**
231 * Get course additional info
232 *
233 * @since 2.6.1
234 *
235 * @param integer $post_id post id.
236 *
237 * @return array
238 */
239 public function course_additional_info( int $post_id ) {
240 $detail = array(
241
242 'course_settings' => get_post_meta( $post_id, '_tutor_course_settings', false ),
243
244 'course_price_type' => get_post_meta( $post_id, '_tutor_course_price_type', false ),
245
246 'course_duration' => get_post_meta( $post_id, '_course_duration', false ),
247
248 'course_level' => get_post_meta( $post_id, '_tutor_course_level', false ),
249
250 'course_benefits' => get_post_meta( $post_id, '_tutor_course_benefits', false ),
251
252 'course_requirements' => get_post_meta( $post_id, '_tutor_course_requirements', false ),
253
254 'course_target_audience' => get_post_meta( $post_id, '_tutor_course_target_audience', false ),
255
256 'course_material_includes' => get_post_meta( $post_id, '_tutor_course_material_includes', false ),
257
258 'video' => get_post_meta( $post_id, '_video', false ),
259
260 'disable_qa' => get_post_meta( $post_id, '_tutor_enable_qa', true ) != 'yes',
261 );
262
263 return apply_filters( 'tutor_course_additional_info', $detail );
264 }
265
266 /**
267 * Validate terms
268 *
269 * @since 1.7.1
270 *
271 * @param array $post post array.
272 *
273 * @return array validation errors.
274 */
275 public function validate_terms( array $post ) {
276 $categories = $post['categories'];
277 $tags = $post['tags'];
278
279 $error = array();
280
281 if ( ! is_array( $categories ) ) {
282 array_push( $error, __( 'Categories field is not an array', 'tutor' ) );
283 }
284
285 if ( ! is_array( $tags ) ) {
286 array_push( $error, __( 'Tags field is not an array', 'tutor' ) );
287 }
288
289 return $error;
290 }
291
292
293
294 /**
295 * Retrieve the course contents for a given course id
296 *
297 * @since 2.7.0
298 *
299 * @param WP_REST_Request $request request params.
300 *
301 * @return WP_REST_Response
302 */
303 public function course_contents( WP_REST_Request $request ) {
304 $course_id = $request->get_param( 'id' );
305 $topics = tutor_utils()->get_topics( $course_id );
306
307 if ( $topics->have_posts() ) {
308 $data = array();
309 foreach ( $topics->get_posts() as $post ) {
310 $current_topic = array(
311 'id' => $post->ID,
312 'title' => $post->post_title,
313 'summary' => $post->post_content,
314 'contents' => array(),
315 );
316
317 $topic_contents = tutor_utils()->get_course_contents_by_topic( $post->ID, -1 );
318
319 if ( $topic_contents->have_posts() ) {
320 foreach ( $topic_contents->get_posts() as $post ) {
321 array_push( $current_topic['contents'], $post );
322 }
323 }
324
325 array_push( $data, $current_topic );
326 }
327
328 $response = array(
329 'code' => 'success',
330 'message' => __( 'Course contents retrieved successfully', 'tutor' ),
331 'data' => $data,
332 );
333 return self::send( $response );
334 }
335
336 $response = array(
337 'code' => 'not_found',
338 'message' => __( 'Contents for this course with the given course id not found', 'tutor' ),
339 'data' => array(),
340 );
341
342 return self::send( $response );
343 }
344 }
345