PluginProbe
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More / 2.6.4
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More v2.6.4
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.10 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 2.0.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 47 releases
content-control / classes / Base / Stream.php

Stream.php in Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More 2.6.4, at classes/Base/Stream.php

159 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin controller.
4 *
5 * @copyright (c) 2021, Code Atlantic LLC.
6 *
7 * @package ContentControl
8 */
9
10 namespace ContentControl\Base;
11
12 defined( 'ABSPATH' ) || exit;
13
14 /**
15 * HTTP Stream class.
16 */
17 class Stream {
18
19 /**
20 * Stream name.
21 *
22 * @var string
23 */
24 protected $stream_name;
25
26 /**
27 * Version.
28 *
29 * @var string
30 */
31 const VERSION = '1.0.0';
32
33 /**
34 * Stream constructor.
35 *
36 * @param string $stream_name Stream name.
37 */
38 public function __construct( $stream_name = 'stream' ) {
39 $this->stream_name = $stream_name;
40 }
41
42 /**
43 * Start SSE stream.
44 *
45 * @return void
46 */
47 public function start() {
48 if ( headers_sent() ) {
49 // Do not start the stream if headers have already been sent.
50 return;
51 }
52
53 // Disable default disconnect checks.
54 ignore_user_abort( true );
55
56 // phpcs:disable WordPress.PHP.IniSet.Risky, WordPress.PHP.NoSilencedErrors.Discouraged
57 @ini_set( 'zlib.output_compression', '0' );
58 @ini_set( 'implicit_flush', '1' );
59 @ini_set( 'log_limit', '8096' );
60
61 @ob_end_clean();
62 set_time_limit( 0 );
63 // phpcs:enable WordPress.PHP.IniSet.Risky, WordPress.PHP.NoSilencedErrors.Discouraged
64
65 $this->send_headers();
66 }
67
68 /**
69 * Send SSE headers.
70 *
71 * @return void
72 */
73 public function send_headers() {
74 header( 'Content-Type: text/event-stream' );
75 header( 'Stream-Name: ' . $this->stream_name );
76 header( 'Cache-Control: no-cache' );
77 header( 'Connection: keep-alive' );
78 // Nginx: unbuffered responses suitable for Comet and HTTP streaming applications.
79 header( 'X-Accel-Buffering: no' );
80 $this->flush_buffers();
81 }
82
83 /**
84 * Flush buffers.
85 *
86 * Uses a micro delay to prevent the stream from flushing too quickly.
87 *
88 * @return void
89 */
90 protected function flush_buffers() {
91 // This is for the buffer achieve the minimum size in order to flush data.
92
93 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
94 echo str_repeat( ' ', 1024 * 8 ) . PHP_EOL;
95
96 flush(); // Unless both are called. Some browsers will still cache.
97
98 // Neccessary to prevent the stream from flushing too quickly.
99 usleep( 1000 );
100 }
101
102 /**
103 * Send general message/data to the client.
104 *
105 * @param mixed $data Data to send.
106 *
107 * @return void
108 */
109 public function send_data( $data ) {
110 $data = is_string( $data ) ? $data : \wp_json_encode( $data );
111
112 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
113 echo "data: {$data}" . PHP_EOL;
114 echo PHP_EOL;
115
116 $this->flush_buffers();
117 }
118
119 /**
120 * Send an event to the client.
121 *
122 * @param string $event Event name.
123 * @param mixed $data Data to send.
124 *
125 * @return void
126 */
127 public function send_event( $event, $data = '' ) {
128 $data = is_string( $data ) ? $data : \wp_json_encode( $data );
129
130 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
131 echo "event: {$event}" . PHP_EOL;
132 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
133 echo "data: {$data}" . PHP_EOL;
134 echo PHP_EOL;
135
136 $this->flush_buffers();
137 }
138
139 /**
140 * Send an error to the client.
141 *
142 * @param array{message:string}|string $error Error message.
143 *
144 * @return void
145 */
146 public function send_error( $error ) {
147 $this->send_event( 'error', $error );
148 }
149
150 /**
151 * Check if the connection should abort.
152 *
153 * @return bool
154 */
155 public function should_abort() {
156 return (bool) connection_aborted();
157 }
158 }
159