PluginProbe
Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity / 3.3.10
Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity v3.3.10
3.3.10 3.3.9 3.3.8 trunk 1.0 1.1.0 1.10.0 1.11.0 1.11.1 1.12.0 1.13.0 1.14.0 1.15.0 1.16.0 1.17.0 1.17.1 1.18.0 1.19.0 1.2.0 1.20.0 1.20.1 1.3.0 1.3.1 1.4.0 1.5.0 All 68 releases
logtivity / Loggers / Easy_Digital_Downloads / Recurring / Logtivity_Easy_Digital_Downloads_Recurring.php

Logtivity_Easy_Digital_Downloads_Recurring.php in Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity 3.3.10, at Loggers/Easy_Digital_Downloads/Recurring/Logtivity_Easy_Digital_Downloads_Recurring.php

228 lines 6.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * @package Logtivity
5 * @contact logtivity.io, [email protected]
6 * @copyright 2024-2026 Logtivity. All rights reserved
7 * @license https://www.gnu.org/licenses/gpl.html GNU/GPL
8 *
9 * This file is part of Logtivity.
10 *
11 * Logtivity is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * Logtivity is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with Logtivity. If not, see <https://www.gnu.org/licenses/>.
23 */
24
25 class Logtivity_Easy_Digital_Downloads_Recurring extends Logtivity_Abstract_Easy_Digital_Downloads
26 {
27 public function __construct()
28 {
29 add_action('edd_subscription_post_renew', [$this, 'subscriptionRenewed'], 10, 4);
30 add_action('edd_subscription_post_create', [$this, 'subscriptionCreated'], 10, 2);
31 add_action('edd_recurring_update_subscription', [$this, 'subscriptionUpdated'], 10, 3);
32 add_action('edd_subscription_status_change', [$this, 'subscriptionStatusChanged'], 10, 3);
33 add_action('edd_recurring_before_delete_subscription', [$this, 'subscriptionDeleted'], 10, 1);
34 add_action('edd_recurring_update_subscription_payment_method', [$this, 'setupPaymentMethodUpdate'], 9, 3);
35 }
36
37 public function subscriptionRenewed($subscription_id, $expiration, $subscription, $payment_id)
38 {
39 $payment = new EDD_Payment($payment_id);
40
41 $log = Logtivity::log()
42 ->setAction('Subscription Renewed')
43 ->setContext($this->getDownloadTitle($subscription));
44
45 if (!is_user_logged_in()) {
46 $log->setUser($subscription->customer->user_id ?? null);
47 }
48
49 $log->setUser($payment->user_id ?? null)
50 ->addMeta('Customer ID', $payment->customer_id)
51 ->addMeta('Subscription ID', $subscription_id);
52
53 if (isset($expiration) && $expiration) {
54 try {
55 $log->addMeta('Expiration Date', date('M d Y', strtotime($expiration)));
56 } catch (Throwable $e) {
57 // Ignore
58 unset($e);
59 }
60 }
61
62 $log->addMeta('Payment ID', $payment_id)
63 ->send();
64 }
65
66 public function subscriptionCreated($subscription_id, $args)
67 {
68 $subscription = new EDD_Subscription($subscription_id);
69
70 $log = Logtivity::log()
71 ->setAction('Subscription Created')
72 ->setContext($this->getDownloadTitle($subscription))
73 ->addMeta('Customer ID', $subscription->customer_id)
74 ->addMeta('Subscription ID', $subscription_id);
75
76 if (isset($args['expiration']) && $args['expiration']) {
77 try {
78 $log->addMeta('Expiration Date', date('M d Y', strtotime($args['expiration'])));
79 } catch (Throwable $e) {
80 // Ignore
81 unset($e);
82 }
83 }
84
85 $log->send();
86 }
87
88 public function subscriptionUpdated($subscription_id, $args, $subscription)
89 {
90 $log = Logtivity::log()
91 ->setAction('Subscription Updated');
92
93 $current_product = $subscription->product_id;
94 $current_price_id = $subscription->price_id;
95
96 if (isset($args['product_id']) && $current_product != $args['product_id']) {
97 $log->setContext('Product changed from '.logtivity_get_the_title($current_product).' to '.logtivity_get_the_title($args['product_id']));
98 }
99
100 if ( isset( $args['price_id'] ) && ! is_null( $args['price_id'] ) && $current_price_id != $args['price_id'] ) {
101 $prices = edd_get_variable_prices($args['product_id'] ?? $current_product);
102
103 if ($prices && count($prices)) {
104 if (isset($prices[$args['price_id']]) && isset($prices[$current_price_id])) {
105 $log->setContext('Price changed from '.$prices[$current_price_id]['name'].' to '.$prices[$args['price_id']]['name']);
106 }
107 } else {
108 $log->setContext('Price ID changed from '.$current_price_id.' to '.$args['price_id']);
109 }
110 }
111
112 if (!$log->context) {
113 return;
114 }
115
116 $log->addMeta('Customer ID', $subscription->customer_id)
117 ->addMeta('Subscription ID', $subscription_id)
118 ->addMeta('Product', logtivity_get_the_title($args['product_id'] ?? $current_product));
119
120 if (!is_user_logged_in()) {
121 $log->setUser($subscription->customer->user_id ?? null);
122 }
123
124 if ($subscription->expiration) {
125 try {
126 $log->addMeta('Expiration Date', date('M d Y', strtotime($subscription->expiration)));
127 } catch (Throwable $e) {
128 // Ignore
129 unset($e);
130 }
131 }
132
133 $log->send();
134 }
135
136 public function subscriptionStatusChanged($old_status, $new_status, $subscription)
137 {
138 if ($old_status === $new_status) {
139 return;
140 }
141
142 $log = Logtivity::log()
143 ->setAction('Subscription '. ucfirst($new_status))
144 ->setContext($this->getDownloadTitle($subscription))
145 ->addMeta('Old Status', $old_status)
146 ->addMeta('Customer ID', $subscription->customer_id)
147 ->addMeta('Subscription ID', $subscription->id);
148
149 if (!is_user_logged_in()) {
150 $log->setUser($subscription->customer->user_id ?? null);
151 }
152
153 if ($subscription->expiration) {
154 try {
155 $log->addMeta('Expiration Date', date('M d Y', strtotime($subscription->expiration)));
156 } catch (Throwable $e) {
157 // Ignore
158 unset($e);
159 }
160 }
161
162 $log->send();
163 }
164
165 public function subscriptionDeleted($subscription)
166 {
167 $log = Logtivity::log()
168 ->setAction('Subscription Deleted')
169 ->setContext($this->getDownloadTitle($subscription))
170 ->addMeta('Customer ID', $subscription->customer_id)
171 ->addMeta('Subscription ID', $subscription->id);
172
173 if (!is_user_logged_in()) {
174 $log->setUser($subscription->customer->user_id ?? null);
175 }
176
177 if ($subscription->expiration) {
178 try {
179 $log->addMeta('Expiration Date', date('M d Y', strtotime($subscription->expiration)));
180 } catch (Throwable $e) {
181 // Ignore
182 unset($e);
183 }
184 }
185
186 $log->send();
187 }
188
189 public function setupPaymentMethodUpdate($user_id, $subscription_id, $verified)
190 {
191 $subscription = new EDD_Subscription($subscription_id);
192
193 add_action( 'edd_recurring_update_' . $subscription->gateway .'_subscription', [$this, 'paymentMethodUpdated'], 999999999999, 2);
194 }
195
196 public function paymentMethodUpdated($subscriber, $subscription)
197 {
198 $errors = edd_get_errors();
199
200 if (!empty($errors)) {
201 return;
202 }
203
204 $log = Logtivity::log()
205 ->setAction('Payment Method Updated')
206 ->setContext($subscription->gateway)
207 ->addMeta('Customer ID', $subscription->customer_id)
208 ->addMeta('Subscription ID', $subscription->id);
209
210 if (!is_user_logged_in()) {
211 $log->setUser($subscription->customer->user_id ?? null);
212 }
213
214 if ($subscription->expiration) {
215 try {
216 $log->addMeta('Expiration Date', date('M d Y', strtotime($subscription->expiration)));
217 } catch (Throwable $e) {
218 // Ignore
219 unset($e);
220 }
221 }
222
223 $log->send();
224 }
225 }
226
227 new Logtivity_Easy_Digital_Downloads_Recurring();
228