/**
  * 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;
 }
 /**
  * Invoked when an order status changes to 'completed', or 'processing'
  * depending on how WooCommerce is configured.  If the order has any
  * voucher items, the voucher PDF files are generated
  *
  * @since 1.2
  * @param int $order_id newly created order identifier
  * @param boolean $force use true to force the voucher to be re-generated regardless of whether it already exists
  */
 public function generate_voucher_pdf($order_id, $force = false)
 {
     global $wpdb;
     $order = wc_get_order($order_id);
     foreach (WC_PDF_Product_Vouchers_Order::get_vouchers($order) as $voucher) {
         // voucher has already been generated
         if ($voucher->file_exists($this->plugin->get_uploads_path()) && !$force) {
             continue;
         }
         // NOTE: I could set the expiration (and voucher number) on the order item meta if I want it displayed in the frontend
         // set the expiration date if needed
         if (!$voucher->get_formatted_expiration_date() && $voucher->get_expiry()) {
             $expiry_from_date = apply_filters('wc_pdf_product_vouchers_expiry_from_date', time(), $order_id, $voucher);
             $voucher->set_expiration_date($expiry_from_date + $voucher->get_expiry() * 24 * 60 * 60);
             wc_update_order_item_meta($voucher->get_item_id(), '_voucher_expiration', date('Y-m-d', time() + $voucher->get_expiry() * 24 * 60 * 60));
         }
         // ensure the path that will hold the voucher pdf exists
         $voucher_pdf_path = $this->plugin->get_uploads_path() . '/' . $voucher->get_voucher_path();
         if (!file_exists($voucher_pdf_path)) {
             @mkdir($voucher_pdf_path, 0777, true);
         }
         // is the output path writable?
         if (!is_writable($voucher_pdf_path)) {
             $order->add_order_note(sprintf(__("PDF Product Vouchers file permission error: unable to generate voucher %s in %s  Please fix the directory permissions and re-generate the voucher.", WC_PDF_Product_Vouchers::TEXT_DOMAIN), $voucher->get_voucher_number(), $voucher_pdf_path));
             continue;
         }
         // try to generate the voucher
         try {
             $voucher->generate_pdf($this->plugin->get_uploads_path());
         } catch (Exception $exception) {
             $order->add_order_note(sprintf(__("PDF Product Vouchers: unable to generate voucher %s due to: %s", WC_PDF_Product_Vouchers::TEXT_DOMAIN), $voucher->get_voucher_number(), $exception->getMessage()));
         }
     }
 }