diff --git a/classes/Course.php b/classes/Course.php index 0f20a0e78f..0bd6a370b8 100644 --- a/classes/Course.php +++ b/classes/Course.php @@ -615,6 +615,7 @@ public function setup_course_categories_tags( $post_id, $params ) { * Validate price for course create * * @since 3.0.0 + * @since 4.3.0 Reject WC products the current user is not authorized to use. * * @param array $params array of params. * @param array $errors array of errors. @@ -632,15 +633,21 @@ public function validate_price( $params, &$errors ) { if ( self::PRICE_TYPE_PAID === $type ) { $monetize_by = tutor_utils()->get_option( 'monetize_by' ); if ( 'wc' === $monetize_by ) { - $product_id = (int) isset( $params['pricing']['product_id'] ) ? $params['pricing']['product_id'] : 0; + $product_id = isset( $params['pricing']['product_id'] ) ? (int) $params['pricing']['product_id'] : 0; // $product_id = 0 then new WC product will be created. if ( $product_id ) { $product = wc_get_product( $product_id ); if ( is_a( $product, 'WC_Product' ) ) { + if ( ! $this->is_valid_wc_product_author( $product_id, 0 ) ) { + $errors['pricing'] = __( 'You are not authorized to use this product', 'tutor' ); + } + $is_linked_with_course = tutor_utils()->product_belongs_with_course( $product_id ); if ( $is_linked_with_course ) { $errors['pricing'] = __( 'Product already linked with course', 'tutor' ); } + } else { + $errors['pricing'] = __( 'Invalid product', 'tutor' ); } } } @@ -652,9 +659,10 @@ public function validate_price( $params, &$errors ) { * Validate WC product author * * @since 4.2.2 + * @since 4.3.0 Support create flows with $course_id = 0 (owner-only). * * @param int $product_id product id. - * @param int $course_id course id. + * @param int $course_id course id. Pass 0 when the course does not exist yet. * * @return bool */ @@ -667,6 +675,9 @@ public function is_valid_wc_product_author( $product_id, $course_id ) { if ( $product_author === $current_user_id ) { return true; } + if ( ! $course_id ) { + return false; + } $course_author = (int) get_post_field( 'post_author', $course_id ); if ( $product_author === $course_author && tutor_utils()->is_instructor_of_this_course( $current_user_id, $course_id ) ) { return true;