| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package ACF |
| 4 |
* @author WP Engine |
| 5 |
* |
| 6 |
* © 2025 Advanced Custom Fields (ACF®). All rights reserved. |
| 7 |
* "ACF" is a trademark of WP Engine. |
| 8 |
* Licensed under the GNU General Public License v2 or later. |
| 9 |
* https://www.gnu.org/licenses/gpl-2.0.html |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace ACF\Meta; |
| 13 |
|
| 14 |
/** |
| 15 |
* A class to add support for saving to options. |
| 16 |
*/ |
| 17 |
class Option extends MetaLocation { |
| 18 |
|
| 19 |
/** |
| 20 |
* The unique slug/name of the meta location. |
| 21 |
* |
| 22 |
* @var string |
| 23 |
*/ |
| 24 |
public string $location_type = 'option'; |
| 25 |
|
| 26 |
/** |
| 27 |
* Retrieves all ACF meta for the provided object ID. |
| 28 |
* |
| 29 |
* @since 6.4 |
| 30 |
* |
| 31 |
* @param integer|string $object_id The ID of the object to get meta from. |
| 32 |
* @return array |
| 33 |
*/ |
| 34 |
public function get_meta( $object_id = 0 ): array { |
| 35 |
$all_meta = acf_get_option_meta( $object_id ); |
| 36 |
$meta = array(); |
| 37 |
|
| 38 |
foreach ( $all_meta as $key => $value ) { |
| 39 |
// If a reference exists for this value, add it to the meta array. |
| 40 |
if ( isset( $all_meta[ $this->reference_prefix . $key ] ) ) { |
| 41 |
$meta[ $key ] = $value[0]; |
| 42 |
$meta[ $this->reference_prefix . $key ] = $all_meta[ $this->reference_prefix . $key ][0]; |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
// Return results. |
| 47 |
return $meta; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Retrieves a field value from the database. |
| 52 |
* |
| 53 |
* @since 6.4 |
| 54 |
* |
| 55 |
* @param integer|string $object_id The ID of the object the metadata is for. |
| 56 |
* @param array $field The field array. |
| 57 |
* @return mixed |
| 58 |
*/ |
| 59 |
public function get_value( $object_id = 0, array $field = array() ) { |
| 60 |
return get_option( $object_id . '_' . $field['name'], null ); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Gets a reference key for the provided field name. |
| 65 |
* |
| 66 |
* @since 6.4 |
| 67 |
* |
| 68 |
* @param integer|string $object_id The ID of the object to get the reference key from. |
| 69 |
* @param string $field_name The name of the field to get the reference for. |
| 70 |
* @return string|boolean |
| 71 |
*/ |
| 72 |
public function get_reference( $object_id = '', $field_name = '' ) { |
| 73 |
return get_option( $this->reference_prefix . $object_id . '_' . $field_name, null ); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Updates an object ID with the provided meta array. |
| 78 |
* |
| 79 |
* @since 6.4 |
| 80 |
* |
| 81 |
* @param integer|string $object_id The ID of the object the metadata is for. |
| 82 |
* @param array $meta The metadata to save to the object. |
| 83 |
* @return void |
| 84 |
*/ |
| 85 |
public function update_meta( $object_id = 0, array $meta = array() ) { |
| 86 |
$autoload = (bool) acf_get_setting( 'autoload' ); |
| 87 |
|
| 88 |
foreach ( $meta as $name => $value ) { |
| 89 |
$value = wp_unslash( $value ); |
| 90 |
update_option( $object_id . '_' . $name, $value, $autoload ); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Updates a field value in the database. |
| 96 |
* |
| 97 |
* @since 6.4 |
| 98 |
* |
| 99 |
* @param integer|string $object_id The ID of the object the metadata is for. |
| 100 |
* @param array $field The field array. |
| 101 |
* @param mixed $value The metadata value. |
| 102 |
* @return integer|boolean |
| 103 |
*/ |
| 104 |
public function update_value( $object_id = 0, array $field = array(), $value = '' ) { |
| 105 |
$value = wp_unslash( $value ); |
| 106 |
$autoload = (bool) acf_get_setting( 'autoload' ); |
| 107 |
|
| 108 |
return update_option( $object_id . '_' . $field['name'], $value, $autoload ); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Updates a reference key in the database. |
| 113 |
* |
| 114 |
* @since 6.4 |
| 115 |
* |
| 116 |
* @param integer|string $object_id The ID of the object the metadata is for. |
| 117 |
* @param string $field_name The name of the field to update the reference for. |
| 118 |
* @param string $value The value of the reference key. |
| 119 |
* @return integer|boolean |
| 120 |
*/ |
| 121 |
public function update_reference( $object_id = 0, string $field_name = '', string $value = '' ) { |
| 122 |
$autoload = (bool) acf_get_setting( 'autoload' ); |
| 123 |
return update_option( $this->reference_prefix . $object_id . '_' . $field_name, $value, $autoload ); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Deletes a field value from the database. |
| 128 |
* |
| 129 |
* @since 6.4 |
| 130 |
* |
| 131 |
* @param integer|string $object_id The ID of the object the metadata is for. |
| 132 |
* @param array $field The field array. |
| 133 |
* @return boolean |
| 134 |
*/ |
| 135 |
public function delete_value( $object_id = 0, array $field = array() ): bool { |
| 136 |
return delete_option( $object_id . '_' . $field['name'] ); |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Deletes a reference key from the database. |
| 141 |
* |
| 142 |
* @since 6.4 |
| 143 |
* |
| 144 |
* @param integer|string $object_id The ID of the object the metadata is for. |
| 145 |
* @param string $field_name The name of the field to delete the reference from. |
| 146 |
* @return boolean |
| 147 |
*/ |
| 148 |
public function delete_reference( $object_id = 0, string $field_name = '' ): bool { |
| 149 |
return delete_option( $this->reference_prefix . $object_id . '_' . $field_name ); |
| 150 |
} |
| 151 |
} |
| 152 |
|