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
WooOrder.php
237 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Adds support for saving/retrieving values from post meta. |
| 4 | * |
| 5 | * @package SCF |
| 6 | * @subpackage Meta |
| 7 | */ |
| 8 | |
| 9 | namespace SCF\Meta; |
| 10 | |
| 11 | use SCF\Meta\MetaLocation; |
| 12 | use Automattic\WooCommerce\Utilities\OrderUtil; |
| 13 | |
| 14 | /** |
| 15 | * A class to add support for saving to WooCommerce order meta. |
| 16 | */ |
| 17 | class WooOrder extends MetaLocation { |
| 18 | |
| 19 | /** |
| 20 | * The unique slug/name of the meta location. |
| 21 | * |
| 22 | * @var string |
| 23 | */ |
| 24 | public string $location_type = 'woo_order'; |
| 25 | |
| 26 | /** |
| 27 | * Constructs the location. |
| 28 | * |
| 29 | * @since 6.5 |
| 30 | */ |
| 31 | public function __construct() { |
| 32 | add_filter( 'acf/decode_post_id', array( $this, 'decode_woo_order_id' ), 10, 2 ); |
| 33 | parent::__construct(); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Checks numerical post IDs to see if they belong to a WC order. |
| 38 | * |
| 39 | * @since 6.5 |
| 40 | * |
| 41 | * @param array $decoded The decoded post ID props. |
| 42 | * @param integer|string $post_id The original post ID. |
| 43 | * @return array |
| 44 | */ |
| 45 | public function decode_woo_order_id( $decoded, $post_id ) { |
| 46 | // Bail if not a standard numeric post ID. |
| 47 | if ( ! is_numeric( $post_id ) || empty( $decoded['type'] ) || 'post' !== $decoded['type'] ) { |
| 48 | return $decoded; |
| 49 | } |
| 50 | |
| 51 | // Bail if HPOS isn't enabled (traditional ACF meta methods work otherwise). |
| 52 | if ( ! method_exists( OrderUtil::class, 'custom_orders_table_usage_is_enabled' ) || |
| 53 | ! OrderUtil::custom_orders_table_usage_is_enabled() ) { |
| 54 | return $decoded; |
| 55 | } |
| 56 | |
| 57 | if ( 'shop_order_placehold' === get_post_type( $post_id ) ) { |
| 58 | $decoded['type'] = 'woo_order'; |
| 59 | } |
| 60 | |
| 61 | return $decoded; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Retrieves all ACF meta for the provided object ID. |
| 66 | * |
| 67 | * @since 6.5 |
| 68 | * |
| 69 | * @param integer|string $object_id The ID of the object to get meta from. |
| 70 | * @return array |
| 71 | */ |
| 72 | public function get_meta( $object_id = 0 ): array { |
| 73 | $meta = array(); |
| 74 | $order = wc_get_order( $object_id ); |
| 75 | |
| 76 | if ( ! $order ) { |
| 77 | return $meta; |
| 78 | } |
| 79 | |
| 80 | $all_meta = $order->get_meta_data(); |
| 81 | $field_names = wp_list_pluck( $all_meta, 'key' ); |
| 82 | $field_values = wp_list_pluck( $all_meta, 'value' ); |
| 83 | |
| 84 | foreach ( $field_names as $key => $field_name ) { |
| 85 | $reference = $this->reference_prefix . $field_name; |
| 86 | $reference_key = array_search( $reference, $field_names, true ); |
| 87 | |
| 88 | if ( false !== $reference_key ) { |
| 89 | $meta[ $field_name ] = $field_values[ $key ]; |
| 90 | $meta[ $reference ] = $field_values[ $reference_key ]; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | // Unserialize results and return. |
| 95 | return array_map( 'acf_maybe_unserialize', $meta ); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Retrieves a field value from the database. |
| 100 | * |
| 101 | * @since 6.5 |
| 102 | * |
| 103 | * @param integer|string $object_id The ID of the object the metadata is for. |
| 104 | * @param array $field The field array. |
| 105 | * @return mixed |
| 106 | */ |
| 107 | public function get_value( $object_id = 0, array $field = array() ) { |
| 108 | $order = wc_get_order( $object_id ); |
| 109 | |
| 110 | if ( ! $order || ! $order->meta_exists( $field['name'] ) ) { |
| 111 | return null; |
| 112 | } |
| 113 | |
| 114 | return $order->get_meta( $field['name'], true, 'edit' ); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Gets a reference key for the provided field name. |
| 119 | * |
| 120 | * @since 6.5 |
| 121 | * |
| 122 | * @param integer|string $object_id The ID of the object to get the reference key from. |
| 123 | * @param string $field_name The name of the field to get the reference for. |
| 124 | * @return string|null |
| 125 | */ |
| 126 | public function get_reference( $object_id = 0, $field_name = '' ) { |
| 127 | $order = wc_get_order( $object_id ); |
| 128 | $key = $this->reference_prefix . $field_name; |
| 129 | |
| 130 | if ( ! $order || ! $order->meta_exists( $key ) ) { |
| 131 | return null; |
| 132 | } |
| 133 | |
| 134 | return $order->get_meta( $key, true, 'edit' ); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Updates an object ID with the provided meta array. |
| 139 | * |
| 140 | * @since 6.5 |
| 141 | * |
| 142 | * @param integer|string $object_id The ID of the object the metadata is for. |
| 143 | * @param array $meta The metadata to save to the object. |
| 144 | * @return void |
| 145 | */ |
| 146 | public function update_meta( $object_id = 0, array $meta = array() ) { |
| 147 | $order = wc_get_order( $object_id ); |
| 148 | |
| 149 | if ( ! $order ) { |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | foreach ( $meta as $name => $value ) { |
| 154 | $value = wp_unslash( $value ); |
| 155 | $order->update_meta_data( $name, $value ); |
| 156 | } |
| 157 | |
| 158 | $order->save(); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Updates a field value in the database. |
| 163 | * |
| 164 | * @since 6.5 |
| 165 | * |
| 166 | * @param integer|string $object_id The ID of the object the metadata is for. |
| 167 | * @param array $field The field array. |
| 168 | * @param mixed $value The metadata value. |
| 169 | * @return integer|boolean |
| 170 | */ |
| 171 | public function update_value( $object_id = 0, array $field = array(), $value = '' ) { |
| 172 | $order = wc_get_order( $object_id ); |
| 173 | |
| 174 | if ( ! $order ) { |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | $value = wp_unslash( $value ); |
| 179 | |
| 180 | $order->update_meta_data( $this->reference_prefix . $field['name'], $field['key'] ); |
| 181 | $order->update_meta_data( $field['name'], $value ); |
| 182 | |
| 183 | return $order->save(); |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Updates a reference key in the database. |
| 188 | * |
| 189 | * @since 6.5 |
| 190 | * |
| 191 | * @param integer|string $object_id The ID of the object the metadata is for. |
| 192 | * @param string $field_name The name of the field to update the reference for. |
| 193 | * @param string $value The value of the reference key. |
| 194 | * @return integer|boolean |
| 195 | */ |
| 196 | public function update_reference( $object_id = 0, string $field_name = '', string $value = '' ) { |
| 197 | // Updated in update_value(). |
| 198 | return true; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Deletes a field value from the database. |
| 203 | * |
| 204 | * @since 6.5 |
| 205 | * |
| 206 | * @param integer|string $object_id The ID of the object the metadata is for. |
| 207 | * @param array $field The field array. |
| 208 | * @return boolean |
| 209 | */ |
| 210 | public function delete_value( $object_id = 0, array $field = array() ): bool { |
| 211 | $order = wc_get_order( $object_id ); |
| 212 | |
| 213 | if ( ! $order ) { |
| 214 | return false; |
| 215 | } |
| 216 | |
| 217 | $order->delete_meta_data( $this->reference_prefix . $field['name'] ); |
| 218 | $order->delete_meta_data( $field['name'] ); |
| 219 | |
| 220 | return $order->save(); |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Deletes a reference key from the database. |
| 225 | * |
| 226 | * @since 6.5 |
| 227 | * |
| 228 | * @param integer|string $object_id The ID of the object the metadata is for. |
| 229 | * @param string $field_name The name of the field to delete the reference from. |
| 230 | * @return boolean |
| 231 | */ |
| 232 | public function delete_reference( $object_id = 0, string $field_name = '' ): bool { |
| 233 | // Deleted in delete_value(). |
| 234 | return true; |
| 235 | } |
| 236 | } |
| 237 |