ref, $this->type, $this->payload, $this->priority, $this->requested_by, $this->attempt + 1 ); } /** * JSON-safe representation (Action Scheduler args). */ public function to_array(): array { return array( 'ref' => $this->ref, 'type' => $this->type, 'payload' => $this->payload, 'priority' => $this->priority, 'requested_by' => $this->requested_by, 'attempt' => $this->attempt, ); } /** * Rebuild from to_array() output. * * @param array $data Serialized task. */ public static function from_array( array $data ): self { return new self( (string) ( $data['ref'] ?? self::uuid4() ), (string) ( $data['type'] ?? 'unknown' ), is_array( $data['payload'] ?? null ) ? $data['payload'] : array(), (int) ( $data['priority'] ?? 50 ), isset( $data['requested_by'] ) ? (string) $data['requested_by'] : null, max( 1, (int) ( $data['attempt'] ?? 1 ) ), ); } /** * RFC 4122 v4 uuid without WordPress dependency (unit-testable). */ private static function uuid4(): string { $bytes = random_bytes( 16 ); $bytes[6] = chr( ( ord( $bytes[6] ) & 0x0f ) | 0x40 ); $bytes[8] = chr( ( ord( $bytes[8] ) & 0x3f ) | 0x80 ); return vsprintf( '%s%s-%s-%s-%s-%s%s%s', str_split( bin2hex( $bytes ), 4 ) ); } }