ve the response. Must exit — never returns. */ final public function handle_request(): void { $key = $this->get_transient_key(); $cached = get_transient( $key ); // --- Cache hit --- if ( is_array( $cached ) ) { $this->send_response( $cached['body'], $cached['etag'], $cached['last_modified'] ); } // --- Generate --- $raw = $this->generate_content(); if ( is_array( $raw ) || is_object( $raw ) ) { $body = wp_json_encode( $raw, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); } else { $body = (string) $raw; } $etag = '"' . md5( $body ) . '"'; $last_modified = gmdate( 'D, d M Y H:i:s' ) . ' GMT'; set_transient( $key, [ 'body' => $body, 'etag' => $etag, 'last_modified' => $last_modified, ], self::CACHE_TTL ); $this->send_response( $body, $etag, $last_modified ); } // ------------------------------------------------------------------ // Helpers for subclasses // ------------------------------------------------------------------ /** * Sanitize a metadata string before embedding in the response. * Strips HTML and neutralises prompt-injection patterns. */ protected function sanitize( string $text ): string { return ( new Prompt_Injection_Sanitizer() )->sanitize( $text ); } // ------------------------------------------------------------------ // Internals // ------------------------------------------------------------------ /** * Check whether the client's cached copy is still fresh (ETag / Last-Modified). * Returns true when a 304 was sent (and already exited). */ private function maybe_send_304( string $etag, string $last_modified ): void { $if_none_match = isset( $_SERVER['HTTP_IF_NONE_MATCH'] ) ? trim( sanitize_text_field( wp_unslash( $_SERVER['HTTP_IF_NONE_MATCH'] ) ) ) : ''; if ( '' !== $if_none_match ) { foreach ( explode( ',', $if_none_match ) as $candidate ) { $candidate = trim( $candidate ); if ( '*' === $candidate || $candidate === $etag ) { status_header( 304 ); exit; } } return; } $if_modified_since = isset( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ? trim( sanitize_text_field( wp_unslash( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) ) : ''; if ( '' === $if_modified_since ) { return; } $client_ts = strtotime( $if_modified_since ); $server_ts = strtotime( $last_modified ); if ( false !== $client_ts && false !== $server_ts && $client_ts >= $server_ts ) { status_header( 304 ); exit; } } private function send_response( string $body, string $etag, string $last_modified ): void { $this->maybe_send_304( $etag, $last_modified ); $max_age = (int) apply_filters( 'elementor/agents/well_known/cache_max_age', self::CACHE_TTL ); status_header( 200 ); header( 'Content-Type: ' . $this->get_content_type() . '; charset=utf-8' ); header( 'X-Content-Type-Options: nosniff' ); header( 'Cache-Control: public, max-age=' . max( 0, $max_age ) ); header( 'ETag: ' . $etag ); header( 'Last-Modified: ' . $last_modified ); Utils::print_unescaped_internal_string( $body ); exit; } }