TaxRateDataStore.php
38 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 | /** |
| 13 | * Fetch multiple tax rate rows in a single query, keyed by tax_rate_id. |
| 14 | * |
| 15 | * @since 11.0.0 |
| 16 | * |
| 17 | * @param int[] $ids Tax rate IDs to fetch. |
| 18 | * @return array<int,object> |
| 19 | */ |
| 20 | public function get_rate_objects_for_ids( array $ids ): array { |
| 21 | $tax_rate_objects = array(); |
| 22 | $ids = array_values( array_filter( array_map( 'absint', array_unique( $ids ) ) ) ); |
| 23 | |
| 24 | if ( ! empty( $ids ) ) { |
| 25 | global $wpdb; |
| 26 | |
| 27 | $list = implode( ', ', $ids ); |
| 28 | // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 29 | $rows = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}woocommerce_tax_rates WHERE tax_rate_id IN ( $list )" ); |
| 30 | foreach ( $rows as $row ) { |
| 31 | $tax_rate_objects[ (int) $row->tax_rate_id ] = $row; |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | return $tax_rate_objects; |
| 36 | } |
| 37 | } |
| 38 |