PluginProbe
Stream – Activity Log & Audit Trail / 3.6.1
Stream – Activity Log & Audit Trail v3.6.1
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 3.6.1, at classes/class-network.php

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