/**
* Check if we need to download a file and check validity
*/
public function download_product()
{
if (isset($_GET['download_file']) && isset($_GET['order']) && isset($_GET['email'])) {
global $wpdb;
$product_id = (int) $_GET['download_file'];
$order_key = $_GET['order'];
$email = sanitize_email(str_replace(' ', '+', $_GET['email']));
$download_id = isset($_GET['key']) ? preg_replace('/\\s+/', ' ', $_GET['key']) : '';
$_product = get_product($product_id);
if (!is_email($email)) {
wp_die(__('Invalid email address.', 'woocommerce') . ' <a href="' . esc_url(home_url()) . '" class="wc-forward">' . __('Go to homepage', 'woocommerce') . '</a>');
}
$query = "\n\t\t\t\tSELECT order_id,downloads_remaining,user_id,download_count,access_expires,download_id\n\t\t\t\tFROM " . $wpdb->prefix . "woocommerce_downloadable_product_permissions\n\t\t\t\tWHERE user_email = %s\n\t\t\t\tAND order_key = %s\n\t\t\t\tAND product_id = %s";
$args = array($email, $order_key, $product_id);
if ($download_id) {
// backwards compatibility for existing download URLs
$query .= " AND download_id = %s";
$args[] = $download_id;
}
$download_result = $wpdb->get_row($wpdb->prepare($query, $args));
if (!$download_result) {
wp_die(__('Invalid download.', 'woocommerce') . ' <a href="' . esc_url(home_url()) . '" class="wc-forward">' . __('Go to homepage', 'woocommerce') . '</a>');
}
$download_id = $download_result->download_id;
$order_id = $download_result->order_id;
$downloads_remaining = $download_result->downloads_remaining;
$download_count = $download_result->download_count;
$user_id = $download_result->user_id;
$access_expires = $download_result->access_expires;
if ($user_id && get_option('woocommerce_downloads_require_login') == 'yes') {
if (!is_user_logged_in()) {
wp_die(__('You must be logged in to download files.', 'woocommerce') . ' <a href="' . esc_url(wp_login_url(get_permalink(wc_get_page_id('myaccount')))) . '" class="wc-forward">' . __('Login', 'woocommerce') . '</a>', __('Log in to Download Files', 'woocommerce'));
} elseif (!current_user_can('download_file', $download_result)) {
wp_die(__('This is not your download link.', 'woocommerce'));
}
}
if (!get_post($product_id)) {
wp_die(__('Product no longer exists.', 'woocommerce') . ' <a href="' . esc_url(home_url()) . '" class="wc-forward">' . __('Go to homepage', 'woocommerce') . '</a>');
}
if ($order_id) {
$order = new WC_Order($order_id);
if (!$order->is_download_permitted() || $order->post_status != 'publish') {
wp_die(__('Invalid order.', 'woocommerce') . ' <a href="' . esc_url(home_url()) . '" class="wc-forward">' . __('Go to homepage', 'woocommerce') . '</a>');
}
}
if ($downloads_remaining == '0') {
wp_die(__('Sorry, you have reached your download limit for this file', 'woocommerce') . ' <a href="' . esc_url(home_url()) . '" class="wc-forward">' . __('Go to homepage', 'woocommerce') . '</a>');
}
if ($access_expires > 0 && strtotime($access_expires) < current_time('timestamp')) {
wp_die(__('Sorry, this download has expired', 'woocommerce') . ' <a href="' . esc_url(home_url()) . '" class="wc-forward">' . __('Go to homepage', 'woocommerce') . '</a>');
}
if ($downloads_remaining > 0) {
$wpdb->update($wpdb->prefix . "woocommerce_downloadable_product_permissions", array('downloads_remaining' => $downloads_remaining - 1), array('user_email' => $email, 'order_key' => $order_key, 'product_id' => $product_id, 'download_id' => $download_id), array('%d'), array('%s', '%s', '%d', '%s'));
}
// Count the download
$wpdb->update($wpdb->prefix . "woocommerce_downloadable_product_permissions", array('download_count' => $download_count + 1), array('user_email' => $email, 'order_key' => $order_key, 'product_id' => $product_id, 'download_id' => $download_id), array('%d'), array('%s', '%s', '%d', '%s'));
// Trigger action
do_action('woocommerce_download_product', $email, $order_key, $product_id, $user_id, $download_id, $order_id);
// Get the download URL and try to replace the url with a path
$file_path = $_product->get_file_download_path($download_id);
// Download it!
$this->download($file_path, $product_id);
}
}