/** * Save the download permissions on the individual subscriptions as well as the order. Hooked into * 'woocommerce_grant_product_download_permissions', which is strictly after the order received all the info * it needed, so we don't need to play with priorities. * * @param integer $order_id the ID of the order. At this point it is guaranteed that it has files in it and that it hasn't been granted permissions before */ public static function save_downloadable_product_permissions($order_id) { global $wpdb; $order = wc_get_order($order_id); if (wcs_order_contains_subscription($order, 'any')) { $subscriptions = wcs_get_subscriptions_for_order($order, array('order_type' => array('any'))); } else { return; } foreach ($subscriptions as $subscription) { if (sizeof($subscription->get_items()) > 0) { foreach ($subscription->get_items() as $item) { $_product = $subscription->get_product_from_item($item); if ($_product && $_product->exists() && $_product->is_downloadable()) { $downloads = $_product->get_files(); $product_id = wcs_get_canonical_product_id($item); foreach (array_keys($downloads) as $download_id) { // grant access on subscription if it does not already exist if (!$wpdb->get_var($wpdb->prepare("SELECT download_id FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions WHERE `order_id` = %d AND `product_id` = %d AND `download_id` = '%s'", $subscription->id, $product_id, $download_id))) { wc_downloadable_file_permission($download_id, $product_id, $subscription, $item['qty']); } self::revoke_downloadable_file_permission($product_id, $order_id, $order->user_id); } } } } update_post_meta($subscription->id, '_download_permissions_granted', 1); } }
/** * Process the remove or re-add a line item from a subscription request. * * @since 2.0 */ public static function maybe_remove_or_add_item_to_subscription() { if (isset($_GET['subscription_id']) && (isset($_GET['remove_item']) || isset($_GET['undo_remove_item'])) && isset($_GET['_wpnonce'])) { $subscription = wcs_is_subscription($_GET['subscription_id']) ? wcs_get_subscription($_GET['subscription_id']) : false; $undo_request = isset($_GET['undo_remove_item']) ? true : false; $item_id = $undo_request ? $_GET['undo_remove_item'] : $_GET['remove_item']; if (false === $subscription) { wc_add_notice(sprintf(_x('Subscription #%d does not exist.', 'hash before subscription ID', 'woocommerce-subscriptions'), $_GET['subscription_id']), 'error'); wp_safe_redirect(wc_get_page_permalink('myaccount')); exit; } if (self::validate_remove_items_request($subscription, $item_id, $undo_request)) { if ($undo_request) { // handle undo request $removed_item = WC()->session->get('removed_subscription_items', array()); if (!empty($removed_item[$item_id]) && $subscription->id == $removed_item[$item_id]) { // restore the item wc_update_order_item($item_id, array('order_item_type' => 'line_item')); unset($removed_item[$item_id]); WC()->session->set('removed_subscription_items', $removed_item); // restore download permissions for this item $line_items = $subscription->get_items(); $line_item = $line_items[$item_id]; $_product = $subscription->get_product_from_item($line_item); $product_id = wcs_get_canonical_product_id($line_item); if ($_product && $_product->exists() && $_product->is_downloadable()) { $downloads = $_product->get_files(); foreach (array_keys($downloads) as $download_id) { wc_downloadable_file_permission($download_id, $product_id, $subscription, $line_item['qty']); } } // translators: 1$: product name, 2$: product id $subscription->add_order_note(sprintf(_x('Customer added "%1$s" (Product ID: #%2$d) via the My Account page.', 'used in order note', 'woocommerce-subscriptions'), wcs_get_line_item_name($line_item), $product_id)); } else { wc_add_notice(__('Your request to undo your previous action was unsuccessful.', 'woocommerce-subscriptions')); } } else { // handle remove item requests WC()->session->set('removed_subscription_items', array($item_id => $subscription->id)); // remove download access for the item $line_items = $subscription->get_items(); $line_item = $line_items[$item_id]; $product_id = wcs_get_canonical_product_id($line_item); WCS_Download_Handler::revoke_downloadable_file_permission($product_id, $subscription->id, $subscription->get_user_id()); // remove the line item from subscription but preserve its data in the DB wc_update_order_item($item_id, array('order_item_type' => 'line_item_removed')); // translators: 1$: product name, 2$: product id $subscription->add_order_note(sprintf(_x('Customer removed "%1$s" (Product ID: #%2$d) via the My Account page.', 'used in order note', 'woocommerce-subscriptions'), wcs_get_line_item_name($line_item), $product_id)); // translators: placeholders are 1$: item name, and, 2$: opening and, 3$: closing link tags wc_add_notice(sprintf(__('You have successfully removed "%1$s" from your subscription. %2$sUndo?%3$s', 'woocommerce-subscriptions'), $line_item['name'], '<a href="' . esc_url(self::get_undo_remove_url($subscription->id, $item_id, $subscription->get_view_order_url())) . '" >', '</a>')); } } $subscription->calculate_totals(); wp_safe_redirect($subscription->get_view_order_url()); exit; } }
/** * Grant download permissions via ajax function */ public static function grant_access_to_download() { check_ajax_referer('grant-access', 'security'); if (!current_user_can('edit_shop_orders')) { die(-1); } global $wpdb; $wpdb->hide_errors(); $order_id = intval($_POST['order_id']); $product_ids = $_POST['product_ids']; $loop = intval($_POST['loop']); $file_counter = 0; $order = wc_get_order($order_id); if (!is_array($product_ids)) { $product_ids = array($product_ids); } foreach ($product_ids as $product_id) { $product = wc_get_product($product_id); $files = $product->get_files(); if (!$order->billing_email) { die; } if ($files) { foreach ($files as $download_id => $file) { if ($inserted_id = wc_downloadable_file_permission($download_id, $product_id, $order)) { // insert complete - get inserted data $download = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions WHERE permission_id = %d", $inserted_id)); $loop++; $file_counter++; if (isset($file['name'])) { $file_count = $file['name']; } else { $file_count = sprintf(__('File %d', 'woocommerce'), $file_counter); } include 'admin/meta-boxes/views/html-order-download-permission.php'; } } } } die; }
/** * Order Status completed - GIVE DOWNLOADABLE PRODUCT ACCESS TO CUSTOMER * * @access public * @param int $order_id * @return void */ function wc_downloadable_product_permissions($order_id) { if (get_post_meta($order_id, '_download_permissions_granted', true) == 1) { return; // Only do this once } $order = wc_get_order($order_id); if ($order->has_status('processing') && get_option('woocommerce_downloads_grant_access_after_payment') == 'no') { return; } if (sizeof($order->get_items()) > 0) { foreach ($order->get_items() as $item) { $_product = $order->get_product_from_item($item); if ($_product && $_product->exists() && $_product->is_downloadable()) { $downloads = $_product->get_files(); foreach (array_keys($downloads) as $download_id) { wc_downloadable_file_permission($download_id, $item['variation_id'] > 0 ? $item['variation_id'] : $item['product_id'], $order, $item['qty']); } } } } update_post_meta($order_id, '_download_permissions_granted', 1); do_action('woocommerce_grant_product_download_permissions', $order_id); }
/** * @deprecated */ function woocommerce_downloadable_file_permission($download_id, $product_id, $order) { return wc_downloadable_file_permission($download_id, $product_id, $order); }
/** * Grant downloadable file access to any newly added files on any existing * orders for this product that have previously been granted downloadable file access * * @param int $product_id product identifier * @param int $variation_id optional product variation identifier * @param array $downloadable_files newly set files */ public function process_product_file_download_paths($product_id, $variation_id, $downloadable_files) { global $wpdb; if ($variation_id) { $product_id = $variation_id; } $product = wc_get_product($product_id); $existing_download_ids = array_keys((array) $product->get_files()); $updated_download_ids = array_keys((array) $downloadable_files); $new_download_ids = array_filter(array_diff($updated_download_ids, $existing_download_ids)); $removed_download_ids = array_filter(array_diff($existing_download_ids, $updated_download_ids)); if (!empty($new_download_ids) || !empty($removed_download_ids)) { // determine whether downloadable file access has been granted 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 = wc_get_order($existing_permission->order_id); if (!empty($order->id)) { // Remove permissions if (!empty($removed_download_ids)) { foreach ($removed_download_ids as $download_id) { if (apply_filters('woocommerce_process_product_file_download_paths_remove_access_to_old_file', true, $download_id, $product_id, $order)) { $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions WHERE order_id = %d AND product_id = %d AND download_id = %s", $order->id, $product_id, $download_id)); } } } // Add permissions if (!empty($new_download_ids)) { foreach ($new_download_ids as $download_id) { if (apply_filters('woocommerce_process_product_file_download_paths_grant_access_to_new_file', true, $download_id, $product_id, $order)) { // grant permission if it doesn't already exist if (!$wpdb->get_var($wpdb->prepare("SELECT 1=1 FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions WHERE order_id = %d AND product_id = %d AND download_id = %s", $order->id, $product_id, $download_id))) { wc_downloadable_file_permission($download_id, $product_id, $order); } } } } } } } }
/** * Order Status completed - GIVE DOWNLOADABLE PRODUCT ACCESS TO CUSTOMER. * * @param int $order_id */ function wc_downloadable_product_permissions($order_id, $force = false) { $order = wc_get_order($order_id); if (!$order || $order->get_data_store()->get_download_permissions_granted($order) && !$force) { return; } if ($order->has_status('processing') && 'no' === get_option('woocommerce_downloads_grant_access_after_payment')) { return; } if (sizeof($order->get_items()) > 0) { foreach ($order->get_items() as $item) { $product = $item->get_product(); if ($product && $product->exists() && $product->is_downloadable()) { $downloads = $product->get_downloads(); foreach (array_keys($downloads) as $download_id) { wc_downloadable_file_permission($download_id, $product, $order, $item->get_quantity()); } } } } $order->get_data_store()->set_download_permissions_granted($order, true); do_action('woocommerce_grant_product_download_permissions', $order_id); }
/** * Grant download permissions via ajax function. */ public static function grant_access_to_download() { check_ajax_referer('grant-access', 'security'); if (!current_user_can('edit_shop_orders')) { die(-1); } global $wpdb; $wpdb->hide_errors(); $order_id = intval($_POST['order_id']); $product_ids = $_POST['product_ids']; $loop = intval($_POST['loop']); $file_counter = 0; $order = wc_get_order($order_id); if (!is_array($product_ids)) { $product_ids = array($product_ids); } foreach ($product_ids as $product_id) { $product = wc_get_product($product_id); $files = $product->get_downloads(); if (!$order->get_billing_email()) { die; } if (!empty($files)) { foreach ($files as $download_id => $file) { if ($inserted_id = wc_downloadable_file_permission($download_id, $product_id, $order)) { $download = new WC_Customer_Download($inserted_id); $loop++; $file_counter++; if ($file->get_name()) { $file_count = $file->get_name(); } else { $file_count = sprintf(__('File %d', 'woocommerce'), $file_counter); } include 'admin/meta-boxes/views/html-order-download-permission.php'; } } } } die; }
/** * Grant downloadable file access to any newly added files on any existing subscriptions * which don't have existing permissions. * * @param int $product_id * @param int $variation_id * @param array $downloadable_files product downloadable files * @since 2.0.18 */ public static function grant_new_file_product_permissions($product_id, $variation_id, $downloadable_files) { global $wpdb; $product_id = $variation_id ? $variation_id : $product_id; $product = wc_get_product($product_id); $existing_download_ids = array_keys((array) $product->get_files()); $downloadable_ids = array_keys((array) $downloadable_files); $new_download_ids = array_filter(array_diff($downloadable_ids, $existing_download_ids)); if (!empty($new_download_ids)) { $existing_permissions = $wpdb->get_col($wpdb->prepare("SELECT order_id from {$wpdb->prefix}woocommerce_downloadable_product_permissions WHERE product_id = %d GROUP BY order_id", $product_id)); $subscriptions = wcs_get_subscriptions_for_product($product_id); foreach ($subscriptions as $subscription_id) { // only grant permissions to subscriptions which have no permissions for this product if (!in_array($subscription_id, $existing_permissions)) { $subscription = wcs_get_subscription($subscription_id); foreach ($new_download_ids as $download_id) { if ($subscription && apply_filters('woocommerce_process_product_file_download_paths_grant_access_to_new_file', true, $download_id, $product_id, $subscription)) { wc_downloadable_file_permission($download_id, $product_id, $subscription); } } } } } }
/** * Grant download permissions via ajax function * * @access public * @return void */ function grant_access_to_download() { check_ajax_referer('grant-access', 'security'); global $wpdb; $order_id = intval($_POST['order_id']); $product_ids = $_POST['product_ids']; $loop = intval($_POST['loop']); $file_counter = 0; $order = new WC_Order($order_id); if (!is_array($product_ids)) { $product_ids = array($product_ids); } foreach ($product_ids as $product_id) { $product = get_product($product_id); $files = $product->get_files(); if (!$order->billing_email) { die; } if ($files) { foreach ($files as $download_id => $file) { if ($inserted_id = wc_downloadable_file_permission($download_id, $product_id, $order)) { // insert complete - get inserted data $download = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions WHERE permission_id = %d", $inserted_id)); $loop++; $file_counter++; if (isset($file['name'])) { $file_count = $file['name']; } else { $file_count = sprintf(__('File %d', 'woocommerce'), $file_counter); } include dirname(dirname(__FILE__)) . '/templates/orders/order-download-permission-html.php'; } } } } die; }
/** * Insert pdf voucher files * * Handles to insert pdf voucher * files in database * * @package WooCommerce - PDF Vouchers * @since 1.0.0 */ public function woo_vou_insert_downloadable_files($order_id) { $prefix = WOO_VOU_META_PREFIX; $downloadable_files = array(); //Get Order $order = new WC_Order($order_id); if (sizeof($order->get_items()) > 0) { //Get all items in order foreach ($order->get_items() as $item_id => $item) { //Product Data $_product = $order->get_product_from_item($item); // Taking variation id $variation_id = !empty($item['variation_id']) ? $item['variation_id'] : ''; if ($_product && $_product->exists()) { // && $_product->is_downloadable() //get product id from prduct data $product_id = isset($_product->id) ? $_product->id : ''; // If product is variable product take variation id else product id $data_id = !empty($variation_id) ? $variation_id : $product_id; if ($this->model->woo_vou_check_enable_voucher($product_id, $variation_id)) { //Check voucher is enabled or not //Get vouchers downlodable pdf files $downloadable_files = $this->woo_vou_get_vouchers_download_key($order_id, $data_id, $item_id); foreach (array_keys($downloadable_files) as $download_id) { //Insert pdf vouchers in downloadable table wc_downloadable_file_permission($download_id, $data_id, $order); } } } } } // Status update from pending to publish when voucher is get completed or processing $args = array('post_status' => array('pending'), 'meta_query' => array(array('key' => $prefix . 'order_id', 'value' => $order_id))); // Get vouchers code of this order $purchased_vochers = $this->model->woo_vou_get_voucher_details($args); if (!empty($purchased_vochers)) { // If not empty voucher codes //For all possible vouchers foreach ($purchased_vochers as $vocher) { // Get voucher data $current_post = get_post($vocher['ID'], 'ARRAY_A'); //Change voucher status $current_post['post_status'] = 'publish'; //Update voucher post wp_update_post($current_post); } } }
/** * Order Status completed - GIVE DOWNLOADABLE PRODUCT ACCESS TO CUSTOMER * * @access public * @param int $order_id * @return void */ function wc_downloadable_product_permissions($order_id) { if (get_post_meta($order_id, '_download_permissions_granted', 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) { $_product = $order->get_product_from_item($item); if ($_product && $_product->exists() && $_product->is_downloadable()) { $downloads = $_product->get_files(); foreach (array_keys($downloads) as $download_id) { wc_downloadable_file_permission($download_id, $item['variation_id'] > 0 ? $item['variation_id'] : $item['product_id'], $order); } } } } update_post_meta($order_id, '_download_permissions_granted', 1); do_action('woocommerce_grant_product_download_permissions', $order_id); }
public static function HandleFiles($ID, &$downloadable_files) { global $wpdb; $product_id = $ID; $existing_download_ids = array_keys((array) get_post_meta($ID, '_downloadable_files', true)); $updated_download_ids = array_keys((array) $downloadable_files); $new_download_ids = array_filter(array_diff($updated_download_ids, $existing_download_ids)); $removed_download_ids = array_filter(array_diff($existing_download_ids, $updated_download_ids)); if ($new_download_ids || $removed_download_ids) { // determine whether downloadable file access has been granted 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 ($wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->prefix}posts WHERE ID = %d", $existing_permission->order_id))) { // Remove permissions if ($removed_download_ids) { foreach ($removed_download_ids as $download_id) { if (apply_filters('woocommerce_process_product_file_download_paths_remove_access_to_old_file', true, $download_id, $product_id, $order)) { $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions WHERE order_id = %d AND product_id = %d AND download_id = %s", $existing_permission->order_id, $product_id, $download_id)); } } } // Add permissions if ($new_download_ids) { foreach ($new_download_ids as $download_id) { if (apply_filters('woocommerce_process_product_file_download_paths_grant_access_to_new_file', true, $download_id, $product_id, $order)) { // grant permission if it doesn't already exist if (!$wpdb->get_var($wpdb->prepare("SELECT 1 FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions WHERE order_id = %d AND product_id = %d AND download_id = %s", $existing_permission->order_id, $product_id, $download_id))) { if (function_exists('wc_downloadable_file_permission')) { wc_downloadable_file_permission($download_id, $product_id, $existing_permission->order_id); } else { self::copied_wc_downloadable_file_permission($download_id, $product_id, $existing_permission->order_id); } } } } } } } } }
/** * Called when an order is updated from the admin, creates a new voucher if * a voucher item was added, and updates the voucher expiration and redeem * item meta. * * @since 1.2 * @param int $post_id the post identifier * @param object $post the order post object * * @return array order item data to persist */ public function process_shop_order_meta($post_id, $post) { // get the order $order = wc_get_order($post_id); $order_items = $order->get_items(); // loop through any order items by id if (isset($_POST['order_item_id'])) { foreach ($_POST['order_item_id'] as $item_id) { $item_id = absint($item_id); if (!isset($order_items[$item_id]) || !$order_items[$item_id]) { continue; } $order_item = $order_items[$item_id]; $product_id = $order_item['variation_id'] ? $order_item['variation_id'] : $order_item['product_id']; $product = wc_get_product($product_id); // if we have a voucher product, but no voucher set for the order item, this is likely an item newly added from the admin, so create a default voucher if ($product && $product->is_downloadable() && WC_PDF_Product_Vouchers_Product::has_voucher($product) && (!isset($order_item['voucher_id']) || !$order_item['voucher_id'])) { $voucher = WC_PDF_Product_Vouchers_Product::get_voucher($product); $voucher_number = WC_PDF_Product_Vouchers_Voucher::generate_voucher_number(); wc_add_order_item_meta($item_id, '_voucher_image_id', $voucher->get_image_id()); wc_add_order_item_meta($item_id, '_voucher_id', $voucher->id); wc_add_order_item_meta($item_id, '_voucher_redeem', array_pad(array(), $order_item['qty'], null)); // TODO: need to handle the order item quantity being changed from the admin wc_add_order_item_meta($item_id, '_voucher_number', $voucher_number); // if download permissions have already been granted, grant permission to the newly created voucher if (isset($order->download_permissions_granted[0]) && 1 == $order->download_permissions_granted[0]) { wc_downloadable_file_permission('wc_vouchers_' . $voucher_number, $product_id, $order); } } if (isset($_POST['voucher_expiration'][$item_id])) { wc_update_order_item_meta($item_id, '_voucher_expiration', $_POST['voucher_expiration'][$item_id]); } if (isset($_POST['voucher_redeem'][$item_id])) { wc_update_order_item_meta($item_id, '_voucher_redeem', $_POST['voucher_redeem'][$item_id]); } } } }