| 1 |
<?php |
| 2 |
/** |
| 3 |
* Feature Manager. |
| 4 |
* |
| 5 |
* Central gate that decides which optional plugin features are loaded. |
| 6 |
* |
| 7 |
* @package WebberZone\Top_Ten |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace WebberZone\Top_Ten; |
| 11 |
|
| 12 |
if ( ! defined( 'WPINC' ) ) { |
| 13 |
die; |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* Feature Manager class. |
| 18 |
* |
| 19 |
* Reads the raw settings option to determine whether an optional feature |
| 20 |
* should be instantiated. All features default to enabled - a missing |
| 21 |
* settings key means the feature is on, so upgrades see no change in |
| 22 |
* behaviour. |
| 23 |
* |
| 24 |
* This class intentionally does not use tptn_get_option(): that function |
| 25 |
* falls back to the registered settings defaults, which would load the |
| 26 |
* admin Settings class on every request. It is safe to call at |
| 27 |
* plugins_loaded. |
| 28 |
* |
| 29 |
* @since 4.4.0 |
| 30 |
*/ |
| 31 |
class Feature_Manager { |
| 32 |
|
| 33 |
/** |
| 34 |
* Cached copy of the settings option. |
| 35 |
* |
| 36 |
* @since 4.4.0 |
| 37 |
* |
| 38 |
* @var array|null |
| 39 |
*/ |
| 40 |
private static $settings = null; |
| 41 |
|
| 42 |
/** |
| 43 |
* Get the map of toggleable features. |
| 44 |
* |
| 45 |
* @since 4.4.0 |
| 46 |
* |
| 47 |
* @return array Associative array of feature ID => array with 'setting' and 'default' keys. |
| 48 |
*/ |
| 49 |
public static function get_features(): array { |
| 50 |
$features = array( |
| 51 |
'blocks' => array( |
| 52 |
'setting' => 'enable_blocks', |
| 53 |
'default' => true, |
| 54 |
), |
| 55 |
'feed' => array( |
| 56 |
'setting' => 'enable_feed', |
| 57 |
'default' => true, |
| 58 |
), |
| 59 |
'legacy_widgets' => array( |
| 60 |
'setting' => 'enable_legacy_widgets', |
| 61 |
'default' => true, |
| 62 |
), |
| 63 |
'query_block' => array( |
| 64 |
'setting' => 'enable_query_block', |
| 65 |
'default' => true, |
| 66 |
), |
| 67 |
'featured_image_block' => array( |
| 68 |
'setting' => 'enable_featured_image_block', |
| 69 |
'default' => true, |
| 70 |
), |
| 71 |
'popular_posts_pro_block' => array( |
| 72 |
'setting' => 'enable_popular_posts_pro_block', |
| 73 |
'default' => true, |
| 74 |
), |
| 75 |
'fast_tracker' => array( |
| 76 |
'setting' => 'enable_fast_tracker', |
| 77 |
'default' => true, |
| 78 |
), |
| 79 |
'pro_dashboard_widgets' => array( |
| 80 |
'setting' => 'enable_pro_dashboard_widgets', |
| 81 |
'default' => true, |
| 82 |
), |
| 83 |
'popular_authors' => array( |
| 84 |
'setting' => 'enable_popular_authors', |
| 85 |
'default' => true, |
| 86 |
), |
| 87 |
); |
| 88 |
|
| 89 |
/** |
| 90 |
* Filter the map of toggleable features. |
| 91 |
* |
| 92 |
* @since 4.4.0 |
| 93 |
* |
| 94 |
* @param array $features Associative array of feature ID => array with 'setting' and 'default' keys. |
| 95 |
*/ |
| 96 |
return apply_filters( 'tptn_features', $features ); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Whether a feature is enabled. |
| 101 |
* |
| 102 |
* A feature is enabled when its setting is missing (default) or truthy. |
| 103 |
* Unknown feature IDs are treated as enabled. |
| 104 |
* |
| 105 |
* @since 4.4.0 |
| 106 |
* |
| 107 |
* @param string $feature Feature ID. |
| 108 |
* @return bool Whether the feature is enabled. |
| 109 |
*/ |
| 110 |
public static function is_enabled( string $feature ): bool { |
| 111 |
$features = self::get_features(); |
| 112 |
|
| 113 |
if ( isset( $features[ $feature ] ) ) { |
| 114 |
$setting = $features[ $feature ]['setting']; |
| 115 |
$default = ! empty( $features[ $feature ]['default'] ); |
| 116 |
|
| 117 |
if ( null === self::$settings ) { |
| 118 |
$settings = get_option( 'tptn_settings' ); |
| 119 |
self::$settings = is_array( $settings ) ? $settings : array(); |
| 120 |
} |
| 121 |
|
| 122 |
$enabled = isset( self::$settings[ $setting ] ) ? ! empty( self::$settings[ $setting ] ) : $default; |
| 123 |
} else { |
| 124 |
$enabled = true; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Filter whether a feature is enabled. |
| 129 |
* |
| 130 |
* @since 4.4.0 |
| 131 |
* |
| 132 |
* @param bool $enabled Whether the feature is enabled. |
| 133 |
* @param string $feature Feature ID. |
| 134 |
*/ |
| 135 |
return (bool) apply_filters( 'tptn_feature_enabled', $enabled, $feature ); |
| 136 |
} |
| 137 |
} |
| 138 |
|