listActiveChunk( $after_id, self::CHUNK_SIZE ); if ( empty( $chunk ) ) { break; } foreach ( $chunk as $connection ) { $after_id = max( $after_id, (int) $connection['id'] ); ++$examined; if ( $this->ownerStillQualifies( (int) $connection['user_id'] ) ) { continue; } $id_disabled = $connections->disable( (int) $connection['id'], ConnectionStore::DISABLED_REASON_CAPABILITY_LOST ); if ( ! $id_disabled ) { continue; } ++$disabled_count; $audit_log->write( array( 'connection_id' => (int) $connection['id'], 'user_id' => (int) $connection['user_id'], 'event_type' => AuditLogStore::EVENT_CONNECTION_DISABLED, 'outcome' => AuditLogStore::OUTCOME_SUCCESS, 'subject_type' => 'connection', 'subject_id' => (int) $connection['id'], 'request_summary' => array( 'reason' => ConnectionStore::DISABLED_REASON_CAPABILITY_LOST ), ) ); } } while ( count( $chunk ) === self::CHUNK_SIZE && $examined < self::MAX_ROWS_PER_RUN ); return $disabled_count; } catch ( \Exception $e ) { return $disabled_count; } finally { delete_transient( self::LOCK_TRANSIENT ); } } /** * Whether a connection's owner still exists and still holds * `manage_woocommerce`. * * @param int $user_id Owner user id. * @return bool */ private function ownerStillQualifies( int $user_id ): bool { if ( $user_id <= 0 ) { return false; } $user = get_userdata( $user_id ); if ( ! $user ) { return false; } return user_can( $user, self::REQUIRED_CAPABILITY ); } /** * Static cron callback. * * @return void */ public static function cron_callback(): void { try { $sweeper = new self(); $sweeper->sweep(); } catch ( \Exception $e ) { } } }