PluginProbe
Stream – Activity Log & Audit Trail / 3.9.0
Stream – Activity Log & Audit Trail v3.9.0
4.4.0 4.3.0 4.2.2 4.2.1 trunk 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.1 3.1.1 3.10.0 3.2.0 3.2.1 3.2.2 3.2.3 All 50 releases
stream / classes / class-live-update.php

class-live-update.php in Stream – Activity Log & Audit Trail 3.9.0, at classes/class-live-update.php

231 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Processes update calls from the Stream Records page.
4 *
5 * @package WP_Stream
6 */
7
8 namespace WP_Stream;
9
10 /**
11 * Class - Live_Update
12 */
13 class Live_Update {
14 /**
15 * Holds instance of plugin object
16 *
17 * @var Plugin
18 */
19 public $plugin;
20
21 /**
22 * User meta key/identifier
23 *
24 * @var string
25 */
26 public $user_meta_key = 'stream_live_update_records';
27
28 /**
29 * List table object instance
30 *
31 * @var List_Table
32 */
33 public $list_table = null;
34
35 /**
36 * Class constructor.
37 *
38 * @param Plugin $plugin Instance of plugin object.
39 */
40 public function __construct( $plugin ) {
41 $this->plugin = $plugin;
42
43 // Heartbeat live update.
44 add_filter( 'heartbeat_received', array( $this, 'heartbeat_received' ), 10, 2 );
45
46 // Enable / Disable live update per user.
47 add_action( 'wp_ajax_stream_enable_live_update', array( $this, 'enable_live_update' ) );
48 }
49
50 /**
51 * Ajax function to enable/disable live update
52 *
53 * @return string Ajax respsonse back in JSON format
54 */
55 public function enable_live_update() {
56 check_ajax_referer( $this->user_meta_key . '_nonce', 'nonce' );
57
58 $input = array(
59 'checked' => FILTER_SANITIZE_STRING,
60 'user' => FILTER_SANITIZE_STRING,
61 'heartbeat' => FILTER_SANITIZE_STRING,
62 );
63
64 $input = filter_input_array( INPUT_POST, $input );
65
66 if ( false === $input ) {
67 wp_send_json_error( 'Error in live update checkbox' );
68 }
69
70 $checked = ( 'checked' === $input['checked'] ) ? 'on' : 'off';
71
72 $user = (int) $input['user'];
73
74 if ( 'false' === $input['heartbeat'] ) {
75 $this->plugin->admin->update_user_meta( $user, $this->user_meta_key, 'off' );
76
77 wp_send_json_error( esc_html__( "Live updates could not be enabled because Heartbeat is not loaded.\n\nYour hosting provider or another plugin may have disabled it for performance reasons.", 'stream' ) );
78
79 return;
80 }
81
82 $success = $this->plugin->admin->update_user_meta( $user, $this->user_meta_key, $checked );
83
84 if ( $success ) {
85 wp_send_json_success( ( 'on' === $checked ) ? 'Live Updates enabled' : 'Live Updates disabled' );
86 } else {
87 wp_send_json_error( 'Live Updates checkbox error' );
88 }
89 }
90
91 /**
92 * Sends updated actions to the list table view
93 *
94 * @todo Fix reliability issues with sidebar widgets
95 *
96 * @uses gather_updated_items
97 * @uses generate_row
98 *
99 * @param array $response Response to heartbeat.
100 * @param array $data Data from heartbeat.
101 *
102 * @return array Data sent to heartbeat
103 */
104 public function live_update( $response, $data ) {
105 unset( $response );
106
107 if ( ! isset( $data['wp-stream-heartbeat-last-time'] ) ) {
108 return array();
109 }
110
111 $last_time = $data['wp-stream-heartbeat-last-time'];
112 $query = $data['wp-stream-heartbeat-query'];
113
114 if ( empty( $query ) ) {
115 $query = array();
116 }
117
118 // Decode the query.
119 $query = json_decode( wp_kses_stripslashes( $query ) );
120
121 $updated_items = $this->gather_updated_items( $last_time, (array) $query );
122
123 if ( ! empty( $updated_items ) ) {
124 ob_start();
125
126 foreach ( $updated_items as $item ) {
127 $this->list_table->single_row( $item );
128 }
129
130 $send = ob_get_clean();
131 } else {
132 $send = '';
133 }
134
135 return $send;
136 }
137
138 /**
139 * Sends Updated Actions to the List Table View
140 *
141 * @param int $last_time Timestamp of last update.
142 * @param array $args Query args.
143 *
144 * @return array Array of recently updated items
145 */
146 public function gather_updated_items( $last_time, $args = array() ) {
147 unset( $args );
148
149 if ( false === $last_time ) {
150 return '';
151 }
152
153 if ( empty( $this->list_table->items ) ) {
154 return '';
155 }
156
157 $items = array();
158
159 foreach ( $this->list_table->items as $item ) {
160 if ( strtotime( $item->created ) > strtotime( $last_time ) ) {
161 $items[] = $item;
162 } else {
163 break;
164 }
165 }
166
167 return $items;
168 }
169
170 /**
171 * Handles live updates for Stream Post List
172 *
173 * @action heartbeat_recieved
174 *
175 * @param array $response Response to be sent to heartbeat tick.
176 * @param array $data Data from heartbeat send.
177 *
178 * @return array Data sent to heartbeat tick
179 */
180 public function heartbeat_received( $response, $data ) {
181 // Only fire when Stream is requesting a live update.
182 if ( ! isset( $data['wp-stream-heartbeat'] ) ) {
183 return $response;
184 }
185
186 $enable_stream_update = ( 'off' !== $this->plugin->admin->get_user_meta( get_current_user_id(), $this->user_meta_key ) );
187
188 // Register list table.
189 $this->list_table = new List_Table(
190 $this->plugin,
191 array(
192 'screen' => 'toplevel_page_' . $this->plugin->admin->records_page_slug,
193 )
194 );
195 $this->list_table->prepare_items();
196
197 $total_items = isset( $this->list_table->_pagination_args['total_items'] ) ? $this->list_table->_pagination_args['total_items'] : null;
198 $total_pages = isset( $this->list_table->_pagination_args['total_pages'] ) ? $this->list_table->_pagination_args['total_pages'] : null;
199
200 if ( isset( $data['wp-stream-heartbeat'] ) && isset( $total_items ) ) {
201 $response['total_items'] = $total_items;
202 /* translators: %d: number of items (e.g. "42") */
203 $response['total_items_i18n'] = sprintf( _n( '%d item', '%d items', $total_items ), number_format_i18n( $total_items ) );
204 }
205
206 if ( isset( $data['wp-stream-heartbeat'] ) && 'live-update' === $data['wp-stream-heartbeat'] && $enable_stream_update ) {
207
208 if ( ! empty( $data['wp-stream-heartbeat'] ) ) {
209 if ( isset( $total_pages ) ) {
210 $response['total_pages'] = $total_pages;
211 $response['total_pages_i18n'] = number_format_i18n( $total_pages );
212
213 $query_args = json_decode( $data['wp-stream-heartbeat-query'], true );
214 $query_args['paged'] = $total_pages;
215
216 $response['last_page_link'] = esc_url( add_query_arg( $query_args, admin_url( 'admin.php' ) ) );
217 } else {
218 $response['total_pages'] = 0;
219 }
220 }
221
222 $response['wp-stream-heartbeat'] = $this->live_update( $response, $data );
223
224 } else {
225 $response['log'] = 'fail';
226 }
227
228 return $response;
229 }
230 }
231