PluginProbe
WP Ultimate Post Grid / 4.1.1
WP Ultimate Post Grid v4.1.1
4.1.1 trunk 1.5 1.6 1.7 1.7.1 1.7.2 1.8 1.9 2.0 2.1 2.2 2.3 2.4.0 2.5.0 2.6.0 2.7.0 2.8.0 2.8.2 3.0.0 3.3.0 3.4.0 3.5.0 3.6.0 3.7.0 All 32 releases
wp-ultimate-post-grid / vendor / bv-settings / includes / class-bvs-structure.php

class-bvs-structure.php in WP Ultimate Post Grid 4.1.1, at vendor/bv-settings/includes/class-bvs-structure.php

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