vendor
7 years ago
admin.php
7 years ago
class-wp-stream-author.php
10 years ago
connector.php
7 years ago
connectors.php
7 years ago
context-query.php
7 years ago
dashboard.php
10 years ago
date-interval.php
10 years ago
db.php
9 years ago
filter-input.php
10 years ago
functions.php
10 years ago
install.php
9 years ago
list-table.php
7 years ago
live-update.php
9 years ago
log.php
10 years ago
network.php
7 years ago
query.php
7 years ago
settings.php
7 years ago
network.php
371 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Multisite Network Class |
| 4 | * |
| 5 | * @author X-Team <x-team.com> |
| 6 | * @author Chris Olbekson <chris@x-team.com> |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | class MainWP_WP_Stream_Network { |
| 11 | |
| 12 | const NETWORK_SETTINGS_PAGE_SLUG = 'mainwp_wp_stream_network_settings'; |
| 13 | const DEFAULT_SETTINGS_PAGE_SLUG = 'mainwp_wp_stream_default_settings'; |
| 14 | |
| 15 | function __construct() { |
| 16 | $this->actions(); |
| 17 | $this->filters(); |
| 18 | } |
| 19 | |
| 20 | function actions() { |
| 21 | add_action( 'init', array( $this, 'ajax_network_admin' ), 1 ); |
| 22 | add_action( 'admin_bar_menu', array( $this, 'network_admin_bar_menu' ), 99, 1 ); |
| 23 | add_action( 'network_admin_menu', array( 'MainWP_WP_Stream_Admin', 'register_menu' ) ); |
| 24 | add_action( 'network_admin_notices', array( 'MainWP_WP_Stream_Admin', 'admin_notices' ) ); |
| 25 | add_action( 'wpmuadminedit', array( $this, 'network_options_action' ) ); |
| 26 | add_action( 'wp_network_dashboard_setup', array( 'MainWP_WP_Stream_Dashboard_Widget', 'stream_activity' ) ); |
| 27 | add_action( 'admin_menu', array( $this, 'admin_menu_screens' ) ); |
| 28 | add_action( 'network_admin_menu', array( $this, 'admin_menu_screens' ) ); |
| 29 | add_action( 'update_site_option_' . MainWP_WP_Stream_Settings::NETWORK_KEY, array( $this, 'updated_option_ttl_remove_records' ), 10, 3 ); |
| 30 | } |
| 31 | |
| 32 | function filters() { |
| 33 | add_filter( 'mainwp_wp_stream_disable_admin_access', array( __CLASS__, 'disable_admin_access' ) ); |
| 34 | add_filter( 'mainwp_wp_stream_settings_form_action', array( $this, 'settings_form_action' ) ); |
| 35 | add_filter( 'mainwp_wp_stream_settings_form_description', array( $this, 'settings_form_description' ) ); |
| 36 | add_filter( 'mainwp_wp_stream_options_fields', array( $this, 'get_network_admin_fields' ) ); |
| 37 | add_filter( 'mainwp_wp_stream_options', array( $this, 'get_network_options' ), 10, 2 ); |
| 38 | add_filter( 'mainwp_wp_stream_serialized_labels', array( $this, 'get_settings_translations' ) ); |
| 39 | add_filter( 'mainwp_wp_stream_list_table_filters', array( $this, 'list_table_filters' ) ); |
| 40 | add_filter( 'stream_toggle_filters', array( $this, 'toggle_filters' ) ); |
| 41 | add_filter( 'mainwp_wp_stream_list_table_screen_id', array( $this, 'list_table_screen_id' ) ); |
| 42 | add_filter( 'mainwp_wp_stream_query_args', array( __CLASS__, 'set_network_option_value' ) ); |
| 43 | add_filter( 'mainwp_wp_stream_list_table_columns', array( $this, 'network_admin_columns' ) ); |
| 44 | } |
| 45 | |
| 46 | function ajax_network_admin() { |
| 47 | if ( defined( 'DOING_AJAX' ) && DOING_AJAX && is_multisite() && preg_match( '#^' . network_admin_url() . '#i', $_SERVER['HTTP_REFERER'] ) ) { |
| 48 | define( 'WP_NETWORK_ADMIN', true ); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | public static function is_network_activated() { |
| 53 | if ( ! function_exists( 'is_plugin_active_for_network' ) ) { |
| 54 | require_once( ABSPATH . '/wp-admin/includes/plugin.php' ); |
| 55 | } |
| 56 | |
| 57 | return is_plugin_active_for_network( MAINWP_WP_STREAM_PLUGIN ); |
| 58 | } |
| 59 | |
| 60 | function network_admin_bar_menu( $admin_bar ) { |
| 61 | if ( ! self::is_network_activated() ) { |
| 62 | return; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | public static function get_network_blog() { |
| 67 | $blog = new stdClass; |
| 68 | $blog->blog_id = 0; |
| 69 | $blog->blogname = __( 'Network Admin', 'default' ); |
| 70 | |
| 71 | return $blog; |
| 72 | } |
| 73 | |
| 74 | public static function disable_admin_access( $disable_access ) { |
| 75 | if ( ! is_network_admin() && self::is_network_activated() ) { |
| 76 | $settings = (array) get_site_option( MainWP_WP_Stream_Settings::NETWORK_KEY, array() ); |
| 77 | |
| 78 | if ( isset( $settings['general_enable_site_access'] ) && false === $settings['general_enable_site_access'] ) { |
| 79 | return true; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | return $disable_access; |
| 84 | } |
| 85 | |
| 86 | function admin_menu_screens() { |
| 87 | if ( ! is_network_admin() ) { |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | } |
| 92 | |
| 93 | function updated_option_ttl_remove_records( $option_key, $new_value, $old_value ) { |
| 94 | MainWP_WP_Stream_Settings::updated_option_ttl_remove_records( $old_value, $new_value ); |
| 95 | } |
| 96 | |
| 97 | function settings_form_action( $action ) { |
| 98 | if ( is_network_admin() ) { |
| 99 | $current_page = mainwp_wp_stream_filter_input( INPUT_GET, 'page' ); |
| 100 | $action = add_query_arg( array( 'action' => $current_page ), 'edit.php' ); |
| 101 | } |
| 102 | |
| 103 | return $action; |
| 104 | } |
| 105 | |
| 106 | function settings_form_description( $description ) { |
| 107 | if ( ! is_network_admin() ) { |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | $current_page = mainwp_wp_stream_filter_input( INPUT_GET, 'page' ); |
| 112 | |
| 113 | switch ( $current_page ) { |
| 114 | case self::NETWORK_SETTINGS_PAGE_SLUG : |
| 115 | $description = __( 'These settings apply to all sites on the network.', 'mainwp-child-reports' ); |
| 116 | break; |
| 117 | case self::DEFAULT_SETTINGS_PAGE_SLUG : |
| 118 | $description = __( 'These default settings will apply to new sites created on the network. These settings do not alter existing sites.', 'mainwp-child-reports' ); |
| 119 | break; |
| 120 | } |
| 121 | |
| 122 | return $description; |
| 123 | } |
| 124 | |
| 125 | function get_network_admin_fields( $fields ) { |
| 126 | if ( ! self::is_network_activated() ) { |
| 127 | return $fields; |
| 128 | } |
| 129 | |
| 130 | $stream_hidden_options = apply_filters( |
| 131 | 'mainwp_wp_stream_hidden_option_fields', |
| 132 | array( |
| 133 | 'general' => array( |
| 134 | 'delete_all_records', |
| 135 | 'records_ttl', |
| 136 | ), |
| 137 | ) |
| 138 | ); |
| 139 | |
| 140 | $network_hidden_options = apply_filters( |
| 141 | 'mainwp_wp_stream_network_option_fields', |
| 142 | array( |
| 143 | 'general' => array( |
| 144 | 'role_access', |
| 145 | 'private_feeds', |
| 146 | ), |
| 147 | 'exclude' => array( |
| 148 | 'authors', |
| 149 | 'roles', |
| 150 | 'connectors', |
| 151 | 'contexts', |
| 152 | 'actions', |
| 153 | 'ip_addresses', |
| 154 | 'hide_previous_records', |
| 155 | ), |
| 156 | ) |
| 157 | ); |
| 158 | |
| 159 | // Remove settings based on context |
| 160 | if ( MainWP_WP_Stream_Settings::NETWORK_KEY === MainWP_WP_Stream_Settings::$option_key ) { |
| 161 | $hidden_options = $network_hidden_options; |
| 162 | } else { |
| 163 | $hidden_options = $stream_hidden_options; |
| 164 | } |
| 165 | |
| 166 | foreach ( $fields as $section_key => $section ) { |
| 167 | foreach ( $section['fields'] as $key => $field ) { |
| 168 | if ( ! isset( $hidden_options[ $section_key ] ) ) { |
| 169 | continue; |
| 170 | } |
| 171 | if ( in_array( $field['name'], $hidden_options[ $section_key ] ) ) { |
| 172 | unset( $fields[ $section_key ]['fields'][ $key ] ); |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | // Add settings based on context |
| 178 | if ( MainWP_WP_Stream_Settings::NETWORK_KEY === MainWP_WP_Stream_Settings::$option_key ) { |
| 179 | $new_fields['general']['fields'][] = array( |
| 180 | 'name' => 'enable_site_access', |
| 181 | 'title' => __( 'Enable Site Access', 'mainwp-child-reports' ), |
| 182 | 'after_field' => __( 'Enabled', 'mainwp-child-reports' ), |
| 183 | 'default' => 1, |
| 184 | 'desc' => __( 'When site access is disabled MainWP Child Reports can only be accessed from the network administration.', 'mainwp-child-reports' ), |
| 185 | 'type' => 'checkbox', |
| 186 | ); |
| 187 | |
| 188 | $fields = array_merge_recursive( $new_fields, $fields ); |
| 189 | |
| 190 | $reset_site_settings_href = add_query_arg( |
| 191 | array( |
| 192 | 'action' => 'mainwp_wp_stream_defaults', |
| 193 | 'mainwp_wp_stream_nonce' => wp_create_nonce( 'stream_nonce' ), |
| 194 | ), |
| 195 | admin_url( 'admin-ajax.php' ) |
| 196 | ); |
| 197 | |
| 198 | $fields['general']['fields'][] = array( |
| 199 | 'name' => 'reset_site_settings', |
| 200 | 'title' => __( 'Reset Site Settings', 'mainwp-child-reports' ), |
| 201 | 'type' => 'link', |
| 202 | 'href' => $reset_site_settings_href, |
| 203 | 'desc' => __( 'Warning: Clicking this will override all site settings with defaults.', 'mainwp-child-reports' ), |
| 204 | 'default' => 0, |
| 205 | ); |
| 206 | } |
| 207 | |
| 208 | // Remove empty settings sections |
| 209 | foreach ( $fields as $section_key => $section ) { |
| 210 | if ( empty( $section['fields'] ) ) { |
| 211 | unset( $fields[ $section_key ] ); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | return $fields; |
| 216 | } |
| 217 | |
| 218 | function get_settings_translations( $labels ) { |
| 219 | $network_key = MainWP_WP_Stream_Settings::NETWORK_KEY; |
| 220 | $defaults_key = MainWP_WP_Stream_Settings::DEFAULTS_KEY; |
| 221 | |
| 222 | if ( ! isset( $labels[ $network_key ] ) ) { |
| 223 | $labels[ $network_key ] = array(); |
| 224 | } |
| 225 | |
| 226 | if ( ! isset( $labels[ $defaults_key ] ) ) { |
| 227 | $labels[ $defaults_key ] = array(); |
| 228 | } |
| 229 | |
| 230 | foreach ( MainWP_WP_Stream_Settings::get_fields() as $section_slug => $section ) { |
| 231 | foreach ( $section['fields'] as $field ) { |
| 232 | $labels[ $network_key ][ sprintf( '%s_%s', $section_slug, $field['name'] ) ] = $field['title']; |
| 233 | $labels[ $defaults_key ][ sprintf( '%s_%s', $section_slug, $field['name'] ) ] = $field['title']; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | return $labels; |
| 238 | } |
| 239 | |
| 240 | function network_options_action() { |
| 241 | $allowed_referers = array( |
| 242 | self::NETWORK_SETTINGS_PAGE_SLUG, |
| 243 | self::DEFAULT_SETTINGS_PAGE_SLUG, |
| 244 | ); |
| 245 | if ( ! isset( $_GET['action'] ) || ! in_array( $_GET['action'], $allowed_referers ) ) { |
| 246 | return; |
| 247 | } |
| 248 | |
| 249 | $options = isset( $_POST['option_page'] ) ? explode( ',', stripslashes( $_POST['option_page'] ) ) : null; |
| 250 | |
| 251 | if ( $options ) { |
| 252 | |
| 253 | foreach ( $options as $option ) { |
| 254 | $option = trim( $option ); |
| 255 | $value = null; |
| 256 | |
| 257 | $sections = MainWP_WP_Stream_Settings::get_fields(); |
| 258 | foreach ( $sections as $section_name => $section ) { |
| 259 | foreach ( $section['fields'] as $field_idx => $field ) { |
| 260 | $option_key = $section_name . '_' . $field['name']; |
| 261 | if ( isset( $_POST[ $option ][ $option_key ] ) ) { |
| 262 | $value[ $option_key ] = $_POST[ $option ][ $option_key ]; |
| 263 | } else { |
| 264 | $value[ $option_key ] = false; |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | if ( ! is_array( $value ) ) { |
| 270 | $value = trim( $value ); |
| 271 | } |
| 272 | |
| 273 | update_site_option( $option, $value ); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | if ( ! count( get_settings_errors() ) ) { |
| 278 | add_settings_error( 'general', 'settings_updated', __( 'Settings saved.', 'default' ), 'updated' ); |
| 279 | } |
| 280 | |
| 281 | set_transient( 'settings_errors', get_settings_errors(), 30 ); |
| 282 | |
| 283 | $go_back = add_query_arg( 'settings-updated', 'true', wp_get_referer() ); |
| 284 | wp_redirect( $go_back ); |
| 285 | exit; |
| 286 | } |
| 287 | |
| 288 | function get_network_options( $options, $option_key ) { |
| 289 | if ( is_network_admin() ) { |
| 290 | $options = wp_parse_args( |
| 291 | (array) get_site_option( $option_key, array() ), |
| 292 | MainWP_WP_Stream_Settings::get_defaults( $option_key ) |
| 293 | ); |
| 294 | } |
| 295 | |
| 296 | return $options; |
| 297 | } |
| 298 | |
| 299 | function list_table_filters( $filters ) { |
| 300 | if ( is_network_admin() && ! wp_is_large_network() ) { |
| 301 | $blogs = array(); |
| 302 | |
| 303 | // display network blog as the first option |
| 304 | $network_blog = self::get_network_blog(); |
| 305 | |
| 306 | $blogs['network'] = array( |
| 307 | 'label' => $network_blog->blogname, |
| 308 | 'disabled' => '', |
| 309 | ); |
| 310 | |
| 311 | // add all sites |
| 312 | foreach ( (array) get_sites() as $blog ) { |
| 313 | $blog_data = get_blog_details( $blog ); |
| 314 | |
| 315 | $blogs[ $blog['blog_id'] ] = array( |
| 316 | 'label' => $blog_data->blogname, |
| 317 | 'disabled' => '', |
| 318 | ); |
| 319 | } |
| 320 | |
| 321 | $filters['blog_id'] = array( |
| 322 | 'title' => __( 'sites', 'mainwp-child-reports' ), |
| 323 | 'items' => $blogs, |
| 324 | ); |
| 325 | } |
| 326 | |
| 327 | return $filters; |
| 328 | } |
| 329 | |
| 330 | function toggle_filters( $filters ) { |
| 331 | if ( is_network_admin() ) { |
| 332 | $filters['blog_id'] = esc_html__( 'Site', 'mainwp-child-reports' ); |
| 333 | } |
| 334 | |
| 335 | return $filters; |
| 336 | } |
| 337 | |
| 338 | function list_table_screen_id( $screen_id ) { |
| 339 | if ( $screen_id && is_network_admin() ) { |
| 340 | if ( '-network' !== substr( $screen_id, -8 ) ) { |
| 341 | $screen_id .= '-network'; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | return $screen_id; |
| 346 | } |
| 347 | |
| 348 | public static function set_network_option_value( $args ) { |
| 349 | if ( isset( $args['blog_id'] ) && 'network' === $args['blog_id'] ) { |
| 350 | $args['blog_id'] = 0; |
| 351 | } |
| 352 | |
| 353 | return $args; |
| 354 | } |
| 355 | |
| 356 | function network_admin_columns( $columns ) { |
| 357 | if ( is_network_admin() ) { |
| 358 | $columns = array_merge( |
| 359 | array_slice( $columns, 0, -1 ), |
| 360 | array( |
| 361 | 'blog_id' => esc_html__( 'Site', 'mainwp-child-reports' ), |
| 362 | ), |
| 363 | array_slice( $columns, -1 ) |
| 364 | ); |
| 365 | } |
| 366 | |
| 367 | return $columns; |
| 368 | } |
| 369 | |
| 370 | } |
| 371 |