| 1 |
<?php |
| 2 |
|
| 3 |
class Logtivity_Easy_Digital_Downloads_Recurring extends Logtivity_Abstract_Easy_Digital_Downloads |
| 4 |
{ |
| 5 |
public function __construct() |
| 6 |
{ |
| 7 |
add_action('edd_subscription_post_renew', [$this, 'subscriptionRenewed'], 10, 4); |
| 8 |
add_action('edd_subscription_post_create', [$this, 'subscriptionCreated'], 10, 2); |
| 9 |
add_action('edd_recurring_update_subscription', [$this, 'subscriptionUpdated'], 10, 3); |
| 10 |
add_action('edd_subscription_status_change', [$this, 'subscriptionStatusChanged'], 10, 3); |
| 11 |
add_action('edd_recurring_before_delete_subscription', [$this, 'subscriptionDeleted'], 10, 1); |
| 12 |
add_action('edd_recurring_update_subscription_payment_method', [$this, 'setupPaymentMethodUpdate'], 9, 3); |
| 13 |
} |
| 14 |
|
| 15 |
public function subscriptionRenewed($subscription_id, $expiration, $subscription, $payment_id) |
| 16 |
{ |
| 17 |
$payment = new EDD_Payment($payment_id); |
| 18 |
|
| 19 |
$log = Logtivity_Logger::log() |
| 20 |
->setAction('Subscription Renewed') |
| 21 |
->setContext($this->getDownloadTitle($subscription)); |
| 22 |
|
| 23 |
if (!is_user_logged_in()) { |
| 24 |
$log->setUser($subscription->customer->user_id ?? null); |
| 25 |
} |
| 26 |
|
| 27 |
$log->setUser($payment->user_id ?? null) |
| 28 |
->addMeta('Customer ID', $payment->customer_id) |
| 29 |
->addMeta('Subscription ID', $subscription_id); |
| 30 |
|
| 31 |
if (isset($expiration) && $expiration) { |
| 32 |
try { |
| 33 |
$log->addMeta('Expiration Date', date('M d Y', strtotime($expiration))); |
| 34 |
} catch (\Exception $e) { |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
$log->addMeta('Payment ID', $payment_id) |
| 39 |
->send(); |
| 40 |
} |
| 41 |
|
| 42 |
public function subscriptionCreated($subscription_id, $args) |
| 43 |
{ |
| 44 |
$subscription = new EDD_Subscription($subscription_id); |
| 45 |
|
| 46 |
$log = Logtivity_Logger::log() |
| 47 |
->setAction('Subscription Created') |
| 48 |
->setContext($this->getDownloadTitle($subscription)) |
| 49 |
->addMeta('Customer ID', $subscription->customer_id) |
| 50 |
->addMeta('Subscription ID', $subscription_id); |
| 51 |
|
| 52 |
if (isset($args['expiration']) && $args['expiration']) { |
| 53 |
try { |
| 54 |
$log->addMeta('Expiration Date', date('M d Y', strtotime($args['expiration']))); |
| 55 |
} catch (\Exception $e) { |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
$log->send(); |
| 60 |
} |
| 61 |
|
| 62 |
public function subscriptionUpdated($subscription_id, $args, $subscription) |
| 63 |
{ |
| 64 |
$log = Logtivity_Logger::log() |
| 65 |
->setAction('Subscription Updated'); |
| 66 |
|
| 67 |
$current_product = $subscription->product_id; |
| 68 |
$current_price_id = $subscription->price_id; |
| 69 |
|
| 70 |
if (isset($args['product_id']) && $current_product != $args['product_id']) { |
| 71 |
$log->setContext('Product changed from '.logtivity_get_the_title($current_product).' to '.logtivity_get_the_title($args['product_id'])); |
| 72 |
} |
| 73 |
|
| 74 |
if ( isset( $args['price_id'] ) && ! is_null( $args['price_id'] ) && $current_price_id != $args['price_id'] ) { |
| 75 |
$prices = edd_get_variable_prices($args['product_id'] ?? $current_product); |
| 76 |
|
| 77 |
if ($prices && count($prices)) { |
| 78 |
if (isset($prices[$args['price_id']]) && isset($prices[$current_price_id])) { |
| 79 |
$log->setContext('Price changed from '.$prices[$current_price_id]['name'].' to '.$prices[$args['price_id']]['name']); |
| 80 |
} |
| 81 |
} else { |
| 82 |
$log->setContext('Price ID changed from '.$current_price_id.' to '.$args['price_id']); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
if (!$log->context) { |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
$log->addMeta('Customer ID', $subscription->customer_id) |
| 91 |
->addMeta('Subscription ID', $subscription_id) |
| 92 |
->addMeta('Product', logtivity_get_the_title($args['product_id'] ?? $current_product)); |
| 93 |
|
| 94 |
if (!is_user_logged_in()) { |
| 95 |
$log->setUser($subscription->customer->user_id ?? null); |
| 96 |
} |
| 97 |
|
| 98 |
if ($subscription->expiration) { |
| 99 |
try { |
| 100 |
$log->addMeta('Expiration Date', date('M d Y', strtotime($subscription->expiration))); |
| 101 |
} catch (\Exception $e) { |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
$log->send(); |
| 106 |
} |
| 107 |
|
| 108 |
public function subscriptionStatusChanged($old_status, $new_status, $subscription) |
| 109 |
{ |
| 110 |
if ($old_status === $new_status) { |
| 111 |
return; |
| 112 |
} |
| 113 |
|
| 114 |
$log = Logtivity_Logger::log() |
| 115 |
->setAction('Subscription '. ucfirst($new_status)) |
| 116 |
->setContext($this->getDownloadTitle($subscription)) |
| 117 |
->addMeta('Old Status', $old_status) |
| 118 |
->addMeta('Customer ID', $subscription->customer_id) |
| 119 |
->addMeta('Subscription ID', $subscription->id); |
| 120 |
|
| 121 |
if (!is_user_logged_in()) { |
| 122 |
$log->setUser($subscription->customer->user_id ?? null); |
| 123 |
} |
| 124 |
|
| 125 |
if ($subscription->expiration) { |
| 126 |
try { |
| 127 |
$log->addMeta('Expiration Date', date('M d Y', strtotime($subscription->expiration))); |
| 128 |
} catch (\Exception $e) { |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
$log->send(); |
| 133 |
} |
| 134 |
|
| 135 |
public function subscriptionDeleted($subscription) |
| 136 |
{ |
| 137 |
$log = Logtivity_Logger::log() |
| 138 |
->setAction('Subscription Deleted') |
| 139 |
->setContext($this->getDownloadTitle($subscription)) |
| 140 |
->addMeta('Customer ID', $subscription->customer_id) |
| 141 |
->addMeta('Subscription ID', $subscription->id); |
| 142 |
|
| 143 |
if (!is_user_logged_in()) { |
| 144 |
$log->setUser($subscription->customer->user_id ?? null); |
| 145 |
} |
| 146 |
|
| 147 |
if ($subscription->expiration) { |
| 148 |
try { |
| 149 |
$log->addMeta('Expiration Date', date('M d Y', strtotime($subscription->expiration))); |
| 150 |
} catch (\Exception $e) { |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
$log->send(); |
| 155 |
} |
| 156 |
|
| 157 |
public function setupPaymentMethodUpdate($user_id, $subscription_id, $verified) |
| 158 |
{ |
| 159 |
$subscription = new EDD_Subscription($subscription_id); |
| 160 |
|
| 161 |
add_action( 'edd_recurring_update_' . $subscription->gateway .'_subscription', [$this, 'paymentMethodUpdated'], 999999999999, 2); |
| 162 |
} |
| 163 |
|
| 164 |
public function paymentMethodUpdated($subscriber, $subscription) |
| 165 |
{ |
| 166 |
$errors = edd_get_errors(); |
| 167 |
|
| 168 |
if (!empty($errors)) { |
| 169 |
return; |
| 170 |
} |
| 171 |
|
| 172 |
$log = Logtivity_Logger::log() |
| 173 |
->setAction('Payment Method Updated') |
| 174 |
->setContext($subscription->gateway) |
| 175 |
->addMeta('Customer ID', $subscription->customer_id) |
| 176 |
->addMeta('Subscription ID', $subscription->id); |
| 177 |
|
| 178 |
if (!is_user_logged_in()) { |
| 179 |
$log->setUser($subscription->customer->user_id ?? null); |
| 180 |
} |
| 181 |
|
| 182 |
if ($subscription->expiration) { |
| 183 |
try { |
| 184 |
$log->addMeta('Expiration Date', date('M d Y', strtotime($subscription->expiration))); |
| 185 |
} catch (\Exception $e) { |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
$log->send(); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
$Logtivity_Easy_Digital_Downloads_Recurring = new Logtivity_Easy_Digital_Downloads_Recurring; |