PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.7
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.7
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 / 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, at inc/admin/class-lp-install-sample-data.php

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