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

@ -50,46 +50,6 @@ class Settings {
'em' => 'em',
'vw' => 'viewport width',
);
/**
* Settings container key for the short code of the footnote's start.
*
* @var string
*
* @since 1.5.0
* @todo Move to `SettingsSection`/`SettingsGroup`.
*/
const FOOTNOTES_SHORT_CODE_START = 'footnote_inputfield_placeholder_start';
/**
* Settings container key for the short code of the footnote's end.
*
* @var string
*
* @since 1.5.0
* @todo Move to `SettingsSection`/`SettingsGroup`.
*/
const FOOTNOTES_SHORT_CODE_END = 'footnote_inputfield_placeholder_end';
/**
* Settings container key for the user-defined short code of the footnotes start.
*
* @var string
*
* @since 1.5.0
* @todo Move to `SettingsSection`/`SettingsGroup`.
*/
const FOOTNOTES_SHORT_CODE_START_USER_DEFINED = 'footnote_inputfield_placeholder_start_user_defined';
/**
* Settings container key for the user-defined short code of the footnotes end.
*
* @var string
*
* @since 1.5.0
* @todo Move to `SettingsSection`/`SettingsGroup`.
*/
const FOOTNOTES_SHORT_CODE_END_USER_DEFINED = 'footnote_inputfield_placeholder_end_user_defined';
/**
* Settings container key for the counter style of the footnotes.
@ -547,26 +507,6 @@ class Settings {
* @todo Move to `SettingsSection`/`SettingsGroup`.
*/
const MOUSE_OVER_BOX_FONT_SIZE_UNIT = 'footnotes_inputfield_mouse_over_box_font_size_unit';
/**
* Settings container key for scroll offset.
*
* @var int
*
* @since 2.1.4
* @todo Move to `SettingsSection`/`SettingsGroup`.
*/
const FOOTNOTES_SCROLL_OFFSET = 'footnotes_inputfield_scroll_offset';
/**
* Settings container key for scroll duration.
*
* @var int
*
* @since 2.1.4
* @todo Move to `SettingsSection`/`SettingsGroup`.
*/
const FOOTNOTES_SCROLL_DURATION = 'footnotes_inputfield_scroll_duration';
/**
* Settings container key for tooltip display fade-in delay.
@ -728,16 +668,6 @@ class Settings {
*/
const HARD_LINK_IDS_SEPARATOR = 'footnotes_inputfield_hard_link_ids_separator';
/**
* Settings container key to enable shortcode syntax validation.
*
* @var string
*
* @since 2.4.0
* @todo Move to `SettingsSection`/`SettingsGroup`.
*/
const FOOTNOTE_SHORTCODE_SYNTAX_VALIDATION_ENABLE = 'footnotes_inputfield_shortcode_syntax_validation_enable';
/**
* Settings container key to enable backlink tooltips.
*
@ -831,46 +761,6 @@ class Settings {
*/
const FOOTNOTES_AMP_COMPATIBILITY_ENABLE = 'footnotes_inputfield_amp_compatibility_enable';
/**
* Settings container key for scroll duration asymmetricity.
*
* @var int
*
* @since 2.5.11
* @todo Move to `SettingsSection`/`SettingsGroup`.
*/
const FOOTNOTES_SCROLL_DURATION_ASYMMETRICITY = 'footnotes_inputfield_scroll_duration_asymmetricity';
/**
* Settings container key for scroll-down duration.
*
* @var int
*
* @since 2.5.11
* @todo Move to `SettingsSection`/`SettingsGroup`.
*/
const FOOTNOTES_SCROLL_DOWN_DURATION = 'footnotes_inputfield_scroll_down_duration';
/**
* Settings container key for scroll-down delay.
*
* @var int
*
* @since 2.5.11
* @todo Move to `SettingsSection`/`SettingsGroup`.
*/
const FOOTNOTES_SCROLL_DOWN_DELAY = 'footnotes_inputfield_scroll_down_delay';
/**
* Settings container key for scroll-up delay.
*
* @var int
*
* @since 2.5.11
* @todo Move to `SettingsSection`/`SettingsGroup`.
*/
const FOOTNOTES_SCROLL_UP_DELAY = 'footnotes_inputfield_scroll_up_delay';
/**
* Settings container key to set the solution of the input element label issue.
*
@ -889,18 +779,6 @@ class Settings {
*/
const FOOTNOTES_LABEL_ISSUE_SOLUTION = 'footnotes_inputfield_label_issue_solution';
/**
* Settings container key to enable CSS smooth scrolling.
*
* Native smooth scrolling only works in recent browsers.
*
* @var string
*
* @since 2.5.12
* @todo Move to `SettingsSection`/`SettingsGroup`.
*/
const FOOTNOTES_CSS_SMOOTH_SCROLLING = 'footnotes_inputfield_css_smooth_scrolling';
/**
* Contains all Settings option group slugs.
*
@ -937,25 +815,10 @@ class Settings {
// AMP compatibility.
self::FOOTNOTES_AMP_COMPATIBILITY_ENABLE => '',
// Footnote start and end short codes.
self::FOOTNOTES_SHORT_CODE_START => '((',
self::FOOTNOTES_SHORT_CODE_END => '))',
self::FOOTNOTES_SHORT_CODE_START_USER_DEFINED => '',
self::FOOTNOTES_SHORT_CODE_END_USER_DEFINED => '',
self::FOOTNOTE_SHORTCODE_SYNTAX_VALIDATION_ENABLE => 'yes',
// Footnotes numbering.
self::FOOTNOTES_COUNTER_STYLE => 'arabic_plain',
//self::COMBINE_IDENTICAL_FOOTNOTES => 'yes',
// Scrolling behavior.
self::FOOTNOTES_CSS_SMOOTH_SCROLLING => 'no',
self::FOOTNOTES_SCROLL_OFFSET => 20,
self::FOOTNOTES_SCROLL_DURATION => 380,
self::FOOTNOTES_SCROLL_DURATION_ASYMMETRICITY => 'no',
self::FOOTNOTES_SCROLL_DOWN_DURATION => 150,
self::FOOTNOTES_SCROLL_DOWN_DELAY => 0,
self::FOOTNOTES_SCROLL_UP_DELAY => 0,
self::FOOTNOTES_HARD_LINKS_ENABLE => 'no',
self::REFERRER_FRAGMENT_ID_SLUG => 'r',
self::FOOTNOTE_FRAGMENT_ID_SLUG => 'f',

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 {