schedule( $job, 0 ); } /** * Schedule a retry. * * @param Job $job Job carrying its incremented attempt count. * @param int $delay_s Seconds to wait. * @return bool */ public function retry( Job $job, int $delay_s ): bool { return $this->schedule( $job, max( 0, $delay_s ) ); } /** * How many deliveries are waiting. * * @return int */ public function size(): int { if ( ! self::is_available() ) { return 0; } $pending = as_get_scheduled_actions( array( 'hook' => self::ACTION, 'group' => self::GROUP, 'status' => 'pending', 'per_page' => -1, ), 'ids' ); return is_array( $pending ) ? count( $pending ) : 0; } /** * Cancel every pending delivery. * * @return int */ public function purge(): int { if ( ! self::is_available() ) { return 0; } $size = $this->size(); as_unschedule_all_actions( self::ACTION, array(), self::GROUP ); return $size; } /** * Name shown on the settings screen. * * @return string */ public function label(): string { return __( 'Action Scheduler', 'adpix-analytics' ); } /** * Schedule one job. * * @param Job $job Job. * @param int $delay_s Seconds to wait. * @return bool */ private function schedule( Job $job, int $delay_s ): bool { if ( ! self::is_available() ) { return false; } // Unique is deliberately off. Two jobs for the same event carry the same event_id, and the // collector already collapses those; suppressing the second one here instead would also // suppress a genuine second status change that happened to look similar. $action = as_schedule_single_action( time() + $delay_s, self::ACTION, array( 'job' => $job->to_array() ), self::GROUP ); return is_numeric( $action ) && (int) $action > 0; } }