Optimize the method that gets the downloads count for a given download (#49008)
The old code was retrieving all the existing download entries from the database and then counting them locally. Now a SQL COUNT query is used instead. --------- Co-authored-by: Naman Malhotra <naman03malhotra@gmail.com>
This commit is contained in:
parent
2a90d1e06b
commit
c4ddaef3bc
|
@ -0,0 +1,4 @@
|
|||
Significance: patch
|
||||
Type: update
|
||||
|
||||
Optimize the method that gets the downloads count for a given download
|
|
@ -172,12 +172,7 @@ class WC_Customer_Download extends WC_Data implements ArrayAccess {
|
|||
public function get_download_count( $context = 'view' ) {
|
||||
// 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 );
|
||||
}
|
||||
$download_log_count = $data_store->get_download_logs_count_for_permission( $this->get_id() );
|
||||
|
||||
// Check download count in prop.
|
||||
$download_count_prop = $this->get_prop( 'download_count', $context );
|
||||
|
|
|
@ -149,10 +149,10 @@ class WC_Customer_Download_Log_Data_Store implements WC_Customer_Download_Log_Da
|
|||
}
|
||||
|
||||
/**
|
||||
* Get array of download log ids by specified args.
|
||||
* Get array of download logs, or the count of existing logs, by specified args.
|
||||
*
|
||||
* @param array $args Arguments to define download logs to retrieve.
|
||||
* @return array
|
||||
* @param array $args Arguments to define download logs to retrieve. If $args['return'] is 'count' then the count of existing logs will be returned.
|
||||
* @return array|int
|
||||
*/
|
||||
public function get_download_logs( $args = array() ) {
|
||||
global $wpdb;
|
||||
|
@ -171,9 +171,11 @@ class WC_Customer_Download_Log_Data_Store implements WC_Customer_Download_Log_Da
|
|||
)
|
||||
);
|
||||
|
||||
$is_count = 'count' === $args['return'];
|
||||
|
||||
$query = array();
|
||||
$table = $wpdb->prefix . self::get_table_name();
|
||||
$query[] = "SELECT * FROM {$table} WHERE 1=1";
|
||||
$query[] = 'SELECT ' . ( $is_count ? 'COUNT(1)' : '*' ) . " FROM {$table} WHERE 1=1";
|
||||
|
||||
if ( $args['permission_id'] ) {
|
||||
$query[] = $wpdb->prepare( 'AND permission_id = %d', $args['permission_id'] );
|
||||
|
@ -197,7 +199,13 @@ class WC_Customer_Download_Log_Data_Store implements WC_Customer_Download_Log_Da
|
|||
$query[] = $wpdb->prepare( 'LIMIT %d, %d', absint( $args['limit'] ) * absint( $args['page'] - 1 ), absint( $args['limit'] ) );
|
||||
}
|
||||
|
||||
$raw_download_logs = $wpdb->get_results( implode( ' ', $query ) ); // WPCS: unprepared SQL ok.
|
||||
// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared
|
||||
if ( $is_count ) {
|
||||
return absint( $wpdb->get_var( implode( ' ', $query ) ) );
|
||||
}
|
||||
|
||||
$raw_download_logs = $wpdb->get_results( implode( ' ', $query ) );
|
||||
// phpcs:enable WordPress.DB.PreparedSQL.NotPrepared
|
||||
|
||||
switch ( $args['return'] ) {
|
||||
case 'ids':
|
||||
|
@ -226,6 +234,26 @@ class WC_Customer_Download_Log_Data_Store implements WC_Customer_Download_Log_Da
|
|||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the count of download logs for a given download permission.
|
||||
*
|
||||
* @param int $permission_id Permission to get logs count for.
|
||||
* @return int
|
||||
*/
|
||||
public function get_download_logs_count_for_permission( $permission_id ) {
|
||||
// If no permission_id is passed, return an empty array.
|
||||
if ( empty( $permission_id ) ) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return $this->get_download_logs(
|
||||
array(
|
||||
'permission_id' => $permission_id,
|
||||
'return' => 'count',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to delete download logs for a given permission ID.
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue