Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Course Teacher going out of sync with Course Lessons #7600

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changelog/lesson-author-sync
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Fix Course Teacher going out of sync with Course Lessons
39 changes: 39 additions & 0 deletions includes/class-sensei-teacher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down Expand Up @@ -959,6 +962,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.
*
Expand Down
Loading