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

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