0 ) { as_schedule_single_action( time() + $delay, self::HOOK, $args, self::GROUP ); } else { as_enqueue_async_action( self::HOOK, $args, self::GROUP ); } return; } $scheduled = wp_schedule_single_event( time() + max( 0, (int) $delay ), self::HOOK, $args, true ); if ( is_wp_error( $scheduled ) || false === $scheduled ) { // Cron unavailable: send now rather than lose the alert. self::handle( $payload, $attempt ); return; } if ( 0 === (int) $delay ) { self::$spawn = true; } } /** * Kick WP-Cron (non-blocking loopback) so low-traffic sites send right away. */ public static function maybe_spawn_cron() { if ( self::$spawn && ! wp_doing_cron() && function_exists( 'spawn_cron' ) && ! ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ) ) { spawn_cron(); } } /** * Send a queued payload. * * @param array $payload Payload. * @param int $attempt Attempt. */ public static function handle( $payload, $attempt = 0 ) { if ( ! is_array( $payload ) ) { return; } $settings = PingPush_Settings::get(); $event = (string) ( $payload['event'] ?? '' ); $title = (string) ( $payload['title'] ?? '' ); if ( empty( $settings['token'] ) || empty( $settings['user_key'] ) ) { PingPush_Logger::add( $event, $title, 'failed', 0, __( 'Pushover keys are not set.', 'pingpush' ) ); return; } self::$sending = true; $result = PingPush_API::send( $settings['token'], $settings['user_key'], $payload ); self::$sending = false; if ( $result['ok'] ) { $detail = ''; if ( null !== $result['remaining'] ) { /* translators: %d: messages remaining this month. */ $detail = sprintf( __( '%d messages left this month', 'pingpush' ), $result['remaining'] ); update_option( 'pingpush_quota_remaining', (int) $result['remaining'], false ); } PingPush_Logger::add( $event, $title, 'sent', $result['code'], $detail ); return; } if ( $result['retry'] && $attempt < self::MAX_RETRIES ) { $delays = array( 60, 300, 900 ); $delay = $delays[ min( $attempt, count( $delays ) - 1 ) ]; /* translators: 1: error, 2: seconds. */ PingPush_Logger::add( $event, $title, 'retrying', $result['code'], sprintf( __( '%1$s Retrying in %2$d seconds.', 'pingpush' ), $result['error'], $delay ) ); self::push( $payload, $attempt + 1, $delay ); return; } PingPush_Logger::add( $event, $title, 'failed', $result['code'], $result['error'] ); } }