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

533 lines 13.6 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 * @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 return is_plugin_active_for_network( $this->plugin->locations['plugin'] );
99 }
100
101 /**
102 * Adds Stream to the admin bar under the "My Sites > Network Admin" menu
103 * if Stream has been network-activated.
104 *
105 * @action admin_bar_menu
106 *
107 * @param object $admin_bar
108 *
109 * @return void
110 */
111 public function network_admin_bar_menu( $admin_bar ) {
112 if ( ! $this->is_network_activated() ) {
113 return;
114 }
115
116 $href = add_query_arg(
117 array(
118 'page' => $this->plugin->admin->records_page_slug,
119 ),
120 network_admin_url( $this->plugin->admin->admin_parent_page )
121 );
122
123 $admin_bar->add_menu(
124 array(
125 'id' => 'network-admin-stream',
126 'parent' => 'network-admin',
127 'title' => esc_html__( 'Stream', 'stream' ),
128 'href' => esc_url( $href ),
129 )
130 );
131 }
132
133 /**
134 * Add Network Settings and Default Settings menu items
135 *
136 * @return array
137 */
138 public function admin_menu_screens() {
139 if ( ! is_network_admin() ) {
140 return;
141 }
142
143 remove_submenu_page( $this->plugin->admin->records_page_slug, 'wp_stream_settings' );
144 remove_submenu_page( $this->plugin->admin->records_page_slug, 'edit.php?post_type=wp_stream_alerts' );
145
146 $this->plugin->admin->screen_id['network_settings'] = add_submenu_page(
147 $this->plugin->admin->records_page_slug,
148 __( 'Stream Network Settings', 'stream' ),
149 __( 'Network Settings', 'stream' ),
150 $this->plugin->admin->settings_cap,
151 $this->network_settings_page_slug,
152 array( $this->plugin->admin, 'render_settings_page' )
153 );
154 }
155
156 /**
157 * Remove records when records TTL is shortened
158 *
159 * @param string $option_key
160 * @param array $old_value
161 * @param array $new_value
162 *
163 * @action update_option_wp_stream
164 * @return void
165 */
166 public function updated_option_ttl_remove_records( $option_key, $new_value, $old_value ) {
167 unset( $option_key );
168 $this->plugin->settings->updated_option_ttl_remove_records( $old_value, $new_value );
169 }
170
171 /**
172 * Adjust the action of the settings form when in the Network Admin
173 *
174 * @param $action
175 *
176 * @return string
177 */
178 public function settings_form_action( $action ) {
179 if ( is_network_admin() ) {
180 $current_page = wp_stream_filter_input( INPUT_GET, 'page' );
181 $action = add_query_arg( array( 'action' => $current_page ), 'edit.php' );
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 ) ) {
331 return;
332 }
333
334 // @codingStandardsIgnoreStart
335 $options = isset( $_POST['option_page'] ) ? explode( ',', stripslashes( $_POST['option_page'] ) ) : null;
336 // @codingStandardsIgnoreEnd
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 $site_count = sprintf( _n( '%d site', '%d sites', get_blog_count(), 'stream' ), number_format( get_blog_count() ) );
491 $page_title = sprintf( '%s (%s)', $page_title, $site_count );
492 }
493
494 return $page_title;
495 }
496
497 /**
498 * Add the Site column to the network stream records
499 *
500 * @param $columns
501 *
502 * @return mixed
503 */
504 public function network_admin_columns( $columns ) {
505 if ( is_network_admin() || $this->ajax_network_admin() ) {
506 $columns = array_merge(
507 array_slice( $columns, 0, -1 ),
508 array(
509 'blog_id' => esc_html__( 'Site', 'stream' ),
510 ),
511 array_slice( $columns, -1 )
512 );
513 }
514
515 return $columns;
516 }
517
518 /**
519 * Prevent the Blogs connector from loading when not in Network Admin
520 *
521 * @param $connectors
522 *
523 * @return mixed
524 */
525 public function hide_blogs_connector( $connectors ) {
526 if ( ! is_network_admin() ) {
527 return array_diff( $connectors, array( 'Connector_Blogs' ) );
528 }
529
530 return $connectors;
531 }
532 }
533