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 +102 -73 3.14.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' ) );
@@ -50,8 +76,10 @@
50 76
51 77 /**
52 78 * Workaround to get admin-ajax.php to know when the request is from the Network Admin
53 79 *
80 + * @return bool
81 + *
54 82 * @action init
55 83 *
56 84 * @see https://core.trac.wordpress.org/ticket/22589
57 85 */
@@ -63,9 +91,12 @@
63 91 &&
64 92 preg_match( '#^' . network_admin_url() . '#i', $_SERVER['HTTP_REFERER'] )
65 93 ) {
66 94 define( 'WP_NETWORK_ADMIN', true );
95 + return WP_NETWORK_ADMIN;
67 96 }
97 +
98 + return false;
68 99 }
69 100
70 101 /**
71 102 * Builds a stdClass object used when displaying actions done in network administration
@@ -72,9 +103,9 @@
72 103 *
73 104 * @return object
74 105 */
75 106 public function get_network_blog() {
76 - $blog = new \stdClass;
107 + $blog = new \stdClass();
77 108 $blog->blog_id = 0;
78 109 $blog->blogname = esc_html__( 'Network Admin', 'stream' );
79 110
80 111 return $blog;
@@ -85,13 +116,9 @@
85 116 *
86 117 * @return bool
87 118 */
88 119 public function is_network_activated() {
89 - if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
90 - require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
91 - }
92 -
93 - return is_plugin_active_for_network( $this->plugin->locations['plugin'] );
120 + return $this->plugin->is_network_activated();
94 121 }
95 122
96 123 /**
97 124 * Adds Stream to the admin bar under the "My Sites > Network Admin" menu
@@ -98,9 +125,9 @@
98 125 * if Stream has been network-activated.
99 126 *
100 127 * @action admin_bar_menu
101 128 *
102 - * @param object $admin_bar
129 + * @param object $admin_bar Admin bar object.
103 130 *
104 131 * @return void
105 132 */
106 133 public function network_admin_bar_menu( $admin_bar ) {
@@ -125,9 +152,9 @@
125 152 );
126 153 }
127 154
128 155 /**
129 - * Add Network Settings and Default Settings menu items
156 + * Add Network Settings and Default Settings menu pages
130 157 *
131 158 * @return array
132 159 */
133 160 public function admin_menu_screens() {
@@ -135,8 +162,9 @@
135 162 return;
136 163 }
137 164
138 165 remove_submenu_page( $this->plugin->admin->records_page_slug, 'wp_stream_settings' );
166 + remove_submenu_page( $this->plugin->admin->records_page_slug, 'edit.php?post_type=wp_stream_alerts' );
139 167
140 168 $this->plugin->admin->screen_id['network_settings'] = add_submenu_page(
141 169 $this->plugin->admin->records_page_slug,
142 170 __( 'Stream Network Settings', 'stream' ),
@@ -149,11 +177,11 @@
149 177
150 178 /**
151 179 * Remove records when records TTL is shortened
152 180 *
153 - * @param string $option_key
154 - * @param array $old_value
155 - * @param array $new_value
181 + * @param string $option_key Unused.
182 + * @param array $new_value New value.
183 + * @param array $old_value Old value.
156 184 *
157 185 * @action update_option_wp_stream
158 186 * @return void
159 187 */
@@ -164,9 +192,9 @@
164 192
165 193 /**
166 194 * Adjust the action of the settings form when in the Network Admin
167 195 *
168 - * @param $action
196 + * @param string $action Query string.
169 197 *
170 198 * @return string
171 199 */
172 200 public function settings_form_action( $action ) {
@@ -171,9 +199,14 @@
171 199 */
172 200 public function settings_form_action( $action ) {
173 201 if ( is_network_admin() ) {
174 202 $current_page = wp_stream_filter_input( INPUT_GET, 'page' );
175 - $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 + );
176 209 }
177 210
178 211 return $action;
179 212 }
@@ -180,9 +213,9 @@
180 213
181 214 /**
182 215 * Add a description to each of the Settings pages in the Network Admin
183 216 *
184 - * @param $description
217 + * @param string $description Description of the current page.
185 218 *
186 219 * @return string
187 220 */
188 221 public function settings_form_description( $description ) {
@@ -191,15 +224,10 @@
191 224 }
192 225
193 226 $current_page = wp_stream_filter_input( INPUT_GET, 'page' );
194 227
195 - switch ( $current_page ) {
196 - case $this->network_settings_page_slug :
197 - $description = __( 'These settings apply to all sites on the network.', 'stream' );
198 - break;
199 - case $this->default_settings_page_slug :
200 - $description = __( 'These default settings will apply to new sites created on the network. These settings do not alter existing sites.', 'stream' );
201 - break;
228 + if ( $this->network_settings_page_slug === $current_page ) {
229 + $description = __( 'These settings apply to all sites on the network.', 'stream' );
202 230 }
203 231
204 232 return $description;
205 233 }
@@ -206,9 +234,9 @@
206 234
207 235 /**
208 236 * Adjusts the settings fields displayed in various network admin screens
209 237 *
210 - * @param $fields
238 + * @param array $fields Page settings fields.
211 239 *
212 240 * @return mixed
213 241 */
214 242 public function get_network_admin_fields( $fields ) {
@@ -218,9 +246,9 @@
218 246
219 247 $stream_hidden_options = apply_filters(
220 248 'wp_stream_hidden_option_fields',
221 249 array(
222 - 'general' => array(
250 + 'general' => array(
223 251 'records_ttl',
224 252 ),
225 253 'advanced' => array(
226 254 'delete_all_records',
@@ -245,9 +273,9 @@
245 273 ),
246 274 )
247 275 );
248 276
249 - // Remove settings based on context
277 + // Remove settings based on context.
250 278 if ( $this->plugin->settings->network_options_key === $this->plugin->settings->option_key ) {
251 279 $hidden_options = $network_hidden_options;
252 280 } else {
253 281 $hidden_options = $stream_hidden_options;
@@ -264,9 +292,9 @@
264 292 }
265 293 }
266 294 }
267 295
268 - // Add settings based on context
296 + // Add settings based on context.
269 297 if ( $this->plugin->settings->network_options_key === $this->plugin->settings->option_key ) {
270 298 $new_fields['general']['fields'][] = array(
271 299 'name' => 'site_access',
272 300 'title' => __( 'Site Access', 'stream' ),
@@ -278,9 +306,9 @@
278 306
279 307 $fields = array_merge_recursive( $new_fields, $fields );
280 308 }
281 309
282 - // Remove empty settings sections
310 + // Remove empty settings sections.
283 311 foreach ( $fields as $section_key => $section ) {
284 312 if ( empty( $section['fields'] ) ) {
285 313 unset( $fields[ $section_key ] );
286 314 }
@@ -293,8 +321,10 @@
293 321 * Get translations of serialized Stream Network settings
294 322 *
295 323 * @filter wp_stream_serialized_labels
296 324 *
325 + * @param array $labels Setting labels.
326 + *
297 327 * @return array Multidimensional array of fields
298 328 */
299 329 public function get_settings_translations( $labels ) {
300 330 $network_key = $this->plugin->settings->network_options_key;
@@ -315,48 +345,44 @@
315 345 /**
316 346 * Wrapper for the settings API to work on the network settings page
317 347 */
318 348 public function network_options_action() {
319 - $allowed_referers = array(
320 - $this->network_settings_page_slug,
321 - $this->default_settings_page_slug,
322 - );
323 349
324 - 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 ) ) ) {
325 352 return;
326 353 }
327 354
328 - // @codingStandardsIgnoreStart
329 - $options = isset( $_POST['option_page'] ) ? explode( ',', stripslashes( $_POST['option_page'] ) ) : null;
330 - // @codingStandardsIgnoreEnd
355 + // Check the user capability.
356 + if ( ! current_user_can( $this->plugin->admin->settings_cap ) ) {
357 + return;
358 + }
331 359
332 - if ( $options ) {
360 + // Check the action.
361 + if ( ! isset( $_GET['action'] ) || $this->network_settings_page_slug !== $_GET['action'] ) {
362 + return;
363 + }
333 364
334 - foreach ( $options as $option ) {
335 - $option = trim( $option );
336 - $value = null;
337 - $sections = $this->plugin->settings->get_fields();
365 + $option = ! empty( $_POST['option_page'] ) ? $_POST['option_page'] : false;
338 366
339 - foreach ( $sections as $section_name => $section ) {
340 - foreach ( $section['fields'] as $field_idx => $field ) {
341 - $option_key = $section_name . '_' . $field['name'];
367 + if ( $option && $this->network_settings_option === $option ) {
342 368
343 - // @codingStandardsIgnoreStart
344 - if ( isset( $_POST[ $option ][ $option_key ] ) ) {
345 - $value[ $option_key ] = $_POST[ $option ][ $option_key ];
346 - } else {
347 - $value[ $option_key ] = false;
348 - }
349 - // @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;
350 380 }
351 381 }
382 + }
352 383
353 - if ( ! is_array( $value ) ) {
354 - $value = trim( $value );
355 - }
356 -
357 - update_site_option( $option, $value );
358 - }
384 + update_site_option( $this->network_settings_option, $value );
359 385 }
360 386
361 387 if ( ! count( get_settings_errors() ) ) {
362 388 add_settings_error( 'general', 'settings_updated', __( 'Settings saved.', 'stream' ), 'updated' );
@@ -365,9 +391,9 @@
365 391 set_transient( 'settings_errors', get_settings_errors(), 30 );
366 392
367 393 $go_back = add_query_arg( 'settings-updated', 'true', wp_get_referer() );
368 394
369 - wp_redirect( $go_back );
395 + wp_safe_redirect( $go_back );
370 396
371 397 exit;
372 398 }
373 399
@@ -375,9 +401,9 @@
375 401 * Add the Site filter to the Network records screen
376 402 *
377 403 * @filter wp_stream_list_table_filters
378 404 *
379 - * @param $filters
405 + * @param array $filters Filters.
380 406 *
381 407 * @return array
382 408 */
383 409 public function list_table_filters( $filters ) {
@@ -386,9 +412,9 @@
386 412 }
387 413
388 414 $blogs = array();
389 415
390 - // Display network blog as the first option
416 + // Display network blog as the first option.
391 417 $network_blog = $this->get_network_blog();
392 418
393 419 $blogs[ $network_blog->blog_id ] = array(
394 420 'label' => $network_blog->blogname,
@@ -394,9 +420,9 @@
394 420 'label' => $network_blog->blogname,
395 421 'disabled' => '',
396 422 );
397 423
398 - // add all sites
424 + // Add all sites.
399 425 foreach ( wp_stream_get_sites() as $blog ) {
400 426 $blog_data = get_blog_details( $blog->blog_id );
401 427
402 428 $blogs[ $blog->blog_id ] = array(
@@ -415,9 +441,9 @@
415 441
416 442 /**
417 443 * Add the Site toggle to screen options in network admin
418 444 *
419 - * @param $filters
445 + * @param array $filters Filters.
420 446 *
421 447 * @return array
422 448 */
423 449 public function toggle_filters( $filters ) {
@@ -430,9 +456,9 @@
430 456
431 457 /**
432 458 * Add the network suffix to the $screen_id when in the network admin
433 459 *
434 - * @param $screen_id
460 + * @param int $screen_id Screen ID.
435 461 *
436 462 * @return string
437 463 */
438 464 public function list_table_screen_id( $screen_id ) {
@@ -447,8 +473,10 @@
447 473
448 474 /**
449 475 * Set blog_id for network admin activity
450 476 *
477 + * @param int $blog_id Blog ID.
478 + *
451 479 * @return int
452 480 */
453 481 public function blog_id_logged( $blog_id ) {
454 482 return is_network_admin() ? 0 : $blog_id;
@@ -458,9 +486,9 @@
458 486 * Customize query args on multisite installs
459 487 *
460 488 * @filter wp_stream_query_args
461 489 *
462 - * @param array $args
490 + * @param array $args Site arguments.
463 491 *
464 492 * @return array
465 493 */
466 494 public function network_query_args( $args ) {
@@ -474,14 +502,15 @@
474 502 * Add site count to the page title in the network admin
475 503 *
476 504 * @filter wp_stream_admin_page_title
477 505 *
478 - * @param string $page_title
506 + * @param string $page_title Page title.
479 507 *
480 508 * @return string
481 509 */
482 510 public function network_admin_page_title( $page_title ) {
483 511 if ( is_network_admin() ) {
512 + /* translators: %d: number of sites on the network (e.g. "42") */
484 513 $site_count = sprintf( _n( '%d site', '%d sites', get_blog_count(), 'stream' ), number_format( get_blog_count() ) );
485 514 $page_title = sprintf( '%s (%s)', $page_title, $site_count );
486 515 }
487 516
@@ -490,14 +519,14 @@
490 519
491 520 /**
492 521 * Add the Site column to the network stream records
493 522 *
494 - * @param $columns
523 + * @param array $columns Columns data.
495 524 *
496 525 * @return mixed
497 526 */
498 527 public function network_admin_columns( $columns ) {
499 - if ( is_network_admin() ) {
528 + if ( is_network_admin() || $this->ajax_network_admin() ) {
500 529 $columns = array_merge(
501 530 array_slice( $columns, 0, -1 ),
502 531 array(
503 532 'blog_id' => esc_html__( 'Site', 'stream' ),
@@ -511,9 +540,9 @@
511 540
512 541 /**
513 542 * Prevent the Blogs connector from loading when not in Network Admin
514 543 *
515 - * @param $connectors
544 + * @param array $connectors Connectors.
516 545 *
517 546 * @return mixed
518 547 */
519 548 public function hide_blogs_connector( $connectors ) {