PluginProbe
Stream – Activity Log & Audit Trail / 4.0.2
Stream – Activity Log & Audit Trail v4.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
← All changes | classes/class-network.php +95 -72 3.2.24.0.2 View file →
@@ -1,32 +1,58 @@
1 1 <?php
2 +/**
3 + * Manages functionality for the Stream Admin pages on both
4 + * single and multi-sites.
5 + *
6 + * @package WP_Stream
7 + */
8 +
2 9 namespace WP_Stream;
3 10
11 +/**
12 + * Class - Network
13 + */
4 14 class Network {
5 15 /**
6 - * Hold Plugin class
16 + * Holds instance of plugin object
17 + *
7 18 * @var Plugin
8 19 */
9 20 public $plugin;
10 21
22 + /**
23 + * Network page slug
24 + *
25 + * @var string
26 + */
11 27 public $network_settings_page_slug = 'wp_stream_network_settings';
12 28
13 - public $default_settings_page_slug = 'wp_stream_default_settings';
29 + /**
30 + * The option name for the network settings.
31 + *
32 + * @var string
33 + */
34 + public $network_settings_option = 'wp_stream_network';
14 35
15 - function __construct( $plugin ) {
36 + /**
37 + * Class constructor
38 + *
39 + * @param Plugin $plugin Instance of plugin object.
40 + */
41 + public function __construct( $plugin ) {
16 42 $this->plugin = $plugin;
17 43
18 - // Always add default site_id/blog_id params when multisite
44 + // Always add default site_id/blog_id params when multisite.
19 45 if ( is_multisite() ) {
20 46 add_filter( 'wp_stream_query_args', array( $this, 'network_query_args' ) );
21 47 }
22 48
23 - // Bail early if not network-activated
49 + // Bail early if not network-activated.
24 50 if ( ! $this->is_network_activated() ) {
25 51 return;
26 52 }
27 53
28 - // Actions
54 + // Actions.
29 55 add_action( 'init', array( $this, 'ajax_network_admin' ) );
30 56 add_action( 'network_admin_menu', array( $this->plugin->admin, 'register_menu' ) );
31 57 add_action( 'network_admin_menu', array( $this, 'admin_menu_screens' ) );
32 58 add_action( 'admin_menu', array( $this, 'admin_menu_screens' ) );
@@ -34,9 +60,9 @@
34 60 add_action( 'network_admin_notices', array( $this->plugin->admin, 'admin_notices' ) );
35 61 add_action( 'wpmuadminedit', array( $this, 'network_options_action' ) );
36 62 add_action( 'update_site_option_' . $this->plugin->settings->network_options_key, array( $this, 'updated_option_ttl_remove_records' ), 10, 3 );
37 63
38 - // Filters
64 + // Filters.
39 65 add_filter( 'wp_stream_blog_id_logged', array( $this, 'blog_id_logged' ) );
40 66 add_filter( 'wp_stream_admin_page_title', array( $this, 'network_admin_page_title' ) );
41 67 add_filter( 'wp_stream_list_table_screen_id', array( $this, 'list_table_screen_id' ) );
42 68 add_filter( 'wp_stream_list_table_filters', array( $this, 'list_table_filters' ) );
@@ -77,9 +103,9 @@
77 103 *
78 104 * @return object
79 105 */
80 106 public function get_network_blog() {
81 - $blog = new \stdClass;
107 + $blog = new \stdClass();
82 108 $blog->blog_id = 0;
83 109 $blog->blogname = esc_html__( 'Network Admin', 'stream' );
84 110
85 111 return $blog;
@@ -90,13 +116,9 @@
90 116 *
91 117 * @return bool
92 118 */
93 119 public function is_network_activated() {
94 - if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
95 - require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
96 - }
97 -
98 - return is_plugin_active_for_network( $this->plugin->locations['plugin'] );
120 + return $this->plugin->is_network_activated();
99 121 }
100 122
101 123 /**
102 124 * Adds Stream to the admin bar under the "My Sites > Network Admin" menu
@@ -103,9 +125,9 @@
103 125 * if Stream has been network-activated.
104 126 *
105 127 * @action admin_bar_menu
106 128 *
107 - * @param object $admin_bar
129 + * @param object $admin_bar Admin bar object.
108 130 *
109 131 * @return void
110 132 */
111 133 public function network_admin_bar_menu( $admin_bar ) {
@@ -130,9 +152,9 @@
130 152 );
131 153 }
132 154
133 155 /**
134 - * Add Network Settings and Default Settings menu items
156 + * Add Network Settings and Default Settings menu pages
135 157 *
136 158 * @return array
137 159 */
138 160 public function admin_menu_screens() {
@@ -155,11 +177,11 @@
155 177
156 178 /**
157 179 * Remove records when records TTL is shortened
158 180 *
159 - * @param string $option_key
160 - * @param array $old_value
161 - * @param array $new_value
181 + * @param string $option_key Unused.
182 + * @param array $new_value New value.
183 + * @param array $old_value Old value.
162 184 *
163 185 * @action update_option_wp_stream
164 186 * @return void
165 187 */
@@ -170,9 +192,9 @@
170 192
171 193 /**
172 194 * Adjust the action of the settings form when in the Network Admin
173 195 *
174 - * @param $action
196 + * @param string $action Query string.
175 197 *
176 198 * @return string
177 199 */
178 200 public function settings_form_action( $action ) {
@@ -177,9 +199,14 @@
177 199 */
178 200 public function settings_form_action( $action ) {
179 201 if ( is_network_admin() ) {
180 202 $current_page = wp_stream_filter_input( INPUT_GET, 'page' );
181 - $action = add_query_arg( array( 'action' => $current_page ), 'edit.php' );
203 + $action = add_query_arg(
204 + array(
205 + 'action' => $current_page,
206 + ),
207 + 'edit.php'
208 + );
182 209 }
183 210
184 211 return $action;
185 212 }
@@ -186,9 +213,9 @@
186 213
187 214 /**
188 215 * Add a description to each of the Settings pages in the Network Admin
189 216 *
190 - * @param $description
217 + * @param string $description Description of the current page.
191 218 *
192 219 * @return string
193 220 */
194 221 public function settings_form_description( $description ) {
@@ -197,15 +224,10 @@
197 224 }
198 225
199 226 $current_page = wp_stream_filter_input( INPUT_GET, 'page' );
200 227
201 - switch ( $current_page ) {
202 - case $this->network_settings_page_slug :
203 - $description = __( 'These settings apply to all sites on the network.', 'stream' );
204 - break;
205 - case $this->default_settings_page_slug :
206 - $description = __( 'These default settings will apply to new sites created on the network. These settings do not alter existing sites.', 'stream' );
207 - break;
228 + if ( $this->network_settings_page_slug === $current_page ) {
229 + $description = __( 'These settings apply to all sites on the network.', 'stream' );
208 230 }
209 231
210 232 return $description;
211 233 }
@@ -212,9 +234,9 @@
212 234
213 235 /**
214 236 * Adjusts the settings fields displayed in various network admin screens
215 237 *
216 - * @param $fields
238 + * @param array $fields Page settings fields.
217 239 *
218 240 * @return mixed
219 241 */
220 242 public function get_network_admin_fields( $fields ) {
@@ -224,9 +246,9 @@
224 246
225 247 $stream_hidden_options = apply_filters(
226 248 'wp_stream_hidden_option_fields',
227 249 array(
228 - 'general' => array(
250 + 'general' => array(
229 251 'records_ttl',
230 252 ),
231 253 'advanced' => array(
232 254 'delete_all_records',
@@ -251,9 +273,9 @@
251 273 ),
252 274 )
253 275 );
254 276
255 - // Remove settings based on context
277 + // Remove settings based on context.
256 278 if ( $this->plugin->settings->network_options_key === $this->plugin->settings->option_key ) {
257 279 $hidden_options = $network_hidden_options;
258 280 } else {
259 281 $hidden_options = $stream_hidden_options;
@@ -270,9 +292,9 @@
270 292 }
271 293 }
272 294 }
273 295
274 - // Add settings based on context
296 + // Add settings based on context.
275 297 if ( $this->plugin->settings->network_options_key === $this->plugin->settings->option_key ) {
276 298 $new_fields['general']['fields'][] = array(
277 299 'name' => 'site_access',
278 300 'title' => __( 'Site Access', 'stream' ),
@@ -284,9 +306,9 @@
284 306
285 307 $fields = array_merge_recursive( $new_fields, $fields );
286 308 }
287 309
288 - // Remove empty settings sections
310 + // Remove empty settings sections.
289 311 foreach ( $fields as $section_key => $section ) {
290 312 if ( empty( $section['fields'] ) ) {
291 313 unset( $fields[ $section_key ] );
292 314 }
@@ -299,8 +321,10 @@
299 321 * Get translations of serialized Stream Network settings
300 322 *
301 323 * @filter wp_stream_serialized_labels
302 324 *
325 + * @param array $labels Setting labels.
326 + *
303 327 * @return array Multidimensional array of fields
304 328 */
305 329 public function get_settings_translations( $labels ) {
306 330 $network_key = $this->plugin->settings->network_options_key;
@@ -321,48 +345,44 @@
321 345 /**
322 346 * Wrapper for the settings API to work on the network settings page
323 347 */
324 348 public function network_options_action() {
325 - $allowed_referers = array(
326 - $this->network_settings_page_slug,
327 - $this->default_settings_page_slug,
328 - );
329 349
330 - if ( ! isset( $_GET['action'] ) || ! in_array( $_GET['action'], $allowed_referers, true ) ) {
350 + // Check the nonce.
351 + if ( empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], sprintf( '%s-options', $this->network_settings_option ) ) ) {
331 352 return;
332 353 }
333 354
334 - // @codingStandardsIgnoreStart
335 - $options = isset( $_POST['option_page'] ) ? explode( ',', stripslashes( $_POST['option_page'] ) ) : null;
336 - // @codingStandardsIgnoreEnd
355 + // Check the user capability.
356 + if ( ! current_user_can( $this->plugin->admin->settings_cap ) ) {
357 + return;
358 + }
337 359
338 - if ( $options ) {
360 + // Check the action.
361 + if ( ! isset( $_GET['action'] ) || $this->network_settings_page_slug !== $_GET['action'] ) {
362 + return;
363 + }
339 364
340 - foreach ( $options as $option ) {
341 - $option = trim( $option );
342 - $value = null;
343 - $sections = $this->plugin->settings->get_fields();
365 + $option = ! empty( $_POST['option_page'] ) ? $_POST['option_page'] : false;
344 366
345 - foreach ( $sections as $section_name => $section ) {
346 - foreach ( $section['fields'] as $field_idx => $field ) {
347 - $option_key = $section_name . '_' . $field['name'];
367 + if ( $option && $this->network_settings_option === $option ) {
348 368
349 - // @codingStandardsIgnoreStart
350 - if ( isset( $_POST[ $option ][ $option_key ] ) ) {
351 - $value[ $option_key ] = $_POST[ $option ][ $option_key ];
352 - } else {
353 - $value[ $option_key ] = false;
354 - }
355 - // @codingStandardsIgnoreEnd
369 + $value = array();
370 + $sections = $this->plugin->settings->get_fields();
371 +
372 + foreach ( $sections as $section_name => $section ) {
373 + foreach ( $section['fields'] as $field_idx => $field ) {
374 + $option_key = $section_name . '_' . $field['name'];
375 +
376 + if ( isset( $_POST[ $option ][ $option_key ] ) ) {
377 + $value[ $option_key ] = $this->plugin->settings->sanitize_setting_by_field_type( $_POST[ $option ][ $option_key ], $field['type'] );
378 + } else {
379 + $value[ $option_key ] = false;
356 380 }
357 381 }
382 + }
358 383
359 - if ( ! is_array( $value ) ) {
360 - $value = trim( $value );
361 - }
362 -
363 - update_site_option( $option, $value );
364 - }
384 + update_site_option( $this->network_settings_option, $value );
365 385 }
366 386
367 387 if ( ! count( get_settings_errors() ) ) {
368 388 add_settings_error( 'general', 'settings_updated', __( 'Settings saved.', 'stream' ), 'updated' );
@@ -371,9 +391,9 @@
371 391 set_transient( 'settings_errors', get_settings_errors(), 30 );
372 392
373 393 $go_back = add_query_arg( 'settings-updated', 'true', wp_get_referer() );
374 394
375 - wp_redirect( $go_back );
395 + wp_safe_redirect( $go_back );
376 396
377 397 exit;
378 398 }
379 399
@@ -381,9 +401,9 @@
381 401 * Add the Site filter to the Network records screen
382 402 *
383 403 * @filter wp_stream_list_table_filters
384 404 *
385 - * @param $filters
405 + * @param array $filters Filters.
386 406 *
387 407 * @return array
388 408 */
389 409 public function list_table_filters( $filters ) {
@@ -392,9 +412,9 @@
392 412 }
393 413
394 414 $blogs = array();
395 415
396 - // Display network blog as the first option
416 + // Display network blog as the first option.
397 417 $network_blog = $this->get_network_blog();
398 418
399 419 $blogs[ $network_blog->blog_id ] = array(
400 420 'label' => $network_blog->blogname,
@@ -400,9 +420,9 @@
400 420 'label' => $network_blog->blogname,
401 421 'disabled' => '',
402 422 );
403 423
404 - // add all sites
424 + // Add all sites.
405 425 foreach ( wp_stream_get_sites() as $blog ) {
406 426 $blog_data = get_blog_details( $blog->blog_id );
407 427
408 428 $blogs[ $blog->blog_id ] = array(
@@ -421,9 +441,9 @@
421 441
422 442 /**
423 443 * Add the Site toggle to screen options in network admin
424 444 *
425 - * @param $filters
445 + * @param array $filters Filters.
426 446 *
427 447 * @return array
428 448 */
429 449 public function toggle_filters( $filters ) {
@@ -436,9 +456,9 @@
436 456
437 457 /**
438 458 * Add the network suffix to the $screen_id when in the network admin
439 459 *
440 - * @param $screen_id
460 + * @param int $screen_id Screen ID.
441 461 *
442 462 * @return string
443 463 */
444 464 public function list_table_screen_id( $screen_id ) {
@@ -453,8 +473,10 @@
453 473
454 474 /**
455 475 * Set blog_id for network admin activity
456 476 *
477 + * @param int $blog_id Blog ID.
478 + *
457 479 * @return int
458 480 */
459 481 public function blog_id_logged( $blog_id ) {
460 482 return is_network_admin() ? 0 : $blog_id;
@@ -464,9 +486,9 @@
464 486 * Customize query args on multisite installs
465 487 *
466 488 * @filter wp_stream_query_args
467 489 *
468 - * @param array $args
490 + * @param array $args Site arguments.
469 491 *
470 492 * @return array
471 493 */
472 494 public function network_query_args( $args ) {
@@ -480,14 +502,15 @@
480 502 * Add site count to the page title in the network admin
481 503 *
482 504 * @filter wp_stream_admin_page_title
483 505 *
484 - * @param string $page_title
506 + * @param string $page_title Page title.
485 507 *
486 508 * @return string
487 509 */
488 510 public function network_admin_page_title( $page_title ) {
489 511 if ( is_network_admin() ) {
512 + /* translators: %d: number of sites on the network (e.g. "42") */
490 513 $site_count = sprintf( _n( '%d site', '%d sites', get_blog_count(), 'stream' ), number_format( get_blog_count() ) );
491 514 $page_title = sprintf( '%s (%s)', $page_title, $site_count );
492 515 }
493 516
@@ -496,9 +519,9 @@
496 519
497 520 /**
498 521 * Add the Site column to the network stream records
499 522 *
500 - * @param $columns
523 + * @param array $columns Columns data.
501 524 *
502 525 * @return mixed
503 526 */
504 527 public function network_admin_columns( $columns ) {
@@ -517,9 +540,9 @@
517 540
518 541 /**
519 542 * Prevent the Blogs connector from loading when not in Network Admin
520 543 *
521 - * @param $connectors
544 + * @param array $connectors Connectors.
522 545 *
523 546 * @return mixed
524 547 */
525 548 public function hide_blogs_connector( $connectors ) {