/**
 * Purchases Remaining
 *
 * @since       1.0.1
 * @param       array $atts Arguments to pass to the shortcode
 * @global      object $post The object related to this post
 * @return      void
 */
function edd_purchase_limit_remaining_shortcode($atts)
{
    global $post;
    $scope = edd_get_option('edd_purchase_limit_scope') ? edd_get_option('edd_purchase_limit_scope') : 'site-wide';
    $sold_out_label = edd_get_option('edd_purchase_limit_sold_out_label') ? edd_get_option('edd_purchase_limit_sold_out_label') : __('Sold Out', 'edd-purchase-limit');
    $defaults = array('download_id' => $post->ID);
    $atts = wp_parse_args($atts, $defaults);
    $max_purchases = edd_pl_get_file_purchase_limit($atts['download_id']);
    if ($scope == 'site-wide' && $max_purchases) {
        $purchases = edd_get_download_sales_stats($atts['download_id']);
    } elseif ($scope == 'per-user' && $max_purchases) {
        $purchases = edd_pl_get_user_purchase_count(get_current_user_id(), $atts['download_id']);
    }
    if ($purchases < $max_purchases) {
        $purchases_left = $max_purchases - $purchases;
        return '<span class="edd_purchases_left">' . $purchases_left . '</span>';
    } else {
        return '<span class="edd_purchases_left edd_sold_out">' . $sold_out_label . '</span>';
    }
}
/**
 * Ensure cart quantities are OK
 *
 * @since       1.0.0
 * @return      void
 */
function edd_pl_checkout_errors($valid_data, $posted)
{
    global $edd_prices_sold_out;
    $cart = edd_get_cart_contents();
    $scope = edd_get_option('edd_purchase_limit_scope') ? edd_get_option('edd_purchase_limit_scope') : 'site-wide';
    $errors = array();
    foreach ($cart as $item) {
        if (edd_has_variable_prices($item['id'])) {
            if (edd_pl_is_item_sold_out($item['id'], $item['options']['price_id'], false, false)) {
                $errors[] = array('id' => $item['id'], 'price' => $item['options']['price_id'], 'type' => 'soldout', 'avail' => null);
            }
        } else {
            $max_purchases = edd_pl_get_file_purchase_limit($item['id']);
            if ($scope == 'site-wide') {
                $purchases = edd_get_download_sales_stats($item['id']);
                if ($max_purchases && $purchases >= $max_purchases || !empty($edd_prices_sold_out)) {
                    $errors[] = array('id' => $item['id'], 'price' => null, 'type' => 'soldout', 'avail' => null);
                }
            } else {
                if (is_user_logged_in()) {
                    $purchases = edd_pl_get_user_purchase_count(get_current_user_id(), $item['id']);
                    if ($max_purchases && $purchases >= $max_purchases || !empty($edd_prices_sold_out)) {
                        $errors[] = array('id' => $item['id'], 'price' => null, 'type' => 'soldout', 'avail' => null);
                    }
                }
            }
        }
        if (edd_item_in_cart($item['id'])) {
            if (edd_has_variable_prices($item['id'])) {
                $max_purchases = edd_pl_get_file_purchase_limit($item['id'], null, $item['options']['price_id']);
                $purchases = edd_pl_get_file_purchases($item['id'], $item['options']['price_id']);
            }
            if ($max_purchases > 0) {
                $cart_qty = edd_get_cart_item_quantity($item['id']);
                $total = $purchases + $cart_qty;
                if ($total > $max_purchases) {
                    $errors[] = array('id' => $item['id'], 'price' => edd_has_variable_prices($item['id']) ? $item['options']['price_id'] : null, 'type' => 'toomany', 'avail' => $max_purchases - $purchases);
                }
            }
        }
    }
    if (count($errors) > 0) {
        foreach ($errors as $error) {
            $product = get_post($error['id']);
            if ($error['type'] == 'soldout') {
                edd_set_error('purchase_limit_reached', sprintf(__('The %s "%s" is sold out!', 'edd-purchase-limit'), strtolower(edd_get_label_singular()), $product->post_title));
            } elseif ($error['type'] == 'toomany') {
                edd_set_error('purchase_limit_exceeded', sprintf(_n('There is only %s available for the %s "%s"!', 'There are only %s available for the %s "%s"!', $error['avail'], 'edd-purchase-limit'), $error['avail'], strtolower(edd_get_label_singular()), $product->post_title));
            }
        }
    }
}
 /**
  * Add the table cell to the variable pricing table
  *
  * @access      public
  * @since       1.0.4
  * @param       int $post_id The ID of this download
  * @param       int $price_key The key of this download item
  * @param       array $args Args to pass for this row
  * @return      void
  */
 public function price_row($post_id = 0, $price_key = 0, $args = array())
 {
     $prices = edd_get_variable_prices($post_id);
     $purchase_limit = edd_pl_get_file_purchase_limit($post_id, 'variable', $price_key);
     echo '<td class="edd_purchase_limit_var_field">';
     echo '<label for="edd_variable_prices[' . $price_key . '][purchase_limit]">';
     echo '<input type="text" value="' . $purchase_limit . '" id="edd_variable_prices[' . $price_key . '][purchase_limit]" name="edd_variable_prices[' . $price_key . '][purchase_limit]" style="float:left;width:100px;" placeholder="0" />';
     echo '</label>';
     echo '</td>';
 }