Example #1
0
/**
 * Load Scripts
 *
 * Enqueues the required scripts.
 *
 * @since 1.0
 * @global $post
 * @return void
 */
function edd_load_scripts()
{
    global $post;
    $js_dir = EDD_PLUGIN_URL . 'assets/js/';
    // Use minified libraries if SCRIPT_DEBUG is turned off
    $suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
    // Get position in cart of current download
    if (isset($post->ID)) {
        $position = edd_get_item_position_in_cart($post->ID);
    }
    if (edd_is_checkout()) {
        if (edd_is_cc_verify_enabled()) {
            wp_register_script('creditCardValidator', $js_dir . 'jquery.creditCardValidator' . $suffix . '.js', array('jquery'), EDD_VERSION);
            wp_enqueue_script('creditCardValidator');
        }
        wp_register_script('edd-checkout-global', $js_dir . 'edd-checkout-global' . $suffix . '.js', array('jquery'), EDD_VERSION);
        wp_enqueue_script('edd-checkout-global');
        wp_localize_script('edd-checkout-global', 'edd_global_vars', apply_filters('edd_global_checkout_script_vars', array('ajaxurl' => edd_get_ajax_url(), 'checkout_nonce' => wp_create_nonce('edd_checkout_nonce'), 'currency_sign' => edd_currency_filter(''), 'currency_pos' => edd_get_option('currency_position', 'before'), 'no_gateway' => __('Please select a payment method', 'edd'), 'no_discount' => __('Please enter a discount code', 'edd'), 'enter_discount' => __('Enter discount', 'edd'), 'discount_applied' => __('Discount Applied', 'edd'), 'no_email' => __('Please enter an email address before applying a discount code', 'edd'), 'no_username' => __('Please enter a username before applying a discount code', 'edd'), 'purchase_loading' => __('Please Wait...', 'edd'), 'complete_purchase' => __('Purchase', 'edd'), 'taxes_enabled' => edd_use_taxes() ? '1' : '0', 'edd_version' => EDD_VERSION)));
    }
    // Load AJAX scripts, if enabled
    if (!edd_is_ajax_disabled()) {
        wp_register_script('edd-ajax', $js_dir . 'edd-ajax' . $suffix . '.js', array('jquery'), EDD_VERSION);
        wp_enqueue_script('edd-ajax');
        wp_localize_script('edd-ajax', 'edd_scripts', apply_filters('edd_ajax_script_vars', array('ajaxurl' => edd_get_ajax_url(), 'position_in_cart' => isset($position) ? $position : -1, 'already_in_cart_message' => __('You have already added this item to your cart', 'edd'), 'empty_cart_message' => __('Your cart is empty', 'edd'), 'loading' => __('Loading', 'edd'), 'select_option' => __('Please select an option', 'edd'), 'ajax_loader' => set_url_scheme(EDD_PLUGIN_URL . 'assets/images/loading.gif', 'relative'), 'is_checkout' => edd_is_checkout() ? '1' : '0', 'default_gateway' => edd_get_default_gateway(), 'redirect_to_checkout' => edd_straight_to_checkout() || edd_is_checkout() ? '1' : '0', 'checkout_page' => edd_get_checkout_uri(), 'permalinks' => get_option('permalink_structure') ? '1' : '0', 'quantities_enabled' => edd_item_quantities_enabled(), 'taxes_enabled' => edd_use_taxes() ? '1' : '0')));
    }
}
/**
 * Process the Add to Cart request
 *
 * @since 1.0
 *
 * @param $data
 */
