abstract-wc-rest-shipping-zones-controller.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * REST API Shipping Zones Controller base
  4. *
  5. * Houses common functionality between Shipping Zones and Locations.
  6. *
  7. * @package WooCommerce/Abstracts
  8. * @since 3.0.0
  9. */
  10. if ( ! defined( 'ABSPATH' ) ) {
  11. exit;
  12. }
  13. /**
  14. * REST API Shipping Zones base class.
  15. *
  16. * @package WooCommerce/API
  17. * @extends WC_REST_Controller
  18. */
  19. abstract class WC_REST_Shipping_Zones_Controller_Base extends WC_REST_Controller {
  20. /**
  21. * Endpoint namespace.
  22. *
  23. * @var string
  24. */
  25. protected $namespace = 'wc/v2';
  26. /**
  27. * Route base.
  28. *
  29. * @var string
  30. */
  31. protected $rest_base = 'shipping/zones';
  32. /**
  33. * Retrieve a Shipping Zone by it's ID.
  34. *
  35. * @param int $zone_id Shipping Zone ID.
  36. * @return WC_Shipping_Zone|WP_Error
  37. */
  38. protected function get_zone( $zone_id ) {
  39. $zone = WC_Shipping_Zones::get_zone_by( 'zone_id', $zone_id );
  40. if ( false === $zone ) {
  41. return new WP_Error( 'woocommerce_rest_shipping_zone_invalid', __( 'Resource does not exist.', 'woocommerce' ), array( 'status' => 404 ) );
  42. }
  43. return $zone;
  44. }
  45. /**
  46. * Check whether a given request has permission to read Shipping Zones.
  47. *
  48. * @param WP_REST_Request $request Full details about the request.
  49. * @return WP_Error|boolean
  50. */
  51. public function get_items_permissions_check( $request ) {
  52. if ( ! wc_shipping_enabled() ) {
  53. return new WP_Error( 'rest_no_route', __( 'Shipping is disabled.', 'woocommerce' ), array( 'status' => 404 ) );
  54. }
  55. if ( ! wc_rest_check_manager_permissions( 'settings', 'read' ) ) {
  56. return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
  57. }
  58. return true;
  59. }
  60. /**
  61. * Check if a given request has access to create Shipping Zones.
  62. *
  63. * @param WP_REST_Request $request Full details about the request.
  64. * @return WP_Error|boolean
  65. */
  66. public function create_item_permissions_check( $request ) {
  67. if ( ! wc_shipping_enabled() ) {
  68. return new WP_Error( 'rest_no_route', __( 'Shipping is disabled.', 'woocommerce' ), array( 'status' => 404 ) );
  69. }
  70. if ( ! wc_rest_check_manager_permissions( 'settings', 'edit' ) ) {
  71. return new WP_Error( 'woocommerce_rest_cannot_create', __( 'Sorry, you are not allowed to create resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
  72. }
  73. return true;
  74. }
  75. /**
  76. * Check whether a given request has permission to edit Shipping Zones.
  77. *
  78. * @param WP_REST_Request $request Full details about the request.
  79. * @return WP_Error|boolean
  80. */
  81. public function update_items_permissions_check( $request ) {
  82. if ( ! wc_shipping_enabled() ) {
  83. return new WP_Error( 'rest_no_route', __( 'Shipping is disabled.', 'woocommerce' ), array( 'status' => 404 ) );
  84. }
  85. if ( ! wc_rest_check_manager_permissions( 'settings', 'edit' ) ) {
  86. return new WP_Error( 'woocommerce_rest_cannot_edit', __( 'Sorry, you are not allowed to edit this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
  87. }
  88. return true;
  89. }
  90. /**
  91. * Check whether a given request has permission to delete Shipping Zones.
  92. *
  93. * @param WP_REST_Request $request Full details about the request.
  94. * @return WP_Error|boolean
  95. */
  96. public function delete_items_permissions_check( $request ) {
  97. if ( ! wc_shipping_enabled() ) {
  98. return new WP_Error( 'rest_no_route', __( 'Shipping is disabled.', 'woocommerce' ), array( 'status' => 404 ) );
  99. }
  100. if ( ! wc_rest_check_manager_permissions( 'settings', 'delete' ) ) {
  101. return new WP_Error( 'woocommerce_rest_cannot_edit', __( 'Sorry, you are not allowed to delete this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
  102. }
  103. return true;
  104. }
  105. }