/** * Constructor. * * @param string $page */ function __construct($page) { $this->page = $page; $base_settings = new \Apple_Exporter\Settings(); self::$base_settings = $base_settings->all(); $this->settings = apply_filters('apple_news_section_settings', $this->settings, $page); $this->groups = apply_filters('apple_news_section_groups', $this->groups, $page); self::$fonts = apply_filters('apple_news_fonts_list', self::$fonts); // Save settings if necessary $this->save_settings(); }
/** * 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'); }
/** * Creates a new \Apple_Exporter\Settings instance and loads it with WordPress' saved * settings. */ public function fetch_settings() { if (is_null($this->loaded_settings)) { $settings = new Settings(); foreach ($settings->all() as $key => $value) { $wp_value = get_option($key); if (empty($wp_value)) { $wp_value = $value; } $settings->set($key, $wp_value); } $this->loaded_settings = $settings; } return apply_filters('apple_news_loaded_settings', $this->loaded_settings); }