| @@ -1,32 +1,58 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * Manages functionality for the Stream Admin pages on both | |
| 4 | + * single and multi-sites. | |
| 5 | + * | |
| 6 | + * @package WP_Stream | |
| 7 | + */ | |
| 8 | + | |
| 2 | 9 | namespace WP_Stream; |
| 3 | 10 | |
| 11 | +/** | |
| 12 | + * Class - Network | |
| 13 | + */ | |
| 4 | 14 | class Network { |
| 5 | 15 | /** |
| 6 | - * Hold Plugin class | |
| 16 | + * Holds instance of plugin object | |
| 17 | + * | |
| 7 | 18 | * @var Plugin |
| 8 | 19 | */ |
| 9 | 20 | public $plugin; |
| 10 | 21 | |
| 22 | + /** | |
| 23 | + * Network page slug | |
| 24 | + * | |
| 25 | + * @var string | |
| 26 | + */ | |
| 11 | 27 | public $network_settings_page_slug = 'wp_stream_network_settings'; |
| 12 | 28 | |
| 29 | + /** | |
| 30 | + * Default setting page slug | |
| 31 | + * | |
| 32 | + * @var string | |
| 33 | + */ | |
| 13 | 34 | public $default_settings_page_slug = 'wp_stream_default_settings'; |
| 14 | 35 | |
| 15 | - function __construct( $plugin ) { | |
| 36 | + /** | |
| 37 | + * Class constructor | |
| 38 | + * | |
| 39 | + * @param Plugin $plugin Instance of plugin object. | |
| 40 | + */ | |
| 41 | + public function __construct( $plugin ) { | |
| 16 | 42 | $this->plugin = $plugin; |
| 17 | 43 | |
| 18 | - // Always add default site_id/blog_id params when multisite | |
| 44 | + // Always add default site_id/blog_id params when multisite. | |
| 19 | 45 | if ( is_multisite() ) { |
| 20 | 46 | add_filter( 'wp_stream_query_args', array( $this, 'network_query_args' ) ); |
| 21 | 47 | } |
| 22 | 48 | |
| 23 | - // Bail early if not network-activated | |
| 49 | + // Bail early if not network-activated. | |
| 24 | 50 | if ( ! $this->is_network_activated() ) { |
| 25 | 51 | return; |
| 26 | 52 | } |
| 27 | 53 | |
| 28 | - // Actions | |
| 54 | + // Actions. | |
| 29 | 55 | add_action( 'init', array( $this, 'ajax_network_admin' ) ); |
| 30 | 56 | add_action( 'network_admin_menu', array( $this->plugin->admin, 'register_menu' ) ); |
| 31 | 57 | add_action( 'network_admin_menu', array( $this, 'admin_menu_screens' ) ); |
| 32 | 58 | add_action( 'admin_menu', array( $this, 'admin_menu_screens' ) ); |
| @@ -34,9 +60,9 @@ | ||
| 34 | 60 | add_action( 'network_admin_notices', array( $this->plugin->admin, 'admin_notices' ) ); |
| 35 | 61 | add_action( 'wpmuadminedit', array( $this, 'network_options_action' ) ); |
| 36 | 62 | add_action( 'update_site_option_' . $this->plugin->settings->network_options_key, array( $this, 'updated_option_ttl_remove_records' ), 10, 3 ); |
| 37 | 63 | |
| 38 | - // Filters | |
| 64 | + // Filters. | |
| 39 | 65 | add_filter( 'wp_stream_blog_id_logged', array( $this, 'blog_id_logged' ) ); |
| 40 | 66 | add_filter( 'wp_stream_admin_page_title', array( $this, 'network_admin_page_title' ) ); |
| 41 | 67 | add_filter( 'wp_stream_list_table_screen_id', array( $this, 'list_table_screen_id' ) ); |
| 42 | 68 | add_filter( 'wp_stream_list_table_filters', array( $this, 'list_table_filters' ) ); |
| @@ -50,8 +76,10 @@ | ||
| 50 | 76 | |
| 51 | 77 | /** |
| 52 | 78 | * Workaround to get admin-ajax.php to know when the request is from the Network Admin |
| 53 | 79 | * |
| 80 | + * @return bool | |
| 81 | + * | |
| 54 | 82 | * @action init |
| 55 | 83 | * |
| 56 | 84 | * @see https://core.trac.wordpress.org/ticket/22589 |
| 57 | 85 | */ |
| @@ -63,9 +91,12 @@ | ||
| 63 | 91 | && |
| 64 | 92 | preg_match( '#^' . network_admin_url() . '#i', $_SERVER['HTTP_REFERER'] ) |
| 65 | 93 | ) { |
| 66 | 94 | define( 'WP_NETWORK_ADMIN', true ); |
| 95 | + return WP_NETWORK_ADMIN; | |
| 67 | 96 | } |
| 97 | + | |
| 98 | + return false; | |
| 68 | 99 | } |
| 69 | 100 | |
| 70 | 101 | /** |
| 71 | 102 | * Builds a stdClass object used when displaying actions done in network administration |
| @@ -72,9 +103,9 @@ | ||
| 72 | 103 | * |
| 73 | 104 | * @return object |
| 74 | 105 | */ |
| 75 | 106 | public function get_network_blog() { |
| 76 | - $blog = new \stdClass; | |
| 107 | + $blog = new \stdClass(); | |
| 77 | 108 | $blog->blog_id = 0; |
| 78 | 109 | $blog->blogname = esc_html__( 'Network Admin', 'stream' ); |
| 79 | 110 | |
| 80 | 111 | return $blog; |
| @@ -85,13 +116,9 @@ | ||
| 85 | 116 | * |
| 86 | 117 | * @return bool |
| 87 | 118 | */ |
| 88 | 119 | 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'] ); | |
| 120 | + return $this->plugin->is_network_activated(); | |
| 94 | 121 | } |
| 95 | 122 | |
| 96 | 123 | /** |
| 97 | 124 | * Adds Stream to the admin bar under the "My Sites > Network Admin" menu |
| @@ -98,9 +125,9 @@ | ||
| 98 | 125 | * if Stream has been network-activated. |
| 99 | 126 | * |
| 100 | 127 | * @action admin_bar_menu |
| 101 | 128 | * |
| 102 | - * @param object $admin_bar | |
| 129 | + * @param object $admin_bar Admin bar object. | |
| 103 | 130 | * |
| 104 | 131 | * @return void |
| 105 | 132 | */ |
| 106 | 133 | public function network_admin_bar_menu( $admin_bar ) { |
| @@ -125,9 +152,9 @@ | ||
| 125 | 152 | ); |
| 126 | 153 | } |
| 127 | 154 | |
| 128 | 155 | /** |
| 129 | - * Add Network Settings and Default Settings menu items | |
| 156 | + * Add Network Settings and Default Settings menu pages | |
| 130 | 157 | * |
| 131 | 158 | * @return array |
| 132 | 159 | */ |
| 133 | 160 | public function admin_menu_screens() { |
| @@ -135,8 +162,9 @@ | ||
| 135 | 162 | return; |
| 136 | 163 | } |
| 137 | 164 | |
| 138 | 165 | remove_submenu_page( $this->plugin->admin->records_page_slug, 'wp_stream_settings' ); |
| 166 | + remove_submenu_page( $this->plugin->admin->records_page_slug, 'edit.php?post_type=wp_stream_alerts' ); | |
| 139 | 167 | |
| 140 | 168 | $this->plugin->admin->screen_id['network_settings'] = add_submenu_page( |
| 141 | 169 | $this->plugin->admin->records_page_slug, |
| 142 | 170 | __( 'Stream Network Settings', 'stream' ), |
| @@ -149,11 +177,11 @@ | ||
| 149 | 177 | |
| 150 | 178 | /** |
| 151 | 179 | * Remove records when records TTL is shortened |
| 152 | 180 | * |
| 153 | - * @param string $option_key | |
| 154 | - * @param array $old_value | |
| 155 | - * @param array $new_value | |
| 181 | + * @param string $option_key Unused. | |
| 182 | + * @param array $new_value New value. | |
| 183 | + * @param array $old_value Old value. | |
| 156 | 184 | * |
| 157 | 185 | * @action update_option_wp_stream |
| 158 | 186 | * @return void |
| 159 | 187 | */ |
| @@ -164,9 +192,9 @@ | ||
| 164 | 192 | |
| 165 | 193 | /** |
| 166 | 194 | * Adjust the action of the settings form when in the Network Admin |
| 167 | 195 | * |
| 168 | - * @param $action | |
| 196 | + * @param string $action Query string. | |
| 169 | 197 | * |
| 170 | 198 | * @return string |
| 171 | 199 | */ |
| 172 | 200 | public function settings_form_action( $action ) { |
| @@ -171,9 +199,14 @@ | ||
| 171 | 199 | */ |
| 172 | 200 | public function settings_form_action( $action ) { |
| 173 | 201 | if ( is_network_admin() ) { |
| 174 | 202 | $current_page = wp_stream_filter_input( INPUT_GET, 'page' ); |
| 175 | - $action = add_query_arg( array( 'action' => $current_page ), 'edit.php' ); | |
| 203 | + $action = add_query_arg( | |
| 204 | + array( | |
| 205 | + 'action' => $current_page, | |
| 206 | + ), | |
| 207 | + 'edit.php' | |
| 208 | + ); | |
| 176 | 209 | } |
| 177 | 210 | |
| 178 | 211 | return $action; |
| 179 | 212 | } |
| @@ -180,9 +213,9 @@ | ||
| 180 | 213 | |
| 181 | 214 | /** |
| 182 | 215 | * Add a description to each of the Settings pages in the Network Admin |
| 183 | 216 | * |
| 184 | - * @param $description | |
| 217 | + * @param string $description Description of the current page. | |
| 185 | 218 | * |
| 186 | 219 | * @return string |
| 187 | 220 | */ |
| 188 | 221 | public function settings_form_description( $description ) { |
| @@ -192,12 +225,12 @@ | ||
| 192 | 225 | |
| 193 | 226 | $current_page = wp_stream_filter_input( INPUT_GET, 'page' ); |
| 194 | 227 | |
| 195 | 228 | switch ( $current_page ) { |
| 196 | - case $this->network_settings_page_slug : | |
| 229 | + case $this->network_settings_page_slug: | |
| 197 | 230 | $description = __( 'These settings apply to all sites on the network.', 'stream' ); |
| 198 | 231 | break; |
| 199 | - case $this->default_settings_page_slug : | |
| 232 | + case $this->default_settings_page_slug: | |
| 200 | 233 | $description = __( 'These default settings will apply to new sites created on the network. These settings do not alter existing sites.', 'stream' ); |
| 201 | 234 | break; |
| 202 | 235 | } |
| 203 | 236 | |
| @@ -206,9 +239,9 @@ | ||
| 206 | 239 | |
| 207 | 240 | /** |
| 208 | 241 | * Adjusts the settings fields displayed in various network admin screens |
| 209 | 242 | * |
| 210 | - * @param $fields | |
| 243 | + * @param array $fields Page settings fields. | |
| 211 | 244 | * |
| 212 | 245 | * @return mixed |
| 213 | 246 | */ |
| 214 | 247 | public function get_network_admin_fields( $fields ) { |
| @@ -218,11 +251,13 @@ | ||
| 218 | 251 | |
| 219 | 252 | $stream_hidden_options = apply_filters( |
| 220 | 253 | 'wp_stream_hidden_option_fields', |
| 221 | 254 | array( |
| 222 | - 'general' => array( | |
| 255 | + 'general' => array( | |
| 256 | + 'records_ttl', | |
| 257 | + ), | |
| 258 | + 'advanced' => array( | |
| 223 | 259 | 'delete_all_records', |
| 224 | - 'records_ttl', | |
| 225 | 260 | ), |
| 226 | 261 | ) |
| 227 | 262 | ); |
| 228 | 263 | |
| @@ -243,9 +278,9 @@ | ||
| 243 | 278 | ), |
| 244 | 279 | ) |
| 245 | 280 | ); |
| 246 | 281 | |
| 247 | - // Remove settings based on context | |
| 282 | + // Remove settings based on context. | |
| 248 | 283 | if ( $this->plugin->settings->network_options_key === $this->plugin->settings->option_key ) { |
| 249 | 284 | $hidden_options = $network_hidden_options; |
| 250 | 285 | } else { |
| 251 | 286 | $hidden_options = $stream_hidden_options; |
| @@ -262,9 +297,9 @@ | ||
| 262 | 297 | } |
| 263 | 298 | } |
| 264 | 299 | } |
| 265 | 300 | |
| 266 | - // Add settings based on context | |
| 301 | + // Add settings based on context. | |
| 267 | 302 | if ( $this->plugin->settings->network_options_key === $this->plugin->settings->option_key ) { |
| 268 | 303 | $new_fields['general']['fields'][] = array( |
| 269 | 304 | 'name' => 'site_access', |
| 270 | 305 | 'title' => __( 'Site Access', 'stream' ), |
| @@ -276,9 +311,9 @@ | ||
| 276 | 311 | |
| 277 | 312 | $fields = array_merge_recursive( $new_fields, $fields ); |
| 278 | 313 | } |
| 279 | 314 | |
| 280 | - // Remove empty settings sections | |
| 315 | + // Remove empty settings sections. | |
| 281 | 316 | foreach ( $fields as $section_key => $section ) { |
| 282 | 317 | if ( empty( $section['fields'] ) ) { |
| 283 | 318 | unset( $fields[ $section_key ] ); |
| 284 | 319 | } |
| @@ -291,8 +326,10 @@ | ||
| 291 | 326 | * Get translations of serialized Stream Network settings |
| 292 | 327 | * |
| 293 | 328 | * @filter wp_stream_serialized_labels |
| 294 | 329 | * |
| 330 | + * @param array $labels Setting labels. | |
| 331 | + * | |
| 295 | 332 | * @return array Multidimensional array of fields |
| 296 | 333 | */ |
| 297 | 334 | public function get_settings_translations( $labels ) { |
| 298 | 335 | $network_key = $this->plugin->settings->network_options_key; |
| @@ -318,15 +355,15 @@ | ||
| 318 | 355 | $this->network_settings_page_slug, |
| 319 | 356 | $this->default_settings_page_slug, |
| 320 | 357 | ); |
| 321 | 358 | |
| 359 | + // @codingStandardsIgnoreLine | |
| 322 | 360 | if ( ! isset( $_GET['action'] ) || ! in_array( $_GET['action'], $allowed_referers, true ) ) { |
| 323 | 361 | return; |
| 324 | 362 | } |
| 325 | 363 | |
| 326 | - // @codingStandardsIgnoreStart | |
| 364 | + // @codingStandardsIgnoreLine | |
| 327 | 365 | $options = isset( $_POST['option_page'] ) ? explode( ',', stripslashes( $_POST['option_page'] ) ) : null; |
| 328 | - // @codingStandardsIgnoreEnd | |
| 329 | 366 | |
| 330 | 367 | if ( $options ) { |
| 331 | 368 | |
| 332 | 369 | foreach ( $options as $option ) { |
| @@ -363,9 +400,9 @@ | ||
| 363 | 400 | set_transient( 'settings_errors', get_settings_errors(), 30 ); |
| 364 | 401 | |
| 365 | 402 | $go_back = add_query_arg( 'settings-updated', 'true', wp_get_referer() ); |
| 366 | 403 | |
| 367 | - wp_redirect( $go_back ); | |
| 404 | + wp_safe_redirect( $go_back ); | |
| 368 | 405 | |
| 369 | 406 | exit; |
| 370 | 407 | } |
| 371 | 408 | |
| @@ -373,9 +410,9 @@ | ||
| 373 | 410 | * Add the Site filter to the Network records screen |
| 374 | 411 | * |
| 375 | 412 | * @filter wp_stream_list_table_filters |
| 376 | 413 | * |
| 377 | - * @param $filters | |
| 414 | + * @param array $filters Filters. | |
| 378 | 415 | * |
| 379 | 416 | * @return array |
| 380 | 417 | */ |
| 381 | 418 | public function list_table_filters( $filters ) { |
| @@ -384,9 +421,9 @@ | ||
| 384 | 421 | } |
| 385 | 422 | |
| 386 | 423 | $blogs = array(); |
| 387 | 424 | |
| 388 | - // Display network blog as the first option | |
| 425 | + // Display network blog as the first option. | |
| 389 | 426 | $network_blog = $this->get_network_blog(); |
| 390 | 427 | |
| 391 | 428 | $blogs[ $network_blog->blog_id ] = array( |
| 392 | 429 | 'label' => $network_blog->blogname, |
| @@ -392,9 +429,9 @@ | ||
| 392 | 429 | 'label' => $network_blog->blogname, |
| 393 | 430 | 'disabled' => '', |
| 394 | 431 | ); |
| 395 | 432 | |
| 396 | - // add all sites | |
| 433 | + // Add all sites. | |
| 397 | 434 | foreach ( wp_stream_get_sites() as $blog ) { |
| 398 | 435 | $blog_data = get_blog_details( $blog->blog_id ); |
| 399 | 436 | |
| 400 | 437 | $blogs[ $blog->blog_id ] = array( |
| @@ -413,9 +450,9 @@ | ||
| 413 | 450 | |
| 414 | 451 | /** |
| 415 | 452 | * Add the Site toggle to screen options in network admin |
| 416 | 453 | * |
| 417 | - * @param $filters | |
| 454 | + * @param array $filters Filters. | |
| 418 | 455 | * |
| 419 | 456 | * @return array |
| 420 | 457 | */ |
| 421 | 458 | public function toggle_filters( $filters ) { |
| @@ -428,9 +465,9 @@ | ||
| 428 | 465 | |
| 429 | 466 | /** |
| 430 | 467 | * Add the network suffix to the $screen_id when in the network admin |
| 431 | 468 | * |
| 432 | - * @param $screen_id | |
| 469 | + * @param int $screen_id Screen ID. | |
| 433 | 470 | * |
| 434 | 471 | * @return string |
| 435 | 472 | */ |
| 436 | 473 | public function list_table_screen_id( $screen_id ) { |
| @@ -445,8 +482,10 @@ | ||
| 445 | 482 | |
| 446 | 483 | /** |
| 447 | 484 | * Set blog_id for network admin activity |
| 448 | 485 | * |
| 486 | + * @param int $blog_id Blog ID. | |
| 487 | + * | |
| 449 | 488 | * @return int |
| 450 | 489 | */ |
| 451 | 490 | public function blog_id_logged( $blog_id ) { |
| 452 | 491 | return is_network_admin() ? 0 : $blog_id; |
| @@ -456,9 +495,9 @@ | ||
| 456 | 495 | * Customize query args on multisite installs |
| 457 | 496 | * |
| 458 | 497 | * @filter wp_stream_query_args |
| 459 | 498 | * |
| 460 | - * @param array $args | |
| 499 | + * @param array $args Site arguments. | |
| 461 | 500 | * |
| 462 | 501 | * @return array |
| 463 | 502 | */ |
| 464 | 503 | public function network_query_args( $args ) { |
| @@ -472,15 +511,16 @@ | ||
| 472 | 511 | * Add site count to the page title in the network admin |
| 473 | 512 | * |
| 474 | 513 | * @filter wp_stream_admin_page_title |
| 475 | 514 | * |
| 476 | - * @param string $page_title | |
| 515 | + * @param string $page_title Page title. | |
| 477 | 516 | * |
| 478 | 517 | * @return string |
| 479 | 518 | */ |
| 480 | 519 | public function network_admin_page_title( $page_title ) { |
| 481 | 520 | if ( is_network_admin() ) { |
| 482 | - $site_count = sprintf( _n( '1 site', '%s sites', get_blog_count(), 'stream' ), number_format( get_blog_count() ) ); | |
| 521 | + /* translators: %d: number of sites on the network (e.g. "42") */ | |
| 522 | + $site_count = sprintf( _n( '%d site', '%d sites', get_blog_count(), 'stream' ), number_format( get_blog_count() ) ); | |
| 483 | 523 | $page_title = sprintf( '%s (%s)', $page_title, $site_count ); |
| 484 | 524 | } |
| 485 | 525 | |
| 486 | 526 | return $page_title; |
| @@ -488,14 +528,14 @@ | ||
| 488 | 528 | |
| 489 | 529 | /** |
| 490 | 530 | * Add the Site column to the network stream records |
| 491 | 531 | * |
| 492 | - * @param $columns | |
| 532 | + * @param array $columns Columns data. | |
| 493 | 533 | * |
| 494 | 534 | * @return mixed |
| 495 | 535 | */ |
| 496 | 536 | public function network_admin_columns( $columns ) { |
| 497 | - if ( is_network_admin() ) { | |
| 537 | + if ( is_network_admin() || $this->ajax_network_admin() ) { | |
| 498 | 538 | $columns = array_merge( |
| 499 | 539 | array_slice( $columns, 0, -1 ), |
| 500 | 540 | array( |
| 501 | 541 | 'blog_id' => esc_html__( 'Site', 'stream' ), |
| @@ -509,9 +549,9 @@ | ||
| 509 | 549 | |
| 510 | 550 | /** |
| 511 | 551 | * Prevent the Blogs connector from loading when not in Network Admin |
| 512 | 552 | * |
| 513 | - * @param $connectors | |
| 553 | + * @param array $connectors Connectors. | |
| 514 | 554 | * |
| 515 | 555 | * @return mixed |
| 516 | 556 | */ |
| 517 | 557 | public function hide_blogs_connector( $connectors ) { |