| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* acf_get_metadata |
| 5 |
* |
| 6 |
* This function will get a value from the DB |
| 7 |
* |
| 8 |
* @type function |
| 9 |
* @date 16/10/2015 |
| 10 |
* @since 5.2.3 |
| 11 |
* |
| 12 |
* @param $post_id (mixed) |
| 13 |
* @param $name (string) |
| 14 |
* @param $hidden (boolean) |
| 15 |
* @return $return (mixed) |
| 16 |
*/ |
| 17 |
|
| 18 |
function acf_get_metadata( $post_id = 0, $name = '', $hidden = false ) { |
| 19 |
|
| 20 |
// vars |
| 21 |
$value = null; |
| 22 |
$prefix = $hidden ? '_' : ''; |
| 23 |
|
| 24 |
|
| 25 |
// get post_id info |
| 26 |
$info = acf_get_post_id_info($post_id); |
| 27 |
|
| 28 |
|
| 29 |
// bail early if no $post_id (acf_form - new_post) |
| 30 |
if( !$info['id'] ) return $value; |
| 31 |
|
| 32 |
|
| 33 |
// option |
| 34 |
if( $info['type'] === 'option' ) { |
| 35 |
|
| 36 |
$name = $prefix . $post_id . '_' . $name; |
| 37 |
$value = get_option( $name, null ); |
| 38 |
|
| 39 |
// meta |
| 40 |
} else { |
| 41 |
|
| 42 |
$name = $prefix . $name; |
| 43 |
$meta = get_metadata( $info['type'], $info['id'], $name, false ); |
| 44 |
|
| 45 |
if( isset($meta[0]) ) { |
| 46 |
|
| 47 |
$value = $meta[0]; |
| 48 |
|
| 49 |
} |
| 50 |
|
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
// return |
| 55 |
return $value; |
| 56 |
|
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
/* |
| 61 |
* acf_update_metadata |
| 62 |
* |
| 63 |
* This function will update a value from the DB |
| 64 |
* |
| 65 |
* @type function |
| 66 |
* @date 16/10/2015 |
| 67 |
* @since 5.2.3 |
| 68 |
* |
| 69 |
* @param $post_id (mixed) |
| 70 |
* @param $name (string) |
| 71 |
* @param $value (mixed) |
| 72 |
* @param $hidden (boolean) |
| 73 |
* @return $return (boolean) |
| 74 |
*/ |
| 75 |
|
| 76 |
function acf_update_metadata( $post_id = 0, $name = '', $value = '', $hidden = false ) { |
| 77 |
|
| 78 |
// vars |
| 79 |
$return = false; |
| 80 |
$prefix = $hidden ? '_' : ''; |
| 81 |
|
| 82 |
|
| 83 |
// get post_id info |
| 84 |
$info = acf_get_post_id_info($post_id); |
| 85 |
|
| 86 |
|
| 87 |
// bail early if no $post_id (acf_form - new_post) |
| 88 |
if( !$info['id'] ) return $return; |
| 89 |
|
| 90 |
|
| 91 |
// option |
| 92 |
if( $info['type'] === 'option' ) { |
| 93 |
|
| 94 |
$name = $prefix . $post_id . '_' . $name; |
| 95 |
$return = acf_update_option( $name, $value ); |
| 96 |
|
| 97 |
// meta |
| 98 |
} else { |
| 99 |
|
| 100 |
$name = $prefix . $name; |
| 101 |
$return = update_metadata( $info['type'], $info['id'], $name, $value ); |
| 102 |
|
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
// return |
| 107 |
return $return; |
| 108 |
|
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
/* |
| 113 |
* acf_delete_metadata |
| 114 |
* |
| 115 |
* This function will delete a value from the DB |
| 116 |
* |
| 117 |
* @type function |
| 118 |
* @date 16/10/2015 |
| 119 |
* @since 5.2.3 |
| 120 |
* |
| 121 |
* @param $post_id (mixed) |
| 122 |
* @param $name (string) |
| 123 |
* @param $hidden (boolean) |
| 124 |
* @return $return (boolean) |
| 125 |
*/ |
| 126 |
|
| 127 |
function acf_delete_metadata( $post_id = 0, $name = '', $hidden = false ) { |
| 128 |
|
| 129 |
// vars |
| 130 |
$return = false; |
| 131 |
$prefix = $hidden ? '_' : ''; |
| 132 |
|
| 133 |
|
| 134 |
// get post_id info |
| 135 |
$info = acf_get_post_id_info($post_id); |
| 136 |
|
| 137 |
|
| 138 |
// bail early if no $post_id (acf_form - new_post) |
| 139 |
if( !$info['id'] ) return $return; |
| 140 |
|
| 141 |
|
| 142 |
// option |
| 143 |
if( $info['type'] === 'option' ) { |
| 144 |
|
| 145 |
$name = $prefix . $post_id . '_' . $name; |
| 146 |
$return = delete_option( $name ); |
| 147 |
|
| 148 |
// meta |
| 149 |
} else { |
| 150 |
|
| 151 |
$name = $prefix . $name; |
| 152 |
$return = delete_metadata( $info['type'], $info['id'], $name ); |
| 153 |
|
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
// return |
| 158 |
return $return; |
| 159 |
|
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
/* |
| 164 |
* acf_update_option |
| 165 |
* |
| 166 |
* This function is a wrapper for the WP update_option but provides logic for a 'no' autoload |
| 167 |
* |
| 168 |
* @type function |
| 169 |
* @date 4/01/2014 |
| 170 |
* @since 5.0.0 |
| 171 |
* |
| 172 |
* @param $option (string) |
| 173 |
* @param $value (mixed) |
| 174 |
* @param autoload (mixed) |
| 175 |
* @return (boolean) |
| 176 |
*/ |
| 177 |
|
| 178 |
function acf_update_option( $option = '', $value = '', $autoload = null ) { |
| 179 |
|
| 180 |
// vars |
| 181 |
$deprecated = ''; |
| 182 |
$return = false; |
| 183 |
|
| 184 |
|
| 185 |
// autoload |
| 186 |
if( $autoload === null ){ |
| 187 |
|
| 188 |
$autoload = acf_get_setting('autoload') ? 'yes' : 'no'; |
| 189 |
|
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
// for some reason, update_option does not use stripslashes_deep. |
| 194 |
// update_metadata -> https://core.trac.wordpress.org/browser/tags/3.4.2/wp-includes/meta.php#L82: line 101 (does use stripslashes_deep) |
| 195 |
// update_option -> https://core.trac.wordpress.org/browser/tags/3.5.1/wp-includes/option.php#L0: line 215 (does not use stripslashes_deep) |
| 196 |
$value = stripslashes_deep($value); |
| 197 |
|
| 198 |
|
| 199 |
// add or update |
| 200 |
if( get_option($option) !== false ) { |
| 201 |
|
| 202 |
$return = update_option( $option, $value ); |
| 203 |
|
| 204 |
} else { |
| 205 |
|
| 206 |
$return = add_option( $option, $value, $deprecated, $autoload ); |
| 207 |
|
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
// return |
| 212 |
return $return; |
| 213 |
|
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
/* |
| 218 |
* acf_get_value |
| 219 |
* |
| 220 |
* This function will load in a field's value |
| 221 |
* |
| 222 |
* @type function |
| 223 |
* @date 28/09/13 |
| 224 |
* @since 5.0.0 |
| 225 |
* |
| 226 |
* @param $post_id (int) |
| 227 |
* @param $field (array) |
| 228 |
* @return (mixed) |
| 229 |
*/ |
| 230 |
|
| 231 |
function acf_get_value( $post_id = 0, $field ) { |
| 232 |
|
| 233 |
// allow filter to short-circuit load_value logic |
| 234 |
//$value = apply_filters( "acf/pre_load_value", null, $post_id, $field ); |
| 235 |
//if( $value !== null ) { |
| 236 |
// return $value; |
| 237 |
//} |
| 238 |
|
| 239 |
|
| 240 |
// vars |
| 241 |
$cache_key = "get_value/post_id={$post_id}/name={$field['name']}"; |
| 242 |
|
| 243 |
|
| 244 |
// return early if cache is found |
| 245 |
if( acf_isset_cache($cache_key) ) { |
| 246 |
return acf_get_cache($cache_key); |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
// load value |
| 251 |
$value = acf_get_metadata( $post_id, $field['name'] ); |
| 252 |
|
| 253 |
|
| 254 |
// if value was duplicated, it may now be a serialized string! |
| 255 |
$value = maybe_unserialize( $value ); |
| 256 |
|
| 257 |
|
| 258 |
// no value? try default_value |
| 259 |
if( $value === null && isset($field['default_value']) ) { |
| 260 |
$value = $field['default_value']; |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
// filter for 3rd party customization |
| 265 |
$value = apply_filters( "acf/load_value", $value, $post_id, $field ); |
| 266 |
$value = apply_filters( "acf/load_value/type={$field['type']}", $value, $post_id, $field ); |
| 267 |
$value = apply_filters( "acf/load_value/name={$field['_name']}", $value, $post_id, $field ); |
| 268 |
$value = apply_filters( "acf/load_value/key={$field['key']}", $value, $post_id, $field ); |
| 269 |
|
| 270 |
|
| 271 |
// update cache |
| 272 |
acf_set_cache($cache_key, $value); |
| 273 |
|
| 274 |
|
| 275 |
// return |
| 276 |
return $value; |
| 277 |
|
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
/* |
| 282 |
* acf_format_value |
| 283 |
* |
| 284 |
* This function will format the value for front end use |
| 285 |
* |
| 286 |
* @type function |
| 287 |
* @date 3/07/2014 |
| 288 |
* @since 5.0.0 |
| 289 |
* |
| 290 |
* @param $value (mixed) |
| 291 |
* @param $post_id (mixed) |
| 292 |
* @param $field (array) |
| 293 |
* @return $value |
| 294 |
*/ |
| 295 |
|
| 296 |
function acf_format_value( $value, $post_id, $field ) { |
| 297 |
|
| 298 |
// vars |
| 299 |
$cache_key = "format_value/post_id={$post_id}/name={$field['name']}"; |
| 300 |
|
| 301 |
|
| 302 |
// return early if cache is found |
| 303 |
if( acf_isset_cache($cache_key) ) { |
| 304 |
|
| 305 |
return acf_get_cache($cache_key); |
| 306 |
|
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
// apply filters |
| 311 |
$value = apply_filters( "acf/format_value", $value, $post_id, $field ); |
| 312 |
$value = apply_filters( "acf/format_value/type={$field['type']}", $value, $post_id, $field ); |
| 313 |
$value = apply_filters( "acf/format_value/name={$field['_name']}", $value, $post_id, $field ); |
| 314 |
$value = apply_filters( "acf/format_value/key={$field['key']}", $value, $post_id, $field ); |
| 315 |
|
| 316 |
|
| 317 |
// update cache |
| 318 |
acf_set_cache($cache_key, $value); |
| 319 |
|
| 320 |
|
| 321 |
// return |
| 322 |
return $value; |
| 323 |
|
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
/* |
| 328 |
* acf_update_value |
| 329 |
* |
| 330 |
* updates a value into the db |
| 331 |
* |
| 332 |
* @type action |
| 333 |
* @date 23/01/13 |
| 334 |
* |
| 335 |
* @param $value (mixed) |
| 336 |
* @param $post_id (mixed) |
| 337 |
* @param $field (array) |
| 338 |
* @return (boolean) |
| 339 |
*/ |
| 340 |
|
| 341 |
function acf_update_value( $value = null, $post_id = 0, $field ) { |
| 342 |
|
| 343 |
// strip slashes |
| 344 |
if( acf_get_setting('stripslashes') ) { |
| 345 |
|
| 346 |
$value = stripslashes_deep($value); |
| 347 |
|
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
// filter for 3rd party customization |
| 352 |
$value = apply_filters( "acf/update_value", $value, $post_id, $field ); |
| 353 |
$value = apply_filters( "acf/update_value/type={$field['type']}", $value, $post_id, $field ); |
| 354 |
$value = apply_filters( "acf/update_value/name={$field['_name']}", $value, $post_id, $field ); |
| 355 |
$value = apply_filters( "acf/update_value/key={$field['key']}", $value, $post_id, $field ); |
| 356 |
|
| 357 |
|
| 358 |
// allow null to delete |
| 359 |
if( $value === null ) { |
| 360 |
|
| 361 |
return acf_delete_value( $post_id, $field ); |
| 362 |
|
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
// update value |
| 367 |
$return = acf_update_metadata( $post_id, $field['name'], $value ); |
| 368 |
|
| 369 |
|
| 370 |
// update reference |
| 371 |
acf_update_metadata( $post_id, $field['name'], $field['key'], true ); |
| 372 |
|
| 373 |
|
| 374 |
// clear cache |
| 375 |
acf_delete_cache("get_value/post_id={$post_id}/name={$field['name']}"); |
| 376 |
acf_delete_cache("format_value/post_id={$post_id}/name={$field['name']}"); |
| 377 |
|
| 378 |
|
| 379 |
// return |
| 380 |
return $return; |
| 381 |
|
| 382 |
} |
| 383 |
|
| 384 |
|
| 385 |
/* |
| 386 |
* acf_delete_value |
| 387 |
* |
| 388 |
* This function will delete a value from the database |
| 389 |
* |
| 390 |
* @type function |
| 391 |
* @date 28/09/13 |
| 392 |
* @since 5.0.0 |
| 393 |
* |
| 394 |
* @param $post_id (mixed) |
| 395 |
* @param $field (array) |
| 396 |
* @return (boolean) |
| 397 |
*/ |
| 398 |
|
| 399 |
function acf_delete_value( $post_id = 0, $field ) { |
| 400 |
|
| 401 |
// action for 3rd party customization |
| 402 |
do_action("acf/delete_value", $post_id, $field['name'], $field); |
| 403 |
do_action("acf/delete_value/type={$field['type']}", $post_id, $field['name'], $field); |
| 404 |
do_action("acf/delete_value/name={$field['_name']}", $post_id, $field['name'], $field); |
| 405 |
do_action("acf/delete_value/key={$field['key']}", $post_id, $field['name'], $field); |
| 406 |
|
| 407 |
|
| 408 |
// delete value |
| 409 |
$return = acf_delete_metadata( $post_id, $field['name'] ); |
| 410 |
|
| 411 |
|
| 412 |
// delete reference |
| 413 |
acf_delete_metadata( $post_id, $field['name'], true ); |
| 414 |
|
| 415 |
|
| 416 |
// clear cache |
| 417 |
acf_delete_cache("get_value/post_id={$post_id}/name={$field['name']}"); |
| 418 |
acf_delete_cache("format_value/post_id={$post_id}/name={$field['name']}"); |
| 419 |
|
| 420 |
|
| 421 |
// return |
| 422 |
return $return; |
| 423 |
|
| 424 |
} |
| 425 |
|
| 426 |
|
| 427 |
/* |
| 428 |
* acf_copy_postmeta |
| 429 |
* |
| 430 |
* This function will copy postmeta from one post to another. |
| 431 |
* Very useful for saving and restoring revisions |
| 432 |
* |
| 433 |
* @type function |
| 434 |
* @date 25/06/2016 |
| 435 |
* @since 5.3.8 |
| 436 |
* |
| 437 |
* @param $from_post_id (int) |
| 438 |
* @param $to_post_id (int) |
| 439 |
* @return n/a |
| 440 |
*/ |
| 441 |
|
| 442 |
function acf_copy_postmeta( $from_post_id, $to_post_id ) { |
| 443 |
|
| 444 |
// get all postmeta |
| 445 |
$meta = get_post_meta( $from_post_id ); |
| 446 |
|
| 447 |
|
| 448 |
// bail early if no meta |
| 449 |
if( !$meta ) return; |
| 450 |
|
| 451 |
|
| 452 |
// loop |
| 453 |
foreach( $meta as $name => $value ) { |
| 454 |
|
| 455 |
// attempt to find key value |
| 456 |
$key = acf_maybe_get( $meta, '_'.$name ); |
| 457 |
|
| 458 |
|
| 459 |
// bail ealry if no key |
| 460 |
if( !$key ) continue; |
| 461 |
|
| 462 |
|
| 463 |
// update vars |
| 464 |
$value = $value[0]; |
| 465 |
$key = $key[0]; |
| 466 |
|
| 467 |
|
| 468 |
// bail early if $key is a not a field_key |
| 469 |
if( !acf_is_field_key($key) ) continue; |
| 470 |
|
| 471 |
|
| 472 |
// get_post_meta will return array before running maybe_unserialize |
| 473 |
$value = maybe_unserialize( $value ); |
| 474 |
|
| 475 |
|
| 476 |
// add in slashes |
| 477 |
// - update_post_meta will unslash the value, so we must first slash it to avoid losing backslashes |
| 478 |
// - https://codex.wordpress.org/Function_Reference/update_post_meta#Character_Escaping |
| 479 |
if( is_string($value) ) { |
| 480 |
|
| 481 |
$value = wp_slash($value); |
| 482 |
|
| 483 |
} |
| 484 |
|
| 485 |
|
| 486 |
// update value |
| 487 |
acf_update_metadata( $to_post_id, $name, $value ); |
| 488 |
acf_update_metadata( $to_post_id, $name, $key, true ); |
| 489 |
|
| 490 |
} |
| 491 |
|
| 492 |
} |
| 493 |
|
| 494 |
|
| 495 |
/* |
| 496 |
* acf_preview_value |
| 497 |
* |
| 498 |
* This function will return a human freindly 'preview' for a given field value |
| 499 |
* |
| 500 |
* @type function |
| 501 |
* @date 24/10/16 |
| 502 |
* @since 5.5.0 |
| 503 |
* |
| 504 |
* @param $value (mixed) |
| 505 |
* @param $post_id (mixed) |
| 506 |
* @param $field (array) |
| 507 |
* @return (string) |
| 508 |
*/ |
| 509 |
|
| 510 |
function acf_preview_value( $value, $post_id, $field ) { |
| 511 |
|
| 512 |
// apply filters |
| 513 |
$value = apply_filters( "acf/preview_value", $value, $post_id, $field ); |
| 514 |
$value = apply_filters( "acf/preview_value/type={$field['type']}", $value, $post_id, $field ); |
| 515 |
$value = apply_filters( "acf/preview_value/name={$field['_name']}", $value, $post_id, $field ); |
| 516 |
$value = apply_filters( "acf/preview_value/key={$field['key']}", $value, $post_id, $field ); |
| 517 |
|
| 518 |
|
| 519 |
// return |
| 520 |
return $value; |
| 521 |
|
| 522 |
} |
| 523 |
|
| 524 |
?> |