| 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 |
$coursePostModel = CoursePostModel::find( $course_id, true ); |
| 201 |
// Save to clear cache |
| 202 |
$coursePostModel->save(); |
| 203 |
|
| 204 |
ob_start(); |
| 205 |
$data_edit_course_html = [ |
| 206 |
'userModel' => $userModel, |
| 207 |
'item_id' => $course_id, |
| 208 |
]; |
| 209 |
BuilderEditCourseTemplate::instance()->layout( $data_edit_course_html ); |
| 210 |
$html_edit_course = ob_get_clean(); |
| 211 |
|
| 212 |
$response->status = 'success'; |
| 213 |
$response->message = ! $is_edit ? |
| 214 |
__( 'Create course successfully!', 'learnpress' ) : |
| 215 |
__( 'Update course successfully!', 'learnpress' ); |
| 216 |
$response->data->html = $html_edit_course; |
| 217 |
|
| 218 |
// Return redirect detail if new course |
| 219 |
if ( ! $is_edit && $course_id ) { |
| 220 |
$response->data->redirect_url = CourseBuilder::get_link_course_builder( |
| 221 |
CourseBuilderTemplate::MENU_COURSES . "/{$course_id}" |
| 222 |
); |
| 223 |
} |
| 224 |
} catch ( Throwable $th ) { |
| 225 |
$response->message = $th->getMessage(); |
| 226 |
} |
| 227 |
|
| 228 |
wp_send_json( $response ); |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Quick edit course. |
| 233 |
* |
| 234 |
* @return void |
| 235 |
*/ |
| 236 |
public static function cb_quick_edit_save_course() { |
| 237 |
$response = new LP_REST_Response(); |
| 238 |
$response->data = new stdClass(); |
| 239 |
$courseService = CourseService::instance(); |
| 240 |
|
| 241 |
try { |
| 242 |
$data = self::check_valid_course(); |
| 243 |
/** @var CourseModel $courseModel */ |
| 244 |
$courseModel = $data['courseModel'] ?? null; |
| 245 |
$userModel = $data['userModel'] ?? null; |
| 246 |
$action_type = $data['action_type'] ?? null; |
| 247 |
if ( ! $action_type || ! $courseModel instanceof CourseModel ) { |
| 248 |
throw new Exception( __( 'Invalid request.', 'learnpress' ) ); |
| 249 |
} |
| 250 |
|
| 251 |
$coursePostModel = new CoursePostModel( $courseModel ); |
| 252 |
|
| 253 |
switch ( $action_type ) { |
| 254 |
case 'duplicate': |
| 255 |
//$courseService->duplicate( $courseModel ); |
| 256 |
// Use old class temporarily |
| 257 |
include_once LP_PLUGIN_PATH . 'inc/admin/class-lp-admin.php'; |
| 258 |
$course_curd = new LP_Course_CURD(); |
| 259 |
$course_id = $courseModel->get_id(); |
| 260 |
$course_id_new = $course_curd->duplicate( $course_id ); |
| 261 |
$response->data->redirect_url = CourseBuilder::get_link_course_builder( |
| 262 |
CourseBuilderTemplate::MENU_COURSES . "/{$course_id_new}" |
| 263 |
); |
| 264 |
$response->message = __( 'Duplicate course successfully. Redirecting...!', 'learnpress' ); |
| 265 |
break; |
| 266 |
case PostModel::STATUS_TRASH: |
| 267 |
case PostModel::STATUS_PUBLISH: |
| 268 |
case PostModel::STATUS_PENDING: |
| 269 |
case PostModel::STATUS_DRAFT: |
| 270 |
$coursePostModel->post_status = $action_type; |
| 271 |
$coursePostModel->save(); |
| 272 |
$courseModel = CourseModel::find( $coursePostModel->get_id(), true ); |
| 273 |
$response->message = __( 'Update course status successfully!', 'learnpress' ); |
| 274 |
$response->data->html = BuilderListCoursesTemplate::render_course( $courseModel ); |
| 275 |
break; |
| 276 |
case 'restore': |
| 277 |
$coursePostModel->post_status = PostModel::STATUS_DRAFT; |
| 278 |
$coursePostModel->save(); |
| 279 |
$courseModel = CourseModel::find( $coursePostModel->get_id(), true ); |
| 280 |
$response->message = __( 'Restore course successfully!', 'learnpress' ); |
| 281 |
$response->data->html = BuilderListCoursesTemplate::render_course( $courseModel ); |
| 282 |
break; |
| 283 |
case 'delete': |
| 284 |
if ( $courseModel->get_status() !== PostModel::STATUS_TRASH ) { |
| 285 |
throw new Exception( __( 'Course must be trashed before deleting.', 'learnpress' ) ); |
| 286 |
} |
| 287 |
$coursePostModel->delete(); |
| 288 |
$response->message = __( 'Delete course successfully!', 'learnpress' ); |
| 289 |
break; |
| 290 |
} |
| 291 |
|
| 292 |
$response->status = 'success'; |
| 293 |
} catch ( Throwable $e ) { |
| 294 |
$response->message = $e->getMessage(); |
| 295 |
} |
| 296 |
|
| 297 |
wp_send_json( $response ); |
| 298 |
} |
| 299 |
} |
| 300 |
|