( new ConnectionManager() )->check() ); } public static function schedules( array $schedules ): array { $schedules['waitlistize_five_minutes'] = array( // phpcs:ignore WordPress.WP.CronInterval.CronSchedulesInterval -- L1 access tokens expire in 15 minutes; renew with a safety margin. 'interval' => 300, 'display' => __( 'Every five minutes (Waitlistize)', 'waitlistize' ), ); return $schedules; } public static function activate( bool $network_wide = false ): void { // Activation can load this file after plugins_loaded has already fired. // phpcs:ignore WordPress.WP.CronInterval.CronSchedulesInterval -- Restore approved connections with the required refresh margin. add_filter( 'cron_schedules', array( self::class, 'schedules' ) ); self::each_site( $network_wide, static function (): void { ( new Mutex() )->run( static function (): void { $store = new TokenStore(); $state = $store->read(); if ( ! $store->disabled() && in_array( $state['status'], array( 'connected', 'offline' ), true ) && ! wp_next_scheduled( ConnectionManager::CRON ) ) { wp_schedule_event( time() + 300, 'waitlistize_five_minutes', ConnectionManager::CRON ); } } ); } ); } public static function deactivate( bool $network_wide = false ): void { self::each_site( $network_wide, static fn () => wp_clear_scheduled_hook( ConnectionManager::CRON ) ); } private static function each_site( bool $network_wide, callable $callback ): void { if ( is_multisite() && $network_wide ) { $offset = 0; do { $sites = get_sites( array( 'fields' => 'ids', 'number' => 100, 'offset' => $offset, ) ); foreach ( $sites as $site_id ) { switch_to_blog( (int) $site_id ); $callback(); restore_current_blog(); } $offset += 100; $count = count( $sites ); } while ( 100 === $count ); } else { $callback(); } } }