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

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