0 ) { ob_end_flush(); } nocache_headers(); header( 'Content-Type: text/event-stream; charset=utf-8' ); header( 'Cache-Control: no-cache, no-store, must-revalidate, private' ); header( 'Connection: keep-alive' ); header( 'X-Accel-Buffering: no' ); header( 'X-Content-Type-Options: nosniff' ); echo ': ' . esc_html__( 'stream open', 'ai-chat-for-amazon-bedrock' ) . "\n\n"; $this->flush_output(); } /** * Emit one named event with a JSON payload. * * @param string $type Event name. * @param array $data Payload. */ public function send( $type, $data ) { $data = is_array( $data ) ? $data : array(); $data['type'] = sanitize_key( $type ); $encoded = wp_json_encode( $data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES ); if ( ! is_string( $encoded ) ) { return; } echo 'event: ' . sanitize_key( $type ) . "\n"; echo 'data: ' . $encoded . "\n\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped $this->flush_output(); } /** * Close the stream and end the request. * * A stream response cannot be followed by anything else, so this exits. */ public function close() { echo "event: close\ndata: {}\n\n"; $this->flush_output(); exit; } /** * Push whatever is buffered to the client. */ public function flush_output() { if ( ob_get_level() > 0 ) { @ob_flush(); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged } flush(); } }