-
Notifications
You must be signed in to change notification settings - Fork 92
Course content stay in sync whenever a course's main instructor is changed #2925
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
Changes from all commits
3ab289d
68b9a7b
0523b1e
e036dbb
ecf7c65
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -328,7 +328,7 @@ | |
| global $wp_query; | ||
| $course_coming_soon_enabled = (int) get_post_meta( $content->ID, '_tutor_course_enable_coming_soon', true ); | ||
| $is_instructor = tutor_utils()->is_instructor_of_this_course( get_current_user_id(), $content->ID, true ); | ||
| if ( ! CourseModel::get_post_types( $content ) || current_user_can( 'administrator' ) || $is_instructor || $course_coming_soon_enabled ) { | ||
| return $content; | ||
| } | ||
|
|
||
|
|
@@ -659,7 +659,7 @@ | |
| } else { | ||
| $errors['pricing'] = __( 'Invalid product', 'tutor' ); | ||
| } | ||
| } else { | ||
| /** | ||
| * If user does not select WC product | ||
| * Then automatic WC product will be create name with course title. | ||
|
|
@@ -796,7 +796,7 @@ | |
| update_post_meta( $post_id, self::COURSE_PRICE_TYPE_META, $params['pricing']['type'] ); | ||
| } | ||
| } catch ( \Throwable $th ) { | ||
| throw new \Exception( $th->getMessage() ); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -1977,10 +1977,21 @@ | |
| /** | ||
| * Adding author to instructor automatically | ||
| */ | ||
| $requested_author_id = Input::post( 'post_author_override', 0, Input::TYPE_INT ); | ||
|
|
||
| // Override post author id. | ||
| $author_id = isset( $_POST['post_author_override'] ) ? $_POST['post_author_override'] : $post->post_author; //phpcs:ignore | ||
| $attached = (int) $wpdb->get_var( | ||
| /** | ||
| * Only accept the requested author override if it targets a real,approved instructor or admin | ||
| * | ||
| * @since 4.0.4 | ||
| */ | ||
| $author_id = $post->post_author; | ||
| if ( $requested_author_id && ( User::is_admin() || User::is_instructor() ) ) { | ||
| if ( User::is_admin( $requested_author_id ) || User::is_instructor( $requested_author_id, true ) ) { | ||
| $author_id = $requested_author_id; | ||
| } | ||
| } | ||
|
|
||
| $attached = (int) $wpdb->get_var( | ||
| $wpdb->prepare( | ||
| "SELECT COUNT(umeta_id) FROM {$wpdb->usermeta} | ||
| WHERE user_id = %d | ||
|
|
@@ -2007,6 +2018,18 @@ | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Update course content if main author is changed and multi-instructor addon is disabled. | ||
| * | ||
| * @since 4.0.4 | ||
| */ | ||
| if ( ! $attached && ! tutor_utils()->is_addon_enabled( 'tutor-multi-instructors' ) ) { | ||
| CourseModel::update_course_content_author( $post_ID, (int) $author_id ); | ||
| // Remove all existing instructors from the course and add the new one. | ||
| delete_metadata( 'user', 0, '_tutor_instructor_course_id', $post_ID, true ); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Before deleting, how are we showing a warning alert? |
||
| add_user_meta( $author_id, '_tutor_instructor_course_id', $post_ID ); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
|
|
||
| do_action( 'tutor_save_course_after', $post_ID, $post ); | ||
| } | ||
|
|
||
|
|
@@ -2420,7 +2443,7 @@ | |
| /** | ||
| * Only admin can change main author | ||
| */ | ||
| if ( $courses_post_type === $post_type && ! current_user_can( 'administrator' ) ) { | ||
| global $wpdb; | ||
| $post_ID = (int) tutor_utils()->avalue_dot( 'ID', $postarr ); | ||
| $post_author = (int) $wpdb->get_var( $wpdb->prepare( "SELECT post_author FROM {$wpdb->posts} WHERE ID = %d ", $post_ID ) ); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -743,7 +743,7 @@ | |
| ); | ||
|
|
||
| // Check if the current user is an admin. | ||
| if ( ! current_user_can( 'administrator' ) ) { | ||
| $args['author'] = $current_user->ID; | ||
| } | ||
|
|
||
|
|
@@ -1180,7 +1180,7 @@ | |
| ), | ||
| ); | ||
|
|
||
| $courses = current_user_can( 'administrator' ) ? self::get_courses() : self::get_courses_by_instructor(); | ||
| if ( ! empty( $courses ) ) { | ||
| foreach ( $courses as $course ) { | ||
| $course_options[] = array( | ||
|
|
@@ -1520,7 +1520,7 @@ | |
| $args = wp_parse_args( $args, $defaults ); | ||
|
|
||
| if ( ! $args['course_id'] || ! $args['current_post_id'] || ! $args['current_post_type'] ) { | ||
| throw new InvalidArgumentException( __( 'Invalid argument passed', 'tutor' ) ); | ||
| } | ||
|
|
||
| $has_access = false; | ||
|
|
@@ -1750,4 +1750,74 @@ | |
| + ( $duration['durationMinutes'] * MINUTE_IN_SECONDS ) | ||
| + ( $duration['durationSeconds'] ); | ||
| } | ||
|
|
||
| /** | ||
| * Update the post_author of a course's content (topics, lessons, | ||
| * quizzes and assignments) to a given user. | ||
| * | ||
| * @since 4.0.3 | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should be 4.0.5 |
||
| * | ||
| * @param int $course_id course id. | ||
| * @param int $author_id new author (user) id. | ||
| * | ||
| * @return bool | ||
| */ | ||
| public static function update_course_content_author( int $course_id, int $author_id ): bool { | ||
| global $wpdb; | ||
|
|
||
| $content_ids = get_posts( | ||
| array( | ||
| 'post_parent' => $course_id, | ||
| 'post_type' => tutor()->topics_post_type, | ||
| 'fields' => 'ids', | ||
| 'posts_per_page' => -1, | ||
| 'post_status' => 'any', | ||
| ) | ||
| ); | ||
|
|
||
| $content_ids = is_array( $content_ids ) ? $content_ids : array(); | ||
| $default_post_types = array( tutor()->lesson_post_type, tutor()->quiz_post_type ); | ||
| $content_post_types = array_unique( apply_filters( 'tutor_course_contents_post_types', $default_post_types ) ); | ||
|
|
||
| $primary_table = 'posts as course'; | ||
| $topic_table = 'posts as topic'; | ||
| $content_table = 'posts as content'; | ||
|
|
||
| $joined_data = QueryHelper::get_joined_data( | ||
| $primary_table, | ||
| array( | ||
| array( | ||
| 'type' => 'INNER', | ||
| 'table' => $topic_table, | ||
| 'on' => 'course.ID = topic.post_parent', | ||
| ), | ||
| array( | ||
| 'type' => 'INNER', | ||
| 'table' => $content_table, | ||
| 'on' => 'topic.ID = content.post_parent', | ||
| ), | ||
| ), | ||
| array( 'content.ID' ), | ||
| array( | ||
| 'course.ID' => $course_id, | ||
| 'content.post_type' => array( 'IN', $content_post_types ), | ||
| ), | ||
| array(), | ||
| '', | ||
| -1 | ||
| ); | ||
|
|
||
| $content_ids = array_merge( $content_ids, wp_list_pluck( $joined_data['results'], 'ID' ) ); | ||
| $content_ids = array_filter( array_unique( array_map( 'absint', $content_ids ) ) ); | ||
|
|
||
| if ( empty( $content_ids ) ) { | ||
| return false; | ||
| } | ||
|
|
||
| return (bool) QueryHelper::update_where_in( | ||
| $wpdb->posts, | ||
| array( 'post_author' => $author_id ), | ||
| implode( ',', $content_ids ) | ||
| ); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.