| @@ -1,19 +1,10 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * Processes update calls from the Stream Records page. | |
| 4 | - * | |
| 5 | - * @package WP_Stream | |
| 6 | - */ | |
| 7 | - | |
| 8 | 2 | namespace WP_Stream; |
| 9 | 3 | |
| 10 | -/** | |
| 11 | - * Class - Live_Update | |
| 12 | - */ | |
| 13 | 4 | class Live_Update { |
| 14 | 5 | /** |
| 15 | - * Holds instance of plugin object | |
| 6 | + * Hold Plugin class | |
| 16 | 7 | * |
| 17 | 8 | * @var Plugin |
| 18 | 9 | */ |
| 19 | 10 | public $plugin; |
| @@ -34,17 +25,17 @@ | ||
| 34 | 25 | |
| 35 | 26 | /** |
| 36 | 27 | * Class constructor. |
| 37 | 28 | * |
| 38 | - * @param Plugin $plugin Instance of plugin object. | |
| 29 | + * @param Plugin $plugin The main Plugin class. | |
| 39 | 30 | */ |
| 40 | 31 | public function __construct( $plugin ) { |
| 41 | 32 | $this->plugin = $plugin; |
| 42 | 33 | |
| 43 | - // Heartbeat live update. | |
| 34 | + // Heartbeat live update | |
| 44 | 35 | add_filter( 'heartbeat_received', array( $this, 'heartbeat_received' ), 10, 2 ); |
| 45 | 36 | |
| 46 | - // Enable / Disable live update per user. | |
| 37 | + // Enable / Disable live update per user | |
| 47 | 38 | add_action( 'wp_ajax_stream_enable_live_update', array( $this, 'enable_live_update' ) ); |
| 48 | 39 | } |
| 49 | 40 | |
| 50 | 41 | /** |
| @@ -54,18 +45,26 @@ | ||
| 54 | 45 | */ |
| 55 | 46 | public function enable_live_update() { |
| 56 | 47 | check_ajax_referer( $this->user_meta_key . '_nonce', 'nonce' ); |
| 57 | 48 | |
| 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' ) ); | |
| 49 | + $input = array( | |
| 50 | + 'checked' => FILTER_SANITIZE_STRING, | |
| 51 | + 'user' => FILTER_SANITIZE_STRING, | |
| 52 | + 'heartbeat' => FILTER_SANITIZE_STRING, | |
| 53 | + ); | |
| 54 | + | |
| 55 | + $input = filter_input_array( INPUT_POST, $input ); | |
| 56 | + | |
| 57 | + if ( false === $input ) { | |
| 58 | + wp_send_json_error( 'Error in live update checkbox' ); | |
| 60 | 59 | } |
| 61 | 60 | |
| 62 | - $checked = ( 'checked' === wp_stream_filter_input( INPUT_POST, 'checked' ) ) ? 'on' : 'off'; | |
| 61 | + $checked = ( 'checked' === $input['checked'] ) ? 'on' : 'off'; | |
| 63 | 62 | |
| 64 | - $user = get_current_user_id(); | |
| 63 | + $user = (int) $input['user']; | |
| 65 | 64 | |
| 66 | - if ( 'false' === wp_stream_filter_input( INPUT_POST, 'heartbeat' ) ) { | |
| 67 | - update_user_meta( $user, $this->user_meta_key, 'off' ); | |
| 65 | + if ( 'false' === $input['heartbeat'] ) { | |
| 66 | + $this->plugin->admin->update_user_meta( $user, $this->user_meta_key, 'off' ); | |
| 68 | 67 | |
| 69 | 68 | 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 | 69 | |
| 71 | 70 | return; |
| @@ -70,9 +69,9 @@ | ||
| 70 | 69 | |
| 71 | 70 | return; |
| 72 | 71 | } |
| 73 | 72 | |
| 74 | - $success = update_user_meta( $user, $this->user_meta_key, $checked ); | |
| 73 | + $success = $this->plugin->admin->update_user_meta( $user, $this->user_meta_key, $checked ); | |
| 75 | 74 | |
| 76 | 75 | if ( $success ) { |
| 77 | 76 | wp_send_json_success( ( 'on' === $checked ) ? 'Live Updates enabled' : 'Live Updates disabled' ); |
| 78 | 77 | } else { |
| @@ -87,10 +86,10 @@ | ||
| 87 | 86 | * |
| 88 | 87 | * @uses gather_updated_items |
| 89 | 88 | * @uses generate_row |
| 90 | 89 | * |
| 91 | - * @param array $response Response to heartbeat. | |
| 92 | - * @param array $data Data from heartbeat. | |
| 90 | + * @param array $response Response to heartbeat | |
| 91 | + * @param array $data Data from heartbeat | |
| 93 | 92 | * |
| 94 | 93 | * @return array Data sent to heartbeat |
| 95 | 94 | */ |
| 96 | 95 | public function live_update( $response, $data ) { |
| @@ -106,9 +105,9 @@ | ||
| 106 | 105 | if ( empty( $query ) ) { |
| 107 | 106 | $query = array(); |
| 108 | 107 | } |
| 109 | 108 | |
| 110 | - // Decode the query. | |
| 109 | + // Decode the query | |
| 111 | 110 | $query = json_decode( wp_kses_stripslashes( $query ) ); |
| 112 | 111 | |
| 113 | 112 | $updated_items = $this->gather_updated_items( $last_time, (array) $query ); |
| 114 | 113 | |
| @@ -129,10 +128,10 @@ | ||
| 129 | 128 | |
| 130 | 129 | /** |
| 131 | 130 | * Sends Updated Actions to the List Table View |
| 132 | 131 | * |
| 133 | - * @param int $last_time Timestamp of last update. | |
| 134 | - * @param array $args Query args. | |
| 132 | + * @param int $last_time Timestamp of last update | |
| 133 | + * @param array $args Query args | |
| 135 | 134 | * |
| 136 | 135 | * @return array Array of recently updated items |
| 137 | 136 | */ |
| 138 | 137 | public function gather_updated_items( $last_time, $args = array() ) { |
| @@ -161,30 +160,24 @@ | ||
| 161 | 160 | |
| 162 | 161 | /** |
| 163 | 162 | * Handles live updates for Stream Post List |
| 164 | 163 | * |
| 165 | - * @action heartbeat_received | |
| 164 | + * @action heartbeat_recieved | |
| 166 | 165 | * |
| 167 | - * @param array $response Response to be sent to heartbeat tick. | |
| 168 | - * @param array $data Data from heartbeat send. | |
| 166 | + * @param array $response Response to be sent to heartbeat tick | |
| 167 | + * @param array $data Data from heartbeat send | |
| 169 | 168 | * |
| 170 | 169 | * @return array Data sent to heartbeat tick |
| 171 | 170 | */ |
| 172 | 171 | public function heartbeat_received( $response, $data ) { |
| 173 | - // Only fire when Stream is requesting a live update. | |
| 172 | + // Only fire when Stream is requesting a live update | |
| 174 | 173 | if ( ! isset( $data['wp-stream-heartbeat'] ) ) { |
| 175 | 174 | return $response; |
| 176 | 175 | } |
| 177 | 176 | |
| 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 | - } | |
| 177 | + $enable_stream_update = ( 'off' !== $this->plugin->admin->get_user_meta( get_current_user_id(), $this->user_meta_key ) ); | |
| 183 | 178 | |
| 184 | - $enable_stream_update = ( 'off' !== get_user_meta( get_current_user_id(), $this->user_meta_key, true ) ); | |
| 185 | - | |
| 186 | - // Register list table. | |
| 179 | + // Register list table | |
| 187 | 180 | $this->list_table = new List_Table( |
| 188 | 181 | $this->plugin, |
| 189 | 182 | array( |
| 190 | 183 | 'screen' => 'toplevel_page_' . $this->plugin->admin->records_page_slug, |
| @@ -196,10 +189,10 @@ | ||
| 196 | 189 | $total_pages = isset( $this->list_table->_pagination_args['total_pages'] ) ? $this->list_table->_pagination_args['total_pages'] : null; |
| 197 | 190 | |
| 198 | 191 | if ( isset( $data['wp-stream-heartbeat'] ) && isset( $total_items ) ) { |
| 199 | 192 | $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 ) ); | |
| 193 | + // translators: Placeholder refers to a number of items (e.g. "42") | |
| 194 | + $response['total_items_i18n'] = sprintf( _n( '%d item', '%d items', $total_items ), number_format_i18n( $total_items ) ); | |
| 202 | 195 | } |
| 203 | 196 | |
| 204 | 197 | if ( isset( $data['wp-stream-heartbeat'] ) && 'live-update' === $data['wp-stream-heartbeat'] && $enable_stream_update ) { |
| 205 | 198 | |