diff --git a/changelog/lesson-author-sync b/changelog/lesson-author-sync new file mode 100644 index 0000000000..8a52439440 --- /dev/null +++ b/changelog/lesson-author-sync @@ -0,0 +1,4 @@ +Significance: patch +Type: fixed + +Fix Course Teacher going out of sync with Course Lessons diff --git a/includes/class-sensei-teacher.php b/includes/class-sensei-teacher.php index fbb9817972..d43f53fb6c 100755 --- a/includes/class-sensei-teacher.php +++ b/includes/class-sensei-teacher.php @@ -107,6 +107,9 @@ public function __construct() { // update lesson owner to course teacher before insert add_filter( 'wp_insert_post_data', array( $this, 'update_lesson_teacher' ), 99, 2 ); + // update lesson owner to course teacher when a lesson is updated. + add_action( 'updated_postmeta', array( $this, 'sync_lesson_teacher' ), 10, 4 ); + // If a Teacher logs in, redirect to /wp-admin/ add_filter( 'wp_login', array( $this, 'teacher_login_redirect' ), 10, 2 ); @@ -975,6 +978,42 @@ public function limit_grading_totals( $args ) { return $args; } + /** + * When the _lesson_course meta is updated, make sure the lesson author is the same as the course author + * + * @param int $meta_id ID of updated metadata entry. + * @param int $object_id Post ID. + * @param string $meta_key Metadata key. + * @param mixed $meta_value Metadata value. + */ + public function sync_lesson_teacher( $meta_id, $object_id, $meta_key, $meta_value ) { + if ( '_lesson_course' !== $meta_key ) { + return; + } + + // Check that the $object id is a Lesson ID. + if ( 'lesson' !== get_post_type( $object_id ) ) { + return; + } + + // Check the $meta_value is a Course ID. + if ( 'course' !== get_post_type( $meta_value ) ) { + return; + } + + $lesson_author_id = get_post_field( 'post_author', $object_id ); + $course_author_id = get_post_field( 'post_author', $meta_value ); + + if ( $lesson_author_id !== $course_author_id ) { + wp_update_post( + array( + 'ID' => (int) $object_id, + 'post_author' => (int) $course_author_id, + ) + ); + } + } + /** * It ensures that the author archive shows course by the current user. *