| 1 |
<?php |
| 2 |
|
| 3 |
namespace MasterAddons\Inc\Admin\Settings; |
| 4 |
|
| 5 |
/** |
| 6 |
* Group-scoped terminal query object |
| 7 |
* |
| 8 |
* Returned by SettingsProxy::group('basic'). Provides |
| 9 |
* scoped queries and bulk operations on a single sub-group: |
| 10 |
* |
| 11 |
* Settings::instance()->addons->group('basic')->enabled() |
| 12 |
* Settings::instance()->addons->group('basic')->enable() |
| 13 |
* |
| 14 |
* @package MasterAddons |
| 15 |
* @since 2.1.0 |
| 16 |
*/ |
| 17 |
class SettingsGroup |
| 18 |
{ |
| 19 |
/** |
| 20 |
* Parent proxy |
| 21 |
* |
| 22 |
* @var SettingsProxy |
| 23 |
*/ |
| 24 |
private $proxy; |
| 25 |
|
| 26 |
/** |
| 27 |
* Top-level group name (addons, extensions) |
| 28 |
* |
| 29 |
* @var string |
| 30 |
*/ |
| 31 |
private $type; |
| 32 |
|
| 33 |
/** |
| 34 |
* Sub-group key (e.g. 'basic', 'marketing') |
| 35 |
* |
| 36 |
* @var string |
| 37 |
*/ |
| 38 |
private $group_key; |
| 39 |
|
| 40 |
/** |
| 41 |
* @param SettingsProxy $proxy Parent proxy |
| 42 |
* @param string $type Top-level group (addons, extensions) |
| 43 |
* @param string $group_key Sub-group key |
| 44 |
*/ |
| 45 |
public function __construct(SettingsProxy $proxy, $type, $group_key) |
| 46 |
{ |
| 47 |
$this->proxy = $proxy; |
| 48 |
$this->type = $type; |
| 49 |
$this->group_key = $group_key; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Get all items in this sub-group from Config |
| 54 |
* |
| 55 |
* @return array |
| 56 |
*/ |
| 57 |
public function all() |
| 58 |
{ |
| 59 |
return $this->proxy->get_config_items_by_group($this->group_key); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Get enabled items in this sub-group |
| 64 |
* |
| 65 |
* @return array |
| 66 |
*/ |
| 67 |
public function enabled() |
| 68 |
{ |
| 69 |
$enabled_settings = $this->proxy->get() ?: []; |
| 70 |
$group_items = $this->all(); |
| 71 |
$enabled = []; |
| 72 |
|
| 73 |
foreach ($group_items as $key => $item) { |
| 74 |
if (!empty($enabled_settings[$key])) { |
| 75 |
$enabled[$key] = $item; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
return $enabled; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Get counts for this sub-group |
| 84 |
* |
| 85 |
* @return array ['total' => n, 'enabled' => n] |
| 86 |
*/ |
| 87 |
public function counts() |
| 88 |
{ |
| 89 |
$enabled_settings = $this->proxy->get() ?: []; |
| 90 |
$group_items = $this->all(); |
| 91 |
$enabled = 0; |
| 92 |
|
| 93 |
foreach (array_keys($group_items) as $key) { |
| 94 |
if (!empty($enabled_settings[$key])) { |
| 95 |
$enabled++; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
return [ |
| 100 |
'total' => count($group_items), |
| 101 |
'enabled' => $enabled, |
| 102 |
]; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Enable all items in this sub-group |
| 107 |
* |
| 108 |
* @return bool |
| 109 |
*/ |
| 110 |
public function enable() |
| 111 |
{ |
| 112 |
return $this->proxy->enable_group($this->group_key); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Disable all items in this sub-group |
| 117 |
* |
| 118 |
* @return bool |
| 119 |
*/ |
| 120 |
public function disable() |
| 121 |
{ |
| 122 |
return $this->proxy->disable_group($this->group_key); |
| 123 |
} |
| 124 |
} |
| 125 |
|