PluginProbe
Tutor LMS – eLearning and online course solution / 2.0.2
Tutor LMS – eLearning and online course solution v2.0.2
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 +166 -314 trunk2.0.2 View file →
@@ -1,460 +1,312 @@
1 -<?php //phpcs:ignore WordPress.Files.FileName.NotHyphenatedLowercase
1 +<?php
2 2 /**
3 3 * RestAPI class
4 4 *
5 - * @package Tutor\API
6 - * @author Themeum <support@themeum.com>
7 - * @link https://themeum.com
8 - * @since 1.5.0
5 + * @author: themeum
6 + * @author_uri: https://themeum.com
7 + * @package Tutor
8 + * @since v.1.5.0
9 9 */
10 10
11 11 namespace TUTOR;
12 12
13 -if ( ! defined( 'ABSPATH' ) ) {
13 +if (!defined('ABSPATH'))
14 14 exit;
15 -}
16 15
17 -/**
18 - * Initialize REST API
19 - *
20 - * @since 1.5.0
21 - */
22 16 class RestAPI {
23 17
24 - /**
25 - * Custom validation trait
26 - */
27 18 use Custom_Validation;
28 19
29 - /**
30 - * API namespace
31 - *
32 - * @var string
33 - */
34 20 private $namespace = 'tutor/v1';
35 21
36 - /**
37 - * Course post type
38 - *
39 - * @var string
40 - */
41 22 protected $course_post_type;
42 23
43 - /**
44 - * Plugin dir Path
45 - *
46 - * @var string
47 - */
48 24 private $path;
49 25
50 - /**
51 - * Course Object
52 - *
53 - * @var object
54 - */
55 - private $course_obj;
26 + private $courseObj;
56 27
57 - /**
58 - * Topic Object
59 - *
60 - * @var object
61 - */
62 - private $topic_obj;
28 + private $topicObj;
63 29
64 - /**
65 - * Lesson Object
66 - *
67 - * @var object
68 - */
69 - private $lesson_obj;
30 + private $lessonObj;
70 31
71 - /**
72 - * Announcement Object
73 - *
74 - * @var object
75 - */
76 - private $announcement_obj;
32 + private $annoucementObj;
77 33
78 - /**
79 - * Quiz Object
80 - *
81 - * @var object
82 - */
83 - private $quiz_obj;
34 + private $quizObj;
84 35
85 - /**
86 - * Author Object
87 - *
88 - * @var object
89 - */
90 - private $author_obj;
36 + private $authorObj;
91 37
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 - */
38 + private $ratingObj;
39 +
104 40 public function __construct() {
105 41
106 - $this->path = plugin_dir_path( TUTOR_FILE );
42 + $this->path = plugin_dir_path(TUTOR_FILE);
43 +
44 + spl_autoload_register(array($this, 'loader'));
107 45
108 - spl_autoload_register( array( $this, 'loader' ) );
109 46
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();
47 + $this->courseObj = new REST_Course;
48 + $this->topicObj = new REST_Topic;
49 + $this->lessonObj = new REST_Lesson;
50 + $this->annoucementObj = new REST_Course_Announcement;
51 + $this->quizObj = new REST_Quiz;
52 + $this->authorObj = new REST_Author;
53 + $this->ratingObj = new REST_Rating;
117 54
118 - add_action( 'rest_api_init', array( $this, 'init_routes' ) );
119 - add_filter( 'rest_request_before_callbacks', array( $this, 'check_permission' ), 10, 3 );
55 + add_action('rest_api_init', array($this, 'init_routes'));
120 56 }
121 57
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 58
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() )
59 + private function loader($className) {
60 + if (!class_exists($className)) {
61 + $className = preg_replace(
62 + array('/([a-z])([A-Z])/', '/\\\/'),
63 + array('$1$2', DIRECTORY_SEPARATOR),
64 + $className
166 65 );
167 - }
168 66
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 - }
67 + $className = str_replace('TUTOR' . DIRECTORY_SEPARATOR, 'restapi' . DIRECTORY_SEPARATOR, $className);
68 + $file_name = $this->path . $className . '.php';
177 69
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 ) ) {
70 + if (file_exists($file_name)) {
202 71 require_once $file_name;
203 72 }
204 73 }
205 74 }
206 75
207 - /**
208 - * Initialize routes
209 - *
210 - * @since 1.5.0
211 - *
212 - * @return void
213 - */
76 + /*
77 + init all routes for api
78 + */
214 79 public function init_routes() {
215 - // Courses.
80 + //courses
216 81 register_rest_route(
217 82 $this->namespace,
218 83 '/courses',
219 84 array(
220 - 'methods' => 'GET',
221 - 'callback' => array(
222 - $this->course_obj,
223 - 'course',
85 + 'methods' => "GET",
86 + 'callback' => array(
87 + $this->courseObj, 'course'
224 88 ),
225 - 'permission_callback' => array( RestAuth::class, 'process_api_request' ),
89 + 'permission_callback' => '__return_true'
226 90 )
227 91 );
228 92
229 - // Course details.
93 + //courses by terms cat and tag
230 94 register_rest_route(
231 95 $this->namespace,
232 - '/courses/(?P<id>\d+)',
96 + '/course-by-terms',
233 97 array(
234 - 'methods' => 'GET',
235 - 'callback' => array(
236 - $this->course_obj,
237 - 'course_detail',
98 + 'methods' => "POST",
99 + 'callback' => array(
100 + $this->courseObj, 'course_by_terms'
238 101 ),
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' ),
102 + 'permission_callback' => '__return_true'
247 103 )
248 104 );
249 105
250 - // Course topic.
106 + //courses by terms cat and tag
251 107 register_rest_route(
252 108 $this->namespace,
253 - '/topics',
109 + '/course-sorting-by-price',
254 110 array(
255 - 'methods' => 'GET',
256 - 'callback' => array(
257 - $this->topic_obj,
258 - 'course_topic',
111 + 'methods' => "GET",
112 + 'callback' => array(
113 + $this->courseObj, 'course_sort_by_price'
259 114 ),
260 - 'args' => array(
261 - 'course_id' => array(
262 - 'validate_callback' => function ( $param ) {
263 - return is_numeric( $param );
264 - },
115 + 'args' => array(
116 + 'order' => array(
117 + 'required' => true,
118 + 'type' => 'string',
119 + 'validate_callback' => function ($order) {
120 + return $this->validate_order($order);
121 + }
265 122 ),
123 + 'page' => array(
124 + 'required' => false,
125 + 'type' => 'number'
126 + )
266 127 ),
267 - 'permission_callback' => array( RestAuth::class, 'process_api_request' ),
128 + 'permission_callback' => '__return_true'
268 129 )
269 130 );
270 131
271 - // Lesson by topic.
132 + //course details
272 133 register_rest_route(
273 134 $this->namespace,
274 - '/lessons',
135 + '/course-detail/(?P<id>\d+)',
275 136 array(
276 - 'methods' => 'GET',
277 - 'callback' => array(
278 - $this->lesson_obj,
279 - 'topic_lesson',
137 + 'methods' => 'GET',
138 + 'callback' => array(
139 + $this->courseObj, 'course_detail'
280 140 ),
281 - 'args' => array(
282 - 'topic_id' => array(
283 - 'validate_callback' => function ( $param ) {
284 - return is_numeric( $param );
285 - },
286 - ),
141 + 'args' => array(
142 + 'id' => array(
143 + 'validate_callback' => function ($param) {
144 + return is_numeric($param);
145 + }
146 + )
287 147 ),
288 - 'permission_callback' => array( RestAuth::class, 'process_api_request' ),
148 + 'permission_callback' => '__return_true'
289 149 )
290 150 );
291 151
292 - // Course announcement by course id.
152 + //course topic
293 153 register_rest_route(
294 154 $this->namespace,
295 - '/course-announcement/(?P<id>\d+)',
155 + '/course-topic/(?P<id>\d+)',
296 156 array(
297 - 'methods' => 'GET',
298 - 'callback' => array(
299 - $this->announcement_obj,
300 - 'course_announcement',
157 + 'methods' => 'GET',
158 + 'callback' => array(
159 + $this->topicObj, 'course_topic'
301 160 ),
302 - 'args' => array(
161 + 'args' => array(
303 162 'id' => array(
304 - 'validate_callback' => function ( $param ) {
305 - return is_numeric( $param );
306 - },
307 - ),
163 + 'validate_callback' => function ($param) {
164 + return is_numeric($param);
165 + }
166 + )
308 167 ),
309 - 'permission_callback' => array( RestAuth::class, 'process_api_request' ),
168 + 'permission_callback' => '__return_true'
310 169 )
311 170 );
312 171
313 - // Quiz by topic id.
172 + //lesson by topic
314 173 register_rest_route(
315 174 $this->namespace,
316 - '/quizzes',
175 + '/lesson/(?P<id>\d+)',
317 176 array(
318 - 'methods' => 'GET',
319 - 'callback' => array(
320 - $this->quiz_obj,
321 - 'quiz_with_settings',
177 + 'methods' => 'GET',
178 + 'callback' => array(
179 + $this->lessonObj, 'topic_lesson'
322 180 ),
323 - 'args' => array(
324 - 'topic_id' => array(
325 - 'validate_callback' => function ( $param ) {
326 - return is_numeric( $param );
327 - },
328 - ),
181 + 'args' => array(
182 + 'id' => array(
183 + 'validate_callback' => function ($param) {
184 + return is_numeric($param);
185 + }
186 + )
329 187 ),
330 - 'permission_callback' => array( RestAuth::class, 'process_api_request' ),
188 + 'permission_callback' => '__return_true'
331 189 )
332 190 );
333 191
334 - // Quiz by quiz id.
192 + //course annoucement by course id
335 193 register_rest_route(
336 194 $this->namespace,
337 - '/quizzes/(?P<id>\d+)',
195 + '/course-annoucement/(?P<id>\d+)',
338 196 array(
339 - 'methods' => 'GET',
340 - 'callback' => array(
341 - $this->quiz_obj,
342 - 'get_quiz',
197 + 'methods' => 'GET',
198 + 'callback' => array(
199 + $this->annoucementObj, 'course_annoucement'
343 200 ),
344 - 'args' => array(
201 + 'args' => array(
345 202 'id' => array(
346 - 'validate_callback' => function ( $param ) {
347 - return is_numeric( $param );
348 - },
349 - ),
203 + 'validate_callback' => function ($param) {
204 + return is_numeric($param);
205 + }
206 + )
350 207 ),
351 - 'permission_callback' => array( RestAuth::class, 'process_api_request' ),
208 + 'permission_callback' => '__return_true'
352 209 )
353 210 );
354 211
355 - // Quiz question answer by quiz id.
212 + //quiz by topic id
356 213 register_rest_route(
357 214 $this->namespace,
358 - '/quiz-question-answer/(?P<id>\d+)',
215 + '/quiz/(?P<id>\d+)',
359 216 array(
360 - 'methods' => 'GET',
361 - 'callback' => array(
362 - $this->quiz_obj,
363 - 'quiz_question_ans',
217 + 'methods' => 'GET',
218 + 'callback' => array(
219 + $this->quizObj, 'quiz_with_settings'
364 220 ),
365 - 'args' => array(
221 + 'args' => array(
366 222 'id' => array(
367 - 'validate_callback' => function ( $param ) {
368 - return is_numeric( $param );
369 - },
370 - ),
223 + 'validate_callback' => function ($param) {
224 + return is_numeric($param);
225 + }
226 + )
371 227 ),
372 - 'permission_callback' => array( RestAuth::class, 'process_api_request' ),
228 + 'permission_callback' => '__return_true'
373 229 )
374 230 );
375 231
376 - // Quiz attempt details by quiz id.
232 + //quiz question answer by quiz id
377 233 register_rest_route(
378 234 $this->namespace,
379 - '/quiz-attempt-details/(?P<id>\d+)',
235 + '/quiz-question-answer/(?P<id>\d+)',
380 236 array(
381 - 'methods' => 'GET',
382 - 'callback' => array(
383 - $this->quiz_obj,
384 - 'quiz_attempt_details',
237 + 'methods' => 'GET',
238 + 'callback' => array(
239 + $this->quizObj, 'quiz_question_ans'
385 240 ),
386 - 'args' => array(
241 + 'args' => array(
387 242 'id' => array(
388 - 'validate_callback' => function ( $param ) {
389 - return is_numeric( $param );
390 - },
391 - ),
243 + 'validate_callback' => function ($param) {
244 + return is_numeric($param);
245 + }
246 + )
392 247 ),
393 - 'permission_callback' => array( RestAuth::class, 'process_api_request' ),
248 + 'permission_callback' => '__return_true'
394 249 )
395 250 );
396 251
397 - // Author detail by id.
252 + //quiz attempt details by quiz id
398 253 register_rest_route(
399 254 $this->namespace,
400 - '/author-information/(?P<id>\d+)',
255 + '/quiz-attempt-details/(?P<id>\d+)',
401 256 array(
402 - 'methods' => 'GET',
403 - 'callback' => array(
404 - $this->author_obj,
405 - 'author_detail',
257 + 'methods' => 'GET',
258 + 'callback' => array(
259 + $this->quizObj, 'quiz_attempt_details'
406 260 ),
407 - 'args' => array(
261 + 'args' => array(
408 262 'id' => array(
409 - 'validate_callback' => function ( $param ) {
410 - return is_numeric( $param );
411 - },
412 - ),
263 + 'validate_callback' => function ($param) {
264 + return is_numeric($param);
265 + }
266 + )
413 267 ),
414 - 'permission_callback' => array( RestAuth::class, 'process_api_request' ),
268 + 'permission_callback' => '__return_true'
415 269 )
416 270 );
417 271
418 - // Reviews by course id.
272 + //author detail by id
419 273 register_rest_route(
420 274 $this->namespace,
421 - '/course-rating/(?P<id>\d+)',
275 + '/author-information/(?P<id>\d+)',
422 276 array(
423 - 'methods' => 'GET',
424 - 'callback' => array(
425 - $this->rating_obj,
426 - 'course_rating',
277 + 'methods' => 'GET',
278 + 'callback' => array(
279 + $this->authorObj, 'author_detail'
427 280 ),
428 - 'args' => array(
281 + 'args' => array(
429 282 'id' => array(
430 - 'validate_callback' => function ( $param ) {
431 - return is_numeric( $param );
432 - },
433 - ),
283 + 'validate_callback' => function ($param) {
284 + return is_numeric($param);
285 + }
286 + )
434 287 ),
435 - 'permission_callback' => array( RestAuth::class, 'process_api_request' ),
288 + 'permission_callback' => '__return_true'
436 289 )
437 290 );
438 291
439 - // Get course content by id.
292 + //reviews by course id
440 293 register_rest_route(
441 294 $this->namespace,
442 - '/course-contents/(?P<id>\d+)',
295 + '/course-rating/(?P<id>\d+)',
443 296 array(
444 - 'methods' => 'GET',
445 - 'callback' => array(
446 - $this->course_obj,
447 - 'course_contents',
297 + 'methods' => 'GET',
298 + 'callback' => array(
299 + $this->ratingObj, 'course_rating'
448 300 ),
449 - 'args' => array(
301 + 'args' => array(
450 302 'id' => array(
451 - 'validate_callback' => function ( $param ) {
452 - return is_numeric( $param );
453 - },
454 - ),
303 + 'validate_callback' => function ($param) {
304 + return is_numeric($param);
305 + }
306 + )
455 307 ),
456 - 'permission_callback' => array( RestAuth::class, 'process_api_request' ),
308 + 'permission_callback' => '__return_true'
457 309 )
458 310 );
459 311 }
460 312 }