Blocks
1 year ago
admin
1 year 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
1 year 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
class-acf-data.php
352 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; // Exit if accessed directly. |
| 5 | } |
| 6 | |
| 7 | if ( ! class_exists( 'ACF_Data' ) ) : |
| 8 | #[AllowDynamicProperties] |
| 9 | class ACF_Data { |
| 10 | |
| 11 | /** @var string Unique identifier. */ |
| 12 | var $cid = ''; |
| 13 | |
| 14 | /** @var array Storage for data. */ |
| 15 | var $data = array(); |
| 16 | |
| 17 | /** @var array Storage for data aliases. */ |
| 18 | var $aliases = array(); |
| 19 | |
| 20 | /** @var boolean Enables unique data per site. */ |
| 21 | var $multisite = false; |
| 22 | |
| 23 | /** |
| 24 | * __construct |
| 25 | * |
| 26 | * Sets up the class functionality. |
| 27 | * |
| 28 | * @date 9/1/19 |
| 29 | * @since ACF 5.7.10 |
| 30 | * |
| 31 | * @param array $data Optional data to set. |
| 32 | * @return void |
| 33 | */ |
| 34 | function __construct( $data = false ) { |
| 35 | |
| 36 | // Set cid. |
| 37 | $this->cid = acf_uniqid(); |
| 38 | |
| 39 | // Set data. |
| 40 | if ( $data ) { |
| 41 | $this->set( $data ); |
| 42 | } |
| 43 | |
| 44 | // Initialize. |
| 45 | $this->initialize(); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * initialize |
| 50 | * |
| 51 | * Called during constructor to setup class functionality. |
| 52 | * |
| 53 | * @date 9/1/19 |
| 54 | * @since ACF 5.7.10 |
| 55 | * |
| 56 | * @return void |
| 57 | */ |
| 58 | function initialize() { |
| 59 | // Do nothing. |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * prop |
| 64 | * |
| 65 | * Sets a property for the given name and returns $this for chaining. |
| 66 | * |
| 67 | * @date 9/1/19 |
| 68 | * @since ACF 5.7.10 |
| 69 | * |
| 70 | * @param (string|array) $name The data name or an array of data. |
| 71 | * @param mixed $value The data value. |
| 72 | * @return ACF_Data |
| 73 | */ |
| 74 | function prop( $name = '', $value = null ) { |
| 75 | |
| 76 | // Update property. |
| 77 | $this->{$name} = $value; |
| 78 | |
| 79 | // Return this for chaining. |
| 80 | return $this; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * _key |
| 85 | * |
| 86 | * Returns a key for the given name allowing aliasses to work. |
| 87 | * |
| 88 | * @date 18/1/19 |
| 89 | * @since ACF 5.7.10 |
| 90 | * |
| 91 | * @param type $var Description. Default. |
| 92 | * @return type Description. |
| 93 | */ |
| 94 | function _key( $name = '' ) { |
| 95 | return isset( $this->aliases[ $name ] ) ? $this->aliases[ $name ] : $name; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * has |
| 100 | * |
| 101 | * Returns true if this has data for the given name. |
| 102 | * |
| 103 | * @date 9/1/19 |
| 104 | * @since ACF 5.7.10 |
| 105 | * |
| 106 | * @param string $name The data name. |
| 107 | * @return boolean |
| 108 | */ |
| 109 | function has( $name = '' ) { |
| 110 | $key = $this->_key( $name ); |
| 111 | return isset( $this->data[ $key ] ); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * is |
| 116 | * |
| 117 | * Similar to has() but does not check aliases. |
| 118 | * |
| 119 | * @date 7/2/19 |
| 120 | * @since ACF 5.7.11 |
| 121 | * |
| 122 | * @param type $var Description. Default. |
| 123 | * @return type Description. |
| 124 | */ |
| 125 | function is( $key = '' ) { |
| 126 | return isset( $this->data[ $key ] ); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * get |
| 131 | * |
| 132 | * Returns data for the given name of null if doesn't exist. |
| 133 | * |
| 134 | * @date 9/1/19 |
| 135 | * @since ACF 5.7.10 |
| 136 | * |
| 137 | * @param string $name The data name. |
| 138 | * @return mixed |
| 139 | */ |
| 140 | function get( $name = false ) { |
| 141 | |
| 142 | // Get all. |
| 143 | if ( $name === false ) { |
| 144 | return $this->data; |
| 145 | |
| 146 | // Get specific. |
| 147 | } else { |
| 148 | $key = $this->_key( $name ); |
| 149 | return isset( $this->data[ $key ] ) ? $this->data[ $key ] : null; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * get_data |
| 155 | * |
| 156 | * Returns an array of all data. |
| 157 | * |
| 158 | * @date 9/1/19 |
| 159 | * @since ACF 5.7.10 |
| 160 | * |
| 161 | * @return array |
| 162 | */ |
| 163 | function get_data() { |
| 164 | return $this->data; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * set |
| 169 | * |
| 170 | * Sets data for the given name and returns $this for chaining. |
| 171 | * |
| 172 | * @date 9/1/19 |
| 173 | * @since ACF 5.7.10 |
| 174 | * |
| 175 | * @param (string|array) $name The data name or an array of data. |
| 176 | * @param mixed $value The data value. |
| 177 | * @return ACF_Data |
| 178 | */ |
| 179 | function set( $name = '', $value = null ) { |
| 180 | |
| 181 | // Set multiple. |
| 182 | if ( is_array( $name ) ) { |
| 183 | $this->data = array_merge( $this->data, $name ); |
| 184 | |
| 185 | // Set single. |
| 186 | } else { |
| 187 | $this->data[ $name ] = $value; |
| 188 | } |
| 189 | |
| 190 | // Return this for chaining. |
| 191 | return $this; |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * append |
| 196 | * |
| 197 | * Appends data for the given name and returns $this for chaining. |
| 198 | * |
| 199 | * @date 9/1/19 |
| 200 | * @since ACF 5.7.10 |
| 201 | * |
| 202 | * @param mixed $value The data value. |
| 203 | * @return ACF_Data |
| 204 | */ |
| 205 | function append( $value = null ) { |
| 206 | |
| 207 | // Append. |
| 208 | $this->data[] = $value; |
| 209 | |
| 210 | // Return this for chaining. |
| 211 | return $this; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * remove |
| 216 | * |
| 217 | * Removes data for the given name. |
| 218 | * |
| 219 | * @date 9/1/19 |
| 220 | * @since ACF 5.7.10 |
| 221 | * |
| 222 | * @param string $name The data name. |
| 223 | * @return ACF_Data |
| 224 | */ |
| 225 | function remove( $name = '' ) { |
| 226 | |
| 227 | // Remove data. |
| 228 | unset( $this->data[ $name ] ); |
| 229 | |
| 230 | // Return this for chaining. |
| 231 | return $this; |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * reset |
| 236 | * |
| 237 | * Resets the data. |
| 238 | * |
| 239 | * @date 22/1/19 |
| 240 | * @since ACF 5.7.10 |
| 241 | * |
| 242 | * @return void |
| 243 | */ |
| 244 | function reset() { |
| 245 | $this->data = array(); |
| 246 | $this->aliases = array(); |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * count |
| 251 | * |
| 252 | * Returns the data count. |
| 253 | * |
| 254 | * @date 23/1/19 |
| 255 | * @since ACF 5.7.10 |
| 256 | * |
| 257 | * @return integer |
| 258 | */ |
| 259 | function count() { |
| 260 | return count( $this->data ); |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * query |
| 265 | * |
| 266 | * Returns a filtered array of data based on the set of key => value arguments. |
| 267 | * |
| 268 | * @date 23/1/19 |
| 269 | * @since ACF 5.7.10 |
| 270 | * |
| 271 | * @return integer |
| 272 | */ |
| 273 | function query( $args, $operator = 'AND' ) { |
| 274 | return wp_list_filter( $this->data, $args, $operator ); |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * alias |
| 279 | * |
| 280 | * Sets an alias for the given name allowing data to be found via multiple identifiers. |
| 281 | * |
| 282 | * @date 18/1/19 |
| 283 | * @since ACF 5.7.10 |
| 284 | * |
| 285 | * @param type $var Description. Default. |
| 286 | * @return type Description. |
| 287 | */ |
| 288 | function alias( $name = '' /*, $alias, $alias2, etc */ ) { |
| 289 | |
| 290 | // Get all aliases. |
| 291 | $args = func_get_args(); |
| 292 | array_shift( $args ); |
| 293 | |
| 294 | // Loop over aliases and add to data. |
| 295 | foreach ( $args as $alias ) { |
| 296 | $this->aliases[ $alias ] = $name; |
| 297 | } |
| 298 | |
| 299 | // Return this for chaining. |
| 300 | return $this; |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * switch_site |
| 305 | * |
| 306 | * Triggered when switching between sites on a multisite installation. |
| 307 | * |
| 308 | * @date 13/2/19 |
| 309 | * @since ACF 5.7.11 |
| 310 | * |
| 311 | * @param integer $site_id New blog ID. |
| 312 | * @param int prev_blog_id Prev blog ID. |
| 313 | * @return void |
| 314 | */ |
| 315 | function switch_site( $site_id, $prev_site_id ) { |
| 316 | |
| 317 | // Bail early if not multisite compatible. |
| 318 | if ( ! $this->multisite ) { |
| 319 | return; |
| 320 | } |
| 321 | |
| 322 | // Bail early if no change in blog ID. |
| 323 | if ( $site_id === $prev_site_id ) { |
| 324 | return; |
| 325 | } |
| 326 | |
| 327 | // Create storage. |
| 328 | if ( ! isset( $this->site_data ) ) { |
| 329 | $this->site_data = array(); |
| 330 | $this->site_aliases = array(); |
| 331 | } |
| 332 | |
| 333 | // Save state. |
| 334 | $this->site_data[ $prev_site_id ] = $this->data; |
| 335 | $this->site_aliases[ $prev_site_id ] = $this->aliases; |
| 336 | |
| 337 | // Reset state. |
| 338 | $this->data = array(); |
| 339 | $this->aliases = array(); |
| 340 | |
| 341 | // Load state. |
| 342 | if ( isset( $this->site_data[ $site_id ] ) ) { |
| 343 | $this->data = $this->site_data[ $site_id ]; |
| 344 | $this->aliases = $this->site_aliases[ $site_id ]; |
| 345 | unset( $this->site_data[ $site_id ] ); |
| 346 | unset( $this->site_aliases[ $site_id ] ); |
| 347 | } |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | endif; // class_exists check |
| 352 |