PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.7.3.1
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.7.3.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 / class-lp-install-sample-data.php

class-lp-install-sample-data.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.1.7.3.1, at inc/admin/class-lp-install-sample-data.php

737 lines 17.1 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_Install_Sample_Data
5 *
6 * Create sample course for testing purpose.
7 * This will create sections, items, questions, answers, ...
8 *
9 * @since 3.0.0
10 */
11 class LP_Install_Sample_Data {
12
13 /**
14 * @var array
15 */
16 public static $section_range = array( 5, 10 );
17
18 /**
19 * @var array
20 */
21 public static $item_range = array( 10, 15 );
22
23 /**
24 * @var array
25 */
26 public static $question_range = array( 10, 15 );
27
28 /**
29 * @var array
30 */
31 public static $answer_range = array( 3, 5 );
32
33 /**
34 * @var int
35 */
36 public static $max_content_paragraph = 5;
37
38 /**
39 * @var string
40 */
41 protected $dummy_text = '';
42
43 /**
44 * LP_Install_Sample_Data constructor.
45 */
46 public function __construct() {
47 add_filter( 'learn-press/script-data', array( $this, 'i18n' ), 10, 2 );
48
49 $actions = array(
50 'lp-install-sample-data',
51 'lp-uninstall-sample-data',
52 );
53
54 if ( ! in_array( LP_Request::get( 'page' ), $actions ) ) {
55 return;
56 }
57
58 add_action( 'init', array( $this, 'install' ) );
59 add_action( 'init', array( $this, 'uninstall' ) );
60 }
61
62 public function i18n( $data, $handle ) {
63 if ( 'learn-press-global' !== $handle ) {
64 return $data;
65 }
66
67 $i18n = array(
68 'confirm_install_sample_data' => esc_html__( 'Are you sure you want to install the sample course data?', 'learnpress' ),
69 'confirm_uninstall_sample_data' => esc_html__( 'Are you sure you want to delete the sample course data?', 'learnpress' ),
70 );
71
72 if ( empty( $data['i18n'] ) ) {
73 $data['i18n'] = $i18n;
74 } else {
75 $data['i18n'] = array_merge( $data['i18n'], $i18n );
76 }
77
78 return $data;
79 }
80
81 /**
82 * Install
83 *
84 * @TODO - Need write to api, not hook init.
85 */
86 public function install() {
87 if ( ! wp_verify_nonce( LP_Request::get_string( '_wpnonce' ), 'install-sample-course' ) ) {
88 return;
89 }
90
91 $dummy_text = LP_WP_Filesystem::instance()->file_get_contents( LP_PLUGIN_PATH . '/dummy-data/dummy-text.txt' );
92 $this->dummy_text = preg_split( '!\s!', $dummy_text );
93
94 $section_range = LP_Request::get_param( 'section-range', 0, 'int' );
95 if ( $section_range ) {
96 self::$section_range = $section_range;
97 }
98
99 $item_range = LP_Request::get_param( 'item-range', 0, 'int' );
100 if ( $item_range ) {
101 self::$item_range = $item_range;
102 }
103
104 $question_range = LP_Request::get_param( 'question-range', 0, 'int' );
105 if ( $question_range ) {
106 self::$question_range = $question_range;
107 }
108
109 $answer_range = LP_Request::get_param( 'answer-range', 0, 'int' );
110 if ( $answer_range ) {
111 self::$answer_range = $answer_range;
112 }
113
114 try {
115 @ini_set( 'memory_limit', '2G' );
116
117 global $wp_filter;
118
119 $keys = array_keys( $wp_filter );
120 $ignore_keys = array( 'sanitize_title' );
121
122 foreach ( $keys as $key ) {
123 if ( in_array( $key, $ignore_keys ) ) {
124 continue;
125 }
126
127 unset( $wp_filter[ $key ] );
128 }
129
130 $name = LP_Request::get_param( 'custom-name' );
131 $course_id = $this->create_course( $name );
132
133 if ( ! $course_id ) {
134 throw new Exception( 'Create course failed.' );
135 }
136
137 $this->create_sections( $course_id );
138
139 $price = LP_Request::get_param( 'course-price', 0, 'float' );
140 if ( $price ) {
141 update_post_meta( $course_id, '_lp_regular_price', $price );
142 }
143
144 /**
145 * Save info course in background.
146 */
147 $bg = LP_Background_Single_Course::instance();
148 $bg->data(
149 array(
150 'handle_name' => 'save_post',
151 'course_id' => $course_id,
152 'data' => [ 'data_sample' => 1 ],
153 )
154 )->dispatch();
155 ?>
156
157 <div class="lp-install-sample__response success">
158 <?php printf( __( 'The Course "%s" has been created', 'learnpress' ), get_the_title( $course_id ) ); ?>
159 <a href="<?php echo esc_url_raw( get_the_permalink( $course_id ) ); ?>" target="_blank"><?php esc_html_e( 'View', 'learnpress' ); ?></a>
160 |
161 <a href="<?php echo esc_url_raw( admin_url( 'post.php?post=' . $course_id . '&action=edit' ) ); ?>" target="_blank"><?php esc_html_e( 'Edit', 'learnpress' ); ?></a>
162 </div>
163
164 <?php
165 } catch ( Exception $ex ) {
166 echo '<div class="lp-install-sample__response fail">';
167 echo wp_kses_post( $ex->getMessage() );
168 echo '</div>';
169 }
170
171 die();
172 }
173
174 /**
175 * Un-install
176 */
177 public function uninstall() {
178 if ( ! wp_verify_nonce( LP_Request::get_string( '_wpnonce' ), 'uninstall-sample-course' ) ) {
179 return;
180 }
181
182 global $wpdb;
183
184 $posts = $this->get_sample_posts();
185 try {
186 if ( ! $posts ) {
187 throw new Exception( esc_html__( 'No data sample.', 'learnpress' ) );
188 }
189
190 foreach ( $posts as $post ) {
191 switch ( $post->post_type ) {
192 case LP_COURSE_CPT:
193 $this->_delete_course( $post->ID );
194 break;
195 case LP_QUIZ_CPT:
196 $this->_delete_quiz( $post->ID );
197 break;
198 case LP_QUESTION_CPT:
199 $this->_delete_question( $post->ID );
200 break;
201 }
202
203 $this->_delete_post( $post->ID );
204 }
205 ?>
206
207 <div class="lp-install-sample__response success">
208 <?php esc_html_e( 'The sample data was successfully deleted!', 'learnpress' ); ?>
209 </div>
210
211 <?php
212 } catch ( Exception $ex ) {
213
214 echo '<div class="lp-install-sample__response fail">';
215 echo 'Error: ' . $ex->getMessage();
216 echo '</div>';
217 }
218
219 die();
220 }
221
222 /**
223 * Get all posts marked as "sample data"
224 *
225 * @return array|null|object
226 */
227 public function get_sample_posts() {
228 global $wpdb;
229
230 $query = $wpdb->prepare(
231 "
232 SELECT p.ID, post_type
233 FROM {$wpdb->posts} p
234 INNER JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id AND pm.meta_key = %s AND pm.meta_value = %s
235 ",
236 '_lp_sample_data',
237 'yes'
238 );
239
240 return $wpdb->get_results( $query );
241 }
242
243 protected function _delete_course( $id ) {
244 global $wpdb;
245 $query = $wpdb->prepare(
246 "
247 SELECT section_id
248 FROM {$wpdb->learnpress_sections}
249 WHERE section_course_id = %d
250 ",
251 $id
252 );
253
254 $section_ids = $wpdb->get_col( $query );
255 if ( $section_ids ) {
256 $wpdb->query( "DELETE FROM {$wpdb->learnpress_section_items} WHERE section_id IN(" . join( ',', $section_ids ) . ')' );
257 $wpdb->query( "DELETE FROM {$wpdb->learnpress_sections} WHERE section_id IN(" . join( ',', $section_ids ) . ')' );
258 }
259 }
260
261 protected function _delete_quiz( $id ) {
262 global $wpdb;
263 $wpdb->query( "DELETE FROM {$wpdb->learnpress_quiz_questions} WHERE quiz_id = {$id}" );
264 }
265
266 protected function _delete_question( $id ) {
267 global $wpdb;
268 $wpdb->query( "DELETE FROM {$wpdb->learnpress_question_answers} WHERE question_id = {$id}" );
269 }
270
271 protected function _delete_post( $post_id ) {
272 global $wpdb;
273 $wpdb->query( "DELETE FROM {$wpdb->postmeta} WHERE post_id = $post_id" );
274 $wpdb->query( "DELETE FROM {$wpdb->posts} WHERE ID = $post_id" );
275 }
276
277 protected function _delete_user_items( $ids ) {
278 global $wpdb;
279 $format = array_fill( 0, sizeof( $ids ), '%d' );
280 $query = $wpdb->prepare(
281 "
282 DELETE
283 FROM {$wpdb->learnpress_user_items}
284 WHERE item_id IN(" . join( ',', $format ) . ')
285 ',
286 $ids
287 );
288
289 $wpdb->query( $query );
290 }
291
292 /**
293 * Generate content with 'lorem' text.
294 *
295 * @param int $min
296 * @param int $max
297 * @param int $paragraphs
298 *
299 * @return string
300 */
301 protected function generate_content( $min = 100, $max = 500, $paragraphs = 10 ) {
302 $length = rand( $min, $max );
303 $max = sizeof( $this->dummy_text ) - 1;
304 $words = array();
305
306 for ( $i = 0; $i < $length; $i ++ ) {
307 $words[] = $this->dummy_text[ rand( 0, $max ) ];
308 }
309
310 $p = array();
311
312 if ( ! $paragraphs ) {
313 $paragraphs = self::$max_content_paragraph;
314 }
315
316 while ( $words && sizeof( $p ) < $paragraphs ) {
317 $len = rand( 10, 20 );
318 $cut = array_splice( $words, 0, $len );
319 $p[] = '<p>' . ucfirst( join( ' ', $cut ) ) . '</p>';
320 }
321
322 return join( '', $p );
323 }
324
325 /**
326 * Generate title with 'lorem' text.
327 *
328 * @param int $min
329 * @param int $max
330 *
331 * @return string
332 */
333 protected function generate_title( $min = 10, $max = 15 ) {
334 $length = rand( $min, $max );
335 $max = sizeof( $this->dummy_text ) - 1;
336 $words = array();
337 for ( $i = 0; $i < $length; $i ++ ) {
338 $words[] = $this->dummy_text[ rand( 0, $max ) ];
339 }
340
341 return ucfirst( join( ' ', $words ) );
342 }
343
344 /**
345 * Create course.
346 *
347 * @param string $name
348 *
349 * @return int|WP_Error
350 */
351 protected function create_course( $name = '' ) {
352
353 $data = array(
354 'post_title' => strlen( $name ) ? $name : __( 'Sample course', 'learnpress' ),
355 'post_type' => LP_COURSE_CPT,
356 'post_status' => 'publish',
357 'post_content' => $this->generate_content( 25, 40, 5 ),
358 );
359
360 $course_id = wp_insert_post( $data );
361
362 if ( $course_id ) {
363 $metas = array(
364 '_lp_duration' => '10 week',
365 '_lp_max_students' => '1000',
366 '_lp_students' => '0',
367 '_lp_retake_count' => '0',
368 '_lp_featured' => 'no',
369 '_lp_has_finish' => 'yes',
370 '_lp_course_result' => 'evaluate_lesson',
371 '_lp_passing_condition' => '80',
372 '_lp_sample_data' => 'yes',
373 );
374 foreach ( $metas as $key => $value ) {
375 update_post_meta( $course_id, $key, $value );
376 }
377
378 $this->add_extra_info( $course_id );
379 }
380
381 return $course_id;
382 }
383
384 protected function add_extra_info( $course_id ) {
385 $features = array( 'requirements', 'target_audiences', 'key_features' );
386
387 // Requirements, Target audiences, Key Features
388 foreach ( $features as $feature ) {
389 $feature_data = array();
390 for ( $i = 0, $n = rand( 5, 10 ); $i <= $n; $i ++ ) {
391 $feature_data[] = $this->generate_title();
392 }
393 update_post_meta( $course_id, '_lp_' . $feature, $feature_data );
394 }
395
396 // FAQs
397 $feature_data = array();
398 for ( $i = 0, $n = rand( 5, 10 ); $i <= $n; $i ++ ) {
399 $feature_data[] = array( $this->generate_title() . '?', $this->generate_content( 20, 30, 3 ) );
400 }
401 update_post_meta( $course_id, '_lp_faqs', $feature_data );
402
403 // Featured review
404 update_post_meta( $course_id, '_lp_featured_review', $this->generate_title( 30, 40 ) );
405 }
406
407 /**
408 * Create sections.
409 *
410 * @param int $course_id
411 */
412 protected function create_sections( $course_id ) {
413 $section_length = call_user_func_array( 'rand', ( self::$section_range ) );
414
415 for ( $i = 1; $i <= $section_length; $i ++ ) {
416 $section_id = $this->create_section( 'Section ' . $i, $course_id );
417
418 if ( $section_id ) {
419 $this->create_section_items( $section_id, $course_id );
420 }
421 }
422 }
423
424 /**
425 * Create section.
426 *
427 * @param string $name
428 * @param int $course_id
429 *
430 * @return int
431 */
432 protected function create_section( $name, $course_id ) {
433 static $order = 0;
434
435 global $wpdb;
436
437 $data = array(
438 'section_name' => $name,
439 'section_course_id' => $course_id,
440 'section_order' => $order,
441 'section_description' => $this->generate_title(),
442 );
443
444 $wpdb->insert(
445 $wpdb->learnpress_sections,
446 $data,
447 array( '%s', '%d', '%d', '%s' )
448 );
449
450 if ( $wpdb->insert_id ) {
451 $order ++;
452
453 return $wpdb->insert_id;
454 }
455
456 return 0;
457 }
458
459 /**
460 * Create section items.
461 *
462 * @param int $section_id
463 * @param int $course_id
464 */
465 protected function create_section_items( $section_id, $course_id ) {
466
467 static $lesson_count = 1;
468 static $quiz_count = 1;
469
470 $order = 0;
471
472 $item_length = call_user_func_array( 'rand', self::$item_range );
473
474 for ( $i = 1; $i < $item_length; $i ++ ) {
475 $lesson_id = $this->create_lesson( 'Lesson ' . $lesson_count ++, $section_id, $course_id, $order );
476
477 if ( $lesson_id ) {
478 $order ++;
479
480 if ( $i == 1 ) {
481 update_post_meta( $lesson_id, '_lp_preview', 'yes' );
482 }
483 }
484 }
485
486 $quiz_id = $this->create_quiz( 'Quiz ' . $quiz_count ++, $section_id, $course_id, $order );
487
488 if ( $quiz_id ) {
489 $order ++;
490 }
491 }
492
493 /**
494 * Create lesson.
495 *
496 * @param string $name
497 * @param int $section_id
498 * @param int $course_id
499 *
500 * @return int|WP_Error
501 */
502 protected function create_lesson( $name, $section_id, $course_id, $order ) {
503 global $wpdb;
504
505 $data = array(
506 'post_title' => $name,
507 'post_type' => LP_LESSON_CPT,
508 'post_status' => 'publish',
509 'post_content' => $this->generate_content(),
510 );
511
512 $lesson_id = wp_insert_post( $data );
513
514 if ( $lesson_id ) {
515
516 update_post_meta( $lesson_id, '_lp_sample_data', 'yes' );
517
518 $section_data = array(
519 'section_id' => $section_id,
520 'item_id' => $lesson_id,
521 'item_type' => LP_LESSON_CPT,
522 'item_order' => absint( $order ),
523 );
524
525 $wpdb->insert(
526 $wpdb->learnpress_section_items,
527 $section_data,
528 array( '%d', '%d', '%s', '%d' )
529 );
530 }
531
532 return $lesson_id;
533 }
534
535 /**
536 * Create quiz.
537 *
538 * @param string $name
539 * @param int $section_id
540 * @param int $course_id
541 *
542 * @return int|WP_Error
543 */
544 protected function create_quiz( $name, $section_id, $course_id, $order ) {
545 global $wpdb;
546
547 $data = array(
548 'post_title' => $name,
549 'post_type' => LP_QUIZ_CPT,
550 'post_status' => 'publish',
551 'post_content' => $this->generate_content( 25, 40, 2 ),
552 );
553
554 $quiz_id = wp_insert_post( $data );
555
556 if ( $quiz_id ) {
557
558 $metas = array(
559 '_lp_preview' => 'no',
560 // '_lp_minus_points' => 0,
561 // '_lp_show_hide_question' => 'no',
562 // '_lp_review_questions' => 'yes',
563 // '_lp_show_result' => 'yes',
564 '_lp_duration' => ( rand( 1, 5 ) * 10 ) . ' ' . 'minute',
565 '_lp_passing_grade' => rand( 5, 9 ) * 10,
566 // '_lp_retake_count' => rand( 0, 10 ),
567 // '_lp_archive_history' => 'no',
568 // '_lp_show_check_answer' => '0',
569 // '_lp_show_hint' => '0',
570 '_lp_sample_data' => 'yes',
571 '_lp_negative_marking' => 'no',
572 '_lp_minus_skip_questions' => 'no',
573 '_lp_instant_check' => 'no',
574 '_lp_retake_count' => '0',
575 '_lp_pagination' => '1',
576 '_lp_review' => 'yes',
577 '_lp_show_correct_review' => 'yes',
578 );
579
580 foreach ( $metas as $key => $value ) {
581 update_post_meta( $quiz_id, $key, $value );
582 }
583
584 $section_data = array(
585 'section_id' => $section_id,
586 'item_id' => $quiz_id,
587 'item_type' => LP_QUIZ_CPT,
588 'item_order' => absint( $order ),
589 );
590
591 $wpdb->insert(
592 $wpdb->learnpress_section_items,
593 $section_data,
594 array( '%d', '%d', '%s', '%d' )
595 );
596
597 $this->create_quiz_questions( $quiz_id );
598 }
599
600 return $quiz_id;
601 }
602
603 /**
604 * Create questions of a quiz.
605 *
606 * @param int $quiz_id
607 */
608 protected function create_quiz_questions( $quiz_id ) {
609 static $question_index = 1;
610 global $wpdb;
611
612 $question_count = call_user_func_array( 'rand', self::$question_range );
613 for ( $i = 1; $i <= $question_count; $i ++ ) {
614 $data = array(
615 'post_title' => 'Question ' . $question_index ++,
616 'post_type' => LP_QUESTION_CPT,
617 'post_status' => 'publish',
618 'post_content' => $this->generate_content( 25, 40, 2 ),
619 );
620
621 $question_id = wp_insert_post( $data );
622
623 if ( ! $question_id ) {
624 continue;
625 }
626
627 $type = $this->get_question_type();
628
629 update_post_meta( $question_id, '_lp_type', $type );
630 update_post_meta( $question_id, '_lp_sample_data', 'yes' );
631
632 $quiz_question_data = array(
633 'quiz_id' => $quiz_id,
634 'question_id' => $question_id,
635 );
636
637 $wpdb->insert(
638 $wpdb->learnpress_quiz_questions,
639 $quiz_question_data,
640 array( '%d', '%d' )
641 );
642
643 if ( $wpdb->insert_id ) {
644 $this->create_question_answers( $question_id, $type );
645 } else {
646 error_log( 'create_quiz_questions => ', $wpdb->last_error );
647 }
648 }
649 }
650
651 /**
652 * Create answers for a question.
653 *
654 * @param int $question_id
655 * @param string $type
656 */
657 protected function create_question_answers( $question_id, $type ) {
658 global $wpdb;
659
660 $answers = $this->get_answers( $type );
661 foreach ( $answers as $order => $answer ) {
662 $data = array(
663 'question_id' => $question_id,
664 'title' => $answer['title'],
665 'value' => $answer['value'],
666 'is_true' => $answer['is_true'],
667 'order' => $order + 1,
668 );
669
670 $wpdb->insert(
671 $wpdb->learnpress_question_answers,
672 $data,
673 array( '%d', '%s', '%s', '%s', '%d' )
674 );
675 }
676 }
677
678 /**
679 * Get random answers by type of question.
680 *
681 * @param string $type
682 *
683 * @return array
684 */
685 protected function get_answers( $type ) {
686 $answers = array();
687
688 $option_count = $type === 'true_or_false' ? 2 : call_user_func_array( 'rand', self::$answer_range );
689
690 for ( $i = 1; $i <= $option_count; $i ++ ) {
691 $answers[] = array(
692 'title' => $this->generate_title(),
693 'value' => learn_press_random_value(),
694 'is_true' => 'no',
695 );
696 }
697
698 // Set option is TRUE randomize
699 if ( $type !== 'multi_choice' ) {
700 $at = rand( 0, sizeof( $answers ) - 1 );
701 $answers[ $at ]['is_true'] = 'yes';
702 $answers[ $at ]['title'] = _x( '[TRUE] - ', 'install-sample-course', 'learnpress' ) . $answers[ $at ]['title'];
703 } else {
704 $has_true_option = false;
705 while ( ! $has_true_option ) {
706 foreach ( $answers as $k => $v ) {
707 $answers[ $k ]['is_true'] = rand( 0, 100 ) % 2 ? 'yes' : 'no';
708
709 if ( $answers[ $k ]['is_true'] === 'yes' ) {
710 $answers[ $k ]['title'] = _x( ' [TRUE] - ', 'install-sample-course', 'learnpress' ) . $answers[ $k ]['title'];
711 $has_true_option = true;
712 }
713 }
714 }
715 }
716
717 return $answers;
718 }
719
720 /**
721 * Get random type for a question.
722 *
723 * @return string
724 */
725 protected function get_question_type() {
726 $types = array(
727 'true_or_false',
728 'single_choice',
729 'multi_choice',
730 );
731
732 return $types[ rand( 0, sizeof( $types ) - 1 ) ];
733 }
734 }
735
736 new LP_Install_Sample_Data();
737