PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.6.9.1
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.6.9.1
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 / 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.1.6.9.1, at inc/admin/editor/class-lp-admin-editor-course.php

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