$args Args. * @param DateTimeImmutable|null $run_at Run at. * @param string $group Group. * @param string|null $unique_key Unique key. */ public function schedule_single( string $hook, array $args, ?DateTimeImmutable $run_at = null, string $group = 'bookline', ?string $unique_key = null ): int { if ( ! function_exists( 'as_schedule_single_action' ) ) { return 0; } $timestamp = null === $run_at ? time() : $run_at->setTimezone( new DateTimeZone( 'UTC' ) )->getTimestamp(); if ( null !== $unique_key && function_exists( 'as_has_scheduled_action' ) && as_has_scheduled_action( $hook, $args, $group ) ) { return 0; } $id = as_schedule_single_action( $timestamp, $hook, $args, $group ); return is_int( $id ) ? $id : (int) $id; } /** * {@inheritDoc} * * @param string $hook Hook. * @param int $interval Interval. * @param string $group Group. */ public function schedule_recurring( string $hook, int $interval, string $group = 'bookline' ): void { if ( ! function_exists( 'as_schedule_recurring_action' ) ) { return; } if ( function_exists( 'as_has_scheduled_action' ) && as_has_scheduled_action( $hook, [], $group ) ) { return; } as_schedule_recurring_action( time(), $interval, $hook, [], $group ); } /** * {@inheritDoc} * * @param string $hook Hook. * @param array $args Args. * @param string $group Group. */ public function cancel( string $hook, array $args = [], string $group = 'bookline' ): void { if ( function_exists( 'as_unschedule_all_actions' ) ) { as_unschedule_all_actions( $hook, $args, $group ); } } /** * {@inheritDoc} * * @param string $group Group. */ public function cancel_group( string $group = 'bookline' ): void { if ( function_exists( 'as_unschedule_all_actions' ) ) { as_unschedule_all_actions( null, [], $group ); } } /** * {@inheritDoc} */ public function is_available(): bool { return function_exists( 'as_schedule_single_action' ); } /** * {@inheritDoc} * * @param string $group Group. */ public function pending_count( string $group = 'bookline' ): int { if ( ! function_exists( 'as_get_scheduled_actions' ) ) { return 0; } $actions = as_get_scheduled_actions( [ 'group' => $group, 'status' => 'pending', 'per_page' => 1, ], 'ids' ); if ( ! is_array( $actions ) ) { return 0; } // Prefer store count when available. if ( function_exists( 'as_get_scheduled_actions' ) && class_exists( '\ActionScheduler', false ) ) { $store = \ActionScheduler::store(); if ( is_object( $store ) && method_exists( $store, 'query_actions' ) ) { $count = $store->query_actions( [ 'group' => $group, 'status' => 'pending', 'per_page' => 1, ], 'count' ); return is_numeric( $count ) ? (int) $count : count( $actions ); } } return count( $actions ); } /** * {@inheritDoc} * * @param string $group Group. */ public function failed_count( string $group = 'bookline' ): int { if ( ! class_exists( '\ActionScheduler', false ) ) { return 0; } $store = \ActionScheduler::store(); if ( ! is_object( $store ) || ! method_exists( $store, 'query_actions' ) ) { return 0; } $count = $store->query_actions( [ 'group' => $group, 'status' => 'failed', 'per_page' => 1, ], 'count' ); return is_numeric( $count ) ? (int) $count : 0; } }