| 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 extends Logtivity_Abstract_Easy_Digital_Downloads |
| 26 |
{ |
| 27 |
public function __construct() |
| 28 |
{ |
| 29 |
add_action('edd_post_add_to_cart', [$this, 'itemAddedToCart'], 10, 3); |
| 30 |
add_action('edd_post_remove_from_cart', [$this, 'itemRemovedFromCart'], 10, 2); |
| 31 |
add_action('edd_customer_post_create', [$this, 'customerCreated'], 10, 2); |
| 32 |
add_action('edd_update_payment_status', [$this, 'paymentStatusUpdated'], 10, 3); |
| 33 |
add_action('edd_process_verified_download', [$this, 'fileDownloaded'], 10, 4); |
| 34 |
add_action('edd_cart_discount_set', [$this, 'discountApplied'], 10, 2); |
| 35 |
add_action('edd_cart_discount_removed', [$this, 'discountRemoved'], 10, 2); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* @param int $downloadId |
| 40 |
* @param array $options |
| 41 |
* @param array $items |
| 42 |
* |
| 43 |
* @return void |
| 44 |
*/ |
| 45 |
public function itemAddedToCart($downloadId, $options, $items): void |
| 46 |
{ |
| 47 |
$log = Logtivity::log() |
| 48 |
->setAction('Download Added to Cart') |
| 49 |
->setContext(logtivity_get_the_title($downloadId)) |
| 50 |
->setPostId($downloadId); |
| 51 |
|
| 52 |
$prices = edd_get_variable_prices($downloadId); |
| 53 |
|
| 54 |
foreach ($items as $item) { |
| 55 |
if (isset($item['options']['price_id']) && isset($prices[$item['options']['price_id']])) { |
| 56 |
$log->addMeta('Variable Item', $prices[$item['options']['price_id']]['name']); |
| 57 |
} |
| 58 |
|
| 59 |
if ($item['quantity']) { |
| 60 |
$log->addMeta('Quantity', $item['quantity']); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
$log->send(); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* @param int $key |
| 69 |
* @param int $itemId |
| 70 |
* |
| 71 |
* @return void |
| 72 |
*/ |
| 73 |
public function itemRemovedFromCart($key, $itemId): void |
| 74 |
{ |
| 75 |
Logtivity::log() |
| 76 |
->setAction('Download Removed from Cart') |
| 77 |
->setContext(logtivity_get_the_title($itemId)) |
| 78 |
->send(); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* @param int $paymentId |
| 83 |
* @param string $status |
| 84 |
* @param string $oldStatus |
| 85 |
* |
| 86 |
* @return void |
| 87 |
*/ |
| 88 |
public function paymentStatusUpdated($paymentId, $status, $oldStatus): void |
| 89 |
{ |
| 90 |
$payment = new EDD_Payment($paymentId); |
| 91 |
switch ($status) { |
| 92 |
case 'refunded': |
| 93 |
$this->paymentRefunded($payment); |
| 94 |
break; |
| 95 |
|
| 96 |
case 'publish': |
| 97 |
$this->paymentCompleted($payment); |
| 98 |
break; |
| 99 |
case 'edd_subscription': |
| 100 |
// Don't log these |
| 101 |
break; |
| 102 |
|
| 103 |
default: |
| 104 |
Logtivity::log() |
| 105 |
->setAction('Payment Status Changed to ' . ucfirst($status)) |
| 106 |
->setContext($this->getPaymentKey($payment)) |
| 107 |
->addMeta('Payment Key', $this->getPaymentKey($payment)) |
| 108 |
->addMeta('Total', $payment->total) |
| 109 |
->addMeta('Currency', $payment->currency) |
| 110 |
->addMeta('Gateway', $payment->gateway) |
| 111 |
->addMeta('Customer ID', $payment->customer_id) |
| 112 |
->addMeta('Prevous Status', ucfirst($oldStatus)) |
| 113 |
->send(); |
| 114 |
break; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* @param EDD_Payment $payment |
| 120 |
* |
| 121 |
* @return void |
| 122 |
*/ |
| 123 |
protected function paymentCompleted(EDD_Payment $payment): void |
| 124 |
{ |
| 125 |
$key = $this->getPaymentKey($payment); |
| 126 |
|
| 127 |
$log = Logtivity::log() |
| 128 |
->setAction('Payment Completed') |
| 129 |
->setContext($key) |
| 130 |
->addMeta('Payment Key', $key); |
| 131 |
|
| 132 |
foreach ($payment->cart_details as $item) { |
| 133 |
$log->addMeta('Cart Item', $item['name']); |
| 134 |
} |
| 135 |
|
| 136 |
if ($payment->discounts != 'none') { |
| 137 |
$log->addMeta('Discount Code', $payment->discounts); |
| 138 |
} |
| 139 |
|
| 140 |
$log->addMeta('Total', $payment->total) |
| 141 |
->addMeta('Currency', $payment->currency) |
| 142 |
->addMeta('Gateway', $payment->gateway) |
| 143 |
->addMeta('Customer ID', $payment->customer_id) |
| 144 |
->send(); |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* @param EDD_Payment $payment |
| 149 |
* |
| 150 |
* @return void |
| 151 |
*/ |
| 152 |
protected function paymentRefunded(EDD_Payment $payment): void |
| 153 |
{ |
| 154 |
Logtivity::log() |
| 155 |
->setAction('Payment Refunded') |
| 156 |
->setContext($this->getPaymentKey($payment)) |
| 157 |
->addMeta('Amount', $payment->get_meta('_edd_payment_total')) |
| 158 |
->addMeta('Payment Key', $this->getPaymentKey($payment)) |
| 159 |
->addMeta('Customer ID', $payment->customer_id) |
| 160 |
->send(); |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* @param EDD_Payment $payment |
| 165 |
* |
| 166 |
* @return ?string |
| 167 |
*/ |
| 168 |
protected function getPaymentKey(EDD_Payment $payment): ?string |
| 169 |
{ |
| 170 |
$meta = $payment->get_meta(); |
| 171 |
|
| 172 |
return $meta['key'] ?? null; |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* @param int $created |
| 177 |
* @param array $args |
| 178 |
* |
| 179 |
* @return void |
| 180 |
*/ |
| 181 |
public function customerCreated($created, $args): void |
| 182 |
{ |
| 183 |
if ($created) { |
| 184 |
Logtivity::log() |
| 185 |
->setAction('Customer Created') |
| 186 |
->setContext($args['name'] ?? null) |
| 187 |
->addMeta('Customer ID', $created) |
| 188 |
->send(); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* @param int $downloadId |
| 194 |
* @param string $email |
| 195 |
* @param int|false $paymentId |
| 196 |
* @param array $args |
| 197 |
* |
| 198 |
* @return void |
| 199 |
*/ |
| 200 |
public function fileDownloaded($downloadId, $email, $paymentId, $args): void |
| 201 |
{ |
| 202 |
$download = new EDD_Download($downloadId); |
| 203 |
|
| 204 |
$payment = new EDD_Payment($paymentId); |
| 205 |
|
| 206 |
$fileKey = $args['file_key'] ?? null; |
| 207 |
|
| 208 |
Logtivity::log() |
| 209 |
->setAction('File Downloaded') |
| 210 |
->setContext($this->getDownloadTitle($download->get_ID(), $args['price_id'] ?? null)) |
| 211 |
->setPostId($download->get_ID()) |
| 212 |
->addMeta('Payment Key', $this->getPaymentKey($payment)) |
| 213 |
->addMetaIf($fileKey, 'File ID', $fileKey) |
| 214 |
->addMeta('Customer ID', $payment->customer_id) |
| 215 |
->send(); |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* @param string $code |
| 220 |
* @param array $discounts |
| 221 |
* |
| 222 |
* @return void |
| 223 |
*/ |
| 224 |
public function discountApplied($code, $discounts): void |
| 225 |
{ |
| 226 |
Logtivity::log() |
| 227 |
->setAction('Discount Applied') |
| 228 |
->setContext($code) |
| 229 |
->send(); |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* @param string $code |
| 234 |
* @param array $discounts |
| 235 |
* |
| 236 |
* @return void |
| 237 |
*/ |
| 238 |
public function discountRemoved($code, $discounts): void |
| 239 |
{ |
| 240 |
Logtivity::log() |
| 241 |
->setAction('Discount Removed') |
| 242 |
->setContext($code) |
| 243 |
->send(); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
new Logtivity_Easy_Digital_Downloads(); |
| 248 |
|