PluginProbe
Tutor LMS – eLearning and online course solution / 1.6.9
Tutor LMS – eLearning and online course solution v1.6.9
4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 4.0.2 4.0.1 4.0.0 3.9.15 3.9.14 3.9.13 3.9.12 3.9.11 trunk 1.0.0 1.0.0-alpha 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 All 191 releases
← All changes | classes/RestAPI.php +118 -400 3.9.131.6.9 View file →
@@ -1,400 +1,118 @@
1 -<?php //phpcs:ignore WordPress.Files.FileName.NotHyphenatedLowercase
2 -/**
3 - * RestAPI class
4 - *
5 - * @package Tutor\API
6 - * @author Themeum <support@themeum.com>
7 - * @link https://themeum.com
8 - * @since 1.5.0
9 - */
10 -
11 -namespace TUTOR;
12 -
13 -if ( ! defined( 'ABSPATH' ) ) {
14 - exit;
15 -}
16 -
17 -/**
18 - * Initialize REST API
19 - *
20 - * @since 1.5.0
21 - */
22 -class RestAPI {
23 -
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 - */
41 - protected $course_post_type;
42 -
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 - public function __construct() {
105 -
106 - $this->path = plugin_dir_path( TUTOR_FILE );
107 -
108 - spl_autoload_register( array( $this, 'loader' ) );
109 -
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 -
118 - add_action( 'rest_api_init', array( $this, 'init_routes' ) );
119 - }
120 -
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
136 - );
137 -
138 - $class_name = str_replace( 'TUTOR' . DIRECTORY_SEPARATOR, 'restapi' . DIRECTORY_SEPARATOR, $class_name );
139 - $file_name = $this->path . $class_name . '.php';
140 -
141 - if ( file_exists( $file_name ) ) {
142 - require_once $file_name;
143 - }
144 - }
145 - }
146 -
147 - /**
148 - * Initialize routes
149 - *
150 - * @since 1.5.0
151 - *
152 - * @return void
153 - */
154 - public function init_routes() {
155 - // Courses.
156 - register_rest_route(
157 - $this->namespace,
158 - '/courses',
159 - array(
160 - 'methods' => 'GET',
161 - 'callback' => array(
162 - $this->course_obj,
163 - 'course',
164 - ),
165 - 'permission_callback' => array( RestAuth::class, 'process_api_request' ),
166 - )
167 - );
168 -
169 - // Course details.
170 - register_rest_route(
171 - $this->namespace,
172 - '/courses/(?P<id>\d+)',
173 - array(
174 - 'methods' => 'GET',
175 - 'callback' => array(
176 - $this->course_obj,
177 - 'course_detail',
178 - ),
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' ),
187 - )
188 - );
189 -
190 - // Course topic.
191 - register_rest_route(
192 - $this->namespace,
193 - '/topics',
194 - array(
195 - 'methods' => 'GET',
196 - 'callback' => array(
197 - $this->topic_obj,
198 - 'course_topic',
199 - ),
200 - 'args' => array(
201 - 'course_id' => array(
202 - 'validate_callback' => function ( $param ) {
203 - return is_numeric( $param );
204 - },
205 - ),
206 - ),
207 - 'permission_callback' => array( RestAuth::class, 'process_api_request' ),
208 - )
209 - );
210 -
211 - // Lesson by topic.
212 - register_rest_route(
213 - $this->namespace,
214 - '/lessons',
215 - array(
216 - 'methods' => 'GET',
217 - 'callback' => array(
218 - $this->lesson_obj,
219 - 'topic_lesson',
220 - ),
221 - 'args' => array(
222 - 'topic_id' => array(
223 - 'validate_callback' => function ( $param ) {
224 - return is_numeric( $param );
225 - },
226 - ),
227 - ),
228 - 'permission_callback' => array( RestAuth::class, 'process_api_request' ),
229 - )
230 - );
231 -
232 - // Course announcement by course id.
233 - register_rest_route(
234 - $this->namespace,
235 - '/course-announcement/(?P<id>\d+)',
236 - array(
237 - 'methods' => 'GET',
238 - 'callback' => array(
239 - $this->announcement_obj,
240 - 'course_announcement',
241 - ),
242 - 'args' => array(
243 - 'id' => array(
244 - 'validate_callback' => function ( $param ) {
245 - return is_numeric( $param );
246 - },
247 - ),
248 - ),
249 - 'permission_callback' => array( RestAuth::class, 'process_api_request' ),
250 - )
251 - );
252 -
253 - // Quiz by topic id.
254 - register_rest_route(
255 - $this->namespace,
256 - '/quizzes',
257 - array(
258 - 'methods' => 'GET',
259 - 'callback' => array(
260 - $this->quiz_obj,
261 - 'quiz_with_settings',
262 - ),
263 - 'args' => array(
264 - 'topic_id' => array(
265 - 'validate_callback' => function ( $param ) {
266 - return is_numeric( $param );
267 - },
268 - ),
269 - ),
270 - 'permission_callback' => array( RestAuth::class, 'process_api_request' ),
271 - )
272 - );
273 -
274 - // Quiz by quiz id.
275 - register_rest_route(
276 - $this->namespace,
277 - '/quizzes/(?P<id>\d+)',
278 - array(
279 - 'methods' => 'GET',
280 - 'callback' => array(
281 - $this->quiz_obj,
282 - 'get_quiz',
283 - ),
284 - 'args' => array(
285 - 'id' => array(
286 - 'validate_callback' => function ( $param ) {
287 - return is_numeric( $param );
288 - },
289 - ),
290 - ),
291 - 'permission_callback' => array( RestAuth::class, 'process_api_request' ),
292 - )
293 - );
294 -
295 - // Quiz question answer by quiz id.
296 - register_rest_route(
297 - $this->namespace,
298 - '/quiz-question-answer/(?P<id>\d+)',
299 - array(
300 - 'methods' => 'GET',
301 - 'callback' => array(
302 - $this->quiz_obj,
303 - 'quiz_question_ans',
304 - ),
305 - 'args' => array(
306 - 'id' => array(
307 - 'validate_callback' => function ( $param ) {
308 - return is_numeric( $param );
309 - },
310 - ),
311 - ),
312 - 'permission_callback' => array( RestAuth::class, 'process_api_request' ),
313 - )
314 - );
315 -
316 - // Quiz attempt details by quiz id.
317 - register_rest_route(
318 - $this->namespace,
319 - '/quiz-attempt-details/(?P<id>\d+)',
320 - array(
321 - 'methods' => 'GET',
322 - 'callback' => array(
323 - $this->quiz_obj,
324 - 'quiz_attempt_details',
325 - ),
326 - 'args' => array(
327 - 'id' => array(
328 - 'validate_callback' => function ( $param ) {
329 - return is_numeric( $param );
330 - },
331 - ),
332 - ),
333 - 'permission_callback' => array( RestAuth::class, 'process_api_request' ),
334 - )
335 - );
336 -
337 - // Author detail by id.
338 - register_rest_route(
339 - $this->namespace,
340 - '/author-information/(?P<id>\d+)',
341 - array(
342 - 'methods' => 'GET',
343 - 'callback' => array(
344 - $this->author_obj,
345 - 'author_detail',
346 - ),
347 - 'args' => array(
348 - 'id' => array(
349 - 'validate_callback' => function ( $param ) {
350 - return is_numeric( $param );
351 - },
352 - ),
353 - ),
354 - 'permission_callback' => array( RestAuth::class, 'process_api_request' ),
355 - )
356 - );
357 -
358 - // Reviews by course id.
359 - register_rest_route(
360 - $this->namespace,
361 - '/course-rating/(?P<id>\d+)',
362 - array(
363 - 'methods' => 'GET',
364 - 'callback' => array(
365 - $this->rating_obj,
366 - 'course_rating',
367 - ),
368 - 'args' => array(
369 - 'id' => array(
370 - 'validate_callback' => function ( $param ) {
371 - return is_numeric( $param );
372 - },
373 - ),
374 - ),
375 - 'permission_callback' => array( RestAuth::class, 'process_api_request' ),
376 - )
377 - );
378 -
379 - // Get course content by id.
380 - register_rest_route(
381 - $this->namespace,
382 - '/course-contents/(?P<id>\d+)',
383 - array(
384 - 'methods' => 'GET',
385 - 'callback' => array(
386 - $this->course_obj,
387 - 'course_contents',
388 - ),
389 - 'args' => array(
390 - 'id' => array(
391 - 'validate_callback' => function ( $param ) {
392 - return is_numeric( $param );
393 - },
394 - ),
395 - ),
396 - 'permission_callback' => array( RestAuth::class, 'process_api_request' ),
397 - )
398 - );
399 - }
400 -}
1 +<?php
2 +/**
3 + * RestAPI class
4 + *
5 + * @author: themeum
6 + * @author_uri: https://themeum.com
7 + * @package Tutor
8 + * @since v.1.5.0
9 + */
10 +
11 +
12 +namespace TUTOR;
13 +
14 +if ( ! defined( 'ABSPATH' ) )
15 + exit;
16 +
17 +class RestAPI {
18 +
19 + protected $namespace = 'tutor/v1';
20 + protected $course_post_type;
21 +
22 + public function __construct() {
23 + $this->course_post_type = tutor()->course_post_type;
24 +
25 + add_action('rest_api_init', array($this, 'rest_api_init'));
26 +
27 + }
28 +
29 +
30 + public function rest_api_init(){
31 + register_rest_route( $this->namespace, 'courses', array( 'methods' => 'GET', 'callback' => array($this, 'courses_api') ) );
32 + }
33 +
34 + public function courses_api(){
35 + global $wpdb;
36 +
37 + $a = array_merge(array(
38 + 'post_type' => $this->course_post_type,
39 + 'post_status' => 'publish',
40 +
41 + 'id' => '',
42 + 'exclude_ids' => '',
43 + 'category' => '',
44 +
45 + 'orderby' => 'ID',
46 + 'order' => 'DESC',
47 + 'count' => '10',
48 + ), $_GET);
49 +
50 + $limit = (int) $a['count'];
51 + $exclude_ids_query = '';
52 + $in_ids_query = '';
53 + $tax_join = '';
54 + $tax_where = '';
55 +
56 + $orderby = sanitize_text_field($a['orderby']);
57 + $order = sanitize_text_field($a['order']);
58 +
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 + }
67 + }
68 +
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 + }
74 + }
75 +
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 + )
87 + );
88 +
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);
92 + }
93 +
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);
105 +
106 +
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();
112 +
113 +
114 + }
115 +
116 +
117 +
118 +}