class-ld-rest-api.php
12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
<?php
/**
* LearnDash REST API loader
*
* @since 2.4.5
* @package LearnDash\REST
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! defined( 'LEARNDASH_REST_API_NAMESPACE' ) ) {
/**
* Define LearnDash LMS - Set REST API Namespace.
*
* @since 2.4.5
*
* @var string Default is 'ldlms'.
*/
define( 'LEARNDASH_REST_API_NAMESPACE', 'ldlms' );
}
if ( ! defined( 'LEARNDASH_REST_API_DIR' ) ) {
/**
* Define LearnDash LMS - Set the REST API library path.
*
* Will be set based on directory of the current file.
*
* @since 2.4.5
*
* @var string Path to REST library.
*/
define( 'LEARNDASH_REST_API_DIR', dirname( __FILE__ ) );
}
require_once LEARNDASH_LMS_PLUGIN_DIR . 'includes/gutenberg/lib/class-ld-rest-gutenberg-posts-controller.php';
if ( ! class_exists( 'LearnDash_REST_API' ) ) {
/**
* Class LearnDash REST API.
*
* @since 2.4.5
*/
class LearnDash_REST_API {
/**
* The reference to *Singleton* instance of this class
*
* @since 2.4.5
*
* @var object $instance
*/
private static $instance;
/**
* Private Controllers
*
* @since 2.4.5
*
* @var array $controllers
*/
private $controllers = array();
/**
* Public constructor for class
*
* @since 2.4.5
*/
public function __construct() {
add_action( 'rest_api_init', array( $this, 'rest_api_init' ), 10 );
}
/**
* Init function to all the LearnDash REST API namespace and endpoints.
*
* @since 2.4.5
*/
public function rest_api_init() {
if ( ( self::enabled() ) && ( self::check_registered_post_type() ) ) {
include_once dirname( __FILE__ ) . '/v1/class-ld-rest-posts-controller.php';
include_once dirname( __FILE__ ) . '/v1/class-ld-rest-users-controller.php';
// v1 controllers.
$controllers_v1 = array(
'LD_REST_Echo_Controller_V1' => array(
'register_routes' => true,
'file' => LEARNDASH_REST_API_DIR . '/v1/class-ld-rest-echo-controller.php',
),
'LD_REST_Courses_Controller_V1' => array(
'register_routes' => true,
'file' => LEARNDASH_REST_API_DIR . '/v1/class-ld-rest-courses-controller.php',
),
'LD_REST_Lessons_Controller_V1' => array(
'register_routes' => true,
'file' => LEARNDASH_REST_API_DIR . '/v1/class-ld-rest-lessons-controller.php',
),
'LD_REST_Topics_Controller_V1' => array(
'register_routes' => true,
'file' => LEARNDASH_REST_API_DIR . '/v1/class-ld-rest-topics-controller.php',
),
'LD_REST_Quizzes_Controller_V1' => array(
'register_routes' => true,
'file' => LEARNDASH_REST_API_DIR . '/v1/class-ld-rest-quizzes-controller.php',
),
'LD_REST_Groups_Controller_V1' => array(
'register_routes' => true,
'file' => LEARNDASH_REST_API_DIR . '/v1/class-ld-rest-groups-controller.php',
),
'LD_REST_Users_Groups_Controller_V1' => array(
'register_routes' => true,
'file' => LEARNDASH_REST_API_DIR . '/v1/class-ld-rest-users-groups-controller.php',
),
'LD_REST_Users_Courses_Controller_V1' => array(
'register_routes' => true,
'file' => LEARNDASH_REST_API_DIR . '/v1/class-ld-rest-users-courses-controller.php',
),
'LD_REST_Questions_Controller_V1' => array(
'register_routes' => true,
'file' => LEARNDASH_REST_API_DIR . '/v1/class-ld-rest-questions-controller.php',
),
'LD_REST_Sections_Controller_V1' => array(
'register_routes' => true,
'file' => LEARNDASH_REST_API_DIR . '/v1/class-ld-rest-sections-controller.php',
),
);
include_once dirname( __FILE__ ) . '/v2/class-ld-rest-posts-controller.php';
include_once dirname( __FILE__ ) . '/v2/class-ld-rest-users-controller.php';
$controllers_v2 = array(
'LD_REST_Courses_Controller_V2' => array(
'register_routes' => true,
'file' => LEARNDASH_REST_API_DIR . '/v2/class-ld-rest-courses-controller.php',
),
'LD_REST_Lessons_Controller_V2' => array(
'register_routes' => true,
'file' => LEARNDASH_REST_API_DIR . '/v2/class-ld-rest-lessons-controller.php',
),
'LD_REST_Topics_Controller_V2' => array(
'register_routes' => true,
'file' => LEARNDASH_REST_API_DIR . '/v2/class-ld-rest-topics-controller.php',
),
'LD_REST_Quizzes_Controller_V2' => array(
'register_routes' => true,
'file' => LEARNDASH_REST_API_DIR . '/v2/class-ld-rest-quizzes-controller.php',
),
'LD_REST_Essays_Controller_V2' => array(
'register_routes' => true,
'file' => LEARNDASH_REST_API_DIR . '/v2/class-ld-rest-essays-controller.php',
),
'LD_REST_Questions_Controller_V2' => array(
'register_routes' => true,
'file' => LEARNDASH_REST_API_DIR . '/v2/class-ld-rest-questions-controller.php',
),
'LD_REST_Assignments_Controller_V2' => array(
'register_routes' => true,
'file' => LEARNDASH_REST_API_DIR . '/v2/class-ld-rest-assignments-controller.php',
),
'LD_REST_Groups_Controller_V2' => array(
'register_routes' => true,
'file' => LEARNDASH_REST_API_DIR . '/v2/class-ld-rest-groups-controller.php',
),
'LD_REST_Users_Courses_Controller_V2' => array(
'register_routes' => true,
'file' => LEARNDASH_REST_API_DIR . '/v2/class-ld-rest-users-courses-controller.php',
),
'LD_REST_Users_Groups_Controller_V2' => array(
'register_routes' => true,
'file' => LEARNDASH_REST_API_DIR . '/v2/class-ld-rest-users-groups-controller.php',
),
'LD_REST_Users_Course_Progress_Controller_V2' => array(
'register_routes' => true,
'file' => LEARNDASH_REST_API_DIR . '/v2/class-ld-rest-users-course-progress-controller.php',
),
'LD_REST_Users_Quiz_Progress_Controller_V2' => array(
'register_routes' => true,
'file' => LEARNDASH_REST_API_DIR . '/v2/class-ld-rest-users-quiz-progress-controller.php',
),
'LD_REST_Quiz_Statistics_Controller_V2' => array(
'register_routes' => true,
'file' => LEARNDASH_REST_API_DIR . '/v2/class-ld-rest-quiz-statistics-controller.php',
),
'LD_REST_Quiz_Form_Entries_Controller_V2' => array(
'register_routes' => true,
'file' => LEARNDASH_REST_API_DIR . '/v2/class-ld-rest-quiz-form-entries-controller.php',
),
'LD_REST_Echo_Controller_V2' => array(
'register_routes' => true,
'file' => LEARNDASH_REST_API_DIR . '/v2/class-ld-rest-echo-controller.php',
),
'LD_REST_Price_Types_Controller_V2' => array(
'register_routes' => true,
'file' => LEARNDASH_REST_API_DIR . '/v2/class-ld-rest-price-types-controller.php',
),
'LD_REST_Question_Types_Controller_V2' => array(
'register_routes' => true,
'file' => LEARNDASH_REST_API_DIR . '/v2/class-ld-rest-question-types-controller.php',
),
'LD_REST_Progress_Status_Controller_V2' => array(
'register_routes' => true,
'file' => LEARNDASH_REST_API_DIR . '/v2/class-ld-rest-progress-status-controller.php',
),
'LD_REST_Exams_Controller_V2' => array(
'register_routes' => true,
'file' => LEARNDASH_REST_API_DIR . '/v2/class-ld-rest-exams-controller.php',
),
);
$this->controllers = array_merge( $controllers_v1, $controllers_v2 );
/**
* Filters the list of REST API controllers.
*
* @param array $controllers An array of REST API controllers data.
*/
//phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
$this->controllers = apply_filters( 'learndash-rest-api-controllers', $this->controllers );
if ( ! empty( $this->controllers ) ) {
foreach ( $this->controllers as $controller_class => $set ) {
if ( ( isset( $set['file'] ) ) && ( ! empty( $set['file'] ) ) && ( file_exists( $set['file'] ) ) ) {
include_once $set['file'];
if ( ( isset( $set['register_routes'] ) ) && ( true === $set['register_routes'] ) ) {
$this->$controller_class = new $controller_class();
$this->$controller_class->register_routes();
}
}
}
}
}
}
/**
* Returns the *Singleton* instance of this class.
*
* @since 2.4.5
*
* @return The *Singleton* instance.
*/
final public static function get_instance() {
if ( null === static::$instance ) {
static::$instance = new self();
}
return static::$instance;
}
/**
* Override class function for 'this'.
* This function handles out Singleton logic.
*
* @since 2.4.5
*
* @return reference to current instance
*/
public static function this() {
return self::$instance;
}
/**
* Check if REST is enabled for post type.
*
* @since 2.5.8
*
* @param string $post_type Post Type slug to check.
*
* @return bool true is enable. Otherwise false.
*/
public static function enabled( $post_type = '' ) {
$return = false;
if ( ( defined( 'LEARNDASH_REST_API_ENABLED' ) ) && ( true === LEARNDASH_REST_API_ENABLED ) ) {
if ( LearnDash_Settings_Section::get_section_setting( 'LearnDash_Settings_Section_General_REST_API', 'enabled' ) === 'yes' ) {
$return = true;
}
}
/**
* Filters whether the LearnDash REST API is enabled or not.
*
* @param boolean $enabled Whether the REST API is enabled or not.
* @param string $post_type The slug of the post type to be checked.
*/
return apply_filters( 'learndash_rest_api_enabled', $return, $post_type );
}
/**
* Check if Gutenberg editor is enabled for post type.
*
* @since 2.5.8
*
* @param string $post_type Post Type slug to check.
*
* @return bool true is enable. Otherwise false.
*/
public static function gutenberg_enabled( $post_type = '' ) {
$return = false;
if ( ( defined( 'LEARNDASH_GUTENBERG' ) ) && ( LEARNDASH_GUTENBERG === true ) ) {
$return = true;
}
/**
* Filters whether the Gutenberg editor is enabled for the plugin or not.
*
* @param boolean $enabled Whether the Gutenberg editor is enabled or not.
* @param string $post_type The slug of the post type to be checked.
*/
return apply_filters( 'learndash_gutenberg_enabled', $return, $post_type );
}
/**
* Get REST controller for post type.
*
* @since 2.5.8
*
* @param string $post_type Post Type slug to check.
*
* @return string Class name.
*/
public static function get_controller( $post_type = '' ) {
$rest_controller = '';
if ( ! empty( $post_type ) ) {
switch ( $post_type ) {
case learndash_get_post_type_slug( 'course' ):
if ( self::enabled( $post_type ) ) {
$rest_controller = 'LD_REST_Courses_Controller_V1';
} elseif ( self::gutenberg_enabled( $post_type ) ) {
$rest_controller = 'LD_REST_Posts_Gutenberg_Controller';
}
break;
case learndash_get_post_type_slug( 'lesson' ):
if ( self::enabled( $post_type ) ) {
$rest_controller = 'LD_REST_Lessons_Controller_V1';
} elseif ( self::gutenberg_enabled( $post_type ) ) {
$rest_controller = 'LD_REST_Posts_Gutenberg_Controller';
}
break;
case learndash_get_post_type_slug( 'topic' ):
if ( self::enabled( $post_type ) ) {
$rest_controller = 'LD_REST_Topics_Controller_V1';
} elseif ( self::gutenberg_enabled( $post_type ) ) {
$rest_controller = 'LD_REST_Posts_Gutenberg_Controller';
}
break;
case learndash_get_post_type_slug( 'quiz' ):
if ( self::enabled( $post_type ) ) {
$rest_controller = 'LD_REST_Quizzes_Controller_V1';
} elseif ( self::gutenberg_enabled( $post_type ) ) {
$rest_controller = 'LD_REST_Posts_Gutenberg_Controller';
}
break;
case learndash_get_post_type_slug( 'group' ):
if ( self::enabled( $post_type ) ) {
$rest_controller = 'LD_REST_Groups_Controller_V1';
} elseif ( self::gutenberg_enabled( $post_type ) ) {
$rest_controller = 'LD_REST_Posts_Gutenberg_Controller';
}
break;
default:
break;
}
}
return $rest_controller;
}
/**
* Check that the LearnDash custom post types are registered.
*
* This is to prevent some instances where the rest_init is called too early.
*
* @since 4.2.0
*
* @return bool true if all post types are registered. Otherwise false.
*/
public static function check_registered_post_type() : bool {
$args = array(
'public' => true,
'_builtin' => false,
);
$output = 'names';
$operator = 'and';
$post_types = get_post_types( $args, $output, $operator );
if ( ( is_array( $post_types ) ) && ( isset( $post_types[ learndash_get_post_type_slug( 'course' ) ] ) ) ) {
return true;
}
return false;
}
}
}
LearnDash_REST_API::get_instance();