WC_Customer_Download::track_download( int $user_id = null, string $user_ip_address = null )
Track a download on this permission.
Description Description
Parameters Parameters
- $user_id
-
(Optional) Id of the user performing the download.
Default value: null
- $user_ip_address
-
(Optional) IP Address of the user performing the download.
Default value: null
Source Source
File: includes/class-wc-customer-download.php
public function track_download( $user_id = null, $user_ip_address = null ) {
global $wpdb;
// Must have a permission_id to track download log.
if ( ! ( $this->get_id() > 0 ) ) {
throw new Exception( __( 'Invalid permission ID.', 'woocommerce' ) );
}
// Increment download count, and decrement downloads remaining.
// Use SQL to avoid possible issues with downloads in quick succession.
// If downloads_remaining is blank, leave it blank (unlimited).
// Also, ensure downloads_remaining doesn't drop below zero.
$query = $wpdb->prepare(
"
UPDATE {$wpdb->prefix}woocommerce_downloadable_product_permissions
SET download_count = download_count + 1,
downloads_remaining = IF( downloads_remaining = '', '', GREATEST( 0, downloads_remaining - 1 ) )
WHERE permission_id = %d",
$this->get_id()
);
$wpdb->query( $query ); // WPCS: unprepared SQL ok.
// Re-read this download from the data store to pull updated counts.
$this->data_store->read( $this );
// Track download in download log.
$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();
}
Changelog Changelog
| Version | Description |
|---|---|
| 3.3.0 | Introduced. |