1 <?php
  2 /**
  3  * Order Downloads
  4  *
  5  * @author      WooThemes
  6  * @category    Admin
  7  * @package     WooCommerce/Admin/Meta Boxes
  8  * @version     2.1.0
  9  */
 10 
 11 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
 12 
 13 /**
 14  * WC_Meta_Box_Order_Downloads
 15  */
 16 class WC_Meta_Box_Order_Downloads {
 17 
 18     /**
 19      * Output the metabox
 20      */
 21     public static function output( $post ) {
 22         global $woocommerce, $post, $wpdb;
 23         ?>
 24         <div class="order_download_permissions wc-metaboxes-wrapper">
 25 
 26             <div class="wc-metaboxes">
 27                 <?php
 28                     $download_permissions = $wpdb->get_results( $wpdb->prepare( "
 29                         SELECT * FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions
 30                         WHERE order_id = %d ORDER BY product_id
 31                     ", $post->ID ) );
 32 
 33                     $product = null;
 34                     $loop    = 0;
 35                     if ( $download_permissions && sizeof( $download_permissions ) > 0 ) foreach ( $download_permissions as $download ) {
 36 
 37                         if ( ! $product || $product->id != $download->product_id ) {
 38                             $product    = get_product( absint( $download->product_id ) );
 39                             $file_counter = 1;
 40                         }
 41 
 42                         // don't show permissions to files that have since been removed
 43                         if ( ! $product || ! $product->exists() || ! $product->has_file( $download->download_id ) )
 44                             continue;
 45 
 46                         // Show file title instead of count if set
 47                         $file = $product->get_file( $download->download_id );
 48                         if ( isset( $file['name'] ) ) {
 49                             $file_count = $file['name'];
 50                         } else {
 51                             $file_count = sprintf( __( 'File %d', 'woocommerce' ), $file_counter );
 52                         }
 53 
 54                         include( 'views/html-order-download-permission.php' );
 55 
 56                         $loop++;
 57                         $file_counter++;
 58                     }
 59                 ?>
 60             </div>
 61 
 62             <div class="toolbar">
 63                 <p class="buttons">
 64                     <select name="grant_access_id" id="grant_access_id" class="ajax_chosen_select_downloadable_products_and_variations" multiple="multiple" data-placeholder="<?php _e( 'Search for a downloadable product&hellip;', 'woocommerce' ) ?>" style="width: 400px"></select>
 65                     <button type="button" class="button grant_access"><?php _e( 'Grant Access', 'woocommerce' ); ?></button>
 66                 </p>
 67                 <div class="clear"></div>
 68             </div>
 69 
 70         </div>
 71         <?php
 72     }
 73 
 74     /**
 75      * Save meta box data
 76      */
 77     public static function save( $post_id, $post ) {
 78         global $wpdb, $woocommerce;
 79 
 80         if ( isset( $_POST['download_id'] ) ) {
 81 
 82             // Download data
 83             $download_ids           = $_POST['download_id'];
 84             $product_ids            = $_POST['product_id'];
 85             $downloads_remaining    = $_POST['downloads_remaining'];
 86             $access_expires         = $_POST['access_expires'];
 87 
 88             // Order data
 89             $order_key              = get_post_meta( $post->ID, '_order_key', true );
 90             $customer_email         = get_post_meta( $post->ID, '_billing_email', true );
 91             $customer_user          = get_post_meta( $post->ID, '_customer_user', true );
 92             $product_ids_count      = sizeof( $product_ids );
 93 
 94             for ( $i = 0; $i < $product_ids_count; $i ++ ) {
 95                 if ( ! isset( $product_ids[ $i ] ) )
 96                     continue;
 97 
 98                 $data = array(
 99                     'user_id'               => absint( $customer_user ),
100                     'user_email'            => wc_clean( $customer_email ),
101                     'downloads_remaining'   => wc_clean( $downloads_remaining[ $i ] )
102                 );
103 
104                 $format = array( '%d', '%s', '%s' );
105 
106                 $expiry  = ( array_key_exists( $i, $access_expires ) && $access_expires[ $i ] != '' ) ? date_i18n( 'Y-m-d', strtotime( $access_expires[ $i ] ) ) : null;
107 
108                 if ( ! is_null( $expiry ) ) {
109                     $data['access_expires'] = $expiry;
110                     $format[]               = '%s';
111                 }
112 
113                 $wpdb->update( $wpdb->prefix . "woocommerce_downloadable_product_permissions",
114                     $data,
115                     array(
116                         'order_id'      => $post_id,
117                         'product_id'    => absint( $product_ids[ $i ] ),
118                         'download_id'   => wc_clean( $download_ids[ $i ] )
119                         ),
120                     $format, array( '%d', '%d', '%s' )
121                 );
122 
123             }
124         }
125     }
126 }
WooCommerce API documentation generated by ApiGen 2.8.0