refactor: prove new settings approach

This commit is contained in:
Ben Goldsworthy 2021-05-17 12:56:22 +01:00
parent b98f327aad
commit 7135811d15
11 changed files with 960 additions and 158 deletions

View file

@ -0,0 +1,56 @@
<?php
/**
* File providing the `GeneralSettingsSection` class.
*
* @package footnotes
* @since 2.8.0
*/
declare(strict_types=1);
namespace footnotes\includes\settings;
require_once plugin_dir_path( __DIR__ ) . 'settings/class-settings-section.php';
use footnotes\includes\settings\general\ReferenceContainerSettingsGroup;
/**
* Class defining general plugin settings.
*
* @package footnotes
* @since 2.8.0
*/
class GeneralSettingsSection extends SettingsSection {
/**
* The groups of settings within this section.
*
* @var SettingsGroup[]
*
* @since 2.8.0
*/
protected array $settings_groups;
public function __construct(
$options_group_slug,
$section_slug,
$title
) {
$this->options_group_slug = $options_group_slug;
$this->section_slug = $section_slug;
$this->title = $title;
$this->load_dependencies();
$this->add_settings_groups(get_option( $this->options_group_slug ));
}
protected function load_dependencies(): void {
require_once plugin_dir_path( __DIR__ ) . 'settings/general/class-reference-container-settings-group.php';
}
protected function add_settings_groups(): void {
$this->settings_groups = array (
ReferenceContainerSettingsGroup::GROUP_ID => new ReferenceContainerSettingsGroup($this->options_group_slug, $this->section_slug),
);
}
}

View file

@ -0,0 +1,137 @@
<?php
/**
* File providing the `Setting` class.
*
* @package footnotes
* @since 2.8.0
*/
declare(strict_types=1);
namespace footnotes\includes\settings;
/**
* Class defining a configurable plugin setting.
*
* @package footnotes
* @since 2.8.0
*/
class Setting {
/**
* Setting value.
*
* @var mixed
*
* @since 2.8.0
*/
private $value;
public function __construct(
/**
* Setting group ID.
*
* @var string
*
* @since 2.8.0
*/
private string $group_id,
/**
* Setting section slug.
*
* @var string
*
* @since 2.8.0
*/
private string $options_group_slug,
/**
* Setting section slug.
*
* @var string
*
* @since 2.8.0
*/
private string $section_slug,
/**
* Setting slug.
*
* @var string
*
* @since 2.8.0
*/
public string $key,
/**
* Setting name.
*
* @var string
*
* @since 2.8.0
*/
public string $name,
/**
* Setting description.
*
* @var string|null
*
* @since 2.8.0
*/
public ?string $description,
/**
* Setting default value.
*
* @var mixed
*
* @since 2.8.0
*/
private $default_value,
/**
* Setting data type.
*
* @var string
*
* @since 2.8.0
*/
private string $type,
/**
* Setting input field type.
*
* @var string
*
* @since 2.8.0
*/
private string $input_type
) {
register_setting( $this->options_group_slug, $this->key, $this->get_setting_args());
}
public function get_setting_args(): array {
return array (
'type' => $this->type,
'description' => $this->description,
'default' => $this->default_value,
);
}
public function get_setting_field_args(): array {
return array (
'name' => $this->key,
'label_for' => $this->key,
'type' => $this->input_type,
'value' => $this->value,
'description' => $this->description,
);
}
public function get_options_group_slug(): string {
return $this->options_group_slug;
}
public function get_section_slug(): string {
return $this->section_slug;
}
}

View file

@ -0,0 +1,71 @@
<?php
/**
* File providing the `SettingsGroup` class.
*
* @package footnotes
* @since 2.8.0
*/
declare(strict_types=1);
namespace footnotes\includes\settings;
use footnotes\admin\layout as Layout;
/**
* Class defining a group of plugin settings within a section.
*
* @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;
/**
* Setting group ID.
*
* @var string
*
* @since 2.8.0
*/
const GROUP_ID = '';
/**
* The setting classes.
*
* @var string[]
*
* @since 2.8.0
*/
protected array $setting_classes;
/**
* The general settings.
*
* @var Setting[]
*
* @since 2.8.0
*/
protected array $settings;
protected abstract function load_dependencies(): void;
protected abstract function add_settings(array $options): void;
protected abstract function add_settings_fields(Layout\Settings $component): void;
}

View file

