woocommerce/includes/admin/meta-boxes/class-wc-meta-box-order-dow...

107 lines
3.0 KiB
PHP
Raw Normal View History

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
*
* @author WooThemes
* @category Admin
2020-08-05 16:36:24 +00:00
* @package WooCommerce\Admin\Meta Boxes
2013-08-06 10:41:20 +00:00
* @version 2.1.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
2013-08-06 10:41:20 +00:00
/**
* WC_Meta_Box_Order_Downloads Class.
2013-08-06 10:41:20 +00:00
*/
class WC_Meta_Box_Order_Downloads {
/**
* Output the metabox.
*
* @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">
<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
}
// 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
// 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 );
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
}
?>
</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&hellip;', 'woocommerce' ); ?>" data-action="woocommerce_json_search_downloadable_products_and_variations"></select>
<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
}
/**
* Save meta box data.
*
* @param int $post_id
* @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 ] ) ) {
continue;
}
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
}
}
}
}