class-wpel-network-fields.php
131 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class WPEL_Network_Fields |
| 4 | * |
| 5 | * @package WPEL |
| 6 | * @category WordPress Plugin |
| 7 | * @version 2.3 |
| 8 | * @link https://www.webfactoryltd.com/ |
| 9 | * @license Dual licensed under the MIT and GPLv2+ licenses |
| 10 | */ |
| 11 | final class WPEL_Network_Fields extends FWP_Settings_Section_Base_1x0x0 |
| 12 | { |
| 13 | |
| 14 | /** |
| 15 | * Initialize |
| 16 | */ |
| 17 | protected function init() |
| 18 | { |
| 19 | $this->set_settings( array( |
| 20 | 'section_id' => 'wpel-network-fields', |
| 21 | 'page_id' => 'wpel-network-fields', |
| 22 | 'option_name' => 'wpel-network-settings', |
| 23 | 'option_group' => 'wpel-network-settings', |
| 24 | 'title' => __( 'Multi Site Settings', 'wp-external-links' ), |
| 25 | 'fields' => array( |
| 26 | 'capability' => array( |
| 27 | 'label' => __( 'Capability for individual sites:', 'wp-external-links' ), |
| 28 | 'default_value' => 'manage_options', |
| 29 | ), |
| 30 | 'default_settings_site' => array( |
| 31 | 'label' => __( 'Use the settings of this site as default for new sites:', 'wp-external-links' ), |
| 32 | 'default_value' => '', |
| 33 | ), |
| 34 | ), |
| 35 | ) ); |
| 36 | |
| 37 | if ( is_network_admin() ) { |
| 38 | add_action( 'network_admin_edit_'. $this->get_setting( 'option_group' ) , $this->get_callback( 'save_network_settings' ) ); |
| 39 | } |
| 40 | |
| 41 | parent::init(); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Save network settings |
| 46 | * @global type $new_whitelist_options |
| 47 | */ |
| 48 | protected function save_network_settings() |
| 49 | { |
| 50 | // when calling 'settings_fields' but we must add the '-options' postfix |
| 51 | check_admin_referer( $this->get_setting( 'option_group' ) .'-options' ); |
| 52 | |
| 53 | global $new_whitelist_options; |
| 54 | $option_names = $new_whitelist_options[ $this->get_setting( 'option_group' ) ]; |
| 55 | |
| 56 | foreach ( $option_names as $option_name ) { |
| 57 | if ( isset( $_POST[ $option_name ] ) ) { |
| 58 | $post_values = $_POST[ $option_name ]; |
| 59 | $sanitized_values = $this->sanitize( $post_values ); |
| 60 | |
| 61 | update_site_option( $option_name, $sanitized_values ); |
| 62 | } else { |
| 63 | delete_site_option( $option_name ); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | $redirect_url = filter_input( INPUT_POST, '_wp_http_referer', FILTER_SANITIZE_STRING ); |
| 68 | |
| 69 | wp_redirect( add_query_arg( |
| 70 | array( |
| 71 | 'page' => $this->get_setting( 'option_group' ) .'-page', |
| 72 | 'updated' => true |
| 73 | ) |
| 74 | , $redirect_url |
| 75 | ) ); |
| 76 | |
| 77 | exit; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Show field methods |
| 82 | */ |
| 83 | |
| 84 | protected function show_capability( array $args ) |
| 85 | { |
| 86 | $this->get_html_fields()->select( |
| 87 | $args[ 'key' ] |
| 88 | , array( |
| 89 | 'manage_options' => __( 'Site Admins and Super Admins', 'wp-external-links' ), |
| 90 | 'manage_network' => __( 'Only Super Admins', 'wp-external-links' ), |
| 91 | ) |
| 92 | ); |
| 93 | } |
| 94 | |
| 95 | protected function show_default_settings_site( array $args ) |
| 96 | { |
| 97 | $sites = wp_get_sites(); |
| 98 | |
| 99 | $values = array(); |
| 100 | $values[ '' ] = __( '- none -', 'wp-external-links' ); |
| 101 | |
| 102 | foreach ( $sites as $site ) { |
| 103 | $values[ $site[ 'blog_id' ] ] = 'blog_id: '. $site[ 'blog_id' ] .' - '. $site[ 'path' ]; |
| 104 | } |
| 105 | |
| 106 | $this->get_html_fields()->select( |
| 107 | $args[ 'key' ] |
| 108 | , $values |
| 109 | ); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Validate and sanitize user input before saving to databse |
| 114 | * @param array $new_values |
| 115 | * @param array $old_values |
| 116 | * @return array |
| 117 | */ |
| 118 | protected function before_update( array $new_values, array $old_values ) |
| 119 | { |
| 120 | $update_values = $new_values; |
| 121 | |
| 122 | $update_values[ 'capability' ] = sanitize_text_field( $new_values[ 'capability' ] ); |
| 123 | $update_values[ 'default_settings_site' ] = sanitize_text_field( $new_values[ 'default_settings_site' ] ); |
| 124 | |
| 125 | return $update_values; |
| 126 | } |
| 127 | |
| 128 | } |
| 129 | |
| 130 | /*?>*/ |
| 131 |