/**
  * Admin voucher download, which will work regardless of the download
  * permissions/current user, and will not count towards the download
  * count
  *
  * @since 2.1
  */
 public function download_voucher()
 {
     if (isset($_GET['post']) && isset($_GET['product_id']) && isset($_GET['item_id']) && isset($_GET['action']) && 'download' == $_GET['action'] && $_GET['voucher_id']) {
         $order = wc_get_order($_GET['post']);
         $items = $order->get_items();
         $voucher = new WC_Voucher($_GET['voucher_id'], $_GET['post'], $items[$_GET['item_id']], $_GET['item_id']);
         $download_handler = new WC_Download_Handler();
         $file_path = $voucher->get_voucher_full_filename(WC_PDF_Product_Vouchers::get_uploads_path());
         if ('redirect' === get_option('woocommerce_file_download_method')) {
             $file_path = $voucher->convert_path_to_url($file_path);
         }
         $download_handler->download($file_path, $_GET['product_id']);
         exit;
     }
 }
 /**
  * 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;
 }
 /**
  * Returns the One True Instance of PDF Product Vouchers
  *
  * @since 2.2.0
  * @return WC_PDF_Product_Vouchers
  */
 function wc_pdf_product_vouchers()
 {
     return WC_PDF_Product_Vouchers::instance();
 }
    /**
     * Order vouchers meta box
     *
     * Displays the order vouchers meta box - for showing and modifying
     * individual vouchers attached to the order
     *
     * @since 1.2
     */
    public function vouchers_meta_box($post)
    {
        $order = wc_get_order($post->ID);
        $order_items = $order->get_items();
        ?>
		<div class="woocommerce_order_vouchers_wrapper">
			<table cellpadding="0" cellspacing="0" class="woocommerce_order_vouchers">
				<thead>
					<tr>
						<th class="thumb" width="1%"><?php 
        _e('Voucher', WC_PDF_Product_Vouchers::TEXT_DOMAIN);
        ?>
</th>
						<th class="voucher_number"><?php 
        _e('Number', WC_PDF_Product_Vouchers::TEXT_DOMAIN);
        ?>
</th>
						<th class="sku"><?php 
        _e('SKU', WC_PDF_Product_Vouchers::TEXT_DOMAIN);
        ?>
</th>
						<th class="data"><?php 
        _e('Data', WC_PDF_Product_Vouchers::TEXT_DOMAIN);
        ?>
</th>
						<th class="expires"><?php 
        _e('Expires', WC_PDF_Product_Vouchers::TEXT_DOMAIN);
        ?>
</th>
						<th class="qty"><?php 
        _e('Qty', WC_PDF_Product_Vouchers::TEXT_DOMAIN);
        ?>
</th>
						<th class="redeem" style="white-space:nowrap;"><?php 
        _e('Mark Redeemed', WC_PDF_Product_Vouchers::TEXT_DOMAIN);
        ?>
&nbsp;<a class="tips" data-tip="<?php 
        _e('Mark the dates that any vouchers are redeemed.  Marking all vouchers redeemed will complete the order.', WC_PDF_Product_Vouchers::TEXT_DOMAIN);
        ?>
" href="#">[?]</a></th>
					</tr>
				</thead>
				<tbody id="order_vouchers_list">

					<?php 
        if (count($order_items) > 0) {
            foreach ($order_items as $item_id => $item) {
                // only voucher items
                if (!isset($item['voucher_id'])) {
                    continue;
                }
                $item['voucher_redeem'] = maybe_unserialize($item['voucher_redeem']);
                $voucher = new WC_Voucher($item['voucher_id'], $post->ID, $item, $item_id);
                if (isset($item['variation_id']) && $item['variation_id'] > 0) {
                    $_product = wc_get_product($item['variation_id']);
                } else {
                    $_product = wc_get_product($item['product_id']);
                }
                // get any user-supplied voucher data (this includes product variation data and user-entered fields like recipient or message)
                $voucher_data = array();
                if (isset($_product->variation_data)) {
                    $voucher_data = $_product->variation_data;
                }
                $voucher_data = array_merge($voucher_data, $voucher->get_user_input_data());
                ?>
						<tr class="item" rel="<?php 
                echo $item_id;
                ?>
">
							<td class="thumb">
								<a href="<?php 
                echo esc_url(admin_url('post.php?post=' . $voucher->id . '&action=edit'));
                ?>
" class="tips" data-tip="<?php 
                echo '<strong>' . __('Voucher ID:', WC_PDF_Product_Vouchers::TEXT_DOMAIN) . '</strong> ' . $voucher->id;
                ?>
"><?php 
                echo $voucher->get_image();
                ?>
</a>
								<?php 
                if ($voucher->file_exists(WC_PDF_Product_Vouchers::get_uploads_path())) {
                    ?>
									<a href="<?php 
                    echo esc_url(add_query_arg(array('action' => 'download', 'product_id' => $item['product_id'], 'item_id' => $item_id, 'voucher_id' => $item['voucher_id'])));
                    ?>
"><?php 
                    esc_html_e('Download', WC_PDF_Product_Vouchers::TEXT_DOMAIN);
                    ?>
</a>
								<?php 
                }
                ?>
							</td>
							<td class="voucher_number" width="1%">
								<?php 
                echo $voucher->get_voucher_number();
                ?>
							</td>
							<td class="sku" width="1%">
								<?php 
                if ($_product->sku) {
                    echo $_product->sku;
                } else {
                    echo '-';
                }
                ?>
							</td>
							<td class="data">

								<?php 
                echo $item['name'];
                ?>
								<?php 
                if (!empty($voucher_data)) {
                    echo '<br/>' . wc_get_formatted_variation($voucher_data, true);
                }
                ?>
							</td>

							<td class="expires" style="width:auto;">
								<input type="text" name="voucher_expiration[<?php 
                echo $item_id;
                ?>
]" id="voucher_expiration_<?php 
                echo $item_id;
                ?>
" maxlength="10" value="<?php 
                echo $voucher->expiration_date ? date("Y-m-d", $voucher->expiration_date) : '';
                ?>
" class="date-picker-field" />
							</td>

							<td class="qty" width="1%">
								<?php 
                echo $item['qty'];
                ?>
							</td>

							<td class="redeem" width="1%">
								<?php 
                foreach ($item['voucher_redeem'] as $i => $redeem) {
                    ?>
									<input type="text" name="voucher_redeem[<?php 
                    echo $item_id;
                    ?>
][<?php 
                    echo $i;
                    ?>
]" id="voucher_redeem_<?php 
                    echo $item_id . '_' . $i;
                    ?>
" class="voucher_redeem date-picker-field" maxlength="10" style="width:85px;" value="<?php 
                    echo $redeem;
                    ?>
" class="date-picker-field" />
									<?php 
                }
                ?>
							</td>

						</tr>
					<?php 
            }
        }
        ?>
				</tbody>
			</table>
		</div>

		<p class="buttons buttons-alt">
			<button type="button" class="button redeem_all_vouchers"><?php 
        _e('Redeem All &uarr;', WC_PDF_Product_Vouchers::TEXT_DOMAIN);
        ?>
</button>
		</p>
		<div class="clear"></div>
		<?php 
    }