/**
 * Checks if a file is at its download limit
 *
 * This limit refers to the maximum number of times files connected to a product
 * can be downloaded.
 *
 * @since 1.3.1
 * @uses EDD_Logging::get_log_count()
 * @param int $download_id Download ID
 * @param int $payment_id Payment ID
 * @param int $file_id File ID
 * @param int $price_id Price ID
 * @return bool True if at limit, false otherwise
 */
function edd_is_file_at_download_limit($download_id = 0, $payment_id = 0, $file_id = 0, $price_id = false)
{
    // Checks to see if at limit
    $logs = new EDD_Logging();
    $meta_query = array('relation' => 'AND', array('key' => '_edd_log_file_id', 'value' => (int) $file_id), array('key' => '_edd_log_payment_id', 'value' => (int) $payment_id), array('key' => '_edd_log_price_id', 'value' => (int) $price_id));
    $ret = false;
    $download_count = $logs->get_log_count($download_id, 'file_download', $meta_query);
    $download_limit = edd_get_file_download_limit($download_id);
    $unlimited_purchase = edd_payment_has_unlimited_downloads($payment_id);
    if (!empty($download_limit) && empty($unlimited_purchase)) {
        if ($download_count >= $download_limit) {
            $ret = true;
            // Check to make sure the limit isn't overwritten
            // A limit is overwritten when purchase receipt is resent
            $limit_override = edd_get_file_download_limit_override($download_id, $payment_id);
            if (!empty($limit_override) && $download_count < $limit_override) {
                $ret = false;
            }
        }
    }
    return (bool) apply_filters('edd_is_file_at_download_limit', $ret, $download_id, $payment_id, $file_id);
}
}
// Setup the variables
$payment_id = absint($_GET['id']);
$number = edd_get_payment_number($payment_id);
$item = get_post($payment_id);
// Sanity check... fail if purchase ID is invalid
if (!is_object($item) || $item->post_type != 'edd_payment') {
    wp_die(__('The specified ID does not belong to a payment. Please try again', 'edd'), __('Error', 'edd'));
}
$payment_meta = edd_get_payment_meta($payment_id);
$transaction_id = esc_attr(edd_get_payment_transaction_id($payment_id));
$cart_items = edd_get_payment_meta_cart_details($payment_id);
$user_id = edd_get_payment_user_id($payment_id);
$customer_id = edd_get_payment_customer_id($payment_id);
$payment_date = strtotime($item->post_date);
$unlimited = edd_payment_has_unlimited_downloads($payment_id);
$user_info = edd_get_payment_meta_user_info($payment_id);
$address = !empty($user_info['address']) ? $user_info['address'] : array('line1' => '', 'line2' => '', 'city' => '', 'country' => '', 'state' => '', 'zip' => '');
$gateway = edd_get_payment_gateway($payment_id);
$currency_code = edd_get_payment_currency_code($payment_id);
?>
<div class="wrap edd-wrap">
	<h2><?php 
printf(__('Payment %s', 'edd'), $number);
?>
</h2>
	<?php 
do_action('edd_view_order_details_before', $payment_id);
?>
	<form id="edd-edit-order-form" method="post">
		<?php