has_management_binding( $storage->get() ) ) { self::schedule_single_event( self::CRON_HOOK, $now + 5 ); self::schedule_single_event( self::HEARTBEAT_CRON_HOOK, $now + 10 ); } if ( $storage ) { $attempt = $storage->get_attempt(); if ( $attempt && in_array( isset( $attempt['status'] ) ? $attempt['status'] : '', array( 'pending', 'dns_pending' ), true ) ) { self::schedule_dns_recovery( $now + 1 ); } } } /** Queue a local diagnostic flush at the earliest required time. */ public static function schedule_diagnostics( $timestamp ) { $timestamp = max( time() + 1, (int) $timestamp ); $scheduled = wp_next_scheduled( self::DIAGNOSTICS_CRON_HOOK ); if ( $scheduled && $scheduled > $timestamp ) { wp_clear_scheduled_hook( self::DIAGNOSTICS_CRON_HOOK ); $scheduled = false; } if ( ! $scheduled ) { wp_schedule_single_event( $timestamp, self::DIAGNOSTICS_CRON_HOOK ); } } public static function schedule_dns_recovery( $timestamp ) { $scheduled = wp_next_scheduled( self::DNS_RECOVERY_CRON_HOOK ); if ( $scheduled && $scheduled < $timestamp ) { wp_clear_scheduled_hook( self::DNS_RECOVERY_CRON_HOOK ); $scheduled = false; } if ( ! $scheduled ) { wp_schedule_single_event( $timestamp, self::DNS_RECOVERY_CRON_HOOK ); } } public static function maybe_upgrade() { if ( false !== get_option( Storage::OPTION_NAME, false ) ) { return false; } self::activate_site(); return true; } public static function activate( $network_wide = false ) { if ( is_multisite() && $network_wide ) { $site_ids = get_sites( array( 'fields' => 'ids', 'number' => 0, ) ); foreach ( $site_ids as $site_id ) { switch_to_blog( $site_id ); self::activate_site(); restore_current_blog(); } return; } self::activate_site(); } public static function deactivate( $network_wide = false ) { if ( is_multisite() && $network_wide ) { $site_ids = get_sites( array( 'fields' => 'ids', 'number' => 0, ) ); foreach ( $site_ids as $site_id ) { switch_to_blog( $site_id ); self::deactivate_site(); restore_current_blog(); } return; } self::deactivate_site(); } public static function activate_site() { $storage = new Storage(); $storage->migrate_legacy(); $settings = $storage->get(); $settings['runtime_active'] = true; $storage->save( $settings ); $storage->reconcile_origin(); if ( ! wp_next_scheduled( self::CRON_HOOK ) ) { wp_schedule_event( time() + 300, 'hourly', self::CRON_HOOK ); } if ( ! wp_next_scheduled( self::HEARTBEAT_CRON_HOOK ) ) { wp_schedule_event( time() + 60, 'hourly', self::HEARTBEAT_CRON_HOOK ); } if ( ! empty( $settings['connection']['binding_id'] ) ) { ( new Lifecycle_Manager( $storage, new Api_Client() ) )->maybe_status( 'activate', false ); } $attempt = $storage->get_attempt(); if ( $attempt && in_array( $attempt['status'], array( 'pending', 'dns_pending' ), true ) ) { self::schedule_dns_recovery( time() + Protocol::DNS_POLL_INTERVAL ); } } public static function deactivate_site() { $storage = new Storage(); $settings = $storage->get(); $settings['runtime_active'] = false; $storage->save( $settings ); wp_clear_scheduled_hook( self::CRON_HOOK ); wp_clear_scheduled_hook( self::HEARTBEAT_CRON_HOOK ); wp_clear_scheduled_hook( self::DNS_RECOVERY_CRON_HOOK ); wp_clear_scheduled_hook( self::DIAGNOSTICS_CRON_HOOK ); } /** Queue one earlier single event while preserving any later recurring event. */ private static function schedule_single_event( $hook, $timestamp ) { $scheduled = wp_next_scheduled( $hook ); if ( ! $scheduled || $scheduled > $timestamp ) { wp_schedule_single_event( $timestamp, $hook ); } } }