/** * Each section is responsible for saving its own settings * since only it knows the nature of the fields and sanitization methods. */ public function save_settings() { // Check if we're saving options and that there are settings to svae if (empty($_POST['action']) || 'apple_news_options' !== $_POST['action'] || empty($this->settings)) { return; } // Form nonce check check_admin_referer('apple_news_options', 'apple_news_options'); // Get the current Apple News settings $settings = get_option(self::$option_name, array()); // Iterate over the settings and save each value. // Settings can't be empty unless allowed, so if no value is found // use the default value to be safe. $default_settings = new Settings(); foreach ($this->settings as $key => $attributes) { if (!empty($_POST[$key])) { // Sanitize the value $sanitize = empty($attributes['sanitize']) || !is_callable($attributes['sanitize']) ? 'sanitize_text_field' : $attributes['sanitize']; $value = call_user_func($sanitize, $_POST[$key]); } else { // Use the default value $value = $default_settings->get($key); } // Add to the array $settings[$key] = $value; } // Clear certain caches delete_transient('apple_news_sections'); // Save to options update_option(self::$option_name, $settings, 'no'); }