| 1 |
<?php |
| 2 |
/** |
| 3 |
* Addon Feature Toggle helper. |
| 4 |
* |
| 5 |
* Reads the per-addon settings option managed by Core's |
| 6 |
* AddonSettingsPage (`f12_doi_addon_{id}_settings`) and reports whether |
| 7 |
* the addon's feature is currently enabled. Distinct from WP plugin |
| 8 |
* activation: a plugin can be active in WordPress while the user has |
| 9 |
* temporarily paused its feature here. |
| 10 |
* |
| 11 |
* Default `true` matches the convention "activating the plugin = |
| 12 |
* opting in to its default behaviour" (Akismet-style). Addons that |
| 13 |
* inherit a legacy on/off setting (e.g. `f12-doi-settings.mx_validation_enabled`) |
| 14 |
* implement their own `isFeatureEnabled()` with a fallback chain — see |
| 15 |
* `MxValidator::isFeatureEnabled()` for the reference pattern. |
| 16 |
* |
| 17 |
* @package Forge12\DoubleOptIn\Addon |
| 18 |
* @since 4.4.0 |
| 19 |
*/ |
| 20 |
|
| 21 |
namespace Forge12\DoubleOptIn\Addon; |
| 22 |
|
| 23 |
if ( ! defined( 'ABSPATH' ) ) { |
| 24 |
exit; |
| 25 |
} |
| 26 |
|
| 27 |
final class AddonFeatureToggle { |
| 28 |
|
| 29 |
private const OPTION_PREFIX = 'f12_doi_addon_'; |
| 30 |
private const OPTION_SUFFIX = '_settings'; |
| 31 |
|
| 32 |
/** |
| 33 |
* Is the addon's feature currently enabled? |
| 34 |
* |
| 35 |
* @param string $addonId Internal addon ID matching `AddonInterface::getId()`. |
| 36 |
* @param bool $default Returned when the option has not been written yet. |
| 37 |
*/ |
| 38 |
public static function isEnabled( string $addonId, bool $default = true ): bool { |
| 39 |
$settings = get_option( self::OPTION_PREFIX . $addonId . self::OPTION_SUFFIX, null ); |
| 40 |
if ( is_array( $settings ) && array_key_exists( 'enabled', $settings ) ) { |
| 41 |
return (bool) $settings['enabled']; |
| 42 |
} |
| 43 |
return $default; |
| 44 |
} |
| 45 |
} |
| 46 |
|