PluginProbe
Stream – Activity Log & Audit Trail / 4.2.2
Stream – Activity Log & Audit Trail v4.2.2
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 4.2.2, at classes/class-live-update.php

229 lines 5.9 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 if ( ! current_user_can( $this->plugin->admin->view_cap ) ) {
59 wp_send_json_error( esc_html__( 'You do not have permission to do this.', 'stream' ) );
60 }
61
62 $checked = ( 'checked' === wp_stream_filter_input( INPUT_POST, 'checked' ) ) ? 'on' : 'off';
63
64 $user = get_current_user_id();
65
66 if ( 'false' === wp_stream_filter_input( INPUT_POST, 'heartbeat' ) ) {
67 update_user_meta( $user, $this->user_meta_key, 'off' );
68
69 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' ) );
70
71 return;
72 }
73
74 $success = update_user_meta( $user, $this->user_meta_key, $checked );
75
76 if ( $success ) {
77 wp_send_json_success( ( 'on' === $checked ) ? 'Live Updates enabled' : 'Live Updates disabled' );
78 } else {
79 wp_send_json_error( 'Live Updates checkbox error' );
80 }
81 }
82
83 /**
84 * Sends updated actions to the list table view
85 *
86 * @todo Fix reliability issues with sidebar widgets
87 *
88 * @uses gather_updated_items
89 * @uses generate_row
90 *
91 * @param array $response Response to heartbeat.
92 * @param array $data Data from heartbeat.
93 *
94 * @return array Data sent to heartbeat
95 */
96 public function live_update( $response, $data ) {
97 unset( $response );
98
99 if ( ! isset( $data['wp-stream-heartbeat-last-time'] ) ) {
100 return array();
101 }
102
103 $last_time = $data['wp-stream-heartbeat-last-time'];
104 $query = $data['wp-stream-heartbeat-query'];
105
106 if ( empty( $query ) ) {
107 $query = array();
108 }
109
110 // Decode the query.
111 $query = json_decode( wp_kses_stripslashes( $query ) );
112
113 $updated_items = $this->gather_updated_items( $last_time, (array) $query );
114
115 if ( ! empty( $updated_items ) ) {
116 ob_start();
117
118 foreach ( $updated_items as $item ) {
119 $this->list_table->single_row( $item );
120 }
121
122 $send = ob_get_clean();
123 } else {
124 $send = '';
125 }
126
127 return $send;
128 }
129
130 /**
131 * Sends Updated Actions to the List Table View
132 *
133 * @param int $last_time Timestamp of last update.
134 * @param array $args Query args.
135 *
136 * @return array Array of recently updated items
137 */
138 public function gather_updated_items( $last_time, $args = array() ) {
139 unset( $args );
140
141 if ( false === $last_time ) {
142 return '';
143 }
144
145 if ( empty( $this->list_table->items ) ) {
146 return '';
147 }
148
149 $items = array();
150
151 foreach ( $this->list_table->items as $item ) {
152 if ( strtotime( $item->created ) > strtotime( $last_time ) ) {
153 $items[] = $item;
154 } else {
155 break;
156 }
157 }
158
159 return $items;
160 }
161
162 /**
163 * Handles live updates for Stream Post List
164 *
165 * @action heartbeat_received
166 *
167 * @param array $response Response to be sent to heartbeat tick.
168 * @param array $data Data from heartbeat send.
169 *
170 * @return array Data sent to heartbeat tick
171 */
172 public function heartbeat_received( $response, $data ) {
173 // Only fire when Stream is requesting a live update.
174 if ( ! isset( $data['wp-stream-heartbeat'] ) ) {
175 return $response;
176 }
177
178 // Ensure the current user is allowed to view Stream records before
179 // exposing any activity data through the Heartbeat API.
180 if ( ! current_user_can( $this->plugin->admin->view_cap ) ) {
181 return $response;
182 }
183
184 $enable_stream_update = ( 'off' !== get_user_meta( get_current_user_id(), $this->user_meta_key, true ) );
185
186 // Register list table.
187 $this->list_table = new List_Table(
188 $this->plugin,
189 array(
190 'screen' => 'toplevel_page_' . $this->plugin->admin->records_page_slug,
191 )
192 );
193 $this->list_table->prepare_items();
194
195 $total_items = isset( $this->list_table->_pagination_args['total_items'] ) ? $this->list_table->_pagination_args['total_items'] : null;
196 $total_pages = isset( $this->list_table->_pagination_args['total_pages'] ) ? $this->list_table->_pagination_args['total_pages'] : null;
197
198 if ( isset( $data['wp-stream-heartbeat'] ) && isset( $total_items ) ) {
199 $response['total_items'] = $total_items;
200 /* translators: %d: number of items (e.g. "42") */
201 $response['total_items_i18n'] = sprintf( _n( '%d item', '%d items', $total_items, 'stream' ), number_format_i18n( $total_items ) );
202 }
203
204 if ( isset( $data['wp-stream-heartbeat'] ) && 'live-update' === $data['wp-stream-heartbeat'] && $enable_stream_update ) {
205
206 if ( ! empty( $data['wp-stream-heartbeat'] ) ) {
207 if ( isset( $total_pages ) ) {
208 $response['total_pages'] = $total_pages;
209 $response['total_pages_i18n'] = number_format_i18n( $total_pages );
210
211 $query_args = json_decode( $data['wp-stream-heartbeat-query'], true );
212 $query_args['paged'] = $total_pages;
213
214 $response['last_page_link'] = esc_url( add_query_arg( $query_args, admin_url( 'admin.php' ) ) );
215 } else {
216 $response['total_pages'] = 0;
217 }
218 }
219
220 $response['wp-stream-heartbeat'] = $this->live_update( $response, $data );
221
222 } else {
223 $response['log'] = 'fail';
224 }
225
226 return $response;
227 }
228 }
229