PluginProbe
Stream – Activity Log & Audit Trail / 2.0.2
Stream – Activity Log & Audit Trail v2.0.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-wp-stream-live-update.php

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

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