function edd_process_add_to_cart($data)
{
    $download_id = absint($data['download_id']);
    $options = isset($data['edd_options']) ? $data['edd_options'] : array();
    if (!empty($data['edd_download_quantity'])) {
        $options['quantity'] = absint($data['edd_download_quantity']);
    }
    if (isset($options['price_id']) && is_array($options['price_id'])) {
        foreach ($options['price_id'] as $key => $price_id) {
            $options['quantity'][$key] = isset($data['edd_download_quantity_' . $price_id]) ? absint($data['edd_download_quantity_' . $price_id]) : 1;
        }
    }
    $cart = edd_add_to_cart($download_id, $options);
    if (edd_straight_to_checkout() && !edd_is_checkout()) {
        $query_args = remove_query_arg(array('edd_action', 'download_id', 'edd_options'));
        $query_part = strpos($query_args, "?");
        $url_parameters = '';
        if (false !== $query_part) {
            $url_parameters = substr($query_args, $query_part);
        }
        wp_redirect(edd_get_checkout_uri() . $url_parameters, 303);
        edd_die();
    } else {
        wp_redirect(remove_query_arg(array('edd_action', 'download_id', 'edd_options')));
        edd_die();
    }
}
/**
 * Process the Add to Cart request
 *
 * @since 1.0
 * @return void
 */
function edd_process_add_to_cart($data)
{
    $download_id = $data['download_id'];
    $options = isset($data['edd_options']) ? $data['edd_options'] : array();
    $cart = edd_add_to_cart($download_id, $options);
    if (edd_straight_to_checkout() && !edd_is_checkout()) {
        wp_redirect(edd_get_checkout_uri(), 303);
        edd_die();
    }
}
/**
 * Process the Add to Cart request
 *
 * @since 1.0
 *
 * @param $data
 */
function edd_process_add_to_cart( $data ) {
	$download_id = absint( $data['download_id'] );
	$options     = isset( $data['edd_options'] ) ? $data['edd_options'] : array();
	$cart        = edd_add_to_cart( $download_id, $options );

	if ( edd_straight_to_checkout() && ! edd_is_checkout() ) {
		wp_redirect( edd_get_checkout_uri(), 303 );
		edd_die();
	} else {
		wp_redirect( remove_query_arg( array( 'edd_action', 'download_id', 'edd_options' ) ) ); edd_die();
	}
}
/**
 * Process the Add to Cart request
 *
 * @since 1.0
 *
 * @param $data
 */
function edd_process_add_to_cart($data)
{
    $download_id = absint($data['download_id']);
    $options = isset($data['edd_options']) ? $data['edd_options'] : array();
    if (!empty($data['edd_download_quantity'])) {
        $options['quantity'] = absint($data['edd_download_quantity']);
    }
    if (is_array($options['price_id'])) {
        foreach ($options['price_id'] as $key => $price_id) {
            $options['quantity'][$key] = absint($data['edd_download_quantity_' . $price_id]);
        }
    }
    $cart = edd_add_to_cart($download_id, $options);
    if (edd_straight_to_checkout() && !edd_is_checkout()) {
        wp_redirect(edd_get_checkout_uri(), 303);
        edd_die();
    } else {
        wp_redirect(remove_query_arg(array('edd_action', 'download_id', 'edd_options')));
        edd_die();
    }
}
/**
 * Get Purchase Link
 *
 * Builds a Purchase link for a specified download based on arguments passed.
 * This function is used all over EDD to generate the Purchase or Add to Cart
 * buttons. If no arguments are passed, the function uses the defaults that have
 * been set by the plugin. The Purchase link is built for simple and variable
 * pricing and filters are available throughout the function to override
 * certain elements of the function.
 *
 * $download_id = null, $link_text = null, $style = null, $color = null, $class = null
 *
 * @since 1.0
 * @param array $args Arguments for display
 * @return string $purchase_form
 */
