PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/woocommerce-payments/vendor/woocommerce/subscriptions-core/includes/class-wc-subscriptions-tracker.php

https://gitlab.com/remyvianne/krowkaramel
PHP | 198 lines | 118 code | 27 blank | 53 comment | 7 complexity | 28ceaa1e17d7cf33dfc772f9bc3d8aea MD5 | raw file
  1. <?php
  2. /**
  3. * Tracker for Subscriptions usage.
  4. *
  5. * @class WC_Subscriptions_Tracker
  6. * @version 2.6.4
  7. * @package WooCommerce Subscriptions/Classes
  8. * @category Class
  9. * @author WooCommerce
  10. */
  11. defined( 'ABSPATH' ) || exit;
  12. class WC_Subscriptions_Tracker {
  13. /**
  14. * Initialize the Tracker.
  15. */
  16. public static function init() {
  17. // Only add data if Tracker enabled
  18. if ( 'yes' === get_option( 'woocommerce_allow_tracking', 'no' ) ) {
  19. add_filter( 'woocommerce_tracker_data', array( __CLASS__, 'add_subscriptions_tracking_data' ), 10, 1 );
  20. }
  21. }
  22. /**
  23. * Adds Subscriptions data to the WC tracked data.
  24. *
  25. * @param array $data
  26. * @return array all the tracking data.
  27. */
  28. public static function add_subscriptions_tracking_data( $data ) {
  29. $data['extensions']['wc_subscriptions']['settings'] = self::get_subscriptions_options();
  30. $data['extensions']['wc_subscriptions']['subscriptions'] = self::get_subscriptions();
  31. $data['extensions']['wc_subscriptions']['subscription_orders'] = self::get_subscription_orders();
  32. return $data;
  33. }
  34. /**
  35. * Gets the tracked Subscriptions options data.
  36. *
  37. * @return array Subscriptions options data.
  38. */
  39. private static function get_subscriptions_options() {
  40. return array(
  41. // Staging and live site
  42. 'wc_subscriptions_staging' => WCS_Staging::is_duplicate_site() ? 'staging' : 'live',
  43. 'wc_subscriptions_live_url' => esc_url( WCS_Staging::get_site_url_from_source( 'subscriptions_install' ) ),
  44. // Button text, roles
  45. 'add_to_cart_button_text' => get_option( WC_Subscriptions_Admin::$option_prefix . '_add_to_cart_button_text' ),
  46. 'order_button_text' => get_option( WC_Subscriptions_Admin::$option_prefix . '_order_button_text' ),
  47. 'subscriber_role' => get_option( WC_Subscriptions_Admin::$option_prefix . '_subscriber_role' ),
  48. 'cancelled_role' => get_option( WC_Subscriptions_Admin::$option_prefix . '_cancelled_role' ),
  49. // Renewals
  50. 'accept_manual_renewals' => get_option( WC_Subscriptions_Admin::$option_prefix . '_accept_manual_renewals' ),
  51. 'turn_off_automatic_payments' => 'no' == get_option( WC_Subscriptions_Admin::$option_prefix . '_accept_manual_renewals' ) ? 'none' : get_option( WC_Subscriptions_Admin::$option_prefix . '_turn_off_automatic_payments', 'none' ),
  52. 'enable_auto_renewal_toggle' => get_option( WC_Subscriptions_Admin::$option_prefix . '_enable_auto_renewal_toggle' ),
  53. // Early renewal
  54. 'enable_early_renewal' => get_option( WC_Subscriptions_Admin::$option_prefix . '_enable_early_renewal' ),
  55. 'enable_early_renewal_via_modal' => 'no' == get_option( WC_Subscriptions_Admin::$option_prefix . '_enable_early_renewal' ) ? 'none' : get_option( WC_Subscriptions_Admin::$option_prefix . '_enable_early_renewal_via_modal', 'none' ),
  56. // Switching
  57. 'allow_switching' => get_option( WC_Subscriptions_Admin::$option_prefix . '_allow_switching' ),
  58. 'apportion_recurring_price' => get_option( WC_Subscriptions_Admin::$option_prefix . '_apportion_recurring_price', 'none' ),
  59. 'apportion_sign_up_fee' => get_option( WC_Subscriptions_Admin::$option_prefix . '_apportion_sign_up_fee', 'none' ),
  60. 'apportion_length' => get_option( WC_Subscriptions_Admin::$option_prefix . '_apportion_length', 'none' ),
  61. 'switch_button_text' => get_option( WC_Subscriptions_Admin::$option_prefix . '_switch_button_text', 'none' ),
  62. // Synchronization
  63. 'sync_payments' => get_option( WC_Subscriptions_Admin::$option_prefix . '_sync_payments' ),
  64. 'prorate_synced_payments' => $prorate_synced_payments = ( 'no' == get_option( WC_Subscriptions_Admin::$option_prefix . '_sync_payments' ) ? 'none' : get_option( WC_Subscriptions_Admin::$option_prefix . '_prorate_synced_payments', 'none' ) ),
  65. 'days_no_fee' => 'recurring' == $prorate_synced_payments ? get_option( WC_Subscriptions_Admin::$option_prefix . '_days_no_fee', 'none' ) : 'none',
  66. // Miscellaneous
  67. 'max_customer_suspensions' => get_option( WC_Subscriptions_Admin::$option_prefix . '_max_customer_suspensions' ),
  68. 'multiple_purchase' => get_option( WC_Subscriptions_Admin::$option_prefix . '_multiple_purchase' ),
  69. 'allow_zero_initial_order_without_payment_method' => get_option( WC_Subscriptions_Admin::$option_prefix . '_zero_initial_payment_requires_payment' ),
  70. 'drip_downloadable_content_on_renewal' => get_option( WC_Subscriptions_Admin::$option_prefix . '_drip_downloadable_content_on_renewal' ),
  71. 'enable_retry' => get_option( WC_Subscriptions_Admin::$option_prefix . '_enable_retry' ),
  72. );
  73. }
  74. /**
  75. * Gets the combined subscription dates, count, and totals data.
  76. *
  77. * @return array
  78. */
  79. private static function get_subscriptions() {
  80. $subscription_dates = self::get_subscription_dates();
  81. $subscription_counts = self::get_subscription_counts();
  82. return array_merge( $subscription_dates, $subscription_counts );
  83. }
  84. /**
  85. * Gets subscription counts.
  86. *
  87. * @return array
  88. */
  89. private static function get_subscription_counts() {
  90. $subscription_counts = array();
  91. $subscription_counts_data = wp_count_posts( 'shop_subscription' );
  92. foreach ( wcs_get_subscription_statuses() as $status_slug => $status_name ) {
  93. $subscription_counts[ $status_slug ] = $subscription_counts_data->{ $status_slug };
  94. }
  95. return $subscription_counts;
  96. }
  97. /**
  98. * Gets subscription order counts and totals.
  99. *
  100. * @return array
  101. */
  102. private static function get_subscription_orders() {
  103. global $wpdb;
  104. $order_totals = array();
  105. $relation_types = array(
  106. 'switch',
  107. 'renewal',
  108. 'resubscribe',
  109. );
  110. foreach ( $relation_types as $relation_type ) {
  111. $total_and_count = $wpdb->get_row( sprintf(
  112. "SELECT
  113. SUM( order_total.meta_value ) AS 'gross_total', COUNT( orders.ID ) as 'count'
  114. FROM {$wpdb->prefix}posts AS orders
  115. LEFT JOIN {$wpdb->prefix}postmeta AS order_relation ON order_relation.post_id = orders.ID
  116. LEFT JOIN {$wpdb->prefix}postmeta AS order_total ON order_total.post_id = orders.ID
  117. WHERE order_relation.meta_key = '_subscription_%s'
  118. AND orders.post_status in ( 'wc-completed', 'wc-processing', 'wc-refunded' )
  119. AND order_total.meta_key = '_order_total'
  120. GROUP BY order_total.meta_key
  121. ", $relation_type
  122. ), ARRAY_A );
  123. $order_totals[ $relation_type . '_gross' ] = is_null( $total_and_count ) ? 0 : $total_and_count['gross_total'];
  124. $order_totals[ $relation_type . '_count' ] = is_null( $total_and_count ) ? 0 : $total_and_count['count'];
  125. }
  126. // Finally get the initial revenue and count
  127. $total_and_count = $wpdb->get_row(
  128. "SELECT
  129. SUM( order_total.meta_value ) AS 'gross_total', COUNT( * ) as 'count'
  130. FROM {$wpdb->prefix}posts AS orders
  131. LEFT JOIN {$wpdb->prefix}posts AS subscriptions ON subscriptions.post_parent = orders.ID
  132. LEFT JOIN {$wpdb->prefix}postmeta AS order_total ON order_total.post_id = orders.ID
  133. WHERE orders.post_status in ( 'wc-completed', 'wc-processing', 'wc-refunded' )
  134. AND subscriptions.post_type = 'shop_subscription'
  135. AND orders.post_type = 'shop_order'
  136. AND order_total.meta_key = '_order_total'
  137. GROUP BY order_total.meta_key
  138. ", ARRAY_A );
  139. $initial_order_total = is_null( $total_and_count ) ? 0 : $total_and_count['gross_total'];
  140. $initial_order_count = is_null( $total_and_count ) ? 0 : $total_and_count['count'];
  141. // Don't double count resubscribe revenue and count
  142. $order_totals['initial_gross'] = $initial_order_total - $order_totals['resubscribe_gross'];
  143. $order_totals['initial_count'] = $initial_order_count - $order_totals['resubscribe_count'];
  144. return $order_totals;
  145. }
  146. /**
  147. * Gets first and last subscription created dates.
  148. *
  149. * @return array
  150. */
  151. private static function get_subscription_dates() {
  152. global $wpdb;
  153. $min_max = $wpdb->get_row(
  154. "
  155. SELECT
  156. MIN( post_date_gmt ) as 'first', MAX( post_date_gmt ) as 'last'
  157. FROM {$wpdb->prefix}posts
  158. WHERE post_type = 'shop_subscription'
  159. AND post_status NOT IN ( 'trash', 'auto-draft' )
  160. ",
  161. ARRAY_A
  162. );
  163. if ( is_null( $min_max ) ) {
  164. $min_max = array(
  165. 'first' => '-',
  166. 'last' => '-',
  167. );
  168. }
  169. return $min_max;
  170. }
  171. }