| @@ -1,460 +1,118 @@ | ||
| 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 | 12 | namespace TUTOR; |
| 12 | 13 | |
| 13 | -if ( ! defined( 'ABSPATH' ) ) { | |
| 14 | +if ( ! defined( 'ABSPATH' ) ) | |
| 14 | 15 | exit; |
| 15 | -} | |
| 16 | 16 | |
| 17 | -/** | |
| 18 | - * Initialize REST API | |
| 19 | - * | |
| 20 | - * @since 1.5.0 | |
| 21 | - */ | |
| 22 | 17 | class RestAPI { |
| 23 | 18 | |
| 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 | - */ | |
| 19 | + protected $namespace = 'tutor/v1'; | |
| 41 | 20 | protected $course_post_type; |
| 42 | 21 | |
| 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 | 22 | public function __construct() { |
| 23 | + $this->course_post_type = tutor()->course_post_type; | |
| 105 | 24 | |
| 106 | - $this->path = plugin_dir_path( TUTOR_FILE ); | |
| 25 | + add_action('rest_api_init', array($this, 'rest_api_init')); | |
| 107 | 26 | |
| 108 | - spl_autoload_register( array( $this, 'loader' ) ); | |
| 27 | + } | |
| 109 | 28 | |
| 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 | 29 | |
| 118 | - add_action( 'rest_api_init', array( $this, 'init_routes' ) ); | |
| 119 | - add_filter( 'rest_request_before_callbacks', array( $this, 'check_permission' ), 10, 3 ); | |
| 30 | + public function rest_api_init(){ | |
| 31 | + register_rest_route( $this->namespace, 'courses', array( 'methods' => 'GET', 'callback' => array($this, 'courses_api') ) ); | |
| 120 | 32 | } |
| 121 | 33 | |
| 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 ); | |
| 34 | + public function courses_api(){ | |
| 35 | + global $wpdb; | |
| 140 | 36 | |
| 141 | - if ( ! $id ) { | |
| 142 | - return $response; | |
| 143 | - } | |
| 37 | + $a = array_merge(array( | |
| 38 | + 'post_type' => $this->course_post_type, | |
| 39 | + 'post_status' => 'publish', | |
| 144 | 40 | |
| 145 | - $post = get_post( $id ); | |
| 41 | + 'id' => '', | |
| 42 | + 'exclude_ids' => '', | |
| 43 | + 'category' => '', | |
| 146 | 44 | |
| 147 | - if ( ! $post ) { | |
| 148 | - return $response; | |
| 149 | - } | |
| 45 | + 'orderby' => 'ID', | |
| 46 | + 'order' => 'DESC', | |
| 47 | + 'count' => '10', | |
| 48 | + ), $_GET); | |
| 150 | 49 | |
| 151 | - $post_types = array( | |
| 152 | - tutor()->course_post_type, | |
| 153 | - tutor()->bundle_post_type, | |
| 154 | - ); | |
| 50 | + $limit = (int) $a['count']; | |
| 51 | + $exclude_ids_query = ''; | |
| 52 | + $in_ids_query = ''; | |
| 53 | + $tax_join = ''; | |
| 54 | + $tax_where = ''; | |
| 155 | 55 | |
| 156 | - if ( ! in_array( $post->post_type, $post_types, true ) ) { | |
| 157 | - return $response; | |
| 158 | - } | |
| 56 | + $orderby = sanitize_text_field($a['orderby']); | |
| 57 | + $order = sanitize_text_field($a['order']); | |
| 159 | 58 | |
| 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 | - ); | |
| 59 | + /** | |
| 60 | + * Exclude Course IDS | |
| 61 | + */ | |
| 62 | + if ( ! empty($a['exclude_ids'])){ | |
| 63 | + $exclude_ids = (array) explode(',', sanitize_text_field($a['exclude_ids'])); | |
| 64 | + if (tutils()->count($exclude_ids)){ | |
| 65 | + $exclude_ids_query = "AND ID NOT IN('$exclude_ids')"; | |
| 66 | + } | |
| 167 | 67 | } |
| 168 | 68 | |
| 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 | - ); | |
| 69 | + if ( ! empty($a['id'])){ | |
| 70 | + $ids = (array) explode(',', $a['id']); | |
| 71 | + if (tutils()->count($ids)){ | |
| 72 | + $in_ids_query = "AND ID IN('$ids')"; | |
| 73 | + } | |
| 176 | 74 | } |
| 177 | 75 | |
| 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 | |
| 76 | + if ( ! empty($a['category'])){ | |
| 77 | + $category = (array) explode(',', $a['category']); | |
| 78 | + $tax = new \WP_Tax_Query( | |
| 79 | + array( | |
| 80 | + array( | |
| 81 | + 'taxonomy' => 'course-category', | |
| 82 | + 'field' => 'term_id', | |
| 83 | + 'terms' => $category, | |
| 84 | + 'operator' => 'IN', | |
| 85 | + ) | |
| 86 | + ) | |
| 196 | 87 | ); |
| 197 | 88 | |
| 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 | - } | |
| 89 | + $tax_sql = $tax->get_sql($wpdb->posts, 'ID'); | |
| 90 | + $tax_join = tutils()->array_get('join', $tax_sql); | |
| 91 | + $tax_where = tutils()->array_get('where', $tax_sql); | |
| 204 | 92 | } |
| 205 | - } | |
| 206 | 93 | |
| 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 | - ); | |
| 94 | + $course_post_type = tutor()->course_post_type; | |
| 95 | + $query = $wpdb->get_results("SELECT ID, post_author, post_title | |
| 96 | + from {$wpdb->posts} | |
| 97 | + | |
| 98 | + {$tax_join} | |
| 99 | + | |
| 100 | + WHERE 1=1 AND post_status = 'publish' | |
| 101 | + {$exclude_ids_query} | |
| 102 | + {$in_ids_query} | |
| 103 | + {$tax_where} | |
| 104 | + AND post_type = '{$course_post_type}' ORDER BY {$orderby} {$order} LIMIT {$limit} ", ARRAY_A); | |
| 228 | 105 | |
| 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 | 106 | |
| 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 | - ); | |
| 107 | + if (tutils()->count($query)){ | |
| 108 | + $results = apply_filters('tutor/api/get_courses', $query); | |
| 109 | + wp_send_json_success($results); | |
| 110 | + } | |
| 111 | + wp_send_json_error(); | |
| 270 | 112 | |
| 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 | 113 | |
| 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 | - ); | |
| 114 | + } | |
| 312 | 115 | |
| 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 | 116 | |
| 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 | 117 | |
| 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 | -} | |
| 118 | +} | |