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

525 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 'delete_all_records',
224 'records_ttl',
225 ),
226 )
227 );
228
229 $network_hidden_options = apply_filters(
230 'wp_stream_network_option_fields',
231 array(
232 'general' => array(
233 'role_access',
234 ),
235 'exclude' => array(
236 'authors',
237 'roles',
238 'connectors',
239 'contexts',
240 'actions',
241 'ip_addresses',
242 'hide_previous_records',
243 ),
244 )
245 );
246
247 // Remove settings based on context
248 if ( $this->plugin->settings->network_options_key === $this->plugin->settings->option_key ) {
249 $hidden_options = $network_hidden_options;
250 } else {
251 $hidden_options = $stream_hidden_options;
252 }
253
254 foreach ( $fields as $section_key => $section ) {
255 foreach ( $section['fields'] as $key => $field ) {
256 if ( ! isset( $hidden_options[ $section_key ] ) ) {
257 continue;
258 }
259
260 if ( in_array( $field['name'], $hidden_options[ $section_key ], true ) ) {
261 unset( $fields[ $section_key ]['fields'][ $key ] );
262 }
263 }
264 }
265
266 // Add settings based on context
267 if ( $this->plugin->settings->network_options_key === $this->plugin->settings->option_key ) {
268 $new_fields['general']['fields'][] = array(
269 'name' => 'site_access',
270 'title' => __( 'Site Access', 'stream' ),
271 'after_field' => __( 'Enabled', 'stream' ),
272 'default' => 1,
273 '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' ),
274 'type' => 'checkbox',
275 );
276
277 $fields = array_merge_recursive( $new_fields, $fields );
278 }
279
280 // Remove empty settings sections
281 foreach ( $fields as $section_key => $section ) {
282 if ( empty( $section['fields'] ) ) {
283 unset( $fields[ $section_key ] );
284 }
285 }
286
287 return $fields;
288 }
289
290 /**
291 * Get translations of serialized Stream Network settings
292 *
293 * @filter wp_stream_serialized_labels
294 *
295 * @return array Multidimensional array of fields
296 */
297 public function get_settings_translations( $labels ) {
298 $network_key = $this->plugin->settings->network_options_key;
299
300 if ( ! isset( $labels[ $network_key ] ) ) {
301 $labels[ $network_key ] = array();
302 }
303
304 foreach ( $this->plugin->settings->get_fields() as $section_slug => $section ) {
305 foreach ( $section['fields'] as $field ) {
306 $labels[ $network_key ][ sprintf( '%s_%s', $section_slug, $field['name'] ) ] = $field['title'];
307 }
308 }
309
310 return $labels;
311 }
312
313 /**
314 * Wrapper for the settings API to work on the network settings page
315 */
316 public function network_options_action() {
317 $allowed_referers = array(
318 $this->network_settings_page_slug,
319 $this->default_settings_page_slug,
320 );
321
322 if ( ! isset( $_GET['action'] ) || ! in_array( $_GET['action'], $allowed_referers, true ) ) {
323 return;
324 }
325
326 // @codingStandardsIgnoreStart
327 $options = isset( $_POST['option_page'] ) ? explode( ',', stripslashes( $_POST['option_page'] ) ) : null;
328 // @codingStandardsIgnoreEnd
329
330 if ( $options ) {
331
332 foreach ( $options as $option ) {
333 $option = trim( $option );
334 $value = null;
335 $sections = $this->plugin->settings->get_fields();
336
337 foreach ( $sections as $section_name => $section ) {
338 foreach ( $section['fields'] as $field_idx => $field ) {
339 $option_key = $section_name . '_' . $field['name'];
340
341 // @codingStandardsIgnoreStart
342 if ( isset( $_POST[ $option ][ $option_key ] ) ) {
343 $value[ $option_key ] = $_POST[ $option ][ $option_key ];
344 } else {
345 $value[ $option_key ] = false;
346 }
347 // @codingStandardsIgnoreEnd
348 }
349 }
350
351 if ( ! is_array( $value ) ) {
352 $value = trim( $value );
353 }
354
355 update_site_option( $option, $value );
356 }
357 }
358
359 if ( ! count( get_settings_errors() ) ) {
360 add_settings_error( 'general', 'settings_updated', __( 'Settings saved.', 'stream' ), 'updated' );
361 }
362
363 set_transient( 'settings_errors', get_settings_errors(), 30 );
364
365 $go_back = add_query_arg( 'settings-updated', 'true', wp_get_referer() );
366
367 wp_redirect( $go_back );
368
369 exit;
370 }
371
372 /**
373 * Add the Site filter to the Network records screen
374 *
375 * @filter wp_stream_list_table_filters
376 *
377 * @param $filters
378 *
379 * @return array
380 */
381 public function list_table_filters( $filters ) {
382 if ( ! is_network_admin() || wp_is_large_network() ) {
383 return $filters;
384 }
385
386 $blogs = array();
387
388 // Display network blog as the first option
389 $network_blog = $this->get_network_blog();
390
391 $blogs[ $network_blog->blog_id ] = array(
392 'label' => $network_blog->blogname,
393 'disabled' => '',
394 );
395
396 // add all sites
397 foreach ( wp_get_sites() as $blog ) {
398 $blog_data = get_blog_details( $blog );
399
400 $blogs[ $blog['blog_id'] ] = array(
401 'label' => $blog_data->blogname,
402 'disabled' => '',
403 );
404 }
405
406 $filters['blog_id'] = array(
407 'title' => __( 'sites', 'stream' ),
408 'items' => $blogs,
409 );
410
411 return $filters;
412 }
413
414 /**
415 * Add the Site toggle to screen options in network admin
416 *
417 * @param $filters
418 *
419 * @return array
420 */
421 public function toggle_filters( $filters ) {
422 if ( is_network_admin() ) {
423 $filters['blog_id'] = esc_html__( 'Site', 'stream' );
424 }
425
426 return $filters;
427 }
428
429 /**
430 * Add the network suffix to the $screen_id when in the network admin
431 *
432 * @param $screen_id
433 *
434 * @return string
435 */
436 public function list_table_screen_id( $screen_id ) {
437 if ( $screen_id && is_network_admin() ) {
438 if ( '-network' !== substr( $screen_id, -8 ) ) {
439 $screen_id .= '-network';
440 }
441 }
442
443 return $screen_id;
444 }
445
446 /**
447 * Set blog_id for network admin activity
448 *
449 * @return int
450 */
451 public function blog_id_logged( $blog_id ) {
452 return is_network_admin() ? 0 : $blog_id;
453 }
454
455 /**
456 * Customize query args on multisite installs
457 *
458 * @filter wp_stream_query_args
459 *
460 * @param array $args
461 *
462 * @return array
463 */
464 public function network_query_args( $args ) {
465 $args['site_id'] = is_numeric( $args['site_id'] ) ? $args['site_id'] : get_current_site()->id;
466 $args['blog_id'] = is_numeric( $args['blog_id'] ) ? $args['blog_id'] : ( is_network_admin() ? null : get_current_blog_id() );
467
468 return $args;
469 }
470
471 /**
472 * Add site count to the page title in the network admin
473 *
474 * @filter wp_stream_admin_page_title
475 *
476 * @param string $page_title
477 *
478 * @return string
479 */
480 public function network_admin_page_title( $page_title ) {
481 if ( is_network_admin() ) {
482 $site_count = sprintf( _n( '1 site', '%s sites', get_blog_count(), 'stream' ), number_format( get_blog_count() ) );
483 $page_title = sprintf( '%s (%s)', $page_title, $site_count );
484 }
485
486 return $page_title;
487 }
488
489 /**
490 * Add the Site column to the network stream records
491 *
492 * @param $columns
493 *
494 * @return mixed
495 */
496 public function network_admin_columns( $columns ) {
497 if ( is_network_admin() ) {
498 $columns = array_merge(
499 array_slice( $columns, 0, -1 ),
500 array(
501 'blog_id' => esc_html__( 'Site', 'stream' ),
502 ),
503 array_slice( $columns, -1 )
504 );
505 }
506
507 return $columns;
508 }
509
510 /**
511 * Prevent the Blogs connector from loading when not in Network Admin
512 *
513 * @param $connectors
514 *
515 * @return mixed
516 */
517 public function hide_blogs_connector( $connectors ) {
518 if ( ! is_network_admin() ) {
519 return array_diff( $connectors, array( 'Connector_Blogs' ) );
520 }
521
522 return $connectors;
523 }
524 }
525