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