PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.8
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.8
4.4.8 4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 All 139 releases
learnpress / inc / jwt / rest-api / version1 / class-lp-rest-section-items-v1-controller.php

class-lp-rest-section-items-v1-controller.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.4.8, at inc/jwt/rest-api/version1/class-lp-rest-section-items-v1-controller.php

366 lines 11.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 class LP_Jwt_Section_Items_V1_Controller extends LP_REST_Jwt_Controller {
3 protected $namespace = 'learnpress/v1';
4
5 protected $rest_base = 'section-items';
6
7 public function register_routes() {
8 register_rest_route(
9 $this->namespace,
10 '/' . $this->rest_base . '/items/(?P<section_id>[\d]+)',
11 array(
12 'args' => array(
13 'section_id' => array(
14 'description' => esc_html__( 'Section ID.', 'learnpress' ),
15 'type' => 'integer',
16 ),
17 ),
18 array(
19 'methods' => WP_REST_Server::READABLE,
20 'callback' => array( $this, 'get_section_items' ),
21 'permission_callback' => array( $this, 'get_items_permissions_check' ),
22 'args' => $this->get_collection_params(),
23 ),
24 'schema' => array( $this, 'get_public_item_schema' ),
25 )
26 );
27 }
28
29 public function get_items_permissions_check( $request ) {
30 if ( empty( $request['section_id'] ) ) {
31 return new WP_Error( 'lp_section_not_section_id', __( 'Sorry, Invalid Section ID param.', 'learnpress' ), array( 'status' => rest_authorization_required_code() ) );
32 }
33
34 $course_id = LP_Section_DB::getInstance()->get_course_id_by_section( $request['section_id'] );
35
36 if ( ! $course_id ) {
37 return new WP_Error( 'lp_section_not_course', __( 'Please assign a section to the Course.', 'learnpress' ), array( 'status' => rest_authorization_required_code() ) );
38 }
39
40 $post = get_post( $course_id );
41
42 $post_status_obj = get_post_status_object( $post->post_status );
43 if ( ! $post_status_obj || ! $post_status_obj->public ) {
44 return new WP_Error( 'lp_section_not_public', __( 'The course is not public', 'learnpress' ), array( 'status' => rest_authorization_required_code() ) );
45 }
46
47 return true;
48 }
49
50 public function get_section_items( $request ) {
51 $filters = new LP_Section_Items_Filter();
52 $filters->section_id = $request['section_id'];
53 $filters->limit = $request['per_page'];
54 $filters->page = $request['page'];
55 $filters->order = $request['order'];
56 $filters->search_title = $request['search'];
57 $filters->item_ids = $request['include'];
58 $filters->item_not_ids = $request['exclude'];
59
60 $query_results = LP_Section_DB::getInstance()->get_section_items_by_section_id( $filters );
61
62 if ( is_wp_error( $query_results ) ) {
63 return $query_results;
64 }
65
66 $results = $query_results['results'];
67
68 $return = array();
69
70 foreach ( $results as $result ) {
71 $data = $this->prepare_object_for_response( $result, $request );
72 $return[] = $this->prepare_response_for_collection( $data );
73 }
74
75 $response = rest_ensure_response( $return );
76
77 $response->header( 'X-WP-Total', $query_results['total'] );
78 $response->header( 'X-WP-TotalPages', (int) $query_results['pages'] );
79
80 return $response;
81 }
82
83 public function prepare_object_for_response( $section_data, $request ) {
84 $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
85
86 $data = $this->get_sections_data( $section_data, $context, $request );
87
88 $response = rest_ensure_response( $data );
89
90 return apply_filters( 'lp_jwt_rest_prepare_sections_object', $response, $section_data, $request );
91 }
92
93 public function get_sections_data( $item_data, $context, $request ) {
94 $fields = $this->get_fields_for_response( $request );
95
96 $course_id = LP_Section_DB::getInstance()->get_course_id_by_section( $request['section_id'] ?? 0 );
97 $course = learn_press_get_course( $course_id );
98
99 if ( ! $course ) {
100 return false;
101 }
102
103 $item_id = absint( $item_data['ID'] );
104
105 $course_item = $course->get_item( $item_id );
106
107 // Check if item is not exists or deactive add-on( Assignment, H5P, etc )
108 if ( ! $course_item instanceof LP_Course_Item ) {
109 return false;
110 }
111
112 $user = $this->get_current_user();
113
114 $user_course = $user ? $user->get_course_data( $course_id ) : false;
115
116 $user_item = $user_course ? $user_course->get_item( $item_id ) : false;
117
118 if ( $user_item ) {
119 $graduation = $user_item->get_graduation();
120 $status = $user_item->get_status();
121 }
122
123 $can_view_item = new LP_Model_User_Can_View_Course_Item();
124 if ( $user ) {
125 $can_view_content_course = $user->can_view_content_course( absint( $course_id ) );
126 $can_view_item = $user->can_view_item( $item_id, $can_view_content_course );
127 }
128
129 if ( rest_is_field_included( 'id', $fields ) ) {
130 $data['id'] = $item_data['ID'] ?? 0;
131 }
132
133 if ( rest_is_field_included( 'name', $fields ) ) {
134 $data['name'] = $item_data['post_title'] ?? '';
135 }
136
137 if ( rest_is_field_included( 'slug', $fields ) ) {
138 $data['slug'] = $item_data['post_name'] ?? '';
139 }
140
141 if ( rest_is_field_included( 'status', $fields ) ) {
142 $data['status'] = $item_data['post_status'] ?? '';
143 }
144
145 if ( rest_is_field_included( 'permalink', $fields ) ) {
146 $data['permalink'] = $can_view_item->flag ? $course_item->get_permalink() : '';
147 }
148
149 if ( rest_is_field_included( 'type', $fields ) ) {
150 $data['type'] = $course_item->get_item_type();
151 }
152
153 if ( rest_is_field_included( 'preview', $fields ) ) {
154 $data['preview'] = $course_item->is_preview();
155 }
156
157 if ( rest_is_field_included( 'duration', $fields ) ) {
158 $data['duration'] = $course_item->get_duration()->to_timer(
159 array(
160 'day' => __( '%s days', 'learnpress' ),
161 'hour' => __( '%s hours', 'learnpress' ),
162 'minute' => __( '%s mins', 'learnpress' ),
163 'second' => __( '%s secs', 'learnpress' ),
164 ),
165 true
166 );
167 }
168
169 if ( rest_is_field_included( 'graduation', $fields ) && $graduation ) {
170 $data['graduation'] = $graduation;
171 }
172
173 if ( rest_is_field_included( 'user_status', $fields ) && $status ) {
174 $data['user_status'] = $status;
175 }
176
177 if ( rest_is_field_included( 'locked', $fields ) ) {
178 $data['locked'] = ! $can_view_item->flag;
179 }
180
181 if ( rest_is_field_included( 'count_question', $fields ) && $course_item->get_item_type() === LP_QUIZ_CPT ) {
182 $data['count_question'] = $course_item->count_questions();
183 }
184
185 if ( rest_is_field_included( 'content.raw', $fields ) && $can_view_item->flag ) {
186 $data['content']['raw'] = $item_data['post_content'];
187 }
188
189 if ( rest_is_field_included( 'content.rendered', $fields ) && $can_view_item->flag ) {
190 $data['content']['rendered'] = apply_filters( 'the_content', $item_data['post_content'] );
191 }
192
193 return $data;
194 }
195
196 public function get_current_user() {
197 $user_id = get_current_user_id();
198
199 if ( ! $user_id ) {
200 return false;
201 }
202
203 return learn_press_get_user( $user_id );
204 }
205
206 public function get_item_schema() {
207 $schema = array(
208 '$schema' => 'http://json-schema.org/draft-04/schema#',
209 'title' => 'learnpress_sections',
210 'type' => 'object',
211 'properties' => array(
212 'id' => array(
213 'description' => __( 'A unique identifier for the resource.', 'learnpress' ),
214 'type' => 'integer',
215 'context' => array( 'view', 'edit' ),
216 'readonly' => true,
217 ),
218 'name' => array(
219 'description' => __( 'Item name.', 'learnpress' ),
220 'type' => 'string',
221 'context' => array( 'view', 'edit' ),
222 ),
223 'slug' => array(
224 'description' => __( 'Item slug.', 'learnpress' ),
225 'type' => 'string',
226 'context' => array( 'view', 'edit' ),
227 ),
228 'permalink' => array(
229 'description' => __( 'Item URL.', 'learnpress' ),
230 'type' => 'string',
231 'format' => 'uri',
232 'context' => array( 'view', 'edit' ),
233 'readonly' => true,
234 ),
235 'status' => array(
236 'description' => __( 'Item status (post status).', 'learnpress' ),
237 'type' => 'string',
238 'default' => 'publish',
239 'enum' => array_merge( array_keys( get_post_statuses() ), array( 'future' ) ),
240 'context' => array( 'view', 'edit' ),
241 ),
242 'type' => array(
243 'description' => __( 'Item type.', 'learnpress' ),
244 'type' => 'string',
245 'default' => '',
246 'enum' => learn_press_get_block_course_item_types(),
247 'context' => array( 'view', 'edit' ),
248 ),
249 'preview' => array(
250 'description' => __( 'Item preview.', 'learnpress' ),
251 'type' => 'boolean',
252 'default' => false,
253 'context' => array( 'view', 'edit' ),
254 ),
255 'duration' => array(
256 'description' => __( 'Item duration.', 'learnpress' ),
257 'type' => 'string',
258 'default' => '',
259 'context' => array( 'view', 'edit' ),
260 ),
261 'graduation' => array(
262 'description' => __( 'Item graduation.', 'learnpress' ),
263 'type' => 'string',
264 'default' => '',
265 'context' => array( 'view', 'edit' ),
266 ),
267 'user_status' => array(
268 'description' => __( 'Item status.', 'learnpress' ),
269 'type' => 'string',
270 'default' => '',
271 'context' => array( 'view', 'edit' ),
272 ),
273 'locked' => array(
274 'description' => __( 'Item locked.', 'learnpress' ),
275 'type' => 'boolean',
276 'default' => false,
277 'context' => array( 'view', 'edit' ),
278 ),
279 'count_question' => array(
280 'description' => __( 'Count questions.', 'learnpress' ),
281 'type' => 'integer',
282 'default' => 0,
283 'context' => array( 'view', 'edit' ),
284 ),
285 'content' => array(
286 'description' => __( 'Item content.', 'learnpress' ),
287 'type' => 'array',
288 'context' => array( 'view', 'edit' ),
289 'properties' => array(
290 'raw' => array(
291 'description' => __( 'Title for the post, as it exists in the database.' ),
292 'type' => 'string',
293 'context' => array( 'edit' ),
294 ),
295 'rendered' => array(
296 'description' => __( 'HTML title for the post, transformed for display.' ),
297 'type' => 'string',
298 'context' => array( 'view', 'edit', 'embed' ),
299 'readonly' => true,
300 ),
301 ),
302 ),
303 ),
304 );
305
306 return $this->add_additional_fields_schema( $schema );
307 }
308
309 public function get_collection_params() {
310 $params = array();
311 $params['context'] = $this->get_context_param();
312 $params['context']['default'] = 'view';
313
314 $params['page'] = array(
315 'description' => __( 'The current page of the collection.', 'learnpress' ),
316 'type' => 'integer',
317 'default' => 1,
318 'sanitize_callback' => 'absint',
319 'validate_callback' => 'rest_validate_request_arg',
320 'minimum' => 1,
321 );
322 $params['per_page'] = array(
323 'description' => __( 'The maximum number of items to be returned in the result set.', 'learnpress' ),
324 'type' => 'integer',
325 'default' => 10,
326 'minimum' => 1,
327 'maximum' => 100,
328 'sanitize_callback' => 'absint',
329 'validate_callback' => 'rest_validate_request_arg',
330 );
331 $params['order'] = array(
332 'description' => __( 'Sorting attributes in ascending or descending order.', 'learnpress' ),
333 'type' => 'string',
334 'default' => 'asc',
335 'enum' => array( 'asc', 'desc' ),
336 'validate_callback' => 'rest_validate_request_arg',
337 );
338 $params['search'] = array(
339 'description' => __( 'Limit results to those matching a string.', 'learnpress' ),
340 'type' => 'string',
341 'sanitize_callback' => 'sanitize_text_field',
342 'validate_callback' => 'rest_validate_request_arg',
343 );
344 $params['exclude'] = array(
345 'description' => __( 'Ensure the result set excludes specific IDs.', 'learnpress' ),
346 'type' => 'array',
347 'items' => array(
348 'type' => 'integer',
349 ),
350 'default' => array(),
351 'sanitize_callback' => 'wp_parse_id_list',
352 );
353 $params['include'] = array(
354 'description' => __( 'Limit the result set to specific IDs.', 'learnpress' ),
355 'type' => 'array',
356 'items' => array(
357 'type' => 'integer',
358 ),
359 'default' => array(),
360 'sanitize_callback' => 'wp_parse_id_list',
361 );
362
363 return apply_filters( 'lp_rest_sections_collection_params', $params );
364 }
365 }
366