PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.3.7
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.3.7
4.4.9 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 All 140 releases
learnpress / inc / Ajax / CourseBuilder / CBEditCourseAjax.php

CBEditCourseAjax.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.3.7, at inc/Ajax/CourseBuilder/CBEditCourseAjax.php

296 lines 10.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * class CourseBuilderAjax
4 *
5 * @since 4.3
6 * @version 1.0.0
7 */
8
9 namespace LearnPress\Ajax\CourseBuilder;
10
11 use Exception;
12 use LearnPress\Ajax\AbstractAjax;
13 use LearnPress\CourseBuilder\CourseBuilder;
14 use LearnPress\Models\CourseModel;
15 use LearnPress\Models\CoursePostModel;
16 use LearnPress\Models\PostModel;
17 use LearnPress\Models\UserModel;
18 use LearnPress\Services\CourseService;
19 use LearnPress\TemplateHooks\CourseBuilder\Course\BuilderEditCourseTemplate;
20 use LearnPress\TemplateHooks\CourseBuilder\Course\BuilderListCoursesTemplate;
21 use LearnPress\TemplateHooks\CourseBuilder\CourseBuilderTemplate;
22 use LP_Course_CURD;
23 use LP_Datetime;
24 use LP_Helper;
25 use LP_REST_Response;
26 use stdClass;
27 use Throwable;
28 use WP_Post;
29
30 class CBEditCourseAjax extends AbstractAjax {
31 /**
32 * Check permissions and validate parameters.
33 *
34 * @throws Exception
35 *
36 * @since 4.3
37 * @version 1.0.0
38 */
39 public static function check_valid_course() {
40 $params = wp_unslash( $_POST['data'] ?? '' );
41 if ( empty( $params ) ) {
42 throw new Exception( 'Error: params invalid!' );
43 }
44
45 // Check permission
46 $userModel = UserModel::find( get_current_user_id(), true );
47 if ( ! $userModel || ! $userModel->is_instructor() ) {
48 throw new Exception( __( 'You are not allowed to edit courses', 'learnpress' ) );
49 }
50
51 $params = LP_Helper::json_decode( $params, true );
52 $course_id = $params['course_id'] ?? 0;
53 $courseModel = CourseModel::find( $course_id, true );
54 if ( $courseModel ) {
55 $params['courseModel'] = $courseModel;
56 }
57 $params['userModel'] = $userModel;
58
59 return $params;
60 }
61
62 /**
63 * Save Course.
64 *
65 * @since 4.3.6
66 * @version 1.0.0
67 */
68 public static function cb_save_course() {
69 $response = new LP_REST_Response();
70 $response->data = new stdClass();
71 $courseService = CourseService::instance();
72
73 try {
74 $data = self::check_valid_course();
75 /** @var CourseModel $courseModel */
76 $courseModel = $data['courseModel'] ?? null;
77 $userModel = $data['userModel'] ?? null;
78 $settings = $data['course_settings'] ?? false;
79 $is_edit = $courseModel instanceof CourseModel;
80 $course_title = trim( LP_Helper::sanitize_params_submitted( $data['course_title'] ?? '' ) );
81 $course_description = LP_Helper::sanitize_params_submitted( $data['course_description'] ?? '', 'html', false );
82 $course_status = LP_Helper::sanitize_params_submitted( $data['course_status'] ?? 'draft' );
83 $course_visibility = LP_Helper::sanitize_params_submitted( $data['course_visibility'] ?? '', 'key' );
84 $course_password = LP_Helper::sanitize_params_submitted( $data['course_password'] ?? '' );
85 $course_post_date = LP_Helper::sanitize_params_submitted( $data['course_post_date'] ?? '' );
86 $course_permalink = LP_Helper::sanitize_params_submitted( $data['course_permalink'] ?? '', 'sanitize_title' );
87 $course_thumbnail_id = LP_Helper::sanitize_params_submitted( $data['course_thumbnail_id'] ?? '', 'int' );
88 $course_categories_str = LP_Helper::sanitize_params_submitted( $data['course_categories'] ?? '' );
89 $course_categories = array_map( 'absint', explode( ',', $course_categories_str ) );
90 $course_tags_str = LP_Helper::sanitize_params_submitted( $data['course_tags'] ?? '' );
91 $course_tags = array_map( 'absint', explode( ',', $course_tags_str ) );
92
93 if ( ! $userModel instanceof UserModel ) {
94 throw new Exception( __( 'Invalid user.', 'learnpress' ) );
95 }
96
97 if ( empty( $course_title ) ) {
98 throw new Exception( __( 'Course title is required.', 'learnpress' ) );
99 }
100
101 $status_allows = [
102 PostModel::STATUS_PUBLISH,
103 PostModel::STATUS_PENDING,
104 PostModel::STATUS_DRAFT,
105 PostModel::STATUS_PRIVATE,
106 PostModel::STATUS_TRASH,
107 PostModel::STATUS_FEATURE,
108 ];
109 if ( ! in_array( $course_status, $status_allows ) ) {
110 throw new Exception( __( 'Invalid course status.', 'learnpress' ) );
111 }
112
113 $lp_date_modified = new LP_Datetime( current_time( 'mysql' ) );
114
115 $data_save = [
116 'post_title' => $course_title,
117 'post_content' => $course_description,
118 'post_status' => $course_status,
119 'post_author' => $userModel->get_id(),
120 'post_modified' => $lp_date_modified->format( LP_DateTime::$format ),
121 'post_modified_gmt' => $lp_date_modified->to_gmt_string( $lp_date_modified ),
122 ];
123
124 // Handle status
125 if ( $course_visibility === PostModel::VISIBILITY_PASSWORD ) {
126 if ( ! empty( $course_password ) ) {
127 $data_save['post_password'] = $course_password;
128 }
129 }
130
131 // Handle date
132 if ( ! empty( $course_post_date ) ) {
133 // Time by local
134 $lp_date = new LP_Datetime( $course_post_date );
135 $lp_date_timestamp = $lp_date->getTimestamp();
136 $lp_date_now = new LP_Datetime( current_time( 'mysql', 1 ) );
137 $lp_date_now_timestamp = $lp_date_now->getTimestamp();
138
139 // Check with time now and status
140 if ( $lp_date_timestamp > $lp_date_now_timestamp && $course_status === PostModel::STATUS_PUBLISH ) {
141 $data_save['post_status'] = PostModel::STATUS_FEATURE;
142 } elseif ( $lp_date_timestamp <= $lp_date_now_timestamp && $course_status === PostModel::STATUS_FEATURE ) {
143 $data_save['post_status'] = PostModel::STATUS_PUBLISH;
144 }
145
146 $data_save['post_date'] = $lp_date->format( LP_DateTime::$format );
147 $data_save['post_date_gmt'] = $lp_date->to_gmt_string( $lp_date );
148 }
149
150 // Check visibility
151 if ( $course_visibility === PostModel::STATUS_PRIVATE ) {
152 $data_save['post_status'] = PostModel::STATUS_PRIVATE;
153 }
154
155 if ( ! $is_edit ) {
156 // Create new course
157 $coursePostModelNew = $courseService->create_info_main( $data_save );
158 $courseModel = new CourseModel( $coursePostModelNew );
159 $course_id = $courseModel->ID;
160 } else {
161 // Update course
162 $coursePostModel = new CoursePostModel( $courseModel );
163 foreach ( $data_save as $key => $value ) {
164 if ( isset( $coursePostModel->{$key} ) ) {
165 $coursePostModel->{$key} = $value;
166 }
167 }
168
169 // Update permalink/slug if provided
170 if ( ! empty( $course_permalink ) ) {
171 $coursePostModel->post_name = $course_permalink;
172 }
173
174 if ( $settings ) {
175 $courseBuilderAjax = new CourseBuilderAjax();
176 $courseBuilderAjax->save_course_settings_to_model( $coursePostModel, $data );
177 }
178
179 $coursePostModel->save();
180
181 $course_id = $courseModel->ID;
182
183 $courseModel = CourseModel::find( $course_id, true );
184 }
185
186 // Set categories and tags
187 $courseService->update_categories( $course_id, $course_categories );
188 $courseService->update_tags( $course_id, $course_tags );
189
190 // Save or remove thumbnail
191 if ( isset( $data['course_thumbnail_id'] ) ) {
192 $post = new WP_Post( $courseModel );
193 if ( $course_thumbnail_id > 0 ) {
194 set_post_thumbnail( $post, $course_thumbnail_id );
195 } else {
196 delete_post_thumbnail( $post );
197 }
198 }
199
200 ob_start();
201 $data_edit_course_html = [
202 'userModel' => $userModel,
203 'item_id' => $course_id,
204 ];
205 BuilderEditCourseTemplate::instance()->layout( $data_edit_course_html );
206 $html_edit_course = ob_get_clean();
207
208 $response->status = 'success';
209 $response->message = ! $is_edit ?
210 __( 'Create course successfully!', 'learnpress' ) :
211 __( 'Update course successfully!', 'learnpress' );
212 $response->data->html = $html_edit_course;
213
214 // Return redirect detail if new course
215 if ( ! $is_edit && $course_id ) {
216 $response->data->redirect_url = CourseBuilder::get_link_course_builder(
217 CourseBuilderTemplate::MENU_COURSES . "/{$course_id}"
218 );
219 }
220 } catch ( Throwable $th ) {
221 $response->message = $th->getMessage();
222 }
223
224 wp_send_json( $response );
225 }
226
227 /**
228 * Quick edit course.
229 *
230 * @return void
231 */
232 public static function cb_quick_edit_save_course() {
233 $response = new LP_REST_Response();
234 $response->data = new stdClass();
235 $courseService = CourseService::instance();
236
237 try {
238 $data = self::check_valid_course();
239 /** @var CourseModel $courseModel */
240 $courseModel = $data['courseModel'] ?? null;
241 $userModel = $data['userModel'] ?? null;
242 $action_type = $data['action_type'] ?? null;
243 if ( ! $action_type || ! $courseModel instanceof CourseModel ) {
244 throw new Exception( __( 'Invalid request.', 'learnpress' ) );
245 }
246
247 $coursePostModel = new CoursePostModel( $courseModel );
248
249 switch ( $action_type ) {
250 case 'duplicate':
251 //$courseService->duplicate( $courseModel );
252 // Use old class temporarily
253 include_once LP_PLUGIN_PATH . 'inc/admin/class-lp-admin.php';
254 $course_curd = new LP_Course_CURD();
255 $course_id = $courseModel->get_id();
256 $course_id_new = $course_curd->duplicate( $course_id );
257 $response->data->redirect_url = CourseBuilder::get_link_course_builder(
258 CourseBuilderTemplate::MENU_COURSES . "/{$course_id_new}"
259 );
260 $response->message = __( 'Duplicate course successfully. Redirecting...!', 'learnpress' );
261 break;
262 case PostModel::STATUS_TRASH:
263 case PostModel::STATUS_PUBLISH:
264 case PostModel::STATUS_PENDING:
265 case PostModel::STATUS_DRAFT:
266 $coursePostModel->post_status = $action_type;
267 $coursePostModel->save();
268 $courseModel = CourseModel::find( $coursePostModel->get_id(), true );
269 $response->message = __( 'Update course status successfully!', 'learnpress' );
270 $response->data->html = BuilderListCoursesTemplate::render_course( $courseModel );
271 break;
272 case 'restore':
273 $coursePostModel->post_status = PostModel::STATUS_DRAFT;
274 $coursePostModel->save();
275 $courseModel = CourseModel::find( $coursePostModel->get_id(), true );
276 $response->message = __( 'Restore course successfully!', 'learnpress' );
277 $response->data->html = BuilderListCoursesTemplate::render_course( $courseModel );
278 break;
279 case 'delete':
280 if ( $courseModel->get_status() !== PostModel::STATUS_TRASH ) {
281 throw new Exception( __( 'Course must be trashed before deleting.', 'learnpress' ) );
282 }
283 $coursePostModel->delete();
284 $response->message = __( 'Delete course successfully!', 'learnpress' );
285 break;
286 }
287
288 $response->status = 'success';
289 } catch ( Throwable $e ) {
290 $response->message = $e->getMessage();
291 }
292
293 wp_send_json( $response );
294 }
295 }
296