/**
 * Process registration submission.
 *
 * @since Astoundify Crowdfunding 1.0
 *
 * @return void
 */
function atcf_registration_handle()
{
    global $edd_options;
    if ('POST' !== strtoupper($_SERVER['REQUEST_METHOD'])) {
        return;
    }
    if (empty($_POST['action']) || 'atcf-register-submit' !== $_POST['action']) {
        return;
    }
    if (!wp_verify_nonce($_POST['_wpnonce'], 'atcf-register-submit')) {
        return;
    }
    $errors = new WP_Error();
    $nicename = isset($_POST['displayname']) ? esc_attr($_POST['displayname']) : null;
    $email = isset($_POST['user_email']) ? esc_attr($_POST['user_email']) : null;
    $username = isset($_POST['user_login']) ? esc_attr($_POST['user_login']) : null;
    $password = isset($_POST['user_pass']) ? esc_attr($_POST['user_pass']) : null;
    /** Check Email */
    if (empty($email) || !is_email($email)) {
        $errors->add('invalid-email', __('Please enter a valid email address.', 'atcf'));
    }
    if (email_exists($email)) {
        $errors->add('taken-email', __('That contact email address already exists.', 'atcf'));
    }
    /** Check Password */
    if (empty($password)) {
        $errors->add('invalid-password', __('Please choose a secure password.', 'atcf'));
    }
    /** Check Username */
    if (!empty($username) && username_exists($username)) {
        $errors->add('username-exists', __('Sorry, this username is already taken.', 'atcf'));
    }
    $errors = apply_filters('atcf_register_validate', $errors, $_POST);
    if (!empty($errors->errors)) {
        wp_die($errors);
    }
    if ('' == $username) {
        $username = $email;
    }
    if ('' == $nicename) {
        $nicename = $username;
    }
    $user_id = atcf_register_user(array('user_login' => $username, 'user_pass' => $password, 'user_email' => $email, 'display_name' => $nicename));
    if (!empty($user_id->errors)) {
        wp_die($user_id);
    }
    do_action('atcf_register_process_after', $user_id, $_POST);
    $redirect = apply_filters('atcf_register_redirect', isset($edd_options['profile_page']) ? get_permalink($edd_options['profile_page']) : home_url());
    if ($user_id) {
        wp_safe_redirect($redirect);
        exit;
    } else {
        wp_safe_redirect(home_url());
        exit;
    }
}
Пример #2
0
/**
 * Process shortcode submission.
 *
 * @since Astoundify Crowdfunding 0.1-alpha
 *
 * @param $key The key of the current field.
 * @param $field The array of field arguments.
 * @param $atts The shortcoe attribtues.
 * @param $campaign The current campaign (if editing/previewing).
 * @return void
 */
function atcf_shortcode_submit_process()
{
    global $edd_options, $post;
    if ('POST' !== strtoupper($_SERVER['REQUEST_METHOD'])) {
        return;
    }
    if (empty($_POST['action']) || 'atcf-campaign-submit' !== $_POST['action']) {
        return;
    }
    if (!wp_verify_nonce($_POST['_wpnonce'], 'atcf-campaign-submit')) {
        return;
    }
    $action = esc_attr($_POST['submit']);
    $existing_campaign = isset($_POST['campaign_id']) ? esc_attr($_POST['campaign_id']) : null;
    $fields = atcf_shortcode_submit_fields();
    $status = 'submit' == $action ? 'pending' : 'draft';
    /** If we are submitting, but this is a live campaign, keep published */
    if ($existing_campaign && ('pending' == $status && get_post($existing_campaign)->post_status == 'publish')) {
        $status = 'publish';
    }
    foreach ($fields as $key => $field) {
        $fields[$key]['value'] = isset($_POST[$key]) ? $_POST[$key] : null;
        $fields[$key]['value'] = apply_filters('atcf_shortcode_submit_validate_' . $key, $fields[$key]['value']);
        if (isset($field['required']) && true === $field['required'] && !$fields[$key]['value'] && 'publish' != $status) {
            edd_set_error('required-' . $key, sprintf(__('The <strong>%s</strong> field is required.', 'atcf'), $field['label']));
        }
    }
    do_action('atcf_campaign_submit_validate', $fields, $_POST);
    if (edd_get_errors()) {
        return;
    }
    /** Register a new user, or get the current user */
    $user = get_user_by('email', $fields['contact_email']['value']);
    if (!$user) {
        $user_id = atcf_register_user(array('user_login' => $fields['contact_email']['value'], 'user_email' => $fields['contact_email']['value'], 'display_name' => isset($fields['name']['value']) ? $fields['name']['value'] : $fields['contact_email']['value']));
    } else {
        $user_id = $user->ID;
    }
    /**
     * Create or update a campaign
     */
    $args = apply_filters('atcf_campaign_submit_data', array('post_type' => 'download', 'post_status' => $status, 'post_content' => $fields['description']['value'], 'post_author' => $user_id), $_POST);
    if ($fields['title']['value']) {
        $args['post_title'] = $fields['title']['value'];
    }
    if ($fields['excerpt']['value']) {
        $args['post_excerpt'] = $fields['excerpt']['value'];
    }
    if (!$existing_campaign) {
        $campaign = wp_insert_post($args, true);
    } else {
        $args['ID'] = $existing_campaign;
        $campaign = wp_update_post($args);
    }
    do_action('atcf_submit_process_after', $campaign, $_POST, $status, $fields);
    if ('publish' == $status) {
        wp_safe_redirect(add_query_arg('updated', 'true', get_permalink($campaign)));
        exit;
    } elseif ('submit' == $action) {
        $url = isset($edd_options['submit_success_page']) ? get_permalink($edd_options['submit_success_page']) : home_url();
        $redirect = apply_filters('atcf_submit_campaign_success_redirect', $url);
        wp_safe_redirect(add_query_arg(array('success' => true, 'campaign' => $campaign), $redirect));
        exit;
    } else {
        wp_safe_redirect(add_query_arg('preview', 'true', get_permalink($campaign)));
        exit;
    }
}