CustomOrdersTableController.php
5 days ago
DataSynchronizer.php
7 months ago
HposOrderCapabilityHelper.php
5 days ago
LegacyDataCleanup.php
1 year ago
LegacyDataHandler.php
6 months ago
OrdersTableDataStore.php
5 days ago
OrdersTableDataStoreMeta.php
5 days ago
OrdersTableFieldQuery.php
2 years ago
OrdersTableMetaQuery.php
1 year ago
OrdersTableQuery.php
5 days ago
OrdersTableRefundDataStore.php
2 months ago
OrdersTableSearchQuery.php
11 months ago
OrdersTableStatusUnionQuery.php
5 days ago
OrdersTableDataStoreMeta.php
251 lines
| 1 | <?php |
| 2 | /** |
| 3 | * OrdersTableDataStoreMeta class file. |
| 4 | */ |
| 5 | |
| 6 | namespace Automattic\WooCommerce\Internal\DataStores\Orders; |
| 7 | |
| 8 | use Automattic\WooCommerce\Caching\WPCacheEngine; |
| 9 | use Automattic\WooCommerce\Internal\DataStores\CustomMetaDataStore; |
| 10 | use Automattic\WooCommerce\Utilities\OrderUtil; |
| 11 | |
| 12 | /** |
| 13 | * Mimics a WP metadata (i.e. add_metadata(), get_metadata() and friends) implementation using a custom table. |
| 14 | */ |
| 15 | class OrdersTableDataStoreMeta extends CustomMetaDataStore { |
| 16 | |
| 17 | /** |
| 18 | * Returns the cache group to store cached data in. |
| 19 | * |
| 20 | * @return string |
| 21 | */ |
| 22 | protected function get_cache_group() { |
| 23 | return 'orders_meta'; |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Returns the name of the table used for storage. |
| 28 | * |
| 29 | * @return string |
| 30 | */ |
| 31 | protected function get_table_name() { |
| 32 | return OrdersTableDataStore::get_meta_table_name(); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Returns the name of the field/column used for associating meta with objects. |
| 37 | * |
| 38 | * @return string |
| 39 | */ |
| 40 | protected function get_object_id_field() { |
| 41 | return 'order_id'; |
| 42 | } |
| 43 | |
| 44 | // @phpcs:disable Universal.NamingConventions.NoReservedKeywordParameterNames.objectFound |
| 45 | |
| 46 | /** |
| 47 | * Deletes meta based on meta ID. |
| 48 | * |
| 49 | * @param \WC_Data $object WC_Data object. |
| 50 | * @param \stdClass $meta (containing at least ->id). |
| 51 | * |
| 52 | * @return bool |
| 53 | */ |
| 54 | public function delete_meta( &$object, $meta ): bool { |
| 55 | $successful = parent::delete_meta( $object, $meta ); |
| 56 | if ( $successful ) { |
| 57 | $this->clear_cached_data( array( $object->get_id() ) ); |
| 58 | } |
| 59 | |
| 60 | return $successful; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Add new piece of meta. |
| 65 | * |
| 66 | * @param \WC_Data $object WC_Data object. |
| 67 | * @param \stdClass $meta (containing ->key and ->value). |
| 68 | * |
| 69 | * @return int|false meta ID |
| 70 | */ |
| 71 | public function add_meta( &$object, $meta ) { |
| 72 | $insert_id = parent::add_meta( $object, $meta ); |
| 73 | if ( false !== $insert_id ) { |
| 74 | $this->clear_cached_data( array( $object->get_id() ) ); |
| 75 | } |
| 76 | |
| 77 | return $insert_id; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Update meta. |
| 82 | * |
| 83 | * @param \WC_Data $object WC_Data object. |
| 84 | * @param \stdClass $meta (containing ->id, ->key and ->value). |
| 85 | * |
| 86 | * @return bool |
| 87 | */ |
| 88 | public function update_meta( &$object, $meta ): bool { |
| 89 | $is_successful = parent::update_meta( $object, $meta ); |
| 90 | if ( $is_successful ) { |
| 91 | $this->clear_cached_data( array( $object->get_id() ) ); |
| 92 | } |
| 93 | |
| 94 | return $is_successful; |
| 95 | } |
| 96 | |
| 97 | // @phpcs:enable Universal.NamingConventions.NoReservedKeywordParameterNames.objectFound |
| 98 | |
| 99 | /** |
| 100 | * Return order meta data for multiple IDs. Results are cached. |
| 101 | * |
| 102 | * @param array $object_ids List of order IDs. |
| 103 | * |
| 104 | * @return \stdClass[][] An array, keyed by the object IDs, containing arrays of raw meta data for each object. |
| 105 | */ |
| 106 | public function get_meta_data_for_object_ids( array $object_ids ): array { |
| 107 | if ( ! OrderUtil::custom_orders_table_datastore_cache_enabled() ) { |
| 108 | return parent::get_meta_data_for_object_ids( $object_ids ); |
| 109 | } |
| 110 | |
| 111 | $meta_data = $this->get_meta_data_for_object_ids_from_cache( $object_ids ); |
| 112 | $object_ids = array_diff( $object_ids, array_keys( $meta_data ) ); |
| 113 | |
| 114 | if ( empty( $object_ids ) ) { |
| 115 | return $meta_data; |
| 116 | } |
| 117 | |
| 118 | $db_meta_data = parent::get_meta_data_for_object_ids( $object_ids ); |
| 119 | $this->set_meta_data_for_objects_in_cache( $db_meta_data ); |
| 120 | |
| 121 | return $db_meta_data + $meta_data; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Retrieve raw object meta from cache for the given a set of IDs. |
| 126 | * |
| 127 | * @param int[] $object_ids List of object IDs. |
| 128 | * |
| 129 | * @return \stdClass[][] An array, keyed by the object IDs, containing arrays of raw meta data for each object. |
| 130 | */ |
| 131 | private function get_meta_data_for_object_ids_from_cache( array $object_ids ): array { |
| 132 | $cache_engine = wc_get_container()->get( WPCacheEngine::class ); |
| 133 | $meta_data = $cache_engine->get_cached_objects( $object_ids, $this->get_cache_group() ); |
| 134 | |
| 135 | foreach ( $meta_data as $object_id => $object_meta ) { |
| 136 | if ( null === $object_meta ) { |
| 137 | unset( $meta_data[ $object_id ] ); |
| 138 | continue; |
| 139 | } |
| 140 | |
| 141 | if ( $this->is_valid_cached_meta( $object_meta ) ) { |
| 142 | continue; |
| 143 | } |
| 144 | |
| 145 | /* |
| 146 | * A malformed cache entry - not an array, or an array whose elements are not complete |
| 147 | * meta rows - would fatal or silently load wrong values downstream |
| 148 | * (WC_Data_Store_WP::filter_raw_meta_data() reads $meta->meta_key; WC_Data::init_meta_data() |
| 149 | * reads meta_id/meta_key/meta_value). This can happen when a third-party persistent |
| 150 | * object cache returns a corrupt or cross-contaminated value. Invalidate the entry and |
| 151 | * exclude it from the cache hits so it is re-read from the database in this same |
| 152 | * request, and surface it for diagnosis. |
| 153 | */ |
| 154 | $cache_engine->delete_cached_object( $object_id, $this->get_cache_group() ); |
| 155 | wc_get_logger()->warning( |
| 156 | sprintf( |
| 157 | 'Discarded a corrupt HPOS meta cache entry for order %1$d; it will be re-read from the database.', |
| 158 | (int) $object_id |
| 159 | ), |
| 160 | array( 'source' => 'hpos-data-cache' ) |
| 161 | ); |
| 162 | unset( $meta_data[ $object_id ] ); |
| 163 | } |
| 164 | |
| 165 | return $meta_data; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Determine whether a cached meta entry is a well-formed array of meta rows. |
| 170 | * |
| 171 | * An empty array is valid: it represents an order with no meta. Each non-empty element must be |
| 172 | * an object carrying the meta_id, meta_key and meta_value properties that database-backed rows |
| 173 | * always have (see CustomMetaDataStore::get_meta_data_for_object_ids()) and that downstream |
| 174 | * consumers read. |
| 175 | * |
| 176 | * @param mixed $object_meta The cached value to validate. |
| 177 | * |
| 178 | * @return bool True when the value is a usable array of meta rows. |
| 179 | */ |
| 180 | private function is_valid_cached_meta( $object_meta ): bool { |
| 181 | if ( ! is_array( $object_meta ) ) { |
| 182 | return false; |
| 183 | } |
| 184 | |
| 185 | foreach ( $object_meta as $meta_row ) { |
| 186 | if ( ! is_object( $meta_row ) |
| 187 | || ! property_exists( $meta_row, 'meta_id' ) |
| 188 | || ! property_exists( $meta_row, 'meta_key' ) |
| 189 | || ! property_exists( $meta_row, 'meta_value' ) ) { |
| 190 | return false; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | return true; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Store the raw meta data for a set of objects in cache. |
| 199 | * |
| 200 | * @param \stdClass[][] $meta_data An array, keyed by the object IDs, containing arrays of raw meta data for each object. |
| 201 | * |
| 202 | * @return void |
| 203 | */ |
| 204 | private function set_meta_data_for_objects_in_cache( array $meta_data ) { |
| 205 | $cache_engine = wc_get_container()->get( WPCacheEngine::class ); |
| 206 | $cache_engine->cache_objects( $meta_data, 0, $this->get_cache_group() ); |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Delete cached meta data for the given object_ids. |
| 211 | * |
| 212 | * @internal This method should only be used by internally and in cases where the CRUD operations of this datastore |
| 213 | * are bypassed for performance purposes. This interface is not guaranteed. |
| 214 | * |
| 215 | * @param array $object_ids The object_ids to delete cache for. |
| 216 | * |
| 217 | * @return bool[] Array of return values, grouped by the object_id. Each value is either true on success, or false |
| 218 | * if the contents were not deleted. |
| 219 | */ |
| 220 | public function clear_cached_data( array $object_ids ): array { |
| 221 | if ( ! OrderUtil::custom_orders_table_datastore_cache_enabled() ) { |
| 222 | return array_fill_keys( $object_ids, true ); |
| 223 | } |
| 224 | |
| 225 | $cache_engine = wc_get_container()->get( WPCacheEngine::class ); |
| 226 | $return_values = array(); |
| 227 | foreach ( $object_ids as $object_id ) { |
| 228 | $return_values[ $object_id ] = $cache_engine->delete_cached_object( $object_id, $this->get_cache_group() ); |
| 229 | } |
| 230 | return $return_values; |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Invalidate all the cache used by this data store. |
| 235 | * |
| 236 | * @internal This method should only be used by internally and in cases where the CRUD operations of this datastore |
| 237 | * are bypassed for performance purposes. This interface is not guaranteed. |
| 238 | * |
| 239 | * @return bool Whether the cache as fully invalidated. |
| 240 | */ |
| 241 | public function clear_all_cached_data(): bool { |
| 242 | if ( ! OrderUtil::custom_orders_table_datastore_cache_enabled() ) { |
| 243 | return true; |
| 244 | } |
| 245 | |
| 246 | $cache_engine = wc_get_container()->get( WPCacheEngine::class ); |
| 247 | |
| 248 | return $cache_engine->delete_cache_group( $this->get_cache_group() ); |
| 249 | } |
| 250 | } |
| 251 |