PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/woocommerce/packages/woocommerce-admin/src/API/Reports/Taxes/Controller.php

https://gitlab.com/campus-academy/krowkaramel
PHP | 327 lines | 223 code | 25 blank | 79 comment | 4 complexity | c2e93227466b4554383e81ff5630be06 MD5 | raw file
  1. <?php
  2. /**
  3. * REST API Reports taxes controller
  4. *
  5. * Handles requests to the /reports/taxes endpoint.
  6. */
  7. namespace Automattic\WooCommerce\Admin\API\Reports\Taxes;
  8. defined( 'ABSPATH' ) || exit;
  9. use \Automattic\WooCommerce\Admin\API\Reports\ExportableInterface;
  10. use \Automattic\WooCommerce\Admin\API\Reports\ExportableTraits;
  11. /**
  12. * REST API Reports taxes controller class.
  13. *
  14. * @extends WC_REST_Reports_Controller
  15. */
  16. class Controller extends \WC_REST_Reports_Controller implements ExportableInterface {
  17. /**
  18. * Exportable traits.
  19. */
  20. use ExportableTraits;
  21. /**
  22. * Endpoint namespace.
  23. *
  24. * @var string
  25. */
  26. protected $namespace = 'wc-analytics';
  27. /**
  28. * Route base.
  29. *
  30. * @var string
  31. */
  32. protected $rest_base = 'reports/taxes';
  33. /**
  34. * Maps query arguments from the REST request.
  35. *
  36. * @param array $request Request array.
  37. * @return array
  38. */
  39. protected function prepare_reports_query( $request ) {
  40. $args = array();
  41. $args['before'] = $request['before'];
  42. $args['after'] = $request['after'];
  43. $args['page'] = $request['page'];
  44. $args['per_page'] = $request['per_page'];
  45. $args['orderby'] = $request['orderby'];
  46. $args['order'] = $request['order'];
  47. $args['taxes'] = $request['taxes'];
  48. return $args;
  49. }
  50. /**
  51. * Get all reports.
  52. *
  53. * @param WP_REST_Request $request Request data.
  54. * @return array|WP_Error
  55. */
  56. public function get_items( $request ) {
  57. $query_args = $this->prepare_reports_query( $request );
  58. $taxes_query = new Query( $query_args );
  59. $report_data = $taxes_query->get_data();
  60. $data = array();
  61. foreach ( $report_data->data as $tax_data ) {
  62. $item = $this->prepare_item_for_response( (object) $tax_data, $request );
  63. $data[] = $this->prepare_response_for_collection( $item );
  64. }
  65. $response = rest_ensure_response( $data );
  66. $response->header( 'X-WP-Total', (int) $report_data->total );
  67. $response->header( 'X-WP-TotalPages', (int) $report_data->pages );
  68. $page = $report_data->page_no;
  69. $max_pages = $report_data->pages;
  70. $base = add_query_arg( $request->get_query_params(), rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ) );
  71. if ( $page > 1 ) {
  72. $prev_page = $page - 1;
  73. if ( $prev_page > $max_pages ) {
  74. $prev_page = $max_pages;
  75. }
  76. $prev_link = add_query_arg( 'page', $prev_page, $base );
  77. $response->link_header( 'prev', $prev_link );
  78. }
  79. if ( $max_pages > $page ) {
  80. $next_page = $page + 1;
  81. $next_link = add_query_arg( 'page', $next_page, $base );
  82. $response->link_header( 'next', $next_link );
  83. }
  84. return $response;
  85. }
  86. /**
  87. * Prepare a report object for serialization.
  88. *
  89. * @param stdClass $report Report data.
  90. * @param WP_REST_Request $request Request object.
  91. * @return WP_REST_Response
  92. */
  93. public function prepare_item_for_response( $report, $request ) {
  94. $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
  95. $report = $this->add_additional_fields_to_object( $report, $request );
  96. $report = $this->filter_response_by_context( $report, $context );
  97. // Wrap the data in a response object.
  98. $response = rest_ensure_response( $report );
  99. $response->add_links( $this->prepare_links( $report ) );
  100. /**
  101. * Filter a report returned from the API.
  102. *
  103. * Allows modification of the report data right before it is returned.
  104. *
  105. * @param WP_REST_Response $response The response object.
  106. * @param object $report The original report object.
  107. * @param WP_REST_Request $request Request used to generate the response.
  108. */
  109. return apply_filters( 'woocommerce_rest_prepare_report_taxes', $response, $report, $request );
  110. }
  111. /**
  112. * Prepare links for the request.
  113. *
  114. * @param WC_Reports_Query $object Object data.
  115. * @return array
  116. */
  117. protected function prepare_links( $object ) {
  118. $links = array(
  119. 'tax' => array(
  120. 'href' => rest_url( sprintf( '/%s/taxes/%d', $this->namespace, $object->tax_rate_id ) ),
  121. ),
  122. );
  123. return $links;
  124. }
  125. /**
  126. * Get the Report's schema, conforming to JSON Schema.
  127. *
  128. * @return array
  129. */
  130. public function get_item_schema() {
  131. $schema = array(
  132. '$schema' => 'http://json-schema.org/draft-04/schema#',
  133. 'title' => 'report_taxes',
  134. 'type' => 'object',
  135. 'properties' => array(
  136. 'tax_rate_id' => array(
  137. 'description' => __( 'Tax rate ID.', 'woocommerce' ),
  138. 'type' => 'integer',
  139. 'context' => array( 'view', 'edit' ),
  140. 'readonly' => true,
  141. ),
  142. 'name' => array(
  143. 'description' => __( 'Tax rate name.', 'woocommerce' ),
  144. 'type' => 'string',
  145. 'context' => array( 'view', 'edit' ),
  146. 'readonly' => true,
  147. ),
  148. 'tax_rate' => array(
  149. 'description' => __( 'Tax rate.', 'woocommerce' ),
  150. 'type' => 'number',
  151. 'context' => array( 'view', 'edit' ),
  152. 'readonly' => true,
  153. ),
  154. 'country' => array(
  155. 'description' => __( 'Country / Region.', 'woocommerce' ),
  156. 'type' => 'string',
  157. 'context' => array( 'view', 'edit' ),
  158. 'readonly' => true,
  159. ),
  160. 'state' => array(
  161. 'description' => __( 'State.', 'woocommerce' ),
  162. 'type' => 'string',
  163. 'context' => array( 'view', 'edit' ),
  164. 'readonly' => true,
  165. ),
  166. 'priority' => array(
  167. 'description' => __( 'Priority.', 'woocommerce' ),
  168. 'type' => 'integer',
  169. 'context' => array( 'view', 'edit' ),
  170. 'readonly' => true,
  171. ),
  172. 'total_tax' => array(
  173. 'description' => __( 'Total tax.', 'woocommerce' ),
  174. 'type' => 'number',
  175. 'context' => array( 'view', 'edit' ),
  176. 'readonly' => true,
  177. ),
  178. 'order_tax' => array(
  179. 'description' => __( 'Order tax.', 'woocommerce' ),
  180. 'type' => 'number',
  181. 'context' => array( 'view', 'edit' ),
  182. 'readonly' => true,
  183. ),
  184. 'shipping_tax' => array(
  185. 'description' => __( 'Shipping tax.', 'woocommerce' ),
  186. 'type' => 'number',
  187. 'context' => array( 'view', 'edit' ),
  188. 'readonly' => true,
  189. ),
  190. 'orders_count' => array(
  191. 'description' => __( 'Number of orders.', 'woocommerce' ),
  192. 'type' => 'integer',
  193. 'context' => array( 'view', 'edit' ),
  194. 'readonly' => true,
  195. ),
  196. ),
  197. );
  198. return $this->add_additional_fields_schema( $schema );
  199. }
  200. /**
  201. * Get the query params for collections.
  202. *
  203. * @return array
  204. */
  205. public function get_collection_params() {
  206. $params = array();
  207. $params['context'] = $this->get_context_param( array( 'default' => 'view' ) );
  208. $params['page'] = array(
  209. 'description' => __( 'Current page of the collection.', 'woocommerce' ),
  210. 'type' => 'integer',
  211. 'default' => 1,
  212. 'sanitize_callback' => 'absint',
  213. 'validate_callback' => 'rest_validate_request_arg',
  214. 'minimum' => 1,
  215. );
  216. $params['per_page'] = array(
  217. 'description' => __( 'Maximum number of items to be returned in result set.', 'woocommerce' ),
  218. 'type' => 'integer',
  219. 'default' => 10,
  220. 'minimum' => 1,
  221. 'maximum' => 100,
  222. 'sanitize_callback' => 'absint',
  223. 'validate_callback' => 'rest_validate_request_arg',
  224. );
  225. $params['after'] = array(
  226. 'description' => __( 'Limit response to resources published after a given ISO8601 compliant date.', 'woocommerce' ),
  227. 'type' => 'string',
  228. 'format' => 'date-time',
  229. 'validate_callback' => 'rest_validate_request_arg',
  230. );
  231. $params['before'] = array(
  232. 'description' => __( 'Limit response to resources published before a given ISO8601 compliant date.', 'woocommerce' ),
  233. 'type' => 'string',
  234. 'format' => 'date-time',
  235. 'validate_callback' => 'rest_validate_request_arg',
  236. );
  237. $params['order'] = array(
  238. 'description' => __( 'Order sort attribute ascending or descending.', 'woocommerce' ),
  239. 'type' => 'string',
  240. 'default' => 'desc',
  241. 'enum' => array( 'asc', 'desc' ),
  242. 'validate_callback' => 'rest_validate_request_arg',
  243. );
  244. $params['orderby'] = array(
  245. 'description' => __( 'Sort collection by object attribute.', 'woocommerce' ),
  246. 'type' => 'string',
  247. 'default' => 'tax_rate_id',
  248. 'enum' => array(
  249. 'name',
  250. 'tax_rate_id',
  251. 'tax_code',
  252. 'rate',
  253. 'order_tax',
  254. 'total_tax',
  255. 'shipping_tax',
  256. 'orders_count',
  257. ),
  258. 'validate_callback' => 'rest_validate_request_arg',
  259. );
  260. $params['taxes'] = array(
  261. 'description' => __( 'Limit result set to items assigned one or more tax rates.', 'woocommerce' ),
  262. 'type' => 'array',
  263. 'sanitize_callback' => 'wp_parse_id_list',
  264. 'validate_callback' => 'rest_validate_request_arg',
  265. 'items' => array(
  266. 'type' => 'string',
  267. ),
  268. );
  269. return $params;
  270. }
  271. /**
  272. * Get the column names for export.
  273. *
  274. * @return array Key value pair of Column ID => Label.
  275. */
  276. public function get_export_columns() {
  277. return array(
  278. 'tax_code' => __( 'Tax code', 'woocommerce' ),
  279. 'rate' => __( 'Rate', 'woocommerce' ),
  280. 'total_tax' => __( 'Total tax', 'woocommerce' ),
  281. 'order_tax' => __( 'Order tax', 'woocommerce' ),
  282. 'shipping_tax' => __( 'Shipping tax', 'woocommerce' ),
  283. 'orders_count' => __( 'Orders', 'woocommerce' ),
  284. );
  285. }
  286. /**
  287. * Get the column values for export.
  288. *
  289. * @param array $item Single report item/row.
  290. * @return array Key value pair of Column ID => Row Value.
  291. */
  292. public function prepare_item_for_export( $item ) {
  293. return array(
  294. 'tax_code' => \WC_Tax::get_rate_code( $item['tax_rate_id'] ),
  295. 'rate' => $item['tax_rate'],
  296. 'total_tax' => self::csv_number_format( $item['total_tax'] ),
  297. 'order_tax' => self::csv_number_format( $item['order_tax'] ),
  298. 'shipping_tax' => self::csv_number_format( $item['shipping_tax'] ),
  299. 'orders_count' => $item['orders_count'],
  300. );
  301. }
  302. }