PluginProbe
Advanced Custom Fields (ACF®) / 5.8.9
Advanced Custom Fields (ACF®) v5.8.9
6.8.9 6.8.8 6.8.7 6.8.6 6.8.5 6.8.4 6.8.3 6.8.2 6.8.1 5.8.5 5.8.6 5.8.7 5.8.8 5.8.9 5.9.0 5.9.1 5.9.2 5.9.3 5.9.4 5.9.5 5.9.6 5.9.7 5.9.8 5.9.9 6.0.0 All 230 releases
advanced-custom-fields / includes / loop.php
loop.php
355 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
4
5 if( ! class_exists('acf_loop') ) :
6
7 class acf_loop {
8
9
10 /*
11 * __construct
12 *
13 * This function will setup the class functionality
14 *
15 * @type function
16 * @date 5/03/2014
17 * @since 5.0.0
18 *
19 * @param n/a
20 * @return n/a
21 */
22
23 function __construct() {
24
25 // vars
26 $this->loops = array();
27
28 }
29
30
31 /*
32 * is_empty
33 *
34 * This function will return true if no loops exist
35 *
36 * @type function
37 * @date 3/03/2016
38 * @since 5.3.2
39 *
40 * @param n/a
41 * @return (boolean)
42 */
43
44 function is_empty() {
45
46 return empty( $this->loops );
47
48 }
49
50
51 /*
52 * is_loop
53 *
54 * This function will return true if a loop exists for the given array index
55 *
56 * @type function
57 * @date 3/03/2016
58 * @since 5.3.2
59 *
60 * @param $i (int)
61 * @return (boolean)
62 */
63
64 function is_loop( $i = 0 ) {
65
66 return isset( $this->loops[ $i ] );
67
68 }
69
70
71 /*
72 * get_i
73 *
74 * This function will return a valid array index for the given $i
75 *
76 * @type function
77 * @date 3/03/2016
78 * @since 5.3.2
79 *
80 * @param $i (mixed)
81 * @return (int)
82 */
83
84 function get_i( $i = 0 ) {
85
86 // 'active'
87 if( $i === 'active' ) $i = -1;
88
89
90 // 'previous'
91 if( $i === 'previous' ) $i = -2;
92
93
94 // allow negative to look at end of loops
95 if( $i < 0 ) {
96
97 $i = count($this->loops) + $i;
98
99 }
100
101
102 // return
103 return $i;
104
105 }
106
107
108 /*
109 * add_loop
110 *
111 * This function will add a new loop
112 *
113 * @type function
114 * @date 3/03/2016
115 * @since 5.3.2
116 *
117 * @param $loop (array)
118 * @return n/a
119 */
120
121 function add_loop( $loop = array() ) {
122
123 // defaults
124 $loop = wp_parse_args( $loop, array(
125 'selector' => '',
126 'name' => '',
127 'value' => false,
128 'field' => false,
129 'i' => -1,
130 'post_id' => 0,
131 'key' => ''
132 ));
133
134
135 // ensure array
136 $loop['value'] = acf_get_array( $loop['value'] );
137
138
139 // Re-index values if this loop starts from index 0.
140 // This allows ajax previews to work ($_POST data contains random unique array keys)
141 if( $loop['i'] == -1 ) {
142
143 $loop['value'] = array_values($loop['value']);
144
145 }
146
147
148 // append
149 $this->loops[] = $loop;
150
151
152 // return
153 return $loop;
154
155 }
156
157
158 /*
159 * update_loop
160 *
161 * This function will update a loop's setting
162 *
163 * @type function
164 * @date 3/03/2016
165 * @since 5.3.2
166 *
167 * @param $i (mixed)
168 * @param $key (string) the loop setting name
169 * @param $value (mixed) the loop setting value
170 * @return (boolean) true on success
171 */
172
173 function update_loop( $i = 'active', $key = null, $value = null ) {
174
175 // i
176 $i = $this->get_i( $i );
177
178
179 // bail early if no set
180 if( !$this->is_loop($i) ) return false;
181
182
183 // set
184 $this->loops[ $i ][ $key ] = $value;
185
186
187 // return
188 return true;
189
190 }
191
192
193 /*
194 * get_loop
195 *
196 * This function will return a loop, or loop's setting for a given index & key
197 *
198 * @type function
199 * @date 3/03/2016
200 * @since 5.3.2
201 *
202 * @param $i (mixed)
203 * @param $key (string) the loop setting name
204 * @return (mixed) false on failure
205 */
206
207 function get_loop( $i = 'active', $key = null ) {
208
209 // i
210 $i = $this->get_i( $i );
211
212
213 // bail early if no set
214 if( !$this->is_loop($i) ) return false;
215
216
217 // check for key
218 if( $key !== null ) {
219
220 return $this->loops[ $i ][ $key ];
221
222 }
223
224
225 // return
226 return $this->loops[ $i ];
227
228 }
229
230
231 /*
232 * remove_loop
233 *
234 * This function will remove a loop
235 *
236 * @type function
237 * @date 3/03/2016
238 * @since 5.3.2
239 *
240 * @param $i (mixed)
241 * @return (boolean) true on success
242 */
243
244 function remove_loop( $i = 'active' ) {
245
246 // i
247 $i = $this->get_i( $i );
248
249
250 // bail early if no set
251 if( !$this->is_loop($i) ) return false;
252
253
254 // remove
255 unset($this->loops[ $i ]);
256
257
258 // reset keys
259 $this->loops = array_values( $this->loops );
260
261 // PHP 7.2 no longer resets array keys for empty value
262 if( $this->is_empty() ) {
263 $this->loops = array();
264 }
265 }
266
267 }
268
269 // initialize
270 acf()->loop = new acf_loop();
271
272 endif; // class_exists check
273
274
275
276 /*
277 * acf_add_loop
278 *
279 * alias of acf()->loop->add_loop()
280 *
281 * @type function
282 * @date 6/10/13
283 * @since 5.0.0
284 *
285 * @param n/a
286 * @return n/a
287 */
288
289 function acf_add_loop( $loop = array() ) {
290
291 return acf()->loop->add_loop( $loop );
292
293 }
294
295
296 /*
297 * acf_update_loop
298 *
299 * alias of acf()->loop->update_loop()
300 *
301 * @type function
302 * @date 6/10/13
303 * @since 5.0.0
304 *
305 * @param n/a
306 * @return n/a
307 */
308
309 function acf_update_loop( $i = 'active', $key = null, $value = null ) {
310
311 return acf()->loop->update_loop( $i, $key, $value );
312
313 }
314
315
316 /*
317 * acf_get_loop
318 *
319 * alias of acf()->loop->get_loop()
320 *
321 * @type function
322 * @date 6/10/13
323 * @since 5.0.0
324 *
325 * @param n/a
326 * @return n/a
327 */
328
329 function acf_get_loop( $i = 'active', $key = null ) {
330
331 return acf()->loop->get_loop( $i, $key );
332
333 }
334
335
336 /*
337 * acf_remove_loop
338 *
339 * alias of acf()->loop->remove_loop()
340 *
341 * @type function
342 * @date 6/10/13
343 * @since 5.0.0
344 *
345 * @param n/a
346 * @return n/a
347 */
348
349 function acf_remove_loop( $i = 'active' ) {
350
351 return acf()->loop->remove_loop( $i );
352
353 }
354
355 ?>