REST_Author.php
5 years ago
REST_Course.php
4 years ago
REST_Course_Announcement.php
5 years ago
REST_Lesson.php
5 years ago
REST_Quiz.php
5 years ago
REST_Rating.php
5 years ago
REST_Response.php
5 years ago
REST_Topic.php
5 years ago
RestAuth.php
2 years ago
REST_Course.php
293 lines
| 1 | <?php |
| 2 | /* |
| 3 | @REST API for courses |
| 4 | @author : themeum |
| 5 | */ |
| 6 | |
| 7 | namespace TUTOR; |
| 8 | use WP_REST_Request; |
| 9 | use WP_Query; |
| 10 | |
| 11 | if( !defined ('ABSPATH')) |
| 12 | exit; |
| 13 | |
| 14 | class REST_Course { |
| 15 | |
| 16 | use REST_Response; |
| 17 | |
| 18 | private $post_type; |
| 19 | |
| 20 | private $course_cat_tax = "course-category"; |
| 21 | |
| 22 | private $course_tag_tax = "course-tag"; |
| 23 | |
| 24 | public function __construct() { |
| 25 | $this->post_type = tutor()->course_post_type; |
| 26 | } |
| 27 | |
| 28 | /* |
| 29 | *require rest request |
| 30 | *return course info |
| 31 | *pagination enable |
| 32 | *category,tags terms included |
| 33 | */ |
| 34 | public function course(WP_REST_Request $request) { |
| 35 | $order = sanitize_text_field($request->get_param('order')); |
| 36 | $orderby = sanitize_text_field($request->get_param('orderby')); |
| 37 | $paged = sanitize_text_field($request->get_param('paged')); |
| 38 | |
| 39 | $args = array( |
| 40 | 'post_type' => $this->post_type, |
| 41 | 'post_status' => 'publish', |
| 42 | 'posts_per_page' => 10, |
| 43 | 'paged' => $paged ? $paged : 1, |
| 44 | 'order' => $order ? $order : 'ASC', |
| 45 | 'orderby' => $orderby ? $orderby :'title' |
| 46 | ); |
| 47 | |
| 48 | $query = new WP_Query($args); |
| 49 | |
| 50 | |
| 51 | //if post found |
| 52 | if(count($query->posts)>0) { |
| 53 | //unset filter properpty |
| 54 | array_map(function($post){ |
| 55 | unset($post->filter); |
| 56 | }, $query->posts); |
| 57 | |
| 58 | $data = [ |
| 59 | 'posts'=> [], |
| 60 | 'total_course' => $query->found_posts, |
| 61 | 'total_page' => $query->max_num_pages |
| 62 | ]; |
| 63 | |
| 64 | foreach($query->posts as $post) { |
| 65 | $category = wp_get_post_terms($post->ID,$this->course_cat_tax); |
| 66 | |
| 67 | $tag = wp_get_post_terms($post->ID,$this->course_tag_tax); |
| 68 | |
| 69 | $post->course_category = $category; |
| 70 | |
| 71 | $post->course_tag = $tag; |
| 72 | |
| 73 | array_push($data['posts'], $post); |
| 74 | |
| 75 | } |
| 76 | |
| 77 | $response = array( |
| 78 | 'status_code'=> "success", |
| 79 | "message"=> __('Course retrieved successfully','tutor'), |
| 80 | 'data'=> $data |
| 81 | ); |
| 82 | |
| 83 | return self::send($response); |
| 84 | } |
| 85 | |
| 86 | $response = array( |
| 87 | 'status_code'=> "not_found", |
| 88 | "message"=> __('Course not found','tutor'), |
| 89 | 'data'=> [] |
| 90 | ); |
| 91 | |
| 92 | return self::send($response); |
| 93 | } |
| 94 | |
| 95 | /* |
| 96 | *require rest request |
| 97 | *return post meta items |
| 98 | */ |
| 99 | function course_detail(WP_REST_Request $request) { |
| 100 | $post_id = $request->get_param('id'); |
| 101 | |
| 102 | $detail = array( |
| 103 | |
| 104 | 'course_settings' => get_post_meta($post_id,'_tutor_course_settings',false), |
| 105 | |
| 106 | 'course_price_type' => get_post_meta($post_id,'_tutor_course_price_type',false), |
| 107 | |
| 108 | 'course_duration' => get_post_meta($post_id,'_course_duration',false), |
| 109 | |
| 110 | 'course_level' => get_post_meta($post_id,'_tutor_course_level',false), |
| 111 | |
| 112 | 'course_benefits' => get_post_meta($post_id,'_tutor_course_benefits',false), |
| 113 | |
| 114 | 'course_requirements' => get_post_meta($post_id,'_tutor_course_requirements',false), |
| 115 | |
| 116 | 'course_target_audience' => get_post_meta($post_id,'_tutor_course_target_audience',false), |
| 117 | |
| 118 | 'course_material_includes' => get_post_meta($post_id,'_tutor_course_material_includes',false), |
| 119 | |
| 120 | 'video' => get_post_meta($post_id,'_video',false), |
| 121 | |
| 122 | 'disable_qa' => get_post_meta($post_id, '_tutor_enable_qa', true)!='yes' |
| 123 | ); |
| 124 | |
| 125 | if($detail) { |
| 126 | $response = array( |
| 127 | 'status_code' => "course_detail", |
| 128 | "message" => __('Course detail retrieved successfully','tutor'), |
| 129 | 'data' => $detail |
| 130 | ); |
| 131 | return self::send($response); |
| 132 | } |
| 133 | $response = array( |
| 134 | 'status_code' => "course_detail", |
| 135 | "message" => __('Detail not found for given ID','tutor'), |
| 136 | 'data' => [] |
| 137 | ); |
| 138 | |
| 139 | return self::send($response); |
| 140 | } |
| 141 | |
| 142 | /* |
| 143 | *return post type terms |
| 144 | */ |
| 145 | public function course_by_terms(WP_REST_Request $request) { |
| 146 | $post_fields = $request->get_params(); |
| 147 | $validate_err = $this->validate_terms($post_fields); |
| 148 | |
| 149 | //check array or not |
| 150 | if(count($validate_err)>0) { |
| 151 | $response = array( |
| 152 | 'status_code'=> "validation_error", |
| 153 | "message"=> $validate_err, |
| 154 | 'data'=> [] |
| 155 | ); |
| 156 | |
| 157 | return self::send($response); |
| 158 | } |
| 159 | |
| 160 | //sanitize terms |
| 161 | $categories = sanitize_term( $request['categories'], $this->course_cat_tax, $context = 'db' ); |
| 162 | |
| 163 | $tags = sanitize_term( $request['tags'], $this->course_tag_tax, $context = 'db' ); |
| 164 | |
| 165 | $args = array( |
| 166 | 'post_type' => $this->post_type, |
| 167 | 'tax_query' => array( |
| 168 | 'relation' => 'OR', |
| 169 | array( |
| 170 | 'taxonomy' => $this->course_cat_tax, |
| 171 | 'field' => 'name', |
| 172 | 'terms' => $categories |
| 173 | ), |
| 174 | array( |
| 175 | 'taxonomy' => $this->course_tag_tax, |
| 176 | 'field' => 'name', |
| 177 | 'terms' => $tags |
| 178 | |
| 179 | ) |
| 180 | ) |
| 181 | ); |
| 182 | |
| 183 | $query = new WP_Query ($args); |
| 184 | |
| 185 | if(count($query->posts)>0) { |
| 186 | //unset filter proterty |
| 187 | array_map(function($post){ |
| 188 | unset($post->filter); |
| 189 | }, $query->posts); |
| 190 | |
| 191 | $response = array( |
| 192 | 'status_code'=> "success", |
| 193 | "message"=> __("Course retrieved successfully",'tutor'), |
| 194 | 'data'=> $query->posts |
| 195 | ); |
| 196 | |
| 197 | return self::send($response); |
| 198 | } |
| 199 | |
| 200 | $response = array( |
| 201 | 'status_code'=> "not_found", |
| 202 | "message"=> __("Course not found for given terms",'tutor'), |
| 203 | 'data'=> [] |
| 204 | ); |
| 205 | return self::send($response); |
| 206 | } |
| 207 | |
| 208 | /* |
| 209 | *categories array validation |
| 210 | *tags array validation |
| 211 | */ |
| 212 | public function validate_terms(array $post) { |
| 213 | $categories = $post['categories']; |
| 214 | $tags = $post['tags']; |
| 215 | |
| 216 | $error = []; |
| 217 | |
| 218 | if (!is_array($categories)) { |
| 219 | array_push($error,__('Categories field is not an array','tutor')); |
| 220 | } |
| 221 | |
| 222 | if (!is_array($tags)) { |
| 223 | array_push($error,__('Tags field is not an array','tutor')); |
| 224 | } |
| 225 | |
| 226 | return $error; |
| 227 | } |
| 228 | |
| 229 | public function course_sort_by_price(WP_REST_Request $request) { |
| 230 | $order = $request->get_param('order'); |
| 231 | $paged = $request->get_param('page'); |
| 232 | |
| 233 | $order = sanitize_text_field($order); |
| 234 | $paged = sanitize_text_field($paged); |
| 235 | |
| 236 | $args = array( |
| 237 | 'post_type'=> 'product', |
| 238 | 'post_status'=> 'publish', |
| 239 | 'posts_per_page' => 10, |
| 240 | 'paged'=> $paged ? $paged : 1, |
| 241 | |
| 242 | 'meta_key'=> '_regular_price', |
| 243 | 'orderby'=> 'meta_value_num', |
| 244 | 'order'=> $order, |
| 245 | 'meta_query'=> array( |
| 246 | 'relation'=>'AND', |
| 247 | array( |
| 248 | 'key'=> '_tutor_product', |
| 249 | 'value'=> "yes" |
| 250 | |
| 251 | ) |
| 252 | ) |
| 253 | ); |
| 254 | |
| 255 | $query = new WP_Query( $args ); |
| 256 | |
| 257 | if (count($query->posts)>0) { |
| 258 | //unset filter property |
| 259 | array_map(function($post){ |
| 260 | unset($post->filter); |
| 261 | }, $query->posts); |
| 262 | |
| 263 | $posts = []; |
| 264 | |
| 265 | foreach ($query->posts as $post) { |
| 266 | $post->price = get_post_meta($post->ID,'_regular_price', true); |
| 267 | array_push($posts, $post); |
| 268 | } |
| 269 | |
| 270 | $data = array( |
| 271 | 'posts'=> $posts, |
| 272 | 'total_course' => $query->found_posts, |
| 273 | 'total_page' => $query->max_num_pages |
| 274 | ); |
| 275 | |
| 276 | $response = array( |
| 277 | 'status_code'=> 'success', |
| 278 | 'message'=> __('Course retrieved successfully','tutor'), |
| 279 | 'data'=> $data |
| 280 | ); |
| 281 | |
| 282 | return self::send($response); |
| 283 | } |
| 284 | |
| 285 | $response = array( |
| 286 | 'status'=> 'not_found', |
| 287 | 'message'=> __('Course not found','tutor'), |
| 288 | 'data'=> [] |
| 289 | ); |
| 290 | return self::send($response); |
| 291 | } |
| 292 | } |
| 293 |