PluginProbe
Stream – Activity Log & Audit Trail / 4.3.0
Stream – Activity Log & Audit Trail v4.3.0
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
stream / classes / class-network.php

class-network.php in Stream – Activity Log & Audit Trail 4.3.0, at classes/class-network.php

564 lines 14.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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
9 namespace WP_Stream;
10
11 /**
12 * Class - Network
13 */
14 class Network {
15 /**
16 * Holds instance of plugin object
17 *
18 * @var Plugin
19 */
20 public $plugin;
21
22 /**
23 * Network page slug
24 *
25 * @var string
26 */
27 public $network_settings_page_slug = 'wp_stream_network_settings';
28
29 /**
30 * The option name for the network settings.
31 *
32 * @var string
33 */
34 public $network_settings_option = 'wp_stream_network';
35
36 /**
37 * Class constructor
38 *
39 * @param Plugin $plugin Instance of plugin object.
40 */
41 public function __construct( $plugin ) {
42 $this->plugin = $plugin;
43
44 // Always add default site_id/blog_id params when multisite.
45 if ( is_multisite() ) {
46 add_filter( 'wp_stream_query_args', array( $this, 'network_query_args' ) );
47 }
48
49 // Bail early if not network-activated.
50 if ( ! $this->is_network_activated() ) {
51 return;
52 }
53
54 // Actions.
55 add_action( 'init', array( $this, 'ajax_network_admin' ) );
56 add_action( 'network_admin_menu', array( $this->plugin->admin, 'register_menu' ) );
57 add_action( 'network_admin_menu', array( $this, 'admin_menu_screens' ) );
58 add_action( 'admin_menu', array( $this, 'admin_menu_screens' ) );
59 add_action( 'admin_bar_menu', array( $this, 'network_admin_bar_menu' ), 99 );
60 add_action( 'network_admin_notices', array( $this->plugin->admin, 'admin_notices' ) );
61 add_action( 'wpmuadminedit', array( $this, 'network_options_action' ) );
62 add_action( 'update_site_option_' . $this->plugin->settings->network_options_key, array( $this, 'updated_option_ttl_remove_records' ), 10, 3 );
63
64 // Filters.
65 add_filter( 'wp_stream_blog_id_logged', array( $this, 'blog_id_logged' ) );
66 add_filter( 'wp_stream_admin_page_title', array( $this, 'network_admin_page_title' ) );
67 add_filter( 'wp_stream_list_table_screen_id', array( $this, 'list_table_screen_id' ) );
68 add_filter( 'wp_stream_list_table_filters', array( $this, 'list_table_filters' ) );
69 add_filter( 'wp_stream_list_table_columns', array( $this, 'network_admin_columns' ) );
70 add_filter( 'wp_stream_settings_form_action', array( $this, 'settings_form_action' ) );
71 add_filter( 'wp_stream_settings_form_description', array( $this, 'settings_form_description' ) );
72 add_filter( 'wp_stream_settings_option_fields', array( $this, 'get_network_admin_fields' ) );
73 add_filter( 'wp_stream_serialized_labels', array( $this, 'get_settings_translations' ) );
74 add_filter( 'wp_stream_connectors', array( $this, 'hide_blogs_connector' ) );
75 }
76
77 /**
78 * Workaround to get admin-ajax.php to know when the request is from the Network Admin
79 *
80 * @return bool
81 *
82 * @action init
83 *
84 * @see https://core.trac.wordpress.org/ticket/22589
85 */
86 public function ajax_network_admin() {
87 $http_referer = isset( $_SERVER['HTTP_REFERER'] ) ? esc_url_raw( wp_unslash( $_SERVER['HTTP_REFERER'] ) ) : '';
88
89 if (
90 defined( 'DOING_AJAX' )
91 &&
92 DOING_AJAX
93 &&
94 0 === stripos( $http_referer, network_admin_url() )
95 ) {
96 define( 'WP_NETWORK_ADMIN', true );
97 return WP_NETWORK_ADMIN;
98 }
99
100 return false;
101 }
102
103 /**
104 * Builds a stdClass object used when displaying actions done in network administration
105 *
106 * @return object
107 */
108 public function get_network_blog() {
109 $blog = new \stdClass();
110 $blog->blog_id = 0;
111 $blog->blogname = esc_html__( 'Network Admin', 'stream' );
112
113 return $blog;
114 }
115
116 /**
117 * Returns true if Stream is network activated, otherwise false
118 *
119 * @return bool
120 */
121 public function is_network_activated() {
122 return $this->plugin->is_network_activated();
123 }
124
125 /**
126 * Adds Stream to the admin bar under the "My Sites > Network Admin" menu
127 * if Stream has been network-activated.
128 *
129 * @action admin_bar_menu
130 *
131 * @param object $admin_bar Admin bar object.
132 *
133 * @return void
134 */
135 public function network_admin_bar_menu( $admin_bar ) {
136 if ( ! $this->is_network_activated() ) {
137 return;
138 }
139
140 $href = add_query_arg(
141 array(
142 'page' => $this->plugin->admin->records_page_slug,
143 ),
144 network_admin_url( $this->plugin->admin->admin_parent_page )
145 );
146
147 $admin_bar->add_menu(
148 array(
149 'id' => 'network-admin-stream',
150 'parent' => 'network-admin',
151 'title' => esc_html__( 'Stream', 'stream' ),
152 'href' => esc_url( $href ),
153 )
154 );
155 }
156
157 /**
158 * Add Network Settings and Default Settings menu pages
159 *
160 * @return array
161 */
162 public function admin_menu_screens() {
163 if ( ! is_network_admin() ) {
164 return;
165 }
166
167 remove_submenu_page( $this->plugin->admin->records_page_slug, 'wp_stream_settings' );
168 remove_submenu_page( $this->plugin->admin->records_page_slug, 'edit.php?post_type=wp_stream_alerts' );
169
170 $this->plugin->admin->screen_id['network_settings'] = add_submenu_page(
171 $this->plugin->admin->records_page_slug,
172 __( 'Stream Network Settings', 'stream' ),
173 __( 'Network Settings', 'stream' ),
174 $this->plugin->admin->settings_cap,
175 $this->network_settings_page_slug,
176 array( $this->plugin->admin, 'render_settings_page' )
177 );
178 }
179
180 /**
181 * Remove records when records TTL is shortened
182 *
183 * @param string $option_key Unused.
184 * @param array $new_value New value.
185 * @param array $old_value Old value.
186 *
187 * @action update_option_wp_stream
188 * @return void
189 */
190 public function updated_option_ttl_remove_records( $option_key, $new_value, $old_value ) {
191 unset( $option_key );
192 $this->plugin->settings->updated_option_ttl_remove_records( $old_value, $new_value );
193 }
194
195 /**
196 * Adjust the action of the settings form when in the Network Admin
197 *
198 * @param string $action Query string.
199 *
200 * @return string
201 */
202 public function settings_form_action( $action ) {
203 if ( is_network_admin() ) {
204 $current_page = wp_stream_filter_input( INPUT_GET, 'page' );
205 $action = add_query_arg(
206 array(
207 'action' => $current_page,
208 ),
209 'edit.php'
210 );
211 }
212
213 return $action;
214 }
215
216 /**
217 * Add a description to each of the Settings pages in the Network Admin
218 *
219 * @param string $description Description of the current page.
220 *
221 * @return string
222 */
223 public function settings_form_description( $description ) {
224 if ( ! is_network_admin() ) {
225 return '';
226 }
227
228 $current_page = wp_stream_filter_input( INPUT_GET, 'page' );
229
230 if ( $this->network_settings_page_slug === $current_page ) {
231 $description = __( 'These settings apply to all sites on the network.', 'stream' );
232 }
233
234 return $description;
235 }
236
237 /**
238 * Adjusts the settings fields displayed in various network admin screens
239 *
240 * @param array $fields Page settings fields.
241 *
242 * @return mixed
243 */
244 public function get_network_admin_fields( $fields ) {
245 if ( ! $this->is_network_activated() ) {
246 return $fields;
247 }
248
249 $stream_hidden_options = apply_filters(
250 'wp_stream_hidden_option_fields',
251 array(
252 'general' => array(
253 'records_ttl',
254 ),
255 'advanced' => array(
256 'delete_all_records',
257 ),
258 )
259 );
260
261 $network_hidden_options = apply_filters(
262 'wp_stream_network_option_fields',
263 array(
264 'general' => array(
265 'role_access',
266 ),
267 'exclude' => array(
268 'authors',
269 'roles',
270 'connectors',
271 'contexts',
272 'actions',
273 'ip_addresses',
274 'hide_previous_records',
275 ),
276 )
277 );
278
279 // Remove settings based on context.
280 if ( $this->plugin->settings->network_options_key === $this->plugin->settings->option_key ) {
281 $hidden_options = $network_hidden_options;
282 } else {
283 $hidden_options = $stream_hidden_options;
284 }
285
286 foreach ( $fields as $section_key => $section ) {
287 foreach ( $section['fields'] as $key => $field ) {
288 if ( ! isset( $hidden_options[ $section_key ] ) ) {
289 continue;
290 }
291
292 if ( in_array( $field['name'], $hidden_options[ $section_key ], true ) ) {
293 unset( $fields[ $section_key ]['fields'][ $key ] );
294 }
295 }
296 }
297
298 // Add settings based on context.
299 if ( $this->plugin->settings->network_options_key === $this->plugin->settings->option_key ) {
300 $new_fields['general']['fields'][] = array(
301 'name' => 'site_access',
302 'title' => __( 'Site Access', 'stream' ),
303 'after_field' => __( 'Enabled', 'stream' ),
304 'default' => 1,
305 'desc' => __( 'Allow sites on this network to view their Stream activity. Leave unchecked to only allow Stream to be viewed in the Network Admin.', 'stream' ),
306 'type' => 'checkbox',
307 );
308
309 $fields = array_merge_recursive( $new_fields, $fields );
310 }
311
312 // Remove empty settings sections.
313 foreach ( $fields as $section_key => $section ) {
314 if ( empty( $section['fields'] ) ) {
315 unset( $fields[ $section_key ] );
316 }
317 }
318
319 return $fields;
320 }
321
322 /**
323 * Get translations of serialized Stream Network settings
324 *
325 * @filter wp_stream_serialized_labels
326 *
327 * @param array $labels Setting labels.
328 *
329 * @return array Multidimensional array of fields
330 */
331 public function get_settings_translations( $labels ) {
332 $network_key = $this->plugin->settings->network_options_key;
333
334 if ( ! isset( $labels[ $network_key ] ) ) {
335 $labels[ $network_key ] = array();
336 }
337
338 foreach ( $this->plugin->settings->get_fields() as $section_slug => $section ) {
339 foreach ( $section['fields'] as $field ) {
340 $labels[ $network_key ][ sprintf( '%s_%s', $section_slug, $field['name'] ) ] = $field['title'];
341 }
342 }
343
344 return $labels;
345 }
346
347 /**
348 * Wrapper for the settings API to work on the network settings page
349 */
350 public function network_options_action() {
351
352 // Check the nonce.
353 if (
354 empty( $_POST['_wpnonce'] )
355 ||
356 ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ), sprintf( '%s-options', $this->network_settings_option ) )
357 ) {
358 return;
359 }
360
361 // Check the user capability.
362 if ( ! current_user_can( $this->plugin->admin->settings_cap ) ) {
363 return;
364 }
365
366 // Check the action.
367 $action = isset( $_GET['action'] ) ? sanitize_key( wp_unslash( $_GET['action'] ) ) : '';
368 if ( $this->network_settings_page_slug !== $action ) {
369 return;
370 }
371
372 $option = ! empty( $_POST['option_page'] ) ? sanitize_key( wp_unslash( $_POST['option_page'] ) ) : false;
373
374 if ( $option && $this->network_settings_option === $option ) {
375
376 $value = array();
377 $posted_options = isset( $_POST[ $option ] ) && is_array( $_POST[ $option ] ) ? wp_unslash( $_POST[ $option ] ) : array();
378 $sections = $this->plugin->settings->get_fields();
379
380 foreach ( $sections as $section_name => $section ) {
381 foreach ( $section['fields'] as $field_idx => $field ) {
382 $option_key = $section_name . '_' . $field['name'];
383
384 if ( isset( $posted_options[ $option_key ] ) ) {
385 $value[ $option_key ] = $this->plugin->settings->sanitize_setting_by_field_type( $posted_options[ $option_key ], $field['type'] );
386 } else {
387 $value[ $option_key ] = false;
388 }
389 }
390 }
391
392 update_site_option( $this->network_settings_option, $value );
393 }
394
395 if ( ! count( get_settings_errors() ) ) {
396 add_settings_error( 'general', 'settings_updated', __( 'Settings saved.', 'stream' ), 'updated' );
397 }
398
399 set_transient( 'settings_errors', get_settings_errors(), 30 );
400
401 $go_back = add_query_arg( 'settings-updated', 'true', wp_get_referer() );
402
403 wp_safe_redirect( $go_back );
404
405 exit;
406 }
407
408 /**
409 * Add the Site filter to the Network records screen
410 *
411 * @filter wp_stream_list_table_filters
412 *
413 * @param array $filters Filters.
414 *
415 * @return array
416 */
417 public function list_table_filters( $filters ) {
418 if ( ! is_network_admin() || wp_is_large_network() ) {
419 return $filters;
420 }
421
422 $blogs = array();
423
424 // Display network blog as the first option.
425 $network_blog = $this->get_network_blog();
426
427 $blogs[ $network_blog->blog_id ] = array(
428 'label' => $network_blog->blogname,
429 'disabled' => '',
430 );
431
432 // Add all sites.
433 foreach ( wp_stream_get_sites() as $blog ) {
434 $blog_data = get_blog_details( $blog->blog_id );
435
436 $blogs[ $blog->blog_id ] = array(
437 'label' => $blog_data->blogname,
438 'disabled' => '',
439 );
440 }
441
442 $filters['blog_id'] = array(
443 'title' => __( 'sites', 'stream' ),
444 'items' => $blogs,
445 );
446
447 return $filters;
448 }
449
450 /**
451 * Add the Site toggle to screen options in network admin
452 *
453 * @param array $filters Filters.
454 *
455 * @return array
456 */
457 public function toggle_filters( $filters ) {
458 if ( is_network_admin() ) {
459 $filters['blog_id'] = esc_html__( 'Site', 'stream' );
460 }
461
462 return $filters;
463 }
464
465 /**
466 * Add the network suffix to the $screen_id when in the network admin
467 *
468 * @param int $screen_id Screen ID.
469 *
470 * @return string
471 */
472 public function list_table_screen_id( $screen_id ) {
473 if ( $screen_id && is_network_admin() ) {
474 if ( '-network' !== substr( $screen_id, -8 ) ) {
475 $screen_id .= '-network';
476 }
477 }
478
479 return $screen_id;
480 }
481
482 /**
483 * Set blog_id for network admin activity
484 *
485 * @param int $blog_id Blog ID.
486 *
487 * @return int
488 */
489 public function blog_id_logged( $blog_id ) {
490 return is_network_admin() ? 0 : $blog_id;
491 }
492
493 /**
494 * Customize query args on multisite installs
495 *
496 * @filter wp_stream_query_args
497 *
498 * @param array $args Site arguments.
499 *
500 * @return array
501 */
502 public function network_query_args( $args ) {
503 $args['site_id'] = is_numeric( $args['site_id'] ) ? $args['site_id'] : get_current_site()->id;
504 $args['blog_id'] = is_numeric( $args['blog_id'] ) ? $args['blog_id'] : ( is_network_admin() ? null : get_current_blog_id() );
505
506 return $args;
507 }
508
509 /**
510 * Add site count to the page title in the network admin
511 *
512 * @filter wp_stream_admin_page_title
513 *
514 * @param string $page_title Page title.
515 *
516 * @return string
517 */
518 public function network_admin_page_title( $page_title ) {
519 if ( is_network_admin() ) {
520 /* translators: %d: number of sites on the network (e.g. "42") */
521 $site_count = sprintf( _n( '%d site', '%d sites', get_blog_count(), 'stream' ), number_format( get_blog_count() ) );
522 $page_title = sprintf( '%s (%s)', $page_title, $site_count );
523 }
524
525 return $page_title;
526 }
527
528 /**
529 * Add the Site column to the network stream records
530 *
531 * @param array $columns Columns data.
532 *
533 * @return mixed
534 */
535 public function network_admin_columns( $columns ) {
536 if ( is_network_admin() || $this->ajax_network_admin() ) {
537 $columns = array_merge(
538 array_slice( $columns, 0, -1 ),
539 array(
540 'blog_id' => esc_html__( 'Site', 'stream' ),
541 ),
542 array_slice( $columns, -1 )
543 );
544 }
545
546 return $columns;
547 }
548
549 /**
550 * Prevent the Blogs connector from loading when not in Network Admin
551 *
552 * @param array $connectors Connectors.
553 *
554 * @return mixed
555 */
556 public function hide_blogs_connector( $connectors ) {
557 if ( ! is_network_admin() ) {
558 return array_diff( $connectors, array( 'Connector_Blogs' ) );
559 }
560
561 return $connectors;
562 }
563 }
564