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

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