PluginProbe
Tutor LMS – eLearning and online course solution / 1.7.0
Tutor LMS – eLearning and online course solution v1.7.0
4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 4.0.2 4.0.1 4.0.0 3.9.15 3.9.14 3.9.13 3.9.12 3.9.11 trunk 1.0.0 1.0.0-alpha 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 All 191 releases
← All changes | classes/RestAPI.php +120 -460 trunk1.7.0 View file →
@@ -1,460 +1,120 @@
1 -<?php //phpcs:ignore WordPress.Files.FileName.NotHyphenatedLowercase
2 -/**
3 - * RestAPI class
4 - *
5 - * @package Tutor\API
6 - * @author Themeum <support@themeum.com>
7 - * @link https://themeum.com
8 - * @since 1.5.0
9 - */
10 -
11 -namespace TUTOR;
12 -
13 -if ( ! defined( 'ABSPATH' ) ) {
14 - exit;
15 -}
16 -
17 -/**
18 - * Initialize REST API
19 - *
20 - * @since 1.5.0
21 - */
22 -class RestAPI {
23 -
24 - /**
25 - * Custom validation trait
26 - */
27 - use Custom_Validation;
28 -
29 - /**
30 - * API namespace
31 - *
32 - * @var string
33 - */
34 - private $namespace = 'tutor/v1';
35 -
36 - /**
37 - * Course post type
38 - *
39 - * @var string
40 - */
41 - protected $course_post_type;
42 -
43 - /**
44 - * Plugin dir Path
45 - *
46 - * @var string
47 - */
48 - private $path;
49 -
50 - /**
51 - * Course Object
52 - *
53 - * @var object
54 - */
55 - private $course_obj;
56 -
57 - /**
58 - * Topic Object
59 - *
60 - * @var object
61 - */
62 - private $topic_obj;
63 -
64 - /**
65 - * Lesson Object
66 - *
67 - * @var object
68 - */
69 - private $lesson_obj;
70 -
71 - /**
72 - * Announcement Object
73 - *
74 - * @var object
75 - */
76 - private $announcement_obj;
77 -
78 - /**
79 - * Quiz Object
80 - *
81 - * @var object
82 - */
83 - private $quiz_obj;
84 -
85 - /**
86 - * Author Object
87 - *
88 - * @var object
89 - */
90 - private $author_obj;
91 -
92 - /**
93 - * Rating Object
94 - *
95 - * @var object
96 - */
97 - private $rating_obj;
98 -
99 - /**
100 - * Manage dependencies
101 - *
102 - * @since 1.5.0
103 - */
104 - public function __construct() {
105 -
106 - $this->path = plugin_dir_path( TUTOR_FILE );
107 -
108 - spl_autoload_register( array( $this, 'loader' ) );
109 -
110 - $this->course_obj = new REST_Course();
111 - $this->topic_obj = new REST_Topic();
112 - $this->lesson_obj = new REST_Lesson();
113 - $this->announcement_obj = new REST_Course_Announcement();
114 - $this->quiz_obj = new REST_Quiz();
115 - $this->author_obj = new REST_Author();
116 - $this->rating_obj = new REST_Rating();
117 -
118 - add_action( 'rest_api_init', array( $this, 'init_routes' ) );
119 - add_filter( 'rest_request_before_callbacks', array( $this, 'check_permission' ), 10, 3 );
120 - }
121 -
122 - /**
123 - * Check permission before dispatching REST request.
124 - *
125 - * @todo will remove and prevent by capability where needed.
126 - *
127 - * @since 4.0.1
128 - *
129 - * @param mixed $response response.
130 - * @param mixed $handler handler.
131 - * @param \WP_REST_Request $request request.
132 - *
133 - * @return mixed|\WP_Error
134 - */
135 - public function check_permission( $response, $handler, $request ) {
136 - $id = absint( $request['id'] );
137 - $method = $request->get_method();
138 - $write_methods = array( 'POST', 'PUT', 'PATCH', 'DELETE' );
139 - $is_write = in_array( $method, $write_methods, true );
140 -
141 - if ( ! $id ) {
142 - return $response;
143 - }
144 -
145 - $post = get_post( $id );
146 -
147 - if ( ! $post ) {
148 - return $response;
149 - }
150 -
151 - $post_types = array(
152 - tutor()->course_post_type,
153 - tutor()->bundle_post_type,
154 - );
155 -
156 - if ( ! in_array( $post->post_type, $post_types, true ) ) {
157 - return $response;
158 - }
159 -
160 - // Prevent write actions which are only allowed for author.
161 - if ( $is_write && ! current_user_can( 'edit_post', $id ) ) {
162 - return new \WP_Error(
163 - 'rest_forbidden',
164 - tutor_utils()->error_message(),
165 - array( 'status' => rest_authorization_required_code() )
166 - );
167 - }
168 -
169 - // Prevent access to non-publish posts, only author can access.
170 - if ( 'publish' !== $post->post_status && ! current_user_can( 'edit_post', $id ) ) {
171 - return new \WP_Error(
172 - 'rest_forbidden',
173 - tutor_utils()->error_message(),
174 - array( 'status' => rest_authorization_required_code() )
175 - );
176 - }
177 -
178 - return $response;
179 - }
180 -
181 - /**
182 - * Class loading
183 - *
184 - * @since 1.5.0
185 - *
186 - * @param string $class_name class name to load.
187 - *
188 - * @return void
189 - */
190 - private function loader( $class_name ) {
191 - if ( ! class_exists( $class_name ) ) {
192 - $class_name = preg_replace(
193 - array( '/([a-z])([A-Z])/', '/\\\/' ),
194 - array( '$1$2', DIRECTORY_SEPARATOR ),
195 - $class_name
196 - );
197 -
198 - $class_name = str_replace( 'TUTOR' . DIRECTORY_SEPARATOR, 'restapi' . DIRECTORY_SEPARATOR, $class_name );
199 - $file_name = $this->path . $class_name . '.php';
200 -
201 - if ( file_exists( $file_name ) ) {
202 - require_once $file_name;
203 - }
204 - }
205 - }
206 -
207 - /**
208 - * Initialize routes
209 - *
210 - * @since 1.5.0
211 - *
212 - * @return void
213 - */
214 - public function init_routes() {
215 - // Courses.
216 - register_rest_route(
217 - $this->namespace,
218 - '/courses',
219 - array(
220 - 'methods' => 'GET',
221 - 'callback' => array(
222 - $this->course_obj,
223 - 'course',
224 - ),
225 - 'permission_callback' => array( RestAuth::class, 'process_api_request' ),
226 - )
227 - );
228 -
229 - // Course details.
230 - register_rest_route(
231 - $this->namespace,
232 - '/courses/(?P<id>\d+)',
233 - array(
234 - 'methods' => 'GET',
235 - 'callback' => array(
236 - $this->course_obj,
237 - 'course_detail',
238 - ),
239 - 'args' => array(
240 - 'id' => array(
241 - 'validate_callback' => function ( $param ) {
242 - return is_numeric( $param );
243 - },
244 - ),
245 - ),
246 - 'permission_callback' => array( RestAuth::class, 'process_api_request' ),
247 - )
248 - );
249 -
250 - // Course topic.
251 - register_rest_route(
252 - $this->namespace,
253 - '/topics',
254 - array(
255 - 'methods' => 'GET',
256 - 'callback' => array(
257 - $this->topic_obj,
258 - 'course_topic',
259 - ),
260 - 'args' => array(
261 - 'course_id' => array(
262 - 'validate_callback' => function ( $param ) {
263 - return is_numeric( $param );
264 - },
265 - ),
266 - ),
267 - 'permission_callback' => array( RestAuth::class, 'process_api_request' ),
268 - )
269 - );
270 -
271 - // Lesson by topic.
272 - register_rest_route(
273 - $this->namespace,
274 - '/lessons',
275 - array(
276 - 'methods' => 'GET',
277 - 'callback' => array(
278 - $this->lesson_obj,
279 - 'topic_lesson',
280 - ),
281 - 'args' => array(
282 - 'topic_id' => array(
283 - 'validate_callback' => function ( $param ) {
284 - return is_numeric( $param );
285 - },
286 - ),
287 - ),
288 - 'permission_callback' => array( RestAuth::class, 'process_api_request' ),
289 - )
290 - );
291 -
292 - // Course announcement by course id.
293 - register_rest_route(
294 - $this->namespace,
295 - '/course-announcement/(?P<id>\d+)',
296 - array(
297 - 'methods' => 'GET',
298 - 'callback' => array(
299 - $this->announcement_obj,
300 - 'course_announcement',
301 - ),
302 - 'args' => array(
303 - 'id' => array(
304 - 'validate_callback' => function ( $param ) {
305 - return is_numeric( $param );
306 - },
307 - ),
308 - ),
309 - 'permission_callback' => array( RestAuth::class, 'process_api_request' ),
310 - )
311 - );
312 -
313 - // Quiz by topic id.
314 - register_rest_route(
315 - $this->namespace,
316 - '/quizzes',
317 - array(
318 - 'methods' => 'GET',
319 - 'callback' => array(
320 - $this->quiz_obj,
321 - 'quiz_with_settings',
322 - ),
323 - 'args' => array(
324 - 'topic_id' => array(
325 - 'validate_callback' => function ( $param ) {
326 - return is_numeric( $param );
327 - },
328 - ),
329 - ),
330 - 'permission_callback' => array( RestAuth::class, 'process_api_request' ),
331 - )
332 - );
333 -
334 - // Quiz by quiz id.
335 - register_rest_route(
336 - $this->namespace,
337 - '/quizzes/(?P<id>\d+)',
338 - array(
339 - 'methods' => 'GET',
340 - 'callback' => array(
341 - $this->quiz_obj,
342 - 'get_quiz',
343 - ),
344 - 'args' => array(
345 - 'id' => array(
346 - 'validate_callback' => function ( $param ) {
347 - return is_numeric( $param );
348 - },
349 - ),
350 - ),
351 - 'permission_callback' => array( RestAuth::class, 'process_api_request' ),
352 - )
353 - );
354 -
355 - // Quiz question answer by quiz id.
356 - register_rest_route(
357 - $this->namespace,
358 - '/quiz-question-answer/(?P<id>\d+)',
359 - array(
360 - 'methods' => 'GET',
361 - 'callback' => array(
362 - $this->quiz_obj,
363 - 'quiz_question_ans',
364 - ),
365 - 'args' => array(
366 - 'id' => array(
367 - 'validate_callback' => function ( $param ) {
368 - return is_numeric( $param );
369 - },
370 - ),
371 - ),
372 - 'permission_callback' => array( RestAuth::class, 'process_api_request' ),
373 - )
374 - );
375 -
376 - // Quiz attempt details by quiz id.
377 - register_rest_route(
378 - $this->namespace,
379 - '/quiz-attempt-details/(?P<id>\d+)',
380 - array(
381 - 'methods' => 'GET',
382 - 'callback' => array(
383 - $this->quiz_obj,
384 - 'quiz_attempt_details',
385 - ),
386 - 'args' => array(
387 - 'id' => array(
388 - 'validate_callback' => function ( $param ) {
389 - return is_numeric( $param );
390 - },
391 - ),
392 - ),
393 - 'permission_callback' => array( RestAuth::class, 'process_api_request' ),
394 - )
395 - );
396 -
397 - // Author detail by id.
398 - register_rest_route(
399 - $this->namespace,
400 - '/author-information/(?P<id>\d+)',
401 - array(
402 - 'methods' => 'GET',
403 - 'callback' => array(
404 - $this->author_obj,
405 - 'author_detail',
406 - ),
407 - 'args' => array(
408 - 'id' => array(
409 - 'validate_callback' => function ( $param ) {
410 - return is_numeric( $param );
411 - },
412 - ),
413 - ),
414 - 'permission_callback' => array( RestAuth::class, 'process_api_request' ),
415 - )
416 - );
417 -
418 - // Reviews by course id.
419 - register_rest_route(
420 - $this->namespace,
421 - '/course-rating/(?P<id>\d+)',
422 - array(
423 - 'methods' => 'GET',
424 - 'callback' => array(
425 - $this->rating_obj,
426 - 'course_rating',
427 - ),
428 - 'args' => array(
429 - 'id' => array(
430 - 'validate_callback' => function ( $param ) {
431 - return is_numeric( $param );
432 - },
433 - ),
434 - ),
435 - 'permission_callback' => array( RestAuth::class, 'process_api_request' ),
436 - )
437 - );
438 -
439 - // Get course content by id.
440 - register_rest_route(
441 - $this->namespace,
442 - '/course-contents/(?P<id>\d+)',
443 - array(
444 - 'methods' => 'GET',
445 - 'callback' => array(
446 - $this->course_obj,
447 - 'course_contents',
448 - ),
449 - 'args' => array(
450 - 'id' => array(
451 - 'validate_callback' => function ( $param ) {
452 - return is_numeric( $param );
453 - },
454 - ),
455 - ),
456 - 'permission_callback' => array( RestAuth::class, 'process_api_request' ),
457 - )
458 - );
459 - }
460 -}
1 +<?php
2 +
3 +/**
4 + * RestAPI class
5 + *
6 + * @author: themeum
7 + * @author_uri: https://themeum.com
8 + * @package Tutor
9 + * @since v.1.5.0
10 + */
11 +
12 +
13 +namespace TUTOR;
14 +
15 +if (!defined('ABSPATH'))
16 + exit;
17 +
18 +class RestAPI {
19 +
20 + protected $namespace = 'tutor/v1';
21 + protected $course_post_type;
22 +
23 + public function __construct() {
24 + $this->course_post_type = tutor()->course_post_type;
25 +
26 + add_action('rest_api_init', array($this, 'rest_api_init'));
27 + }
28 +
29 +
30 + public function rest_api_init() {
31 + register_rest_route(
32 + $this->namespace, 'courses',
33 + array(
34 + 'methods' => 'GET',
35 + 'callback' => array($this, 'courses_api'),
36 + 'permission_callback' => '__return_true'
37 + )
38 + );
39 + }
40 +
41 + public function courses_api() {
42 + global $wpdb;
43 +
44 + $a = array_merge(array(
45 + 'post_type' => $this->course_post_type,
46 + 'post_status' => 'publish',
47 +
48 + 'id' => '',
49 + 'exclude_ids' => '',
50 + 'category' => '',
51 +
52 + 'orderby' => 'ID',
53 + 'order' => 'DESC',
54 + 'count' => '10',
55 + ), $_GET);
56 +
57 + $limit = (int) $a['count'];
58 + $exclude_ids_query = '';
59 + $in_ids_query = '';
60 + $tax_join = '';
61 + $tax_where = '';
62 +
63 + $orderby = sanitize_text_field($a['orderby']);
64 + $order = sanitize_text_field($a['order']);
65 +
66 + /**
67 + * Exclude Course IDS
68 + */
69 + if (!empty($a['exclude_ids'])) {
70 + $exclude_ids = (array) explode(',', sanitize_text_field($a['exclude_ids']));
71 + if (tutils()->count($exclude_ids)) {
72 + $exclude_ids_query = "AND ID NOT IN('$exclude_ids')";
73 + }
74 + }
75 +
76 + if (!empty($a['id'])) {
77 + $ids = (array) explode(',', $a['id']);
78 + if (tutils()->count($ids)) {
79 + $in_ids_query = "AND ID IN('$ids')";
80 + }
81 + }
82 +
83 + if (!empty($a['category'])) {
84 + $category = (array) explode(',', $a['category']);
85 + $tax = new \WP_Tax_Query(
86 + array(
87 + array(
88 + 'taxonomy' => 'course-category',
89 + 'field' => 'term_id',
90 + 'terms' => $category,
91 + 'operator' => 'IN',
92 + )
93 + )
94 + );
95 +
96 + $tax_sql = $tax->get_sql($wpdb->posts, 'ID');
97 + $tax_join = tutils()->array_get('join', $tax_sql);
98 + $tax_where = tutils()->array_get('where', $tax_sql);
99 + }
100 +
101 + $course_post_type = tutor()->course_post_type;
102 + $query = $wpdb->get_results("SELECT ID, post_author, post_title
103 + from {$wpdb->posts}
104 +
105 + {$tax_join}
106 +
107 + WHERE 1=1 AND post_status = 'publish'
108 + {$exclude_ids_query}
109 + {$in_ids_query}
110 + {$tax_where}
111 + AND post_type = '{$course_post_type}' ORDER BY {$orderby} {$order} LIMIT {$limit} ", ARRAY_A);
112 +
113 +
114 + if (tutils()->count($query)) {
115 + $results = apply_filters('tutor/api/get_courses', $query);
116 + wp_send_json_success($results);
117 + }
118 + wp_send_json_error();
119 + }
120 +}