WC_AJAX::grant_access_to_download()public staticWC 1.0

Grant download permissions via ajax function.

Метод класса: WC_AJAX{}

Хуков нет.

Возвращает

null. Ничего (null).

Использование

$result = WC_AJAX::grant_access_to_download();

Код WC_AJAX::grant_access_to_download() WC 8.7.0

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();
}