From 37f70db550566eca529825aa576063644164ae7c Mon Sep 17 00:00:00 2001 From: markcummins Date: Thu, 25 Apr 2024 15:29:54 +0100 Subject: [PATCH 1/3] Adding Action to Sync Lesson Author when a lesson is saved --- includes/class-sensei-teacher.php | 37 +++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/includes/class-sensei-teacher.php b/includes/class-sensei-teacher.php index c4e4caa856..64c6682963 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 ); + // Define a custom function to execute when postmeta 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 ); @@ -959,6 +962,40 @@ 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 + * @param int $object_id + * @param string $meta_key + * @param mixed $meta_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' => $object_id, + 'post_author' => $course_author_id, + )); + } + } + /** * It ensures that the author archive shows course by the current user. * From c9e8ee7e5e04491de27f86904894a9679116c271 Mon Sep 17 00:00:00 2001 From: markcummins Date: Thu, 25 Apr 2024 15:54:08 +0100 Subject: [PATCH 2/3] Updating Comment --- includes/class-sensei-teacher.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/class-sensei-teacher.php b/includes/class-sensei-teacher.php index 64c6682963..b6a1b8e422 100755 --- a/includes/class-sensei-teacher.php +++ b/includes/class-sensei-teacher.php @@ -107,7 +107,7 @@ public function __construct() { // update lesson owner to course teacher before insert add_filter( 'wp_insert_post_data', array( $this, 'update_lesson_teacher' ), 99, 2 ); - // Define a custom function to execute when postmeta is updated + // 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/ From fd2262c1bdb4525f30a718dd2a34a07b978b6101 Mon Sep 17 00:00:00 2001 From: markcummins Date: Mon, 3 Jun 2024 02:07:16 +0100 Subject: [PATCH 3/3] Fixing code formatting issues, added changelog info --- changelog/lesson-author-sync | 4 ++++ includes/class-sensei-teacher.php | 36 ++++++++++++++++--------------- 2 files changed, 23 insertions(+), 17 deletions(-) create mode 100644 changelog/lesson-author-sync 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 b6a1b8e422..cbbe42eeed 100755 --- a/includes/class-sensei-teacher.php +++ b/includes/class-sensei-teacher.php @@ -107,8 +107,8 @@ 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 ); + // 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 ); @@ -964,35 +964,37 @@ public function limit_grading_totals( $args ) { /** * When the _lesson_course meta is updated, make sure the lesson author is the same as the course author - * - * @param int $meta_id - * @param int $object_id - * @param string $meta_key - * @param mixed $meta_value + * + * @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 ){ + if ( '_lesson_course' !== $meta_key ) { return; } - // Check that the $object id is a Lesson ID - if ('lesson' !== get_post_type( $object_id )){ + // 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 )){ + // 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' => $object_id, - 'post_author' => $course_author_id, - )); + if ( $lesson_author_id !== $course_author_id ) { + wp_update_post( + array( + 'ID' => (int) $object_id, + 'post_author' => (int) $course_author_id, + ) + ); } }