| 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 |
* All features default to enabled — a missing settings key means the feature is on, so |
| 20 |
* upgrades see no change in behaviour. Reads the raw settings option directly instead |
| 21 |
* of tptn_get_option(), which would load the admin Settings class on every request; |
| 22 |
* this makes the check safe to call at plugins_loaded. |
| 23 |
* |
| 24 |
* @since 4.4.0 |
| 25 |
*/ |
| 26 |
class Feature_Manager { |
| 27 |
|
| 28 |
/** |
| 29 |
* Cached copy of the settings option. |
| 30 |
* |
| 31 |
* @since 4.4.0 |
| 32 |
* |
| 33 |
* @var array|null |
| 34 |
*/ |
| 35 |
private static $settings = null; |
| 36 |
|
| 37 |
/** |
| 38 |
* Get the map of toggleable features. |
| 39 |
* |
| 40 |
* @since 4.4.0 |
| 41 |
* |
| 42 |
* @return array Associative array of feature ID => array with 'setting' and 'default' keys. |
| 43 |
*/ |
| 44 |
public static function get_features(): array { |
| 45 |
$features = array( |
| 46 |
'blocks' => array( |
| 47 |
'setting' => 'enable_blocks', |
| 48 |
'default' => true, |
| 49 |
), |
| 50 |
'feed' => array( |
| 51 |
'setting' => 'enable_feed', |
| 52 |
'default' => true, |
| 53 |
), |
| 54 |
'legacy_widgets' => array( |
| 55 |
'setting' => 'enable_legacy_widgets', |
| 56 |
'default' => true, |
| 57 |
), |
| 58 |
'query_block' => array( |
| 59 |
'setting' => 'enable_query_block', |
| 60 |
'default' => true, |
| 61 |
), |
| 62 |
'featured_image_block' => array( |
| 63 |
'setting' => 'enable_featured_image_block', |
| 64 |
'default' => true, |
| 65 |
), |
| 66 |
'popular_posts_pro_block' => array( |
| 67 |
'setting' => 'enable_popular_posts_pro_block', |
| 68 |
'default' => true, |
| 69 |
), |
| 70 |
'fast_tracker' => array( |
| 71 |
'setting' => 'enable_fast_tracker', |
| 72 |
'default' => true, |
| 73 |
), |
| 74 |
'pro_dashboard_widgets' => array( |
| 75 |
'setting' => 'enable_pro_dashboard_widgets', |
| 76 |
'default' => true, |
| 77 |
), |
| 78 |
'popular_authors' => array( |
| 79 |
'setting' => 'enable_popular_authors', |
| 80 |
'default' => true, |
| 81 |
), |
| 82 |
'page_builders' => array( |
| 83 |
'setting' => 'enable_page_builders', |
| 84 |
'default' => true, |
| 85 |
), |
| 86 |
'sitewide_tracking' => array( |
| 87 |
'setting' => 'enable_sitewide_tracking', |
| 88 |
'default' => true, |
| 89 |
), |
| 90 |
'daily_table_rollup' => array( |
| 91 |
'setting' => 'enable_daily_table_rollup', |
| 92 |
'default' => true, |
| 93 |
), |
| 94 |
); |
| 95 |
|
| 96 |
/** |
| 97 |
* Filter the map of toggleable features. |
| 98 |
* |
| 99 |
* @since 4.4.0 |
| 100 |
* |
| 101 |
* @param array $features Associative array of feature ID => array with 'setting' and 'default' keys. |
| 102 |
*/ |
| 103 |
return apply_filters( 'tptn_features', $features ); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Whether a feature is enabled. |
| 108 |
* |
| 109 |
* A feature is enabled when its setting is missing (default) or truthy. |
| 110 |
* Unknown feature IDs are treated as enabled. |
| 111 |
* |
| 112 |
* @since 4.4.0 |
| 113 |
* |
| 114 |
* @param string $feature Feature ID. |
| 115 |
* @return bool Whether the feature is enabled. |
| 116 |
*/ |
| 117 |
public static function is_enabled( string $feature ): bool { |
| 118 |
$features = self::get_features(); |
| 119 |
|
| 120 |
if ( isset( $features[ $feature ] ) ) { |
| 121 |
$setting = $features[ $feature ]['setting']; |
| 122 |
$default = ! empty( $features[ $feature ]['default'] ); |
| 123 |
|
| 124 |
if ( null === self::$settings ) { |
| 125 |
$settings = get_option( 'tptn_settings' ); |
| 126 |
self::$settings = is_array( $settings ) ? $settings : array(); |
| 127 |
} |
| 128 |
|
| 129 |
$enabled = isset( self::$settings[ $setting ] ) ? ! empty( self::$settings[ $setting ] ) : $default; |
| 130 |
} else { |
| 131 |
$enabled = true; |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Filter whether a feature is enabled. |
| 136 |
* |
| 137 |
* @since 4.4.0 |
| 138 |
* |
| 139 |
* @param bool $enabled Whether the feature is enabled. |
| 140 |
* @param string $feature Feature ID. |
| 141 |
*/ |
| 142 |
return (bool) apply_filters( 'tptn_feature_enabled', $enabled, $feature ); |
| 143 |
} |
| 144 |
} |
| 145 |
|