WC_REST_Settings_Controller

REST API Settings controller class.

Defined (1)

The class is defined in the following location(s).

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