[ "type" => "radio", "label" => __( "Follow Locations?", "atr-server-status" ), "description" => __( "If a request is answered with a redirect, a new request will be made at the new location according to the configured protocol.", "atr-server-status" ), "options" => [ 1 => "Yes", 0 => "No" ], "default" => 1 ], "max-redirs" => [ "type" => "number", "label" => __( "Maximum Redirects", "atr-server-status" ), "description" => __( "Only used for HTTP(S) connections, and requires 'Follow Location?' to be enabled.", "atr-server-status" ), "default" => 5 ] ] ); } /** * Loads saved settings, and applies default values if they don't exist yet. */ public function Load() { $all = []; foreach( $this->All() as $key => $args ) { $all[$key] = $args["default"]; } $settings = wp_parse_args( get_option( $this->optionKey ), $all ); $this->settings = apply_filters( "atr_settings_values", $settings ); } /** * Saves submitted values */ public function Save( $settings ) { $settings = $this->FilterBadValues( $settings ); return update_option( $this->optionKey, $settings ); } /** * Get the value of a desired setting, null if it doesn't exists */ public function Get( $setting = null) { return isset( $this->settings[$setting] ) ? $this->settings[$setting] : $this->settings; } /** * Call this to get the class instance */ public static function Instance() { static $instance = false; if( $instance === false ) { $instance = new static(); // Late static binding (PHP 5.3+) } return $instance; } /** * Make sure only values defined are present. */ private function FilterBadValues( $settings ) { $all = $this->All(); // Set it to the default value, if a given value isn't present. foreach( $settings as $key => $value ) { if( $all[$key]["type"] == "radio" ) { if( isset($all[$key]["options"][$value]) !== true ) { $settings[$key] = $all[$key]["default"]; } } else if( $all[$key] == "text" ) { $settings[$key] = sanitize_text_field($value); } else if ( $all[$key] == "number" ) { $settings[$key] = (int) $value; } } return $settings; } } }