PluginProbe
Stream – Activity Log & Audit Trail / 3.2.3
Stream – Activity Log & Audit Trail v3.2.3
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.2.3, 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 namespace WP_Stream;
3
4 class Network {
5 /**
6 * Hold Plugin class
7 * @var Plugin
8 */
9 public $plugin;
10
11 public $network_settings_page_slug = 'wp_stream_network_settings';
12
13 public $default_settings_page_slug = 'wp_stream_default_settings';
14
15 public function __construct( $plugin ) {
16 $this->plugin = $plugin;
17
18 // Always add default site_id/blog_id params when multisite
19 if ( is_multisite() ) {
20 add_filter( 'wp_stream_query_args', array( $this, 'network_query_args' ) );
21 }
22
23 // Bail early if not network-activated
24 if ( ! $this->is_network_activated() ) {
25 return;
26 }
27
28 // Actions
29 add_action( 'init', array( $this, 'ajax_network_admin' ) );
30 add_action( 'network_admin_menu', array( $this->plugin->admin, 'register_menu' ) );
31 add_action( 'network_admin_menu', array( $this, 'admin_menu_screens' ) );
32 add_action( 'admin_menu', array( $this, 'admin_menu_screens' ) );
33 add_action( 'admin_bar_menu', array( $this, 'network_admin_bar_menu' ), 99 );
34 add_action( 'network_admin_notices', array( $this->plugin->admin, 'admin_notices' ) );
35 add_action( 'wpmuadminedit', array( $this, 'network_options_action' ) );
36 add_action( 'update_site_option_' . $this->plugin->settings->network_options_key, array( $this, 'updated_option_ttl_remove_records' ), 10, 3 );
37
38 // Filters
39 add_filter( 'wp_stream_blog_id_logged', array( $this, 'blog_id_logged' ) );
40 add_filter( 'wp_stream_admin_page_title', array( $this, 'network_admin_page_title' ) );
41 add_filter( 'wp_stream_list_table_screen_id', array( $this, 'list_table_screen_id' ) );
42 add_filter( 'wp_stream_list_table_filters', array( $this, 'list_table_filters' ) );
43 add_filter( 'wp_stream_list_table_columns', array( $this, 'network_admin_columns' ) );
44 add_filter( 'wp_stream_settings_form_action', array( $this, 'settings_form_action' ) );
45 add_filter( 'wp_stream_settings_form_description', array( $this, 'settings_form_description' ) );
46 add_filter( 'wp_stream_settings_option_fields', array( $this, 'get_network_admin_fields' ) );
47 add_filter( 'wp_stream_serialized_labels', array( $this, 'get_settings_translations' ) );
48 add_filter( 'wp_stream_connectors', array( $this, 'hide_blogs_connector' ) );
49 }
50
51 /**
52 * Workaround to get admin-ajax.php to know when the request is from the Network Admin
53 *
54 * @return bool
55 *
56 * @action init
57 *
58 * @see https://core.trac.wordpress.org/ticket/22589
59 */
60 public function ajax_network_admin() {
61 if (
62 defined( 'DOING_AJAX' )
63 &&
64 DOING_AJAX
65 &&
66 preg_match( '#^' . network_admin_url() . '#i', $_SERVER['HTTP_REFERER'] )
67 ) {
68 define( 'WP_NETWORK_ADMIN', true );
69 return WP_NETWORK_ADMIN;
70 }
71
72 return false;
73 }
74
75 /**
76 * Builds a stdClass object used when displaying actions done in network administration
77 *
78 * @return object
79 */
80 public function get_network_blog() {
81 $blog = new \stdClass();
82 $blog->blog_id = 0;
83 $blog->blogname = esc_html__( 'Network Admin', 'stream' );
84
85 return $blog;
86 }
87
88 /**
89 * Returns true if Stream is network activated, otherwise false
90 *
91 * @return bool
92 */
93 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'] );
103 }
104
105 /**
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 * Adds Stream to the admin bar under the "My Sites > Network Admin" menu
123 * if Stream has been network-activated.
124 *
125 * @action admin_bar_menu
126 *
127 * @param object $admin_bar
128 *
129 * @return void
130 */
131 public function network_admin_bar_menu( $admin_bar ) {
132 if ( ! $this->is_network_activated() ) {
133 return;
134 }
135
136 $href = add_query_arg(
137 array(
138 'page' => $this->plugin->admin->records_page_slug,
139 ),
140 network_admin_url( $this->plugin->admin->admin_parent_page )
141 );
142
143 $admin_bar->add_menu(
144 array(
145 'id' => 'network-admin-stream',
146 'parent' => 'network-admin',
147 'title' => esc_html__( 'Stream', 'stream' ),
148 'href' => esc_url( $href ),
149 )
150 );
151 }
152
153 /**
154 * Add Network Settings and Default Settings menu items
155 *
156 * @return array
157 */
158 public function admin_menu_screens() {
159 if ( ! is_network_admin() ) {
160 return;
161 }
162
163 remove_submenu_page( $this->plugin->admin->records_page_slug, 'wp_stream_settings' );
164 remove_submenu_page( $this->plugin->admin->records_page_slug, 'edit.php?post_type=wp_stream_alerts' );
165
166 $this->plugin->admin->screen_id['network_settings'] = add_submenu_page(
167 $this->plugin->admin->records_page_slug,
168 __( 'Stream Network Settings', 'stream' ),
169 __( 'Network Settings', 'stream' ),
170 $this->plugin->admin->settings_cap,
171 $this->network_settings_page_slug,
172 array( $this->plugin->admin, 'render_settings_page' )
173 );
174 }
175
176 /**
177 * Remove records when records TTL is shortened
178 *
179 * @param string $option_key
180 * @param array $old_value
181 * @param array $new_value
182 *
183 * @action update_option_wp_stream
184 * @return void
185 */
186 public function updated_option_ttl_remove_records( $option_key, $new_value, $old_value ) {
187 unset( $option_key );
188 $this->plugin->settings->updated_option_ttl_remove_records( $old_value, $new_value );
189 }
190
191 /**
192 * Adjust the action of the settings form when in the Network Admin
193 *
194 * @param $action
195 *
196 * @return string
197 */
198 public function settings_form_action( $action ) {
199 if ( is_network_admin() ) {
200 $current_page = wp_stream_filter_input( INPUT_GET, 'page' );
201 $action = add_query_arg(
202 array(
203 'action' => $current_page,
204 ), 'edit.php'
205 );
206 }
207
208 return $action;
209 }
210
211 /**
212 * Add a description to each of the Settings pages in the Network Admin
213 *
214 * @param $description
215 *
216 * @return string
217 */
218 public function settings_form_description( $description ) {
219 if ( ! is_network_admin() ) {
220 return '';
221 }
222
223 $current_page = wp_stream_filter_input( INPUT_GET, 'page' );
224
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;
232 }
233
234 return $description;
235 }
236
237 /**
238 * Adjusts the settings fields displayed in various network admin screens
239 *
240 * @param $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 * @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 $allowed_referers = array(
350 $this->network_settings_page_slug,
351 $this->default_settings_page_slug,
352 );
353
354 if ( ! isset( $_GET['action'] ) || ! in_array( $_GET['action'], $allowed_referers, true ) ) { // CSRF okay
355 return;
356 }
357
358 $options = isset( $_POST['option_page'] ) ? explode( ',', stripslashes( $_POST['option_page'] ) ) : null; // CSRF okay
359
360 if ( $options ) {
361
362 foreach ( $options as $option ) {
363 $option = trim( $option );
364 $value = null;
365 $sections = $this->plugin->settings->get_fields();
366
367 foreach ( $sections as $section_name => $section ) {
368 foreach ( $section['fields'] as $field_idx => $field ) {
369 $option_key = $section_name . '_' . $field['name'];
370
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
378 }
379 }
380
381 if ( ! is_array( $value ) ) {
382 $value = trim( $value );
383 }
384
385 update_site_option( $option, $value );
386 }
387 }
388
389 if ( ! count( get_settings_errors() ) ) {
390 add_settings_error( 'general', 'settings_updated', __( 'Settings saved.', 'stream' ), 'updated' );
391 }
392
393 set_transient( 'settings_errors', get_settings_errors(), 30 );
394
395 $go_back = add_query_arg( 'settings-updated', 'true', wp_get_referer() );
396
397 wp_redirect( $go_back );
398
399 exit;
400 }
401
402 /**
403 * Add the Site filter to the Network records screen
404 *
405 * @filter wp_stream_list_table_filters
406 *
407 * @param $filters
408 *
409 * @return array
410 */
411 public function list_table_filters( $filters ) {
412 if ( ! is_network_admin() || wp_is_large_network() ) {
413 return $filters;
414 }
415
416 $blogs = array();
417
418 // Display network blog as the first option
419 $network_blog = $this->get_network_blog();
420
421 $blogs[ $network_blog->blog_id ] = array(
422 'label' => $network_blog->blogname,
423 'disabled' => '',
424 );
425
426 // add all sites
427 foreach ( wp_stream_get_sites() as $blog ) {
428 $blog_data = get_blog_details( $blog->blog_id );
429
430 $blogs[ $blog->blog_id ] = array(
431 'label' => $blog_data->blogname,
432 'disabled' => '',
433 );
434 }
435
436 $filters['blog_id'] = array(
437 'title' => __( 'sites', 'stream' ),
438 'items' => $blogs,
439 );
440
441 return $filters;
442 }
443
444 /**
445 * Add the Site toggle to screen options in network admin
446 *
447 * @param $filters
448 *
449 * @return array
450 */
451 public function toggle_filters( $filters ) {
452 if ( is_network_admin() ) {
453 $filters['blog_id'] = esc_html__( 'Site', 'stream' );
454 }
455
456 return $filters;
457 }
458
459 /**
460 * Add the network suffix to the $screen_id when in the network admin
461 *
462 * @param $screen_id
463 *
464 * @return string
465 */
466 public function list_table_screen_id( $screen_id ) {
467 if ( $screen_id && is_network_admin() ) {
468 if ( '-network' !== substr( $screen_id, -8 ) ) {
469 $screen_id .= '-network';
470 }
471 }
472
473 return $screen_id;
474 }
475
476 /**
477 * Set blog_id for network admin activity
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
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
507 *
508 * @return string
509 */
510 public function network_admin_page_title( $page_title ) {
511 if ( is_network_admin() ) {
512 // translators: Placeholder refers to a 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 $columns
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 $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