class-wc-rest-network-orders-controller.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. * REST API Network Orders controller
  4. *
  5. * Handles requests to the /orders/network endpoint
  6. *
  7. * @package WooCommerce/API
  8. * @since 3.4.0
  9. */
  10. defined( 'ABSPATH' ) || exit;
  11. /**
  12. * REST API Network Orders controller class.
  13. *
  14. * @package WooCommerce/API
  15. * @extends WC_REST_Orders_Controller
  16. */
  17. class WC_REST_Network_Orders_Controller extends WC_REST_Orders_Controller {
  18. /**
  19. * Register the routes for network orders.
  20. */
  21. public function register_routes() {
  22. if ( is_multisite() ) {
  23. register_rest_route(
  24. $this->namespace, '/' . $this->rest_base . '/network', array(
  25. array(
  26. 'methods' => WP_REST_Server::READABLE,
  27. 'callback' => array( $this, 'network_orders' ),
  28. 'permission_callback' => array( $this, 'network_orders_permissions_check' ),
  29. 'args' => $this->get_collection_params(),
  30. ),
  31. 'schema' => array( $this, 'get_public_item_schema' ),
  32. )
  33. );
  34. }
  35. }
  36. /**
  37. * Retrieves the item's schema for display / public consumption purposes.
  38. *
  39. * @access public
  40. *
  41. * @return array Public item schema data.
  42. */
  43. public function get_public_item_schema() {
  44. $schema = parent::get_public_item_schema();
  45. $schema['properties']['blog'] = array(
  46. 'description' => __( 'Blog id of the record on the multisite.', 'woocommerce' ),
  47. 'type' => 'integer',
  48. 'context' => array( 'view' ),
  49. 'readonly' => true,
  50. );
  51. $schema['properties']['edit_url'] = array(
  52. 'description' => __( 'URL to edit the order', 'woocommerce' ),
  53. 'type' => 'string',
  54. 'context' => array( 'view' ),
  55. 'readonly' => true,
  56. );
  57. $schema['properties']['customer'][] = array(
  58. 'description' => __( 'Name of the customer for the order', 'woocommerce' ),
  59. 'type' => 'string',
  60. 'context' => array( 'view' ),
  61. 'readonly' => true,
  62. );
  63. $schema['properties']['status_name'][] = array(
  64. 'description' => __( 'Order Status', 'woocommerce' ),
  65. 'type' => 'string',
  66. 'context' => array( 'view' ),
  67. 'readonly' => true,
  68. );
  69. $schema['properties']['formatted_total'][] = array(
  70. 'description' => __( 'Order total formatted for locale', 'woocommerce' ),
  71. 'type' => 'string',
  72. 'context' => array( 'view' ),
  73. 'readonly' => true,
  74. );
  75. return $schema;
  76. }
  77. /**
  78. * Does a permissions check for the proper requested blog
  79. *
  80. * @param WP_REST_Request $request Full details about the request.
  81. *
  82. * @return bool $permission
  83. */
  84. public function network_orders_permissions_check( $request ) {
  85. $blog_id = $request->get_param( 'blog_id' );
  86. $blog_id = ! empty( $blog_id ) ? $blog_id : get_current_blog_id();
  87. switch_to_blog( $blog_id );
  88. $permission = $this->get_items_permissions_check( $request );
  89. restore_current_blog();
  90. return $permission;
  91. }
  92. /**
  93. * Get a collection of orders from the requested blog id
  94. *
  95. * @param WP_REST_Request $request Full details about the request.
  96. *
  97. * @return WP_REST_Response
  98. */
  99. public function network_orders( $request ) {
  100. $blog_id = $request->get_param( 'blog_id' );
  101. $blog_id = ! empty( $blog_id ) ? $blog_id : get_current_blog_id();
  102. switch_to_blog( $blog_id );
  103. add_filter( 'woocommerce_rest_orders_prepare_object_query', array( $this, 'network_orders_filter_args' ) );
  104. $items = $this->get_items( $request );
  105. remove_filter( 'woocommerce_rest_orders_prepare_object_query', array( $this, 'network_orders_filter_args' ) );
  106. foreach ( $items->data as &$current_order ) {
  107. $order = wc_get_order( $current_order['id'] );
  108. $current_order['blog'] = get_blog_details( get_current_blog_id() );
  109. $current_order['edit_url'] = get_admin_url( $blog_id, 'post.php?post=' . absint( $order->get_id() ) . '&action=edit' );
  110. /* translators: 1: first name 2: last name */
  111. $current_order['customer'] = trim( sprintf( _x( '%1$s %2$s', 'full name', 'woocommerce' ), $order->get_billing_first_name(), $order->get_billing_last_name() ) );
  112. $current_order['status_name'] = wc_get_order_status_name( $order->get_status() );
  113. $current_order['formatted_total'] = $order->get_formatted_order_total();
  114. }
  115. restore_current_blog();
  116. return $items;
  117. }
  118. /**
  119. * Filters the post statuses to on hold and processing for the network order query.
  120. *
  121. * @param array $args Query args.
  122. *
  123. * @return array
  124. */
  125. public function network_orders_filter_args( $args ) {
  126. $args['post_status'] = array(
  127. 'wc-on-hold',
  128. 'wc-processing',
  129. );
  130. return $args;
  131. }
  132. }