PluginProbe
WebberZone Top 10 — Popular Posts / 4.5.2
WebberZone Top 10 — Popular Posts v4.5.2
4.5.2 4.5.1 4.5.0 4.4.3 4.4.2 4.4.1 4.4.0 4.3.4 4.3.3 4.3.2 4.3.1 4.3.0 trunk 1.0 1.0.1 1.1 1.2 1.3 1.4 1.4.1 1.5 1.5.1 1.5.2 1.5.3 1.6 All 118 releases
top-10 / includes / class-feature-manager.php

class-feature-manager.php in WebberZone Top 10 — Popular Posts 4.5.2, at includes/class-feature-manager.php

153 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 'rest_api' => array(
59 'setting' => 'enable_rest_api',
60 'default' => true,
61 ),
62 'lazy_load' => array(
63 'setting' => 'enable_lazy_load',
64 'default' => true,
65 ),
66 'query_block' => array(
67 'setting' => 'enable_query_block',
68 'default' => true,
69 ),
70 'featured_image_block' => array(
71 'setting' => 'enable_featured_image_block',
72 'default' => true,
73 ),
74 'popular_posts_pro_block' => array(
75 'setting' => 'enable_popular_posts_pro_block',
76 'default' => true,
77 ),
78 'fast_tracker' => array(
79 'setting' => 'enable_fast_tracker',
80 'default' => true,
81 ),
82 'pro_dashboard_widgets' => array(
83 'setting' => 'enable_pro_dashboard_widgets',
84 'default' => true,
85 ),
86 'popular_authors' => array(
87 'setting' => 'enable_popular_authors',
88 'default' => true,
89 ),
90 'page_builders' => array(
91 'setting' => 'enable_page_builders',
92 'default' => true,
93 ),
94 'sitewide_tracking' => array(
95 'setting' => 'enable_sitewide_tracking',
96 'default' => true,
97 ),
98 'daily_table_rollup' => array(
99 'setting' => 'enable_daily_table_rollup',
100 'default' => true,
101 ),
102 );
103
104 /**
105 * Filter the map of toggleable features.
106 *
107 * @since 4.4.0
108 *
109 * @param array $features Associative array of feature ID => array with 'setting' and 'default' keys.
110 */
111 return apply_filters( 'tptn_features', $features );
112 }
113
114 /**
115 * Whether a feature is enabled.
116 *
117 * A feature is enabled when its setting is missing (default) or truthy.
118 * Unknown feature IDs are treated as enabled.
119 *
120 * @since 4.4.0
121 *
122 * @param string $feature Feature ID.
123 * @return bool Whether the feature is enabled.
124 */
125 public static function is_enabled( string $feature ): bool {
126 $features = self::get_features();
127
128 if ( isset( $features[ $feature ] ) ) {
129 $setting = $features[ $feature ]['setting'];
130 $default = ! empty( $features[ $feature ]['default'] );
131
132 if ( null === self::$settings ) {
133 $settings = get_option( 'tptn_settings' );
134 self::$settings = is_array( $settings ) ? $settings : array();
135 }
136
137 $enabled = isset( self::$settings[ $setting ] ) ? ! empty( self::$settings[ $setting ] ) : $default;
138 } else {
139 $enabled = true;
140 }
141
142 /**
143 * Filter whether a feature is enabled.
144 *
145 * @since 4.4.0
146 *
147 * @param bool $enabled Whether the feature is enabled.
148 * @param string $feature Feature ID.
149 */
150 return (bool) apply_filters( 'tptn_feature_enabled', $enabled, $feature );
151 }
152 }
153