@ -0,0 +1,96 @@
<?php
/**
* File providing the (new) `SettingsSection` class.
*
* @package footnotes
* @since 2.8.0
*/
declare(strict_types=1);
namespace footnotes\includes\settings;
use footnotes\admin\layout as Layout;
/**
* Class defining plugin settings.
*
* @package footnotes
* @since 2.8.0
*/
abstract class SettingsSection {
/**
* Setting options group slug.
*
* @var string
*
* @since 2.8.0
*/
protected string $options_group_slug;
/**
* Settings section slug.
*
* @var string
*
* @since 2.8.0
*/
protected string $section_slug = '';
/**
* Settings section title.
*
* @var string
*
* @since 2.8.0
*/
protected string $title = '';
/**
* The groups of settings within this section.
*
* @var SettingGroup[]
*
* @since 2.8.0
*/
protected array $settings_groups;
protected abstract function load_dependencies(): void;
public function add_settings_section(): void {
add_settings_section(
$this->section_slug,
$this->title,
array($this, 'setting_section_callback'),
'footnotes'
);
}
public function add_settings_fields($component): void {
foreach($this->settings_groups as $settings_group) {
$settings_group->add_settings_fields($component);
}
}
public function setting_section_callback(): void {
echo "<hr>";
}
protected abstract function add_settings_groups(): void;
public function get_options_group_slug(): string {
return $this->options_group_slug;
}
public function get_section_slug(): string {
return $this->section_slug;
}
public function get_title(): string {
return $this->title;
}
public function get_settings_group(string $group_id) {
return $this->settings_groups[$group_id];
}
}

View file

@ -0,0 +1,122 @@
<?php
/**
* File providing the `ReferenceContainerSettingsGroup` class.
*
* @package footnotes
* @since 2.8.0
*/
declare(strict_types=1);
namespace footnotes\includes\settings\general;
require_once plugin_dir_path( __DIR__ ) . 'class-settings-group.php';
use footnotes\includes\settings\Setting;
use footnotes\includes\settings\SettingsGroup;
use footnotes\admin\layout\Settings as SettingsLayout;
/**
* Class defining the reference container settings.
*
* @package footnotes
* @since 2.8.0
*/
class ReferenceContainerSettingsGroup extends SettingsGroup {
/**
* Setting group ID.
*
* @var string
*
* @since 2.8.0
*/
const GROUP_ID = 'reference-container';
/**
* Settings container key for the label of the reference container.
*
* @var string
*
* @since 1.5.0
*/
const REFERENCE_CONTAINER_NAME = array(
'key' => 'footnote_inputfield_references_label',
'name' => 'Reference Container Name',
'description' => 'The title of the reference container.',
'default_value' => 'Reference',
'type' => 'string',
'input_type' => 'text'
);
/**
* The general settings.
*
* @var Setting[]
*
* @since 2.8.0
*/
protected array $settings;
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 function load_dependencies(): void {
require_once plugin_dir_path( __DIR__ ) . 'class-setting.php';
}
protected function add_settings(array $options): void {
$this->settings = array(
self::REFERENCE_CONTAINER_NAME['key'] => $this->add_setting(self::REFERENCE_CONTAINER_NAME)
);
}
private function add_setting(array $setting): Setting {
extract( $setting );
return new Setting(
self::GROUP_ID,
$this->options_group_slug,
$this->section_slug,
$key,
$name,
$description,
$default_value,
$type,
$input_type
);
}
public function add_settings_fields($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()
);
}
}
}

View file

@ -0,0 +1,106 @@
<?php
/**
* File providing the `ReferenceContainerNameSetting` class.
*
* @package footnotes
* @since 2.8.0
*/
declare(strict_types=1);
namespace footnotes\includes\settings\general\ReferenceContainer;
require_once plugin_dir_path( dirname( __FILE__, 2 ) ) . 'class-setting.php';
use footnotes\includes\settings as Settings;
/**
* Class defining the reference container name setting.
*
* @package footnotes
* @since 2.8.0
*/
class ReferenceContainerNameSetting extends Settings\Setting {
/**
* Setting slug.
*
* @var string
*
* @since 2.8.0
*/
const KEY = 'footnote_inputfield_references_label';
/**
* Setting name.
*
* @var string
*
* @since 2.8.0
*/
const NAME = 'Reference Container Title';
/**
* Setting description.
*
* @var string
*
* @since 2.8.0
*/
const DESCRIPTION = 'The title of the reference container.';
/**
* Setting default value.
*
* @var mixed
*
* @since 2.8.0
*/
const DEFAULT_VALUE = 'Reference';
/**
* Setting data type.
*
* @var string
*
* @since 2.8.0
*/
const TYPE = 'string';
/**
* Setting input field type.
*
* @var string
*
* @since 2.8.0
*/
const INPUT_TYPE = 'text';
public function __construct(
/**
* Setting group ID.
*
* @var string
*
* @since 2.8.0
*/
private string $section_group_id,
/**
* Setting section slug.
*
* @var string
*
* @since 2.8.0
*/
private string $options_group_slug,
/**
* Setting section slug.
*
* @var string
*
* @since 2.8.0
*/
private string $section_slug
) {
register_setting( $this->options_group_slug, self::NAME, $this->get_setting_args());
}
}