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
class-acf-data.php
1 year ago
class-acf-internal-post-type.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
third-party.php
1 year ago
upgrades.php
1 year ago
validation.php
1 year ago
wpml.php
1 year ago
acf-value-functions.php
382 lines
| 1 | <?php |
| 2 | |
| 3 | // Register store. |
| 4 | acf_register_store( 'values' )->prop( 'multisite', true ); |
| 5 | |
| 6 | /** |
| 7 | * acf_get_reference |
| 8 | * |
| 9 | * Retrieves the field key for a given field name and post_id. |
| 10 | * |
| 11 | * @date 26/1/18 |
| 12 | * @since 5.6.5 |
| 13 | * |
| 14 | * @param string $field_name The name of the field. eg 'sub_heading'. |
| 15 | * @param mixed $post_id The post_id of which the value is saved against. |
| 16 | * @return string The field key. |
| 17 | */ |
| 18 | function acf_get_reference( $field_name, $post_id ) { |
| 19 | |
| 20 | // Allow filter to short-circuit load_value logic. |
| 21 | $reference = apply_filters( 'acf/pre_load_reference', null, $field_name, $post_id ); |
| 22 | if ( $reference !== null ) { |
| 23 | return $reference; |
| 24 | } |
| 25 | |
| 26 | // Get hidden meta for this field name. |
| 27 | $reference = acf_get_metadata( $post_id, $field_name, true ); |
| 28 | |
| 29 | /** |
| 30 | * Filters the reference value. |
| 31 | * |
| 32 | * @date 25/1/19 |
| 33 | * @since 5.7.11 |
| 34 | * |
| 35 | * @param string $reference The reference value. |
| 36 | * @param string $field_name The field name. |
| 37 | * @param (int|string) $post_id The post ID where meta is stored. |
| 38 | */ |
| 39 | return apply_filters( 'acf/load_reference', $reference, $field_name, $post_id ); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Retrieves the value for a given field and post_id. |
| 44 | * |
| 45 | * @date 28/09/13 |
| 46 | * @since 5.0.0 |
| 47 | * |
| 48 | * @param integer|string $post_id The post id. |
| 49 | * @param array $field The field array. |
| 50 | * @return mixed |
| 51 | */ |
| 52 | function acf_get_value( $post_id, $field ) { |
| 53 | |
| 54 | // Allow filter to short-circuit load_value logic. |
| 55 | $value = apply_filters( 'acf/pre_load_value', null, $post_id, $field ); |
| 56 | if ( $value !== null ) { |
| 57 | return $value; |
| 58 | } |
| 59 | |
| 60 | // Get field name. |
| 61 | $field_name = $field['name']; |
| 62 | |
| 63 | // Get field ID & type. |
| 64 | $decoded = acf_decode_post_id( $post_id ); |
| 65 | |
| 66 | $allow_load = true; |
| 67 | |
| 68 | // If we don't have a proper field array, the field doesn't exist currently. |
| 69 | if ( empty( $field['type'] ) && empty( $field['key'] ) ) { |
| 70 | |
| 71 | // Check if we should trigger warning about accessing fields too early via action. |
| 72 | do_action( 'acf/get_invalid_field_value', $field, __FUNCTION__ ); |
| 73 | |
| 74 | if ( apply_filters( 'acf/prevent_access_to_unknown_fields', false ) || ( 'option' === $decoded['type'] && 'options' !== $decoded['id'] ) ) { |
| 75 | $allow_load = false; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | // If we're using a non options_ option key, ensure we have a valid reference key. |
| 80 | if ( 'option' === $decoded['type'] && 'options' !== $decoded['id'] ) { |
| 81 | $meta = acf_get_metadata( $post_id, $field_name, true ); |
| 82 | if ( ! $meta ) { |
| 83 | $allow_load = false; |
| 84 | } elseif ( $meta !== $field['key'] ) { |
| 85 | if ( ! isset( $field['__key'] ) || $meta !== $field['__key'] ) { |
| 86 | $allow_load = false; |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | // Load Store. |
| 92 | $store = acf_get_store( 'values' ); |
| 93 | |
| 94 | // If we're allowing load, check the store or load value from database. |
| 95 | if ( $allow_load ) { |
| 96 | if ( $store->has( "$post_id:$field_name" ) ) { |
| 97 | return $store->get( "$post_id:$field_name" ); |
| 98 | } |
| 99 | |
| 100 | $value = acf_get_metadata( $post_id, $field_name ); |
| 101 | } |
| 102 | |
| 103 | // Use field's default_value if no meta was found. |
| 104 | if ( $value === null && isset( $field['default_value'] ) ) { |
| 105 | $value = $field['default_value']; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Filters the $value after it has been loaded. |
| 110 | * |
| 111 | * @date 28/09/13 |
| 112 | * @since 5.0.0 |
| 113 | * |
| 114 | * @param mixed $value The value to preview. |
| 115 | * @param string $post_id The post ID for this value. |
| 116 | * @param array $field The field array. |
| 117 | */ |
| 118 | $value = apply_filters( 'acf/load_value', $value, $post_id, $field ); |
| 119 | |
| 120 | // Update store if we allowed the value load. |
| 121 | if ( $allow_load ) { |
| 122 | $store->set( "$post_id:$field_name", $value ); |
| 123 | } |
| 124 | |
| 125 | // Return value. |
| 126 | return $value; |
| 127 | } |
| 128 | |
| 129 | // Register variation. |
| 130 | acf_add_filter_variations( 'acf/load_value', array( 'type', 'name', 'key' ), 2 ); |
| 131 | |
| 132 | /** |
| 133 | * Returns a formatted version of the provided value. |
| 134 | * |
| 135 | * @since 5.0.0 |
| 136 | * |
| 137 | * @param mixed $value The field value. |
| 138 | * @param integer|string $post_id The post id. |
| 139 | * @param array $field The field array. |
| 140 | * @param boolean $escape_html Ask the field for a HTML safe version of it's output. |
| 141 | * |
| 142 | * @return mixed |
| 143 | */ |
| 144 | function acf_format_value( $value, $post_id, $field, $escape_html = false ) { |
| 145 | |
| 146 | // Allow filter to short-circuit load_value logic. |
| 147 | $check = apply_filters( 'acf/pre_format_value', null, $value, $post_id, $field, $escape_html ); |
| 148 | if ( $check !== null ) { |
| 149 | return $check; |
| 150 | } |
| 151 | |
| 152 | // Get field name. |
| 153 | $field_name = $field['name']; |
| 154 | $cache_name = $escape_html ? "$post_id:$field_name:escaped" : "$post_id:$field_name:formatted"; |
| 155 | |
| 156 | // Check store. |
| 157 | $store = acf_get_store( 'values' ); |
| 158 | if ( $store->has( $cache_name ) ) { |
| 159 | return $store->get( $cache_name ); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Filters the $value for use in a template function. |
| 164 | * |
| 165 | * @since 5.0.0 |
| 166 | * |
| 167 | * @param mixed $value The value to preview. |
| 168 | * @param string $post_id The post ID for this value. |
| 169 | * @param array $field The field array. |
| 170 | * @param boolean $escape_html Ask the field for a HTML safe version of it's output. |
| 171 | */ |
| 172 | $value = apply_filters( 'acf/format_value', $value, $post_id, $field, $escape_html ); |
| 173 | |
| 174 | // Update store. |
| 175 | $store->set( $cache_name, $value ); |
| 176 | |
| 177 | // Return value. |
| 178 | return $value; |
| 179 | } |
| 180 | |
| 181 | // Register variation. |
| 182 | acf_add_filter_variations( 'acf/format_value', array( 'type', 'name', 'key' ), 2 ); |
| 183 | |
| 184 | /** |
| 185 | * acf_update_value |
| 186 | * |
| 187 | * Updates the value for a given field and post_id. |
| 188 | * |
| 189 | * @date 28/09/13 |
| 190 | * @since 5.0.0 |
| 191 | * |
| 192 | * @param mixed $value The new value. |
| 193 | * @param (int|string) $post_id The post id. |
| 194 | * @param array $field The field array. |
| 195 | * @return boolean |
| 196 | */ |
| 197 | function acf_update_value( $value, $post_id, $field ) { |
| 198 | |
| 199 | // Allow filter to short-circuit update_value logic. |
| 200 | $check = apply_filters( 'acf/pre_update_value', null, $value, $post_id, $field ); |
| 201 | if ( $check !== null ) { |
| 202 | return $check; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Filters the $value before it is updated. |
| 207 | * |
| 208 | * @date 28/09/13 |
| 209 | * @since 5.0.0 |
| 210 | * |
| 211 | * @param mixed $value The value to update. |
| 212 | * @param string $post_id The post ID for this value. |
| 213 | * @param array $field The field array. |
| 214 | * @param mixed $original The original value before modification. |
| 215 | */ |
| 216 | $value = apply_filters( 'acf/update_value', $value, $post_id, $field, $value ); |
| 217 | |
| 218 | // Allow null to delete value. |
| 219 | if ( $value === null ) { |
| 220 | return acf_delete_value( $post_id, $field ); |
| 221 | } |
| 222 | |
| 223 | // Update meta. |
| 224 | $return = acf_update_metadata( $post_id, $field['name'], $value ); |
| 225 | |
| 226 | // Update reference. |
| 227 | acf_update_metadata( $post_id, $field['name'], $field['key'], true ); |
| 228 | |
| 229 | // Delete stored data. |
| 230 | acf_flush_value_cache( $post_id, $field['name'] ); |
| 231 | |
| 232 | // Return update status. |
| 233 | return $return; |
| 234 | } |
| 235 | |
| 236 | // Register variation. |
| 237 | acf_add_filter_variations( 'acf/update_value', array( 'type', 'name', 'key' ), 2 ); |
| 238 | |
| 239 | /** |
| 240 | * acf_update_values |
| 241 | * |
| 242 | * Updates an array of values. |
| 243 | * |
| 244 | * @date 26/2/19 |
| 245 | * @since 5.7.13 |
| 246 | * |
| 247 | * @param array values The array of values. |
| 248 | * @param (int|string) $post_id The post id. |
| 249 | * @return void |
| 250 | */ |
| 251 | function acf_update_values( $values, $post_id ) { |
| 252 | |
| 253 | // Loop over values. |
| 254 | foreach ( $values as $key => $value ) { |
| 255 | |
| 256 | // Get field. |
| 257 | $field = acf_get_field( $key ); |
| 258 | |
| 259 | // Update value. |
| 260 | if ( $field ) { |
| 261 | acf_update_value( $value, $post_id, $field ); |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * acf_flush_value_cache |
| 268 | * |
| 269 | * Deletes all cached data for this value. |
| 270 | * |
| 271 | * @date 22/1/19 |
| 272 | * @since 5.7.10 |
| 273 | * |
| 274 | * @param (int|string) $post_id The post id. |
| 275 | * @param string $field_name The field name. |
| 276 | * @return void |
| 277 | */ |
| 278 | function acf_flush_value_cache( $post_id = 0, $field_name = '' ) { |
| 279 | |
| 280 | // Delete stored data. |
| 281 | acf_get_store( 'values' ) |
| 282 | ->remove( "$post_id:$field_name" ) |
| 283 | ->remove( "$post_id:$field_name:formatted" ) |
| 284 | ->remove( "$post_id:$field_name:escaped" ); |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * acf_delete_value |
| 289 | * |
| 290 | * Deletes the value for a given field and post_id. |
| 291 | * |
| 292 | * @date 28/09/13 |
| 293 | * @since 5.0.0 |
| 294 | * |
| 295 | * @param (int|string) $post_id The post id. |
| 296 | * @param array $field The field array. |
| 297 | * @return boolean |
| 298 | */ |
| 299 | function acf_delete_value( $post_id, $field ) { |
| 300 | |
| 301 | /** |
| 302 | * Fires before a value is deleted. |
| 303 | * |
| 304 | * @date 28/09/13 |
| 305 | * @since 5.0.0 |
| 306 | * |
| 307 | * @param string $post_id The post ID for this value. |
| 308 | * @param mixed $name The meta name. |
| 309 | * @param array $field The field array. |
| 310 | */ |
| 311 | do_action( 'acf/delete_value', $post_id, $field['name'], $field ); |
| 312 | |
| 313 | // Delete meta. |
| 314 | $return = acf_delete_metadata( $post_id, $field['name'] ); |
| 315 | |
| 316 | // Delete reference. |
| 317 | acf_delete_metadata( $post_id, $field['name'], true ); |
| 318 | |
| 319 | // Delete stored data. |
| 320 | acf_flush_value_cache( $post_id, $field['name'] ); |
| 321 | |
| 322 | // Return delete status. |
| 323 | return $return; |
| 324 | } |
| 325 | |
| 326 | // Register variation. |
| 327 | acf_add_filter_variations( 'acf/delete_value', array( 'type', 'name', 'key' ), 2 ); |
| 328 | |
| 329 | /** |
| 330 | * acf_preview_value |
| 331 | * |
| 332 | * Return a human friendly 'preview' for a given field value. |
| 333 | * |
| 334 | * @date 28/09/13 |
| 335 | * @since 5.0.0 |
| 336 | * |
| 337 | * @param mixed $value The new value. |
| 338 | * @param (int|string) $post_id The post id. |
| 339 | * @param array $field The field array. |
| 340 | * @return boolean |
| 341 | */ |
| 342 | function acf_preview_value( $value, $post_id, $field ) { |
| 343 | |
| 344 | /** |
| 345 | * Filters the $value before used in HTML. |
| 346 | * |
| 347 | * @date 24/10/16 |
| 348 | * @since 5.5.0 |
| 349 | * |
| 350 | * @param mixed $value The value to preview. |
| 351 | * @param string $post_id The post ID for this value. |
| 352 | * @param array $field The field array. |
| 353 | */ |
| 354 | return apply_filters( 'acf/preview_value', $value, $post_id, $field ); |
| 355 | } |
| 356 | |
| 357 | // Register variation. |
| 358 | acf_add_filter_variations( 'acf/preview_value', array( 'type', 'name', 'key' ), 2 ); |
| 359 | |
| 360 | /** |
| 361 | * Potentially log an error if a field doesn't exist when we expect it to. |
| 362 | * |
| 363 | * @param array $field An array representing the field that a value was requested for. |
| 364 | * @param string $function The function that noticed the problem. |
| 365 | * |
| 366 | * @return void |
| 367 | */ |
| 368 | function acf_log_invalid_field_notice( $field, $function ) { |
| 369 | // If "init" has fired, ACF probably wasn't initialized early. |
| 370 | if ( did_action( 'init' ) ) { |
| 371 | return; |
| 372 | } |
| 373 | |
| 374 | $error_text = sprintf( |
| 375 | /* translators: %1 plugin name */ |
| 376 | __( '<strong>%1$s</strong> - We\'ve detected one or more calls to retrieve ACF field values before ACF has been initialized. This is not supported and can result in malformed or missing data.', 'acf' ), |
| 377 | acf_get_setting( 'name' ), |
| 378 | ); |
| 379 | _doing_it_wrong( esc_html( $function ), acf_esc_html( $error_text ), '5.11.1' ); |
| 380 | } |
| 381 | add_action( 'acf/get_invalid_field_value', 'acf_log_invalid_field_notice', 10, 2 ); |
| 382 |