log( $logging, 'albert/connection-dropped-unused', $connection ); } foreach ( ConnectionRetention::sweep_idle() as $connection ) { $this->log( $logging, 'albert/connection-expired-idle', $connection ); } } catch ( \Throwable $e ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch // Never let a cron failure surface to the site. } } /** * Log one automatic removal. * * @param LoggingRepository $logging The log repository. * @param string $ability The synthetic ability id for this event. * @param array $connection The dropped connection, from * {@see \Albert\OAuth\Repositories\ClientRepository::getLiveConnections()}. * * @return void * @since 1.4.0 */ private function log( LoggingRepository $logging, string $ability, array $connection ): void { $user_ids = (array) $connection['user_ids']; $user_id = (int) ( $user_ids[0] ?? 0 ); $name = (string) ( $connection['label'] !== '' ? $connection['label'] : $connection['name'] ); $logging->insert( $ability, $user_id, 'success', null, [ 'client_id' => (string) $connection['client_id'], 'client_name' => $name, ] ); } /** * Schedule the daily sweep if not already scheduled. * * @return void * @since 1.4.0 */ public static function schedule(): void { if ( ! wp_next_scheduled( self::HOOK ) ) { wp_schedule_event( time() + HOUR_IN_SECONDS, 'daily', self::HOOK ); } } /** * Unschedule the daily sweep. * * @return void * @since 1.4.0 */ public static function unschedule(): void { $timestamp = wp_next_scheduled( self::HOOK ); while ( $timestamp !== false ) { wp_unschedule_event( $timestamp, self::HOOK ); $timestamp = wp_next_scheduled( self::HOOK ); } } }