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