TaxRateDataStore.php
50 lines
| 1 | <?php |
| 2 | |
| 3 | declare( strict_types=1 ); |
| 4 | |
| 5 | namespace Automattic\WooCommerce\Internal\Tax; |
| 6 | |
| 7 | /** |
| 8 | * Data store for tax rates. |
| 9 | */ |
| 10 | class TaxRateDataStore { |
| 11 | /** |
| 12 | * Request-level cache of fetched tax rate rows, keyed by tax_rate_id. |
| 13 | * |
| 14 | * @var array<int,object> |
| 15 | */ |
| 16 | private array $rate_objects_cache = array(); |
| 17 | |
| 18 | /** |
| 19 | * Fetch multiple tax rate rows in a single query, keyed by tax_rate_id. |
| 20 | * |
| 21 | * @since 11.0.0 |
| 22 | * |
| 23 | * @param int[] $ids Tax rate IDs to fetch. |
| 24 | * @return array<int,object> |
| 25 | */ |
| 26 | public function get_rate_objects_for_ids( array $ids ): array { |
| 27 | global $wpdb; |
| 28 | |
| 29 | $ids = array_filter( array_map( 'absint', array_unique( $ids ) ) ); |
| 30 | $uncached_ids = array_diff( $ids, array_keys( $this->rate_objects_cache ) ); |
| 31 | if ( ! empty( $uncached_ids ) ) { |
| 32 | $list = implode( ', ', $uncached_ids ); |
| 33 | // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 34 | $rows = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}woocommerce_tax_rates WHERE tax_rate_id IN ( $list )" ); |
| 35 | foreach ( $rows as $row ) { |
| 36 | $this->rate_objects_cache[ (int) $row->tax_rate_id ] = $row; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | $result = array(); |
| 41 | foreach ( $ids as $id ) { |
| 42 | if ( isset( $this->rate_objects_cache[ $id ] ) ) { |
| 43 | $result[ $id ] = $this->rate_objects_cache[ $id ]; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | return $result; |
| 48 | } |
| 49 | } |
| 50 |