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