give
/
src
/
FeatureFlags
/
OptionBasedFormEditor
/
Settings
/
AbstractOptionBasedFormEditorSettings.php
AbstractOptionBasedFormEditorSettings.php
1 year ago
Advanced.php
1 year ago
DefaultOptions.php
1 year ago
General.php
1 year ago
AbstractOptionBasedFormEditorSettings.php
101 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\FeatureFlags\OptionBasedFormEditor\Settings; |
| 4 | |
| 5 | use Give\FeatureFlags\OptionBasedFormEditor\OptionBasedFormEditor; |
| 6 | |
| 7 | /** |
| 8 | * @since 3.18.0 |
| 9 | */ |
| 10 | abstract class AbstractOptionBasedFormEditorSettings |
| 11 | { |
| 12 | /** |
| 13 | * @since 3.18.0 |
| 14 | */ |
| 15 | abstract public function getDisabledOptionIds(): array; |
| 16 | |
| 17 | /** |
| 18 | * @since 3.18.0 |
| 19 | */ |
| 20 | public function getDisabledSectionIds(): array |
| 21 | { |
| 22 | return []; |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * @since 3.18.0 |
| 27 | */ |
| 28 | public function getNewDefaultSection(): string |
| 29 | { |
| 30 | return ''; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * @since 3.18.0 |
| 35 | */ |
| 36 | final public function maybeDisableSections(array $sections): array |
| 37 | { |
| 38 | if (OptionBasedFormEditor::isEnabled()) { |
| 39 | return $sections; |
| 40 | } |
| 41 | |
| 42 | foreach ($sections as $key => $value) { |
| 43 | if (in_array($key, $this->getDisabledSectionIds())) { |
| 44 | unset($sections[$key]); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | return $sections; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * @since 3.18.0 |
| 53 | */ |
| 54 | final public function maybeDisableOptions(array $options): array |
| 55 | { |
| 56 | foreach ($options as $key => $value) { |
| 57 | if ( ! $this->isOptionDisabled($value['id']) && ! $this->isCurrentSectionDisabled()) { |
| 58 | continue; |
| 59 | } |
| 60 | |
| 61 | if (OptionBasedFormEditor::isEnabled()) { |
| 62 | $options[$key]['name'] .= isset($value['name']) ? OptionBasedFormEditor::helperText() : ''; |
| 63 | } else { |
| 64 | unset($options[$key]); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | return $options; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @since 3.18.0 |
| 73 | */ |
| 74 | final public function maybeSetNewDefaultSection($currentSection) |
| 75 | { |
| 76 | if (OptionBasedFormEditor::isEnabled()) { |
| 77 | return $currentSection; |
| 78 | } |
| 79 | |
| 80 | $newDefaultSection = $this->getNewDefaultSection(); |
| 81 | |
| 82 | return ! empty($newDefaultSection) && $newDefaultSection != $currentSection ? $newDefaultSection : $currentSection; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * @since 3.18.0 |
| 87 | */ |
| 88 | private function isOptionDisabled($option): bool |
| 89 | { |
| 90 | return $option && in_array($option, $this->getDisabledOptionIds()); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * @since 3.18.0 |
| 95 | */ |
| 96 | private function isCurrentSectionDisabled(): bool |
| 97 | { |
| 98 | return in_array(give_get_current_setting_section(), $this->getDisabledSectionIds()); |
| 99 | } |
| 100 | } |
| 101 |