/** * 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); } }
/** * Download a file - hook into init function. * * @access public * @return void */ function woocommerce_download_product() { if (isset($_GET['download_file']) && isset($_GET['order']) && isset($_GET['email'])) { global $wpdb, $is_IE; $product_id = (int) urldecode($_GET['download_file']); $order_key = urldecode($_GET['order']); $email = sanitize_email(str_replace(' ', '+', urldecode($_GET['email']))); $download_id = isset($_GET['key']) ? urldecode($_GET['key']) : ''; // backwards compatibility for existing download URLs $_product = get_product($product_id); $file_download_method = apply_filters('woocommerce_file_download_method', get_option('woocommerce_file_download_method'), $product_id); if (!is_email($email)) { wp_die(__('Invalid email address.', 'woocommerce') . ' <a href="' . home_url() . '">' . __('Go to homepage →', 'woocommerce') . '</a>'); } $query = "\n\t\t\tSELECT order_id,downloads_remaining,user_id,download_count,access_expires,download_id\n\t\t\tFROM " . $wpdb->prefix . "woocommerce_downloadable_product_permissions\n\t\t\tWHERE user_email = %s\n\t\t\tAND order_key = %s\n\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="' . home_url() . '">' . __('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="' . wp_login_url(get_permalink(woocommerce_get_page_id('myaccount'))) . '">' . __('Login →', 'woocommerce') . '</a>'); } elseif ($user_id != get_current_user_id()) { wp_die(__('This is not your download link.', 'woocommerce')); } } if (!get_post($product_id)) { wp_die(__('Product no longer exists.', 'woocommerce') . ' <a href="' . home_url() . '">' . __('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="' . home_url() . '">' . __('Go to homepage →', 'woocommerce') . '</a>'); } } if ($downloads_remaining == '0') { wp_die(__('Sorry, you have reached your download limit for this file', 'woocommerce') . ' <a href="' . home_url() . '">' . __('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="' . home_url() . '">' . __('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); if (!$file_path) { wp_die(__('No file defined', 'woocommerce') . ' <a href="' . home_url() . '">' . __('Go to homepage →', 'woocommerce') . '</a>'); } // Redirect to the file... if ($file_download_method == "redirect") { header('Location: ' . $file_path); exit; } // ...or serve it if (!is_multisite()) { /* * Download file may be either http or https. * site_url() depends on whether the page containing the download (ie; My Account) is served via SSL because WC * modifies site_url() via a filter to force_ssl. * So blindly doing a str_replace is incorrect because it will fail when schemes are mismatched. This code * handles the various permutations. */ $scheme = parse_url($file_path, PHP_URL_SCHEME); if ($scheme) { $site_url = set_url_scheme(site_url(''), $scheme); } else { $site_url = is_ssl() ? str_replace('https:', 'http:', site_url()) : site_url(); } $file_path = str_replace(trailingslashit($site_url), ABSPATH, $file_path); } else { $network_url = is_ssl() ? str_replace('https:', 'http:', network_admin_url()) : network_admin_url(); $upload_dir = wp_upload_dir(); // Try to replace network url $file_path = str_replace(trailingslashit($network_url), ABSPATH, $file_path); // Now try to replace upload URL $file_path = str_replace($upload_dir['baseurl'], $upload_dir['basedir'], $file_path); } // See if its local or remote if (strstr($file_path, 'http:') || strstr($file_path, 'https:') || strstr($file_path, 'ftp:')) { $remote_file = true; } else { $remote_file = false; // Remove Query String if (strstr($file_path, '?')) { $file_path = current(explode('?', $file_path)); } $file_path = realpath($file_path); } $file_extension = strtolower(substr(strrchr($file_path, "."), 1)); $ctype = "application/force-download"; foreach (get_allowed_mime_types() as $mime => $type) { $mimes = explode('|', $mime); if (in_array($file_extension, $mimes)) { $ctype = $type; break; } } // Start setting headers if (!ini_get('safe_mode')) { @set_time_limit(0); } if (function_exists('get_magic_quotes_runtime') && get_magic_quotes_runtime()) { @set_magic_quotes_runtime(0); } if (function_exists('apache_setenv')) { @apache_setenv('no-gzip', 1); } @session_write_close(); @ini_set('zlib.output_compression', 'Off'); @ob_end_clean(); if (ob_get_level()) { @ob_end_clean(); } // Zip corruption fix if ($is_IE && is_ssl()) { // IE bug prevents download via SSL when Cache Control and Pragma no-cache headers set. header('Expires: Wed, 11 Jan 1984 05:00:00 GMT'); header('Cache-Control: private'); } else { nocache_headers(); } $file_name = basename($file_path); if (strstr($file_name, '?')) { $file_name = current(explode('?', $file_name)); } header("Robots: none"); header("Content-Type: " . $ctype); header("Content-Description: File Transfer"); header("Content-Disposition: attachment; filename=\"" . $file_name . "\";"); header("Content-Transfer-Encoding: binary"); if ($size = @filesize($file_path)) { header("Content-Length: " . $size); } if ($file_download_method == 'xsendfile') { // Path fix - kudos to Jason Judge if (getcwd()) { $file_path = trim(preg_replace('`^' . getcwd() . '`', '', $file_path), '/'); } header("Content-Disposition: attachment; filename=\"" . $file_name . "\";"); if (function_exists('apache_get_modules') && in_array('mod_xsendfile', apache_get_modules())) { header("X-Sendfile: {$file_path}"); exit; } elseif (stristr(getenv('SERVER_SOFTWARE'), 'lighttpd')) { header("X-Lighttpd-Sendfile: {$file_path}"); exit; } elseif (stristr(getenv('SERVER_SOFTWARE'), 'nginx') || stristr(getenv('SERVER_SOFTWARE'), 'cherokee')) { header("X-Accel-Redirect: /{$file_path}"); exit; } } if ($remote_file) { @woocommerce_readfile_chunked($file_path) or header('Location: ' . $file_path); } else { @woocommerce_readfile_chunked($file_path) or wp_die(__('File not found', 'woocommerce') . ' <a href="' . home_url() . '">' . __('Go to homepage →', 'woocommerce') . '</a>'); } exit; } }
/** * Gets a user's downloadable products if they are logged in. * * @access public * @return array Array of downloadable products */ public function get_downloadable_products() { global $wpdb; $downloads = array(); $_product = null; $order = null; $file_number = 0; if (is_user_logged_in()) { // Get results from valid orders only $results = $wpdb->get_results($wpdb->prepare("\n\t\t\t\tSELECT permissions.* \n\t\t\t\tFROM {$wpdb->prefix}woocommerce_downloadable_product_permissions as permissions\n\t\t\t\tLEFT JOIN {$wpdb->posts} as posts ON permissions.order_id = posts.ID\n\t\t\t\tWHERE user_id = %s \n\t\t\t\tAND permissions.order_id > 0\n\t\t\t\tAND posts.post_status = 'publish'\n\t\t\t\tAND \n\t\t\t\t\t(\n\t\t\t\t\t\tpermissions.downloads_remaining > 0\n\t\t\t\t\t\tOR \n\t\t\t\t\t\tpermissions.downloads_remaining = ''\n\t\t\t\t\t)\n\t\t\t\tAND \n\t\t\t\t\t(\n\t\t\t\t\t\tpermissions.access_expires IS NULL\n\t\t\t\t\t\tOR \n\t\t\t\t\t\tpermissions.access_expires >= %s\n\t\t\t\t\t)\n\t\t\t\tGROUP BY permissions.download_id\n\t\t\t\tORDER BY permissions.order_id, permissions.product_id, permissions.download_id;\n\t\t\t\t", get_current_user_id(), date('Y-m-d', current_time('timestamp')))); if ($results) { foreach ($results as $result) { if (!$order || $order->id != $result->order_id) { // new order $order = new WC_Order($result->order_id); $_product = null; } // Downloads permitted? if (!$order->is_download_permitted()) { continue; } if (!$_product || $_product->id != $result->product_id) { // new product $file_number = 0; $_product = get_product($result->product_id); } // Check product exists and has the file if (!$_product || !$_product->exists() || !$_product->has_file($result->download_id)) { continue; } $download_file = $_product->get_file($result->download_id); // Download name will be 'Product Name' for products with a single downloadable file, and 'Product Name - File X' for products with multiple files $download_name = apply_filters('woocommerce_downloadable_product_name', $_product->get_title() . ' – ' . $download_file['name'], $_product, $result->download_id, $file_number); $downloads[] = array('download_url' => add_query_arg(array('download_file' => $result->product_id, 'order' => $result->order_key, 'email' => $result->user_email, 'key' => $result->download_id), home_url('/', 'http')), 'download_id' => $result->download_id, 'product_id' => $result->product_id, 'download_name' => $download_name, 'order_id' => $order->id, 'order_key' => $order->order_key, 'downloads_remaining' => $result->downloads_remaining); $file_number++; } } } return apply_filters('woocommerce_customer_get_downloadable_products', $downloads); }
private function output_wc_start() { global $product, $woocommerce; $this->disable_export_btns = $product->is_downloadable() ? true : false; //added to cart, recall added product if (isset($_POST['fpd_product'])) { $views = $_POST['fpd_product']; $this->form_views = stripslashes($views); } else { if (isset($_GET['cart_item_key'])) { //load from cart item $cart = $woocommerce->cart->get_cart(); $cart_item = $cart[$_GET['cart_item_key']]; if ($cart_item) { if (isset($cart_item['fpd_data'])) { $views = $cart_item['fpd_data']['fpd_product']; $this->form_views = stripslashes($views); } } else { //cart item could not be found echo '<p><strong>'; _e('Sorry, but the cart item could not be found!', 'radykal'); echo '</strong></p>'; return; } } else { if (isset($_GET['order']) && isset($_GET['item_id'])) { //load ordered product in designer $order = new WC_Order($_GET['order']); $item_meta = $order->get_item_meta($_GET['item_id'], 'fpd_data'); $this->form_views = $item_meta[0]["fpd_product"]; if ($product->is_downloadable() && $order->is_download_permitted()) { $this->disable_export_btns = false; ?> <br /> <a href="#" id="fpd-extern-download-pdf"><?php echo fpd_get_option('fpd_label_downLoadPDF'); ?> </a> <?php } } else { if (isset($_GET['share_id'])) { $transient_key = 'fpd_share_' . $_GET['share_id']; $transient_val = get_transient($transient_key); if ($transient_val !== false) { $this->form_views = stripslashes($transient_val['product']); } } } } } }
/** * Gets a user's downloadable products if they are logged in. * * @access public * @return array Array of downloadable products */ public function get_downloadable_products() { global $wpdb, $woocommerce; $downloads = array(); if (is_user_logged_in()) { $user_info = get_userdata(get_current_user_id()); $results = $wpdb->get_results($wpdb->prepare("SELECT * FROM " . $wpdb->prefix . "woocommerce_downloadable_product_permissions WHERE user_id = '%s' ORDER BY order_id, product_id, download_id", get_current_user_id())); $_product = null; $order = null; $file_number = 0; if ($results) { foreach ($results as $result) { if ($result->order_id > 0) { if (!$order || $order->id != $result->order_id) { // new order $order = new WC_Order($result->order_id); $_product = null; } // order exists and downloads permitted? if (!$order->id || !$order->is_download_permitted() || $order->post_status != 'publish') { continue; } if (!$_product || $_product->id != $result->product_id) { // new product $file_number = 0; $_product = get_product($result->product_id); } if (!$_product || !$_product->exists()) { continue; } if (!$_product->has_file($result->download_id)) { continue; } // Download name will be 'Product Name' for products with a single downloadable file, and 'Product Name - File X' for products with multiple files $download_name = apply_filters('woocommerce_downloadable_product_name', $_product->get_title() . ($file_number > 0 ? ' — ' . sprintf(__('File %d', 'woocommerce'), $file_number + 1) : ''), $_product, $result->download_id, $file_number); // Rename previous download with file number if there are multiple files only if ($file_number == 1) { $previous_result =& $downloads[count($downloads) - 1]; $previous_product = get_product($previous_result['product_id']); $previous_result['download_name'] = apply_filters('woocommerce_downloadable_product_name', $previous_result['download_name'] . ' — ' . sprintf(__('File %d', 'woocommerce'), $file_number), $previous_product, $previous_result['download_id'], 0); } $downloads[] = array('download_url' => add_query_arg(array('download_file' => $result->product_id, 'order' => $result->order_key, 'email' => $result->user_email, 'key' => $result->download_id), trailingslashit(home_url('', 'http'))), 'download_id' => $result->download_id, 'product_id' => $result->product_id, 'download_name' => $download_name, 'order_id' => $order->id, 'order_key' => $order->order_key, 'downloads_remaining' => $result->downloads_remaining); $file_number++; } } } } return apply_filters('woocommerce_customer_get_downloadable_products', $downloads); }
/** * Test: is_download_permitted */ function test_is_download_permitted() { $object = new WC_Order(); $object->set_status('pending'); $this->assertFalse($object->is_download_permitted()); $object->set_status('completed'); $this->assertTrue($object->is_download_permitted()); }
/** * Get HTML for the order items to be shown in emails. * @param WC_Order $order * @param array $args * @since 2.7.0 */ function wc_get_email_order_items($order, $args = array()) { ob_start(); $defaults = array('show_sku' => false, 'show_image' => false, 'image_size' => array(32, 32), 'plain_text' => false, 'sent_to_admin' => false); $args = wp_parse_args($args, $defaults); $template = $args['plain_text'] ? 'emails/plain/email-order-items.php' : 'emails/email-order-items.php'; wc_get_template($template, array('order' => $order, 'items' => $order->get_items(), 'show_download_links' => $order->is_download_permitted(), 'show_sku' => $args['show_sku'], 'show_purchase_note' => $order->is_paid(), 'show_image' => $args['show_image'], 'image_size' => $args['image_size'], 'plain_text' => $args['plain_text'], 'sent_to_admin' => $args['sent_to_admin'])); return apply_filters('woocommerce_email_order_items_table', ob_get_clean(), $order); }