Example #1
0
/**
 * Get Purchase Form User
 *
 * @param array $valid_data
 *
 * @access  private
 * @since   1.0
 * @return  array
 */
function give_get_purchase_form_user($valid_data = array())
{
    // Initialize user
    $user = false;
    $is_ajax = defined('DOING_AJAX') && DOING_AJAX;
    if ($is_ajax) {
        // Do not create or login the user during the ajax submission (check for errors only)
        return true;
    } else {
        if (is_user_logged_in()) {
            // Set the valid user as the logged in collected data
            $user = $valid_data['logged_in_user'];
        } else {
            if ($valid_data['need_new_user'] === true || $valid_data['need_user_login'] === true) {
                // New user registration
                if ($valid_data['need_new_user'] === true) {
                    // Set user
                    $user = $valid_data['new_user_data'];
                    // Register and login new user
                    $user['user_id'] = give_register_and_login_new_user($user);
                    // User login
                } else {
                    if ($valid_data['need_user_login'] === true && !$is_ajax) {
                        /*
                         * The login form is now processed in the give_process_purchase_login() function.
                         * This is still here for backwards compatibility.
                         * This also allows the old login process to still work if a user removes the
                         * checkout login submit button.
                         *
                         * This also ensures that the donor is logged in correctly if they click "Purchase"
                         * instead of submitting the login form, meaning the donor is logged in during the purchase process.
                         */
                        // Set user
                        $user = $valid_data['login_user_data'];
                        // Login user
                        give_log_user_in($user['user_id'], $user['user_login'], $user['user_pass']);
                    }
                }
            }
        }
    }
    // Check guest checkout
    if (false === $user && false === give_logged_in_only($_POST['give-form-id'])) {
        // Set user
        $user = $valid_data['guest_user_data'];
    }
    // Verify we have an user
    if (false === $user || empty($user)) {
        // Return false
        return false;
    }
    // Get user first name
    if (!isset($user['user_first']) || strlen(trim($user['user_first'])) < 1) {
        $user['user_first'] = isset($_POST['give_first']) ? strip_tags(trim($_POST['give_first'])) : '';
    }
    // Get user last name
    if (!isset($user['user_last']) || strlen(trim($user['user_last'])) < 1) {
        $user['user_last'] = isset($_POST['give_last']) ? strip_tags(trim($_POST['give_last'])) : '';
    }
    // Get the user's billing address details
    $user['address'] = array();
    $user['address']['line1'] = !empty($_POST['card_address']) ? sanitize_text_field($_POST['card_address']) : false;
    $user['address']['line2'] = !empty($_POST['card_address_2']) ? sanitize_text_field($_POST['card_address_2']) : false;
    $user['address']['city'] = !empty($_POST['card_city']) ? sanitize_text_field($_POST['card_city']) : false;
    $user['address']['state'] = !empty($_POST['card_state']) ? sanitize_text_field($_POST['card_state']) : false;
    $user['address']['country'] = !empty($_POST['billing_country']) ? sanitize_text_field($_POST['billing_country']) : false;
    $user['address']['zip'] = !empty($_POST['card_zip']) ? sanitize_text_field($_POST['card_zip']) : false;
    if (empty($user['address']['country'])) {
        $user['address'] = false;
    }
    // Country will always be set if address fields are present
    if (!empty($user['user_id']) && $user['user_id'] > 0 && !empty($user['address'])) {
        // Store the address in the user's meta so the donation form can be pre-populated with it on return purchases
        update_user_meta($user['user_id'], '_give_user_address', $user['address']);
    }
    // Return valid user
    return $user;
}
Example #2
0
/**
 * Members-only Form
 *
 * If "Disable Guest Donations" and "Display Register / Login" is set to none
 *
 * @since  1.4.1
 *
 * @param  string $final_output
 * @param  array  $args
 *
 * @return string
 */
function give_members_only_form($final_output, $args)
{
    $form_id = isset($args['form_id']) ? $args['form_id'] : 0;
    //Sanity Check: Must have form_id & not be logged in
    if (empty($form_id) || is_user_logged_in()) {
        return $final_output;
    }
    //Logged in only and Register / Login set to none
    if (give_logged_in_only($form_id) && give_show_login_register_option($form_id) == 'none') {
        $final_output = give_output_error(esc_html__('Please log in in order to complete your donation.', 'give'), false);
        return apply_filters('give_members_only_output', $final_output, $form_id);
    }
    return $final_output;
}
Example #3
0
/**
 * Purchase Form Validate Guest User
 *
 * @access  private
 * @since   1.0
 * @return  array
 */
