/**
  * Wrapper for the settings API to work on the network settings page
  */
 function network_options_action()
 {
     $allowed_referers = array(self::NETWORK_SETTINGS_PAGE_SLUG, self::DEFAULT_SETTINGS_PAGE_SLUG);
     if (!isset($_GET['action']) || !in_array($_GET['action'], $allowed_referers)) {
         return;
     }
     $options = isset($_POST['option_page']) ? explode(',', stripslashes($_POST['option_page'])) : null;
     if ($options) {
         foreach ($options as $option) {
             $option = trim($option);
             $value = null;
             $sections = WP_Stream_Settings::get_fields();
             foreach ($sections as $section_name => $section) {
                 foreach ($section['fields'] as $field_idx => $field) {
                     $option_key = $section_name . '_' . $field['name'];
                     if (isset($_POST[$option][$option_key])) {
                         $value[$option_key] = $_POST[$option][$option_key];
                     } else {
                         $value[$option_key] = false;
                     }
                 }
             }
             if (!is_array($value)) {
                 $value = trim($value);
             }
             update_site_option($option, $value);
         }
     }
     if (!count(get_settings_errors())) {
         add_settings_error('general', 'settings_updated', __('Settings saved.', 'default'), 'updated');
     }
     set_transient('wp_stream_settings_errors', get_settings_errors(), 30);
     $go_back = add_query_arg('settings-updated', 'true', wp_get_referer());
     wp_redirect($go_back);
     exit;
 }
    /**
     * Render settings page
     *
     * @return void
     */
    public static function render_settings_page()
    {
        $option_key = WP_Stream_Settings::$option_key;
        $form_action = apply_filters('wp_stream_settings_form_action', admin_url('options.php'));
        $page_title = apply_filters('wp_stream_settings_form_title', get_admin_page_title());
        $page_description = apply_filters('wp_stream_settings_form_description', '');
        $sections = WP_Stream_Settings::get_fields();
        $active_tab = wp_stream_filter_input(INPUT_GET, 'tab');
        wp_enqueue_script('stream-settings', plugins_url('../ui/js/settings.js', __FILE__), array('jquery'), WP_Stream::VERSION, true);
        ?>
		<div class="wrap">
			<h2><?php 
        echo esc_html($page_title);
        ?>
</h2>

			<?php 
        if (!empty($page_description)) {
            ?>
				<p><?php 
            echo esc_html($page_description);
            ?>
</p>
			<?php 
        }
        ?>

			<?php 
        settings_errors();
        ?>

			<?php 
        if (count($sections) > 1) {
            ?>
				<h2 class="nav-tab-wrapper">
					<?php 
            $i = 0;
            ?>
					<?php 
            foreach ($sections as $section => $data) {
                ?>
						<?php 
                $i++;
                ?>
						<?php 
                $is_active = 1 === $i && !$active_tab || $active_tab === $section;
                ?>
						<a href="<?php 
                echo esc_url(add_query_arg('tab', $section));
                ?>
" class="nav-tab<?php 
                if ($is_active) {
                    echo esc_attr(' nav-tab-active');
                }
                ?>
">
							<?php 
                echo esc_html($data['title']);
                ?>
						</a>
					<?php 
            }
            ?>
				</h2>
			<?php 
        }
        ?>

			<div class="nav-tab-content" id="tab-content-settings">
				<form method="post" action="<?php 
        echo esc_attr($form_action);
        ?>
" enctype="multipart/form-data">
					<div class="settings-sections">
		<?php 
        $i = 0;
        foreach ($sections as $section => $data) {
            $i++;
            $is_active = 1 === $i && !$active_tab || $active_tab === $section;
            if ($is_active) {
                settings_fields($option_key);
                do_settings_sections($option_key);
            }
        }
        ?>
					</div>
					<?php 
        submit_button();
        ?>
				</form>
			</div>
		</div>
	<?php 
    }
 /**
  * Add action links to Stream drop row in admin list screen
  *
  * @filter wp_stream_action_links_{connector}
  *
  * @param  array  $links     Previous links registered
  * @param  object $record    Stream record
  *
  * @return array             Action links
  */
 public static function action_links($links, $record)
 {
     $context_labels = self::get_context_labels();
     $rules = array('stream' => array('menu_slug' => 'wp_stream', 'submenu_slug' => WP_Stream_Admin::SETTINGS_PAGE_SLUG, 'url' => function ($rule, $record) {
         $option_key = wp_stream_get_meta($record, 'option_key', true);
         $url_tab = null;
         if ('' !== $option_key) {
             foreach (WP_Stream_Settings::get_fields() as $tab_name => $tab_properties) {
                 foreach ($tab_properties['fields'] as $field) {
                     $field_key = sprintf('%s_%s', $tab_name, $field['name']);
                     if ($field_key === $option_key) {
                         $url_tab = $tab_name;
                         break 2;
                     }
                 }
             }
         }
         return add_query_arg(array('page' => $rule['submenu_slug'], 'tab' => $url_tab), admin_url('admin.php'));
     }, 'applicable' => function ($submenu, $record) {
         return $record->context === 'wp_stream';
     }), 'background_header' => array('menu_slug' => 'themes.php', 'submenu_slug' => function ($record) {
         return str_replace('_', '-', $record->context);
     }, 'url' => function ($rule, $record) {
         return add_query_arg('page', $rule['submenu_slug']($record), admin_url($rule['menu_slug']));
     }, 'applicable' => function ($submenu, $record) {
         return in_array($record->context, array('custom_header', 'custom_background'));
     }), 'general' => array('menu_slug' => 'options-general.php', 'submenu_slug' => function ($record) {
         return sprintf('options-%s.php', $record->context);
     }, 'url' => function ($rule, $record) {
         return admin_url($rule['submenu_slug']($record));
     }, 'applicable' => function ($submenu, $record) {
         return !empty($submenu['options-general.php']);
     }), 'network' => array('menu_slug' => 'settings.php', 'submenu_slug' => function ($record) {
         return 'settings.php';
     }, 'url' => function ($rule, $record) {
         return network_admin_url($rule['menu_slug']);
     }, 'applicable' => function ($submenu, $record) {
         if (!$record->blog_id) {
             return !empty($submenu['settings.php']);
         }
         return false;
     }));
     if ('settings' !== $record->context && in_array($record->context, array_keys($context_labels))) {
         global $submenu;
         $applicable_rules = array_filter($rules, function ($rule) use($submenu, $record) {
             return call_user_func($rule['applicable'], $submenu, $record);
         });
         if (!empty($applicable_rules)) {
             // The first applicable rule wins
             $rule = array_shift($applicable_rules);
             $menu_slug = $rule['menu_slug'];
             $submenu_slug = is_object($rule['submenu_slug']) && $rule['submenu_slug'] instanceof Closure ? $rule['submenu_slug']($record) : $rule['submenu_slug'];
             $url = $rule['url']($rule, $record);
             if (isset($submenu[$menu_slug])) {
                 $found_submenus = wp_list_filter($submenu[$menu_slug], array(2 => $submenu_slug));
             }
             if (!empty($found_submenus)) {
                 $target_submenu = array_pop($found_submenus);
                 list($menu_title, $capability) = $target_submenu;
                 if (current_user_can($capability)) {
                     $url = apply_filters('wp_stream_action_link_url', $url, $record);
                     $text = sprintf(__('Edit %s Settings', 'stream'), $context_labels[$record->context]);
                     $field_name = wp_stream_get_meta($record, 'option_key', true);
                     if ('' === $field_name) {
                         $field_name = wp_stream_get_meta($record, 'option', true);
                     }
                     if ('' !== $field_name) {
                         $url = sprintf('%s#%s%s', rtrim(preg_replace('/#.*/', '', $url), '/'), self::HIGHLIGHT_FIELD_URL_HASH_PREFIX, $field_name);
                     }
                     $links[$text] = $url;
                 }
             }
         }
     }
     return $links;
 }