PluginProbe
Master Addons for Elementor – Elementor Addons, Widgets, Mega Menu Builder, Popup Builder, Widget Builder & Template Kits / 3.2.3
Master Addons for Elementor – Elementor Addons, Widgets, Mega Menu Builder, Popup Builder, Widget Builder & Template Kits v3.2.3
3.2.2 3.2.3 3.2.1 3.2.0 3.1.9 3.1.8 3.1.7 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.9 trunk 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.3 1.1.4 1.1.5 All 174 releases
master-addons / inc / admin / settings / settings-proxy.php

settings-proxy.php in Master Addons for Elementor – Elementor Addons, Widgets, Mega Menu Builder, Popup Builder, Widget Builder & Template Kits 3.2.3, at inc/admin/settings/settings-proxy.php

284 lines 6.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace MasterAddons\Inc\Admin\Settings;
4
5 use MasterAddons\Inc\Admin\Config;
6
7 /**
8 * Per-group fluent proxy for Settings
9 *
10 * Provides chainable access to a single settings group:
11 * Settings::instance()->addons->get('key')
12 * Settings::instance()->addons->is_enabled('key')
13 * Settings::instance()->addons->group('basic')->enabled()
14 *
15 * @package MasterAddons
16 * @since 2.1.0
17 */
18 class SettingsProxy
19 {
20 /**
21 * Group name (addons, extensions, plugins, icons, api)
22 *
23 * @var string
24 */
25 private $group;
26
27 /**
28 * Option key for this group
29 *
30 * @var string
31 */
32 private $option_key;
33
34 /**
35 * Groups that support sub-group operations
36 *
37 * @var array
38 */
39 private const GROUPABLE = ['addons', 'extensions'];
40
41 /**
42 * @param string $group Group name
43 * @param string $option_key WordPress option key
44 */
45 public function __construct($group, $option_key)
46 {
47 $this->group = $group;
48 $this->option_key = $option_key;
49 }
50
51 /**
52 * Get setting value(s)
53 *
54 * @param string|null $key Specific key or null for all
55 * @param mixed $default Default value
56 * @return mixed
57 */
58 public function get($key = null, $default = null)
59 {
60 return Settings::get_option($this->option_key, $key, $default);
61 }
62
63 /**
64 * Save settings
65 *
66 * @param array $data Settings array
67 * @return bool
68 */
69 public function save(array $data)
70 {
71 return Settings::save_option($this->option_key, $data);
72 }
73
74 /**
75 * Check if an item is enabled
76 *
77 * @param string $key Item key
78 * @return bool
79 */
80 public function is_enabled($key)
81 {
82 return (bool) $this->get($key, true);
83 }
84
85 /**
86 * Get all enabled items with full config data
87 *
88 * Merges DB enabled state with Config definitions.
89 *
90 * @return array
91 */
92 public function enabled()
93 {
94 $enabled_settings = $this->get() ?: [];
95 $all_items = $this->get_config_items();
96 $enabled = [];
97
98 foreach ($all_items as $key => $item) {
99 if (!empty($enabled_settings[$key])) {
100 $enabled[$key] = $item;
101 }
102 }
103
104 return $enabled;
105 }
106
107 /**
108 * Get default settings array
109 *
110 * @return array
111 */
112 public function defaults()
113 {
114 $all_items = $this->get_config_items();
115 $defaults = [];
116
117 foreach (array_keys($all_items) as $key) {
118 // Mega menu disabled by default for extensions
119 if ($this->group === 'extensions' && $key === 'mega-menu') {
120 $defaults[$key] = false;
121 } else {
122 $defaults[$key] = true;
123 }
124 }
125
126 return $defaults;
127 }
128
129 /**
130 * Get counts by sub-group (addons/extensions only)
131 *
132 * @return array ['group_key' => ['total' => n, 'enabled' => n]]
133 */
134 public function counts()
135 {
136 $this->require_groupable('counts');
137
138 $enabled_settings = $this->get() ?: [];
139 $groups = $this->get_config_groups();
140 $counts = [];
141
142 foreach (array_keys($groups) as $group_key) {
143 $group_items = $this->get_config_items_by_group($group_key);
144 $enabled = 0;
145
146 foreach (array_keys($group_items) as $key) {
147 if (!empty($enabled_settings[$key])) {
148 $enabled++;
149 }
150 }
151
152 $counts[$group_key] = [
153 'total' => count($group_items),
154 'enabled' => $enabled,
155 ];
156 }
157
158 return $counts;
159 }
160
161 /**
162 * Get a group-scoped query object
163 *
164 * @param string $group_key Sub-group key (e.g. 'basic', 'marketing')
165 * @return SettingsGroup
166 */
167 public function group($group_key)
168 {
169 $this->require_groupable('group');
170 $group_key = sanitize_key($group_key);
171
172 return new SettingsGroup($this, $this->group, $group_key);
173 }
174
175 /**
176 * Enable all items in a sub-group
177 *
178 * @param string $group_key Sub-group key
179 * @return bool
180 */
181 public function enable_group($group_key)
182 {
183 $this->require_groupable('enable_group');
184 $group_key = sanitize_key($group_key);
185
186 $current = $this->get() ?: [];
187 $group_items = $this->get_config_items_by_group($group_key);
188
189 foreach (array_keys($group_items) as $key) {
190 $current[$key] = true;
191 }
192
193 return $this->save($current);
194 }
195
196 /**
197 * Disable all items in a sub-group
198 *
199 * @param string $group_key Sub-group key
200 * @return bool
201 */
202 public function disable_group($group_key)
203 {
204 $this->require_groupable('disable_group');
205 $group_key = sanitize_key($group_key);
206
207 $current = $this->get() ?: [];
208 $group_items = $this->get_config_items_by_group($group_key);
209
210 foreach (array_keys($group_items) as $key) {
211 $current[$key] = false;
212 }
213
214 return $this->save($current);
215 }
216
217 /**
218 * Get all config items for this group
219 *
220 * @return array
221 */
222 public function get_config_items()
223 {
224 switch ($this->group) {
225 case 'addons':
226 return Config::get_addons();
227 case 'extensions':
228 return Config::get_extensions();
229 default:
230 return [];
231 }
232 }
233
234 /**
235 * Get config items by sub-group
236 *
237 * @param string $group_key
238 * @return array
239 */
240 public function get_config_items_by_group($group_key)
241 {
242 switch ($this->group) {
243 case 'addons':
244 return Config::get_addons_by_group($group_key);
245 case 'extensions':
246 return Config::get_extensions_by_group($group_key);
247 default:
248 return [];
249 }
250 }
251
252 /**
253 * Get config groups
254 *
255 * @return array
256 */
257 private function get_config_groups()
258 {
259 switch ($this->group) {
260 case 'addons':
261 return Config::get_addons_grouped();
262 case 'extensions':
263 return Config::get_extensions_grouped();
264 default:
265 return [];
266 }
267 }
268
269 /**
270 * Guard: throw if this group doesn't support sub-group operations
271 *
272 * @param string $method Method name for error message
273 * @throws \BadMethodCallException
274 */
275 private function require_groupable($method)
276 {
277 if (!in_array($this->group, self::GROUPABLE, true)) {
278 throw new \BadMethodCallException(
279 sprintf('%s() is not available for the "%s" settings group.', $method, $this->group) // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- Exception message, not direct output
280 );
281 }
282 }
283 }
284