function edd_get_purchase_link($args = array())
{
    global $post, $edd_displayed_form_ids;
    $purchase_page = edd_get_option('purchase_page', false);
    if (!$purchase_page || $purchase_page == 0) {
        edd_set_error('set_checkout', sprintf(__('No checkout page has been configured. Visit <a href="%s">Settings</a> to set one.', 'easy-digital-downloads'), admin_url('edit.php?post_type=download&page=edd-settings')));
        edd_print_errors();
        return false;
    }
    $post_id = is_object($post) ? $post->ID : 0;
    $button_behavior = edd_get_download_button_behavior($post_id);
    $defaults = apply_filters('edd_purchase_link_defaults', array('download_id' => $post_id, 'price' => (bool) true, 'price_id' => isset($args['price_id']) ? $args['price_id'] : false, 'direct' => $button_behavior == 'direct' ? true : false, 'text' => $button_behavior == 'direct' ? edd_get_option('buy_now_text', __('Buy Now', 'easy-digital-downloads')) : edd_get_option('add_to_cart_text', __('Purchase', 'easy-digital-downloads')), 'style' => edd_get_option('button_style', 'button'), 'color' => edd_get_option('checkout_color', 'blue'), 'class' => 'edd-submit'));
    $args = wp_parse_args($args, $defaults);
    // Override the stright_to_gateway if the shop doesn't support it
    if (!edd_shop_supports_buy_now()) {
        $args['direct'] = false;
    }
    $download = new EDD_Download($args['download_id']);
    if (empty($download->ID)) {
        return false;
    }
    if ('publish' !== $download->post_status && !current_user_can('edit_product', $download->ID)) {
        return false;
        // Product not published or user doesn't have permission to view drafts
    }
    // Override color if color == inherit
    $args['color'] = $args['color'] == 'inherit' ? '' : $args['color'];
    $options = array();
    $variable_pricing = $download->has_variable_prices();
    $data_variable = $variable_pricing ? ' data-variable-price="yes"' : 'data-variable-price="no"';
    $type = $download->is_single_price_mode() ? 'data-price-mode=multi' : 'data-price-mode=single';
    $show_price = $args['price'] && $args['price'] !== 'no';
    $data_price_value = 0;
    $price = false;
    if ($variable_pricing && false !== $args['price_id']) {
        $price_id = $args['price_id'];
        $prices = $download->prices;
        $options['price_id'] = $args['price_id'];
        $found_price = isset($prices[$price_id]) ? $prices[$price_id]['amount'] : false;
        $data_price_value = $found_price;
        if ($show_price) {
            $price = $found_price;
        }
    } elseif (!$variable_pricing) {
        $data_price_value = $download->price;
        if ($show_price) {
            $price = $download->price;
        }
    }
    $args['display_price'] = $data_price_value;
    $data_price = 'data-price="' . $data_price_value . '"';
    $button_text = !empty($args['text']) ? '&nbsp;&ndash;&nbsp;' . $args['text'] : '';
    if (false !== $price) {
        if (0 == $price) {
            $args['text'] = __('Free', 'easy-digital-downloads') . $button_text;
        } else {
            $args['text'] = edd_currency_filter(edd_format_amount($price)) . $button_text;
        }
    }
    if (edd_item_in_cart($download->ID, $options) && (!$variable_pricing || !$download->is_single_price_mode())) {
        $button_display = 'style="display:none;"';
        $checkout_display = '';
    } else {
        $button_display = '';
        $checkout_display = 'style="display:none;"';
    }
    // Collect any form IDs we've displayed already so we can avoid duplicate IDs
    if (isset($edd_displayed_form_ids[$download->ID])) {
        $edd_displayed_form_ids[$download->ID]++;
    } else {
        $edd_displayed_form_ids[$download->ID] = 1;
    }
    $form_id = !empty($args['form_id']) ? $args['form_id'] : 'edd_purchase_' . $download->ID;
    // If we've already generated a form ID for this download ID, apped -#
    if ($edd_displayed_form_ids[$download->ID] > 1) {
        $form_id .= '-' . $edd_displayed_form_ids[$download->ID];
    }
    $args = apply_filters('edd_purchase_link_args', $args);
    ob_start();
    ?>
	<form id="<?php 
    echo $form_id;
    ?>
" class="edd_download_purchase_form edd_purchase_<?php 
    echo absint($download->ID);
    ?>
" method="post">

		<?php 
    do_action('edd_purchase_link_top', $download->ID, $args);
    ?>

		<div class="edd_purchase_submit_wrapper">
			<?php 
    $class = implode(' ', array($args['style'], $args['color'], trim($args['class'])));
    if (!edd_is_ajax_disabled()) {
        echo '<a href="#" class="edd-add-to-cart ' . esc_attr($class) . '" data-action="edd_add_to_cart" data-download-id="' . esc_attr($download->ID) . '" ' . $data_variable . ' ' . $type . ' ' . $data_price . ' ' . $button_display . '><span class="edd-add-to-cart-label">' . $args['text'] . '</span> <span class="edd-loading"><i class="edd-icon-spinner edd-icon-spin"></i></span></a>';
    }
    echo '<input type="submit" class="edd-add-to-cart edd-no-js ' . esc_attr($class) . '" name="edd_purchase_download" value="' . esc_attr($args['text']) . '" data-action="edd_add_to_cart" data-download-id="' . esc_attr($download->ID) . '" ' . $data_variable . ' ' . $type . ' ' . $button_display . '/>';
    echo '<a href="' . esc_url(edd_get_checkout_uri()) . '" class="edd_go_to_checkout ' . esc_attr($class) . '" ' . $checkout_display . '>' . __('Checkout', 'easy-digital-downloads') . '</a>';
    ?>

			<?php 
    if (!edd_is_ajax_disabled()) {
        ?>
				<span class="edd-cart-ajax-alert">
					<span class="edd-cart-added-alert" style="display: none;">
						<?php 
        echo '<i class="edd-icon-ok"></i> ' . __('Added to cart', 'easy-digital-downloads');
        ?>
					</span>
				</span>
			<?php 
    }
    ?>
			<?php 
    if (!$download->is_free($args['price_id'])) {
        ?>
				<?php 
        if (edd_display_tax_rate() && edd_prices_include_tax()) {
            echo '<span class="edd_purchase_tax_rate">' . sprintf(__('Includes %1$s&#37; tax', 'easy-digital-downloads'), edd_get_tax_rate() * 100) . '</span>';
        } elseif (edd_display_tax_rate() && !edd_prices_include_tax()) {
            echo '<span class="edd_purchase_tax_rate">' . sprintf(__('Excluding %1$s&#37; tax', 'easy-digital-downloads'), edd_get_tax_rate() * 100) . '</span>';
        }
        ?>
			<?php 
    }
    ?>
		</div><!--end .edd_purchase_submit_wrapper-->

		<input type="hidden" name="download_id" value="<?php 
    echo esc_attr($download->ID);
    ?>
">
		<?php 
    if ($variable_pricing && isset($price_id) && isset($prices[$price_id])) {
        ?>
			<input type="hidden" name="edd_options[price_id][]" id="edd_price_option_<?php 
        echo $download->ID;
        ?>
_1" class="edd_price_option_<?php 
        echo $download->ID;
        ?>
" value="<?php 
        echo $price_id;
        ?>
">
		<?php 
    }
    ?>
		<?php 
    if (!empty($args['direct']) && !$download->is_free($args['price_id'])) {
        ?>
			<input type="hidden" name="edd_action" class="edd_action_input" value="straight_to_gateway">
		<?php 
    } else {
        ?>
			<input type="hidden" name="edd_action" class="edd_action_input" value="add_to_cart">
		<?php 
    }
    ?>

		<?php 
    if (apply_filters('edd_download_redirect_to_checkout', edd_straight_to_checkout(), $download->ID, $args)) {
        ?>
			<input type="hidden" name="edd_redirect_to_checkout" id="edd_redirect_to_checkout" value="1">
		<?php 
    }
    ?>

		<?php 
    do_action('edd_purchase_link_end', $download->ID, $args);
    ?>

	</form><!--end #<?php 
    echo esc_attr($form_id);
    ?>
-->
<?php 
    $purchase_form = ob_get_clean();
    return apply_filters('edd_purchase_download_form', $purchase_form, $args);
}
function edd_helium_js_vars($vars)
{
    return array_merge($vars, array('EDD' => array('ajaxDisabled' => edd_is_ajax_disabled(), 'straightToCheckout' => edd_straight_to_checkout(), 'checkoutPage' => edd_get_checkout_uri())));
}