PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/woocommerce-payments/vendor/woocommerce/subscriptions-core/includes/data-stores/class-wcs-related-order-store-cpt.php

https://gitlab.com/remyvianne/krowkaramel
PHP | 261 lines | 115 code | 32 blank | 114 comment | 22 complexity | 050a64f2059c3cf9abc9181fcb18dba2 MD5 | raw file
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit;
  4. }
  5. /**
  6. * Related order data store for orders stored in Custom Post Types.
  7. *
  8. * Importantly, this class uses WC_Data API methods, like WC_Data::add_meta_data() and WC_Data::get_meta(), to manage the
  9. * relationships instead of add_post_meta() or get_post_meta(). This ensures that the relationship is stored, regardless
  10. * of the order data store being used. However, it also creates potential for relationships to fall out of sync if a
  11. * custom order data store is active, because @see $this->get_related_order_ids() queries the posts table via wp_posts(),
  12. * not the order's data store. This is unavoidable as wc_get_orders() and WC_Order_Query do not provide any way to query
  13. * meta data abstracted from the data store. Instead, it relies on 3rd party code to add custom parameter support for meta.
  14. * Source: https://github.com/woocommerce/woocommerce/wiki/wc_get_orders-and-WC_Order_Query#adding-custom-parameter-support
  15. *
  16. * Adding custom parameter support to order querying APIs won't help solve this issue, as the code would still directly
  17. * query post meta by default, and require additional code for different order data stores.
  18. *
  19. * The solution will be to eventually move away from the use of meta data on the order to a standalone relationship table.
  20. * This can be done already on sites running custom order data stores as WCS_Related_Order_Store::instance() is
  21. * filterable. It will eventually also be the default implementation in a future version of Subscriptions.
  22. *
  23. * @version 2.3.0
  24. * @category Class
  25. * @author Prospress
  26. */
  27. class WCS_Related_Order_Store_CPT extends WCS_Related_Order_Store {
  28. /**
  29. * Meta keys used to link an order with a subscription for each type of relationship.
  30. *
  31. * @since 2.3.0
  32. * @var array $meta_keys Relationship => Meta key
  33. */
  34. private $meta_keys;
  35. /**
  36. * Constructor: sets meta keys used for storing each order relation.
  37. */
  38. public function __construct() {
  39. foreach ( $this->get_relation_types() as $relation_type ) {
  40. $this->meta_keys[ $relation_type ] = sprintf( '_subscription_%s', $relation_type );
  41. }
  42. }
  43. /**
  44. * Find orders related to a given subscription in a given way.
  45. *
  46. * This method uses the WordPress Posts API instead of the WooCommerce Order's API, because
  47. * order's can not be queried by meta data with either wc_get_orders() or WC_Order_Query, so
  48. * a custom query parameter would need to be added to WC_Order_Query to run that query, which
  49. * is not something we want to add public APIs for because in future, that relationship will
  50. * be moved out of order meta and into its own table and queries on it should come through
  51. * here instead of the order querying API.
  52. *
  53. * @param WC_Order $subscription The ID of the subscription for which calling code wants the related orders.
  54. * @param string $relation_type The relationship between the subscription and the orders. Must be 'renewal', 'switch' or 'resubscribe.
  55. *
  56. * @return array
  57. */
  58. public function get_related_order_ids( WC_Order $subscription, $relation_type ) {
  59. $related_order_ids = get_posts( array(
  60. 'posts_per_page' => -1,
  61. 'post_type' => 'shop_order',
  62. 'post_status' => 'any',
  63. 'fields' => 'ids',
  64. 'meta_query' => array(
  65. array(
  66. 'key' => $this->get_meta_key( $relation_type ),
  67. 'compare' => '=',
  68. 'value' => wcs_get_objects_property( $subscription, 'id' ), // We can't rely on get_id() being available here, because we only require a WC_Order, not a WC_Subscription, and WC_Order does not have get_id() available with WC < 3.0
  69. 'type' => 'numeric',
  70. ),
  71. ),
  72. 'update_post_term_cache' => false,
  73. ) );
  74. rsort( $related_order_ids );
  75. return $related_order_ids;
  76. }
  77. /**
  78. * Find subscriptions related to a given order in a given way, if any.
  79. *
  80. * @param WC_Order $order The ID of an order that may be linked with subscriptions.
  81. * @param string $relation_type The relationship between the subscription and the orders. Must be 'renewal', 'switch' or 'resubscribe.
  82. *
  83. * @return array
  84. */
  85. public function get_related_subscription_ids( WC_Order $order, $relation_type ) {
  86. $related_order_meta_key = $this->get_meta_key( $relation_type );
  87. if ( wcs_is_woocommerce_pre( '3.0' ) ) {
  88. $related_subscription_ids = get_post_meta( wcs_get_objects_property( $order, 'id' ), $related_order_meta_key, false );
  89. } else {
  90. $related_subscription_ids = $order->get_meta( $related_order_meta_key, false );
  91. // Normalise the return value: WooCommerce returns a set of WC_Meta_Data objects, with values cast to strings, even if they're integers
  92. $related_subscription_ids = wp_list_pluck( $related_subscription_ids, 'value' );
  93. $related_subscription_ids = array_map( 'absint', $related_subscription_ids );
  94. $related_subscription_ids = array_values( $related_subscription_ids );
  95. }
  96. $related_subscription_ids = $this->apply_deprecated_related_order_filter( $related_subscription_ids, $order, $relation_type );
  97. return apply_filters( 'wcs_orders_related_subscription_ids', $related_subscription_ids, $order, $relation_type );
  98. }
  99. /**
  100. * Apply the deprecated 'wcs_subscriptions_for_renewal_order' and 'wcs_subscriptions_for_resubscribe_order' filters
  101. * to maintain backward compatibility.
  102. *
  103. * @param array $subscription_ids The IDs of subscription linked to the given order, if any.
  104. * @param WC_Order $order An instance of an order that may be linked with subscriptions.
  105. * @param string $relation_type The relationship between the subscription and the orders. Must be 'renewal', 'switch' or 'resubscribe'.
  106. *
  107. * @return array
  108. */
  109. protected function apply_deprecated_related_order_filter( $subscription_ids, WC_Order $order, $relation_type ) {
  110. $deprecated_filter_hook = "wcs_subscriptions_for_{$relation_type}_order";
  111. if ( has_filter( $deprecated_filter_hook ) ) {
  112. wcs_deprecated_function( sprintf( '"%s" hook should no longer be used and', esc_html( $deprecated_filter_hook ) ), '2.3.2', '"wcs_orders_related_subscription_ids" with a check on the 3rd param, to take advantage of the new persistent caching layer for related subscription IDs' );
  113. $subscriptions = array();
  114. foreach ( $subscription_ids as $subscription_id ) {
  115. if ( wcs_is_subscription( $subscription_id ) ) {
  116. $subscriptions[ $subscription_id ] = wcs_get_subscription( $subscription_id );
  117. }
  118. }
  119. $filtered_subscriptions = apply_filters( $deprecated_filter_hook, $subscriptions, $order );
  120. // Although this array was previously ordered by ID => instance, that key requirement wasn't enforced so it's possible 3rd party code was not using the ID as the key, and instead, numerical indexes are being used, so its safest not to rely on IDs as keys
  121. if ( $filtered_subscriptions != $subscriptions ) {
  122. $subscription_ids = array();
  123. foreach ( $filtered_subscriptions as $subscription ) {
  124. $subscription_ids[] = $subscription->get_id();
  125. }
  126. }
  127. }
  128. return $subscription_ids;
  129. }
  130. /**
  131. * Helper function for linking an order to a subscription via a given relationship.
  132. *
  133. * Existing order relationships of the same type will not be overwritten. This only adds a relationship. To overwrite,
  134. * you must also remove any existing relationship with @see $this->delete_relation().
  135. *
  136. * This data store links the relationship for a renewal order and a subscription in meta data against the order.
  137. * That's inefficient for queries, so will be changed in future with a different data store. It also leads to potential
  138. * bugs when WooCommerce 3.0 or newer is running with a custom data store for order data, as related orders are queried
  139. * in $this->get_related_order_ids() using post meta directly, but set here using the CRUD WC_Data::add_meta_data() method.
  140. * This is unfortunately unavoidable. See the WCS_Related_Order_Store_CPT docblock for more details.
  141. *
  142. * @param WC_Order $order The order to link with the subscription.
  143. * @param WC_Order $subscription The order or subscription to link the order to.
  144. * @param string $relation_type The relationship between the subscription and the order. Must be 'renewal', 'switch' or 'resubscribe' unless custom relationships are implemented.
  145. */
  146. public function add_relation( WC_Order $order, WC_Order $subscription, $relation_type ) {
  147. // We can't rely on $subscription->get_id() being available here, because we only require a WC_Order, not a WC_Subscription, and WC_Order does not have get_id() available with WC < 3.0
  148. $subscription_id = wcs_get_objects_property( $subscription, 'id' );
  149. $related_order_meta_key = $this->get_meta_key( $relation_type );
  150. if ( wcs_is_woocommerce_pre( '3.0' ) ) {
  151. $order_id = wcs_get_objects_property( $order, 'id' );
  152. $existing_related_ids = get_post_meta( $order_id, $related_order_meta_key, false );
  153. if ( empty( $existing_related_ids ) || ! in_array( $subscription_id, $existing_related_ids ) ) {
  154. add_post_meta( $order_id, $related_order_meta_key, $subscription_id, false );
  155. }
  156. } else {
  157. // We want to allow more than one piece of meta per key on the order, but we don't want to duplicate the same meta key => value combination, so we need to check if it is set first
  158. $existing_relations = $order->get_meta( $related_order_meta_key, false );
  159. $existing_related_ids = wp_list_pluck( $existing_relations, 'value' );
  160. if ( empty( $existing_relations ) || ! in_array( $subscription_id, $existing_related_ids ) ) {
  161. $order->add_meta_data( $related_order_meta_key, $subscription_id, false );
  162. $order->save_meta_data();
  163. }
  164. }
  165. }
  166. /**
  167. * Remove the relationship between a given order and subscription.
  168. *
  169. * This data store links the relationship for a renewal order and a subscription in meta data against the order.
  170. * That's inefficient for queries, so will be changed in future with a different data store. It also leads to bugs
  171. * with custom data stores for order data, as $this->get_related_order_ids() queries post meta directly. This is
  172. * unavoidable. See the WCS_Related_Order_Store_CPT docblock for more details.
  173. *
  174. * @param WC_Order $order An order that may be linked with subscriptions.
  175. * @param WC_Order $subscription A subscription or order to unlink the order with, if a relation exists.
  176. * @param string $relation_type The relationship between the subscription and the order. Must be 'renewal', 'switch' or 'resubscribe' unless custom relationships are implemented.
  177. */
  178. public function delete_relation( WC_Order $order, WC_Order $subscription, $relation_type ) {
  179. $related_order_meta_key = $this->get_meta_key( $relation_type );
  180. if ( wcs_is_woocommerce_pre( '3.0' ) ) {
  181. delete_post_meta( wcs_get_objects_property( $order, 'id' ), $related_order_meta_key, wcs_get_objects_property( $subscription, 'id' ) );
  182. } else {
  183. foreach ( $order->get_meta_data() as $meta ) {
  184. if ( $meta->key == $related_order_meta_key && $meta->value == wcs_get_objects_property( $subscription, 'id' ) ) { // we can't do strict comparison here, because WC_Meta_Data casts the subscription ID to be a string
  185. $order->delete_meta_data_by_mid( $meta->id );
  186. }
  187. }
  188. $order->save_meta_data();
  189. }
  190. }
  191. /**
  192. * Remove all related orders/subscriptions of a given type from an order.
  193. *
  194. * @param WC_Order $order An order that may be linked with subscriptions.
  195. * @param string $relation_type The relationship between the subscription and the order. Must be 'renewal', 'switch' or 'resubscribe' unless custom relationships are implemented.
  196. */
  197. public function delete_relations( WC_Order $order, $relation_type ) {
  198. $related_order_meta_key = $this->get_meta_key( $relation_type );
  199. if ( wcs_is_woocommerce_pre( '3.0' ) ) {
  200. delete_post_meta( wcs_get_objects_property( $order, 'id' ), $related_order_meta_key, null );
  201. } else {
  202. $order->delete_meta_data( $related_order_meta_key );
  203. $order->save_meta_data();
  204. }
  205. }
  206. /**
  207. * Get the meta keys used to link orders with subscriptions.
  208. *
  209. * @return array
  210. */
  211. protected function get_meta_keys() {
  212. return $this->meta_keys;
  213. }
  214. /**
  215. * Get the meta key used to link an order with a subscription based on the type of relationship.
  216. *
  217. * @param string $relation_type The order's relationship with the subscription. Must be 'renewal', 'switch' or 'resubscribe'.
  218. * @param string $prefix_meta_key Whether to add the underscore prefix to the meta key or not. 'prefix' to prefix the key. 'do_not_prefix' to not prefix the key.
  219. * @return string
  220. */
  221. protected function get_meta_key( $relation_type, $prefix_meta_key = 'prefix' ) {
  222. $this->check_relation_type( $relation_type );
  223. $meta_key = $this->meta_keys[ $relation_type ];
  224. if ( 'do_not_prefix' === $prefix_meta_key ) {
  225. $meta_key = wcs_maybe_unprefix_key( $meta_key );
  226. }
  227. return $meta_key;
  228. }
  229. }