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
stream / classes / class-network.php

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

556 lines 14.1 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 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 if ( $this->network_settings_page_slug === $current_page ) {
229 $description = __( 'These settings apply to all sites on the network.', 'stream' );
230 }
231
232 return $description;
233 }
234
235 /**
236 * Adjusts the settings fields displayed in various network admin screens
237 *
238 * @param array $fields Page settings fields.
239 *
240 * @return mixed
241 */
242 public function get_network_admin_fields( $fields ) {
243 if ( ! $this->is_network_activated() ) {
244 return $fields;
245 }
246
247 $stream_hidden_options = apply_filters(
248 'wp_stream_hidden_option_fields',
249 array(
250 'general' => array(
251 'records_ttl',
252 ),
253 'advanced' => array(
254 'delete_all_records',
255 ),
256 )
257 );
258
259 $network_hidden_options = apply_filters(
260 'wp_stream_network_option_fields',
261 array(
262 'general' => array(
263 'role_access',
264 ),
265 'exclude' => array(
266 'authors',
267 'roles',
268 'connectors',
269 'contexts',
270 'actions',
271 'ip_addresses',
272 'hide_previous_records',
273 ),
274 )
275 );
276
277 // Remove settings based on context.
278 if ( $this->plugin->settings->network_options_key === $this->plugin->settings->option_key ) {
279 $hidden_options = $network_hidden_options;
280 } else {
281 $hidden_options = $stream_hidden_options;
282 }
283
284 foreach ( $fields as $section_key => $section ) {
285 foreach ( $section['fields'] as $key => $field ) {
286 if ( ! isset( $hidden_options[ $section_key ] ) ) {
287 continue;
288 }
289
290 if ( in_array( $field['name'], $hidden_options[ $section_key ], true ) ) {
291 unset( $fields[ $section_key ]['fields'][ $key ] );
292 }
293 }
294 }
295
296 // Add settings based on context.
297 if ( $this->plugin->settings->network_options_key === $this->plugin->settings->option_key ) {
298 $new_fields['general']['fields'][] = array(
299 'name' => 'site_access',
300 'title' => __( 'Site Access', 'stream' ),
301 'after_field' => __( 'Enabled', 'stream' ),
302 'default' => 1,
303 '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' ),
304 'type' => 'checkbox',
305 );
306
307 $fields = array_merge_recursive( $new_fields, $fields );
308 }
309
310 // Remove empty settings sections.
311 foreach ( $fields as $section_key => $section ) {
312 if ( empty( $section['fields'] ) ) {
313 unset( $fields[ $section_key ] );
314 }
315 }
316
317 return $fields;
318 }
319
320 /**
321 * Get translations of serialized Stream Network settings
322 *
323 * @filter wp_stream_serialized_labels
324 *
325 * @param array $labels Setting labels.
326 *
327 * @return array Multidimensional array of fields
328 */
329 public function get_settings_translations( $labels ) {
330 $network_key = $this->plugin->settings->network_options_key;
331
332 if ( ! isset( $labels[ $network_key ] ) ) {
333 $labels[ $network_key ] = array();
334 }
335
336 foreach ( $this->plugin->settings->get_fields() as $section_slug => $section ) {
337 foreach ( $section['fields'] as $field ) {
338 $labels[ $network_key ][ sprintf( '%s_%s', $section_slug, $field['name'] ) ] = $field['title'];
339 }
340 }
341
342 return $labels;
343 }
344
345 /**
346 * Wrapper for the settings API to work on the network settings page
347 */
348 public function network_options_action() {
349
350 // Check the nonce.
351 if ( empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], sprintf( '%s-options', $this->network_settings_option ) ) ) {
352 return;
353 }
354
355 // Check the user capability.
356 if ( ! current_user_can( $this->plugin->admin->settings_cap ) ) {
357 return;
358 }
359
360 // Check the action.
361 if ( ! isset( $_GET['action'] ) || $this->network_settings_page_slug !== $_GET['action'] ) {
362 return;
363 }
364
365 $option = ! empty( $_POST['option_page'] ) ? $_POST['option_page'] : false;
366
367 if ( $option && $this->network_settings_option === $option ) {
368
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;
380 }
381 }
382 }
383
384 update_site_option( $this->network_settings_option, $value );
385 }
386
387 if ( ! count( get_settings_errors() ) ) {
388 add_settings_error( 'general', 'settings_updated', __( 'Settings saved.', 'stream' ), 'updated' );
389 }
390
391 set_transient( 'settings_errors', get_settings_errors(), 30 );
392
393 $go_back = add_query_arg( 'settings-updated', 'true', wp_get_referer() );
394
395 wp_safe_redirect( $go_back );
396
397 exit;
398 }
399
400 /**
401 * Add the Site filter to the Network records screen
402 *
403 * @filter wp_stream_list_table_filters
404 *
405 * @param array $filters Filters.
406 *
407 * @return array
408 */
409 public function list_table_filters( $filters ) {
410 if ( ! is_network_admin() || wp_is_large_network() ) {
411 return $filters;
412 }
413
414 $blogs = array();
415
416 // Display network blog as the first option.
417 $network_blog = $this->get_network_blog();
418
419 $blogs[ $network_blog->blog_id ] = array(
420 'label' => $network_blog->blogname,
421 'disabled' => '',
422 );
423
424 // Add all sites.
425 foreach ( wp_stream_get_sites() as $blog ) {
426 $blog_data = get_blog_details( $blog->blog_id );
427
428 $blogs[ $blog->blog_id ] = array(
429 'label' => $blog_data->blogname,
430 'disabled' => '',
431 );
432 }
433
434 $filters['blog_id'] = array(
435 'title' => __( 'sites', 'stream' ),
436 'items' => $blogs,
437 );
438
439 return $filters;
440 }
441
442 /**
443 * Add the Site toggle to screen options in network admin
444 *
445 * @param array $filters Filters.
446 *
447 * @return array
448 */
449 public function toggle_filters( $filters ) {
450 if ( is_network_admin() ) {
451 $filters['blog_id'] = esc_html__( 'Site', 'stream' );
452 }
453
454 return $filters;
455 }
456
457 /**
458 * Add the network suffix to the $screen_id when in the network admin
459 *
460 * @param int $screen_id Screen ID.
461 *
462 * @return string
463 */
464 public function list_table_screen_id( $screen_id ) {
465 if ( $screen_id && is_network_admin() ) {
466 if ( '-network' !== substr( $screen_id, -8 ) ) {
467 $screen_id .= '-network';
468 }
469 }
470
471 return $screen_id;
472 }
473
474 /**
475 * Set blog_id for network admin activity
476 *
477 * @param int $blog_id Blog ID.
478 *
479 * @return int
480 */
481 public function blog_id_logged( $blog_id ) {
482 return is_network_admin() ? 0 : $blog_id;
483 }
484
485 /**
486 * Customize query args on multisite installs
487 *
488 * @filter wp_stream_query_args
489 *
490 * @param array $args Site arguments.
491 *
492 * @return array
493 */
494 public function network_query_args( $args ) {
495 $args['site_id'] = is_numeric( $args['site_id'] ) ? $args['site_id'] : get_current_site()->id;
496 $args['blog_id'] = is_numeric( $args['blog_id'] ) ? $args['blog_id'] : ( is_network_admin() ? null : get_current_blog_id() );
497
498 return $args;
499 }
500
501 /**
502 * Add site count to the page title in the network admin
503 *
504 * @filter wp_stream_admin_page_title
505 *
506 * @param string $page_title Page title.
507 *
508 * @return string
509 */
510 public function network_admin_page_title( $page_title ) {
511 if ( is_network_admin() ) {
512 /* translators: %d: number of sites on the network (e.g. "42") */
513 $site_count = sprintf( _n( '%d site', '%d sites', get_blog_count(), 'stream' ), number_format( get_blog_count() ) );
514 $page_title = sprintf( '%s (%s)', $page_title, $site_count );
515 }
516
517 return $page_title;
518 }
519
520 /**
521 * Add the Site column to the network stream records
522 *
523 * @param array $columns Columns data.
524 *
525 * @return mixed
526 */
527 public function network_admin_columns( $columns ) {
528 if ( is_network_admin() || $this->ajax_network_admin() ) {
529 $columns = array_merge(
530 array_slice( $columns, 0, -1 ),
531 array(
532 'blog_id' => esc_html__( 'Site', 'stream' ),
533 ),
534 array_slice( $columns, -1 )
535 );
536 }
537
538 return $columns;
539 }
540
541 /**
542 * Prevent the Blogs connector from loading when not in Network Admin
543 *
544 * @param array $connectors Connectors.
545 *
546 * @return mixed
547 */
548 public function hide_blogs_connector( $connectors ) {
549 if ( ! is_network_admin() ) {
550 return array_diff( $connectors, array( 'Connector_Blogs' ) );
551 }
552
553 return $connectors;
554 }
555 }
556