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