| 1 |
<?php |
| 2 |
|
| 3 |
class Logtivity_Easy_Digital_Downloads extends Logtivity_Abstract_Logger |
| 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, 3); |
| 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(get_the_title($download_id)); |
| 21 |
|
| 22 |
$prices = edd_get_variable_prices($download_id); |
| 23 |
|
| 24 |
foreach ($items as $item) { |
| 25 |
if (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 |
$log = Logtivity_Logger::log() |
| 40 |
->setAction('Download Removed from Cart') |
| 41 |
->setContext(get_the_title($item_id)); |
| 42 |
|
| 43 |
$prices = edd_get_variable_prices($item_id); |
| 44 |
|
| 45 |
if ($prices && count($prices)) { |
| 46 |
if (isset($prices[$key]['name'])) { |
| 47 |
$log->addMeta('Variable Item', $prices[$key]['name']); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
$log->send(); |
| 52 |
} |
| 53 |
|
| 54 |
public function paymentStatusUpdated($payment_id, $status, $old_status) |
| 55 |
{ |
| 56 |
$payment = new EDD_Payment($payment_id); |
| 57 |
|
| 58 |
if ($status == 'refunded') { |
| 59 |
return $this->paymentRefunded($payment); |
| 60 |
} |
| 61 |
|
| 62 |
if ($status == 'publish') { |
| 63 |
return $this->paymentCompleted($payment); |
| 64 |
} |
| 65 |
|
| 66 |
Logtivity_Logger::log() |
| 67 |
->setAction('Payment ' . ucfirst($status)) |
| 68 |
->setContext($this->getPaymentKey($payment)) |
| 69 |
->addMeta('Payment Key', $this->getPaymentKey($payment)) |
| 70 |
->send(); |
| 71 |
} |
| 72 |
|
| 73 |
public function paymentCompleted($payment) |
| 74 |
{ |
| 75 |
$key = $this->getPaymentKey($payment); |
| 76 |
|
| 77 |
$log = Logtivity_Logger::log() |
| 78 |
->setAction('Payment Completed') |
| 79 |
->setContext($key) |
| 80 |
->addMeta('Payment Key', $key); |
| 81 |
|
| 82 |
foreach ($payment->cart_details as $item) { |
| 83 |
$log->addMeta('Cart Item', $item['name']); |
| 84 |
} |
| 85 |
|
| 86 |
$customer = new EDD_Customer($payment->get_meta( '_edd_payment_customer_id', true )); |
| 87 |
|
| 88 |
if ($payment->discounts != 'none') { |
| 89 |
$log->addMeta('Discount Code', $payment->discounts); |
| 90 |
} |
| 91 |
|
| 92 |
$log->addMeta('Total', $payment->total) |
| 93 |
->addMeta('Currency', $payment->currency) |
| 94 |
->addMeta('Gateway', $payment->gateway) |
| 95 |
->addMeta('Customer ID', $customer->id) |
| 96 |
->send(); |
| 97 |
} |
| 98 |
|
| 99 |
public function paymentRefunded($EDD_Payment) |
| 100 |
{ |
| 101 |
Logtivity_Logger::log() |
| 102 |
->setAction('Payment Refunded') |
| 103 |
->setContext($this->getPaymentKey($EDD_Payment)) |
| 104 |
->addMeta('Amount', $EDD_Payment->get_meta('_edd_payment_total')) |
| 105 |
->addMeta('Payment Key', $this->getPaymentKey($EDD_Payment)) |
| 106 |
->send(); |
| 107 |
} |
| 108 |
|
| 109 |
public function getPaymentKey($payment) |
| 110 |
{ |
| 111 |
$meta = $payment->get_meta(); |
| 112 |
|
| 113 |
if (isset($meta['key'])) { |
| 114 |
return $meta['key']; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
public function customerCreated($created, $args) |
| 119 |
{ |
| 120 |
if (!$created) { |
| 121 |
return; |
| 122 |
} |
| 123 |
|
| 124 |
Logtivity_Logger::log() |
| 125 |
->setAction('Customer Created') |
| 126 |
->setContext($args['name']) |
| 127 |
->addMeta('Customer ID', $created) |
| 128 |
->send(); |
| 129 |
} |
| 130 |
|
| 131 |
public function fileDownloaded($download, $email, $payment, $args) |
| 132 |
{ |
| 133 |
$download = new EDD_Download($download); |
| 134 |
|
| 135 |
$payment = new EDD_Payment($payment); |
| 136 |
|
| 137 |
$customer = new EDD_Customer($payment->get_meta( '_edd_payment_customer_id', true )); |
| 138 |
|
| 139 |
$log = Logtivity_Logger::log() |
| 140 |
->setAction('File Downloaded') |
| 141 |
->setContext($download->post_title) |
| 142 |
->addMeta('Payment Key', $this->getPaymentKey($payment)); |
| 143 |
|
| 144 |
if (isset($args['price_id'])) { |
| 145 |
$prices = edd_get_variable_prices($download->get_ID()); |
| 146 |
|
| 147 |
if ($prices && count($prices)) { |
| 148 |
if (isset($prices[$args['price_id']]['name'])) { |
| 149 |
$log->addMeta('Variable Item', $prices[$args['price_id']]['name']); |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
$log->addMeta('Customer ID', $customer->id) |
| 155 |
->send(); |
| 156 |
} |
| 157 |
|
| 158 |
public function discountApplied($code, $discounts) |
| 159 |
{ |
| 160 |
Logtivity_Logger::log() |
| 161 |
->setAction('Discount Applied') |
| 162 |
->setContext($code) |
| 163 |
->send(); |
| 164 |
} |
| 165 |
|
| 166 |
public function discountRemoved($code, $discounts) |
| 167 |
{ |
| 168 |
Logtivity_Logger::log() |
| 169 |
->setAction('Discount Removed') |
| 170 |
->setContext($code) |
| 171 |
->send(); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
$Logtivity_Easy_Digital_Downloads = new Logtivity_Easy_Digital_Downloads; |
| 176 |
|
| 177 |
|