PluginProbe
Advanced Custom Fields (ACF®) / 6.1.0
Advanced Custom Fields (ACF®) v6.1.0
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
356 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 #[AllowDynamicProperties]
10 class acf_loop {
11
12
13 /*
14 * __construct
15 *
16 * This function will setup the class functionality
17 *
18 * @type function
19 * @date 5/03/2014
20 * @since 5.0.0
21 *
22 * @param n/a
23 * @return n/a
24 */
25
26 function __construct() {
27
28 // vars
29 $this->loops = array();
30
31 }
32
33
34 /*
35 * is_empty
36 *
37 * This function will return true if no loops exist
38 *
39 * @type function
40 * @date 3/03/2016
41 * @since 5.3.2
42 *
43 * @param n/a
44 * @return (boolean)
45 */
46
47 function is_empty() {
48
49 return empty( $this->loops );
50
51 }
52
53
54 /*
55 * is_loop
56 *
57 * This function will return true if a loop exists for the given array index
58 *
59 * @type function
60 * @date 3/03/2016
61 * @since 5.3.2
62 *
63 * @param $i (int)
64 * @return (boolean)
65 */
66
67 function is_loop( $i = 0 ) {
68
69 return isset( $this->loops[ $i ] );
70
71 }
72
73
74 /*
75 * get_i
76 *
77 * This function will return a valid array index for the given $i
78 *
79 * @type function
80 * @date 3/03/2016
81 * @since 5.3.2
82 *
83 * @param $i (mixed)
84 * @return (int)
85 */
86
87 function get_i( $i = 0 ) {
88
89 // 'active'
90 if ( $i === 'active' ) {
91 $i = -1;
92 }
93
94 // 'previous'
95 if ( $i === 'previous' ) {
96 $i = -2;
97 }
98
99 // allow negative to look at end of loops
100 if ( $i < 0 ) {
101
102 $i = count( $this->loops ) + $i;
103
104 }
105
106 // return
107 return $i;
108
109 }
110
111
112 /*
113 * add_loop
114 *
115 * This function will add a new loop
116 *
117 * @type function
118 * @date 3/03/2016
119 * @since 5.3.2
120 *
121 * @param $loop (array)
122 * @return n/a
123 */
124
125 function add_loop( $loop = array() ) {
126
127 // defaults
128 $loop = wp_parse_args(
129 $loop,
130 array(
131 'selector' => '',
132 'name' => '',
133 'value' => false,
134 'field' => false,
135 'i' => -1,
136 'post_id' => 0,
137 'key' => '',
138 )
139 );
140
141 // ensure array
142 $loop['value'] = acf_get_array( $loop['value'] );
143
144 // Re-index values if this loop starts from index 0.
145 // This allows ajax previews to work ($_POST data contains random unique array keys)
146 if ( $loop['i'] == -1 ) {
147
148 $loop['value'] = array_values( $loop['value'] );
149
150 }
151
152 // append
153 $this->loops[] = $loop;
154
155 // return
156 return $loop;
157
158 }
159
160
161 /*
162 * update_loop
163 *
164 * This function will update a loop's setting
165 *
166 * @type function
167 * @date 3/03/2016
168 * @since 5.3.2
169 *
170 * @param $i (mixed)
171 * @param $key (string) the loop setting name
172 * @param $value (mixed) the loop setting value
173 * @return (boolean) true on success
174 */
175
176 function update_loop( $i = 'active', $key = null, $value = null ) {
177
178 // i
179 $i = $this->get_i( $i );
180
181 // bail early if no set
182 if ( ! $this->is_loop( $i ) ) {
183 return false;
184 }
185
186 // set
187 $this->loops[ $i ][ $key ] = $value;
188
189 // return
190 return true;
191
192 }
193
194
195 /*
196 * get_loop
197 *
198 * This function will return a loop, or loop's setting for a given index & key
199 *
200 * @type function
201 * @date 3/03/2016
202 * @since 5.3.2
203 *
204 * @param $i (mixed)
205 * @param $key (string) the loop setting name
206 * @return (mixed) false on failure
207 */
208
209 function get_loop( $i = 'active', $key = null ) {
210
211 // i
212 $i = $this->get_i( $i );
213
214 // bail early if no set
215 if ( ! $this->is_loop( $i ) ) {
216 return false;
217 }
218
219 // check for key
220 if ( $key !== null ) {
221
222 return $this->loops[ $i ][ $key ];
223
224 }
225
226 // return
227 return $this->loops[ $i ];
228
229 }
230
231
232 /*
233 * remove_loop
234 *
235 * This function will remove a loop
236 *
237 * @type function
238 * @date 3/03/2016
239 * @since 5.3.2
240 *
241 * @param $i (mixed)
242 * @return (boolean) true on success
243 */
244
245 function remove_loop( $i = 'active' ) {
246
247 // i
248 $i = $this->get_i( $i );
249
250 // bail early if no set
251 if ( ! $this->is_loop( $i ) ) {
252 return false;
253 }
254
255 // remove
256 unset( $this->loops[ $i ] );
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
356