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 +88 -88 3.2.34.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
36 + /**
37 + * Class constructor
38 + *
39 + * @param Plugin $plugin Instance of plugin object.
40 + */
15 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' ) );
@@ -90,42 +116,18 @@
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 - if ( $this->is_mustuse() ) {
99 - return true;
100 - }
101 -
102 - return is_plugin_active_for_network( $this->plugin->locations['plugin'] );
120 + return $this->plugin->is_network_activated();
103 121 }
104 122
105 123 /**
106 - * Returns true if Stream is a must-use plugin, otherwise false
107 - *
108 - * @return bool
109 - */
110 - public function is_mustuse() {
111 -
112 - $stream_php = trailingslashit( WPMU_PLUGIN_DIR ) . $this->plugin->locations['plugin'];
113 -
114 - if ( file_exists( $stream_php ) && class_exists( 'WP_Stream\Plugin' ) ) {
115 - return true;
116 - }
117 -
118 - return false;
119 - }
120 -
121 - /**
122 124 * Adds Stream to the admin bar under the "My Sites > Network Admin" menu
123 125 * if Stream has been network-activated.
124 126 *
125 127 * @action admin_bar_menu
126 128 *
127 - * @param object $admin_bar
129 + * @param object $admin_bar Admin bar object.
128 130 *
129 131 * @return void
130 132 */
131 133 public function network_admin_bar_menu( $admin_bar ) {
@@ -150,9 +152,9 @@
150 152 );
151 153 }
152 154
153 155 /**
154 - * Add Network Settings and Default Settings menu items
156 + * Add Network Settings and Default Settings menu pages
155 157 *
156 158 * @return array
157 159 */
158 160 public function admin_menu_screens() {
@@ -175,11 +177,11 @@
175 177
176 178 /**
177 179 * Remove records when records TTL is shortened
178 180 *
179 - * @param string $option_key
180 - * @param array $old_value
181 - * @param array $new_value
181 + * @param string $option_key Unused.
182 + * @param array $new_value New value.
183 + * @param array $old_value Old value.
182 184 *
183 185 * @action update_option_wp_stream
184 186 * @return void
185 187 */
@@ -190,9 +192,9 @@
190 192
191 193 /**
192 194 * Adjust the action of the settings form when in the Network Admin
193 195 *
194 - * @param $action
196 + * @param string $action Query string.
195 197 *
196 198 * @return string
197 199 */
198 200 public function settings_form_action( $action ) {
@@ -200,9 +202,10 @@
200 202 $current_page = wp_stream_filter_input( INPUT_GET, 'page' );
201 203 $action = add_query_arg(
202 204 array(
203 205 'action' => $current_page,
204 - ), 'edit.php'
206 + ),
207 + 'edit.php'
205 208 );
206 209 }
207 210
208 211 return $action;
@@ -210,9 +213,9 @@
210 213
211 214 /**
212 215 * Add a description to each of the Settings pages in the Network Admin
213 216 *
214 - * @param $description
217 + * @param string $description Description of the current page.
215 218 *
216 219 * @return string
217 220 */
218 221 public function settings_form_description( $description ) {
@@ -221,15 +224,10 @@
221 224 }
222 225
223 226 $current_page = wp_stream_filter_input( INPUT_GET, 'page' );
224 227
225 - switch ( $current_page ) {
226 - case $this->network_settings_page_slug:
227 - $description = __( 'These settings apply to all sites on the network.', 'stream' );
228 - break;
229 - case $this->default_settings_page_slug:
230 - $description = __( 'These default settings will apply to new sites created on the network. These settings do not alter existing sites.', 'stream' );
231 - break;
228 + if ( $this->network_settings_page_slug === $current_page ) {
229 + $description = __( 'These settings apply to all sites on the network.', 'stream' );
232 230 }
233 231
234 232 return $description;
235 233 }
@@ -236,9 +234,9 @@
236 234
237 235 /**
238 236 * Adjusts the settings fields displayed in various network admin screens
239 237 *
240 - * @param $fields
238 + * @param array $fields Page settings fields.
241 239 *
242 240 * @return mixed
243 241 */
244 242 public function get_network_admin_fields( $fields ) {
@@ -275,9 +273,9 @@
275 273 ),
276 274 )
277 275 );
278 276
279 - // Remove settings based on context
277 + // Remove settings based on context.
280 278 if ( $this->plugin->settings->network_options_key === $this->plugin->settings->option_key ) {
281 279 $hidden_options = $network_hidden_options;
282 280 } else {
283 281 $hidden_options = $stream_hidden_options;
@@ -294,9 +292,9 @@
294 292 }
295 293 }
296 294 }
297 295
298 - // Add settings based on context
296 + // Add settings based on context.
299 297 if ( $this->plugin->settings->network_options_key === $this->plugin->settings->option_key ) {
300 298 $new_fields['general']['fields'][] = array(
301 299 'name' => 'site_access',
302 300 'title' => __( 'Site Access', 'stream' ),
@@ -308,9 +306,9 @@
308 306
309 307 $fields = array_merge_recursive( $new_fields, $fields );
310 308 }
311 309
312 - // Remove empty settings sections
310 + // Remove empty settings sections.
313 311 foreach ( $fields as $section_key => $section ) {
314 312 if ( empty( $section['fields'] ) ) {
315 313 unset( $fields[ $section_key ] );
316 314 }
@@ -323,8 +321,10 @@
323 321 * Get translations of serialized Stream Network settings
324 322 *
325 323 * @filter wp_stream_serialized_labels
326 324 *
325 + * @param array $labels Setting labels.
326 + *
327 327 * @return array Multidimensional array of fields
328 328 */
329 329 public function get_settings_translations( $labels ) {
330 330 $network_key = $this->plugin->settings->network_options_key;
@@ -345,46 +345,44 @@
345 345 /**
346 346 * Wrapper for the settings API to work on the network settings page
347 347 */
348 348 public function network_options_action() {
349 - $allowed_referers = array(
350 - $this->network_settings_page_slug,
351 - $this->default_settings_page_slug,
352 - );
353 349
354 - if ( ! isset( $_GET['action'] ) || ! in_array( $_GET['action'], $allowed_referers, true ) ) { // CSRF okay
350 + // Check the nonce.
351 + if ( empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], sprintf( '%s-options', $this->network_settings_option ) ) ) {
355 352 return;
356 353 }
357 354
358 - $options = isset( $_POST['option_page'] ) ? explode( ',', stripslashes( $_POST['option_page'] ) ) : null; // CSRF okay
355 + // Check the user capability.
356 + if ( ! current_user_can( $this->plugin->admin->settings_cap ) ) {
357 + return;
358 + }
359 359
360 - if ( $options ) {
360 + // Check the action.
361 + if ( ! isset( $_GET['action'] ) || $this->network_settings_page_slug !== $_GET['action'] ) {
362 + return;
363 + }
361 364
362 - foreach ( $options as $option ) {
363 - $option = trim( $option );
364 - $value = null;
365 - $sections = $this->plugin->settings->get_fields();
365 + $option = ! empty( $_POST['option_page'] ) ? $_POST['option_page'] : false;
366 366
367 - foreach ( $sections as $section_name => $section ) {
368 - foreach ( $section['fields'] as $field_idx => $field ) {
369 - $option_key = $section_name . '_' . $field['name'];
367 + if ( $option && $this->network_settings_option === $option ) {
370 368
371 - // @codingStandardsIgnoreStart
372 - if ( isset( $_POST[ $option ][ $option_key ] ) ) {
373 - $value[ $option_key ] = $_POST[ $option ][ $option_key ];
374 - } else {
375 - $value[ $option_key ] = false;
376 - }
377 - // @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;
378 380 }
379 381 }
382 + }
380 383
381 - if ( ! is_array( $value ) ) {
382 - $value = trim( $value );
383 - }
384 -
385 - update_site_option( $option, $value );
386 - }
384 + update_site_option( $this->network_settings_option, $value );
387 385 }
388 386
389 387 if ( ! count( get_settings_errors() ) ) {
390 388 add_settings_error( 'general', 'settings_updated', __( 'Settings saved.', 'stream' ), 'updated' );
@@ -393,9 +391,9 @@
393 391 set_transient( 'settings_errors', get_settings_errors(), 30 );
394 392
395 393 $go_back = add_query_arg( 'settings-updated', 'true', wp_get_referer() );
396 394
397 - wp_redirect( $go_back );
395 + wp_safe_redirect( $go_back );
398 396
399 397 exit;
400 398 }
401 399
@@ -403,9 +401,9 @@
403 401 * Add the Site filter to the Network records screen
404 402 *
405 403 * @filter wp_stream_list_table_filters
406 404 *
407 - * @param $filters
405 + * @param array $filters Filters.
408 406 *
409 407 * @return array
410 408 */
411 409 public function list_table_filters( $filters ) {
@@ -414,9 +412,9 @@
414 412 }
415 413
416 414 $blogs = array();
417 415
418 - // Display network blog as the first option
416 + // Display network blog as the first option.
419 417 $network_blog = $this->get_network_blog();
420 418
421 419 $blogs[ $network_blog->blog_id ] = array(
422 420 'label' => $network_blog->blogname,
@@ -422,9 +420,9 @@
422 420 'label' => $network_blog->blogname,
423 421 'disabled' => '',
424 422 );
425 423
426 - // add all sites
424 + // Add all sites.
427 425 foreach ( wp_stream_get_sites() as $blog ) {
428 426 $blog_data = get_blog_details( $blog->blog_id );
429 427
430 428 $blogs[ $blog->blog_id ] = array(
@@ -443,9 +441,9 @@
443 441
444 442 /**
445 443 * Add the Site toggle to screen options in network admin
446 444 *
447 - * @param $filters
445 + * @param array $filters Filters.
448 446 *
449 447 * @return array
450 448 */
451 449 public function toggle_filters( $filters ) {
@@ -458,9 +456,9 @@
458 456
459 457 /**
460 458 * Add the network suffix to the $screen_id when in the network admin
461 459 *
462 - * @param $screen_id
460 + * @param int $screen_id Screen ID.
463 461 *
464 462 * @return string
465 463 */
466 464 public function list_table_screen_id( $screen_id ) {
@@ -475,8 +473,10 @@
475 473
476 474 /**
477 475 * Set blog_id for network admin activity
478 476 *
477 + * @param int $blog_id Blog ID.
478 + *
479 479 * @return int
480 480 */
481 481 public function blog_id_logged( $blog_id ) {
482 482 return is_network_admin() ? 0 : $blog_id;
@@ -486,9 +486,9 @@
486 486 * Customize query args on multisite installs
487 487 *
488 488 * @filter wp_stream_query_args
489 489 *
490 - * @param array $args
490 + * @param array $args Site arguments.
491 491 *
492 492 * @return array
493 493 */
494 494 public function network_query_args( $args ) {
@@ -502,15 +502,15 @@
502 502 * Add site count to the page title in the network admin
503 503 *
504 504 * @filter wp_stream_admin_page_title
505 505 *
506 - * @param string $page_title
506 + * @param string $page_title Page title.
507 507 *
508 508 * @return string
509 509 */
510 510 public function network_admin_page_title( $page_title ) {
511 511 if ( is_network_admin() ) {
512 - // translators: Placeholder refers to a number of sites on the network (e.g. "42")
512 + /* translators: %d: number of sites on the network (e.g. "42") */
513 513 $site_count = sprintf( _n( '%d site', '%d sites', get_blog_count(), 'stream' ), number_format( get_blog_count() ) );
514 514 $page_title = sprintf( '%s (%s)', $page_title, $site_count );
515 515 }
516 516
@@ -519,9 +519,9 @@
519 519
520 520 /**
521 521 * Add the Site column to the network stream records
522 522 *
523 - * @param $columns
523 + * @param array $columns Columns data.
524 524 *
525 525 * @return mixed
526 526 */
527 527 public function network_admin_columns( $columns ) {
@@ -540,9 +540,9 @@
540 540
541 541 /**
542 542 * Prevent the Blogs connector from loading when not in Network Admin
543 543 *
544 - * @param $connectors
544 + * @param array $connectors Connectors.
545 545 *
546 546 * @return mixed
547 547 */
548 548 public function hide_blogs_connector( $connectors ) {