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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. /**
  3. * REST API Settings controller
  4. *
  5. * Handles requests to the /settings endpoints.
  6. *
  7. * @package WooCommerce/API
  8. * @since 3.0.0
  9. */
  10. defined( 'ABSPATH' ) || exit;
  11. /**
  12. * REST API Settings controller class.
  13. *
  14. * @package WooCommerce/API
  15. * @extends WC_REST_Controller
  16. */
  17. class WC_REST_Settings_Controller extends WC_REST_Controller {
  18. /**
  19. * WP REST API namespace/version.
  20. *
  21. * @var string
  22. */
  23. protected $namespace = 'wc/v2';
  24. /**
  25. * Route base.
  26. *
  27. * @var string
  28. */
  29. protected $rest_base = 'settings';
  30. /**
  31. * Register routes.
  32. *
  33. * @since 3.0.0
  34. */
  35. public function register_routes() {
  36. register_rest_route(
  37. $this->namespace, '/' . $this->rest_base, array(
  38. array(
  39. 'methods' => WP_REST_Server::READABLE,
  40. 'callback' => array( $this, 'get_items' ),
  41. 'permission_callback' => array( $this, 'get_items_permissions_check' ),
  42. ),
  43. 'schema' => array( $this, 'get_public_item_schema' ),
  44. )
  45. );
  46. }
  47. /**
  48. * Get all settings groups items.
  49. *
  50. * @since 3.0.0
  51. * @param WP_REST_Request $request Request data.
  52. * @return WP_Error|WP_REST_Response
  53. */
  54. public function get_items( $request ) {
  55. $groups = apply_filters( 'woocommerce_settings_groups', array() );
  56. if ( empty( $groups ) ) {
  57. return new WP_Error( 'rest_setting_groups_empty', __( 'No setting groups have been registered.', 'woocommerce' ), array( 'status' => 500 ) );
  58. }
  59. $defaults = $this->group_defaults();
  60. $filtered_groups = array();
  61. foreach ( $groups as $group ) {
  62. $sub_groups = array();
  63. foreach ( $groups as $_group ) {
  64. if ( ! empty( $_group['parent_id'] ) && $group['id'] === $_group['parent_id'] ) {
  65. $sub_groups[] = $_group['id'];
  66. }
  67. }
  68. $group['sub_groups'] = $sub_groups;
  69. $group = wp_parse_args( $group, $defaults );
  70. if ( ! is_null( $group['id'] ) && ! is_null( $group['label'] ) ) {
  71. $group_obj = $this->filter_group( $group );
  72. $group_data = $this->prepare_item_for_response( $group_obj, $request );
  73. $group_data = $this->prepare_response_for_collection( $group_data );
  74. $filtered_groups[] = $group_data;
  75. }
  76. }
  77. $response = rest_ensure_response( $filtered_groups );
  78. return $response;
  79. }
  80. /**
  81. * Prepare links for the request.
  82. *
  83. * @param string $group_id Group ID.
  84. * @return array Links for the given group.
  85. */
  86. protected function prepare_links( $group_id ) {
  87. $base = '/' . $this->namespace . '/' . $this->rest_base;
  88. $links = array(
  89. 'options' => array(
  90. 'href' => rest_url( trailingslashit( $base ) . $group_id ),
  91. ),
  92. );
  93. return $links;
  94. }
  95. /**
  96. * Prepare a report sales object for serialization.
  97. *
  98. * @since 3.0.0
  99. * @param array $item Group object.
  100. * @param WP_REST_Request $request Request object.
  101. * @return WP_REST_Response $response Response data.
  102. */
  103. public function prepare_item_for_response( $item, $request ) {
  104. $context = empty( $request['context'] ) ? 'view' : $request['context'];
  105. $data = $this->add_additional_fields_to_object( $item, $request );
  106. $data = $this->filter_response_by_context( $data, $context );
  107. $response = rest_ensure_response( $data );
  108. $response->add_links( $this->prepare_links( $item['id'] ) );
  109. return $response;
  110. }
  111. /**
  112. * Filters out bad values from the groups array/filter so we
  113. * only return known values via the API.
  114. *
  115. * @since 3.0.0
  116. * @param array $group Group.
  117. * @return array
  118. */
  119. public function filter_group( $group ) {
  120. return array_intersect_key(
  121. $group,
  122. array_flip( array_filter( array_keys( $group ), array( $this, 'allowed_group_keys' ) ) )
  123. );
  124. }
  125. /**
  126. * Callback for allowed keys for each group response.
  127. *
  128. * @since 3.0.0
  129. * @param string $key Key to check.
  130. * @return boolean
  131. */
  132. public function allowed_group_keys( $key ) {
  133. return in_array( $key, array( 'id', 'label', 'description', 'parent_id', 'sub_groups' ) );
  134. }
  135. /**
  136. * Returns default settings for groups. null means the field is required.
  137. *
  138. * @since 3.0.0
  139. * @return array
  140. */
  141. protected function group_defaults() {
  142. return array(
  143. 'id' => null,
  144. 'label' => null,
  145. 'description' => '',
  146. 'parent_id' => '',
  147. 'sub_groups' => array(),
  148. );
  149. }
  150. /**
  151. * Makes sure the current user has access to READ the settings APIs.
  152. *
  153. * @since 3.0.0
  154. * @param WP_REST_Request $request Full data about the request.
  155. * @return WP_Error|boolean
  156. */
  157. public function get_items_permissions_check( $request ) {
  158. if ( ! wc_rest_check_manager_permissions( 'settings', 'read' ) ) {
  159. return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
  160. }
  161. return true;
  162. }
  163. /**
  164. * Get the groups schema, conforming to JSON Schema.
  165. *
  166. * @since 3.0.0
  167. * @return array
  168. */
  169. public function get_item_schema() {
  170. $schema = array(
  171. '$schema' => 'http://json-schema.org/draft-04/schema#',
  172. 'title' => 'setting_group',
  173. 'type' => 'object',
  174. 'properties' => array(
  175. 'id' => array(
  176. 'description' => __( 'A unique identifier that can be used to link settings together.', 'woocommerce' ),
  177. 'type' => 'string',
  178. 'context' => array( 'view' ),
  179. 'readonly' => true,
  180. ),
  181. 'label' => array(
  182. 'description' => __( 'A human readable label for the setting used in interfaces.', 'woocommerce' ),
  183. 'type' => 'string',
  184. 'context' => array( 'view' ),
  185. 'readonly' => true,
  186. ),
  187. 'description' => array(
  188. 'description' => __( 'A human readable description for the setting used in interfaces.', 'woocommerce' ),
  189. 'type' => 'string',
  190. 'context' => array( 'view' ),
  191. 'readonly' => true,
  192. ),
  193. 'parent_id' => array(
  194. 'description' => __( 'ID of parent grouping.', 'woocommerce' ),
  195. 'type' => 'string',
  196. 'context' => array( 'view' ),
  197. 'readonly' => true,
  198. ),
  199. 'sub_groups' => array(
  200. 'description' => __( 'IDs for settings sub groups.', 'woocommerce' ),
  201. 'type' => 'string',
  202. 'context' => array( 'view' ),
  203. 'readonly' => true,
  204. ),
  205. ),
  206. );
  207. return $this->add_additional_fields_schema( $schema );
  208. }
  209. }