Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 52 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
| Feature | |
0.00% |
0 / 52 |
|
0.00% |
0 / 9 |
156 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
6 | |||
| initialize | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setValue | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| enable | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
6 | |||
| disable | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
6 | |||
| isEnabled | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
| isTogglable | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
| canToggle | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace NewfoldLabs\WP\Module\Features; |
| 4 | |
| 5 | use NewfoldLabs\WP\Module\Features\Registry; |
| 6 | use WP_Forge\Options\Options; |
| 7 | |
| 8 | /** |
| 9 | * Base class for a feature in the Newfold plugin. |
| 10 | * |
| 11 | * Child classes should define a name property as the feature name for all API calls. This name will be used in the registry. |
| 12 | * Child class naming convention is {FeatureName}Feature. |
| 13 | */ |
| 14 | abstract class Feature { |
| 15 | |
| 16 | /** |
| 17 | * Options object |
| 18 | * |
| 19 | * @var Options |
| 20 | */ |
| 21 | private $options; |
| 22 | |
| 23 | /** |
| 24 | * The feature name. |
| 25 | * |
| 26 | * @var string |
| 27 | */ |
| 28 | protected $name; |
| 29 | |
| 30 | /** |
| 31 | * The feature value. |
| 32 | * |
| 33 | * @var boolean |
| 34 | */ |
| 35 | protected $value = false; |
| 36 | |
| 37 | /** |
| 38 | * Constructor |
| 39 | * |
| 40 | * @param Options $options The associated Options for saving to database |
| 41 | */ |
| 42 | final public function __construct( $options ) { |
| 43 | |
| 44 | // assign options |
| 45 | $this->options = $options; |
| 46 | $this->setValue( |
| 47 | // check if state already saved to options |
| 48 | $this->options->get( |
| 49 | $this->name, |
| 50 | // use default value via defaultValue filter |
| 51 | apply_filters( |
| 52 | "newfold/features/filter/defaultValue:{$this->name}", |
| 53 | $this->value |
| 54 | ) |
| 55 | ) |
| 56 | ); |
| 57 | |
| 58 | // only initialize if enabled |
| 59 | if ( $this->isEnabled() ) { |
| 60 | $this->initialize(); |
| 61 | |
| 62 | // specific feature onInitialize action |
| 63 | do_action( "newfold/features/action/onInitialize:{$this->name}" ); |
| 64 | } |
| 65 | |
| 66 | // else not initialized or loaded - does nothing |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Init |
| 71 | * |
| 72 | * Add this in the child feature class. |
| 73 | */ |
| 74 | protected function initialize() { |
| 75 | // do initialization stuff - nothing here but in the child class |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Set Value - this updates the value as well as the option |
| 80 | * |
| 81 | * @param boolean $value The value to set. |
| 82 | */ |
| 83 | private function setValue( $value ) { |
| 84 | $this->value = $value; |
| 85 | $this->options->set( $this->name, $value ); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Enables the feature. |
| 90 | * |
| 91 | * @return boolean True if successful, false otherwise |
| 92 | */ |
| 93 | final public function enable() { |
| 94 | if ( $this->isTogglable() ) { |
| 95 | |
| 96 | // generic feature beforeEnable action |
| 97 | do_action( 'newfold/features/action/beforeEnable', $this->name ); |
| 98 | // specific feature beforeEnable action |
| 99 | do_action( "newfold/features/action/beforeEnable:{$this->name}" ); |
| 100 | |
| 101 | // generic feature onEnable action |
| 102 | do_action( 'newfold/features/action/onEnable', $this->name ); |
| 103 | // specific feature onEnable action |
| 104 | do_action( "newfold/features/action/onEnable:{$this->name}" ); |
| 105 | $this->setValue( true ); |
| 106 | |
| 107 | // generic feature afterEnable action |
| 108 | do_action( 'newfold/features/action/afterEnable', $this->name ); |
| 109 | // specific feature afterEnable action |
| 110 | do_action( "newfold/features/action/afterEnable:{$this->name}" ); |
| 111 | |
| 112 | return true; |
| 113 | } |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Disables the feature. |
| 119 | * |
| 120 | * @return boolean True if successful, false otherwise |
| 121 | */ |
| 122 | final public function disable() { |
| 123 | if ( $this->isTogglable() ) { |
| 124 | |
| 125 | // generic feature beforeDisable action |
| 126 | do_action( 'newfold/features/action/beforeDisable', $this->name ); |
| 127 | // specific feature beforeDisable action |
| 128 | do_action( "newfold/features/action/beforeDisable:{$this->name}" ); |
| 129 | |
| 130 | // generic feature onDisable action |
| 131 | do_action( 'newfold/features/action/onDisable', $this->name ); |
| 132 | // specific feature onDisable action |
| 133 | do_action( "newfold/features/action/onDisable:{$this->name}" ); |
| 134 | $this->setValue( false ); |
| 135 | |
| 136 | // generic feature afterDisable action |
| 137 | do_action( 'newfold/features/action/afterDisable', $this->name ); |
| 138 | // specific feature afterDisable action |
| 139 | do_action( "newfold/features/action/afterDisable:{$this->name}" ); |
| 140 | |
| 141 | return true; |
| 142 | } |
| 143 | return false; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Checks if the feature is enabled. |
| 148 | * |
| 149 | * @return bool True if the feature is enabled, false otherwise. |
| 150 | */ |
| 151 | final public function isEnabled() { |
| 152 | return apply_filters( |
| 153 | // specific feature isEnabled filter |
| 154 | "newfold/features/filter/isEnabled:{$this->name}", |
| 155 | apply_filters( |
| 156 | // generic isEnabled filter |
| 157 | 'newfold/features/filter/isEnabled', |
| 158 | $this->value |
| 159 | ) |
| 160 | ); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Determine if the features toggle is allowed |
| 165 | * |
| 166 | * @return bool True if the feature can toggle, false otherwise. |
| 167 | */ |
| 168 | final public function isTogglable() { |
| 169 | return (bool) apply_filters( |
| 170 | // specific feature canToggle filter |
| 171 | "newfold/features/filter/canToggle:{$this->name}", |
| 172 | apply_filters( |
| 173 | // generic canToggle filter |
| 174 | 'newfold/features/filter/canToggle', |
| 175 | $this->canToggle() |
| 176 | ) |
| 177 | ); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Check if the feature can be toggled with current permissions. |
| 182 | * |
| 183 | * @return bool True if the feature toggle is allowed, false otherwise. |
| 184 | */ |
| 185 | public function canToggle() { |
| 186 | return current_user_can( 'manage_options' ); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Get Name |
| 191 | */ |
| 192 | public function getName() { |
| 193 | return $this->name; |
| 194 | } |
| 195 | } |