/**
 * Order Status completed - GIVE DOWNLOADABLE PRODUCT ACCESS TO CUSTOMER
 *
 * @access public
 * @param int $order_id
 * @return void
 */
function woocommerce_downloadable_product_permissions($order_id)
{
    global $wpdb;
    if (get_post_meta($order_id, __('Download Permissions Granted', 'woocommerce'), true) == 1) {
        return;
    }
    // Only do this once
    $order = new WC_Order($order_id);
    if (sizeof($order->get_items()) > 0) {
        foreach ($order->get_items() as $item) {
            if ($item['product_id'] > 0) {
                $_product = $order->get_product_from_item($item);
                if ($_product->exists() && $_product->is_downloadable()) {
                    $product_id = $item['variation_id'] > 0 ? $item['variation_id'] : $item['product_id'];
                    $file_download_paths = apply_filters('woocommerce_file_download_paths', get_post_meta($product_id, '_file_paths', true), $product_id, $order_id, $item);
                    if (!empty($file_download_paths)) {
                        foreach ($file_download_paths as $download_id => $file_path) {
                            woocommerce_downloadable_file_permission($download_id, $product_id, $order);
                        }
                    }
                }
            }
        }
    }
    update_post_meta($order_id, __('Download Permissions Granted', 'woocommerce'), 1);
}
/**
 * Grant downloadable file access to any newly added files on any existing
 * orders for this product that have previously been granted downloadable file access
 *
 * @access public
 * @param int $product_id product identifier
 * @param int $variation_id optional product variation identifier
 * @param array $file_paths newly set file paths
 */
function woocommerce_process_product_file_download_paths($product_id, $variation_id, $file_paths)
{
    global $wpdb;
    if ($variation_id) {
        $product_id = $variation_id;
    }
    // determine whether any new files have been added
    $existing_file_paths = apply_filters('woocommerce_file_download_paths', get_post_meta($product_id, '_file_paths', true), $product_id, null, null);
    if (!$existing_file_paths) {
        $existing_file_paths = array();
    }
    $new_download_ids = array_diff(array_keys($file_paths), array_keys($existing_file_paths));
    if ($new_download_ids) {
        // determine whether downloadable file access has been granted (either via the typical order completion, or via the admin ajax method)
        $existing_permissions = $wpdb->get_results($wpdb->prepare("SELECT * from {$wpdb->prefix}woocommerce_downloadable_product_permissions WHERE product_id = %d GROUP BY order_id", $product_id));
        foreach ($existing_permissions as $existing_permission) {
            $order = new WC_Order($existing_permission->order_id);
            if ($order->id) {
                foreach ($new_download_ids as $new_download_id) {
                    // grant permission if it doesn't already exist
                    if (!$wpdb->get_var($wpdb->prepare("SELECT true FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions WHERE order_id = %d AND product_id = %d AND download_id = %s", $order->id, $product_id, $new_download_id))) {
                        woocommerce_downloadable_file_permission($new_download_id, $product_id, $order);
                    }
                }
            }
        }
    }
}