*/ private $queue_source; /** * @param callable():array|null $queue_source Testable fixed-query queue adapter. */ public function __construct( FindingFactory $findings, ?callable $queue_source = null ) { $this->findings = $findings; $this->queue_source = $queue_source ?? array( $this, 'database_queue' ); } public function id(): string { return 'jobs.action_scheduler'; } /** * @return array */ public function execute( string $timestamp ): array { try { $queue = call_user_func( $this->queue_source ); } catch ( Throwable $exception ) { return $this->error_finding( $timestamp ); } if ( ! is_array( $queue ) || ! isset( $queue['available'], $queue['groups'] ) || ! is_bool( $queue['available'] ) || ! is_array( $queue['groups'] ) ) { return $this->error_finding( $timestamp ); } if ( ! $queue['available'] ) { return $this->not_tested_finding( $timestamp ); } $objects = array(); $seen = array(); $invalid = 0; $total = 0; $attention = false; $truncated = count( $queue['groups'] ) > count( self::STATUSES ); foreach ( array_slice( $queue['groups'], 0, self::GROUP_LIMIT ) as $group ) { if ( ! is_array( $group ) || ! isset( $group['status'], $group['count'], $group['overdue'] ) || ! is_string( $group['status'] ) || ! in_array( $group['status'], self::STATUSES, true ) || isset( $seen[ $group['status'] ] ) ) { ++$invalid; continue; } $count = $this->non_negative_integer( $group['count'] ); $overdue = $this->non_negative_integer( $group['overdue'] ); if ( null === $count || null === $overdue || $overdue > $count ) { ++$invalid; continue; } $seen[ $group['status'] ] = true; $objects[] = array( 'status' => $group['status'], 'action_count' => $count, 'overdue_count' => $overdue, ); $total += $count; $attention = $attention || ( 'failed' === $group['status'] && $count > 0 ) || $overdue > 0; } if ( 0 === count( $queue['groups'] ) ) { $status = 'pass'; $coverage = 'complete'; } elseif ( 0 === count( $objects ) ) { $status = 'not_tested'; $coverage = 'none'; } elseif ( $attention ) { $status = 'warning'; $coverage = $invalid > 0 || $truncated ? 'partial' : 'complete'; } elseif ( $invalid > 0 || $truncated ) { $status = 'not_tested'; $coverage = 'partial'; } else { $status = 'pass'; $coverage = 'complete'; } return $this->findings->build( $this->id(), $status, array( 'objects' => $objects, 'observed_count' => $total, 'total_count' => $total, 'truncated' => $truncated, 'limitations' => array( 'Only status counts and a five-minute pending-overdue aggregate were read from the documented custom table.', 'Hooks, arguments, extended arguments, schedules, groups, claims, logs and exact timestamps were not selected. Historical failed actions require context and are not proof of an active fault.', ), ), $total, $total, $coverage, $timestamp ); } /** * @return array{available:bool,groups:list} */ private function database_queue(): array { global $wpdb; $database = isset( $wpdb->dbname ) && is_string( $wpdb->dbname ) ? $wpdb->dbname : ''; $table = $wpdb->prefix . 'actionscheduler_actions'; if ( '' === $database || 1 !== preg_match( '/^[A-Za-z0-9_]+$/', $table ) ) { throw new RuntimeException( 'action_scheduler_schema_unavailable' ); } // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $available = $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_SCHEMA = %s AND TABLE_NAME = %s', $database, $table ) ); // phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $available_count = $this->non_negative_integer( $available ); if ( null === $available_count ) { throw new RuntimeException( 'action_scheduler_schema_unavailable' ); } if ( 0 === $available_count ) { return array( 'available' => false, 'groups' => array(), ); } // The identifier is compiled from a validated WordPress prefix. No hook, argument, payload or exact timestamp is selected. // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $rows = $wpdb->get_results( $wpdb->prepare( "SELECT status, COUNT(*) AS action_count, SUM(CASE WHEN status = 'pending' AND scheduled_date_gmt < (UTC_TIMESTAMP() - INTERVAL 5 MINUTE) THEN 1 ELSE 0 END) AS overdue_count FROM %i GROUP BY status ORDER BY status ASC LIMIT 101", $table ), ARRAY_A ); // phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching if ( ! is_array( $rows ) ) { throw new RuntimeException( 'action_scheduler_inventory_unavailable' ); } $groups = array(); foreach ( $rows as $row ) { $groups[] = array( 'status' => is_array( $row ) && isset( $row['status'] ) && is_string( $row['status'] ) ? $row['status'] : '', 'count' => $this->non_negative_integer( is_array( $row ) ? ( $row['action_count'] ?? null ) : null ) ?? -1, 'overdue' => $this->non_negative_integer( is_array( $row ) ? ( $row['overdue_count'] ?? null ) : null ) ?? -1, ); } return array( 'available' => true, 'groups' => $groups, ); } /** * @param mixed $value Integer-like database value. */ private function non_negative_integer( $value ): ?int { if ( ! is_int( $value ) && ! is_string( $value ) ) { return null; } $value = (string) $value; if ( 1 !== preg_match( '/^\d{1,18}$/', $value ) ) { return null; } $integer = (int) $value; return $integer >= 0 ? $integer : null; } /** * @return array */ private function not_tested_finding( string $timestamp ): array { return $this->findings->build( $this->id(), 'not_tested', array( 'objects' => array(), 'observed_count' => 0, 'total_count' => 0, 'truncated' => false, 'limitations' => array( 'The documented Action Scheduler custom actions table was not present, so this subsystem was not tested rather than marked healthy.' ), ), 0, 0, 'none', $timestamp ); } /** * @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 scheduler adapter failed; no SQL error, hook, argument, payload, group, timestamp or connection detail was stored.' ), ), 0, 1, 'error', $timestamp ); } }