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.9, at Loggers/Easy_Digital_Downloads/Recurring/Logtivity_Easy_Digital_Downloads_Recurring.php
| 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 | } |
| 59 | } |
| 60 | |
| 61 | $log->addMeta('Payment ID', $payment_id) |
| 62 | ->send(); |
| 63 | } |
| 64 | |
| 65 | public function subscriptionCreated($subscription_id, $args) |
| 66 | { |
| 67 | $subscription = new EDD_Subscription($subscription_id); |
| 68 | |
| 69 | $log = Logtivity::log() |
| 70 | ->setAction('Subscription Created') |
| 71 | ->setContext($this->getDownloadTitle($subscription)) |
| 72 | ->addMeta('Customer ID', $subscription->customer_id) |
| 73 | ->addMeta('Subscription ID', $subscription_id); |
| 74 | |
| 75 | if (isset($args['expiration']) && $args['expiration']) { |
| 76 | try { |
| 77 | $log->addMeta('Expiration Date', date('M d Y', strtotime($args['expiration']))); |
| 78 | } catch (Throwable $e) { |
| 79 | // Ignore |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | $log->send(); |
| 84 | } |
| 85 | |
| 86 | public function subscriptionUpdated($subscription_id, $args, $subscription) |
| 87 | { |
| 88 | $log = Logtivity::log() |
| 89 | ->setAction('Subscription Updated'); |
| 90 | |
| 91 | $current_product = $subscription->product_id; |
| 92 | $current_price_id = $subscription->price_id; |
| 93 | |
| 94 | if (isset($args['product_id']) && $current_product != $args['product_id']) { |
| 95 | $log->setContext('Product changed from '.logtivity_get_the_title($current_product).' to '.logtivity_get_the_title($args['product_id'])); |
| 96 | } |
| 97 | |
| 98 | if ( isset( $args['price_id'] ) && ! is_null( $args['price_id'] ) && $current_price_id != $args['price_id'] ) { |
| 99 | $prices = edd_get_variable_prices($args['product_id'] ?? $current_product); |
| 100 | |
| 101 | if ($prices && count($prices)) { |
| 102 | if (isset($prices[$args['price_id']]) && isset($prices[$current_price_id])) { |
| 103 | $log->setContext('Price changed from '.$prices[$current_price_id]['name'].' to '.$prices[$args['price_id']]['name']); |
| 104 | } |
| 105 | } else { |
| 106 | $log->setContext('Price ID changed from '.$current_price_id.' to '.$args['price_id']); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | if (!$log->context) { |
| 111 | return; |
| 112 | } |
| 113 | |
| 114 | $log->addMeta('Customer ID', $subscription->customer_id) |
| 115 | ->addMeta('Subscription ID', $subscription_id) |
| 116 | ->addMeta('Product', logtivity_get_the_title($args['product_id'] ?? $current_product)); |
| 117 | |
| 118 | if (!is_user_logged_in()) { |
| 119 | $log->setUser($subscription->customer->user_id ?? null); |
| 120 | } |
| 121 | |
| 122 | if ($subscription->expiration) { |
| 123 | try { |
| 124 | $log->addMeta('Expiration Date', date('M d Y', strtotime($subscription->expiration))); |
| 125 | } catch (Throwable $e) { |
| 126 | // Ignore |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | $log->send(); |
| 131 | } |
| 132 | |
| 133 | public function subscriptionStatusChanged($old_status, $new_status, $subscription) |
| 134 | { |
| 135 | if ($old_status === $new_status) { |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | $log = Logtivity::log() |
| 140 | ->setAction('Subscription '. ucfirst($new_status)) |
| 141 | ->setContext($this->getDownloadTitle($subscription)) |
| 142 | ->addMeta('Old Status', $old_status) |
| 143 | ->addMeta('Customer ID', $subscription->customer_id) |
| 144 | ->addMeta('Subscription ID', $subscription->id); |
| 145 | |
| 146 | if (!is_user_logged_in()) { |
| 147 | $log->setUser($subscription->customer->user_id ?? null); |
| 148 | } |
| 149 | |
| 150 | if ($subscription->expiration) { |
| 151 | try { |
| 152 | $log->addMeta('Expiration Date', date('M d Y', strtotime($subscription->expiration))); |
| 153 | } catch (Throwable $e) { |
| 154 | // Ignore |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | $log->send(); |
| 159 | } |
| 160 | |
| 161 | public function subscriptionDeleted($subscription) |
| 162 | { |
| 163 | $log = Logtivity::log() |
| 164 | ->setAction('Subscription Deleted') |
| 165 | ->setContext($this->getDownloadTitle($subscription)) |
| 166 | ->addMeta('Customer ID', $subscription->customer_id) |
| 167 | ->addMeta('Subscription ID', $subscription->id); |
| 168 | |
| 169 | if (!is_user_logged_in()) { |
| 170 | $log->setUser($subscription->customer->user_id ?? null); |
| 171 | } |
| 172 | |
| 173 | if ($subscription->expiration) { |
| 174 | try { |
| 175 | $log->addMeta('Expiration Date', date('M d Y', strtotime($subscription->expiration))); |
| 176 | } catch (Throwable $e) { |
| 177 | // Ignore |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | $log->send(); |
| 182 | } |
| 183 | |
| 184 | public function setupPaymentMethodUpdate($user_id, $subscription_id, $verified) |
| 185 | { |
| 186 | $subscription = new EDD_Subscription($subscription_id); |
| 187 | |
| 188 | add_action( 'edd_recurring_update_' . $subscription->gateway .'_subscription', [$this, 'paymentMethodUpdated'], 999999999999, 2); |
| 189 | } |
| 190 | |
| 191 | public function paymentMethodUpdated($subscriber, $subscription) |
| 192 | { |
| 193 | $errors = edd_get_errors(); |
| 194 | |
| 195 | if (!empty($errors)) { |
| 196 | return; |
| 197 | } |
| 198 | |
| 199 | $log = Logtivity::log() |
| 200 | ->setAction('Payment Method Updated') |
| 201 | ->setContext($subscription->gateway) |
| 202 | ->addMeta('Customer ID', $subscription->customer_id) |
| 203 | ->addMeta('Subscription ID', $subscription->id); |
| 204 | |
| 205 | if (!is_user_logged_in()) { |
| 206 | $log->setUser($subscription->customer->user_id ?? null); |
| 207 | } |
| 208 | |
| 209 | if ($subscription->expiration) { |
| 210 | try { |
| 211 | $log->addMeta('Expiration Date', date('M d Y', strtotime($subscription->expiration))); |
| 212 | } catch (Throwable $e) { |
| 213 | // Ignore |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | $log->send(); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | new Logtivity_Easy_Digital_Downloads_Recurring(); |
| 222 |