PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.7
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.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 / Services / SetupDemoCourseService.php

SetupDemoCourseService.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.4.7, at inc/Services/SetupDemoCourseService.php

207 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class SetupDemoCourseService
4 *
5 * Import the bundled Setup Wizard demo courses.
6 *
7 * @since 4.4.6
8 * @version 1.0.0
9 */
10
11 namespace LearnPress\Services;
12
13 use Exception;
14 use LearnPress\Helpers\Config;
15 use LearnPress\Models\CoursePostModel;
16 use LearnPress\Models\LessonPostModel;
17 use LearnPress\Models\PostModel;
18 use LP_Helper;
19 use Throwable;
20
21 defined( 'ABSPATH' ) || exit;
22
23 /**
24 * Class SetupDemoCourseService
25 *
26 * Load, validate, normalize, and import demo courses for the Setup Wizard.
27 *
28 * @since 4.4.7
29 * @version 1.0.0
30 */
31 class SetupDemoCourseService {
32 /**
33 * @var string
34 */
35 protected $json_data;
36
37 /**
38 * Initialize the demo course data from the Setup Wizard configuration.
39 */
40 public function __construct() {
41 $this->json_data = Config::instance()->get( 'demo-data-courses', 'setup-wizard' );
42 }
43
44 /**
45 * Read and validate all demo courses before importing.
46 *
47 * @return array Normalized demo courses.
48 * @throws Exception If the demo course data is invalid or empty.
49 */
50 public function load_courses(): array {
51 $data = LP_Helper::json_decode( $this->json_data, true );
52 if ( empty( $data['courses'] ) ) {
53 throw new Exception( __( 'Demo course data has not been added yet.', 'learnpress' ) );
54 }
55
56 $courses = array();
57 foreach ( $data['courses'] as $course ) {
58 $normalized = $this->normalize_course( $course );
59 $courses[] = $normalized;
60 }
61
62 return $courses;
63 }
64
65 /**
66 * Import one course and return progress for the client.
67 *
68 * @param int $index Zero-based index of the course to import.
69 *
70 * @return array Import progress and created course data.
71 * @throws Exception If the requested course does not exist or cannot be loaded.
72 * @throws Throwable
73 */
74 public function import_course( int $index ): array {
75 $courses = $this->load_courses();
76 $total = count( $courses );
77 if ( $index < 0 || $index >= $total ) {
78 throw new Exception( __( 'The requested demo course does not exist.', 'learnpress' ) );
79 }
80
81 $course = $courses[ $index ];
82 $course_id = $this->persist_course( $course );
83 $processed = $index + 1;
84
85 return array(
86 'index' => $processed,
87 'total' => $total,
88 'title' => $course['post_title'],
89 'percent' => (int) round( $processed / $total * 100 ),
90 'result' => 'created',
91 'course_id' => $course_id,
92 'complete' => $processed === $total,
93 'view_url' => admin_url( 'edit.php?post_type=lp_course' ),
94 );
95 }
96
97 /**
98 * Normalize one course from the JSON contract.
99 *
100 * @param mixed $course Raw course data.
101 *
102 * @return array Normalized course data.
103 * @throws Exception If the course or its lessons are invalid.
104 */
105 protected function normalize_course( $course ): array {
106 if ( ! is_array( $course ) ) {
107 throw new Exception( __( 'Each demo course must be an object.', 'learnpress' ) );
108 }
109
110 $title = sanitize_text_field( $course['title'] ?? '' );
111 if ( '' === $title ) {
112 throw new Exception( __( 'Every demo course requires a title.', 'learnpress' ) );
113 }
114
115 $section = $course['section'] ?? array();
116 if ( ! is_array( $section ) || empty( $section['title'] ) || ! isset( $section['lessons'] ) || ! is_array( $section['lessons'] ) ) {
117 throw new Exception( __( 'Every demo course requires one valid section.', 'learnpress' ) );
118 }
119
120 $lessons = array();
121 foreach ( $section['lessons'] as $lesson ) {
122 $lesson_title = is_array( $lesson ) ? sanitize_text_field( $lesson['title'] ?? '' ) : '';
123 if ( '' === $lesson_title ) {
124 throw new Exception( __( 'Every demo lesson requires a title.', 'learnpress' ) );
125 }
126
127 $lessons[] = array(
128 'post_title' => $lesson_title,
129 'post_content' => wp_kses_post( $lesson['content'] ?? '' ),
130 'meta_input' => array(
131 LessonPostModel::META_KEY_DURATION => sanitize_text_field( $lesson['duration'] ?? '' ),
132 ),
133 );
134 }
135
136 $status = sanitize_key( $course['status'] ?? 'publish' );
137 if ( ! in_array( $status, array( 'draft', 'publish' ), true ) ) {
138 $status = 'publish';
139 }
140
141 return array(
142 'post_title' => $title,
143 'post_content' => wp_kses_post( $course['content'] ?? '' ),
144 'post_excerpt' => sanitize_textarea_field( $course['excerpt'] ?? '' ),
145 'post_status' => $status,
146 'section' => array(
147 'title' => sanitize_text_field( $section['title'] ),
148 'description' => wp_kses_post( $section['description'] ?? '' ),
149 'lessons' => $lessons,
150 ),
151 );
152 }
153
154 /**
155 * Persist one normalized course.
156 *
157 * @param array $course Normalized course data.
158 *
159 * @return int Created course ID.
160 * @throws Throwable If the course, section, or lessons cannot be persisted.
161 */
162 protected function persist_course( array $course ): int {
163 $lesson_models = array();
164 $section_data = $course['section'];
165
166 unset( $course['section'] );
167
168 $course['post_type'] = LP_COURSE_CPT;
169 $course['post_author'] = get_current_user_id();
170 $course['meta_input'] = array(
171 CoursePostModel::META_KEY_SAMPLE_DATA => 'yes',
172 );
173
174 $courseModel = CourseService::instance()->create_info_main( $course );
175
176 $section = $courseModel->add_section(
177 array(
178 'section_name' => $section_data['title'],
179 'section_description' => $section_data['description'],
180 )
181 );
182
183 foreach ( $section_data['lessons'] as $lesson ) {
184 $lesson['post_status'] = PostModel::STATUS_PUBLISH;
185 $lesson['post_author'] = get_current_user_id();
186 $lesson['post_type'] = LP_LESSON_CPT;
187 $lesson['meta_input'][ CoursePostModel::META_KEY_SAMPLE_DATA ] = 'yes';
188
189 $lesson_model = new LessonPostModel( $lesson );
190 $lesson_model->meta_data = (object) $lesson['meta_input'];
191 $lesson_model->save();
192 $section->add_items(
193 array(
194 'items' => array(
195 array(
196 'id' => $lesson_model->get_id(),
197 'type' => LP_LESSON_CPT,
198 ),
199 ),
200 )
201 );
202 }
203
204 return $courseModel->get_id();
205 }
206 }
207