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