WC_REST_Controller

Abstract Rest Controller Class.

Defined (1)

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

/includes/abstracts/abstract-wc-rest-controller.php  
  1. abstract class WC_REST_Controller extends WP_REST_Controller { 
  2.  
  3. /** 
  4. * Endpoint namespace. 
  5. * @var string 
  6. */ 
  7. protected $namespace = 'wc/v1'; 
  8.  
  9. /** 
  10. * Route base. 
  11. * @var string 
  12. */ 
  13. protected $rest_base = ''; 
  14.  
  15. /** 
  16. * Add the schema from additional fields to an schema array. 
  17. * The type of object is inferred from the passed schema. 
  18. * @param array $schema Schema array. 
  19. */ 
  20. protected function add_additional_fields_schema( $schema ) { 
  21. if ( empty( $schema['title'] ) ) { 
  22. return $schema; 
  23.  
  24. /** 
  25. * Can't use $this->get_object_type otherwise we cause an inf loop. 
  26. */ 
  27. $object_type = $schema['title']; 
  28.  
  29. $additional_fields = $this->get_additional_fields( $object_type ); 
  30.  
  31. foreach ( $additional_fields as $field_name => $field_options ) { 
  32. if ( ! $field_options['schema'] ) { 
  33. continue; 
  34.  
  35. $schema['properties'][ $field_name ] = $field_options['schema']; 
  36.  
  37. $schema['properties'] = apply_filters( 'woocommerce_rest_' . $object_type . '_schema', $schema['properties'] ); 
  38.  
  39. return $schema; 
  40.  
  41. /** 
  42. * Get normalized rest base. 
  43. * @return string 
  44. */ 
  45. protected function get_normalized_rest_base() { 
  46. return preg_replace( '/\(.*\)\//i', '', $this->rest_base ); 
  47.  
  48. /** 
  49. * Check batch limit. 
  50. * @param array $items Request items. 
  51. * @return bool|WP_Error 
  52. */ 
  53. protected function check_batch_limit( $items ) { 
  54. $limit = apply_filters( 'woocommerce_rest_batch_items_limit', 100, $this->get_normalized_rest_base() ); 
  55. $total = 0; 
  56.  
  57. if ( ! empty( $items['create'] ) ) { 
  58. $total += count( $items['create'] ); 
  59.  
  60. if ( ! empty( $items['update'] ) ) { 
  61. $total += count( $items['update'] ); 
  62.  
  63. if ( ! empty( $items['delete'] ) ) { 
  64. $total += count( $items['delete'] ); 
  65.  
  66. if ( $total > $limit ) { 
  67. /** translators: %s: items limit */ 
  68. return new WP_Error( 'woocommerce_rest_request_entity_too_large', sprintf( __( 'Unable to accept more than %s items for this request.', 'woocommerce' ), $limit ), array( 'status' => 413 ) ); 
  69.  
  70. return true; 
  71.  
  72. /** 
  73. * Bulk create, update and delete items. 
  74. * @param WP_REST_Request $request Full details about the request. 
  75. * @return array Of WP_Error or WP_REST_Response. 
  76. */ 
  77. public function batch_items( $request ) { 
  78. /** @var WP_REST_Server $wp_rest_server */ 
  79. global $wp_rest_server; 
  80.  
  81. // Get the request params. 
  82. $items = array_filter( $request->get_params() ); 
  83. $response = array(); 
  84.  
  85. // Check batch limit. 
  86. $limit = $this->check_batch_limit( $items ); 
  87. if ( is_wp_error( $limit ) ) { 
  88. return $limit; 
  89.  
  90. if ( ! empty( $items['create'] ) ) { 
  91. foreach ( $items['create'] as $item ) { 
  92. $_item = new WP_REST_Request( 'POST' ); 
  93.  
  94. // Default parameters. 
  95. $defaults = array(); 
  96. $schema = $this->get_public_item_schema(); 
  97. foreach ( $schema['properties'] as $arg => $options ) { 
  98. if ( isset( $options['default'] ) ) { 
  99. $defaults[ $arg ] = $options['default']; 
  100. $_item->set_default_params( $defaults ); 
  101.  
  102. // Set request parameters. 
  103. $_item->set_body_params( $item ); 
  104. $_response = $this->create_item( $_item ); 
  105.  
  106. if ( is_wp_error( $_response ) ) { 
  107. $response['create'][] = array( 
  108. 'id' => 0,  
  109. 'error' => array( 'code' => $_response->get_error_code(), 'message' => $_response->get_error_message(), 'data' => $_response->get_error_data() ),  
  110. ); 
  111. } else { 
  112. $response['create'][] = $wp_rest_server->response_to_data( $_response, '' ); 
  113.  
  114. if ( ! empty( $items['update'] ) ) { 
  115. foreach ( $items['update'] as $item ) { 
  116. $_item = new WP_REST_Request( 'PUT' ); 
  117. $_item->set_body_params( $item ); 
  118. $_response = $this->update_item( $_item ); 
  119.  
  120. if ( is_wp_error( $_response ) ) { 
  121. $response['update'][] = array( 
  122. 'id' => $item['id'],  
  123. 'error' => array( 'code' => $_response->get_error_code(), 'message' => $_response->get_error_message(), 'data' => $_response->get_error_data() ),  
  124. ); 
  125. } else { 
  126. $response['update'][] = $wp_rest_server->response_to_data( $_response, '' ); 
  127.  
  128. if ( ! empty( $items['delete'] ) ) { 
  129. foreach ( $items['delete'] as $id ) { 
  130. $id = (int) $id; 
  131.  
  132. if ( 0 === $id ) { 
  133. continue; 
  134.  
  135. $_item = new WP_REST_Request( 'DELETE' ); 
  136. $_item->set_query_params( array( 'id' => $id, 'force' => true ) ); 
  137. $_response = $this->delete_item( $_item ); 
  138.  
  139. if ( is_wp_error( $_response ) ) { 
  140. $response['delete'][] = array( 
  141. 'id' => $id,  
  142. 'error' => array( 'code' => $_response->get_error_code(), 'message' => $_response->get_error_message(), 'data' => $_response->get_error_data() ),  
  143. ); 
  144. } else { 
  145. $response['delete'][] = $wp_rest_server->response_to_data( $_response, '' ); 
  146.  
  147. return $response; 
  148.  
  149. /** 
  150. * Validate a text value for a text based setting. 
  151. * @since 3.0.0 
  152. * @param string $value 
  153. * @param array $setting 
  154. * @return string 
  155. */ 
  156. public function validate_setting_text_field( $value, $setting ) { 
  157. $value = is_null( $value ) ? '' : $value; 
  158. return wp_kses_post( trim( stripslashes( $value ) ) ); 
  159. return $value; 
  160.  
  161. /** 
  162. * Validate select based settings. 
  163. * @since 3.0.0 
  164. * @param string $value 
  165. * @param array $setting 
  166. * @return string|WP_Error 
  167. */ 
  168. public function validate_setting_select_field( $value, $setting ) { 
  169. if ( array_key_exists( $value, $setting['options'] ) ) { 
  170. return $value; 
  171. } else { 
  172. return new WP_Error( 'rest_setting_value_invalid', __( 'An invalid setting value was passed.', 'woocommerce' ), array( 'status' => 400 ) ); 
  173.  
  174. /** 
  175. * Validate multiselect based settings. 
  176. * @since 3.0.0 
  177. * @param array $values 
  178. * @param array $setting 
  179. * @return string|WP_Error 
  180. */ 
  181. public function validate_setting_multiselect_field( $values, $setting ) { 
  182. if ( empty( $values ) ) { 
  183. return array(); 
  184.  
  185. if ( ! is_array( $values ) ) { 
  186. return new WP_Error( 'rest_setting_value_invalid', __( 'An invalid setting value was passed.', 'woocommerce' ), array( 'status' => 400 ) ); 
  187.  
  188. $final_values = array(); 
  189. foreach ( $values as $value ) { 
  190. if ( array_key_exists( $value, $setting['options'] ) ) { 
  191. $final_values[] = $value; 
  192.  
  193. return $final_values; 
  194.  
  195. /** 
  196. * Validate image_width based settings. 
  197. * @since 3.0.0 
  198. * @param array $value 
  199. * @param array $setting 
  200. * @return string|WP_Error 
  201. */ 
  202. public function validate_setting_image_width_field( $values, $setting ) { 
  203. if ( ! is_array( $values ) ) { 
  204. return new WP_Error( 'rest_setting_value_invalid', __( 'An invalid setting value was passed.', 'woocommerce' ), array( 'status' => 400 ) ); 
  205.  
  206. $current = $setting['value']; 
  207. if ( isset( $values['width'] ) ) { 
  208. $current['width'] = intval( $values['width'] ); 
  209. if ( isset( $values['height'] ) ) { 
  210. $current['height'] = intval( $values['height'] ); 
  211. if ( isset( $values['crop'] ) ) { 
  212. $current['crop'] = (bool) $values['crop']; 
  213. return $current; 
  214.  
  215. /** 
  216. * Validate radio based settings. 
  217. * @since 3.0.0 
  218. * @param string $value 
  219. * @param array $setting 
  220. * @return string|WP_Error 
  221. */ 
  222. public function validate_setting_radio_field( $value, $setting ) { 
  223. return $this->validate_setting_select_field( $value, $setting ); 
  224.  
  225. /** 
  226. * Validate checkbox based settings. 
  227. * @since 3.0.0 
  228. * @param string $value 
  229. * @param array $setting 
  230. * @return string|WP_Error 
  231. */ 
  232. public function validate_setting_checkbox_field( $value, $setting ) { 
  233. if ( in_array( $value, array( 'yes', 'no' ) ) ) { 
  234. return $value; 
  235. } elseif ( empty( $value ) ) { 
  236. $value = isset( $setting['default'] ) ? $setting['default'] : 'no'; 
  237. return $value; 
  238. } else { 
  239. return new WP_Error( 'rest_setting_value_invalid', __( 'An invalid setting value was passed.', 'woocommerce' ), array( 'status' => 400 ) ); 
  240.  
  241. /** 
  242. * Validate textarea based settings. 
  243. * @since 3.0.0 
  244. * @param string $value 
  245. * @param array $setting 
  246. * @return string 
  247. */ 
  248. public function validate_setting_textarea_field( $value, $setting ) { 
  249. $value = is_null( $value ) ? '' : $value; 
  250. return wp_kses( trim( stripslashes( $value ) ),  
  251. array_merge( 
  252. array( 
  253. 'iframe' => array( 'src' => true, 'style' => true, 'id' => true, 'class' => true ),  
  254. ),  
  255. wp_kses_allowed_html( 'post' ) 
  256. ); 
  257.  
  258. /** 
  259. * Add meta query. 
  260. * @since 3.0.0 
  261. * @param array $args Query args. 
  262. * @param array $meta_query Meta query. 
  263. * @return array 
  264. */ 
  265. protected function add_meta_query( $args, $meta_query ) { 
  266. if ( ! empty( $args['meta_query'] ) ) { 
  267. $args['meta_query'] = array(); 
  268.  
  269. $args['meta_query'][] = $meta_query; 
  270.  
  271. return $args['meta_query']; 
  272.  
  273. /** 
  274. * Get the batch schema, conforming to JSON Schema. 
  275. * @return array 
  276. */ 
  277. public function get_public_batch_schema() { 
  278. $schema = array( 
  279. '$schema' => 'http://json-schema.org/draft-04/schema#',  
  280. 'title' => 'batch',  
  281. 'type' => 'object',  
  282. 'properties' => array( 
  283. 'create' => array( 
  284. 'description' => __( 'List of created resources.', 'woocommerce' ),  
  285. 'type' => 'array',  
  286. 'context' => array( 'view', 'edit' ),  
  287. 'items' => array( 
  288. 'type' => 'object',  
  289. ),  
  290. ),  
  291. 'update' => array( 
  292. 'description' => __( 'List of updated resources.', 'woocommerce' ),  
  293. 'type' => 'array',  
  294. 'context' => array( 'view', 'edit' ),  
  295. 'items' => array( 
  296. 'type' => 'object',  
  297. ),  
  298. ),  
  299. 'delete' => array( 
  300. 'description' => __( 'List of delete resources.', 'woocommerce' ),  
  301. 'type' => 'array',  
  302. 'context' => array( 'view', 'edit' ),  
  303. 'items' => array( 
  304. 'type' => 'integer',  
  305. ),  
  306. ),  
  307. ),  
  308. ); 
  309.  
  310. return $schema;