PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.3
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.3
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 4.2.1 All 138 releases
learnpress / inc / admin / editor / class-lp-admin-editor-course.php

class-lp-admin-editor-course.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.4.3, at inc/admin/editor/class-lp-admin-editor-course.php

400 lines 9.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Class LP_Admin_Editor_Course
5 *
6 * @since 3.0.2
7 */
8 class LP_Admin_Editor_Course extends LP_Admin_Editor {
9
10 /**
11 * @var LP_Course_CURD
12 */
13 protected $course_curd = null;
14
15 /**
16 * @var LP_Section_CURD
17 */
18 protected $section_curd = null;
19
20 /**
21 * @var LP_Course
22 */
23 protected $course = null;
24
25 /**
26 * LP_Admin_Editor_Course constructor.
27 */
28 public function __construct() {
29 }
30
31 /**
32 * Do the action depending on ajax calls with params
33 *
34 * @return bool|WP_Error
35 */
36 public function dispatch() {
37 check_ajax_referer( 'learnpress_update_curriculum', 'nonce' );
38
39 $args = wp_parse_args(
40 $_REQUEST,
41 array(
42 'id' => 0,
43 'type' => '',
44 )
45 );
46 $course_id = $args['id'] ?? 0;
47 $course = learn_press_get_course( $course_id );
48
49 if ( ! $course ) {
50 return false;
51 }
52
53 $this->course = $course;
54 $this->course_curd = new LP_Course_CURD();
55 $this->section_curd = new LP_Section_CURD( $course_id );
56 $this->result = array( $args['type'] );
57
58 $this->call( $args['type'], array( $args ) );
59 //LP_Course_Post_Type::instance()->save_post( $course_id, null, true );
60 $bg = LP_Background_Single_Course::instance();
61 $bg->data(
62 array(
63 'handle_name' => 'save_post',
64 'course_id' => $course_id,
65 'data' => [],
66 )
67 )->dispatch();
68
69 return $this->get_result();
70 }
71
72 /**
73 * @param array $args
74 *
75 * @return bool
76 */
77 public function draft_course( $args = array() ) {
78 $new_course = ! empty( $args['course'] ) ? $args['course'] : false;
79 $new_course = json_decode( wp_unslash( $new_course ), true );
80
81 if ( ! $new_course ) {
82 return false;
83 }
84
85 $title = $new_course['title'] ? $new_course['title'] : __( 'New Course', 'learnpress' );
86 $content = $new_course['content'] ? $new_course['content'] : '';
87
88 $args = array(
89 'id' => $this->course->get_id(),
90 'status' => 'draft',
91 'title' => $title,
92 'content' => $content,
93 );
94
95 $this->course_curd->create( $args );
96
97 return true;
98 }
99
100 /**
101 * @param array $args
102 */
103 public function hidden_sections( $args ) {
104 $hidden = ! empty( $args['hidden'] ) ? $args['hidden'] : false;
105 update_post_meta( $this->course->get_id(), '_admin_hidden_sections', $hidden );
106 }
107
108 /**
109 * Sort sections
110 *
111 * @param array $args
112 */
113 public function sort_sections( $args = array() ) {
114 $order = ! empty( $args['order'] ) ? $args['order'] : false;
115 $order = json_decode( wp_unslash( $order ), true );
116
117 if ( ! $order ) {
118 return;
119 }
120
121 //$this->course ? $this->course->get_sections() : '';
122 $this->result = $this->section_curd->update_sections_order( $order );
123
124 // update final quiz
125 //$this->section_curd->update_final_item();
126 }
127
128 /**
129 * @param array $args
130 *
131 * @return mixed
132 */
133 public function update_section( $args = array() ) {
134 $section = $args['section'] ?? false;
135 $section = json_decode( wp_unslash( $section ), true );
136
137 if ( ! $section ) {
138 return false;
139 }
140
141 if ( ! isset( $section['course_id'] ) && ! isset( $section['id'] ) ) {
142 return false;
143 }
144
145 $data = array(
146 'section_id' => $section['id'] ?? $section['section_id'] ?? 0,
147 'section_name' => $section['title'] ?? $section['section_name'] ?? '',
148 'section_description' => $section['description'] ?? $section['section_description'] ?? '',
149 'section_order' => $section['order'] ?? $section['section_order'] ?? 0,
150 'section_course_id' => $this->course->get_id(),
151 );
152
153 $this->result = $this->section_curd->update( $data );
154
155 return $this->result;
156 }
157
158 /**
159 * @param array $args
160 *
161 * @return mixed
162 */
163 public function remove_section( $args = array() ) {
164 $section_id = ! empty( $args['section_id'] ) ? $args['section_id'] : false;
165
166 if ( ! $section_id ) {
167 return false;
168 }
169
170 $this->section_curd->delete( $section_id );
171
172 return true;
173 }
174
175 /**
176 * @param array $args
177 *
178 * @return mixed
179 */
180 public function new_section( $args = array() ) {
181 $section_name = $args['section_name'] ?? '';
182 $temp_id = $args['temp_id'] ?? 0;
183
184 $args = array(
185 'section_course_id' => $this->course->get_id(),
186 'section_description' => '',
187 'section_name' => $section_name,
188 'items' => array(),
189 );
190
191 $section = $this->section_curd->create( $args );
192
193 $this->result = array(
194 'temp_id' => $temp_id,
195 'id' => $section['section_id'],
196 'items' => $section['items'],
197 'title' => $section['section_name'],
198 'description' => $section['section_description'],
199 'course_id' => $section['section_course_id'],
200 'order' => $section['section_order'],
201 );
202
203 return true;
204 }
205
206 /**
207 * @param array $args
208 *
209 * @return mixed
210 */
211 public function update_section_item( $args = array() ) {
212 $section_id = ! empty( $args['section_id'] ) ? $args['section_id'] : false;
213 $item = ! empty( $args['item'] ) ? $args['item'] : false;
214 $item = json_decode( wp_unslash( $item ), true );
215
216 if ( ! ( $section_id && $item ) ) {
217 return func_get_args();
218 }
219
220 // update lesson, quiz title
221 $this->result = $this->section_curd->update_item( $item );
222
223 return true;
224 }
225
226 /**
227 * @param array $args
228 *
229 * @return mixed
230 */
231 public function remove_section_item( $args = array() ) {
232 $section_id = $args['section_id'] ?? 0;
233 $item_id = $args['item_id'] ?? 0;
234
235 try {
236 // Instructor only remove item in my item.
237 if ( absint( get_post_field( 'post_author', $item_id ) ) !== absint( get_current_user_id() ) && ! current_user_can( 'administrator' ) ) {
238 throw new Exception( __( 'You can not delete this item!', 'learnpress' ) );
239 }
240
241 if ( ! ( $section_id && $item_id ) ) {
242 throw new Exception( __( 'Invalid params!', 'learnpress' ) );
243 }
244
245 // remove item from course
246 $this->course_curd->remove_item( $item_id );
247 $this->result = true;
248 } catch ( Throwable $e ) {
249 $this->result = new WP_Error( '2', $e->getMessage() );
250 }
251
252 return $this->result;
253 }
254
255 /**
256 * @param array $args
257 *
258 * @return mixed
259 */
260 public function delete_section_item( $args = array() ) {
261 $section_id = $args['section_id'] ?? 0;
262 $item_id = $args['item_id'] ?? 0;
263
264 try {
265 // Instructor only remove item in my item.
266 if ( absint( get_post_field( 'post_author', $item_id ) ) !== absint( get_current_user_id() ) && ! current_user_can( 'administrator' ) ) {
267 throw new Exception( __( 'You can not delete this item!', 'learnpress' ) );
268 }
269
270 if ( ! ( $section_id && $item_id ) ) {
271 throw new Exception( __( 'Invalid params!', 'learnpress' ) );
272 }
273
274 $this->result = wp_trash_post( $item_id );
275 } catch ( Throwable $e ) {
276 $this->result = new WP_Error( '2', $e->getMessage() );
277 }
278
279 return $this->result;
280 }
281
282 /**
283 * @param array $args
284 *
285 * @return array|bool
286 */
287 public function new_section_item( $args = array() ) {
288 $section_id = $args['section_id'] ?? 0;
289 $item = $args['item'] ?? '';
290 $item = json_decode( wp_unslash( $item ), true );
291
292 if ( ! ( $section_id && $item ) ) {
293 return false;
294 }
295
296 // create new lesson, quiz and add to course
297 $this->result = $this->section_curd->new_item( $section_id, $item );
298
299 //$this->section_curd->update_final_item();
300
301 return true;
302 }
303
304 /**
305 * @param array $args
306 *
307 * @return mixed
308 */
309 public function update_section_items( $args = array() ) {
310 $section_id = $args['section_id'] ?? 0;
311 $items = $args['items'] ?? '';
312 $items = json_decode( wp_unslash( $items ), true );
313
314 if ( ! ( $section_id && $items ) ) {
315 return false;
316 }
317
318 $this->result = $this->section_curd->update_section_items( $section_id, $items );
319
320 //$this->section_curd->update_final_item();
321
322 return true;
323 }
324
325 /**
326 * @param array $args
327 *
328 * @return mixed
329 */
330 public function search_items( $args = array() ) {
331 $query = isset( $args['query'] ) ? $args['query'] : '';
332 $type = isset( $args['item_type'] ) ? $args['item_type'] : '';
333 $page = ! empty( $args['page'] ) ? $args['page'] : 1;
334 $exclude = ! empty( $args['exclude'] ) ? $args['exclude'] : '';
335
336 if ( $exclude ) {
337 $exclude = json_decode( $exclude, true );
338 }
339
340 $ids_exclude = array();
341
342 if ( is_array( $exclude ) ) {
343 foreach ( $exclude as $item ) {
344 $ids_exclude[] = $item['id'];
345 }
346 }
347
348 $search = new LP_Modal_Search_Items(
349 array(
350 'type' => $type,
351 'context' => 'course',
352 'context_id' => $this->course->get_id(),
353 'term' => $query,
354 'limit' => apply_filters( 'learn-press/course-editor/choose-items-limit', 10 ),
355 'paged' => $page,
356 'exclude' => $ids_exclude,
357 )
358 );
359
360 $id_items = $search->get_items();
361
362 $items = array();
363 foreach ( $id_items as $id ) {
364 $post = get_post( $id );
365
366 $items[] = array(
367 'id' => $post->ID,
368 'title' => $post->post_title,
369 'type' => $post->post_type,
370 );
371 }
372
373 $this->result = array(
374 'items' => $items,
375 'pagination' => $search->get_pagination( false ),
376 );
377
378 return true;
379 }
380
381 /**
382 * @param array $args
383 *
384 * @return mixed
385 */
386 public function add_items_to_section( $args = array() ) {
387 $section_id = ! empty( $args['section_id'] ) ? $args['section_id'] : false;
388 $items = ! empty( $args['items'] ) ? $args['items'] : false;
389 $items = json_decode( wp_unslash( $items ), true );
390
391 if ( ! $items || ! $section_id ) {
392 return false;
393 }
394
395 $this->result = $this->section_curd->add_items_section( $section_id, $items );
396
397 return true;
398 }
399 }
400