| 1 |
<?php |
| 2 |
|
| 3 |
class Logtivity_Easy_Digital_Downloads extends Logtivity_Abstract_Easy_Digital_Downloads |
| 4 |
{ |
| 5 |
public function __construct() |
| 6 |
{ |
| 7 |
add_action('edd_post_add_to_cart', [$this, 'itemAddedToCart'], 10, 3); |
| 8 |
add_action('edd_post_remove_from_cart', [$this, 'itemRemovedFromCart'], 10, 2); |
| 9 |
add_action('edd_customer_post_create', [$this, 'customerCreated'], 10, 2); |
| 10 |
add_action('edd_update_payment_status', [$this, 'paymentStatusUpdated'], 10, 3); |
| 11 |
add_action('edd_process_verified_download', [$this, 'fileDownloaded'], 10, 4); |
| 12 |
add_action( 'edd_cart_discount_set', [$this, 'discountApplied'], 10, 2); |
| 13 |
add_action( 'edd_cart_discount_removed', [$this, 'discountRemoved'], 10, 2); |
| 14 |
} |
| 15 |
|
| 16 |
public function itemAddedToCart($download_id, $options, $items) |
| 17 |
{ |
| 18 |
$log = Logtivity_Logger::log() |
| 19 |
->setAction('Download Added to Cart') |
| 20 |
->setContext(logtivity_get_the_title($download_id)); |
| 21 |
|
| 22 |
$prices = edd_get_variable_prices($download_id); |
| 23 |
|
| 24 |
foreach ($items as $item) { |
| 25 |
if (isset($item['options']['price_id']) && isset($prices[$item['options']['price_id']])) { |
| 26 |
$log->addMeta('Variable Item', $prices[$item['options']['price_id']]['name']); |
| 27 |
} |
| 28 |
|
| 29 |
if ($item['quantity']) { |
| 30 |
$log->addMeta('Quantity', $item['quantity']); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
$log->send(); |
| 35 |
} |
| 36 |
|
| 37 |
public function itemRemovedFromCart($key, $item_id) |
| 38 |
{ |
| 39 |
Logtivity_Logger::log() |
| 40 |
->setAction('Download Removed from Cart') |
| 41 |
->setContext(logtivity_get_the_title($item_id)) |
| 42 |
->send(); |
| 43 |
} |
| 44 |
|
| 45 |
public function paymentStatusUpdated($payment_id, $status, $old_status) |
| 46 |
{ |
| 47 |
$payment = new EDD_Payment($payment_id); |
| 48 |
|
| 49 |
if ($status == 'refunded') { |
| 50 |
return $this->paymentRefunded($payment); |
| 51 |
} |
| 52 |
|
| 53 |
if ($status == 'publish') { |
| 54 |
return $this->paymentCompleted($payment); |
| 55 |
} |
| 56 |
|
| 57 |
if ($status == 'edd_subscription') { |
| 58 |
return; |
| 59 |
} |
| 60 |
|
| 61 |
Logtivity_Logger::log() |
| 62 |
->setAction('Payment Status Changed to ' . ucfirst($status)) |
| 63 |
->setContext($this->getPaymentKey($payment)) |
| 64 |
->addMeta('Payment Key', $this->getPaymentKey($payment)) |
| 65 |
->addMeta('Total', $payment->total) |
| 66 |
->addMeta('Currency', $payment->currency) |
| 67 |
->addMeta('Gateway', $payment->gateway) |
| 68 |
->addMeta('Customer ID', $payment->customer_id) |
| 69 |
->send(); |
| 70 |
} |
| 71 |
|
| 72 |
public function paymentCompleted($payment) |
| 73 |
{ |
| 74 |
$key = $this->getPaymentKey($payment); |
| 75 |
|
| 76 |
$log = Logtivity_Logger::log() |
| 77 |
->setAction('Payment Completed') |
| 78 |
->setContext($key) |
| 79 |
->addMeta('Payment Key', $key); |
| 80 |
|
| 81 |
foreach ($payment->cart_details as $item) { |
| 82 |
$log->addMeta('Cart Item', $item['name']); |
| 83 |
} |
| 84 |
|
| 85 |
if ($payment->discounts != 'none') { |
| 86 |
$log->addMeta('Discount Code', $payment->discounts); |
| 87 |
} |
| 88 |
|
| 89 |
$log->addMeta('Total', $payment->total) |
| 90 |
->addMeta('Currency', $payment->currency) |
| 91 |
->addMeta('Gateway', $payment->gateway) |
| 92 |
->addMeta('Customer ID', $payment->customer_id) |
| 93 |
->send(); |
| 94 |
} |
| 95 |
|
| 96 |
public function paymentRefunded($EDD_Payment) |
| 97 |
{ |
| 98 |
Logtivity_Logger::log() |
| 99 |
->setAction('Payment Refunded') |
| 100 |
->setContext($this->getPaymentKey($EDD_Payment)) |
| 101 |
->addMeta('Amount', $EDD_Payment->get_meta('_edd_payment_total')) |
| 102 |
->addMeta('Payment Key', $this->getPaymentKey($EDD_Payment)) |
| 103 |
->addMeta('Customer ID', $EDD_Payment->customer_id) |
| 104 |
->send(); |
| 105 |
} |
| 106 |
|
| 107 |
public function getPaymentKey($payment) |
| 108 |
{ |
| 109 |
$meta = $payment->get_meta(); |
| 110 |
|
| 111 |
if (isset($meta['key'])) { |
| 112 |
return $meta['key']; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
public function customerCreated($created, $args) |
| 117 |
{ |
| 118 |
if (!$created) { |
| 119 |
return; |
| 120 |
} |
| 121 |
|
| 122 |
Logtivity_Logger::log() |
| 123 |
->setAction('Customer Created') |
| 124 |
->setContext($args['name']) |
| 125 |
->addMeta('Customer ID', $created) |
| 126 |
->send(); |
| 127 |
} |
| 128 |
|
| 129 |
public function fileDownloaded($download, $email, $payment, $args) |
| 130 |
{ |
| 131 |
$download = new EDD_Download($download); |
| 132 |
|
| 133 |
$payment = new EDD_Payment($payment); |
| 134 |
|
| 135 |
$log = Logtivity_Logger::log() |
| 136 |
->setAction('File Downloaded') |
| 137 |
->setContext($this->getDownloadTitle($download->get_ID(), $args['price_id'] ?? null)) |
| 138 |
->addMeta('Payment Key', $this->getPaymentKey($payment)); |
| 139 |
|
| 140 |
if (isset($args['file_key'])) { |
| 141 |
$log->addMeta('File ID', $args['file_key']); |
| 142 |
} |
| 143 |
|
| 144 |
$log->addMeta('Customer ID', $payment->customer_id) |
| 145 |
->send(); |
| 146 |
} |
| 147 |
|
| 148 |
public function discountApplied($code, $discounts) |
| 149 |
{ |
| 150 |
Logtivity_Logger::log() |
| 151 |
->setAction('Discount Applied') |
| 152 |
->setContext($code) |
| 153 |
->send(); |
| 154 |
} |
| 155 |
|
| 156 |
public function discountRemoved($code, $discounts) |
| 157 |
{ |
| 158 |
Logtivity_Logger::log() |
| 159 |
->setAction('Discount Removed') |
| 160 |
->setContext($code) |
| 161 |
->send(); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
$Logtivity_Easy_Digital_Downloads = new Logtivity_Easy_Digital_Downloads; |
| 166 |
|
| 167 |
|