Нет описания

class-wc-rest-settings-controller.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * REST API Settings controller
  4. *
  5. * Handles requests to the /settings endpoints.
  6. *
  7. * @package WooCommerce\RestApi
  8. * @since 3.0.0
  9. */
  10. defined( 'ABSPATH' ) || exit;
  11. /**
  12. * REST API Settings controller class.
  13. *
  14. * @package WooCommerce\RestApi
  15. * @extends WC_REST_Settings_V2_Controller
  16. */
  17. class WC_REST_Settings_Controller extends WC_REST_Settings_V2_Controller {
  18. /**
  19. * Endpoint namespace.
  20. *
  21. * @var string
  22. */
  23. protected $namespace = 'wc/v3';
  24. /**
  25. * Register routes.
  26. */
  27. public function register_routes() {
  28. parent::register_routes();
  29. register_rest_route( $this->namespace, '/' . $this->rest_base . '/batch', array(
  30. array(
  31. 'methods' => WP_REST_Server::EDITABLE,
  32. 'callback' => array( $this, 'batch_items' ),
  33. 'permission_callback' => array( $this, 'update_items_permissions_check' ),
  34. ),
  35. 'schema' => array( $this, 'get_public_batch_schema' ),
  36. ) );
  37. }
  38. /**
  39. * Makes sure the current user has access to WRITE the settings APIs.
  40. *
  41. * @param WP_REST_Request $request Full data about the request.
  42. * @return WP_Error|bool
  43. */
  44. public function update_items_permissions_check( $request ) {
  45. if ( ! wc_rest_check_manager_permissions( 'settings', 'edit' ) ) {
  46. return new WP_Error( 'woocommerce_rest_cannot_edit', __( 'Sorry, you cannot edit this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
  47. }
  48. return true;
  49. }
  50. /**
  51. * Update a setting.
  52. *
  53. * @param WP_REST_Request $request Request data.
  54. * @return WP_Error|WP_REST_Response
  55. */
  56. public function update_item( $request ) {
  57. $options_controller = new WC_REST_Setting_Options_Controller();
  58. $response = $options_controller->update_item( $request );
  59. return $response;
  60. }
  61. /**
  62. * Get the groups schema, conforming to JSON Schema.
  63. *
  64. * @since 3.0.0
  65. * @return array
  66. */
  67. public function get_item_schema() {
  68. $schema = array(
  69. '$schema' => 'http://json-schema.org/draft-04/schema#',
  70. 'title' => 'setting_group',
  71. 'type' => 'object',
  72. 'properties' => array(
  73. 'id' => array(
  74. 'description' => __( 'A unique identifier that can be used to link settings together.', 'woocommerce' ),
  75. 'type' => 'string',
  76. 'context' => array( 'view', 'edit' ),
  77. ),
  78. 'label' => array(
  79. 'description' => __( 'A human readable label for the setting used in interfaces.', 'woocommerce' ),
  80. 'type' => 'string',
  81. 'context' => array( 'view', 'edit' ),
  82. ),
  83. 'description' => array(
  84. 'description' => __( 'A human readable description for the setting used in interfaces.', 'woocommerce' ),
  85. 'type' => 'string',
  86. 'context' => array( 'view', 'edit' ),
  87. ),
  88. 'parent_id' => array(
  89. 'description' => __( 'ID of parent grouping.', 'woocommerce' ),
  90. 'type' => 'string',
  91. 'context' => array( 'view', 'edit' ),
  92. ),
  93. 'sub_groups' => array(
  94. 'description' => __( 'IDs for settings sub groups.', 'woocommerce' ),
  95. 'type' => 'string',
  96. 'context' => array( 'view', 'edit' ),
  97. ),
  98. ),
  99. );
  100. return $this->add_additional_fields_schema( $schema );
  101. }
  102. }