| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handle the settings structure. |
| 4 |
* |
| 5 |
* @link https://bootstrapped.ventures |
| 6 |
* @since 1.0.0 |
| 7 |
* |
| 8 |
* @package BV_Settings |
| 9 |
* @author Brecht Vandersmissen <[email protected]> |
| 10 |
*/ |
| 11 |
|
| 12 |
class BV_Structure { |
| 13 |
private $bvs; |
| 14 |
|
| 15 |
private $structure = array(); |
| 16 |
private $defaults = array(); |
| 17 |
private $settings = array(); |
| 18 |
|
| 19 |
/** |
| 20 |
* Store main instance and initialize. |
| 21 |
* |
| 22 |
* @since 1.0.0 |
| 23 |
*/ |
| 24 |
public function __construct( $bvs ) { |
| 25 |
$this->bvs = $bvs; |
| 26 |
$this->set_structure( $this->bvs->atts['settings'] ); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Set the settings structure. |
| 31 |
* |
| 32 |
* @since 1.0.0 |
| 33 |
* @param array $settings_structure Settings structure. |
| 34 |
*/ |
| 35 |
public function set_structure( $settings_structure ) { |
| 36 |
// Associate IDs. |
| 37 |
$structure = array(); |
| 38 |
|
| 39 |
$index = 1; |
| 40 |
foreach ( $settings_structure as $group ) { |
| 41 |
if ( isset( $group['id'] ) ) { |
| 42 |
$id = $group['id']; |
| 43 |
} else { |
| 44 |
$id = 'group_' . $index; |
| 45 |
$index++; |
| 46 |
} |
| 47 |
|
| 48 |
$structure[ $id ] = $group; |
| 49 |
} |
| 50 |
|
| 51 |
$this->structure = $structure; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Get the settings structure. |
| 56 |
* |
| 57 |
* @since 1.0.0 |
| 58 |
* @param mixed $resolve_callbacks Wether to resolve the callbacks. |
| 59 |
*/ |
| 60 |
public function get_structure( $resolve_callbacks = false ) { |
| 61 |
$structure = apply_filters( $this->bvs->atts['uid'] . '_settings_structure', $this->structure ); |
| 62 |
|
| 63 |
if ( $resolve_callbacks ) { |
| 64 |
// Loop over structure to find settings with callback. |
| 65 |
foreach ( $structure as $group_id => $group ) { |
| 66 |
if ( isset( $group['settings'] ) ) { |
| 67 |
foreach ( $group['settings'] as $setting_id => $setting ) { |
| 68 |
if ( isset( $setting['optionsCallback'] ) ) { |
| 69 |
$structure[ $group_id ]['settings'][ $setting_id ]['options'] = call_user_func( $setting['optionsCallback'], $setting ); |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
if ( isset( $group['subGroups'] ) ) { |
| 75 |
foreach ( $group['subGroups'] as $sub_group_id => $sub_group ) { |
| 76 |
if ( isset( $sub_group['settings'] ) ) { |
| 77 |
foreach ( $sub_group['settings'] as $setting_id => $setting ) { |
| 78 |
if ( isset( $setting['optionsCallback'] ) ) { |
| 79 |
$structure[ $group_id ]['subGroups'][ $sub_group_id ]['settings'][ $setting_id ]['options'] = call_user_func( $setting['optionsCallback'], $setting ); |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
apply_filters( $this->bvs->atts['uid'] . '_settings_structure_callbacks', $structure ); |
| 88 |
} |
| 89 |
|
| 90 |
return $structure; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Get the value for a specific setting. |
| 95 |
* |
| 96 |
* @since 1.0.0 |
| 97 |
* @param mixed $setting Setting to get the value for. |
| 98 |
*/ |
| 99 |
public function get( $setting ) { |
| 100 |
$settings = $this->get_settings(); |
| 101 |
|
| 102 |
if ( isset( $settings[ $setting ] ) ) { |
| 103 |
return $settings[ $setting ]; |
| 104 |
} else { |
| 105 |
return $this->get_default( $setting ); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Get all the settings. |
| 111 |
* |
| 112 |
* @since 1.0.0 |
| 113 |
*/ |
| 114 |
public function get_settings() { |
| 115 |
// Lazy load settings. |
| 116 |
if ( empty( $this->settings ) ) { |
| 117 |
$this->set_settings( apply_filters( $this->bvs->atts['uid'] . '_settings', get_option( $this->bvs->atts['uid'] . '_settings', array() ) ) ); |
| 118 |
} |
| 119 |
|
| 120 |
return $this->settings; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Set the settings. |
| 125 |
* |
| 126 |
* @since 1.0.0 |
| 127 |
* @param array $settings Settings to set. |
| 128 |
*/ |
| 129 |
public function set_settings( $settings ) { |
| 130 |
$this->settings = $settings; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Get all the settings with defaults if not set. |
| 135 |
* |
| 136 |
* @since 1.0.0 |
| 137 |
*/ |
| 138 |
public function get_settings_with_defaults() { |
| 139 |
$settings = $this->get_settings(); |
| 140 |
$defaults = $this->get_defaults(); |
| 141 |
|
| 142 |
return array_merge( $defaults, $settings ); |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Get the default for a specific setting. |
| 147 |
* |
| 148 |
* @since 1.0.0 |
| 149 |
* @param mixed $setting Setting to get the default for. |
| 150 |
*/ |
| 151 |
public function get_default( $setting ) { |
| 152 |
$defaults = $this->get_defaults(); |
| 153 |
if ( isset( $defaults[ $setting ] ) ) { |
| 154 |
return $defaults[ $setting ]; |
| 155 |
} else { |
| 156 |
// Force defaults cache update. |
| 157 |
$defaults = $this->get_defaults( true ); |
| 158 |
if ( isset( $defaults[ $setting ] ) ) { |
| 159 |
return $defaults[ $setting ]; |
| 160 |
} else { |
| 161 |
return false; |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Get the default settings. |
| 168 |
* |
| 169 |
* @since 1.0.0 |
| 170 |
* @param boolean $force_update Wether to force an update of the cache. |
| 171 |
*/ |
| 172 |
public function get_defaults( $force_update = false ) { |
| 173 |
if ( $force_update || empty( $this->defaults ) ) { |
| 174 |
$defaults = array(); |
| 175 |
$structure = $this->get_structure(); |
| 176 |
|
| 177 |
// Loop over structure to find settings and defaults. |
| 178 |
foreach ( $structure as $group ) { |
| 179 |
if ( isset( $group['settings'] ) ) { |
| 180 |
foreach ( $group['settings'] as $setting ) { |
| 181 |
if ( isset( $setting['id'] ) && isset( $setting['default'] ) ) { |
| 182 |
$defaults[ $setting['id'] ] = $setting['default']; |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
if ( isset( $group['subGroups'] ) ) { |
| 188 |
foreach ( $group['subGroups'] as $sub_group ) { |
| 189 |
if ( isset( $sub_group['settings'] ) ) { |
| 190 |
foreach ( $sub_group['settings'] as $setting ) { |
| 191 |
if ( isset( $setting['id'] ) && isset( $setting['default'] ) ) { |
| 192 |
$defaults[ $setting['id'] ] = $setting['default']; |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
$this->defaults = $defaults; |
| 201 |
} |
| 202 |
|
| 203 |
return $this->defaults; |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Get the settings details. |
| 208 |
* |
| 209 |
* @since 1.0.0 |
| 210 |
*/ |
| 211 |
public function get_details() { |
| 212 |
$details = array(); |
| 213 |
$structure = $this->get_structure(); |
| 214 |
|
| 215 |
// Loop over structure to find settings. |
| 216 |
foreach ( $structure as $group ) { |
| 217 |
if ( isset( $group['settings'] ) ) { |
| 218 |
foreach ( $group['settings'] as $setting ) { |
| 219 |
if ( isset( $setting['id'] ) ) { |
| 220 |
$details[ $setting['id'] ] = $setting; |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
if ( isset( $group['subGroups'] ) ) { |
| 226 |
foreach ( $group['subGroups'] as $sub_group ) { |
| 227 |
if ( isset( $sub_group['settings'] ) ) { |
| 228 |
foreach ( $sub_group['settings'] as $setting ) { |
| 229 |
if ( isset( $setting['id'] ) ) { |
| 230 |
$details[ $setting['id'] ] = $setting; |
| 231 |
} |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
return $details; |
| 239 |
} |
| 240 |
} |
| 241 |
|