Skip to content

Commit

Permalink
[woocommerce#12517] Get data for get_download_count from download log…
Browse files Browse the repository at this point in the history
… table
  • Loading branch information
procifer committed Jul 31, 2017
1 parent 399269a commit 3953fd0
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 34 deletions.
24 changes: 12 additions & 12 deletions includes/class-wc-customer-download-log.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
/**
* Class for customer download logs.
*
* @version 3.3.0
* @since 3.3.0
* @package WooCommerce/Classes
* @author WooThemes
* @version 3.3.0
* @since 3.3.0
* @package WooCommerce/Classes
* @author WooThemes
*/
class WC_Customer_Download_Log extends WC_Data implements ArrayAccess {

Expand All @@ -25,10 +25,10 @@ class WC_Customer_Download_Log extends WC_Data implements ArrayAccess {
* @var array
*/
protected $data = array(
'timestamp' => null,
'permission_id' => '',
'user_id' => null,
'user_ip_address' => null,
'timestamp' => null,
'permission_id' => 0,
'user_id' => null,
'user_ip_address' => null,
);

/**
Expand Down Expand Up @@ -115,9 +115,9 @@ public function get_user_ip_address( $context = 'view' ) {
*
* @param string|integer|null $date UTC timestamp, or ISO 8601 DateTime. If the DateTime string has no timezone or offset, WordPress site timezone will be assumed. Null if their is no date.
*/
public function set_timestamp( $date = null ) {
public function set_timestamp( $date = null ) {
$this->set_date_prop( 'timestamp', $date );
}
}

/**
* Set permission id.
Expand Down Expand Up @@ -153,8 +153,8 @@ public function set_user_ip_address( $value ) {
*/

/**
* Save data to the database.
*
* Save data to the database.
*
* @return int Log ID
*/
public function save() {
Expand Down
25 changes: 21 additions & 4 deletions includes/class-wc-customer-download.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,22 @@ public function get_access_expires( $context = 'view' ) {
* @return integer
*/
public function get_download_count( $context = 'view' ) {
return $this->get_prop( 'download_count', $context );
// Check for count of download logs
$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 );
}

// Check download count in prop
$download_count_prop = $this->get_prop( 'download_count', $context );

// Return the larger of the two in case they differ.
// If logs are removed for some reason, we should still respect the
// count stored in the prop.
return max( $download_log_count, $download_count_prop );
}

/*
Expand Down Expand Up @@ -279,6 +294,10 @@ public function track_download( $user_id = null, $user_ip_address = null ) {
throw new Exception( __( 'Invalid permission ID.', 'woocommerce' ) );
}

// Increment download count on parameter
$count = $this->get_download_count();
$this->set_download_count( $count + 1 );

// Track download in download log
$download_log = new WC_Customer_Download_Log();
$download_log->set_timestamp( current_time( 'timestamp', true ) );
Expand All @@ -294,10 +313,8 @@ public function track_download( $user_id = null, $user_ip_address = null ) {

$download_log->save();

// Increment download count on parameter
$count = $this->get_download_count();
// Update downloads remaining
$remaining = $this->get_downloads_remaining();
$this->set_download_count( $count + 1 );
if ( '' !== $remaining ) {
$this->set_downloads_remaining( $remaining - 1 );
}
Expand Down
10 changes: 5 additions & 5 deletions includes/class-wc-download-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ public static function download_product() {
$download->get_download_id(),
$download->get_order_id()
);
$download->save();
$download->save();

// Track the download in logs and change remaining/counts
$current_user_id = get_current_user_id();
$ip_address = WC_Geolocation::get_ip_address();
$download->track_download( $current_user_id > 0 ? $current_user_id : null, !empty( $ip_address ) ? $ip_address : null );
// Track the download in logs and change remaining/counts
$current_user_id = get_current_user_id();
$ip_address = WC_Geolocation::get_ip_address();
$download->track_download( $current_user_id > 0 ? $current_user_id : null, !empty( $ip_address ) ? $ip_address : null );

self::download( $product->get_file_download_path( $download->get_download_id() ), $download->get_product_id() );
}
Expand Down
18 changes: 5 additions & 13 deletions includes/data-stores/class-wc-customer-download-log-data-store.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public function get_download_logs( $args = array() ) {
$query[] = "SELECT * FROM {$wpdb->prefix}woocommerce_downloadable_product_download_log WHERE 1=1";

if ( $args['permission_id'] ) {
$query[] = $wpdb->prepare( "AND permission_id = %d", sanitize_email( $args['permission_id'] ) );
$query[] = $wpdb->prepare( "AND permission_id = %d", $args['permission_id'] );
}

if ( $args['user_id'] ) {
Expand Down Expand Up @@ -177,23 +177,15 @@ public function get_download_logs( $args = array() ) {
}

/**
* Get logs for a specific download permission.
* Get download logs for a given download permission.
*
* @param int $permission_id
* @return array
*/
public function get_download_logs_for_permission( $permission_id ) {
global $wpdb;

return $wpdb->get_results(
$wpdb->prepare( "
SELECT * FROM {$wpdb->prefix}woocommerce_downloadable_product_download_log as logs
WHERE permission_id = %d
ORDER BY logs.timestamp DESC;
",
$permission_id
)
);
return $this->get_download_logs( array(
'permission_id' => $permission_id
) );
}

}

0 comments on commit 3953fd0

Please sign in to comment.