feat: move common functionality to generic classes

This commit is contained in:
Ben Goldsworthy 2021-08-05 15:52:28 +01:00
parent 2e27f6e449
commit c0628bc7e3
7 changed files with 92 additions and 172 deletions

View file

@ -182,7 +182,8 @@ class Setting {
private function is_disabled_or_overridden(): ?bool {
if ($this->enabled_by) {
if (!Settings::instance()->get_setting($this->enabled_by)->value) return true;
$enabled_by_value = Settings::instance()->get_setting($this->enabled_by)->value;
if ((!$enabled_by_value || $enabled_by_value !== 'userdefined')) return true;
if (!$this->overridden_by) return false;
else if (isset(Settings::instance()->get_setting($this->overridden_by)->value)) return true;

View file

@ -18,24 +18,7 @@ use footnotes\admin\layout as Layout;
* @package footnotes
* @since 2.8.0
*/
abstract class SettingsGroup {
/**
* Setting section slug.
*
* @var string
*
* @since 2.8.0
*/
protected string $options_group_slug;
/**
* Setting section slug.
*
* @var string
*
* @since 2.8.0
*/
protected string $section_slug;
abstract class SettingsGroup {
/**
* Setting group ID.
*
@ -63,11 +46,77 @@ abstract class SettingsGroup {
*/
protected array $settings;
protected abstract function load_dependencies(): void;
public function __construct(
/**
* Setting options group slug.
*
* @var string
*
* @since 2.8.0
*/
protected string $options_group_slug,
/**
* Setting section slug.
*
* @var string
*
* @since 2.8.0
*/
protected string $section_slug
) {
$this->load_dependencies();
$this->add_settings( get_option( $this->options_group_slug ) );
}
protected abstract function add_settings(array $options): void;
protected function load_dependencies(): void {
require_once plugin_dir_path( __DIR__ ) . 'settings/class-setting.php';
}
protected abstract function add_settings_fields(Layout\Settings $component): void;
protected abstract function add_settings(array|false $options): void;
protected function add_setting(array $setting): Setting {
extract( $setting );
return new Setting(
self::GROUP_ID,
$this->options_group_slug,
$this->section_slug,
$key,
$name,
$description ?? null,
$default_value ?? null,
$type,
$input_type,
$input_options ?? null,
$input_max ?? null,
$input_min ?? null,
$enabled_by['key'] ?? null,
$overridden_by['key'] ?? null
);
}
protected function load_values(array|false $options): void {
if ( ! $options ) return;
foreach ( $options as $setting_key => $setting_value ) {
$this->settings[$setting_key]->set_value( $setting_value );
}
}
public function add_settings_fields(Layout\SettingsPage $component): void {
foreach ($this->settings as $setting) {
add_settings_field(
$setting->key,
__( $setting->name, 'footnotes' ),
array ($component, 'setting_field_callback'),
'footnotes',
$setting->get_section_slug(),
$setting->get_setting_field_args()
);
}
}
public function get_setting(string $setting_key): ?Setting {
foreach ($this->settings as $setting) {

View file

@ -55,7 +55,9 @@ abstract class SettingsSection {
*/
protected array $settings_groups;
protected abstract function load_dependencies(): void;
protected function load_dependencies(): void {
require_once plugin_dir_path( __DIR__ ) . 'settings/class-setting.php';
}
public function load_options_group(): void {
$options_group = get_option($this->options_group_slug);

View file

@ -10,7 +10,9 @@ declare(strict_types=1);
namespace footnotes\includes\settings\customcss;
require_once plugin_dir_path( __DIR__ ) . 'settings/class-settings-section.php';
require_once plugin_dir_path( __DIR__ ) . 'class-settings-section.php';
use footnotes\includes\settings\SettingsSection;
// Import settings groups.
//use footnotes\includes\settings\custom-css\...;
@ -48,9 +50,9 @@ class CustomCSSSettingsSection extends SettingsSection {
}
protected function load_dependencies(): void {
require_once plugin_dir_path( __DIR__ ) . 'settings/class-setting.php';
// Require settings groups.
//require_once plugin_dir_path( __DIR__ ) . 'settings/custom-css/...';
parent::load_dependencies();
//require_once plugin_dir_path( __DIR__ ) . 'general/class-reference-container-settings-group.php';
}
protected function add_settings_groups(): void {

View file

@ -10,7 +10,9 @@ declare(strict_types=1);
namespace footnotes\includes\settings\referrersandtooltips;
require_once plugin_dir_path( __DIR__ ) . 'settings/class-settings-section.php';
require_once plugin_dir_path( __DIR__ ) . 'class-settings-section.php';
use footnotes\includes\settings\SettingsSection;
// Import settings groups.
//use footnotes\includes\settings\referrers-and-tooltips\...;
@ -48,9 +50,9 @@ class ReferrersAndTooltipsSettingsSection extends SettingsSection {
}
protected function load_dependencies(): void {
require_once plugin_dir_path( __DIR__ ) . 'settings/class-setting.php';
// Require settings groups.
//require_once plugin_dir_path( __DIR__ ) . 'settings/referrers-and-tooltips/...';
parent::load_dependencies();
//require_once plugin_dir_path( __DIR__ ) . 'general/class-reference-container-settings-group.php';
}
protected function add_settings_groups(): void {

View file

@ -10,8 +10,9 @@ declare(strict_types=1);
namespace footnotes\includes\settings\scopeandpriority;
require_once plugin_dir_path( __DIR__ ) . 'settings/class-settings-section.php';
require_once plugin_dir_path( __DIR__ ) . 'class-settings-section.php';
use footnotes\includes\settings\SettingsSection;
// Import settings groups.
//use footnotes\includes\settings\scope-and-priority\...;
@ -48,9 +49,9 @@ class ScopeAndPrioritySettingsSection extends SettingsSection {
}
protected function load_dependencies(): void {
require_once plugin_dir_path( __DIR__ ) . 'settings/class-setting.php';
// Require settings groups.
//require_once plugin_dir_path( __DIR__ ) . 'settings/scope-and-priority/...';
parent::load_dependencies();
//require_once plugin_dir_path( __DIR__ ) . 'general/class-reference-container-settings-group.php';
}
protected function add_settings_groups(): void {