WC_Customer_Download_Log_Data_Store::get_download_logs()publicWC 1.0

Get array of download logs, or the count of existing logs, by specified args.

Method of the class: WC_Customer_Download_Log_Data_Store{}

No Hooks.

Return

Array|Int.

Usage

$WC_Customer_Download_Log_Data_Store = new WC_Customer_Download_Log_Data_Store();
$WC_Customer_Download_Log_Data_Store->get_download_logs( $args );
$args(array)
Arguments to define download logs to retrieve. If $args['return'] is 'count' then the count of existing logs will be returned.
Default: array()

WC_Customer_Download_Log_Data_Store::get_download_logs() code WC 9.4.2

public function get_download_logs( $args = array() ) {
	global $wpdb;

	$args = wp_parse_args(
		$args,
		array(
			'permission_id'   => '',
			'user_id'         => '',
			'user_ip_address' => '',
			'orderby'         => 'download_log_id',
			'order'           => 'ASC',
			'limit'           => -1,
			'page'            => 1,
			'return'          => 'objects',
		)
	);

	$is_count = 'count' === $args['return'];

	$query   = array();
	$table   = $wpdb->prefix . self::get_table_name();
	$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'] );
	}

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

	if ( $args['user_ip_address'] ) {
		$query[] = $wpdb->prepare( 'AND user_ip_address = %s', $args['user_ip_address'] );
	}

	$allowed_orders = array( 'download_log_id', 'timestamp', 'permission_id', 'user_id' );
	$orderby        = in_array( $args['orderby'], $allowed_orders, true ) ? $args['orderby'] : 'download_log_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: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':
			return wp_list_pluck( $raw_download_logs, 'download_log_id' );
		default:
			return array_map( array( $this, 'get_download_log' ), $raw_download_logs );
	}
}