REST_Author.php
2 years ago
REST_Course.php
2 years ago
REST_Course_Announcement.php
2 years ago
REST_Lesson.php
2 years ago
REST_Quiz.php
2 years ago
REST_Rating.php
2 years ago
REST_Response.php
2 years ago
REST_Topic.php
2 years ago
RestAuth.php
2 years ago
REST_Course.php
456 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Manage Course API |
| 4 | * |
| 5 | * @package Tutor\RestAPI |
| 6 | * @author Themeum <support@themeum.com> |
| 7 | * @link https://themeum.com |
| 8 | * @since 1.7.1 |
| 9 | */ |
| 10 | |
| 11 | namespace TUTOR; |
| 12 | |
| 13 | use WP_REST_Request; |
| 14 | use WP_Query; |
| 15 | |
| 16 | if ( ! defined( 'ABSPATH' ) ) { |
| 17 | exit; |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * Rest_Course class |
| 22 | */ |
| 23 | class REST_Course { |
| 24 | |
| 25 | use REST_Response; |
| 26 | |
| 27 | |
| 28 | /** |
| 29 | * The post type associated with the course handler. |
| 30 | * |
| 31 | * @since 1.7.1 |
| 32 | * |
| 33 | * @var string $post_type The post type for courses. |
| 34 | */ |
| 35 | private $post_type; |
| 36 | |
| 37 | /** |
| 38 | * The taxonomy for course categories. |
| 39 | * |
| 40 | * @since 1.7.1 |
| 41 | * |
| 42 | * @var string $course_cat_tax The taxonomy for course categories. |
| 43 | */ |
| 44 | private $course_cat_tax = 'course-category'; |
| 45 | |
| 46 | /** |
| 47 | * The taxonomy for course tags. |
| 48 | * |
| 49 | * @since 1.7.1 |
| 50 | * |
| 51 | * @var string $course_tag_tax The taxonomy for course tags. |
| 52 | */ |
| 53 | private $course_tag_tax = 'course-tag'; |
| 54 | |
| 55 | /** |
| 56 | * Constructor for the Tutor_Course_Handler class. |
| 57 | * |
| 58 | * Initializes the post type property. |
| 59 | * |
| 60 | * @since 1.7.1 |
| 61 | */ |
| 62 | public function __construct() { |
| 63 | $this->post_type = tutor()->course_post_type; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Course read API handler |
| 68 | * |
| 69 | * Get course list along with pagination, categories, tags |
| 70 | * author details, reviews |
| 71 | * |
| 72 | * @param WP_REST_Request $request request data. |
| 73 | * |
| 74 | * @return WP_REST_Response |
| 75 | */ |
| 76 | public function course( WP_REST_Request $request ) { |
| 77 | $order = sanitize_text_field( $request->get_param( 'order' ) ); |
| 78 | $orderby = sanitize_text_field( $request->get_param( 'orderby' ) ); |
| 79 | $paged = sanitize_text_field( $request->get_param( 'paged' ) ); |
| 80 | $post_per_page = tutor_utils()->get_option( 'pagination_per_page' ); |
| 81 | |
| 82 | $args = array( |
| 83 | 'post_type' => $this->post_type, |
| 84 | 'post_status' => 'publish', |
| 85 | 'posts_per_page' => $post_per_page, |
| 86 | 'paged' => $paged ? $paged : 1, |
| 87 | 'order' => $order ? $order : 'ASC', |
| 88 | 'orderby' => $orderby ? $orderby : 'title', |
| 89 | ); |
| 90 | |
| 91 | $args = apply_filters( 'tutor_rest_course_query_args', $args ); |
| 92 | |
| 93 | $query = new WP_Query( $args ); |
| 94 | |
| 95 | // if post found. |
| 96 | if ( count( $query->posts ) > 0 ) { |
| 97 | // unset filter property. |
| 98 | array_map( |
| 99 | function ( $post ) { |
| 100 | unset( $post->filter ); |
| 101 | }, |
| 102 | $query->posts |
| 103 | ); |
| 104 | |
| 105 | $data = array( |
| 106 | 'posts' => array(), |
| 107 | 'total_course' => $query->found_posts, |
| 108 | 'total_page' => $query->max_num_pages, |
| 109 | ); |
| 110 | |
| 111 | foreach ( $query->posts as $post ) { |
| 112 | $category = wp_get_post_terms( $post->ID, $this->course_cat_tax ); |
| 113 | |
| 114 | $tag = wp_get_post_terms( $post->ID, $this->course_tag_tax ); |
| 115 | |
| 116 | $author = get_userdata( $post->post_author ); |
| 117 | |
| 118 | if ( $author ) { |
| 119 | // Unset user pass & key. |
| 120 | unset( $author->data->user_pass ); |
| 121 | unset( $author->data->user_activation_key ); |
| 122 | } |
| 123 | |
| 124 | is_a( $author, 'WP_User' ) ? $post->post_author = $author->data : new \stdClass(); |
| 125 | |
| 126 | $thumbnail_size = apply_filters( 'tutor_rest_course_thumbnail_size', 'post-thumbnail' ); |
| 127 | $post->thumbnail_url = get_the_post_thumbnail_url( $post->ID, $thumbnail_size ); |
| 128 | |
| 129 | $post->additional_info = $this->course_additional_info( $post->ID ); |
| 130 | |
| 131 | $post->ratings = tutor_utils()->get_course_rating( $post->ID ); |
| 132 | |
| 133 | $post->course_category = $category; |
| 134 | |
| 135 | $post->course_tag = $tag; |
| 136 | |
| 137 | $post = apply_filters( 'tutor_rest_course_single_post', $post ); |
| 138 | array_push( $data['posts'], $post ); |
| 139 | } |
| 140 | |
| 141 | $response = array( |
| 142 | 'code' => 'success', |
| 143 | 'message' => __( 'Course retrieved successfully', 'tutor' ), |
| 144 | 'data' => $data, |
| 145 | ); |
| 146 | |
| 147 | return self::send( $response ); |
| 148 | } |
| 149 | |
| 150 | $response = array( |
| 151 | 'code' => 'not_found', |
| 152 | 'message' => __( 'Course not found', 'tutor' ), |
| 153 | 'data' => array(), |
| 154 | ); |
| 155 | |
| 156 | return self::send( $response ); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Course Details API handler |
| 161 | * |
| 162 | * @since 1.7.1 |
| 163 | * |
| 164 | * @param WP_REST_Request $request request params. |
| 165 | * |
| 166 | * @return WP_REST_Response |
| 167 | */ |
| 168 | public function course_detail( WP_REST_Request $request ) { |
| 169 | $post_id = $request->get_param( 'id' ); |
| 170 | |
| 171 | $detail = $this->course_additional_info( $post_id ); |
| 172 | if ( $detail ) { |
| 173 | $response = array( |
| 174 | 'code' => 'course_detail', |
| 175 | 'message' => __( 'Course detail retrieved successfully', 'tutor' ), |
| 176 | 'data' => $detail, |
| 177 | ); |
| 178 | return self::send( $response ); |
| 179 | } |
| 180 | $response = array( |
| 181 | 'code' => 'course_detail', |
| 182 | 'message' => __( 'Detail not found for given ID', 'tutor' ), |
| 183 | 'data' => array(), |
| 184 | ); |
| 185 | |
| 186 | return self::send( $response ); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Get course additional info |
| 191 | * |
| 192 | * @since 2.6.1 |
| 193 | * |
| 194 | * @param integer $post_id post id. |
| 195 | * |
| 196 | * @return array |
| 197 | */ |
| 198 | public function course_additional_info( int $post_id ) { |
| 199 | $detail = array( |
| 200 | |
| 201 | 'course_settings' => get_post_meta( $post_id, '_tutor_course_settings', false ), |
| 202 | |
| 203 | 'course_price_type' => get_post_meta( $post_id, '_tutor_course_price_type', false ), |
| 204 | |
| 205 | 'course_duration' => get_post_meta( $post_id, '_course_duration', false ), |
| 206 | |
| 207 | 'course_level' => get_post_meta( $post_id, '_tutor_course_level', false ), |
| 208 | |
| 209 | 'course_benefits' => get_post_meta( $post_id, '_tutor_course_benefits', false ), |
| 210 | |
| 211 | 'course_requirements' => get_post_meta( $post_id, '_tutor_course_requirements', false ), |
| 212 | |
| 213 | 'course_target_audience' => get_post_meta( $post_id, '_tutor_course_target_audience', false ), |
| 214 | |
| 215 | 'course_material_includes' => get_post_meta( $post_id, '_tutor_course_material_includes', false ), |
| 216 | |
| 217 | 'video' => get_post_meta( $post_id, '_video', false ), |
| 218 | |
| 219 | 'disable_qa' => get_post_meta( $post_id, '_tutor_enable_qa', true ) != 'yes', |
| 220 | ); |
| 221 | |
| 222 | return apply_filters( 'tutor_course_additional_info', $detail ); |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Get Course by Terms API handler |
| 227 | * |
| 228 | * @since 1.7.1 |
| 229 | * |
| 230 | * @param WP_REST_Request $request request params. |
| 231 | * |
| 232 | * @return WP_REST_Response |
| 233 | */ |
| 234 | public function course_by_terms( WP_REST_Request $request ) { |
| 235 | $post_fields = $request->get_params(); |
| 236 | $validate_err = $this->validate_terms( $post_fields ); |
| 237 | |
| 238 | // check array or not. |
| 239 | if ( count( $validate_err ) > 0 ) { |
| 240 | $response = array( |
| 241 | 'code' => 'validation_error', |
| 242 | 'message' => $validate_err, |
| 243 | 'data' => array(), |
| 244 | ); |
| 245 | |
| 246 | return self::send( $response ); |
| 247 | } |
| 248 | |
| 249 | // sanitize terms. |
| 250 | $categories = sanitize_term( $request['categories'], $this->course_cat_tax, $context = 'db' ); |
| 251 | |
| 252 | $tags = sanitize_term( $request['tags'], $this->course_tag_tax, $context = 'db' ); |
| 253 | |
| 254 | $args = array( |
| 255 | 'post_type' => $this->post_type, |
| 256 | 'tax_query' => array( |
| 257 | 'relation' => 'OR', |
| 258 | array( |
| 259 | 'taxonomy' => $this->course_cat_tax, |
| 260 | 'field' => 'name', |
| 261 | 'terms' => $categories, |
| 262 | ), |
| 263 | array( |
| 264 | 'taxonomy' => $this->course_tag_tax, |
| 265 | 'field' => 'name', |
| 266 | 'terms' => $tags, |
| 267 | |
| 268 | ), |
| 269 | ), |
| 270 | ); |
| 271 | |
| 272 | $args = apply_filters( 'tutor_rest_course_by_terms_query_args', $args ); |
| 273 | $query = new WP_Query( $args ); |
| 274 | |
| 275 | if ( count( $query->posts ) > 0 ) { |
| 276 | // unset filter property. |
| 277 | array_map( |
| 278 | function ( $post ) { |
| 279 | unset( $post->filter ); |
| 280 | }, |
| 281 | $query->posts |
| 282 | ); |
| 283 | |
| 284 | $response = array( |
| 285 | 'code' => 'success', |
| 286 | 'message' => __( 'Course retrieved successfully', 'tutor' ), |
| 287 | 'data' => $query->posts, |
| 288 | ); |
| 289 | |
| 290 | return self::send( $response ); |
| 291 | } |
| 292 | |
| 293 | $response = array( |
| 294 | 'code' => 'not_found', |
| 295 | 'message' => __( 'Course not found for given terms', 'tutor' ), |
| 296 | 'data' => array(), |
| 297 | ); |
| 298 | return self::send( $response ); |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Validate terms |
| 303 | * |
| 304 | * @since 1.7.1 |
| 305 | * |
| 306 | * @param array $post post array. |
| 307 | * |
| 308 | * @return array validation errors. |
| 309 | */ |
| 310 | public function validate_terms( array $post ) { |
| 311 | $categories = $post['categories']; |
| 312 | $tags = $post['tags']; |
| 313 | |
| 314 | $error = array(); |
| 315 | |
| 316 | if ( ! is_array( $categories ) ) { |
| 317 | array_push( $error, __( 'Categories field is not an array', 'tutor' ) ); |
| 318 | } |
| 319 | |
| 320 | if ( ! is_array( $tags ) ) { |
| 321 | array_push( $error, __( 'Tags field is not an array', 'tutor' ) ); |
| 322 | } |
| 323 | |
| 324 | return $error; |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Course sort by price API handler |
| 329 | * |
| 330 | * @since 1.7.1 |
| 331 | * |
| 332 | * @param WP_REST_Request $request request params. |
| 333 | * |
| 334 | * @return WP_REST_Response |
| 335 | */ |
| 336 | public function course_sort_by_price( WP_REST_Request $request ) { |
| 337 | $order = $request->get_param( 'order' ); |
| 338 | $paged = $request->get_param( 'page' ); |
| 339 | |
| 340 | $order = sanitize_text_field( $order ); |
| 341 | $paged = sanitize_text_field( $paged ); |
| 342 | $post_per_page = tutor_utils()->get_option( 'pagination_per_page' ); |
| 343 | |
| 344 | $args = array( |
| 345 | 'post_type' => 'product', |
| 346 | 'post_status' => 'publish', |
| 347 | 'posts_per_page' => $post_per_page, |
| 348 | 'paged' => $paged ? $paged : 1, |
| 349 | |
| 350 | 'meta_key' => '_regular_price', |
| 351 | 'orderby' => 'meta_value_num', |
| 352 | 'order' => $order, |
| 353 | 'meta_query' => array( |
| 354 | 'relation' => 'AND', |
| 355 | array( |
| 356 | 'key' => '_tutor_product', |
| 357 | 'value' => 'yes', |
| 358 | |
| 359 | ), |
| 360 | ), |
| 361 | ); |
| 362 | $args = apply_filters( 'tutor_rest_course_sort_by_price_args', $args ); |
| 363 | |
| 364 | $query = new WP_Query( $args ); |
| 365 | |
| 366 | if ( count( $query->posts ) > 0 ) { |
| 367 | // unset filter property. |
| 368 | array_map( |
| 369 | function ( $post ) { |
| 370 | unset( $post->filter ); |
| 371 | }, |
| 372 | $query->posts |
| 373 | ); |
| 374 | |
| 375 | $posts = array(); |
| 376 | |
| 377 | foreach ( $query->posts as $post ) { |
| 378 | $post->price = get_post_meta( $post->ID, '_regular_price', true ); |
| 379 | array_push( $posts, $post ); |
| 380 | } |
| 381 | |
| 382 | $data = array( |
| 383 | 'posts' => $posts, |
| 384 | 'total_course' => $query->found_posts, |
| 385 | 'total_page' => $query->max_num_pages, |
| 386 | ); |
| 387 | |
| 388 | $response = array( |
| 389 | 'code' => 'success', |
| 390 | 'message' => __( 'Course retrieved successfully', 'tutor' ), |
| 391 | 'data' => $data, |
| 392 | ); |
| 393 | |
| 394 | return self::send( $response ); |
| 395 | } |
| 396 | |
| 397 | $response = array( |
| 398 | 'code' => 'not_found', |
| 399 | 'message' => __( 'Course not found', 'tutor' ), |
| 400 | 'data' => array(), |
| 401 | ); |
| 402 | return self::send( $response ); |
| 403 | } |
| 404 | |
| 405 | /** |
| 406 | * Retrieve the course contents for a given course id |
| 407 | * |
| 408 | * @since 2.7.0 |
| 409 | * |
| 410 | * @param WP_REST_Request $request request params. |
| 411 | * |
| 412 | * @return WP_REST_Response |
| 413 | */ |
| 414 | public function course_contents( WP_REST_Request $request ) { |
| 415 | $course_id = $request->get_param( 'id' ); |
| 416 | $topics = tutor_utils()->get_topics( $course_id ); |
| 417 | |
| 418 | if ( $topics->have_posts() ) { |
| 419 | $data = array(); |
| 420 | foreach ( $topics->get_posts() as $post ) { |
| 421 | $current_topic = array( |
| 422 | 'id' => $post->ID, |
| 423 | 'title' => $post->post_title, |
| 424 | 'summary' => $post->post_content, |
| 425 | 'contents' => array(), |
| 426 | ); |
| 427 | |
| 428 | $topic_contents = tutor_utils()->get_course_contents_by_topic( $post->ID, -1 ); |
| 429 | |
| 430 | if ( $topic_contents->have_posts() ) { |
| 431 | foreach ( $topic_contents->get_posts() as $post ) { |
| 432 | array_push( $current_topic['contents'], $post ); |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | array_push( $data, $current_topic ); |
| 437 | } |
| 438 | |
| 439 | $response = array( |
| 440 | 'code' => 'success', |
| 441 | 'message' => __( 'Course contents retrieved successfully', 'tutor' ), |
| 442 | 'data' => $data, |
| 443 | ); |
| 444 | return self::send( $response ); |
| 445 | } |
| 446 | |
| 447 | $response = array( |
| 448 | 'code' => 'not_found', |
| 449 | 'message' => __( 'Contents for this course with the given course id not found', 'tutor' ), |
| 450 | 'data' => array(), |
| 451 | ); |
| 452 | |
| 453 | return self::send( $response ); |
| 454 | } |
| 455 | } |
| 456 |