*/ private $event_source; /** @var callable():mixed */ private $time_source; private string $hash_key; /** * @param callable():array|null $event_source Testable bounded cron adapter. * @param callable():mixed|null $time_source Testable UTC timestamp source. */ public function __construct( FindingFactory $findings, ?callable $event_source = null, ?callable $time_source = null, ?string $hash_key = null ) { $this->findings = $findings; $this->event_source = $event_source ?? array( $this, 'wordpress_events' ); $this->time_source = $time_source ?? static fn (): int => time(); $this->hash_key = $hash_key ?? ( new LocalHashKey() )->derive(); } public function id(): string { // phpcs:ignore WordPress.WP.CapitalPDangit.MisspelledInText -- Stable registry machine ID. return 'jobs.wordpress_cron'; } /** * @return array */ public function execute( string $timestamp ): array { try { $events = call_user_func( $this->event_source ); $now = call_user_func( $this->time_source ); } catch ( Throwable $exception ) { return $this->error_finding( $timestamp ); } if ( ! is_array( $events ) || ! is_int( $now ) || $now < 1 ) { return $this->error_finding( $timestamp ); } $total = count( $events ); $truncated = $total > self::MAX_EVENTS; $objects = array(); $observed = 0; $warning = false; $failure = false; $band_ranks = array( 'scheduled' => 0, 'grace_period' => 1, 'overdue_hour' => 2, 'overdue_day' => 3, 'overdue_long' => 4, ); foreach ( array_slice( $events, 0, self::MAX_EVENTS ) as $event ) { if ( ! is_array( $event ) || ! isset( $event['identifier'], $event['scheduled_at'], $event['recurring'] ) || ! is_string( $event['identifier'] ) || '' === $event['identifier'] || ! is_int( $event['scheduled_at'] ) || $event['scheduled_at'] < 1 || ! is_bool( $event['recurring'] ) ) { continue; } $reference = hash_hmac( 'sha256', $event['identifier'], $this->hash_key ); $band = $this->overdue_band( $now - $event['scheduled_at'] ); if ( ! isset( $objects[ $reference ] ) ) { if ( count( $objects ) >= self::MAX_OBJECTS ) { $truncated = true; continue; } $objects[ $reference ] = array( 'reference' => $reference, 'instance_count' => 0, 'recurrence' => $event['recurring'] ? 'recurring' : 'single', 'worst_state' => $band, ); } else { $current_recurrence = $objects[ $reference ]['recurrence']; $next_recurrence = $event['recurring'] ? 'recurring' : 'single'; if ( $current_recurrence !== $next_recurrence ) { $objects[ $reference ]['recurrence'] = 'mixed'; } if ( $band_ranks[ $band ] > $band_ranks[ $objects[ $reference ]['worst_state'] ] ) { $objects[ $reference ]['worst_state'] = $band; } } ++$objects[ $reference ]['instance_count']; ++$observed; $warning = $warning || in_array( $band, array( 'overdue_hour', 'overdue_day' ), true ); $failure = $failure || 'overdue_long' === $band; } $objects = array_values( $objects ); usort( $objects, static function ( array $left, array $right ): int { return $left['reference'] <=> $right['reference']; } ); if ( 0 === $observed ) { $status = 'not_tested'; $coverage = 'none'; } elseif ( $failure ) { $status = 'fail'; $coverage = $truncated || $observed < $total ? 'partial' : 'complete'; } elseif ( $warning ) { $status = 'warning'; $coverage = $truncated || $observed < $total ? 'partial' : 'complete'; } elseif ( $truncated || $observed < $total ) { $status = 'not_tested'; $coverage = 'partial'; } else { $status = 'pass'; $coverage = 'complete'; } return $this->findings->build( $this->id(), $status, array( 'objects' => $objects, 'observed_count' => $observed, 'total_count' => $total, 'truncated' => $truncated, 'limitations' => array( 'WordPress loads its cron option as one array; plugin-side processing is limited to 5,000 events and 500 hook references. Hook names use site-keyed HMAC values. Arguments, payloads and exact timestamps were not collected; overdue state uses five-minute grace and coarse bands.' ), ), $observed, $total, $coverage, $timestamp ); } private function overdue_band( int $delay ): string { if ( $delay <= 0 ) { return 'scheduled'; } if ( $delay <= 300 ) { return 'grace_period'; } if ( $delay <= 3600 ) { return 'overdue_hour'; } if ( $delay <= 86400 ) { return 'overdue_day'; } return 'overdue_long'; } /** * @return array */ private function wordpress_events(): array { $cron = _get_cron_array(); $events = array(); if ( ! is_array( $cron ) ) { return $events; } foreach ( $cron as $scheduled_at => $hooks ) { if ( ! is_numeric( $scheduled_at ) || ! is_array( $hooks ) ) { continue; } foreach ( $hooks as $identifier => $instances ) { if ( ! is_string( $identifier ) || ! is_array( $instances ) ) { continue; } foreach ( $instances as $instance ) { if ( ! is_array( $instance ) ) { continue; } $events[] = array( 'identifier' => $identifier, 'scheduled_at' => (int) $scheduled_at, 'recurring' => isset( $instance['schedule'] ) && is_string( $instance['schedule'] ) && '' !== $instance['schedule'], ); if ( count( $events ) > self::MAX_EVENTS ) { break 3; } } } } return $events; } /** * @return array */ private function error_finding( string $timestamp ): array { return $this->findings->build( $this->id(), 'error', array( 'objects' => array(), 'observed_count' => 0, 'total_count' => 0, 'truncated' => false, 'limitations' => array( 'The local cron adapter failed; no exception, hook, argument, payload or timestamp value was stored.' ), ), 0, 0, 'error', $timestamp ); } }