| @@ -1110,8 +1110,10 @@ | ||
| 1110 | 1110 | |
| 1111 | 1111 | try { |
| 1112 | 1112 | $data = self::check_valid_lesson(); |
| 1113 | 1113 | $lesson_id = $data['lesson_id'] ?? 0; |
| 1114 | + $has_title = array_key_exists( 'lesson_title', $data ); | |
| 1115 | + $has_content = array_key_exists( 'lesson_description', $data ); | |
| 1114 | 1116 | $title = LP_Helper::sanitize_params_submitted( $data['lesson_title'] ?? '' ); |
| 1115 | 1117 | $description = LP_Helper::sanitize_params_submitted( |
| 1116 | 1118 | $data['lesson_description'] ?? '', |
| 1117 | 1119 | 'html' |
| @@ -1132,9 +1134,9 @@ | ||
| 1132 | 1134 | 'key' |
| 1133 | 1135 | ); |
| 1134 | 1136 | $restore_with_custom_slug = false; |
| 1135 | 1137 | |
| 1136 | - if ( empty( $title ) ) { | |
| 1138 | + if ( ( $insert || $has_title ) && empty( $title ) ) { | |
| 1137 | 1139 | throw new Exception( __( 'Lesson title is required', 'learnpress' ) ); |
| 1138 | 1140 | } |
| 1139 | 1141 | |
| 1140 | 1142 | if ( $insert ) { |
| @@ -1178,23 +1180,39 @@ | ||
| 1178 | 1180 | 'post_type' => LP_LESSON_CPT, |
| 1179 | 1181 | 'post_status' => $target_status, |
| 1180 | 1182 | );*/ |
| 1181 | 1183 | |
| 1182 | - if ( defined( 'ELEMENTOR_VERSION' ) ) { | |
| 1184 | + if ( defined( 'ELEMENTOR_VERSION' ) && array_key_exists( 'is_elementor', $data ) ) { | |
| 1183 | 1185 | \Elementor\Plugin::$instance->documents->get( $lesson_id )->set_is_built_with_elementor( ! empty( $is_elementor ) ); |
| 1184 | 1186 | } |
| 1185 | 1187 | |
| 1186 | - $lesson_model->post_title = $title; | |
| 1187 | - $lesson_model->post_content = $description; | |
| 1188 | - $lesson_model->post_status = $target_status; | |
| 1188 | + $must_save_lesson = false; | |
| 1189 | + if ( $has_title ) { | |
| 1190 | + $lesson_model->post_title = $title; | |
| 1191 | + $must_save_lesson = true; | |
| 1192 | + } | |
| 1193 | + | |
| 1194 | + if ( $has_content ) { | |
| 1195 | + $lesson_model->post_content = $description; | |
| 1196 | + $must_save_lesson = true; | |
| 1197 | + } | |
| 1198 | + | |
| 1199 | + if ( ! empty( $target_status ) ) { | |
| 1200 | + $lesson_model->post_status = $target_status; | |
| 1201 | + $must_save_lesson = true; | |
| 1202 | + } | |
| 1203 | + | |
| 1189 | 1204 | if ( ! empty( $lesson_slug ) ) { |
| 1190 | 1205 | $lesson_model->post_name = $lesson_slug; |
| 1206 | + $must_save_lesson = true; | |
| 1191 | 1207 | } |
| 1192 | 1208 | |
| 1193 | 1209 | //$restore_with_custom_slug = $this->prepare_desired_slug_for_restore( $lesson_id, $target_status, $lesson_slug ); |
| 1194 | 1210 | |
| 1195 | 1211 | //$update = wp_update_post( $update_arg ); |
| 1196 | - $lesson_model->save(); | |
| 1212 | + if ( $must_save_lesson ) { | |
| 1213 | + $lesson_model->save(); | |
| 1214 | + } | |
| 1197 | 1215 | |
| 1198 | 1216 | /*if ( $restore_with_custom_slug ) { |
| 1199 | 1217 | $this->sync_slug_after_restore( $lesson_id, $lesson_slug ); |
| 1200 | 1218 | }*/ |
| @@ -1214,9 +1232,9 @@ | ||
| 1214 | 1232 | $this->save_lesson_settings_to_model( $lesson_model, $data ); |
| 1215 | 1233 | } |
| 1216 | 1234 | |
| 1217 | 1235 | // Remove lesson from curriculum if status is not public |
| 1218 | - if ( $target_status !== 'publish' ) { | |
| 1236 | + if ( ! empty( $target_status ) && $target_status !== 'publish' ) { | |
| 1219 | 1237 | $this->remove_course_item_from_curriculum( $lesson_id, $course_id ); |
| 1220 | 1238 | } |
| 1221 | 1239 | |
| 1222 | 1240 | $response->status = 'success'; |
| @@ -1241,9 +1259,9 @@ | ||
| 1241 | 1259 | $response->data->lesson_permalink = urldecode( $course->get_item_link( $lesson_id ) ); |
| 1242 | 1260 | } |
| 1243 | 1261 | } |
| 1244 | 1262 | |
| 1245 | - $lesson_model_for_html = LessonPostModel::find( $lesson_id, true ); | |
| 1263 | + $lesson_model_for_html = LessonPostModel::find( $lesson_id, false ); | |
| 1246 | 1264 | if ( $return_html ) { |
| 1247 | 1265 | $response->data->list_item_html = $lesson_model_for_html |
| 1248 | 1266 | ? BuilderListLessonsTemplate::render_lesson( $lesson_model_for_html ) |
| 1249 | 1267 | : ''; |
| @@ -1276,9 +1294,9 @@ | ||
| 1276 | 1294 | try { |
| 1277 | 1295 | $data = self::check_valid_lesson(); |
| 1278 | 1296 | $lesson_id = $data['lesson_id'] ?? 0; |
| 1279 | 1297 | $status = $data['status'] ?? 'trash'; |
| 1280 | - $lesson_model = $data['lesson_model'] ?? []; | |
| 1298 | + $lesson_model = LessonPostModel::find( $lesson_id, false ); | |
| 1281 | 1299 | $lesson_slug = ! empty( $data['lesson_permalink'] ) |
| 1282 | 1300 | ? sanitize_title( wp_unslash( (string) $data['lesson_permalink'] ) ) |
| 1283 | 1301 | : ''; |
| 1284 | 1302 | |
| @@ -1380,9 +1398,9 @@ | ||
| 1380 | 1398 | } |
| 1381 | 1399 | } |
| 1382 | 1400 | |
| 1383 | 1401 | if ( 'delete' !== $status ) { |
| 1384 | - $lesson_model_new = LessonPostModel::find( $lesson_id, true ); | |
| 1402 | + $lesson_model_new = LessonPostModel::find( $lesson_id, false ); | |
| 1385 | 1403 | $response->data->html = $lesson_model_new ? BuilderListLessonsTemplate::render_lesson( $lesson_model_new ) : ''; |
| 1386 | 1404 | } |
| 1387 | 1405 | |
| 1388 | 1406 | $response->status = 'success'; |
| @@ -1396,10 +1414,13 @@ | ||
| 1396 | 1414 | } |
| 1397 | 1415 | |
| 1398 | 1416 | /** |
| 1399 | 1417 | * Save Lesson Settings |
| 1418 | + * @throws Exception | |
| 1400 | 1419 | */ |
| 1401 | 1420 | protected function save_lesson_settings_to_model( LessonPostModel $lessonModel, array $data ) { |
| 1421 | + $must_save = 0; | |
| 1422 | + | |
| 1402 | 1423 | if ( isset( $data['_lp_duration'] ) ) { |
| 1403 | 1424 | $duration = ! empty( $data['_lp_duration'] ) ? str_replace( ',', ' ', $data['_lp_duration'] ) : '0 minute'; |
| 1404 | 1425 | $explode = explode( ' ', $duration ); |
| 1405 | 1426 | $number = (float) $explode[0] < 0 ? 0 : absint( $explode[0] ); |
| @@ -1404,16 +1425,22 @@ | ||
| 1404 | 1425 | $explode = explode( ' ', $duration ); |
| 1405 | 1426 | $number = (float) $explode[0] < 0 ? 0 : absint( $explode[0] ); |
| 1406 | 1427 | $unit = $explode[1] ?? 'minute'; |
| 1407 | 1428 | |
| 1408 | - $lessonModel->save_meta_value_by_key( '_lp_duration', $number . ' ' . $unit ); | |
| 1429 | + $lessonModel->set_meta_value_by_key( $lessonModel::META_KEY_DURATION, $number . ' ' . $unit ); | |
| 1430 | + $must_save = 1; | |
| 1409 | 1431 | } |
| 1410 | 1432 | |
| 1411 | 1433 | if ( isset( $data['_lp_preview'] ) ) { |
| 1412 | 1434 | $enable = $data['_lp_preview'] === 'yes'; |
| 1413 | 1435 | $lessonModel->set_preview( $enable ); |
| 1436 | + $must_save = 1; | |
| 1414 | 1437 | } |
| 1415 | 1438 | |
| 1439 | + if ( $must_save ) { | |
| 1440 | + $lessonModel->save(); | |
| 1441 | + } | |
| 1442 | + | |
| 1416 | 1443 | /** |
| 1417 | 1444 | * Allow addons to persist their own lesson settings fields added to the |
| 1418 | 1445 | * settings metabox tabs when saving from Course Builder. |
| 1419 | 1446 | * |
| @@ -1684,9 +1711,9 @@ | ||
| 1684 | 1711 | try { |
| 1685 | 1712 | $data = self::check_valid_quiz(); |
| 1686 | 1713 | $quiz_id = $data['quiz_id'] ?? 0; |
| 1687 | 1714 | $status = $data['status'] ?? 'trash'; |
| 1688 | - $quiz_model = $data['quiz_model'] ?? []; | |
| 1715 | + $quiz_model = QuizPostModel::find( $quiz_id, false ); | |
| 1689 | 1716 | $quiz_slug = ! empty( $data['quiz_permalink'] ) |
| 1690 | 1717 | ? sanitize_title( wp_unslash( (string) $data['quiz_permalink'] ) ) |
| 1691 | 1718 | : ''; |
| 1692 | 1719 | |
| @@ -1788,9 +1815,9 @@ | ||
| 1788 | 1815 | } |
| 1789 | 1816 | } |
| 1790 | 1817 | |
| 1791 | 1818 | if ( 'delete' !== $status ) { |
| 1792 | - $fresh_quiz_model = QuizPostModel::find( $quiz_id, true ); | |
| 1819 | + $fresh_quiz_model = QuizPostModel::find( $quiz_id, false ); | |
| 1793 | 1820 | $response->data->html = $fresh_quiz_model |
| 1794 | 1821 | ? BuilderListQuizzesTemplate::render_quiz( $fresh_quiz_model ) |
| 1795 | 1822 | : ''; |
| 1796 | 1823 | } |
| @@ -1955,9 +1982,9 @@ | ||
| 1955 | 1982 | try { |
| 1956 | 1983 | $data = self::check_valid_question(); |
| 1957 | 1984 | $question_id = $data['question_id'] ?? 0; |
| 1958 | 1985 | $status = $data['status'] ?? 'trash'; |
| 1959 | - $question_model = $data['question_model'] ?? []; | |
| 1986 | + $question_model = QuestionPostModel::find( $question_id, false ); | |
| 1960 | 1987 | |
| 1961 | 1988 | if ( ! $question_model ) { |
| 1962 | 1989 | throw new Exception( __( 'Question not found', 'learnpress' ) ); |
| 1963 | 1990 | } |
| @@ -2021,9 +2048,9 @@ | ||
| 2021 | 2048 | $response->data->status = $status; |
| 2022 | 2049 | $response->data->button_title = __( 'Publish', 'learnpress' ); |
| 2023 | 2050 | |
| 2024 | 2051 | if ( 'delete' !== $status ) { |
| 2025 | - $fresh_question_model = QuestionPostModel::find( $question_id, true ); | |
| 2052 | + $fresh_question_model = QuestionPostModel::find( $question_id, false ); | |
| 2026 | 2053 | $response->data->html = $fresh_question_model |
| 2027 | 2054 | ? BuilderListQuestionsTemplate::render_question( $fresh_question_model ) |
| 2028 | 2055 | : ''; |
| 2029 | 2056 | } |