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

132 lines
3.5 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
* @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 ) {
global $post, $wpdb;
2013-08-06 10:41:20 +00:00
?>
<div class="order_download_permissions wc-metaboxes-wrapper">
<div class="wc-metaboxes">
<?php
$download_permissions = $wpdb->get_results( $wpdb->prepare( "
SELECT * FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions
WHERE order_id = %d ORDER BY product_id
", $post->ID ) );
2015-01-14 12:17:19 +00:00
$product = null;
$loop = 0;
$file_counter = 1;
2013-08-06 10:41:20 +00:00
if ( $download_permissions && sizeof( $download_permissions ) > 0 ) foreach ( $download_permissions as $download ) {
if ( ! $product || $product->id != $download->product_id ) {
$product = wc_get_product( absint( $download->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
if ( ! $product || ! $product->exists() || ! $product->has_file( $download->download_id ) )
continue;
// Show file title instead of count if set
$file = $product->get_file( $download->download_id );
if ( isset( $file['name'] ) ) {
$file_count = $file['name'];
} else {
$file_count = sprintf( __( 'File %d', 'woocommerce' ), $file_counter );
}
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
}
?>
</div>
<div class="toolbar">
<p class="buttons">
<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&hellip;', 'woocommerce' ); ?>" data-action="woocommerce_json_search_downloadable_products_and_variations" />
2013-08-06 10:41:20 +00:00
<button type="button" class="button grant_access"><?php _e( 'Grant Access', 'woocommerce' ); ?></button>
</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 ) {
global $wpdb;
2013-08-06 10:41:20 +00:00
if ( isset( $_POST['download_id'] ) ) {
// Download data
$download_ids = $_POST['download_id'];
$product_ids = $_POST['product_id'];
$downloads_remaining = $_POST['downloads_remaining'];
$access_expires = $_POST['access_expires'];
2013-08-06 10:41:20 +00:00
// Order data
$product_ids_max = max( array_keys( $product_ids ) );
2013-08-06 10:41:20 +00:00
for ( $i = 0; $i <= $product_ids_max; $i ++ ) {
if ( ! isset( $product_ids[ $i ] ) ) {
continue;
}
2013-08-06 10:41:20 +00:00
$data = array(
'downloads_remaining' => wc_clean( $downloads_remaining[ $i ] ),
);
2013-08-06 10:41:20 +00:00
$format = array(
'%s',
);
2013-08-06 10:41:20 +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
$data['access_expires'] = $expiry;
$format[] = '%s';
2013-08-06 10:41:20 +00:00
$wpdb->update( $wpdb->prefix . "woocommerce_downloadable_product_permissions",
$data,
array(
2013-08-06 10:41:20 +00:00
'order_id' => $post_id,
'product_id' => absint( $product_ids[ $i ] ),
'download_id' => wc_clean( $download_ids[ $i ] )
2013-08-06 10:41:20 +00:00
),
$format, array( '%d', '%d', '%s' )
);
}
}
}
}