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