PluginProbe
Stream – Activity Log & Audit Trail / 3.3.0
Stream – Activity Log & Audit Trail v3.3.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.3.0, at classes/class-live-update.php

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