CustomOrdersTableController.php
3 months ago
DataSynchronizer.php
7 months ago
LegacyDataCleanup.php
1 year ago
LegacyDataHandler.php
5 months ago
OrdersTableDataStore.php
1 month ago
OrdersTableDataStoreMeta.php
1 year ago
OrdersTableFieldQuery.php
2 years ago
OrdersTableMetaQuery.php
1 year ago
OrdersTableQuery.php
1 month ago
OrdersTableRefundDataStore.php
1 month ago
OrdersTableSearchQuery.php
11 months ago
OrdersTableDataStoreMeta.php
192 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 | return array_filter( $meta_data ); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Store the raw meta data for a set of objects in cache. |
| 140 | * |
| 141 | * @param \stdClass[][] $meta_data An array, keyed by the object IDs, containing arrays of raw meta data for each object. |
| 142 | * |
| 143 | * @return void |
| 144 | */ |
| 145 | private function set_meta_data_for_objects_in_cache( array $meta_data ) { |
| 146 | $cache_engine = wc_get_container()->get( WPCacheEngine::class ); |
| 147 | $cache_engine->cache_objects( $meta_data, 0, $this->get_cache_group() ); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Delete cached meta data for the given object_ids. |
| 152 | * |
| 153 | * @internal This method should only be used by internally and in cases where the CRUD operations of this datastore |
| 154 | * are bypassed for performance purposes. This interface is not guaranteed. |
| 155 | * |
| 156 | * @param array $object_ids The object_ids to delete cache for. |
| 157 | * |
| 158 | * @return bool[] Array of return values, grouped by the object_id. Each value is either true on success, or false |
| 159 | * if the contents were not deleted. |
| 160 | */ |
| 161 | public function clear_cached_data( array $object_ids ): array { |
| 162 | if ( ! OrderUtil::custom_orders_table_datastore_cache_enabled() ) { |
| 163 | return array_fill_keys( $object_ids, true ); |
| 164 | } |
| 165 | |
| 166 | $cache_engine = wc_get_container()->get( WPCacheEngine::class ); |
| 167 | $return_values = array(); |
| 168 | foreach ( $object_ids as $object_id ) { |
| 169 | $return_values[ $object_id ] = $cache_engine->delete_cached_object( $object_id, $this->get_cache_group() ); |
| 170 | } |
| 171 | return $return_values; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Invalidate all the cache used by this data store. |
| 176 | * |
| 177 | * @internal This method should only be used by internally and in cases where the CRUD operations of this datastore |
| 178 | * are bypassed for performance purposes. This interface is not guaranteed. |
| 179 | * |
| 180 | * @return bool Whether the cache as fully invalidated. |
| 181 | */ |
| 182 | public function clear_all_cached_data(): bool { |
| 183 | if ( ! OrderUtil::custom_orders_table_datastore_cache_enabled() ) { |
| 184 | return true; |
| 185 | } |
| 186 | |
| 187 | $cache_engine = wc_get_container()->get( WPCacheEngine::class ); |
| 188 | |
| 189 | return $cache_engine->delete_cache_group( $this->get_cache_group() ); |
| 190 | } |
| 191 | } |
| 192 |