Addons.php
11 months ago
Admin.php
2 months ago
Ajax.php
9 months ago
Announcements.php
1 year ago
Assets.php
7 months ago
Backend_Page_Trait.php
1 year ago
BaseController.php
1 year ago
Config.php
11 months ago
Container.php
11 months ago
Course.php
2 months ago
Course_Embed.php
3 years ago
Course_Filter.php
1 year ago
Course_List.php
5 months ago
Course_Settings_Tabs.php
1 year ago
Course_Widget.php
1 year ago
Custom_Validation.php
3 years ago
Dashboard.php
1 year ago
Earnings.php
9 months ago
FormHandler.php
2 years ago
Frontend.php
1 year ago
Gutenberg.php
1 year ago
Icon.php
8 months ago
Input.php
1 year ago
Instructor.php
2 months ago
Instructors_List.php
2 months ago
Lesson.php
8 months ago
Options_V2.php
7 months ago
Permalink.php
2 years ago
Post_types.php
1 year ago
Private_Course_Access.php
1 year ago
Q_And_A.php
10 months ago
Question_Answers_List.php
11 months ago
Quiz.php
5 months ago
QuizBuilder.php
3 months ago
Quiz_Attempts_List.php
9 months ago
RestAPI.php
2 years ago
Reviews.php
9 months ago
Rewrite_Rules.php
2 years ago
Shortcode.php
9 months ago
Singleton.php
1 year ago
Student.php
2 months ago
Students_List.php
1 year ago
Taxonomies.php
1 year ago
Template.php
9 months ago
Theme_Compatibility.php
3 years ago
Tools.php
1 year ago
Tools_V2.php
1 year ago
Tutor.php
3 months ago
TutorEDD.php
1 year ago
Tutor_Base.php
2 years ago
Tutor_Setup.php
8 months ago
Upgrader.php
9 months ago
User.php
4 months ago
Utils.php
5 months ago
Video_Stream.php
3 years ago
WhatsNew.php
9 months ago
Withdraw.php
1 year ago
Withdraw_Requests_List.php
11 months ago
WooCommerce.php
7 months ago
RestAPI.php
401 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 | } |
| 120 | |
| 121 | /** |
| 122 | * Class loading |
| 123 | * |
| 124 | * @since 1.5.0 |
| 125 | * |
| 126 | * @param string $class_name class name to load. |
| 127 | * |
| 128 | * @return void |
| 129 | */ |
| 130 | private function loader( $class_name ) { |
| 131 | if ( ! class_exists( $class_name ) ) { |
| 132 | $class_name = preg_replace( |
| 133 | array( '/([a-z])([A-Z])/', '/\\\/' ), |
| 134 | array( '$1$2', DIRECTORY_SEPARATOR ), |
| 135 | $class_name |
| 136 | ); |
| 137 | |
| 138 | $class_name = str_replace( 'TUTOR' . DIRECTORY_SEPARATOR, 'restapi' . DIRECTORY_SEPARATOR, $class_name ); |
| 139 | $file_name = $this->path . $class_name . '.php'; |
| 140 | |
| 141 | if ( file_exists( $file_name ) ) { |
| 142 | require_once $file_name; |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Initialize routes |
| 149 | * |
| 150 | * @since 1.5.0 |
| 151 | * |
| 152 | * @return void |
| 153 | */ |
| 154 | public function init_routes() { |
| 155 | // Courses. |
| 156 | register_rest_route( |
| 157 | $this->namespace, |
| 158 | '/courses', |
| 159 | array( |
| 160 | 'methods' => 'GET', |
| 161 | 'callback' => array( |
| 162 | $this->course_obj, |
| 163 | 'course', |
| 164 | ), |
| 165 | 'permission_callback' => array( RestAuth::class, 'process_api_request' ), |
| 166 | ) |
| 167 | ); |
| 168 | |
| 169 | // Course details. |
| 170 | register_rest_route( |
| 171 | $this->namespace, |
| 172 | '/courses/(?P<id>\d+)', |
| 173 | array( |
| 174 | 'methods' => 'GET', |
| 175 | 'callback' => array( |
| 176 | $this->course_obj, |
| 177 | 'course_detail', |
| 178 | ), |
| 179 | 'args' => array( |
| 180 | 'id' => array( |
| 181 | 'validate_callback' => function ( $param ) { |
| 182 | return is_numeric( $param ); |
| 183 | }, |
| 184 | ), |
| 185 | ), |
| 186 | 'permission_callback' => array( RestAuth::class, 'process_api_request' ), |
| 187 | ) |
| 188 | ); |
| 189 | |
| 190 | // Course topic. |
| 191 | register_rest_route( |
| 192 | $this->namespace, |
| 193 | '/topics', |
| 194 | array( |
| 195 | 'methods' => 'GET', |
| 196 | 'callback' => array( |
| 197 | $this->topic_obj, |
| 198 | 'course_topic', |
| 199 | ), |
| 200 | 'args' => array( |
| 201 | 'course_id' => array( |
| 202 | 'validate_callback' => function ( $param ) { |
| 203 | return is_numeric( $param ); |
| 204 | }, |
| 205 | ), |
| 206 | ), |
| 207 | 'permission_callback' => array( RestAuth::class, 'process_api_request' ), |
| 208 | ) |
| 209 | ); |
| 210 | |
| 211 | // Lesson by topic. |
| 212 | register_rest_route( |
| 213 | $this->namespace, |
| 214 | '/lessons', |
| 215 | array( |
| 216 | 'methods' => 'GET', |
| 217 | 'callback' => array( |
| 218 | $this->lesson_obj, |
| 219 | 'topic_lesson', |
| 220 | ), |
| 221 | 'args' => array( |
| 222 | 'topic_id' => array( |
| 223 | 'validate_callback' => function ( $param ) { |
| 224 | return is_numeric( $param ); |
| 225 | }, |
| 226 | ), |
| 227 | ), |
| 228 | 'permission_callback' => array( RestAuth::class, 'process_api_request' ), |
| 229 | ) |
| 230 | ); |
| 231 | |
| 232 | // Course announcement by course id. |
| 233 | register_rest_route( |
| 234 | $this->namespace, |
| 235 | '/course-announcement/(?P<id>\d+)', |
| 236 | array( |
| 237 | 'methods' => 'GET', |
| 238 | 'callback' => array( |
| 239 | $this->announcement_obj, |
| 240 | 'course_announcement', |
| 241 | ), |
| 242 | 'args' => array( |
| 243 | 'id' => array( |
| 244 | 'validate_callback' => function ( $param ) { |
| 245 | return is_numeric( $param ); |
| 246 | }, |
| 247 | ), |
| 248 | ), |
| 249 | 'permission_callback' => array( RestAuth::class, 'process_api_request' ), |
| 250 | ) |
| 251 | ); |
| 252 | |
| 253 | // Quiz by topic id. |
| 254 | register_rest_route( |
| 255 | $this->namespace, |
| 256 | '/quizzes', |
| 257 | array( |
| 258 | 'methods' => 'GET', |
| 259 | 'callback' => array( |
| 260 | $this->quiz_obj, |
| 261 | 'quiz_with_settings', |
| 262 | ), |
| 263 | 'args' => array( |
| 264 | 'topic_id' => array( |
| 265 | 'validate_callback' => function ( $param ) { |
| 266 | return is_numeric( $param ); |
| 267 | }, |
| 268 | ), |
| 269 | ), |
| 270 | 'permission_callback' => array( RestAuth::class, 'process_api_request' ), |
| 271 | ) |
| 272 | ); |
| 273 | |
| 274 | // Quiz by quiz id. |
| 275 | register_rest_route( |
| 276 | $this->namespace, |
| 277 | '/quizzes/(?P<id>\d+)', |
| 278 | array( |
| 279 | 'methods' => 'GET', |
| 280 | 'callback' => array( |
| 281 | $this->quiz_obj, |
| 282 | 'get_quiz', |
| 283 | ), |
| 284 | 'args' => array( |
| 285 | 'id' => array( |
| 286 | 'validate_callback' => function ( $param ) { |
| 287 | return is_numeric( $param ); |
| 288 | }, |
| 289 | ), |
| 290 | ), |
| 291 | 'permission_callback' => array( RestAuth::class, 'process_api_request' ), |
| 292 | ) |
| 293 | ); |
| 294 | |
| 295 | // Quiz question answer by quiz id. |
| 296 | register_rest_route( |
| 297 | $this->namespace, |
| 298 | '/quiz-question-answer/(?P<id>\d+)', |
| 299 | array( |
| 300 | 'methods' => 'GET', |
| 301 | 'callback' => array( |
| 302 | $this->quiz_obj, |
| 303 | 'quiz_question_ans', |
| 304 | ), |
| 305 | 'args' => array( |
| 306 | 'id' => array( |
| 307 | 'validate_callback' => function ( $param ) { |
| 308 | return is_numeric( $param ); |
| 309 | }, |
| 310 | ), |
| 311 | ), |
| 312 | 'permission_callback' => array( RestAuth::class, 'process_api_request' ), |
| 313 | ) |
| 314 | ); |
| 315 | |
| 316 | // Quiz attempt details by quiz id. |
| 317 | register_rest_route( |
| 318 | $this->namespace, |
| 319 | '/quiz-attempt-details/(?P<id>\d+)', |
| 320 | array( |
| 321 | 'methods' => 'GET', |
| 322 | 'callback' => array( |
| 323 | $this->quiz_obj, |
| 324 | 'quiz_attempt_details', |
| 325 | ), |
| 326 | 'args' => array( |
| 327 | 'id' => array( |
| 328 | 'validate_callback' => function ( $param ) { |
| 329 | return is_numeric( $param ); |
| 330 | }, |
| 331 | ), |
| 332 | ), |
| 333 | 'permission_callback' => array( RestAuth::class, 'process_api_request' ), |
| 334 | ) |
| 335 | ); |
| 336 | |
| 337 | // Author detail by id. |
| 338 | register_rest_route( |
| 339 | $this->namespace, |
| 340 | '/author-information/(?P<id>\d+)', |
| 341 | array( |
| 342 | 'methods' => 'GET', |
| 343 | 'callback' => array( |
| 344 | $this->author_obj, |
| 345 | 'author_detail', |
| 346 | ), |
| 347 | 'args' => array( |
| 348 | 'id' => array( |
| 349 | 'validate_callback' => function ( $param ) { |
| 350 | return is_numeric( $param ); |
| 351 | }, |
| 352 | ), |
| 353 | ), |
| 354 | 'permission_callback' => array( RestAuth::class, 'process_api_request' ), |
| 355 | ) |
| 356 | ); |
| 357 | |
| 358 | // Reviews by course id. |
| 359 | register_rest_route( |
| 360 | $this->namespace, |
| 361 | '/course-rating/(?P<id>\d+)', |
| 362 | array( |
| 363 | 'methods' => 'GET', |
| 364 | 'callback' => array( |
| 365 | $this->rating_obj, |
| 366 | 'course_rating', |
| 367 | ), |
| 368 | 'args' => array( |
| 369 | 'id' => array( |
| 370 | 'validate_callback' => function ( $param ) { |
| 371 | return is_numeric( $param ); |
| 372 | }, |
| 373 | ), |
| 374 | ), |
| 375 | 'permission_callback' => array( RestAuth::class, 'process_api_request' ), |
| 376 | ) |
| 377 | ); |
| 378 | |
| 379 | // Get course content by id. |
| 380 | register_rest_route( |
| 381 | $this->namespace, |
| 382 | '/course-contents/(?P<id>\d+)', |
| 383 | array( |
| 384 | 'methods' => 'GET', |
| 385 | 'callback' => array( |
| 386 | $this->course_obj, |
| 387 | 'course_contents', |
| 388 | ), |
| 389 | 'args' => array( |
| 390 | 'id' => array( |
| 391 | 'validate_callback' => function ( $param ) { |
| 392 | return is_numeric( $param ); |
| 393 | }, |
| 394 | ), |
| 395 | ), |
| 396 | 'permission_callback' => array( RestAuth::class, 'process_api_request' ), |
| 397 | ) |
| 398 | ); |
| 399 | } |
| 400 | } |
| 401 |