class-ld-rest-posts-controller.php
9.92 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
<?php
/**
* LearnDash REST API V1 Post Controller.
*
* @since 2.5.8
* @package LearnDash\REST\V1
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ( ! class_exists( 'LD_REST_Posts_Controller_V1' ) ) && ( class_exists( 'WP_REST_Posts_Controller' ) ) ) {
/**
* Class LearnDash REST API V1 Post Controller.
*
* @since 2.5.8
*/
abstract class LD_REST_Posts_Controller_V1 extends WP_REST_Posts_Controller /* phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound */ {
/**
* REST API version.
*
* @var $version string.
*/
protected $version = 'v1';
/**
* REST API Sub-Controllers
*
* @var array $sub_controllers.
*/
protected $sub_controllers = array();
/**
* Current Course Post object
*
* @var WP_Post $course_post.
*/
protected $course_post = null;
/**
* Current Lesson Post object
*
* @var WP_Post $lesson_post.
*/
protected $lesson_post = null;
/**
* Current Topic Post object
*
* @var WP_Post $topic_post.
*/
protected $topic_post = null;
/**
* Taxonomies
*
* @var array
*/
protected $taxonomies = array();
/**
* Public constructor for class
*
* @since 2.5.8
*
* @param string $post_type Post type.
*/
public function __construct( $post_type = '' ) {
parent::__construct( $post_type );
add_filter( "rest_{$this->post_type}_collection_params", array( $this, 'rest_collection_params_filter' ), 20, 2 );
add_filter( "rest_{$this->post_type}_query", array( $this, 'rest_query_filter' ), 20, 2 );
add_filter( "rest_prepare_{$this->post_type}", array( $this, 'rest_prepare_response_filter' ), 20, 3 );
}
/**
* Registers the routes for the objects of the controller.
*
* @since 2.5.8
*
* @see register_rest_route() in WordPress core.
*/
public function register_routes_wpv2() {
if ( class_exists( 'LD_REST_Posts_Gutenberg_Controller' ) ) {
$g = new LD_REST_Posts_Gutenberg_Controller( $this->post_type );
$g->register_routes();
}
}
/**
* Prepare the LearnDash Post Type Settings.
*
* @since 2.5.8
*/
public function register_fields() {
global $sfwd_lms;
$post_args_fields = $sfwd_lms->get_post_args_section( $this->post_type, 'fields' );
if ( ! empty( $post_args_fields ) ) {
foreach ( $post_args_fields as $field_key => $field_set ) {
if ( ( isset( $field_set['show_in_rest'] ) ) && ( true === $field_set['show_in_rest'] ) ) {
if ( ( isset( $field_set['rest_args'] ) ) && ( is_array( $field_set['rest_args'] ) ) ) {
$field_args = $field_set['rest_args'];
} else {
$field_args = array();
}
if ( ! isset( $field_args['get_callback'] ) ) {
$field_args['get_callback'] = array( $this, 'ld_get_field_value' );
}
if ( ! isset( $rest_field_args['update_callback'] ) ) {
$field_args['update_callback'] = array( $this, 'ld_update_field_value' );
}
if ( ! isset( $field_args['sanitize_callback'] ) ) {
$field_args['sanitize_callback'] = 'sanitize_key';
}
$field_args['schema']['validate_callback'] = array( $this, 'ld_rest_validate_request_arg' );
if ( ( ! isset( $field_args['schema'] ) ) || ( empty( $field_args['schema'] ) ) ) {
$field_args['schema'] = array();
}
if ( ( ! isset( $field_args['schema']['description'] ) ) && ( isset( $field_set['name'] ) ) ) {
$field_args['schema']['description'] = $field_set['name'];
}
if ( ( ! isset( $field_args['schema']['type'] ) ) && ( isset( $field_set['type'] ) ) ) {
switch ( $field_set['type'] ) {
case 'select':
case 'multiselect':
$field_args['schema']['type'] = 'string';
break;
case 'checkbox':
$field_args['schema']['type'] = 'boolean';
break;
default:
$field_args['schema']['type'] = $field_set['type'];
break;
}
}
if ( ( ! isset( $field_args['schema']['required'] ) ) || ( empty( $field_args['schema']['required'] ) ) ) {
$field_args['schema']['required'] = false;
}
if ( ( ! isset( $field_args['schema']['default'] ) ) && ( isset( $field_set['default'] ) ) ) {
$field_args['schema']['default'] = $field_set['default'];
}
if ( ( ! isset( $field_args['schema']['enum'] ) ) && ( ( isset( $field_set['initial_options'] ) ) && ( ! empty( $field_set['initial_options'] ) ) ) ) {
$field_args['schema']['enum'] = array_keys( $field_set['initial_options'] );
}
if ( ! isset( $field_args['schema']['context'] ) ) {
$field_args['schema']['context'] = array( 'view', 'edit' );
}
register_rest_field(
$this->post_type,
$field_key,
$field_args
);
}
}
}
}
/**
* Validate REST request args.
*
* @since 2.5.8
*
* @param mixed $value REST request value.
* @param array $args REST request args.
* @param string $param REST request param.
*/
public function ld_rest_validate_request_arg( $value, $args, $param = '' ) {
return true;
}
/**
* Get Field Value
*
* @since 2.5.8
*
* @param array $postdata Post data.
* @param string $field_name Field name.
* @param WP_REST_Request $request WP_REST_Request instance.
* @param string $post_type Post Type.
*/
public function ld_get_field_value( array $postdata, $field_name, WP_REST_Request $request, $post_type ) {
if ( ( isset( $postdata['id'] ) ) && ( ! empty( $postdata['id'] ) ) ) {
$ld_post = get_post( $postdata['id'] );
if ( ( is_a( $ld_post, 'WP_Post' ) ) && ( $ld_post->post_type == $this->post_type ) ) {
$field_value = learndash_get_setting( $ld_post, $field_name );
switch ( $field_name ) {
case 'course_materials':
$field_value = wp_specialchars_decode( strval( $field_value ), ENT_QUOTES );
if ( ! empty( $field_value ) ) {
$field_value = do_shortcode( $field_value );
}
break;
case 'course_price_type':
if ( 'paynow' === $field_value ) {
$field_value = 'buynow';
}
break;
default:
break;
}
return $field_value;
}
}
}
/**
* Update Field Value
*
* @since 2.5.8
*
* @param mixed $value Field value.
* @param WP_Post $post WP_Post instance.
* @param string $field_name Field name.
* @param WP_REST_Request $request WP_REST_Request instance.
* @param string $post_type Post Type.
*/
public function ld_update_field_value( $value, WP_Post $post, $field_name, WP_REST_Request $request, $post_type ) {
switch ( $field_name ) {
case 'course_prerequisite_enabled':
if ( true === $value ) {
$value = 'on';
}
break;
case 'course_price_type':
if ( 'buynow' === $value ) {
$value = 'paynow';
}
break;
default:
break;
}
learndash_update_setting( $post->ID, $field_name, $value );
return true;
}
/**
* Filters collection parameters for the posts controller.
*
* @since 2.5.8
*
* @param array $query_params Quest params array.
* @param WP_Post_Type $post_type Post type object.
*/
public function rest_collection_params_filter( $query_params, $post_type ) {
global $learndash_post_types;
if ( in_array( $this->post_type, $learndash_post_types, true ) ) {
if ( ( isset( $query_params['orderby']['default'] ) ) && ( 'title' != $query_params['orderby']['default'] ) ) {
$query_params['orderby']['default'] = 'title';
}
if ( ( isset( $query_params['order']['default'] ) ) && ( 'asc' != $query_params['order']['default'] ) ) {
$query_params['order']['default'] = 'asc';
}
}
return $query_params;
}
/**
* Filter query args.
*
* @since 2.5.8
*
* @param array $args Key value array of query var to query value.
* @param WP_REST_Request $request The request used.
*
* @return array Key value array of query var to query value.
*/
public function rest_query_filter( $args, $request ) {
return $args;
}
/**
* Override the REST response links. This is needed when Course Shared Steps is enabled.
*
* @since 3.0.0
*
* @param WP_REST_Response $response WP_REST_Response instance.
* @param WP_Post $post WP_Post instance.
* @param WP_REST_Request $request WP_REST_Request instance.
*/
public function rest_prepare_response_filter( WP_REST_Response $response, WP_Post $post, WP_REST_Request $request ) {
if ( ( LearnDash_Settings_Section::get_section_setting( 'LearnDash_Settings_Section_Permalinks', 'nested_urls' ) == 'yes' ) && ( in_array( $post->post_type, learndash_get_post_types( 'course_steps' ), true ) ) ) {
$request_params_json = $request->get_json_params();
if ( ( isset( $request_params_json['course_id'] ) ) && ( ! empty( $request_params_json['course_id'] ) ) ) {
$course_id = absint( $request_params_json['course_id'] );
if ( ! empty( $course_id ) ) {
$link = learndash_get_step_permalink( $post->ID, $course_id );
$response->data['link'] = $link;
$response->data['permalink_template'] = str_replace( $post->post_name, '%pagename%', $link );
// These are not needed or used on the Gutenberg UI but change anyway.
$response->data['guid']['rendered'] = $link;
$response->data['guid']['raw'] = $link;
}
}
}
return $response;
}
/**
* Check if REST Request is for this version/route.
*
* @since 3.4.2
*
* @param WP_REST_Request $request WP_REST_Request Request instance.
*
* @return bool true if match.
*/
protected function is_rest_request( WP_REST_Request $request ) {
$request_route_base = '/' . $this->namespace . '/' . $this->rest_base;
if ( strncasecmp( $request->get_route(), $request_route_base, strlen( $request_route_base ) ) === 0 ) {
return true;
}
return false;
}
// End of functions.
}
}