send_due( 50, 20 ); self::install(); } public static function install(): void { $next = Services::notifier()->next_due(); if ( null !== $next ) { self::schedule( max( Clock::timestamp(), (int) strtotime( $next . ' UTC' ) ) ); } } public static function schedule( int $timestamp ): void { if ( ! did_action( 'action_scheduler_init' ) ) { if ( null === self::$deferred ) { add_action( 'action_scheduler_init', array( self::class, 'schedule_deferred' ) ); } self::$deferred = null === self::$deferred ? $timestamp : min( self::$deferred, $timestamp ); return; } if ( ! function_exists( 'as_get_scheduled_actions' ) || ! function_exists( 'as_schedule_single_action' ) ) { return; } $next = self::next_pending(); if ( null !== $next && $next <= $timestamp ) { return; } if ( null !== $next && function_exists( 'as_unschedule_all_actions' ) ) { as_unschedule_all_actions( self::HOOK, array(), self::GROUP ); } as_schedule_single_action( $timestamp, self::HOOK, array(), self::GROUP, false ); } private static function next_pending(): ?int { if ( ! class_exists( '\ActionScheduler_Store' ) ) { return null; } $actions = as_get_scheduled_actions( array( 'hook' => self::HOOK, 'group' => self::GROUP, 'status' => \ActionScheduler_Store::STATUS_PENDING, 'orderby' => 'date', 'order' => 'ASC', 'per_page' => 1, ) ); foreach ( is_array( $actions ) ? $actions : array() as $action ) { $date = $action instanceof \ActionScheduler_Action ? $action->get_schedule()->get_date() : null; return $date instanceof \DateTimeInterface ? $date->getTimestamp() : 0; } return null; } public static function schedule_deferred(): void { $at = self::$deferred; self::$deferred = null; if ( null !== $at ) { self::schedule( $at ); } } }