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 ) {
|
|
|
|
?>
|
|
|
|
<div class="order_download_permissions wc-metaboxes-wrapper">
|
|
|
|
|
2018-03-05 18:59:17 +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',
|
|
|
|
)
|
|
|
|
);
|
2016-11-18 17:13:02 +00:00
|
|
|
|
|
|
|
$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() ) {
|
2016-11-18 19:29:37 +00:00
|
|
|
$product = wc_get_product( $download->get_product_id() );
|
2013-12-10 10:50:48 +00:00
|
|
|
$file_counter = 1;
|
2013-08-06 10:41:20 +00:00
|
|
|
}
|
|
|
|
|
2018-03-05 18:59:17 +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
|
|
|
|
2018-03-05 18:59:17 +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
|
|
|
|
2018-03-05 18:59:17 +00:00
|
|
|
include 'views/html-order-download-permission.php';
|
2013-08-06 10:41:20 +00:00
|
|
|
|
|
|
|
$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
|
|
|
}
|
2018-03-05 18:59:17 +00:00
|
|
|
?>
|
|
|
|
</div>
|
2013-08-06 10:41:20 +00:00
|
|
|
|
|
|
|
<div class="toolbar">
|
|
|
|
<p class="buttons">
|
2016-12-21 13:23:26 +00:00
|
|
|
<select id="grant_access_id" class="wc-product-search" name="grant_access_id[]" multiple="multiple" style="width: 400px;" data-placeholder="<?php esc_attr_e( 'Search for a downloadable product…', 'woocommerce' ); ?>" data-action="woocommerce_json_search_downloadable_products_and_variations"></select>
|
2018-03-05 18:59:17 +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
|
|
|
*
|
2018-03-05 18:59:17 +00:00
|
|
|
* @param int $post_id
|
2016-01-04 21:31:36 +00:00
|
|
|
* @param WP_Post $post
|
2013-08-06 10:41:20 +00:00
|
|
|
*/
|
|
|
|
public static function save( $post_id, $post ) {
|
2016-11-18 19:56:17 +00:00
|
|
|
if ( isset( $_POST['permission_id'] ) ) {
|
|
|
|
$permission_ids = $_POST['permission_id'];
|
2016-11-18 17:13:02 +00:00
|
|
|
$downloads_remaining = $_POST['downloads_remaining'];
|
|
|
|
$access_expires = $_POST['access_expires'];
|
2016-11-18 19:56:17 +00:00
|
|
|
$max = max( array_keys( $permission_ids ) );
|
2013-08-06 10:41:20 +00:00
|
|
|
|
2016-11-18 19:56:17 +00:00
|
|
|
for ( $i = 0; $i <= $max; $i ++ ) {
|
|
|
|
if ( ! isset( $permission_ids[ $i ] ) ) {
|
2013-10-25 19:02:21 +00:00
|
|
|
continue;
|
2014-08-31 07:18:21 +00:00
|
|
|
}
|
2016-11-18 19:56:17 +00:00
|
|
|
$download = new WC_Customer_Download( $permission_ids[ $i ] );
|
2016-11-18 19:29:37 +00:00
|
|
|
$download->set_downloads_remaining( wc_clean( $downloads_remaining[ $i ] ) );
|
|
|
|
$download->set_access_expires( array_key_exists( $i, $access_expires ) && '' !== $access_expires[ $i ] ? strtotime( $access_expires[ $i ] ) : '' );
|
|
|
|
$download->save();
|
2013-08-06 10:41:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-31 07:18:21 +00:00
|
|
|
}
|