PluginProbe
Advanced Custom Fields (ACF®) / 5.9.8
Advanced Custom Fields (ACF®) v5.9.8
6.8.9 6.8.8 6.8.7 6.8.6 6.8.5 6.8.4 6.8.3 6.8.2 6.8.1 5.8.5 5.8.6 5.8.7 5.8.8 5.8.9 5.9.0 5.9.1 5.9.2 5.9.3 5.9.4 5.9.5 5.9.6 5.9.7 5.9.8 5.9.9 6.0.0 All 230 releases
advanced-custom-fields / includes / acf-value-functions.php
acf-value-functions.php
325 lines 7.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 // Check store.
64 $store = acf_get_store( 'values' );
65 if( $store->has( "$post_id:$field_name" ) ) {
66 return $store->get( "$post_id:$field_name" );
67 }
68
69 // Load value from database.
70 $value = acf_get_metadata( $post_id, $field_name );
71
72 // Use field's default_value if no meta was found.
73 if( $value === null && isset($field['default_value']) ) {
74 $value = $field['default_value'];
75 }
76
77 /**
78 * Filters the $value after it has been loaded.
79 *
80 * @date 28/09/13
81 * @since 5.0.0
82 *
83 * @param mixed $value The value to preview.
84 * @param string $post_id The post ID for this value.
85 * @param array $field The field array.
86 */
87 $value = apply_filters( "acf/load_value", $value, $post_id, $field );
88
89 // Update store.
90 $store->set( "$post_id:$field_name", $value );
91
92 // Return value.
93 return $value;
94 }
95
96 // Register variation.
97 acf_add_filter_variations( 'acf/load_value', array('type', 'name', 'key'), 2 );
98
99 /**
100 * acf_format_value
101 *
102 * Returns a formatted version of the provided value.
103 *
104 * @date 28/09/13
105 * @since 5.0.0
106 *
107 * @param mixed $value The field value.
108 * @param (int|string) $post_id The post id.
109 * @param array $field The field array.
110 * @return mixed.
111 */
112 function acf_format_value( $value, $post_id, $field ) {
113
114 // Allow filter to short-circuit load_value logic.
115 $check = apply_filters( "acf/pre_format_value", null, $value, $post_id, $field );
116 if( $check !== null ) {
117 return $check;
118 }
119
120 // Get field name.
121 $field_name = $field['name'];
122
123 // Check store.
124 $store = acf_get_store( 'values' );
125 if( $store->has( "$post_id:$field_name:formatted" ) ) {
126 return $store->get( "$post_id:$field_name:formatted" );
127 }
128
129 /**
130 * Filters the $value for use in a template function.
131 *
132 * @date 28/09/13
133 * @since 5.0.0
134 *
135 * @param mixed $value The value to preview.
136 * @param string $post_id The post ID for this value.
137 * @param array $field The field array.
138 */
139 $value = apply_filters( "acf/format_value", $value, $post_id, $field );
140
141 // Update store.
142 $store->set( "$post_id:$field_name:formatted", $value );
143
144 // Return value.
145 return $value;
146 }
147
148 // Register variation.
149 acf_add_filter_variations( 'acf/format_value', array('type', 'name', 'key'), 2 );
150
151 /**
152 * acf_update_value
153 *
154 * Updates the value for a given field and post_id.
155 *
156 * @date 28/09/13
157 * @since 5.0.0
158 *
159 * @param mixed $value The new value.
160 * @param (int|string) $post_id The post id.
161 * @param array $field The field array.
162 * @return bool.
163 */
164 function acf_update_value( $value, $post_id, $field ) {
165
166 // Allow filter to short-circuit update_value logic.
167 $check = apply_filters( "acf/pre_update_value", null, $value, $post_id, $field );
168 if( $check !== null ) {
169 return $check;
170 }
171
172 /**
173 * Filters the $value before it is updated.
174 *
175 * @date 28/09/13
176 * @since 5.0.0
177 *
178 * @param mixed $value The value to update.
179 * @param string $post_id The post ID for this value.
180 * @param array $field The field array.
181 * @param mixed $original The original value before modification.
182 */
183 $value = apply_filters( "acf/update_value", $value, $post_id, $field, $value );
184
185 // Allow null to delete value.
186 if( $value === null ) {
187 return acf_delete_value( $post_id, $field );
188 }
189
190 // Update meta.
191 $return = acf_update_metadata( $post_id, $field['name'], $value );
192
193 // Update reference.
194 acf_update_metadata( $post_id, $field['name'], $field['key'], true );
195
196 // Delete stored data.
197 acf_flush_value_cache( $post_id, $field['name'] );
198
199 // Return update status.
200 return $return;
201 }
202
203 // Register variation.
204 acf_add_filter_variations( 'acf/update_value', array('type', 'name', 'key'), 2 );
205
206 /**
207 * acf_update_values
208 *
209 * Updates an array of values.
210 *
211 * @date 26/2/19
212 * @since 5.7.13
213 *
214 * @param array values The array of values.
215 * @param (int|string) $post_id The post id.
216 * @return void
217 */
218 function acf_update_values( $values, $post_id ) {
219
220 // Loop over values.
221 foreach( $values as $key => $value ) {
222
223 // Get field.
224 $field = acf_get_field( $key );
225
226 // Update value.
227 if( $field ) {
228 acf_update_value( $value, $post_id, $field );
229 }
230 }
231 }
232
233 /**
234 * acf_flush_value_cache
235 *
236 * Deletes all cached data for this value.
237 *
238 * @date 22/1/19
239 * @since 5.7.10
240 *
241 * @param (int|string) $post_id The post id.
242 * @param string $field_name The field name.
243 * @return void
244 */
245 function acf_flush_value_cache( $post_id = 0, $field_name = '' ) {
246
247 // Delete stored data.
248 acf_get_store( 'values' )
249 ->remove( "$post_id:$field_name" )
250 ->remove( "$post_id:$field_name:formatted" );
251 }
252
253 /**
254 * acf_delete_value
255 *
256 * Deletes the value for a given field and post_id.
257 *
258 * @date 28/09/13
259 * @since 5.0.0
260 *
261 * @param (int|string) $post_id The post id.
262 * @param array $field The field array.
263 * @return bool.
264 */
265 function acf_delete_value( $post_id, $field ) {
266
267 /**
268 * Fires before a value is deleted.
269 *
270 * @date 28/09/13
271 * @since 5.0.0
272 *
273 * @param string $post_id The post ID for this value.
274 * @param mixed $name The meta name.
275 * @param array $field The field array.
276 */
277 do_action( "acf/delete_value", $post_id, $field['name'], $field );
278
279 // Delete meta.
280 $return = acf_delete_metadata( $post_id, $field['name'] );
281
282 // Delete reference.
283 acf_delete_metadata( $post_id, $field['name'], true );
284
285 // Delete stored data.
286 acf_flush_value_cache( $post_id, $field['name'] );
287
288 // Return delete status.
289 return $return;
290 }
291
292 // Register variation.
293 acf_add_filter_variations( 'acf/delete_value', array('type', 'name', 'key'), 2 );
294
295 /**
296 * acf_preview_value
297 *
298 * Return a human friendly 'preview' for a given field value.
299 *
300 * @date 28/09/13
301 * @since 5.0.0
302 *
303 * @param mixed $value The new value.
304 * @param (int|string) $post_id The post id.
305 * @param array $field The field array.
306 * @return bool.
307 */
308 function acf_preview_value( $value, $post_id, $field ) {
309
310 /**
311 * Filters the $value before used in HTML.
312 *
313 * @date 24/10/16
314 * @since 5.5.0
315 *
316 * @param mixed $value The value to preview.
317 * @param string $post_id The post ID for this value.
318 * @param array $field The field array.
319 */
320 return apply_filters( "acf/preview_value", $value, $post_id, $field );
321 }
322
323 // Register variation.
324 acf_add_filter_variations( 'acf/preview_value', array('type', 'name', 'key'), 2 );
325