2013-08-06 10:41:20 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2015-11-03 13:53:50 +00:00
|
|
|
* Order Downloads
|
2013-08-06 10:41:20 +00:00
|
|
|
*
|
2014-08-31 07:18:21 +00:00
|
|
|
* @author WooThemes
|
|
|
|
* @category Admin
|
|
|
|
* @package WooCommerce/Admin/Meta Boxes
|
2013-08-06 10:41:20 +00:00
|
|
|
* @version 2.1.0
|
|
|
|
*/
|
|
|
|
|
2014-09-20 20:05:06 +00:00
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit; // Exit if accessed directly
|
|
|
|
}
|
2013-08-06 10:41:20 +00:00
|
|
|
|
|
|
|
/**
|
2015-11-03 12:28:01 +00:00
|
|
|
* WC_Meta_Box_Order_Downloads Class.
|
2013-08-06 10:41:20 +00:00
|
|
|
*/
|
|
|
|
class WC_Meta_Box_Order_Downloads {
|
|
|
|
|
|
|
|
/**
|
2015-11-03 12:28:01 +00:00
|
|
|
* Output the metabox.
|
2016-01-04 21:31:36 +00:00
|
|
|
*
|
|
|
|
* @param WP_Post $post
|
2013-08-06 10:41:20 +00:00
|
|
|
*/
|
|
|
|
public static function output( $post ) {
|
2014-06-08 20:33:11 +00:00
|
|
|
global $post, $wpdb;
|
2013-08-06 10:41:20 +00:00
|
|
|
?>
|
|
|
|
<div class="order_download_permissions wc-metaboxes-wrapper">
|
|
|
|
|
2016-11-18 17:13:02 +00:00
|
|
|
<div class="wc-metaboxes"><?php
|
|
|
|
$data_store = WC_Data_Store::load( 'customer-download' );
|
|
|
|
$download_permissions = $data_store->get_downloads( array(
|
|
|
|
'order_id' => $post->ID,
|
|
|
|
'orderby' => 'product_id',
|
|
|
|
) );
|
|
|
|
|
|
|
|
$product = null;
|
|
|
|
$loop = 0;
|
|
|
|
$file_counter = 1;
|
|
|
|
|
|
|
|
if ( $download_permissions && sizeof( $download_permissions ) > 0 ) {
|
|
|
|
foreach ( $download_permissions as $download ) {
|
|
|
|
if ( ! $product || $product->get_id() !== $download->get_product_id() ) {
|
|
|
|
$product = wc_get_product( absint( $download->get_product_id() ) );
|
2013-12-10 10:50:48 +00:00
|
|
|
$file_counter = 1;
|
2013-08-06 10:41:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// don't show permissions to files that have since been removed
|
2016-11-18 17:13:02 +00:00
|
|
|
if ( ! $product || ! $product->exists() || ! $product->has_file( $download->get_download_id() ) ) {
|
2013-08-06 10:41:20 +00:00
|
|
|
continue;
|
2016-11-18 17:13:02 +00:00
|
|
|
}
|
2013-08-06 10:41:20 +00:00
|
|
|
|
2013-12-10 10:49:27 +00:00
|
|
|
// Show file title instead of count if set
|
2016-11-18 17:13:02 +00:00
|
|
|
$file = $product->get_file( $download->get_download_id() );
|
|
|
|
$file_count = isset( $file['name'] ) ? $file['name'] : sprintf( __( 'File %d', 'woocommerce' ), $file_counter );
|
2013-12-10 10:49:27 +00:00
|
|
|
|
2013-08-06 10:41:20 +00:00
|
|
|
include( 'views/html-order-download-permission.php' );
|
|
|
|
|
|
|
|
$loop++;
|
2013-12-10 10:50:48 +00:00
|
|
|
$file_counter++;
|
2013-08-06 10:41:20 +00:00
|
|
|
}
|
2016-11-18 17:13:02 +00:00
|
|
|
}
|
|
|
|
?></div>
|
2013-08-06 10:41:20 +00:00
|
|
|
|
|
|
|
<div class="toolbar">
|
|
|
|
<p class="buttons">
|
2015-08-05 18:08:15 +00:00
|
|
|
<input type="hidden" id="grant_access_id" name="grant_access_id" data-multiple="true" class="wc-product-search" style="width: 400px;" data-placeholder="<?php esc_attr_e( 'Search for a downloadable product…', 'woocommerce' ); ?>" data-action="woocommerce_json_search_downloadable_products_and_variations" />
|
2016-10-12 10:16:30 +00:00
|
|
|
<button type="button" class="button grant_access"><?php _e( 'Grant access', 'woocommerce' ); ?></button>
|
2013-08-06 10:41:20 +00:00
|
|
|
</p>
|
|
|
|
<div class="clear"></div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
</div>
|
|
|
|
<?php
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 12:28:01 +00:00
|
|
|
* Save meta box data.
|
2016-01-04 21:31:36 +00:00
|
|
|
*
|
|
|
|
* @param int $post_id
|
|
|
|
* @param WP_Post $post
|
2013-08-06 10:41:20 +00:00
|
|
|
*/
|
|
|
|
public static function save( $post_id, $post ) {
|
2014-06-08 20:33:11 +00:00
|
|
|
global $wpdb;
|
2013-08-06 10:41:20 +00:00
|
|
|
|
|
|
|
if ( isset( $_POST['download_id'] ) ) {
|
2016-11-18 17:13:02 +00:00
|
|
|
$download_ids = $_POST['download_id'];
|
|
|
|
$product_ids = $_POST['product_id'];
|
|
|
|
$downloads_remaining = $_POST['downloads_remaining'];
|
|
|
|
$access_expires = $_POST['access_expires'];
|
|
|
|
$product_ids_max = max( array_keys( $product_ids ) );
|
2013-08-06 10:41:20 +00:00
|
|
|
|
2015-02-03 10:57:39 +00:00
|
|
|
for ( $i = 0; $i <= $product_ids_max; $i ++ ) {
|
2014-08-31 07:18:21 +00:00
|
|
|
if ( ! isset( $product_ids[ $i ] ) ) {
|
2013-10-25 19:02:21 +00:00
|
|
|
continue;
|
2014-08-31 07:18:21 +00:00
|
|
|
}
|
2013-08-06 10:41:20 +00:00
|
|
|
|
2016-11-18 17:13:02 +00:00
|
|
|
// @todo $download = new WC_Customer_Download( $data );
|
|
|
|
|
2014-08-31 07:18:21 +00:00
|
|
|
$data = array(
|
2016-03-14 14:36:28 +00:00
|
|
|
'downloads_remaining' => wc_clean( $downloads_remaining[ $i ] ),
|
2014-08-31 07:18:21 +00:00
|
|
|
);
|
2013-08-06 10:41:20 +00:00
|
|
|
|
2016-03-14 14:36:28 +00:00
|
|
|
$format = array(
|
|
|
|
'%s',
|
|
|
|
);
|
2013-08-06 10:41:20 +00:00
|
|
|
|
2015-03-16 12:53:59 +00:00
|
|
|
$expiry = ( array_key_exists( $i, $access_expires ) && '' != $access_expires[ $i ] ) ? date_i18n( 'Y-m-d', strtotime( $access_expires[ $i ] ) ) : null;
|
2013-08-06 10:41:20 +00:00
|
|
|
|
2015-03-16 12:53:59 +00:00
|
|
|
$data['access_expires'] = $expiry;
|
|
|
|
$format[] = '%s';
|
2013-08-06 10:41:20 +00:00
|
|
|
|
2014-08-31 07:18:21 +00:00
|
|
|
$wpdb->update( $wpdb->prefix . "woocommerce_downloadable_product_permissions",
|
|
|
|
$data,
|
|
|
|
array(
|
2013-08-06 10:41:20 +00:00
|
|
|
'order_id' => $post_id,
|
2013-10-25 19:02:21 +00:00
|
|
|
'product_id' => absint( $product_ids[ $i ] ),
|
2016-08-27 01:46:45 +00:00
|
|
|
'download_id' => wc_clean( $download_ids[ $i ] ),
|
2013-08-06 10:41:20 +00:00
|
|
|
),
|
|
|
|
$format, array( '%d', '%d', '%s' )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-31 07:18:21 +00:00
|
|
|
}
|