function give_purchase_form_validate_guest_user()
{
    // Start an array to collect valid user data
    $valid_user_data = array('user_id' => 0);
    // Show error message if user must be logged in
    if (give_logged_in_only()) {
        give_set_error('logged_in_only', __('You must be logged into an account to donation', 'give'));
    }
    // Get the guest email
    $guest_email = isset($_POST['give_email']) ? $_POST['give_email'] : false;
    // Check email
    if ($guest_email && strlen($guest_email) > 0) {
        // Validate email
        if (!is_email($guest_email)) {
            // Invalid email
            give_set_error('email_invalid', __('Invalid email', 'give'));
        } else {
            // All is good to go
            $valid_user_data['user_email'] = $guest_email;
        }
    } else {
        // No email
        give_set_error('email_empty', __('Enter an email', 'give'));
    }
    // Loop through required fields and show error messages
    foreach (give_purchase_form_required_fields() as $field_name => $value) {
        if (in_array($value, give_purchase_form_required_fields()) && empty($_POST[$field_name])) {
            give_set_error($value['error_id'], $value['error_message']);
        }
    }
    return $valid_user_data;
}
Example #4
0
/**
 * Gets the login fields for the login form on the checkout. This function hooks
 * on the give_purchase_form_login_fields to display the login form if a user already
 * had an account.
 *
 * @since 1.0
 *
 * @param int $form_id
 *
 * @return string
 */
function give_get_login_fields($form_id)
{
    global $give_options;
    $form_id = isset($_POST['form_id']) ? $_POST['form_id'] : $form_id;
    $show_register_form = apply_filters('give_show_register_form', get_post_meta($form_id, '_give_show_register_form', true));
    ob_start();
    ?>
	<fieldset id="give-login-fields-<?php 
    echo $form_id;
    ?>
">
		<legend><?php 
    echo apply_filters('give_account_login_fieldset_heading', __('Login to Your Account', 'give'));
    if (!give_logged_in_only($form_id)) {
        echo ' <span class="sub-text">' . __('(optional)', 'give') . '</span>';
    }
    ?>
		</legend>
		<?php 
    if ($show_register_form == 'both') {
        ?>
			<p class="give-new-account-link">
				<?php 
        _e('Need to create an account?', 'give');
        ?>
&nbsp;
				<a href="<?php 
        echo remove_query_arg('login');
        ?>
" class="give-checkout-register-login"
				   data-action="give_checkout_register">
					<?php 
        _e('Register', 'give');
        if (!give_logged_in_only($form_id)) {
            echo ' ' . __('or checkout as a guest.', 'give');
        }
        ?>
				</a>
			</p>
			<p class="give-loading-text">
				<span class="give-loading-animation"></span> <?php 
        _e('Loading...', 'give');
        ?>
 </p>
		<?php 
    }
    ?>
		<?php 
    do_action('give_checkout_login_fields_before', $form_id);
    ?>
		<p id="give-user-login-wrap-<?php 
    echo $form_id;
    ?>
" class="form-row form-row-first">
			<label class="give-label" for="give-user-login-<?php 
    echo $form_id;
    ?>
">
				<?php 
    _e('Username', 'give');
    ?>
				<?php 
    if (give_logged_in_only($form_id)) {
        ?>
					<span class="give-required-indicator">*</span>
				<?php 
    }
    ?>
			</label>

			<input class="<?php 
    if (give_logged_in_only($form_id)) {
        echo 'required ';
    }
    ?>
give-input" type="text" name="give_user_login" id="give-user-login-<?php 
    echo $form_id;
    ?>
" value="" placeholder="<?php 
    _e('Your username', 'give');
    ?>
"/>
		</p>

		<p id="give-user-pass-wrap-<?php 
    echo $form_id;
    ?>
" class="give_login_password form-row form-row-last">
			<label class="give-label" for="give-user-pass-<?php 
    echo $form_id;
    ?>
">
				<?php 
    _e('Password', 'give');
    ?>
				<?php 
    if (give_logged_in_only($form_id)) {
        ?>
					<span class="give-required-indicator">*</span>
				<?php 
    }
    ?>
			</label>
			<input class="<?php 
    if (give_logged_in_only($form_id)) {
        echo 'required ';
    }
    ?>
give-input" type="password" name="give_user_pass" id="give-user-pass-<?php 
    echo $form_id;
    ?>
" placeholder="<?php 
    _e('Your password', 'give');
    ?>
"/>
			<input type="hidden" name="give-purchase-var" value="needs-to-login"/>
		</p>

		<p id="give-user-login-submit-<?php 
    echo $form_id;
    ?>
" class="give-clearfix">
			<input type="submit" class="give-submit give-btn button" name="give_login_submit" value="<?php 
    _e('Login', 'give');
    ?>
"/>
			<?php 
    if ($show_register_form !== 'login') {
        ?>
				<input type="button" data-action="give_cancel_login" class="give-cancel-login give-btn button" name="give_login_cancel" value="<?php 
        _e('Cancel', 'give');
        ?>
"/>
			<?php 
    }
    ?>
			<span class="give-loading-animation"></span>
		</p>
		<?php 
    do_action('give_checkout_login_fields_after');
    ?>
	</fieldset><!--end #give-login-fields-->
	<?php 
    echo ob_get_clean();
}