WC_Customer_Download_Data_Store::get_downloads
Get array of download ids by specified args.
Method of the class: WC_Customer_Download_Data_Store{}
No Hooks.
Returns
Array. Can be an array of permission_ids, an array of WC_Customer_Download objects or an array of arrays containing specified fields depending on the value of $args['return'].
Usage
$WC_Customer_Download_Data_Store = new WC_Customer_Download_Data_Store(); $WC_Customer_Download_Data_Store->get_downloads( $args );
- $args(array)
- Arguments to filter downloads. $args['return'] accepts the following values: 'objects' (default), 'ids' or a comma separated list of fields (for example: 'order_id,user_id,user_email').
Default: array()
WC_Customer_Download_Data_Store::get_downloads() WC Customer Download Data Store::get downloads code WC 10.3.6
public function get_downloads( $args = array() ) {
global $wpdb;
$args = wp_parse_args(
$args,
array(
'user_email' => '',
'user_id' => '',
'order_id' => '',
'order_key' => '',
'product_id' => '',
'download_id' => '',
'orderby' => 'permission_id',
'order' => 'ASC',
'limit' => -1,
'page' => 1,
'return' => 'objects',
)
);
$valid_fields = array( 'permission_id', 'download_id', 'product_id', 'order_id', 'order_key', 'user_email', 'user_id', 'downloads_remaining', 'access_granted', 'access_expires', 'download_count' );
$get_results_output = ARRAY_A;
if ( 'ids' === $args['return'] ) {
$fields = 'permission_id';
} elseif ( 'objects' === $args['return'] ) {
$fields = '*';
$get_results_output = OBJECT;
} else {
$fields = explode( ',', (string) $args['return'] );
$fields = implode( ', ', array_intersect( $fields, $valid_fields ) );
}
$query = array();
$query[] = "SELECT {$fields} FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions WHERE 1=1";
if ( $args['user_email'] ) {
$query[] = $wpdb->prepare( 'AND user_email = %s', sanitize_email( $args['user_email'] ) );
}
if ( $args['user_id'] ) {
$query[] = $wpdb->prepare( 'AND user_id = %d', absint( $args['user_id'] ) );
}
if ( $args['order_id'] ) {
$query[] = $wpdb->prepare( 'AND order_id = %d', $args['order_id'] );
}
if ( $args['order_key'] ) {
$query[] = $wpdb->prepare( 'AND order_key = %s', $args['order_key'] );
}
if ( $args['product_id'] ) {
$query[] = $wpdb->prepare( 'AND product_id = %d', $args['product_id'] );
}
if ( $args['download_id'] ) {
$query[] = $wpdb->prepare( 'AND download_id = %s', $args['download_id'] );
}
$orderby = in_array( $args['orderby'], $valid_fields, true ) ? $args['orderby'] : 'permission_id';
$order = 'DESC' === strtoupper( $args['order'] ) ? 'DESC' : 'ASC';
$orderby_sql = sanitize_sql_orderby( "{$orderby} {$order}" );
$query[] = "ORDER BY {$orderby_sql}";
if ( 0 < $args['limit'] ) {
$query[] = $wpdb->prepare( 'LIMIT %d, %d', absint( $args['limit'] ) * absint( $args['page'] - 1 ), absint( $args['limit'] ) );
}
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
$results = $wpdb->get_results( implode( ' ', $query ), $get_results_output );
switch ( $args['return'] ) {
case 'ids':
return wp_list_pluck( $results, 'permission_id' );
case 'objects':
return array_map( array( $this, 'get_download' ), $results );
default:
return $results;
}
}