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.3.4 4.3.5 4.3.6 4.3.7 4.3.8 4.3.9 4.4.0 4.4.1 4.4.10 4.4.11 4.4.12 4.4.2 4.4.3 4.4.4 4.4.5 4.4.6 4.4.7 4.4.8 4.4.9 5.10 5.10.1 5.10.2 5.11 5.11.1 5.11.2 5.11.3 5.11.4 5.12 5.12.1 5.12.2 5.12.3 5.12.4 5.12.5 5.12.6 5.6.10 5.6.2 5.6.3 5.6.4 5.6.5 5.6.6 5.6.7 5.6.8 5.6.9 5.7.0 5.7.1 5.7.10 5.7.12 5.7.13 5.7.2 5.7.3 5.7.4 5.7.5 5.7.6 5.7.7 5.7.8 5.7.9 5.8.0 5.8.1 5.8.10 5.8.11 5.8.12 5.8.13 5.8.14 5.8.2 5.8.3 5.8.4
advanced-custom-fields / includes / acf-value-functions.php
advanced-custom-fields / includes Last commit date
admin 2 years ago ajax 3 years ago api 2 years ago fields 2 years ago forms 2 years ago legacy 4 years ago locations 3 years ago post-types 2 years ago rest-api 3 years ago walkers 2 years ago acf-bidirectional-functions.php 2 years ago acf-field-functions.php 3 years ago acf-field-group-functions.php 3 years ago acf-form-functions.php 3 years ago acf-helper-functions.php 2 years ago acf-hook-functions.php 4 years ago acf-input-functions.php 3 years ago acf-internal-post-type-functions.php 2 years ago acf-meta-functions.php 3 years ago acf-post-functions.php 4 years ago acf-post-type-functions.php 2 years ago acf-taxonomy-functions.php 3 years ago acf-user-functions.php 4 years ago acf-utility-functions.php 3 years ago acf-value-functions.php 2 years ago acf-wp-functions.php 2 years ago assets.php 2 years ago class-acf-data.php 3 years ago class-acf-internal-post-type.php 2 years ago compatibility.php 4 years ago deprecated.php 4 years ago fields.php 2 years ago l10n.php 3 years ago local-fields.php 2 years ago local-json.php 2 years ago local-meta.php 4 years ago locations.php 3 years ago loop.php 3 years ago media.php 3 years ago rest-api.php 4 years ago revisions.php 3 years ago third-party.php 3 years ago upgrades.php 3 years ago validation.php 2 years ago wpml.php 3 years ago
acf-value-functions.php
381 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 int|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 * acf_format_value
134 *
135 * Returns a formatted version of the provided value.
136 *
137 * @date 28/09/13
138 * @since 5.0.0
139 *
140 * @param mixed $value The field value.
141 * @param (int|string) $post_id The post id.
142 * @param array $field The field array.
143 * @return mixed.
144 */
145 function acf_format_value( $value, $post_id, $field ) {
146
147 // Allow filter to short-circuit load_value logic.
148 $check = apply_filters( 'acf/pre_format_value', null, $value, $post_id, $field );
149 if ( $check !== null ) {
150 return $check;
151 }
152
153 // Get field name.
154 $field_name = $field['name'];
155
156 // Check store.
157 $store = acf_get_store( 'values' );
158 if ( $store->has( "$post_id:$field_name:formatted" ) ) {
159 return $store->get( "$post_id:$field_name:formatted" );
160 }
161
162 /**
163 * Filters the $value for use in a template function.
164 *
165 * @date 28/09/13
166 * @since 5.0.0
167 *
168 * @param mixed $value The value to preview.
169 * @param string $post_id The post ID for this value.
170 * @param array $field The field array.
171 */
172 $value = apply_filters( 'acf/format_value', $value, $post_id, $field );
173
174 // Update store.
175 $store->set( "$post_id:$field_name:formatted", $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 bool.
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 }
285
286 /**
287 * acf_delete_value
288 *
289 * Deletes the value for a given field and post_id.
290 *
291 * @date 28/09/13
292 * @since 5.0.0
293 *
294 * @param (int|string) $post_id The post id.
295 * @param array $field The field array.
296 * @return bool.
297 */
298 function acf_delete_value( $post_id, $field ) {
299
300 /**
301 * Fires before a value is deleted.
302 *
303 * @date 28/09/13
304 * @since 5.0.0
305 *
306 * @param string $post_id The post ID for this value.
307 * @param mixed $name The meta name.
308 * @param array $field The field array.
309 */
310 do_action( 'acf/delete_value', $post_id, $field['name'], $field );
311
312 // Delete meta.
313 $return = acf_delete_metadata( $post_id, $field['name'] );
314
315 // Delete reference.
316 acf_delete_metadata( $post_id, $field['name'], true );
317
318 // Delete stored data.
319 acf_flush_value_cache( $post_id, $field['name'] );
320
321 // Return delete status.
322 return $return;
323 }
324
325 // Register variation.
326 acf_add_filter_variations( 'acf/delete_value', array( 'type', 'name', 'key' ), 2 );
327
328 /**
329 * acf_preview_value
330 *
331 * Return a human friendly 'preview' for a given field value.
332 *
333 * @date 28/09/13
334 * @since 5.0.0
335 *
336 * @param mixed $value The new value.
337 * @param (int|string) $post_id The post id.
338 * @param array $field The field array.
339 * @return bool.
340 */
341 function acf_preview_value( $value, $post_id, $field ) {
342
343 /**
344 * Filters the $value before used in HTML.
345 *
346 * @date 24/10/16
347 * @since 5.5.0
348 *
349 * @param mixed $value The value to preview.
350 * @param string $post_id The post ID for this value.
351 * @param array $field The field array.
352 */
353 return apply_filters( 'acf/preview_value', $value, $post_id, $field );
354 }
355
356 // Register variation.
357 acf_add_filter_variations( 'acf/preview_value', array( 'type', 'name', 'key' ), 2 );
358
359 /**
360 * Potentially log an error if a field doesn't exist when we expect it to.
361 *
362 * @param array $field An array representing the field that a value was requested for.
363 * @param string $function The function that noticed the problem.
364 *
365 * @return void
366 */
367 function acf_log_invalid_field_notice( $field, $function ) {
368 // If "init" has fired, ACF probably wasn't initialized early.
369 if ( did_action( 'init' ) ) {
370 return;
371 }
372
373 $error_text = sprintf(
374 __( '<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. <a href="%2$s" target="_blank">Learn how to fix this</a>.', 'acf' ),
375 acf_get_setting( 'name' ),
376 acf_add_url_utm_tags( 'https://www.advancedcustomfields.com/resources/acf-field-functions/', 'docs', 'early_init_warning' )
377 );
378 _doing_it_wrong( $function, $error_text, '5.11.1' );
379 }
380 add_action( 'acf/get_invalid_field_value', 'acf_log_invalid_field_notice', 10, 2 );
381