diff --git a/classes/Tutor.php b/classes/Tutor.php index e873452ca1..ea56e71937 100644 --- a/classes/Tutor.php +++ b/classes/Tutor.php @@ -1114,6 +1114,7 @@ public static function manage_tutor_roles_and_permissions() { if ( $instructor ) { $instructor_cap = array( 'edit_posts', + 'edit_published_posts', 'read', 'upload_files', ); diff --git a/migrations/InstructorCapabilityMigrator.php b/migrations/InstructorCapabilityMigrator.php new file mode 100644 index 0000000000..378b7a74ec --- /dev/null +++ b/migrations/InstructorCapabilityMigrator.php @@ -0,0 +1,132 @@ + + * @link https://themeum.com + * @since 4.0.8 + */ + +namespace Tutor\Migrations; + +use Tutor\Migrations\Contracts\SingleProcessor; +use Tutor\Models\UserModel; + +/** + * Class InstructorCapabilityMigrator + * + * @since 4.0.8 + */ +class InstructorCapabilityMigrator extends BatchProcessor implements SingleProcessor { + + /** + * User model + * + * @var UserModel + */ + private $user_model; + + /** + * Name of the migration + * + * @since 4.0.8 + * + * @var string + */ + protected $name = 'Instructor Capability Migrator'; + + /** + * Action + * + * @since 4.0.8 + * + * @var string + */ + protected $action = 'instructor_capability_migrator'; + + /** + * Batch size + * + * @since 4.0.8 + * + * @var integer + */ + protected $batch_size = 100; + + /** + * Schedule interval. + * + * @since 4.0.8 + * + * @var integer + */ + protected $schedule_interval = 10; + + /** + * Get total unprocessed result. + * + * @since 4.0.8 + * + * @return int + */ + protected function get_total_items(): int { + $this->user_model = new UserModel(); + $users = $this->user_model->get_users_list( + array( + 'role' => tutor()->instructor_role, + 'fields' => 'ID', + ) + ); + return $users->get_total(); + } + + /** + * Get items to batch process. + * + * @since 4.0.8 + * + * @param int $offset offset. + * @param int $limit limit. + * + * @return array + */ + protected function get_items( $offset, $limit ): array { + $this->user_model = new UserModel(); + $users = $this->user_model->get_users_list( + array( + 'role' => tutor()->instructor_role, + 'number' => $limit, + 'offset' => $offset, + ) + ); + return $users->get_results(); + } + + /** + * Process instructor to add capability. + * + * @since 4.0.8 + * + * @param object $item item. + * + * @return void + */ + public function process_item( $item ): void { + $user = new \WP_User( $item->ID ); + if ( ! $user->has_cap( 'edit_published_posts' ) ) { + $user->add_cap( 'edit_published_posts' ); + } + } + + /** + * On migration complete event. + * + * @since 4.0.8 + * + * @return void + */ + protected function on_complete() { + error_log( 'Instructor capability migration completed!' ); + } +} diff --git a/migrations/Migration.php b/migrations/Migration.php index 998d99bdaf..96891cc145 100644 --- a/migrations/Migration.php +++ b/migrations/Migration.php @@ -30,6 +30,7 @@ public function __construct() { */ public function schedule_migrations() { $migrators = array( + InstructorCapabilityMigrator::instance(), QuizAttemptMigrator::instance(), );