PluginProbe
Advanced Custom Fields (ACF®) / 5.6.6
Advanced Custom Fields (ACF®) v5.6.6
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 in Advanced Custom Fields (ACF®) 5.6.6, at includes/loop.php

351 lines 4.8 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 }
262
263 }
264
265 // initialize
266 acf()->loop = new acf_loop();
267
268 endif; // class_exists check
269
270
271
272 /*
273 * acf_add_loop
274 *
275 * alias of acf()->loop->add_loop()
276 *
277 * @type function
278 * @date 6/10/13
279 * @since 5.0.0
280 *
281 * @param n/a
282 * @return n/a
283 */
284
285 function acf_add_loop( $loop = array() ) {
286
287 return acf()->loop->add_loop( $loop );
288
289 }
290
291
292 /*
293 * acf_update_loop
294 *
295 * alias of acf()->loop->update_loop()
296 *
297 * @type function
298 * @date 6/10/13
299 * @since 5.0.0
300 *
301 * @param n/a
302 * @return n/a
303 */
304
305 function acf_update_loop( $i = 'active', $key = null, $value = null ) {
306
307 return acf()->loop->update_loop( $i, $key, $value );
308
309 }
310
311
312 /*
313 * acf_get_loop
314 *
315 * alias of acf()->loop->get_loop()
316 *
317 * @type function
318 * @date 6/10/13
319 * @since 5.0.0
320 *
321 * @param n/a
322 * @return n/a
323 */
324
325 function acf_get_loop( $i = 'active', $key = null ) {
326
327 return acf()->loop->get_loop( $i, $key );
328
329 }
330
331
332 /*
333 * acf_remove_loop
334 *
335 * alias of acf()->loop->remove_loop()
336 *
337 * @type function
338 * @date 6/10/13
339 * @since 5.0.0
340 *
341 * @param n/a
342 * @return n/a
343 */
344
345 function acf_remove_loop( $i = 'active' ) {
346
347 return acf()->loop->remove_loop( $i );
348
349 }
350
351 ?>