PageRenderTime 28ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/campus-academy/krowkaramel
PHP | 400 lines | 306 code | 26 blank | 68 comment | 5 complexity | 5a6687071b4edb80497b5ddb90a6fb47 MD5 | raw file
  1. <?php
  2. /**
  3. * REST API Reports taxes stats controller
  4. *
  5. * Handles requests to the /reports/taxes/stats endpoint.
  6. */
  7. namespace Automattic\WooCommerce\Admin\API\Reports\Taxes\Stats;
  8. defined( 'ABSPATH' ) || exit;
  9. /**
  10. * REST API Reports taxes stats controller class.
  11. *
  12. * @extends WC_REST_Reports_Controller
  13. */
  14. class Controller extends \WC_REST_Reports_Controller {
  15. /**
  16. * Endpoint namespace.
  17. *
  18. * @var string
  19. */
  20. protected $namespace = 'wc-analytics';
  21. /**
  22. * Route base.
  23. *
  24. * @var string
  25. */
  26. protected $rest_base = 'reports/taxes/stats';
  27. /**
  28. * Constructor.
  29. */
  30. public function __construct() {
  31. add_filter( 'woocommerce_analytics_taxes_stats_select_query', array( $this, 'set_default_report_data' ) );
  32. }
  33. /**
  34. * Set the default results to 0 if API returns an empty array
  35. *
  36. * @param Mixed $results Report data.
  37. * @return object
  38. */
  39. public function set_default_report_data( $results ) {
  40. if ( empty( $results ) ) {
  41. $results = new \stdClass();
  42. $results->total = 0;
  43. $results->totals = new \stdClass();
  44. $results->totals->tax_codes = 0;
  45. $results->totals->total_tax = 0;
  46. $results->totals->order_tax = 0;
  47. $results->totals->shipping_tax = 0;
  48. $results->totals->orders = 0;
  49. $results->intervals = array();
  50. $results->pages = 1;
  51. $results->page_no = 1;
  52. }
  53. return $results;
  54. }
  55. /**
  56. * Maps query arguments from the REST request.
  57. *
  58. * @param array $request Request array.
  59. * @return array
  60. */
  61. protected function prepare_reports_query( $request ) {
  62. $args = array();
  63. $args['before'] = $request['before'];
  64. $args['after'] = $request['after'];
  65. $args['interval'] = $request['interval'];
  66. $args['page'] = $request['page'];
  67. $args['per_page'] = $request['per_page'];
  68. $args['orderby'] = $request['orderby'];
  69. $args['order'] = $request['order'];
  70. $args['taxes'] = (array) $request['taxes'];
  71. $args['segmentby'] = $request['segmentby'];
  72. $args['fields'] = $request['fields'];
  73. return $args;
  74. }
  75. /**
  76. * Get all reports.
  77. *
  78. * @param WP_REST_Request $request Request data.
  79. * @return array|WP_Error
  80. */
  81. public function get_items( $request ) {
  82. $query_args = $this->prepare_reports_query( $request );
  83. $taxes_query = new Query( $query_args );
  84. $report_data = $taxes_query->get_data();
  85. $out_data = array(
  86. 'totals' => get_object_vars( $report_data->totals ),
  87. 'intervals' => array(),
  88. );
  89. foreach ( $report_data->intervals as $interval_data ) {
  90. $item = $this->prepare_item_for_response( (object) $interval_data, $request );
  91. $out_data['intervals'][] = $this->prepare_response_for_collection( $item );
  92. }
  93. $response = rest_ensure_response( $out_data );
  94. $response->header( 'X-WP-Total', (int) $report_data->total );
  95. $response->header( 'X-WP-TotalPages', (int) $report_data->pages );
  96. $page = $report_data->page_no;
  97. $max_pages = $report_data->pages;
  98. $base = add_query_arg( $request->get_query_params(), rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ) );
  99. if ( $page > 1 ) {
  100. $prev_page = $page - 1;
  101. if ( $prev_page > $max_pages ) {
  102. $prev_page = $max_pages;
  103. }
  104. $prev_link = add_query_arg( 'page', $prev_page, $base );
  105. $response->link_header( 'prev', $prev_link );
  106. }
  107. if ( $max_pages > $page ) {
  108. $next_page = $page + 1;
  109. $next_link = add_query_arg( 'page', $next_page, $base );
  110. $response->link_header( 'next', $next_link );
  111. }
  112. return $response;
  113. }
  114. /**
  115. * Prepare a report object for serialization.
  116. *
  117. * @param stdClass $report Report data.
  118. * @param WP_REST_Request $request Request object.
  119. * @return WP_REST_Response
  120. */
  121. public function prepare_item_for_response( $report, $request ) {
  122. $data = get_object_vars( $report );
  123. $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
  124. $data = $this->add_additional_fields_to_object( $data, $request );
  125. $data = $this->filter_response_by_context( $data, $context );
  126. // Wrap the data in a response object.
  127. $response = rest_ensure_response( $data );
  128. /**
  129. * Filter a report returned from the API.
  130. *
  131. * Allows modification of the report data right before it is returned.
  132. *
  133. * @param WP_REST_Response $response The response object.
  134. * @param object $report The original report object.
  135. * @param WP_REST_Request $request Request used to generate the response.
  136. */
  137. return apply_filters( 'woocommerce_rest_prepare_report_taxes_stats', $response, $report, $request );
  138. }
  139. /**
  140. * Get the Report's schema, conforming to JSON Schema.
  141. *
  142. * @return array
  143. */
  144. public function get_item_schema() {
  145. $data_values = array(
  146. 'total_tax' => array(
  147. 'description' => __( 'Total tax.', 'woocommerce' ),
  148. 'type' => 'number',
  149. 'context' => array( 'view', 'edit' ),
  150. 'readonly' => true,
  151. 'indicator' => true,
  152. 'format' => 'currency',
  153. ),
  154. 'order_tax' => array(
  155. 'description' => __( 'Order tax.', 'woocommerce' ),
  156. 'type' => 'number',
  157. 'context' => array( 'view', 'edit' ),
  158. 'readonly' => true,
  159. 'indicator' => true,
  160. 'format' => 'currency',
  161. ),
  162. 'shipping_tax' => array(
  163. 'description' => __( 'Shipping tax.', 'woocommerce' ),
  164. 'type' => 'number',
  165. 'context' => array( 'view', 'edit' ),
  166. 'readonly' => true,
  167. 'indicator' => true,
  168. 'format' => 'currency',
  169. ),
  170. 'orders_count' => array(
  171. 'description' => __( 'Number of orders.', 'woocommerce' ),
  172. 'type' => 'integer',
  173. 'context' => array( 'view', 'edit' ),
  174. 'readonly' => true,
  175. ),
  176. 'tax_codes' => array(
  177. 'description' => __( 'Amount of tax codes.', 'woocommerce' ),
  178. 'type' => 'integer',
  179. 'context' => array( 'view', 'edit' ),
  180. 'readonly' => true,
  181. ),
  182. );
  183. $segments = array(
  184. 'segments' => array(
  185. 'description' => __( 'Reports data grouped by segment condition.', 'woocommerce' ),
  186. 'type' => 'array',
  187. 'context' => array( 'view', 'edit' ),
  188. 'readonly' => true,
  189. 'items' => array(
  190. 'type' => 'object',
  191. 'properties' => array(
  192. 'segment_id' => array(
  193. 'description' => __( 'Segment identificator.', 'woocommerce' ),
  194. 'type' => 'integer',
  195. 'context' => array( 'view', 'edit' ),
  196. 'readonly' => true,
  197. ),
  198. 'subtotals' => array(
  199. 'description' => __( 'Interval subtotals.', 'woocommerce' ),
  200. 'type' => 'object',
  201. 'context' => array( 'view', 'edit' ),
  202. 'readonly' => true,
  203. 'properties' => $data_values,
  204. ),
  205. ),
  206. ),
  207. ),
  208. );
  209. $totals = array_merge( $data_values, $segments );
  210. $schema = array(
  211. '$schema' => 'http://json-schema.org/draft-04/schema#',
  212. 'title' => 'report_taxes_stats',
  213. 'type' => 'object',
  214. 'properties' => array(
  215. 'totals' => array(
  216. 'description' => __( 'Totals data.', 'woocommerce' ),
  217. 'type' => 'object',
  218. 'context' => array( 'view', 'edit' ),
  219. 'readonly' => true,
  220. 'properties' => $totals,
  221. ),
  222. 'intervals' => array(
  223. 'description' => __( 'Reports data grouped by intervals.', 'woocommerce' ),
  224. 'type' => 'array',
  225. 'context' => array( 'view', 'edit' ),
  226. 'readonly' => true,
  227. 'items' => array(
  228. 'type' => 'object',
  229. 'properties' => array(
  230. 'interval' => array(
  231. 'description' => __( 'Type of interval.', 'woocommerce' ),
  232. 'type' => 'string',
  233. 'context' => array( 'view', 'edit' ),
  234. 'readonly' => true,
  235. 'enum' => array( 'day', 'week', 'month', 'year' ),
  236. ),
  237. 'date_start' => array(
  238. 'description' => __( "The date the report start, in the site's timezone.", 'woocommerce' ),
  239. 'type' => 'date-time',
  240. 'context' => array( 'view', 'edit' ),
  241. 'readonly' => true,
  242. ),
  243. 'date_start_gmt' => array(
  244. 'description' => __( 'The date the report start, as GMT.', 'woocommerce' ),
  245. 'type' => 'date-time',
  246. 'context' => array( 'view', 'edit' ),
  247. 'readonly' => true,
  248. ),
  249. 'date_end' => array(
  250. 'description' => __( "The date the report end, in the site's timezone.", 'woocommerce' ),
  251. 'type' => 'date-time',
  252. 'context' => array( 'view', 'edit' ),
  253. 'readonly' => true,
  254. ),
  255. 'date_end_gmt' => array(
  256. 'description' => __( 'The date the report end, as GMT.', 'woocommerce' ),
  257. 'type' => 'date-time',
  258. 'context' => array( 'view', 'edit' ),
  259. 'readonly' => true,
  260. ),
  261. 'subtotals' => array(
  262. 'description' => __( 'Interval subtotals.', 'woocommerce' ),
  263. 'type' => 'object',
  264. 'context' => array( 'view', 'edit' ),
  265. 'readonly' => true,
  266. 'properties' => $totals,
  267. ),
  268. ),
  269. ),
  270. ),
  271. ),
  272. );
  273. return $this->add_additional_fields_schema( $schema );
  274. }
  275. /**
  276. * Get the query params for collections.
  277. *
  278. * @return array
  279. */
  280. public function get_collection_params() {
  281. $params = array();
  282. $params['context'] = $this->get_context_param( array( 'default' => 'view' ) );
  283. $params['page'] = array(
  284. 'description' => __( 'Current page of the collection.', 'woocommerce' ),
  285. 'type' => 'integer',
  286. 'default' => 1,
  287. 'sanitize_callback' => 'absint',
  288. 'validate_callback' => 'rest_validate_request_arg',
  289. 'minimum' => 1,
  290. );
  291. $params['per_page'] = array(
  292. 'description' => __( 'Maximum number of items to be returned in result set.', 'woocommerce' ),
  293. 'type' => 'integer',
  294. 'default' => 10,
  295. 'minimum' => 1,
  296. 'maximum' => 100,
  297. 'sanitize_callback' => 'absint',
  298. 'validate_callback' => 'rest_validate_request_arg',
  299. );
  300. $params['after'] = array(
  301. 'description' => __( 'Limit response to resources published after a given ISO8601 compliant date.', 'woocommerce' ),
  302. 'type' => 'string',
  303. 'format' => 'date-time',
  304. 'validate_callback' => 'rest_validate_request_arg',
  305. );
  306. $params['before'] = array(
  307. 'description' => __( 'Limit response to resources published before a given ISO8601 compliant date.', 'woocommerce' ),
  308. 'type' => 'string',
  309. 'format' => 'date-time',
  310. 'validate_callback' => 'rest_validate_request_arg',
  311. );
  312. $params['order'] = array(
  313. 'description' => __( 'Order sort attribute ascending or descending.', 'woocommerce' ),
  314. 'type' => 'string',
  315. 'default' => 'desc',
  316. 'enum' => array( 'asc', 'desc' ),
  317. 'validate_callback' => 'rest_validate_request_arg',
  318. );
  319. $params['orderby'] = array(
  320. 'description' => __( 'Sort collection by object attribute.', 'woocommerce' ),
  321. 'type' => 'string',
  322. 'default' => 'date',
  323. 'enum' => array(
  324. 'date',
  325. 'items_sold',
  326. 'total_sales',
  327. 'orders_count',
  328. 'products_count',
  329. ),
  330. 'validate_callback' => 'rest_validate_request_arg',
  331. );
  332. $params['interval'] = array(
  333. 'description' => __( 'Time interval to use for buckets in the returned data.', 'woocommerce' ),
  334. 'type' => 'string',
  335. 'default' => 'week',
  336. 'enum' => array(
  337. 'hour',
  338. 'day',
  339. 'week',
  340. 'month',
  341. 'quarter',
  342. 'year',
  343. ),
  344. 'validate_callback' => 'rest_validate_request_arg',
  345. );
  346. $params['taxes'] = array(
  347. 'description' => __( 'Limit result set to all items that have the specified term assigned in the taxes taxonomy.', 'woocommerce' ),
  348. 'type' => 'array',
  349. 'sanitize_callback' => 'wp_parse_id_list',
  350. 'validate_callback' => 'rest_validate_request_arg',
  351. 'items' => array(
  352. 'type' => 'integer',
  353. ),
  354. );
  355. $params['segmentby'] = array(
  356. 'description' => __( 'Segment the response by additional constraint.', 'woocommerce' ),
  357. 'type' => 'string',
  358. 'enum' => array(
  359. 'tax_rate_id',
  360. ),
  361. 'validate_callback' => 'rest_validate_request_arg',
  362. );
  363. $params['fields'] = array(
  364. 'description' => __( 'Limit stats fields to the specified items.', 'woocommerce' ),
  365. 'type' => 'array',
  366. 'sanitize_callback' => 'wp_parse_slug_list',
  367. 'validate_callback' => 'rest_validate_request_arg',
  368. 'items' => array(
  369. 'type' => 'string',
  370. ),
  371. );
  372. return $params;
  373. }
  374. }