Accessor/mutator settings functions.
This commit is contained in:
@@ -133,8 +133,14 @@ class Settings {
|
|||||||
|
|
||||||
// Output settings page
|
// Output settings page
|
||||||
|
|
||||||
public function render():void {
|
private function load_options():bool {
|
||||||
|
if (isset($this->options) && is_array($this->options)) return false;
|
||||||
$this->options = get_option($this->get_option_group());
|
$this->options = get_option($this->get_option_group());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function render():void {
|
||||||
|
$this->load_options();
|
||||||
echo '<div class="wrap">';
|
echo '<div class="wrap">';
|
||||||
printf('<h1>%s</h1>', esc_html($this->get_page_title()));
|
printf('<h1>%s</h1>', esc_html($this->get_page_title()));
|
||||||
echo '<form method="post" action="options.php">';
|
echo '<form method="post" action="options.php">';
|
||||||
@@ -161,6 +167,30 @@ class Settings {
|
|||||||
]), admin_url(self::PARENT_SLUG));
|
]), admin_url(self::PARENT_SLUG));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Field Accessors
|
||||||
|
public function get_field(string $name, string $default = ''):string {
|
||||||
|
$this->load_options();
|
||||||
|
return $this->options[$name] ?? $default;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function set_field(string $name, string $value):bool {
|
||||||
|
$this->load_options();
|
||||||
|
$this->options[$name] = sanitize_text_field($value);
|
||||||
|
return update_option($this->get_option_group(), $this->options);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function get(string $name, string $default = ''):string {
|
||||||
|
return self::instance()->get_field($name, $default);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function set(string $name, string $value):bool {
|
||||||
|
return self::instance()->set_field($name, $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function exists(string $name):bool {
|
||||||
|
return !empty(self::get($name));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Settings::instance();
|
Settings::instance();
|
||||||
|
|||||||
Reference in New Issue
Block a user