| @@ -582,8 +582,19 @@ | ||
| 582 | 582 | $this->save_assessment_settings_to_model( $courseModel, $data ); |
| 583 | 583 | |
| 584 | 584 | // Author settings |
| 585 | 585 | $this->save_author_settings_to_model( $courseModel, $data ); |
| 586 | + | |
| 587 | + /** | |
| 588 | + * Allow addons to persist their own course settings fields added to the | |
| 589 | + * settings metabox tabs (e.g. Co-Instructor) when saving from Course Builder. | |
| 590 | + * | |
| 591 | + * @param CoursePostModel $courseModel Course model being saved. | |
| 592 | + * @param array $data Submitted settings data. | |
| 593 | + * | |
| 594 | + * @since 4.3.8 | |
| 595 | + */ | |
| 596 | + do_action( 'learn-press/course-builder/save-course-settings', $courseModel, $data ); | |
| 586 | 597 | } |
| 587 | 598 | |
| 588 | 599 | /** |
| 589 | 600 | * Save general settings to CourseModel |
| @@ -717,8 +728,27 @@ | ||
| 717 | 728 | } |
| 718 | 729 | } |
| 719 | 730 | |
| 720 | 731 | /** |
| 732 | + * Normalize an extra-info field value to an array. | |
| 733 | + * | |
| 734 | + * The Course Builder sends these fields as arrays (one entry per input); | |
| 735 | + * older paths may send a comma-separated string. Splitting on comma must | |
| 736 | + * never be applied to array input, or values that legitimately contain a | |
| 737 | + * comma get broken into multiple entries. | |
| 738 | + * | |
| 739 | + * @param mixed $value | |
| 740 | + * @return array | |
| 741 | + */ | |
| 742 | + protected function extra_field_to_array( $value ): array { | |
| 743 | + if ( is_array( $value ) ) { | |
| 744 | + return $value; | |
| 745 | + } | |
| 746 | + | |
| 747 | + return $value !== '' ? explode( ',', (string) $value ) : []; | |
| 748 | + } | |
| 749 | + | |
| 750 | + /** | |
| 721 | 751 | * Save extra info settings to CourseModel |
| 722 | 752 | * |
| 723 | 753 | * @param CoursePostModel $courseModel |
| 724 | 754 | * @param array $data |
| @@ -725,11 +755,10 @@ | ||
| 725 | 755 | */ |
| 726 | 756 | protected function save_extra_settings_to_model( CoursePostModel &$courseModel, array $data ) { |
| 727 | 757 | // Requirements |
| 728 | 758 | if ( isset( $data['_lp_requirements'] ) ) { |
| 729 | - $requirements = ! empty( $data['_lp_requirements'] ) ? explode( ',', $data['_lp_requirements'] ) : []; | |
| 730 | 759 | $requirements = array_filter( |
| 731 | - $requirements, | |
| 760 | + $this->extra_field_to_array( $data['_lp_requirements'] ), | |
| 732 | 761 | function ( $item ) { |
| 733 | 762 | return ! is_null( $item ) && $item !== ''; |
| 734 | 763 | } |
| 735 | 764 | ); |
| @@ -736,11 +765,10 @@ | ||
| 736 | 765 | $courseModel->meta_data->{CoursePostModel::META_KEY_REQUIREMENTS} = array_map( 'sanitize_text_field', array_values( $requirements ) ); |
| 737 | 766 | } |
| 738 | 767 | |
| 739 | 768 | if ( isset( $data['_lp_target_audiences'] ) ) { |
| 740 | - $target_audiences = ! empty( $data['_lp_target_audiences'] ) ? explode( ',', $data['_lp_target_audiences'] ) : []; | |
| 741 | 769 | $target_audiences = array_filter( |
| 742 | - $target_audiences, | |
| 770 | + $this->extra_field_to_array( $data['_lp_target_audiences'] ), | |
| 743 | 771 | function ( $item ) { |
| 744 | 772 | return ! is_null( $item ) && $item !== ''; |
| 745 | 773 | } |
| 746 | 774 | ); |
| @@ -747,11 +775,10 @@ | ||
| 747 | 775 | $courseModel->meta_data->{CoursePostModel::META_KEY_TARGET} = array_map( 'sanitize_text_field', array_values( $target_audiences ) ); |
| 748 | 776 | } |
| 749 | 777 | |
| 750 | 778 | if ( isset( $data['_lp_key_features'] ) ) { |
| 751 | - $key_features = ! empty( $data['_lp_key_features'] ) ? explode( ',', $data['_lp_key_features'] ) : []; | |
| 752 | 779 | $key_features = array_filter( |
| 753 | - $key_features, | |
| 780 | + $this->extra_field_to_array( $data['_lp_key_features'] ), | |
| 754 | 781 | function ( $item ) { |
| 755 | 782 | return ! is_null( $item ) && $item !== ''; |
| 756 | 783 | } |
| 757 | 784 | ); |
| @@ -759,10 +786,10 @@ | ||
| 759 | 786 | } |
| 760 | 787 | |
| 761 | 788 | // FAQs |
| 762 | 789 | if ( isset( $data['_lp_faqs_question'] ) ) { |
| 763 | - $questions = ! empty( $data['_lp_faqs_question'] ) ? explode( ',', $data['_lp_faqs_question'] ) : []; | |
| 764 | - $answers = ! empty( $data['_lp_faqs_answer'] ) ? explode( ',', $data['_lp_faqs_answer'] ) : []; | |
| 790 | + $questions = $this->extra_field_to_array( $data['_lp_faqs_question'] ); | |
| 791 | + $answers = ! empty( $data['_lp_faqs_answer'] ) ? $this->extra_field_to_array( $data['_lp_faqs_answer'] ) : []; | |
| 765 | 792 | $faqs = []; |
| 766 | 793 | |
| 767 | 794 | if ( ! empty( $questions ) ) { |
| 768 | 795 | foreach ( $questions as $index => $question ) { |
| @@ -1114,8 +1141,9 @@ | ||
| 1114 | 1141 | $insert_arg = array( |
| 1115 | 1142 | 'post_title' => $title, |
| 1116 | 1143 | 'post_content' => $description, |
| 1117 | 1144 | 'post_status' => $target_status, |
| 1145 | + 'post_author' => get_current_user_id(), | |
| 1118 | 1146 | ); |
| 1119 | 1147 | |
| 1120 | 1148 | $lessonPostModelNew = new LessonPostModel( $insert_arg ); |
| 1121 | 1149 | $lessonPostModelNew->check_capabilities_create_item_course(); |
| @@ -1368,10 +1396,13 @@ | ||
| 1368 | 1396 | } |
| 1369 | 1397 | |
| 1370 | 1398 | /** |
| 1371 | 1399 | * Save Lesson Settings |
| 1400 | + * @throws Exception | |
| 1372 | 1401 | */ |
| 1373 | 1402 | protected function save_lesson_settings_to_model( LessonPostModel $lessonModel, array $data ) { |
| 1403 | + $must_save = 0; | |
| 1404 | + | |
| 1374 | 1405 | if ( isset( $data['_lp_duration'] ) ) { |
| 1375 | 1406 | $duration = ! empty( $data['_lp_duration'] ) ? str_replace( ',', ' ', $data['_lp_duration'] ) : '0 minute'; |
| 1376 | 1407 | $explode = explode( ' ', $duration ); |
| 1377 | 1408 | $number = (float) $explode[0] < 0 ? 0 : absint( $explode[0] ); |
| @@ -1376,15 +1407,32 @@ | ||
| 1376 | 1407 | $explode = explode( ' ', $duration ); |
| 1377 | 1408 | $number = (float) $explode[0] < 0 ? 0 : absint( $explode[0] ); |
| 1378 | 1409 | $unit = $explode[1] ?? 'minute'; |
| 1379 | 1410 | |
| 1380 | - $lessonModel->save_meta_value_by_key( '_lp_duration', $number . ' ' . $unit ); | |
| 1411 | + $lessonModel->set_meta_value_by_key( $lessonModel::META_KEY_DURATION, $number . ' ' . $unit ); | |
| 1412 | + $must_save = 1; | |
| 1381 | 1413 | } |
| 1382 | 1414 | |
| 1383 | 1415 | if ( isset( $data['_lp_preview'] ) ) { |
| 1384 | 1416 | $enable = $data['_lp_preview'] === 'yes'; |
| 1385 | 1417 | $lessonModel->set_preview( $enable ); |
| 1418 | + $must_save = 1; | |
| 1386 | 1419 | } |
| 1420 | + | |
| 1421 | + if ( $must_save ) { | |
| 1422 | + $lessonModel->save(); | |
| 1423 | + } | |
| 1424 | + | |
| 1425 | + /** | |
| 1426 | + * Allow addons to persist their own lesson settings fields added to the | |
| 1427 | + * settings metabox tabs when saving from Course Builder. | |
| 1428 | + * | |
| 1429 | + * @param LessonPostModel $lessonModel Lesson model being saved. | |
| 1430 | + * @param array $data Submitted settings data. | |
| 1431 | + * | |
| 1432 | + * @since 4.3.8 | |
| 1433 | + */ | |
| 1434 | + do_action( 'learn-press/course-builder/save-lesson-settings', $lessonModel, $data ); | |
| 1387 | 1435 | } |
| 1388 | 1436 | |
| 1389 | 1437 | /** |
| 1390 | 1438 | * Save Quiz Settings to QuizPostModel |
| @@ -1436,8 +1484,19 @@ | ||
| 1436 | 1484 | } |
| 1437 | 1485 | $quizModel->save_meta_value_by_key( $key, $value ); |
| 1438 | 1486 | } |
| 1439 | 1487 | } |
| 1488 | + | |
| 1489 | + /** | |
| 1490 | + * Allow addons to persist their own quiz settings fields added to the | |
| 1491 | + * settings metabox tabs when saving from Course Builder. | |
| 1492 | + * | |
| 1493 | + * @param QuizPostModel $quizModel Quiz model being saved. | |
| 1494 | + * @param array $data Submitted settings data. | |
| 1495 | + * | |
| 1496 | + * @since 4.3.8 | |
| 1497 | + */ | |
| 1498 | + do_action( 'learn-press/course-builder/save-quiz-settings', $quizModel, $data ); | |
| 1440 | 1499 | } |
| 1441 | 1500 | |
| 1442 | 1501 | /** |
| 1443 | 1502 | * Update Quiz from Course Builder. |
| @@ -1482,8 +1541,9 @@ | ||
| 1482 | 1541 | $insert_arg = array( |
| 1483 | 1542 | 'post_title' => $title, |
| 1484 | 1543 | 'post_content' => $description, |
| 1485 | 1544 | 'post_status' => $target_status, |
| 1545 | + 'post_author' => get_current_user_id(), | |
| 1486 | 1546 | ); |
| 1487 | 1547 | |
| 1488 | 1548 | $quizPostModelNew = new QuizPostModel( $insert_arg ); |
| 1489 | 1549 | $quizPostModelNew->check_capabilities_create_item_course(); |
| @@ -1823,8 +1883,9 @@ | ||
| 1823 | 1883 | $insert_arg = array( |
| 1824 | 1884 | 'post_title' => $title, |
| 1825 | 1885 | 'post_content' => $description, |
| 1826 | 1886 | 'post_status' => $target_status, |
| 1887 | + 'post_author' => get_current_user_id(), | |
| 1827 | 1888 | ); |
| 1828 | 1889 | |
| 1829 | 1890 | $questionPostModelNew = new QuestionPostModel( $insert_arg ); |
| 1830 | 1891 | $questionPostModelNew->check_capabilities_create_item_course(); |