class-wpel-network-admin-fields.php
115 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class WPEL_Network_Admin_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_Admin_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-admin-fields', |
| 21 | 'page_id' => 'wpel-network-admin-fields', |
| 22 | 'option_name' => 'wpel-network-admin-settings', |
| 23 | 'option_group' => 'wpel-network-admin-settings', |
| 24 | 'title' => __( 'Network Admin Settings', 'wp-external-links' ), |
| 25 | 'fields' => array( |
| 26 | 'own_admin_menu' => array( |
| 27 | 'label' => __( 'Main Network Admin Menu:', 'wp-external-links' ), |
| 28 | 'default_value' => '1', |
| 29 | ), |
| 30 | ), |
| 31 | ) ); |
| 32 | |
| 33 | if ( is_network_admin() ) { |
| 34 | add_action( 'network_admin_edit_'. $this->get_setting( 'option_group' ) , $this->get_callback( 'save_network_settings' ) ); |
| 35 | } |
| 36 | |
| 37 | parent::init(); |
| 38 | } |
| 39 | |
| 40 | protected function save_network_settings() |
| 41 | { |
| 42 | // when calling 'settings_fields' but we must add the '-options' postfix |
| 43 | check_admin_referer( $this->get_setting( 'option_group' ) .'-options' ); |
| 44 | |
| 45 | global $new_whitelist_options; |
| 46 | $option_names = $new_whitelist_options[ $this->get_setting( 'option_group' ) ]; |
| 47 | |
| 48 | foreach ( $option_names as $option_name ) { |
| 49 | if ( isset( $_POST[ $option_name ] ) ) { |
| 50 | $post_values = $_POST[ $option_name ]; |
| 51 | $sanitized_values = $this->sanitize( $post_values ); |
| 52 | |
| 53 | update_site_option( $option_name, $sanitized_values ); |
| 54 | } else { |
| 55 | delete_site_option( $option_name ); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | $redirect_url = filter_input( INPUT_POST, '_wp_http_referer', FILTER_SANITIZE_STRING ); |
| 60 | |
| 61 | wp_redirect( add_query_arg( |
| 62 | array( |
| 63 | 'page' => 'wpel-network-settings-page', |
| 64 | 'updated' => true |
| 65 | ) |
| 66 | , $redirect_url |
| 67 | ) ); |
| 68 | |
| 69 | exit; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Show field methods |
| 74 | */ |
| 75 | |
| 76 | protected function show_own_admin_menu( array $args ) |
| 77 | { |
| 78 | $this->get_html_fields()->check_with_label( |
| 79 | $args[ 'key' ] |
| 80 | , __( 'Create own network admin menu for this plugin', 'wp-external-links' ) |
| 81 | , '1' |
| 82 | , '' |
| 83 | ); |
| 84 | |
| 85 | echo ' <p class="description">' |
| 86 | . __( 'Or else it will be added to the "Settings" menu', 'wp-external-links' ) |
| 87 | .'</p>'; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Validate and sanitize user input before saving to databse |
| 92 | * @param array $new_values |
| 93 | * @param array $old_values |
| 94 | * @return array |
| 95 | */ |
| 96 | protected function before_update( array $new_values, array $old_values ) |
| 97 | { |
| 98 | $update_values = $new_values; |
| 99 | $is_valid = true; |
| 100 | |
| 101 | $is_valid = $is_valid && in_array( $new_values[ 'own_admin_menu' ], array( '', '1' ) ); |
| 102 | |
| 103 | if ( false === $is_valid ) { |
| 104 | // error when user input is not valid conform the UI, probably tried to "hack" |
| 105 | $this->add_error( __( 'Something went wrong. One or more values were invalid.', 'wp-external-links' ) ); |
| 106 | return $old_values; |
| 107 | } |
| 108 | |
| 109 | return $update_values; |
| 110 | } |
| 111 | |
| 112 | } |
| 113 | |
| 114 | /*?>*/ |
| 115 |