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