PluginProbe
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More / 2.0.2
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More v2.0.2
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.0.2, at classes/Base/Stream.php

153 lines 3.1 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 // Disable default disconnect checks.
49 ignore_user_abort( true );
50
51 // phpcs:disable WordPress.PHP.IniSet.Risky, WordPress.PHP.NoSilencedErrors.Discouraged
52 @ini_set( 'zlib.output_compression', 0 );
53 @ini_set( 'implicit_flush', 1 );
54 @ini_set( 'log_limit', 8096 );
55 @ob_end_clean();
56 set_time_limit( 0 );
57 // phpcs:enable WordPress.PHP.IniSet.Risky, WordPress.PHP.NoSilencedErrors.Discouraged
58
59 $this->send_headers();
60 }
61
62 /**
63 * Send SSE headers.
64 *
65 * @return void
66 */
67 public function send_headers() {
68 header( 'Content-Type: text/event-stream' );
69 header( 'Stream-Name: ' . $this->stream_name );
70 header( 'Cache-Control: no-cache' );
71 header( 'Connection: keep-alive' );
72 // Nginx: unbuffered responses suitable for Comet and HTTP streaming applications.
73 header( 'X-Accel-Buffering: no' );
74 $this->flush_buffers();
75 }
76
77 /**
78 * Flush buffers.
79 *
80 * Uses a micro delay to prevent the stream from flushing too quickly.
81 *
82 * @return void
83 */
84 protected function flush_buffers() {
85 // This is for the buffer achieve the minimum size in order to flush data.
86
87 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
88 echo str_repeat( ' ', 1024 * 8 ) . PHP_EOL;
89
90 flush(); // Unless both are called. Some browsers will still cache.
91
92 // Neccessary to prevent the stream from flushing too quickly.
93 usleep( 1000 );
94 }
95
96 /**
97 * Send general message/data to the client.
98 *
99 * @param mixed $data Data to send.
100 *
101 * @return void
102 */
103 public function send_data( $data ) {
104 $data = is_string( $data ) ? $data : \wp_json_encode( $data );
105
106 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
107 echo "data: {$data}" . PHP_EOL;
108 echo PHP_EOL;
109
110 $this->flush_buffers();
111 }
112
113 /**
114 * Send an event to the client.
115 *
116 * @param string $event Event name.
117 * @param mixed $data Data to send.
118 *
119 * @return void
120 */
121 public function send_event( $event, $data = '' ) {
122 $data = is_string( $data ) ? $data : \wp_json_encode( $data );
123
124 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
125 echo "event: {$event}" . PHP_EOL;
126 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
127 echo "data: {$data}" . PHP_EOL;
128 echo PHP_EOL;
129
130 $this->flush_buffers();
131 }
132
133 /**
134 * Send an error to the client.
135 *
136 * @param string $error Error message.
137 *
138 * @return void
139 */
140 public function send_error( $error ) {
141 $this->send_event( 'error', $error );
142 }
143
144 /**
145 * Check if the connection should abort.
146 *
147 * @return bool
148 */
149 public function should_abort() {
150 return connection_aborted();
151 }
152 }
153