Skip to content
Merged
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
29 changes: 26 additions & 3 deletions classes/Course.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {

Check failure on line 331 in classes/Course.php

View workflow job for this annotation

GitHub Actions / WPCS

Capabilities should be used instead of roles. Found "administrator" in function call to current_user_can()
return $content;
}

Expand Down Expand Up @@ -659,7 +659,7 @@
} else {
$errors['pricing'] = __( 'Invalid product', 'tutor' );
}
} else {

Check failure on line 662 in classes/Course.php

View workflow job for this annotation

GitHub Actions / WPCS

If control structure block found as the only statement within an "else" block. Use elseif instead.
/**
* If user does not select WC product
* Then automatic WC product will be create name with course title.
Expand Down Expand Up @@ -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() );

Check failure on line 799 in classes/Course.php

View workflow job for this annotation

GitHub Actions / WPCS

All output should be run through an escaping function (see the Security sections in the WordPress Developer Handbooks), found '$th'.
}
}

Expand Down Expand Up @@ -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 ) ) {
Comment thread
shewa12 marked this conversation as resolved.
$author_id = $requested_author_id;
}
}

$attached = (int) $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(umeta_id) FROM {$wpdb->usermeta}
WHERE user_id = %d
Expand All @@ -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 );

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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 );

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add_user_meta( $author_id, '_tutor_instructor_course_id', $post_ID ); this code exists above. Please check.

}

do_action( 'tutor_save_course_after', $post_ID, $post );
}

Expand Down Expand Up @@ -2420,7 +2443,7 @@
/**
* Only admin can change main author
*/
if ( $courses_post_type === $post_type && ! current_user_can( 'administrator' ) ) {

Check failure on line 2446 in classes/Course.php

View workflow job for this annotation

GitHub Actions / WPCS

Capabilities should be used instead of roles. Found "administrator" in function call to current_user_can()
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 ) );
Expand Down
70 changes: 70 additions & 0 deletions models/CourseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,7 @@
);

// Check if the current user is an admin.
if ( ! current_user_can( 'administrator' ) ) {

Check failure on line 746 in models/CourseModel.php

View workflow job for this annotation

GitHub Actions / WPCS

Capabilities should be used instead of roles. Found "administrator" in function call to current_user_can()
$args['author'] = $current_user->ID;
}

Expand Down Expand Up @@ -1180,7 +1180,7 @@
),
);

$courses = current_user_can( 'administrator' ) ? self::get_courses() : self::get_courses_by_instructor();

Check failure on line 1183 in models/CourseModel.php

View workflow job for this annotation

GitHub Actions / WPCS

Capabilities should be used instead of roles. Found "administrator" in function call to current_user_can()
if ( ! empty( $courses ) ) {
foreach ( $courses as $course ) {
$course_options[] = array(
Expand Down Expand Up @@ -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' ) );

Check failure on line 1523 in models/CourseModel.php

View workflow job for this annotation

GitHub Actions / WPCS

All output should be run through an escaping function (see the Security sections in the WordPress Developer Handbooks), found '__'.
}

$has_access = false;
Expand Down Expand Up @@ -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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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 )
);
}
}
Loading