/**
  * Called from the customer account page, this adds the date to the
  * voucher links so they are a little bit easier to distinguish from one
  * another.  Also filter out links for voucher files that don't exist for
  * whatever reason.
  *
  * @since 1.2
  * @param array $downloads available downloads
  *
  * @return array available downloads
  */
 public function customer_get_downloadable_products($downloads)
 {
     $new_downloads = array();
     foreach ($downloads as $download) {
         $product = wc_get_product($download['product_id']);
         // is the product a voucher product?
         if (false !== strpos($download['download_id'], 'wc_vouchers_') && WC_PDF_Product_Vouchers_Product::has_voucher($product)) {
             $order = wc_get_order($download['order_id']);
             // download id looks like "wc_vouchers_{voucher number}"
             $voucher = WC_PDF_Product_Vouchers_Order::get_voucher_by_voucher_number($order, str_replace('wc_vouchers_', '', $download['download_id']));
             if ($voucher && $voucher->file_exists(WC_PDF_Product_Vouchers::get_uploads_path())) {
                 $download['download_name'] .= ' (' . sprintf(_x('Voucher %s %s', 'Voucher number and date', WC_PDF_Product_Vouchers::TEXT_DOMAIN), $voucher->get_voucher_number(), date_i18n(get_option('date_format'), strtotime($order->order_date))) . ')';
                 $new_downloads[] = $download;
             }
         } else {
             // regular file
             $new_downloads[] = $download;
         }
     }
     return $new_downloads;
 }
 /**
  * Add the voucher number (if any) onto the downloadable product title
  * shown in the Downloadable Product Permissions box of the edit order
  * page
  *
  * @since 1.2
  * @param string $title the downloadable product title
  * @param int $product_id the product identifier
  * @param int $order_id the order identifier
  * @param string $order_key the order key
  * @param string $download_id the download id
  *
  * @return string the download permissions title
  */
 public function download_permissions_title($title, $product_id, $order_id, $order_key, $download_id)
 {
     $order = wc_get_order($order_id);
     $voucher = WC_PDF_Product_Vouchers_Order::get_voucher_by_voucher_number($order, str_replace('wc_vouchers_', '', $download_id));
     if ($voucher) {
         $title .= ' (' . sprintf(__('Voucher %s', WC_PDF_Product_Vouchers::TEXT_DOMAIN), $voucher->get_voucher_number()) . ')';
     }
     return $title;
 }
 /**
  * Return our voucher file path as the download file
  *
  * @since 1.2
  * @param string $download_file_path the download file path
  * @param int $product the product
  * @param int $download_id downloadable file identifier
  *
  * @return string the download file path
  */
 public function voucher_file_download_path($download_file_path, $product, $download_id)
 {
     global $wpdb, $post;
     // if there is no download file path set, it means this might be an item voucher being requested
     if (!$download_file_path) {
         $order_id = null;
         if ($post && 'shop_order' == $post->post_type && $post->ID) {
             $order_id = $post->ID;
         }
         // are we being called from the context of the has_file() checks?
         if (!$order_id && (!isset($_GET['download_file']) || !isset($_GET['order']) || !isset($_GET['email']) || !isset($_GET['key']))) {
             if (WC_PDF_Product_Vouchers_Product::has_voucher($product)) {
                 return $download_id;
             } else {
                 // otherwise, non-voucher product, return the passed-in value
                 return $download_file_path;
             }
         }
         if (!$order_id) {
             // get the order id from the GET parameters when needed
             $order_key = urldecode($_GET['order']);
             $email = str_replace(' ', '+', urldecode($_GET['email']));
             $order_id = $wpdb->get_var($wpdb->prepare("\n\t\t\t\t\tSELECT order_id\n\t\t\t\t\tFROM {$wpdb->prefix}woocommerce_downloadable_product_permissions\n\t\t\t\t\tWHERE user_email = %s\n\t\t\t\t\tAND order_key = %s\n\t\t\t\t\tAND product_id = %s\n\t\t\t\t\tAND download_id = %s", $email, $order_key, isset($product->variation_id) ? $product->variation_id : $product->id, $download_id));
         }
         // get and return the path to the requested voucher file
         if ($order_id) {
             $order = wc_get_order($order_id);
             $voucher = WC_PDF_Product_Vouchers_Order::get_voucher_by_voucher_number($order, str_replace('wc_vouchers_', '', $download_id));
             if ($voucher && $voucher->file_exists($this->plugin->get_uploads_path())) {
                 $download_file_path = $voucher->get_voucher_full_filename($this->plugin->get_uploads_path());
                 if ('redirect' === get_option('woocommerce_file_download_method')) {
                     $download_file_path = $voucher->convert_path_to_url($file_path);
                 }
             }
         }
     }
     return $download_file_path;
 }