<?php defined('ABSPATH') || exit; class WC_Customer_Download extends WC_Data implements ArrayAccess { protected $object_type = 'customer_download'; protected $data = array('download_id' => '', 'product_id' => 0, 'user_id' => 0, 'user_email' => '', 'order_id' => 0, 'order_key' => '', 'downloads_remaining' => '', 'access_granted' => null, 'access_expires' => null, 'download_count' => 0); public function __construct($download = 0) { parent::__construct($download); if (is_numeric($download) && $download > 0) { $this->set_id($download); } elseif ($download instanceof self) { $this->set_id($download->get_id()); } elseif (is_object($download) && !empty($download->permission_id)) { $this->set_id($download->permission_id); $this->set_props((array) $download); $this->set_object_read(true); } else { $this->set_object_read(true); } $this->data_store = WC_Data_Store::load('customer-download'); if ($this->get_id() > 0) { $this->data_store->read($this); } } public function get_download_id($context = 'view') { return $this->get_prop('download_id', $context); } public function get_product_id($context = 'view') { return $this->get_prop('product_id', $context); } public function get_user_id($context = 'view') { return $this->get_prop('user_id', $context); } public function get_user_email($context = 'view') { return $this->get_prop('user_email', $context); } public function get_order_id($context = 'view') { return $this->get_prop('order_id', $context); } public function get_order_key($context = 'view') { return $this->get_prop('order_key', $context); } public function get_downloads_remaining($context = 'view') { return $this->get_prop('downloads_remaining', $context); } public function get_access_granted($context = 'view') { return $this->get_prop('access_granted', $context); } public function get_access_expires($context = 'view') { return $this->get_prop('access_expires', $context); } public function get_download_count($context = 'view') { $data_store = WC_Data_Store::load('customer-download-log'); $download_log_ids = $data_store->get_download_logs_for_permission($this->get_id()); $download_log_count = 0; if (!empty($download_log_ids)) { $download_log_count = count($download_log_ids); } $download_count_prop = $this->get_prop('download_count', $context); return max($download_log_count, $download_count_prop); } public function set_download_id($value) { $this->set_prop('download_id', $value); } public function set_product_id($value) { $this->set_prop('product_id', absint($value)); } public function set_user_id($value) { $this->set_prop('user_id', absint($value)); } public function set_user_email($value) { $this->set_prop('user_email', sanitize_email($value)); } public function set_order_id($value) { $this->set_prop('order_id', absint($value)); } public function set_order_key($value) { $this->set_prop('order_key', $value); } public function set_downloads_remaining($value) { $this->set_prop('downloads_remaining', '' === $value ? '' : absint($value)); } public function set_access_granted($date = null) { $this->set_date_prop('access_granted', $date); } public function set_access_expires($date = null) { $this->set_date_prop('access_expires', $date); } public function set_download_count($value) { $this->set_prop('download_count', absint($value)); } public function track_download($user_id = null, $user_ip_address = null) { global $wpdb; if (!($this->get_id() > 0)) { throw new Exception(__('Invalid permission ID.', 'woocommerce')); } $query = $wpdb->prepare("\nUPDATE {$wpdb->prefix}woocommerce_downloadable_product_permissions\nSET download_count = download_count + 1,\ndownloads_remaining = IF( downloads_remaining = '', '', GREATEST( 0, downloads_remaining - 1 ) )\nWHERE permission_id = %d", $this->get_id()); $wpdb->query($query); $this->data_store->read($this); $download_log = new WC_Customer_Download_Log(); $download_log->set_timestamp(current_time('timestamp', true)); $download_log->set_permission_id($this->get_id()); if (!is_null($user_id)) { $download_log->set_user_id($user_id); } if (!is_null($user_ip_address)) { $download_log->set_user_ip_address($user_ip_address); } $download_log->save(); } public function offsetGet($offset) { if (is_callable(array($this, "get_{$offset}"))) { return $this->{"get_{$offset}"}(); } } public function offsetSet($offset, $value) { if (is_callable(array($this, "set_{$offset}"))) { $this->{"set_{$offset}"}($value); } } public function offsetUnset($offset) { if (is_callable(array($this, "set_{$offset}"))) { $this->{"set_{$offset}"}(''); } } public function offsetExists($offset) { return in_array($offset, array_keys($this->data), true); } public function __isset($key) { return in_array($key, array_keys($this->data), true); } public function __get($key) { if (is_callable(array($this, "get_{$key}"))) { return $this->{"get_{$key}"}(''); } } }