PluginProbe ʕ •ᴥ•ʔ
MainWP Child Reports / 2.0.3
MainWP Child Reports v2.0.3
0.0.1 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9.1 1.9.2 1.9.3 2.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.1 2.1.1 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.3 2.3.1 trunk
mainwp-child-reports / classes / class-network.php
mainwp-child-reports / classes Last commit date
class-admin.php 6 years ago class-alert-trigger.php 6 years ago class-alert-type.php 6 years ago class-alert.php 6 years ago class-alerts-list.php 6 years ago class-alerts.php 6 years ago class-author.php 6 years ago class-cli.php 6 years ago class-connector.php 6 years ago class-connectors.php 6 years ago class-date-interval.php 6 years ago class-db-driver-wpdb.php 6 years ago class-db-driver.php 6 years ago class-db.php 6 years ago class-export.php 6 years ago class-exporter.php 6 years ago class-filter-input.php 6 years ago class-form-generator.php 6 years ago class-install.php 6 years ago class-list-table.php 6 years ago class-live-update.php 6 years ago class-log.php 6 years ago class-mainwp-child-report-helper.php 6 years ago class-network.php 6 years ago class-plugin.php 6 years ago class-preview-list-table.php 6 years ago class-query.php 6 years ago class-record.php 6 years ago class-settings.php 6 years ago class-uninstall.php 6 years ago debug.log 6 years ago
class-network.php
527 lines
1 <?php
2 namespace WP_MainWP_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_mainwp_stream_network_settings';
12
13 public $default_settings_page_slug = 'wp_mainwp_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_mainwp_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
31 // DISABLED
32 //add_action( 'network_admin_menu', array( $this->plugin->admin, 'register_menu' ) );
33 //add_action( 'network_admin_menu', array( $this, 'admin_menu_screens' ) );
34 //add_action( 'admin_menu', array( $this, 'admin_menu_screens' ) );
35
36 add_action( 'network_admin_notices', array( $this->plugin->admin, 'admin_notices' ) );
37 add_action( 'wpmuadminedit', array( $this, 'network_options_action' ) );
38 add_action( 'update_site_option_' . $this->plugin->settings->network_options_key, array( $this, 'updated_option_ttl_remove_records' ), 10, 3 );
39
40 // Filters
41 add_filter( 'wp_mainwp_stream_blog_id_logged', array( $this, 'blog_id_logged' ) );
42 add_filter( 'wp_mainwp_stream_admin_page_title', array( $this, 'network_admin_page_title' ) );
43 add_filter( 'wp_mainwp_stream_list_table_screen_id', array( $this, 'list_table_screen_id' ) );
44 add_filter( 'wp_mainwp_stream_list_table_filters', array( $this, 'list_table_filters' ) );
45 add_filter( 'wp_mainwp_stream_list_table_columns', array( $this, 'network_admin_columns' ) );
46 add_filter( 'wp_mainwp_stream_settings_form_action', array( $this, 'settings_form_action' ) );
47 add_filter( 'wp_mainwp_stream_settings_form_description', array( $this, 'settings_form_description' ) );
48 //add_filter( 'wp_mainwp_stream_settings_option_fields', array( $this, 'get_network_admin_fields' ) );
49 add_filter( 'wp_mainwp_stream_serialized_labels', array( $this, 'get_settings_translations' ) );
50 add_filter( 'wp_mainwp_stream_connectors', array( $this, 'hide_blogs_connector' ) );
51 }
52
53 /**
54 * Workaround to get admin-ajax.php to know when the request is from the Network Admin
55 *
56 * @return bool
57 *
58 * @action init
59 *
60 * @see https://core.trac.wordpress.org/ticket/22589
61 */
62 public function ajax_network_admin() {
63 if (
64 defined( 'DOING_AJAX' )
65 &&
66 DOING_AJAX
67 &&
68 preg_match( '#^' . network_admin_url() . '#i', $_SERVER['HTTP_REFERER'] )
69 ) {
70 define( 'WP_NETWORK_ADMIN', true );
71 return WP_NETWORK_ADMIN;
72 }
73
74 return false;
75 }
76
77 /**
78 * Builds a stdClass object used when displaying actions done in network administration
79 *
80 * @return object
81 */
82 public function get_network_blog() {
83 $blog = new \stdClass();
84 $blog->blog_id = 0;
85 $blog->blogname = esc_html__( 'Network Admin', 'mainwp-child-reports' );
86
87 return $blog;
88 }
89
90 /**
91 * Returns true if Stream is network activated, otherwise false
92 *
93 * @return bool
94 */
95 public function is_network_activated() {
96 if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
97 require_once ABSPATH . '/wp-admin/includes/plugin.php';
98 }
99
100 if ( $this->is_mustuse() ) {
101 return true;
102 }
103
104 return is_plugin_active_for_network( $this->plugin->locations['plugin'] );
105 }
106
107 /**
108 * Returns true if Stream is a must-use plugin, otherwise false
109 *
110 * @return bool
111 */
112 public function is_mustuse() {
113
114 $stream_php = trailingslashit( WPMU_PLUGIN_DIR ) . $this->plugin->locations['plugin'];
115
116 if ( file_exists( $stream_php ) && class_exists( 'WP_MainWP_Stream\Plugin' ) ) {
117 return true;
118 }
119
120 return false;
121 }
122
123 /**
124 * Add Network Settings and Default Settings menu items
125 *
126 * @return array
127 */
128 // DISABLED
129 public function admin_menu_screens() {
130 if ( ! is_network_admin() ) {
131 return;
132 }
133
134 remove_submenu_page( $this->plugin->admin->records_page_slug, 'wp_mainwp_stream_settings' );
135 remove_submenu_page( $this->plugin->admin->records_page_slug, 'edit.php?post_type=wp_mainwp_alerts' );
136
137 $this->plugin->admin->screen_id['network_settings'] = add_submenu_page(
138 $this->plugin->admin->records_page_slug,
139 __( 'Reports Network Settings', 'mainwp-child-reports' ),
140 __( 'Network Settings', 'mainwp-child-reports' ),
141 $this->plugin->admin->settings_cap,
142 $this->network_settings_page_slug,
143 array( $this->plugin->admin, 'render_settings_page' )
144 );
145 }
146
147 /**
148 * Remove records when records TTL is shortened
149 *
150 * @param string $option_key
151 * @param array $old_value
152 * @param array $new_value
153 *
154 * @action update_option_wp_stream
155 * @return void
156 */
157 public function updated_option_ttl_remove_records( $option_key, $new_value, $old_value ) {
158 unset( $option_key );
159 $this->plugin->settings->updated_option_ttl_remove_records( $old_value, $new_value );
160 }
161
162 /**
163 * Adjust the action of the settings form when in the Network Admin
164 *
165 * @param $action
166 *
167 * @return string
168 */
169 public function settings_form_action( $action ) {
170 if ( is_network_admin() ) {
171 $current_page = wp_mainwp_stream_filter_input( INPUT_GET, 'page' );
172 $action = add_query_arg(
173 array(
174 'action' => $current_page,
175 ), 'edit.php'
176 );
177 }
178
179 return $action;
180 }
181
182 /**
183 * Add a description to each of the Settings pages in the Network Admin
184 *
185 * @param $description
186 *
187 * @return string
188 */
189 public function settings_form_description( $description ) {
190 if ( ! is_network_admin() ) {
191 return '';
192 }
193
194 $current_page = wp_mainwp_stream_filter_input( INPUT_GET, 'page' );
195
196 switch ( $current_page ) {
197 case $this->network_settings_page_slug:
198 $description = __( 'These settings apply to all sites on the network.', 'mainwp-child-reports' );
199 break;
200 case $this->default_settings_page_slug:
201 $description = __( 'These default settings will apply to new sites created on the network. These settings do not alter existing sites.', 'mainwp-child-reports' );
202 break;
203 }
204
205 return $description;
206 }
207
208 /**
209 * Adjusts the settings fields displayed in various network admin screens
210 *
211 * @param $fields
212 *
213 * @return mixed
214 */
215 public function get_network_admin_fields( $fields ) {
216 if ( ! $this->is_network_activated() ) {
217 return $fields;
218 }
219
220 $stream_hidden_options = apply_filters(
221 'wp_mainwp_stream_hidden_option_fields',
222 array(
223 'general' => array(
224 'records_ttl',
225 ),
226 'advanced' => array(
227 'delete_all_records',
228 ),
229 )
230 );
231
232 $network_hidden_options = apply_filters(
233 'wp_mainwp_stream_network_option_fields',
234 array(
235 'general' => array(
236 'role_access',
237 ),
238 'exclude' => array(
239 'authors',
240 'roles',
241 'connectors',
242 'contexts',
243 'actions',
244 'ip_addresses',
245 'hide_previous_records',
246 ),
247 )
248 );
249
250 // Remove settings based on context
251 if ( $this->plugin->settings->network_options_key === $this->plugin->settings->option_key ) {
252 $hidden_options = $network_hidden_options;
253 } else {
254 $hidden_options = $stream_hidden_options;
255 }
256
257 foreach ( $fields as $section_key => $section ) {
258 foreach ( $section['fields'] as $key => $field ) {
259 if ( ! isset( $hidden_options[ $section_key ] ) ) {
260 continue;
261 }
262
263 if ( in_array( $field['name'], $hidden_options[ $section_key ], true ) ) {
264 unset( $fields[ $section_key ]['fields'][ $key ] );
265 }
266 }
267 }
268
269 // Add settings based on context
270 if ( $this->plugin->settings->network_options_key === $this->plugin->settings->option_key ) {
271 $new_fields['general']['fields'][] = array(
272 'name' => 'site_access',
273 'title' => __( 'Site Access', 'mainwp-child-reports' ),
274 'after_field' => __( 'Enabled', 'mainwp-child-reports' ),
275 'default' => 1,
276 'desc' => __( 'Allow sites on this network to view their Reports activity. Leave unchecked to only allow Reports to be viewed in the Network Admin.', 'mainwp-child-reports' ),
277 'type' => 'checkbox',
278 );
279
280 $fields = array_merge_recursive( $new_fields, $fields );
281 }
282
283 // Remove empty settings sections
284 foreach ( $fields as $section_key => $section ) {
285 if ( empty( $section['fields'] ) ) {
286 unset( $fields[ $section_key ] );
287 }
288 }
289
290 return $fields;
291 }
292
293 /**
294 * Get translations of serialized Stream Network settings
295 *
296 * @filter wp_mainwp_stream_serialized_labels
297 *
298 * @return array Multidimensional array of fields
299 */
300 public function get_settings_translations( $labels ) {
301 $network_key = $this->plugin->settings->network_options_key;
302
303 if ( ! isset( $labels[ $network_key ] ) ) {
304 $labels[ $network_key ] = array();
305 }
306
307 foreach ( $this->plugin->settings->get_fields() as $section_slug => $section ) {
308 foreach ( $section['fields'] as $field ) {
309 $labels[ $network_key ][ sprintf( '%s_%s', $section_slug, $field['name'] ) ] = $field['title'];
310 }
311 }
312
313 return $labels;
314 }
315
316 /**
317 * Wrapper for the settings API to work on the network settings page
318 */
319 public function network_options_action() {
320 $allowed_referers = array(
321 $this->network_settings_page_slug,
322 $this->default_settings_page_slug,
323 );
324
325 if ( ! isset( $_GET['action'] ) || ! in_array( $_GET['action'], $allowed_referers, true ) ) { // CSRF okay
326 return;
327 }
328
329 $options = isset( $_POST['option_page'] ) ? explode( ',', stripslashes( $_POST['option_page'] ) ) : null; // CSRF okay
330
331 if ( $options ) {
332
333 foreach ( $options as $option ) {
334 $option = trim( $option );
335 $value = null;
336 $sections = $this->plugin->settings->get_fields();
337
338 foreach ( $sections as $section_name => $section ) {
339 foreach ( $section['fields'] as $field_idx => $field ) {
340 $option_key = $section_name . '_' . $field['name'];
341
342 // @codingStandardsIgnoreStart
343 if ( isset( $_POST[ $option ][ $option_key ] ) ) {
344 $value[ $option_key ] = $_POST[ $option ][ $option_key ];
345 } else {
346 $value[ $option_key ] = false;
347 }
348 // @codingStandardsIgnoreEnd
349 }
350 }
351
352 if ( ! is_array( $value ) ) {
353 $value = trim( $value );
354 }
355
356 update_site_option( $option, $value );
357 }
358 }
359
360 if ( ! count( get_settings_errors() ) ) {
361 add_settings_error( 'general', 'settings_updated', __( 'Settings saved.', 'mainwp-child-reports' ), 'updated' );
362 }
363
364 set_transient( 'settings_errors', get_settings_errors(), 30 );
365
366 $go_back = add_query_arg( 'settings-updated', 'true', wp_get_referer() );
367
368 wp_redirect( $go_back );
369
370 exit;
371 }
372
373 /**
374 * Add the Site filter to the Network records screen
375 *
376 * @filter wp_mainwp_stream_list_table_filters
377 *
378 * @param $filters
379 *
380 * @return array
381 */
382 public function list_table_filters( $filters ) {
383 if ( ! is_network_admin() || wp_is_large_network() ) {
384 return $filters;
385 }
386
387 $blogs = array();
388
389 // Display network blog as the first option
390 $network_blog = $this->get_network_blog();
391
392 $blogs[ $network_blog->blog_id ] = array(
393 'label' => $network_blog->blogname,
394 'disabled' => '',
395 );
396
397 // add all sites
398 foreach ( wp_mainwp_stream_get_sites() as $blog ) {
399 $blog_data = get_blog_details( $blog->blog_id );
400
401 $blogs[ $blog->blog_id ] = array(
402 'label' => $blog_data->blogname,
403 'disabled' => '',
404 );
405 }
406
407 $filters['blog_id'] = array(
408 'title' => __( 'sites', 'mainwp-child-reports' ),
409 'items' => $blogs,
410 );
411
412 return $filters;
413 }
414
415 /**
416 * Add the Site toggle to screen options in network admin
417 *
418 * @param $filters
419 *
420 * @return array
421 */
422 public function toggle_filters( $filters ) {
423 if ( is_network_admin() ) {
424 $filters['blog_id'] = esc_html__( 'Site', 'mainwp-child-reports' );
425 }
426
427 return $filters;
428 }
429
430 /**
431 * Add the network suffix to the $screen_id when in the network admin
432 *
433 * @param $screen_id
434 *
435 * @return string
436 */
437 public function list_table_screen_id( $screen_id ) {
438 if ( $screen_id && is_network_admin() ) {
439 if ( '-network' !== substr( $screen_id, -8 ) ) {
440 $screen_id .= '-network';
441 }
442 }
443
444 return $screen_id;
445 }
446
447 /**
448 * Set blog_id for network admin activity
449 *
450 * @return int
451 */
452 public function blog_id_logged( $blog_id ) {
453 return is_network_admin() ? 0 : $blog_id;
454 }
455
456 /**
457 * Customize query args on multisite installs
458 *
459 * @filter wp_mainwp_stream_query_args
460 *
461 * @param array $args
462 *
463 * @return array
464 */
465 public function network_query_args( $args ) {
466 $args['site_id'] = is_numeric( $args['site_id'] ) ? $args['site_id'] : get_current_site()->id;
467 $args['blog_id'] = is_numeric( $args['blog_id'] ) ? $args['blog_id'] : ( is_network_admin() ? null : get_current_blog_id() );
468
469 return $args;
470 }
471
472 /**
473 * Add site count to the page title in the network admin
474 *
475 * @filter wp_mainwp_stream_admin_page_title
476 *
477 * @param string $page_title
478 *
479 * @return string
480 */
481 public function network_admin_page_title( $page_title ) {
482 if ( is_network_admin() ) {
483 // translators: Placeholder refers to a number of sites on the network (e.g. "42")
484 $site_count = sprintf( _n( '%d site', '%d sites', get_blog_count(), 'mainwp-child-reports' ), 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() || $this->ajax_network_admin() ) {
500 $columns = array_merge(
501 array_slice( $columns, 0, -1 ),
502 array(
503 'blog_id' => esc_html__( 'Site', 'mainwp-child-reports' ),
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