PluginProbe
WooCommerce / 11.1.0-rc.2
WooCommerce v11.1.0-rc.2
11.1.0 11.1.0-rc.2 11.1.0-rc.1 11.1.0-beta.2 11.1.0-beta.1 11.0.1 11.0.0 11.0.0-rc.3 11.0.0-rc.2 11.0.0-rc.1 11.0.0-beta.2 11.0.0-beta.1 10.9.4 10.9.3 10.9.2 10.9.1 10.9.0 10.9.0-rc.1 10.9.0-beta.2 10.9.0-beta.1 10.8.1 10.8.0 10.8.0-rc.1 10.8.0-beta.2 10.8.0-beta.1 All 648 releases
woocommerce / src / Admin / API / Taxes.php
Taxes.php
159 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * REST API Taxes Controller
4 *
5 * Handles requests to /taxes/*
6 */
7
8 namespace Automattic\WooCommerce\Admin\API;
9
10 defined( 'ABSPATH' ) || exit;
11
12 /**
13 * Taxes controller.
14 *
15 * @internal
16 * @extends WC_REST_Taxes_Controller
17 */
18 class Taxes extends \WC_REST_Taxes_Controller {
19
20 /**
21 * Endpoint namespace.
22 *
23 * @var string
24 */
25 protected $namespace = 'wc-analytics';
26
27 /**
28 * Get the query params for collections.
29 *
30 * @return array
31 */
32 public function get_collection_params() {
33 $params = parent::get_collection_params();
34 $params['search'] = array(
35 'description' => __( 'Search by similar tax code.', 'woocommerce' ),
36 'type' => 'string',
37 'validate_callback' => 'rest_validate_request_arg',
38 );
39 $params['include'] = array(
40 'description' => __( 'Limit result set to items that have the specified rate ID(s) assigned.', 'woocommerce' ),
41 'type' => 'array',
42 'items' => array(
43 'type' => 'integer',
44 ),
45 'default' => array(),
46 'validate_callback' => 'rest_validate_request_arg',
47 );
48 return $params;
49 }
50
51 /**
52 * Get all taxes and allow filtering by tax code.
53 *
54 * @param WP_REST_Request $request Full details about the request.
55 * @return WP_Error|WP_REST_Response
56 */
57 public function get_items( $request ) {
58 global $wpdb;
59
60 $prepared_args = array();
61 $prepared_args['order'] = $request['order'];
62 $prepared_args['number'] = $request['per_page'];
63 if ( ! empty( $request['offset'] ) ) {
64 $prepared_args['offset'] = $request['offset'];
65 } else {
66 $prepared_args['offset'] = ( $request['page'] - 1 ) * $prepared_args['number'];
67 }
68 $orderby_possibles = array(
69 'id' => 'tax_rate_id',
70 'order' => 'tax_rate_order',
71 );
72 $prepared_args['orderby'] = $orderby_possibles[ $request['orderby'] ];
73 $prepared_args['class'] = $request['class'];
74 $prepared_args['search'] = $request['search'];
75 $prepared_args['include'] = $request['include'];
76
77 /**
78 * Filter arguments, before passing to $wpdb->get_results(), when querying taxes via the REST API.
79 *
80 * @param array $prepared_args Array of arguments for $wpdb->get_results().
81 * @param WP_REST_Request $request The current request.
82 */
83 $prepared_args = apply_filters( 'woocommerce_rest_tax_query', $prepared_args, $request );
84
85 $query = "
86 SELECT *
87 FROM {$wpdb->prefix}woocommerce_tax_rates
88 WHERE 1 = 1
89 ";
90
91 // Filter by tax class.
92 if ( ! empty( $prepared_args['class'] ) ) {
93 $class = 'standard' !== $prepared_args['class'] ? sanitize_title( $prepared_args['class'] ) : '';
94 $query .= " AND tax_rate_class = '$class'";
95 }
96
97 // Filter by tax code.
98 $tax_code_search = $prepared_args['search'];
99 if ( $tax_code_search ) {
100 $code_like = '%' . $wpdb->esc_like( $tax_code_search ) . '%';
101 $query .= $wpdb->prepare( ' AND CONCAT_WS( "-", NULLIF(tax_rate_country, ""), NULLIF(tax_rate_state, ""), NULLIF(tax_rate_name, ""), NULLIF(tax_rate_priority, "") ) LIKE %s', $code_like );
102 }
103
104 // Filter by included tax rate IDs.
105 $included_taxes = array_map( 'absint', $prepared_args['include'] );
106 if ( ! empty( $included_taxes ) ) {
107 $included_taxes = implode( ',', $prepared_args['include'] );
108 $query .= " AND tax_rate_id IN ({$included_taxes})";
109 }
110
111 // Order tax rates.
112 $order_by = sprintf( ' ORDER BY %s', sanitize_key( $prepared_args['orderby'] ) );
113
114 // Pagination.
115 $pagination = sprintf( ' LIMIT %d, %d', $prepared_args['offset'], $prepared_args['number'] );
116
117 // Query taxes.
118 $results = $wpdb->get_results( $query . $order_by . $pagination ); // @codingStandardsIgnoreLine.
119
120 $taxes = array();
121 foreach ( $results as $tax ) {
122 $data = $this->prepare_item_for_response( $tax, $request );
123 $taxes[] = $this->prepare_response_for_collection( $data );
124 }
125
126 $response = rest_ensure_response( $taxes );
127
128 // Store pagination values for headers then unset for count query.
129 $per_page = (int) $prepared_args['number'];
130 $page = ceil( ( ( (int) $prepared_args['offset'] ) / $per_page ) + 1 );
131
132 // Query only for ids.
133 $wpdb->get_results( str_replace( 'SELECT *', 'SELECT tax_rate_id', $query ) ); // @codingStandardsIgnoreLine.
134
135 // Calculate totals.
136 $total_taxes = (int) $wpdb->num_rows;
137 $response->header( 'X-WP-Total', (int) $total_taxes );
138 $max_pages = ceil( $total_taxes / $per_page );
139 $response->header( 'X-WP-TotalPages', (int) $max_pages );
140
141 $base = add_query_arg( $request->get_query_params(), rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ) );
142 if ( $page > 1 ) {
143 $prev_page = $page - 1;
144 if ( $prev_page > $max_pages ) {
145 $prev_page = $max_pages;
146 }
147 $prev_link = add_query_arg( 'page', $prev_page, $base );
148 $response->link_header( 'prev', $prev_link );
149 }
150 if ( $max_pages > $page ) {
151 $next_page = $page + 1;
152 $next_link = add_query_arg( 'page', $next_page, $base );
153 $response->link_header( 'next', $next_link );
154 }
155
156 return $response;
157 }
158 }
159