WC_AJAX::grant_access_to_download
Grant download permissions via ajax function.
Method of the class: WC_AJAX{}
No Hooks.
Returns
null. Nothing (null).
Usage
$result = WC_AJAX::grant_access_to_download();
WC_AJAX::grant_access_to_download() WC AJAX::grant access to download code WC 10.4.3
public static function grant_access_to_download() {
check_ajax_referer( 'grant-access', 'security' );
if ( ! current_user_can( 'edit_shop_orders' ) || ! isset( $_POST['loop'], $_POST['order_id'], $_POST['product_ids'] ) ) {
wp_die( -1 );
}
global $wpdb;
$wpdb->hide_errors();
$order_id = intval( $_POST['order_id'] );
$product_ids = array_filter( array_map( 'absint', (array) wp_unslash( $_POST['product_ids'] ) ) );
$loop = intval( $_POST['loop'] );
$file_counter = 0;
$order = wc_get_order( $order_id );
if ( ! $order->get_billing_email() ) {
wp_die();
}
$data = array();
$items = $order->get_items();
// Check against order items first.
foreach ( $items as $item ) {
$product = $item->get_product();
if ( $product && $product->exists() && in_array( $product->get_id(), $product_ids, true ) && $product->is_downloadable() ) {
$data[ $product->get_id() ] = array(
'files' => $product->get_downloads(),
'quantity' => $item->get_quantity(),
'order_item' => $item,
);
}
}
foreach ( $product_ids as $product_id ) {
$product = wc_get_product( $product_id );
if ( isset( $data[ $product->get_id() ] ) ) {
$download_data = $data[ $product->get_id() ];
} else {
$download_data = array(
'files' => $product->get_downloads(),
'quantity' => 1,
'order_item' => null,
);
}
if ( ! empty( $download_data['files'] ) ) {
foreach ( $download_data['files'] as $download_id => $file ) {
$inserted_id = wc_downloadable_file_permission( $download_id, $product->get_id(), $order, $download_data['quantity'], $download_data['order_item'] );
if ( $inserted_id ) {
$download = new WC_Customer_Download( $inserted_id );
++$loop;
++$file_counter;
if ( $file->get_name() ) {
$file_count = $file->get_name();
} else {
/* translators: %d file count */
$file_count = sprintf( __( 'File %d', 'woocommerce' ), $file_counter );
}
include __DIR__ . '/admin/meta-boxes/views/html-order-download-permission.php';
}
}
}
}
wp_die();
}