| 1 |
<?php |
| 2 |
|
| 3 |
class Logtivity_Easy_Digital_Downloads_Software_Licensing extends Logtivity_Abstract_Easy_Digital_Downloads |
| 4 |
{ |
| 5 |
protected $deactivatingLicenseArgs; |
| 6 |
|
| 7 |
public function __construct() |
| 8 |
{ |
| 9 |
add_action('edd_sl_store_license', [$this, 'licenseCreated'], 10, 4); |
| 10 |
add_filter('edd_sl_post_activate_license_result', [$this, 'licenseActivated'], 10, 2); |
| 11 |
add_filter('edd_sl_pre_deactivate_license_args', [$this, 'deactivatingLicense']); |
| 12 |
add_action('edd_sl_deactivate_license', [$this, 'licenseDeactivated'], 10, 2); |
| 13 |
add_action('edd_sl_license_upgraded', [$this, 'licenseUpgraded'], 10, 2); |
| 14 |
add_action('edd_sl_post_set_status', [$this, 'licenseStatusUpdated'], 10, 2); |
| 15 |
add_action('edd_sl_post_license_renewal', [$this, 'licenseRenewed'], 10, 2); |
| 16 |
add_action('edd_deactivate_site', [$this, 'siteDeactivated'], 9); |
| 17 |
add_action('edd_insert_site', [$this, 'siteAdded'], 9); |
| 18 |
} |
| 19 |
|
| 20 |
public function licenseCreated($license_id, $purchased_download_id, $payment_id, $type) |
| 21 |
{ |
| 22 |
$license = edd_software_licensing()->get_license($license_id); |
| 23 |
|
| 24 |
Logtivity_Logger::log() |
| 25 |
->setAction('License Created') |
| 26 |
->setContext($this->getDownloadTitle($license)) |
| 27 |
->setUser($license->user_id ?? null) |
| 28 |
->addMeta('License Key', $license->key) |
| 29 |
->addMeta('Customer ID', $license->customer_id) |
| 30 |
->addMeta('Payment ID', $payment_id) |
| 31 |
->addMeta('Type', $type) |
| 32 |
->addMeta('Download ID', $purchased_download_id) |
| 33 |
->send(); |
| 34 |
} |
| 35 |
|
| 36 |
public function licenseActivated($result, $args) |
| 37 |
{ |
| 38 |
$license = edd_software_licensing()->get_license($args['key'], true); |
| 39 |
|
| 40 |
$log = Logtivity_Logger::log() |
| 41 |
->addMeta('License Key', $args['key']); |
| 42 |
|
| 43 |
if ( false !== $license ) { |
| 44 |
$log->setContext($this->getDownloadTitle($license)); |
| 45 |
|
| 46 |
if (!is_user_logged_in()) { |
| 47 |
$log->setUser($license->user_id ?? null); |
| 48 |
} |
| 49 |
try { |
| 50 |
if ($license->customer_id) { |
| 51 |
$log->addMeta('Customer ID', $license->customer_id); |
| 52 |
} |
| 53 |
} catch (\Exception $e) { |
| 54 |
} |
| 55 |
} else { |
| 56 |
$log->setContext($args['key']); |
| 57 |
} |
| 58 |
|
| 59 |
if (isset($args['url'])) { |
| 60 |
$log->addMeta('Site', $args['url']); |
| 61 |
} |
| 62 |
|
| 63 |
if ($result['success']) { |
| 64 |
$log->setAction('License Activated'); |
| 65 |
|
| 66 |
if (isset($result['site_count'])) { |
| 67 |
$log->addMeta('Activation Count', $result['site_count']); |
| 68 |
} |
| 69 |
|
| 70 |
if (isset($result['license_limit'])) { |
| 71 |
$log->addMeta('Activation Limit', $result['license_limit']); |
| 72 |
} |
| 73 |
|
| 74 |
if (isset($result['expires']) && $result['expires']) { |
| 75 |
try { |
| 76 |
$log->addMeta('Expiration Date', date('M d Y', $result['expires'])); |
| 77 |
} catch (\Exception $e) { |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
$log->send(); |
| 82 |
|
| 83 |
return $result; |
| 84 |
} |
| 85 |
|
| 86 |
$log->setAction('License Activation Failed') |
| 87 |
->addMeta('Reason', $result['error'] ?? ''); |
| 88 |
|
| 89 |
$log->send(); |
| 90 |
|
| 91 |
return $result; |
| 92 |
} |
| 93 |
|
| 94 |
public function deactivatingLicense($args) |
| 95 |
{ |
| 96 |
$this->deactivatingLicenseArgs = $args; |
| 97 |
|
| 98 |
return $args; |
| 99 |
} |
| 100 |
|
| 101 |
public function licenseDeactivated($license_id, $download_id) |
| 102 |
{ |
| 103 |
$license = edd_software_licensing()->get_license($this->deactivatingLicenseArgs['key'], true); |
| 104 |
|
| 105 |
if ( empty( $this->deactivatingLicenseArgs['url'] ) && ! edd_software_licensing()->force_increase() ) { |
| 106 |
|
| 107 |
// Attempt to grab the URL from the user agent if no URL is specified |
| 108 |
$domain = array_map( 'trim', explode( ';', $_SERVER['HTTP_USER_AGENT'] ) ); |
| 109 |
|
| 110 |
if ( ! empty( $domain[1] ) ) { |
| 111 |
$this->deactivatingLicenseArgs['url'] = trim( $domain[1] ); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
$log = Logtivity_Logger::log() |
| 116 |
->setAction('License Deactivated') |
| 117 |
->setContext($this->getDownloadTitle($license)) |
| 118 |
->addMeta('License Key', $this->deactivatingLicenseArgs['key']); |
| 119 |
|
| 120 |
if (!is_user_logged_in()) { |
| 121 |
$log->setUser($license->user_id ?? null); |
| 122 |
} |
| 123 |
|
| 124 |
try { |
| 125 |
if ($license->customer_id) { |
| 126 |
$log->addMeta('Customer ID', $license->customer_id); |
| 127 |
} |
| 128 |
} catch (\Exception $e) { |
| 129 |
} |
| 130 |
|
| 131 |
$log->addMeta('Site', $this->deactivatingLicenseArgs['url'] ?? $domain ?? null) |
| 132 |
->send(); |
| 133 |
} |
| 134 |
|
| 135 |
public function licenseUpgraded($license_id, $args) |
| 136 |
{ |
| 137 |
$license = edd_software_licensing()->get_license($license_id); |
| 138 |
|
| 139 |
$log = Logtivity_Logger::log() |
| 140 |
->setAction('License Upgraded') |
| 141 |
->setContext($this->getDownloadTitle($license)) |
| 142 |
->addMeta('License Key', $license->key); |
| 143 |
|
| 144 |
if (!is_user_logged_in()) { |
| 145 |
$log->setUser($license->user_id ?? null); |
| 146 |
} |
| 147 |
|
| 148 |
if (isset($args['payment_id'])) { |
| 149 |
$log->addMeta('Payment ID', $args['payment_id']); |
| 150 |
} |
| 151 |
|
| 152 |
if (isset($args['old_payment_id'])) { |
| 153 |
$log->addMeta('Old Payment ID', $args['old_payment_id']); |
| 154 |
} |
| 155 |
|
| 156 |
if (isset($args['download_id'])) { |
| 157 |
$log->addMeta('Download ID', $args['download_id']); |
| 158 |
} |
| 159 |
|
| 160 |
if (isset($args['old_download_id'])) { |
| 161 |
$log->addMeta('Old Download ID', $args['old_download_id']); |
| 162 |
} |
| 163 |
|
| 164 |
if (isset($args['old_price_id']) && $args['old_price_id']) { |
| 165 |
$log->addMeta('Old Price ID', $args['old_price_id']); |
| 166 |
} |
| 167 |
|
| 168 |
if (isset($args['upgrade_id']) && $args['upgrade_id']) { |
| 169 |
$log->addMeta('Upgrade ID', $args['upgrade_id']); |
| 170 |
} |
| 171 |
|
| 172 |
if (isset($args['upgrade_price_id']) && $args['upgrade_price_id']) { |
| 173 |
$log->addMeta('Upgrade Price ID', $args['upgrade_price_id']); |
| 174 |
} |
| 175 |
|
| 176 |
$log->addMeta('Customer ID', $license->customer_id) |
| 177 |
->send(); |
| 178 |
} |
| 179 |
|
| 180 |
public function licenseStatusUpdated($license_id, $status) |
| 181 |
{ |
| 182 |
$license = edd_software_licensing()->get_license($license_id); |
| 183 |
|
| 184 |
$log = Logtivity_Logger::log() |
| 185 |
->setAction('License Status Changed to ' . ucfirst($status)) |
| 186 |
->setContext($this->getDownloadTitle($license)) |
| 187 |
->addMeta('License Key', $license->key); |
| 188 |
|
| 189 |
if (!is_user_logged_in()) { |
| 190 |
$log->setUser($license->user_id ?? null); |
| 191 |
} |
| 192 |
|
| 193 |
$log->addMeta('Customer ID', $license->customer_id) |
| 194 |
->send(); |
| 195 |
} |
| 196 |
|
| 197 |
public function licenseRenewed($license_id, $new_expiration) |
| 198 |
{ |
| 199 |
$license = edd_software_licensing()->get_license($license_id); |
| 200 |
|
| 201 |
$log = Logtivity_Logger::log() |
| 202 |
->setAction('License Renewed') |
| 203 |
->setContext($this->getDownloadTitle($license)) |
| 204 |
->addMeta('License Key', $license->key) |
| 205 |
->addMeta('Customer ID', $license->customer_id); |
| 206 |
|
| 207 |
if (!is_user_logged_in()) { |
| 208 |
$log->setUser($license->user_id ?? null); |
| 209 |
} |
| 210 |
|
| 211 |
try { |
| 212 |
$log->addMeta('New Expiration Date', date('M d Y', $new_expiration)); |
| 213 |
} catch (\Exception $e) { |
| 214 |
} |
| 215 |
|
| 216 |
$log->send(); |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* |
| 221 |
* Taken from edd_sl_process_deactivate_site as there are no hooks for when a |
| 222 |
* user deactivates a site from within their dashboard. |
| 223 |
* |
| 224 |
* Use same validation code as in the original method, then log it. |
| 225 |
* |
| 226 |
*/ |
| 227 |
public function siteDeactivated() |
| 228 |
{ |
| 229 |
if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'edd_deactivate_site_nonce' ) ) { |
| 230 |
return; |
| 231 |
} |
| 232 |
|
| 233 |
$license_id = absint( $_GET['license'] ); |
| 234 |
$license = edd_software_licensing()->get_license( $license_id ); |
| 235 |
|
| 236 |
if ( $license_id !== $license->ID ) { |
| 237 |
return; |
| 238 |
} |
| 239 |
|
| 240 |
if ( ( is_admin() && ! current_user_can( 'manage_licenses' ) ) || ( ! is_admin() && $license->user_id != get_current_user_id() ) ) { |
| 241 |
return; |
| 242 |
} |
| 243 |
|
| 244 |
$site_url = ! empty( $_GET['site_url'] ) ? urldecode( $_GET['site_url'] ) : false; |
| 245 |
$site_id = ! empty( $_GET['site_id'] ) ? absint( $_GET['site_id'] ) : false; |
| 246 |
|
| 247 |
if ( empty( $site_url ) && empty( $site_id ) ) { |
| 248 |
return; |
| 249 |
} |
| 250 |
|
| 251 |
$site = ! empty( $site_id ) ? $site_id : $site_url; |
| 252 |
|
| 253 |
/** |
| 254 |
* If we've made it this far let's log it. |
| 255 |
*/ |
| 256 |
|
| 257 |
$log = Logtivity_Logger::log() |
| 258 |
->setAction('Site Deactivated') |
| 259 |
->setContext($this->getDownloadTitle($license)) |
| 260 |
->addMeta('License Key', $license->key) |
| 261 |
->addMeta('Site ID', $site); |
| 262 |
|
| 263 |
if (!is_user_logged_in()) { |
| 264 |
$log->setUser($license->user_id ?? null); |
| 265 |
} |
| 266 |
|
| 267 |
try { |
| 268 |
if ($license->customer_id) { |
| 269 |
$log->addMeta('Customer ID', $license->customer_id); |
| 270 |
} |
| 271 |
} catch (\Exception $e) { |
| 272 |
} |
| 273 |
|
| 274 |
$log->send(); |
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
* |
| 279 |
* Taken from edd_sl_process_add_site as there are no hooks for when a |
| 280 |
* user adds a site from within their dashboard. |
| 281 |
* |
| 282 |
* Use same validation code as in the original method, then log it. |
| 283 |
* |
| 284 |
*/ |
| 285 |
public function siteAdded() |
| 286 |
{ |
| 287 |
if ( ! wp_verify_nonce( $_POST['edd_add_site_nonce'], 'edd_add_site_nonce' ) ) { |
| 288 |
return; |
| 289 |
} |
| 290 |
|
| 291 |
if ( ! empty( $_POST['license_id'] ) && empty( $_POST['license'] ) ) { |
| 292 |
// In 3.5, we switched from checking for license_id to just license. Fallback check for backwards compatibility |
| 293 |
$_POST['license'] = $_POST['license_id']; |
| 294 |
} |
| 295 |
|
| 296 |
$license_id = absint( $_POST['license'] ); |
| 297 |
$license = edd_software_licensing()->get_license( $license_id ); |
| 298 |
if ( $license_id !== $license->ID ) { |
| 299 |
return; |
| 300 |
} |
| 301 |
|
| 302 |
if ( ( is_admin() && ! current_user_can( 'manage_licenses' ) ) || ( ! is_admin() && $license->user_id != get_current_user_id() ) ) { |
| 303 |
return; |
| 304 |
} |
| 305 |
|
| 306 |
$site_url = sanitize_text_field( $_POST['site_url'] ); |
| 307 |
|
| 308 |
if ( $license->is_at_limit() && ! current_user_can( 'manage_licenses' ) ) { |
| 309 |
// The license is at its activation limit so stop and show an error |
| 310 |
return; |
| 311 |
} |
| 312 |
|
| 313 |
/** |
| 314 |
* If we've made it this far let's log it. |
| 315 |
*/ |
| 316 |
|
| 317 |
$log = Logtivity_Logger::log() |
| 318 |
->setAction('Site Added') |
| 319 |
->setContext($this->getDownloadTitle($license)) |
| 320 |
->addMeta('License Key', $license->key) |
| 321 |
->addMeta('Site', $site_url); |
| 322 |
|
| 323 |
if (!is_user_logged_in()) { |
| 324 |
$log->setUser($license->user_id ?? null); |
| 325 |
} |
| 326 |
|
| 327 |
try { |
| 328 |
if ($license->customer_id) { |
| 329 |
$log->addMeta('Customer ID', $license->customer_id); |
| 330 |
} |
| 331 |
} catch (\Exception $e) { |
| 332 |
} |
| 333 |
|
| 334 |
$log->send(); |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
$Logtivity_Easy_Digital_Downloads_Software_Licensing = new Logtivity_Easy_Digital_Downloads_Software_Licensing; |