| @@ -1,400 +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' ) ); | |
| 55 | + add_action('rest_api_init', array($this, 'init_routes')); | |
| 119 | 56 | } |
| 120 | 57 | |
| 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 | |
| 58 | + | |
| 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 | |
| 136 | 65 | ); |
| 137 | 66 | |
| 138 | - $class_name = str_replace( 'TUTOR' . DIRECTORY_SEPARATOR, 'restapi' . DIRECTORY_SEPARATOR, $class_name ); | |
| 139 | - $file_name = $this->path . $class_name . '.php'; | |
| 67 | + $className = str_replace('TUTOR' . DIRECTORY_SEPARATOR, 'restapi' . DIRECTORY_SEPARATOR, $className); | |
| 68 | + $file_name = $this->path . $className . '.php'; | |
| 140 | 69 | |
| 141 | - if ( file_exists( $file_name ) ) { | |
| 70 | + if (file_exists($file_name)) { | |
| 142 | 71 | require_once $file_name; |
| 143 | 72 | } |
| 144 | 73 | } |
| 145 | 74 | } |
| 146 | 75 | |
| 147 | - /** | |
| 148 | - * Initialize routes | |
| 149 | - * | |
| 150 | - * @since 1.5.0 | |
| 151 | - * | |
| 152 | - * @return void | |
| 153 | - */ | |
| 76 | + /* | |
| 77 | + init all routes for api | |
| 78 | + */ | |
| 154 | 79 | public function init_routes() { |
| 155 | - // Courses. | |
| 80 | + //courses | |
| 156 | 81 | register_rest_route( |
| 157 | 82 | $this->namespace, |
| 158 | 83 | '/courses', |
| 159 | 84 | array( |
| 160 | - 'methods' => 'GET', | |
| 161 | - 'callback' => array( | |
| 162 | - $this->course_obj, | |
| 163 | - 'course', | |
| 85 | + 'methods' => "GET", | |
| 86 | + 'callback' => array( | |
| 87 | + $this->courseObj, 'course' | |
| 164 | 88 | ), |
| 165 | - 'permission_callback' => array( RestAuth::class, 'process_api_request' ), | |
| 89 | + 'permission_callback' => '__return_true' | |
| 166 | 90 | ) |
| 167 | 91 | ); |
| 168 | 92 | |
| 169 | - // Course details. | |
| 93 | + //courses by terms cat and tag | |
| 170 | 94 | register_rest_route( |
| 171 | 95 | $this->namespace, |
| 172 | - '/courses/(?P<id>\d+)', | |
| 96 | + '/course-by-terms', | |
| 173 | 97 | array( |
| 174 | - 'methods' => 'GET', | |
| 175 | - 'callback' => array( | |
| 176 | - $this->course_obj, | |
| 177 | - 'course_detail', | |
| 98 | + 'methods' => "POST", | |
| 99 | + 'callback' => array( | |
| 100 | + $this->courseObj, 'course_by_terms' | |
| 178 | 101 | ), |
| 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' ), | |
| 102 | + 'permission_callback' => '__return_true' | |
| 187 | 103 | ) |
| 188 | 104 | ); |
| 189 | 105 | |
| 190 | - // Course topic. | |
| 106 | + //courses by terms cat and tag | |
| 191 | 107 | register_rest_route( |
| 192 | 108 | $this->namespace, |
| 193 | - '/topics', | |
| 109 | + '/course-sorting-by-price', | |
| 194 | 110 | array( |
| 195 | - 'methods' => 'GET', | |
| 196 | - 'callback' => array( | |
| 197 | - $this->topic_obj, | |
| 198 | - 'course_topic', | |
| 111 | + 'methods' => "GET", | |
| 112 | + 'callback' => array( | |
| 113 | + $this->courseObj, 'course_sort_by_price' | |
| 199 | 114 | ), |
| 200 | - 'args' => array( | |
| 201 | - 'course_id' => array( | |
| 202 | - 'validate_callback' => function ( $param ) { | |
| 203 | - return is_numeric( $param ); | |
| 204 | - }, | |
| 115 | + 'args' => array( | |
| 116 | + 'order' => array( | |
| 117 | + 'required' => true, | |
| 118 | + 'type' => 'string', | |
| 119 | + 'validate_callback' => function ($order) { | |
| 120 | + return $this->validate_order($order); | |
| 121 | + } | |
| 205 | 122 | ), |
| 123 | + 'page' => array( | |
| 124 | + 'required' => false, | |
| 125 | + 'type' => 'number' | |
| 126 | + ) | |
| 206 | 127 | ), |
| 207 | - 'permission_callback' => array( RestAuth::class, 'process_api_request' ), | |
| 128 | + 'permission_callback' => '__return_true' | |
| 208 | 129 | ) |
| 209 | 130 | ); |
| 210 | 131 | |
| 211 | - // Lesson by topic. | |
| 132 | + //course details | |
| 212 | 133 | register_rest_route( |
| 213 | 134 | $this->namespace, |
| 214 | - '/lessons', | |
| 135 | + '/course-detail/(?P<id>\d+)', | |
| 215 | 136 | array( |
| 216 | - 'methods' => 'GET', | |
| 217 | - 'callback' => array( | |
| 218 | - $this->lesson_obj, | |
| 219 | - 'topic_lesson', | |
| 137 | + 'methods' => 'GET', | |
| 138 | + 'callback' => array( | |
| 139 | + $this->courseObj, 'course_detail' | |
| 220 | 140 | ), |
| 221 | - 'args' => array( | |
| 222 | - 'topic_id' => array( | |
| 223 | - 'validate_callback' => function ( $param ) { | |
| 224 | - return is_numeric( $param ); | |
| 225 | - }, | |
| 226 | - ), | |
| 141 | + 'args' => array( | |
| 142 | + 'id' => array( | |
| 143 | + 'validate_callback' => function ($param) { | |
| 144 | + return is_numeric($param); | |
| 145 | + } | |
| 146 | + ) | |
| 227 | 147 | ), |
| 228 | - 'permission_callback' => array( RestAuth::class, 'process_api_request' ), | |
| 148 | + 'permission_callback' => '__return_true' | |
| 229 | 149 | ) |
| 230 | 150 | ); |
| 231 | 151 | |
| 232 | - // Course announcement by course id. | |
| 152 | + //course topic | |
| 233 | 153 | register_rest_route( |
| 234 | 154 | $this->namespace, |
| 235 | - '/course-announcement/(?P<id>\d+)', | |
| 155 | + '/course-topic/(?P<id>\d+)', | |
| 236 | 156 | array( |
| 237 | - 'methods' => 'GET', | |
| 238 | - 'callback' => array( | |
| 239 | - $this->announcement_obj, | |
| 240 | - 'course_announcement', | |
| 157 | + 'methods' => 'GET', | |
| 158 | + 'callback' => array( | |
| 159 | + $this->topicObj, 'course_topic' | |
| 241 | 160 | ), |
| 242 | - 'args' => array( | |
| 161 | + 'args' => array( | |
| 243 | 162 | 'id' => array( |
| 244 | - 'validate_callback' => function ( $param ) { | |
| 245 | - return is_numeric( $param ); | |
| 246 | - }, | |
| 247 | - ), | |
| 163 | + 'validate_callback' => function ($param) { | |
| 164 | + return is_numeric($param); | |
| 165 | + } | |
| 166 | + ) | |
| 248 | 167 | ), |
| 249 | - 'permission_callback' => array( RestAuth::class, 'process_api_request' ), | |
| 168 | + 'permission_callback' => '__return_true' | |
| 250 | 169 | ) |
| 251 | 170 | ); |
| 252 | 171 | |
| 253 | - // Quiz by topic id. | |
| 172 | + //lesson by topic | |
| 254 | 173 | register_rest_route( |
| 255 | 174 | $this->namespace, |
| 256 | - '/quizzes', | |
| 175 | + '/lesson/(?P<id>\d+)', | |
| 257 | 176 | array( |
| 258 | - 'methods' => 'GET', | |
| 259 | - 'callback' => array( | |
| 260 | - $this->quiz_obj, | |
| 261 | - 'quiz_with_settings', | |
| 177 | + 'methods' => 'GET', | |
| 178 | + 'callback' => array( | |
| 179 | + $this->lessonObj, 'topic_lesson' | |
| 262 | 180 | ), |
| 263 | - 'args' => array( | |
| 264 | - 'topic_id' => array( | |
| 265 | - 'validate_callback' => function ( $param ) { | |
| 266 | - return is_numeric( $param ); | |
| 267 | - }, | |
| 268 | - ), | |
| 181 | + 'args' => array( | |
| 182 | + 'id' => array( | |
| 183 | + 'validate_callback' => function ($param) { | |
| 184 | + return is_numeric($param); | |
| 185 | + } | |
| 186 | + ) | |
| 269 | 187 | ), |
| 270 | - 'permission_callback' => array( RestAuth::class, 'process_api_request' ), | |
| 188 | + 'permission_callback' => '__return_true' | |
| 271 | 189 | ) |
| 272 | 190 | ); |
| 273 | 191 | |
| 274 | - // Quiz by quiz id. | |
| 192 | + //course annoucement by course id | |
| 275 | 193 | register_rest_route( |
| 276 | 194 | $this->namespace, |
| 277 | - '/quizzes/(?P<id>\d+)', | |
| 195 | + '/course-annoucement/(?P<id>\d+)', | |
| 278 | 196 | array( |
| 279 | - 'methods' => 'GET', | |
| 280 | - 'callback' => array( | |
| 281 | - $this->quiz_obj, | |
| 282 | - 'get_quiz', | |
| 197 | + 'methods' => 'GET', | |
| 198 | + 'callback' => array( | |
| 199 | + $this->annoucementObj, 'course_annoucement' | |
| 283 | 200 | ), |
| 284 | - 'args' => array( | |
| 201 | + 'args' => array( | |
| 285 | 202 | 'id' => array( |
| 286 | - 'validate_callback' => function ( $param ) { | |
| 287 | - return is_numeric( $param ); | |
| 288 | - }, | |
| 289 | - ), | |
| 203 | + 'validate_callback' => function ($param) { | |
| 204 | + return is_numeric($param); | |
| 205 | + } | |
| 206 | + ) | |
| 290 | 207 | ), |
| 291 | - 'permission_callback' => array( RestAuth::class, 'process_api_request' ), | |
| 208 | + 'permission_callback' => '__return_true' | |
| 292 | 209 | ) |
| 293 | 210 | ); |
| 294 | 211 | |
| 295 | - // Quiz question answer by quiz id. | |
| 212 | + //quiz by topic id | |
| 296 | 213 | register_rest_route( |
| 297 | 214 | $this->namespace, |
| 298 | - '/quiz-question-answer/(?P<id>\d+)', | |
| 215 | + '/quiz/(?P<id>\d+)', | |
| 299 | 216 | array( |
| 300 | - 'methods' => 'GET', | |
| 301 | - 'callback' => array( | |
| 302 | - $this->quiz_obj, | |
| 303 | - 'quiz_question_ans', | |
| 217 | + 'methods' => 'GET', | |
| 218 | + 'callback' => array( | |
| 219 | + $this->quizObj, 'quiz_with_settings' | |
| 304 | 220 | ), |
| 305 | - 'args' => array( | |
| 221 | + 'args' => array( | |
| 306 | 222 | 'id' => array( |
| 307 | - 'validate_callback' => function ( $param ) { | |
| 308 | - return is_numeric( $param ); | |
| 309 | - }, | |
| 310 | - ), | |
| 223 | + 'validate_callback' => function ($param) { | |
| 224 | + return is_numeric($param); | |
| 225 | + } | |
| 226 | + ) | |
| 311 | 227 | ), |
| 312 | - 'permission_callback' => array( RestAuth::class, 'process_api_request' ), | |
| 228 | + 'permission_callback' => '__return_true' | |
| 313 | 229 | ) |
| 314 | 230 | ); |
| 315 | 231 | |
| 316 | - // Quiz attempt details by quiz id. | |
| 232 | + //quiz question answer by quiz id | |
| 317 | 233 | register_rest_route( |
| 318 | 234 | $this->namespace, |
| 319 | - '/quiz-attempt-details/(?P<id>\d+)', | |
| 235 | + '/quiz-question-answer/(?P<id>\d+)', | |
| 320 | 236 | array( |
| 321 | - 'methods' => 'GET', | |
| 322 | - 'callback' => array( | |
| 323 | - $this->quiz_obj, | |
| 324 | - 'quiz_attempt_details', | |
| 237 | + 'methods' => 'GET', | |
| 238 | + 'callback' => array( | |
| 239 | + $this->quizObj, 'quiz_question_ans' | |
| 325 | 240 | ), |
| 326 | - 'args' => array( | |
| 241 | + 'args' => array( | |
| 327 | 242 | 'id' => array( |
| 328 | - 'validate_callback' => function ( $param ) { | |
| 329 | - return is_numeric( $param ); | |
| 330 | - }, | |
| 331 | - ), | |
| 243 | + 'validate_callback' => function ($param) { | |
| 244 | + return is_numeric($param); | |
| 245 | + } | |
| 246 | + ) | |
| 332 | 247 | ), |
| 333 | - 'permission_callback' => array( RestAuth::class, 'process_api_request' ), | |
| 248 | + 'permission_callback' => '__return_true' | |
| 334 | 249 | ) |
| 335 | 250 | ); |
| 336 | 251 | |
| 337 | - // Author detail by id. | |
| 252 | + //quiz attempt details by quiz id | |
| 338 | 253 | register_rest_route( |
| 339 | 254 | $this->namespace, |
| 340 | - '/author-information/(?P<id>\d+)', | |
| 255 | + '/quiz-attempt-details/(?P<id>\d+)', | |
| 341 | 256 | array( |
| 342 | - 'methods' => 'GET', | |
| 343 | - 'callback' => array( | |
| 344 | - $this->author_obj, | |
| 345 | - 'author_detail', | |
| 257 | + 'methods' => 'GET', | |
| 258 | + 'callback' => array( | |
| 259 | + $this->quizObj, 'quiz_attempt_details' | |
| 346 | 260 | ), |
| 347 | - 'args' => array( | |
| 261 | + 'args' => array( | |
| 348 | 262 | 'id' => array( |
| 349 | - 'validate_callback' => function ( $param ) { | |
| 350 | - return is_numeric( $param ); | |
| 351 | - }, | |
| 352 | - ), | |
| 263 | + 'validate_callback' => function ($param) { | |
| 264 | + return is_numeric($param); | |
| 265 | + } | |
| 266 | + ) | |
| 353 | 267 | ), |
| 354 | - 'permission_callback' => array( RestAuth::class, 'process_api_request' ), | |
| 268 | + 'permission_callback' => '__return_true' | |
| 355 | 269 | ) |
| 356 | 270 | ); |
| 357 | 271 | |
| 358 | - // Reviews by course id. | |
| 272 | + //author detail by id | |
| 359 | 273 | register_rest_route( |
| 360 | 274 | $this->namespace, |
| 361 | - '/course-rating/(?P<id>\d+)', | |
| 275 | + '/author-information/(?P<id>\d+)', | |
| 362 | 276 | array( |
| 363 | - 'methods' => 'GET', | |
| 364 | - 'callback' => array( | |
| 365 | - $this->rating_obj, | |
| 366 | - 'course_rating', | |
| 277 | + 'methods' => 'GET', | |
| 278 | + 'callback' => array( | |
| 279 | + $this->authorObj, 'author_detail' | |
| 367 | 280 | ), |
| 368 | - 'args' => array( | |
| 281 | + 'args' => array( | |
| 369 | 282 | 'id' => array( |
| 370 | - 'validate_callback' => function ( $param ) { | |
| 371 | - return is_numeric( $param ); | |
| 372 | - }, | |
| 373 | - ), | |
| 283 | + 'validate_callback' => function ($param) { | |
| 284 | + return is_numeric($param); | |
| 285 | + } | |
| 286 | + ) | |
| 374 | 287 | ), |
| 375 | - 'permission_callback' => array( RestAuth::class, 'process_api_request' ), | |
| 288 | + 'permission_callback' => '__return_true' | |
| 376 | 289 | ) |
| 377 | 290 | ); |
| 378 | 291 | |
| 379 | - // Get course content by id. | |
| 292 | + //reviews by course id | |
| 380 | 293 | register_rest_route( |
| 381 | 294 | $this->namespace, |
| 382 | - '/course-contents/(?P<id>\d+)', | |
| 295 | + '/course-rating/(?P<id>\d+)', | |
| 383 | 296 | array( |
| 384 | - 'methods' => 'GET', | |
| 385 | - 'callback' => array( | |
| 386 | - $this->course_obj, | |
| 387 | - 'course_contents', | |
| 297 | + 'methods' => 'GET', | |
| 298 | + 'callback' => array( | |
| 299 | + $this->ratingObj, 'course_rating' | |
| 388 | 300 | ), |
| 389 | - 'args' => array( | |
| 301 | + 'args' => array( | |
| 390 | 302 | 'id' => array( |
| 391 | - 'validate_callback' => function ( $param ) { | |
| 392 | - return is_numeric( $param ); | |
| 393 | - }, | |
| 394 | - ), | |
| 303 | + 'validate_callback' => function ($param) { | |
| 304 | + return is_numeric($param); | |
| 305 | + } | |
| 306 | + ) | |
| 395 | 307 | ), |
| 396 | - 'permission_callback' => array( RestAuth::class, 'process_api_request' ), | |
| 308 | + 'permission_callback' => '__return_true' | |
| 397 | 309 | ) |
| 398 | 310 | ); |
| 399 | 311 | } |
| 400 | 312 | } |