| 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 |
$categories = null; |
| 81 |
if ( isset( $request['categories'] ) ) { |
| 82 |
$categories = sanitize_term( explode( ',', $request['categories'] ), $this->course_cat_tax, $context = 'db' ); |
| 83 |
} |
| 84 |
$tags = null; |
| 85 |
if ( isset( $request['tags'] ) ) { |
| 86 |
$tags = sanitize_term( explode( ',', $request['tags'] ), $this->course_tag_tax, $context = 'db' ); |
| 87 |
} |
| 88 |
|
| 89 |
$post_per_page = tutor_utils()->get_option( 'pagination_per_page' ); |
| 90 |
|
| 91 |
$args = array( |
| 92 |
'post_type' => $this->post_type, |
| 93 |
'post_status' => 'publish', |
| 94 |
'posts_per_page' => $post_per_page, |
| 95 |
'paged' => $paged ? $paged : 1, |
| 96 |
'order' => $order ? $order : 'ASC', |
| 97 |
'orderby' => $orderby ? $orderby : 'title', |
| 98 |
); |
| 99 |
|
| 100 |
if ( isset( $categories ) || isset( $tags ) ) { |
| 101 |
$args['tax_query'] = array( |
| 102 |
'relation' => 'OR', |
| 103 |
array( |
| 104 |
'taxonomy' => $this->course_cat_tax, |
| 105 |
'field' => 'name', |
| 106 |
'terms' => $categories, |
| 107 |
), |
| 108 |
array( |
| 109 |
'taxonomy' => $this->course_tag_tax, |
| 110 |
'field' => 'name', |
| 111 |
'terms' => $tags, |
| 112 |
|
| 113 |
), |
| 114 |
); |
| 115 |
} |
| 116 |
|
| 117 |
if ( 'price' === $orderby ) { |
| 118 |
$args['post_type'] = 'product'; |
| 119 |
$args['meta_key'] = '_regular_price'; |
| 120 |
$args['meta_query'] = array( |
| 121 |
'relation' => 'AND', |
| 122 |
array( |
| 123 |
'key' => '_tutor_product', |
| 124 |
'value' => 'yes', |
| 125 |
), |
| 126 |
); |
| 127 |
} |
| 128 |
|
| 129 |
$args = apply_filters( 'tutor_rest_course_query_args', $args ); |
| 130 |
|
| 131 |
$query = new WP_Query( $args ); |
| 132 |
|
| 133 |
// if post found. |
| 134 |
if ( count( $query->posts ) > 0 ) { |
| 135 |
// unset filter property. |
| 136 |
array_map( |
| 137 |
function ( $post ) { |
| 138 |
unset( $post->filter ); |
| 139 |
}, |
| 140 |
$query->posts |
| 141 |
); |
| 142 |
|
| 143 |
$data = array( |
| 144 |
'posts' => array(), |
| 145 |
'total_course' => $query->found_posts, |
| 146 |
'total_page' => $query->max_num_pages, |
| 147 |
); |
| 148 |
|
| 149 |
foreach ( $query->posts as $post ) { |
| 150 |
$category = wp_get_post_terms( $post->ID, $this->course_cat_tax ); |
| 151 |
|
| 152 |
$tag = wp_get_post_terms( $post->ID, $this->course_tag_tax ); |
| 153 |
|
| 154 |
$author = get_userdata( $post->post_author ); |
| 155 |
|
| 156 |
if ( $author ) { |
| 157 |
// Unset user pass & key. |
| 158 |
unset( $author->data->user_pass ); |
| 159 |
unset( $author->data->user_activation_key ); |
| 160 |
} |
| 161 |
|
| 162 |
is_a( $author, 'WP_User' ) ? $post->post_author = $author->data : new \stdClass(); |
| 163 |
|
| 164 |
$thumbnail_size = apply_filters( 'tutor_rest_course_thumbnail_size', 'post-thumbnail' ); |
| 165 |
$post->thumbnail_url = get_the_post_thumbnail_url( $post->ID, $thumbnail_size ); |
| 166 |
|
| 167 |
$post->additional_info = $this->course_additional_info( $post->ID ); |
| 168 |
|
| 169 |
$post->ratings = tutor_utils()->get_course_rating( $post->ID ); |
| 170 |
|
| 171 |
$post->course_category = $category; |
| 172 |
|
| 173 |
$post->course_tag = $tag; |
| 174 |
|
| 175 |
$post->price = get_post_meta( $post->ID, '_regular_price', true ); |
| 176 |
|
| 177 |
$post = apply_filters( 'tutor_rest_course_single_post', $post ); |
| 178 |
|
| 179 |
array_push( $data['posts'], $post ); |
| 180 |
} |
| 181 |
|
| 182 |
$response = array( |
| 183 |
'code' => 'success', |
| 184 |
'message' => __( 'Course retrieved successfully', 'tutor' ), |
| 185 |
'data' => $data, |
| 186 |
); |
| 187 |
|
| 188 |
return self::send( $response ); |
| 189 |
} |
| 190 |
|
| 191 |
$response = array( |
| 192 |
'code' => 'not_found', |
| 193 |
'message' => __( 'Course not found', 'tutor' ), |
| 194 |
'data' => array(), |
| 195 |
); |
| 196 |
|
| 197 |
return self::send( $response ); |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Course Details API handler |
| 202 |
* |
| 203 |
* @since 1.7.1 |
| 204 |
* |
| 205 |
* @param WP_REST_Request $request request params. |
| 206 |
* |
| 207 |
* @return WP_REST_Response |
| 208 |
*/ |
| 209 |
public function course_detail( WP_REST_Request $request ) { |
| 210 |
$post_id = $request->get_param( 'id' ); |
| 211 |
|
| 212 |
$detail = $this->course_additional_info( $post_id ); |
| 213 |
if ( $detail ) { |
| 214 |
$response = array( |
| 215 |
'code' => 'course_detail', |
| 216 |
'message' => __( 'Course detail retrieved successfully', 'tutor' ), |
| 217 |
'data' => $detail, |
| 218 |
); |
| 219 |
return self::send( $response ); |
| 220 |
} |
| 221 |
$response = array( |
| 222 |
'code' => 'course_detail', |
| 223 |
'message' => __( 'Detail not found for given ID', 'tutor' ), |
| 224 |
'data' => array(), |
| 225 |
); |
| 226 |
|
| 227 |
return self::send( $response ); |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Get course additional info |
| 232 |
* |
| 233 |
* @since 2.6.1 |
| 234 |
* |
| 235 |
* @param integer $post_id post id. |
| 236 |
* |
| 237 |
* @return array |
| 238 |
*/ |
| 239 |
public function course_additional_info( int $post_id ) { |
| 240 |
$detail = array( |
| 241 |
|
| 242 |
'course_settings' => get_post_meta( $post_id, '_tutor_course_settings', false ), |
| 243 |
|
| 244 |
'course_price_type' => get_post_meta( $post_id, '_tutor_course_price_type', false ), |
| 245 |
|
| 246 |
'course_duration' => get_post_meta( $post_id, '_course_duration', false ), |
| 247 |
|
| 248 |
'course_level' => get_post_meta( $post_id, '_tutor_course_level', false ), |
| 249 |
|
| 250 |
'course_benefits' => get_post_meta( $post_id, '_tutor_course_benefits', false ), |
| 251 |
|
| 252 |
'course_requirements' => get_post_meta( $post_id, '_tutor_course_requirements', false ), |
| 253 |
|
| 254 |
'course_target_audience' => get_post_meta( $post_id, '_tutor_course_target_audience', false ), |
| 255 |
|
| 256 |
'course_material_includes' => get_post_meta( $post_id, '_tutor_course_material_includes', false ), |
| 257 |
|
| 258 |
'video' => get_post_meta( $post_id, '_video', false ), |
| 259 |
|
| 260 |
'disable_qa' => get_post_meta( $post_id, '_tutor_enable_qa', true ) != 'yes', |
| 261 |
); |
| 262 |
|
| 263 |
return apply_filters( 'tutor_course_additional_info', $detail ); |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Validate terms |
| 268 |
* |
| 269 |
* @since 1.7.1 |
| 270 |
* |
| 271 |
* @param array $post post array. |
| 272 |
* |
| 273 |
* @return array validation errors. |
| 274 |
*/ |
| 275 |
public function validate_terms( array $post ) { |
| 276 |
$categories = $post['categories']; |
| 277 |
$tags = $post['tags']; |
| 278 |
|
| 279 |
$error = array(); |
| 280 |
|
| 281 |
if ( ! is_array( $categories ) ) { |
| 282 |
array_push( $error, __( 'Categories field is not an array', 'tutor' ) ); |
| 283 |
} |
| 284 |
|
| 285 |
if ( ! is_array( $tags ) ) { |
| 286 |
array_push( $error, __( 'Tags field is not an array', 'tutor' ) ); |
| 287 |
} |
| 288 |
|
| 289 |
return $error; |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
/** |
| 295 |
* Retrieve the course contents for a given course id |
| 296 |
* |
| 297 |
* @since 2.7.0 |
| 298 |
* |
| 299 |
* @param WP_REST_Request $request request params. |
| 300 |
* |
| 301 |
* @return WP_REST_Response |
| 302 |
*/ |
| 303 |
public function course_contents( WP_REST_Request $request ) { |
| 304 |
$course_id = $request->get_param( 'id' ); |
| 305 |
$topics = tutor_utils()->get_topics( $course_id ); |
| 306 |
|
| 307 |
if ( $topics->have_posts() ) { |
| 308 |
$data = array(); |
| 309 |
foreach ( $topics->get_posts() as $post ) { |
| 310 |
$current_topic = array( |
| 311 |
'id' => $post->ID, |
| 312 |
'title' => $post->post_title, |
| 313 |
'summary' => $post->post_content, |
| 314 |
'contents' => array(), |
| 315 |
); |
| 316 |
|
| 317 |
$topic_contents = tutor_utils()->get_course_contents_by_topic( $post->ID, -1 ); |
| 318 |
|
| 319 |
if ( $topic_contents->have_posts() ) { |
| 320 |
foreach ( $topic_contents->get_posts() as $post ) { |
| 321 |
array_push( $current_topic['contents'], $post ); |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
array_push( $data, $current_topic ); |
| 326 |
} |
| 327 |
|
| 328 |
$response = array( |
| 329 |
'code' => 'success', |
| 330 |
'message' => __( 'Course contents retrieved successfully', 'tutor' ), |
| 331 |
'data' => $data, |
| 332 |
); |
| 333 |
return self::send( $response ); |
| 334 |
} |
| 335 |
|
| 336 |
$response = array( |
| 337 |
'code' => 'not_found', |
| 338 |
'message' => __( 'Contents for this course with the given course id not found', 'tutor' ), |
| 339 |
'data' => array(), |
| 340 |
); |
| 341 |
|
| 342 |
return self::send( $response ); |
| 343 |
} |
| 344 |
} |
| 345 |
|