PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/woocommerce/includes/legacy/api/v1/class-wc-api-resource.php

https://gitlab.com/campus-academy/krowkaramel
PHP | 409 lines | 193 code | 87 blank | 129 comment | 52 complexity | cf9b380e5c797d838db1f90c121291b7 MD5 | raw file
  1. <?php
  2. /**
  3. * WooCommerce API Resource class
  4. *
  5. * Provides shared functionality for resource-specific API classes
  6. *
  7. * @author WooThemes
  8. * @category API
  9. * @package WooCommerce\RestApi
  10. * @since 2.1
  11. * @version 2.1
  12. */
  13. if ( ! defined( 'ABSPATH' ) ) {
  14. exit; // Exit if accessed directly
  15. }
  16. class WC_API_Resource {
  17. /** @var WC_API_Server the API server */
  18. protected $server;
  19. /** @var string sub-classes override this to set a resource-specific base route */
  20. protected $base;
  21. /**
  22. * Setup class
  23. *
  24. * @since 2.1
  25. * @param WC_API_Server $server
  26. */
  27. public function __construct( WC_API_Server $server ) {
  28. $this->server = $server;
  29. // automatically register routes for sub-classes
  30. add_filter( 'woocommerce_api_endpoints', array( $this, 'register_routes' ) );
  31. // remove fields from responses when requests specify certain fields
  32. // note these are hooked at a later priority so data added via filters (e.g. customer data to the order response)
  33. // still has the fields filtered properly
  34. foreach ( array( 'order', 'coupon', 'customer', 'product', 'report' ) as $resource ) {
  35. add_filter( "woocommerce_api_{$resource}_response", array( $this, 'maybe_add_meta' ), 15, 2 );
  36. add_filter( "woocommerce_api_{$resource}_response", array( $this, 'filter_response_fields' ), 20, 3 );
  37. }
  38. }
  39. /**
  40. * Validate the request by checking:
  41. *
  42. * 1) the ID is a valid integer
  43. * 2) the ID returns a valid post object and matches the provided post type
  44. * 3) the current user has the proper permissions to read/edit/delete the post
  45. *
  46. * @since 2.1
  47. * @param string|int $id the post ID
  48. * @param string $type the post type, either `shop_order`, `shop_coupon`, or `product`
  49. * @param string $context the context of the request, either `read`, `edit` or `delete`
  50. * @return int|WP_Error valid post ID or WP_Error if any of the checks fails
  51. */
  52. protected function validate_request( $id, $type, $context ) {
  53. if ( 'shop_order' === $type || 'shop_coupon' === $type ) {
  54. $resource_name = str_replace( 'shop_', '', $type );
  55. } else {
  56. $resource_name = $type;
  57. }
  58. $id = absint( $id );
  59. // validate ID
  60. if ( empty( $id ) ) {
  61. return new WP_Error( "woocommerce_api_invalid_{$resource_name}_id", sprintf( __( 'Invalid %s ID', 'woocommerce' ), $type ), array( 'status' => 404 ) );
  62. }
  63. // only custom post types have per-post type/permission checks
  64. if ( 'customer' !== $type ) {
  65. $post = get_post( $id );
  66. // for checking permissions, product variations are the same as the product post type
  67. $post_type = ( 'product_variation' === $post->post_type ) ? 'product' : $post->post_type;
  68. // validate post type
  69. if ( $type !== $post_type ) {
  70. return new WP_Error( "woocommerce_api_invalid_{$resource_name}", sprintf( __( 'Invalid %s', 'woocommerce' ), $resource_name ), array( 'status' => 404 ) );
  71. }
  72. // validate permissions
  73. switch ( $context ) {
  74. case 'read':
  75. if ( ! $this->is_readable( $post ) ) {
  76. return new WP_Error( "woocommerce_api_user_cannot_read_{$resource_name}", sprintf( __( 'You do not have permission to read this %s', 'woocommerce' ), $resource_name ), array( 'status' => 401 ) );
  77. }
  78. break;
  79. case 'edit':
  80. if ( ! $this->is_editable( $post ) ) {
  81. return new WP_Error( "woocommerce_api_user_cannot_edit_{$resource_name}", sprintf( __( 'You do not have permission to edit this %s', 'woocommerce' ), $resource_name ), array( 'status' => 401 ) );
  82. }
  83. break;
  84. case 'delete':
  85. if ( ! $this->is_deletable( $post ) ) {
  86. return new WP_Error( "woocommerce_api_user_cannot_delete_{$resource_name}", sprintf( __( 'You do not have permission to delete this %s', 'woocommerce' ), $resource_name ), array( 'status' => 401 ) );
  87. }
  88. break;
  89. }
  90. }
  91. return $id;
  92. }
  93. /**
  94. * Add common request arguments to argument list before WP_Query is run
  95. *
  96. * @since 2.1
  97. * @param array $base_args required arguments for the query (e.g. `post_type`, etc)
  98. * @param array $request_args arguments provided in the request
  99. * @return array
  100. */
  101. protected function merge_query_args( $base_args, $request_args ) {
  102. $args = array();
  103. // date
  104. if ( ! empty( $request_args['created_at_min'] ) || ! empty( $request_args['created_at_max'] ) || ! empty( $request_args['updated_at_min'] ) || ! empty( $request_args['updated_at_max'] ) ) {
  105. $args['date_query'] = array();
  106. // resources created after specified date
  107. if ( ! empty( $request_args['created_at_min'] ) ) {
  108. $args['date_query'][] = array( 'column' => 'post_date_gmt', 'after' => $this->server->parse_datetime( $request_args['created_at_min'] ), 'inclusive' => true );
  109. }
  110. // resources created before specified date
  111. if ( ! empty( $request_args['created_at_max'] ) ) {
  112. $args['date_query'][] = array( 'column' => 'post_date_gmt', 'before' => $this->server->parse_datetime( $request_args['created_at_max'] ), 'inclusive' => true );
  113. }
  114. // resources updated after specified date
  115. if ( ! empty( $request_args['updated_at_min'] ) ) {
  116. $args['date_query'][] = array( 'column' => 'post_modified_gmt', 'after' => $this->server->parse_datetime( $request_args['updated_at_min'] ), 'inclusive' => true );
  117. }
  118. // resources updated before specified date
  119. if ( ! empty( $request_args['updated_at_max'] ) ) {
  120. $args['date_query'][] = array( 'column' => 'post_modified_gmt', 'before' => $this->server->parse_datetime( $request_args['updated_at_max'] ), 'inclusive' => true );
  121. }
  122. }
  123. // search
  124. if ( ! empty( $request_args['q'] ) ) {
  125. $args['s'] = $request_args['q'];
  126. }
  127. // resources per response
  128. if ( ! empty( $request_args['limit'] ) ) {
  129. $args['posts_per_page'] = $request_args['limit'];
  130. }
  131. // resource offset
  132. if ( ! empty( $request_args['offset'] ) ) {
  133. $args['offset'] = $request_args['offset'];
  134. }
  135. // resource page
  136. $args['paged'] = ( isset( $request_args['page'] ) ) ? absint( $request_args['page'] ) : 1;
  137. return array_merge( $base_args, $args );
  138. }
  139. /**
  140. * Add meta to resources when requested by the client. Meta is added as a top-level
  141. * `<resource_name>_meta` attribute (e.g. `order_meta`) as a list of key/value pairs
  142. *
  143. * @since 2.1
  144. * @param array $data the resource data
  145. * @param object $resource the resource object (e.g WC_Order)
  146. * @return mixed
  147. */
  148. public function maybe_add_meta( $data, $resource ) {
  149. if ( isset( $this->server->params['GET']['filter']['meta'] ) && 'true' === $this->server->params['GET']['filter']['meta'] && is_object( $resource ) ) {
  150. // don't attempt to add meta more than once
  151. if ( preg_grep( '/[a-z]+_meta/', array_keys( $data ) ) ) {
  152. return $data;
  153. }
  154. // define the top-level property name for the meta
  155. switch ( get_class( $resource ) ) {
  156. case 'WC_Order':
  157. $meta_name = 'order_meta';
  158. break;
  159. case 'WC_Coupon':
  160. $meta_name = 'coupon_meta';
  161. break;
  162. case 'WP_User':
  163. $meta_name = 'customer_meta';
  164. break;
  165. default:
  166. $meta_name = 'product_meta';
  167. break;
  168. }
  169. if ( is_a( $resource, 'WP_User' ) ) {
  170. // customer meta
  171. $meta = (array) get_user_meta( $resource->ID );
  172. } else {
  173. // coupon/order/product meta
  174. $meta = (array) get_post_meta( $resource->get_id() );
  175. }
  176. foreach ( $meta as $meta_key => $meta_value ) {
  177. // don't add hidden meta by default
  178. if ( ! is_protected_meta( $meta_key ) ) {
  179. $data[ $meta_name ][ $meta_key ] = maybe_unserialize( $meta_value[0] );
  180. }
  181. }
  182. }
  183. return $data;
  184. }
  185. /**
  186. * Restrict the fields included in the response if the request specified certain only certain fields should be returned
  187. *
  188. * @since 2.1
  189. * @param array $data the response data
  190. * @param object $resource the object that provided the response data, e.g. WC_Coupon or WC_Order
  191. * @param array|string the requested list of fields to include in the response
  192. * @return array response data
  193. */
  194. public function filter_response_fields( $data, $resource, $fields ) {
  195. if ( ! is_array( $data ) || empty( $fields ) ) {
  196. return $data;
  197. }
  198. $fields = explode( ',', $fields );
  199. $sub_fields = array();
  200. // get sub fields
  201. foreach ( $fields as $field ) {
  202. if ( false !== strpos( $field, '.' ) ) {
  203. list( $name, $value ) = explode( '.', $field );
  204. $sub_fields[ $name ] = $value;
  205. }
  206. }
  207. // iterate through top-level fields
  208. foreach ( $data as $data_field => $data_value ) {
  209. // if a field has sub-fields and the top-level field has sub-fields to filter
  210. if ( is_array( $data_value ) && in_array( $data_field, array_keys( $sub_fields ) ) ) {
  211. // iterate through each sub-field
  212. foreach ( $data_value as $sub_field => $sub_field_value ) {
  213. // remove non-matching sub-fields
  214. if ( ! in_array( $sub_field, $sub_fields ) ) {
  215. unset( $data[ $data_field ][ $sub_field ] );
  216. }
  217. }
  218. } else {
  219. // remove non-matching top-level fields
  220. if ( ! in_array( $data_field, $fields ) ) {
  221. unset( $data[ $data_field ] );
  222. }
  223. }
  224. }
  225. return $data;
  226. }
  227. /**
  228. * Delete a given resource
  229. *
  230. * @since 2.1
  231. * @param int $id the resource ID
  232. * @param string $type the resource post type, or `customer`
  233. * @param bool $force true to permanently delete resource, false to move to trash (not supported for `customer`)
  234. * @return array|WP_Error
  235. */
  236. protected function delete( $id, $type, $force = false ) {
  237. if ( 'shop_order' === $type || 'shop_coupon' === $type ) {
  238. $resource_name = str_replace( 'shop_', '', $type );
  239. } else {
  240. $resource_name = $type;
  241. }
  242. if ( 'customer' === $type ) {
  243. $result = wp_delete_user( $id );
  244. if ( $result ) {
  245. return array( 'message' => __( 'Permanently deleted customer', 'woocommerce' ) );
  246. } else {
  247. return new WP_Error( 'woocommerce_api_cannot_delete_customer', __( 'The customer cannot be deleted', 'woocommerce' ), array( 'status' => 500 ) );
  248. }
  249. } else {
  250. // delete order/coupon/product
  251. $result = ( $force ) ? wp_delete_post( $id, true ) : wp_trash_post( $id );
  252. if ( ! $result ) {
  253. return new WP_Error( "woocommerce_api_cannot_delete_{$resource_name}", sprintf( __( 'This %s cannot be deleted', 'woocommerce' ), $resource_name ), array( 'status' => 500 ) );
  254. }
  255. if ( $force ) {
  256. return array( 'message' => sprintf( __( 'Permanently deleted %s', 'woocommerce' ), $resource_name ) );
  257. } else {
  258. $this->server->send_status( '202' );
  259. return array( 'message' => sprintf( __( 'Deleted %s', 'woocommerce' ), $resource_name ) );
  260. }
  261. }
  262. }
  263. /**
  264. * Checks if the given post is readable by the current user
  265. *
  266. * @since 2.1
  267. * @see WC_API_Resource::check_permission()
  268. * @param WP_Post|int $post
  269. * @return bool
  270. */
  271. protected function is_readable( $post ) {
  272. return $this->check_permission( $post, 'read' );
  273. }
  274. /**
  275. * Checks if the given post is editable by the current user
  276. *
  277. * @since 2.1
  278. * @see WC_API_Resource::check_permission()
  279. * @param WP_Post|int $post
  280. * @return bool
  281. */
  282. protected function is_editable( $post ) {
  283. return $this->check_permission( $post, 'edit' );
  284. }
  285. /**
  286. * Checks if the given post is deletable by the current user
  287. *
  288. * @since 2.1
  289. * @see WC_API_Resource::check_permission()
  290. * @param WP_Post|int $post
  291. * @return bool
  292. */
  293. protected function is_deletable( $post ) {
  294. return $this->check_permission( $post, 'delete' );
  295. }
  296. /**
  297. * Checks the permissions for the current user given a post and context
  298. *
  299. * @since 2.1
  300. * @param WP_Post|int $post
  301. * @param string $context the type of permission to check, either `read`, `write`, or `delete`
  302. * @return bool true if the current user has the permissions to perform the context on the post
  303. */
  304. private function check_permission( $post, $context ) {
  305. if ( ! is_a( $post, 'WP_Post' ) ) {
  306. $post = get_post( $post );
  307. }
  308. if ( is_null( $post ) ) {
  309. return false;
  310. }
  311. $post_type = get_post_type_object( $post->post_type );
  312. if ( 'read' === $context ) {
  313. return current_user_can( $post_type->cap->read_private_posts, $post->ID );
  314. } elseif ( 'edit' === $context ) {
  315. return current_user_can( $post_type->cap->edit_post, $post->ID );
  316. } elseif ( 'delete' === $context ) {
  317. return current_user_can( $post_type->cap->delete_post, $post->ID );
  318. } else {
  319. return false;
  320. }
  321. }
  322. }