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.2 3.5.0 3.6.0 3.6.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 / classes / RestAPI.php
tutor / classes Last commit date
Addons.php 4 days ago Admin.php 4 days ago Ajax.php 4 days ago Announcements.php 4 days ago Assets.php 1 day ago Backend_Page_Trait.php 1 year ago BaseController.php 1 year ago Config.php 4 days ago Container.php 11 months ago Course.php 4 days ago Course_Embed.php 3 years ago Course_Filter.php 4 days ago Course_List.php 4 days ago Course_Settings_Tabs.php 4 days ago Course_Widget.php 1 year ago Custom_Validation.php 4 days ago Dashboard.php 4 days ago Earnings.php 9 months ago FormHandler.php 4 days ago Frontend.php 4 days ago Gutenberg.php 1 year ago Icon.php 4 days ago Input.php 4 days ago Instructor.php 4 days ago Instructors_List.php 4 days ago Lesson.php 4 days ago Options_V2.php 4 days ago Permalink.php 4 days ago Post_types.php 5 days ago Private_Course_Access.php 4 days ago Q_And_A.php 4 days ago Question_Answers_List.php 11 months ago Quiz.php 1 day ago QuizBuilder.php 4 days ago Quiz_Attempts_List.php 1 day ago RestAPI.php 1 day ago Reviews.php 4 days ago Rewrite_Rules.php 2 years ago SampleCourse.php 4 days ago Shortcode.php 4 days ago Singleton.php 1 year ago Student.php 4 days ago Students_List.php 1 year ago Taxonomies.php 1 year ago Template.php 4 days ago Theme_Compatibility.php 3 years ago Tools.php 1 year ago Tools_V2.php 1 month ago Tutor.php 4 days ago TutorEDD.php 4 days ago Tutor_Base.php 2 years ago Tutor_Setup.php 4 days ago Upgrader.php 4 days ago User.php 4 days ago UserPreference.php 4 days ago Utils.php 1 day ago Video_Stream.php 3 years ago WhatsNew.php 1 day ago Withdraw.php 4 days ago Withdraw_Requests_List.php 11 months ago WooCommerce.php 4 days ago
RestAPI.php
461 lines
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 }
461