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