/**
 * Set the template to use, buffers the needed template parts
 * and resets post vars.
 *
 * @package WP Idea Stream
 * @subpackage core/template-loader
 *
 * @since 2.0.0
 *
 * @global $wp_query
 * @param  string $template name of the template to use
 * @uses   is_buddypress() to bail early if it's this plugin's territory
 * @uses   wp_idea_stream_get_idea_var() to get a globalized var
 * @uses   is_404() to check for a 404
 * @uses   get_query_template() to get a specific template
 * @uses   get_index_template() to get the index template
 * @uses   wp_idea_stream_set_idea_var() to set a globalized var
 * @uses   is_post_type_archive() to check if it's ideas post type archive
 * @uses   wp_idea_stream_get_post_type() to get ideas post type identifier
 * @uses   set_query_var() to get a query var
 * @uses   remove_all_filters() to remove all filters on a specific hook
 * @uses   wp_idea_stream_reset_post() to reset WordPress $post global and avoid notices
 * @uses   wp_idea_stream_reset_post_title() to reset the title depending on the context
 * @uses   wp_idea_stream_buffer_template_part() to buffer the content to display
 * @uses   wp_idea_stream_is_edit() to check if the idea is to be edited
 * @uses   wp_idea_stream_ideas_lock_idea() to check if the idea to edit is not currently edited by another user
 * @uses   wp_idea_stream_add_message() to give a user some feedback
 * @uses   wp_idea_stream_ideas_can_edit() to check current user can edit an idea
 * @uses   wp_safe_redirect() to safely redirect the user
 * @uses   wp_idea_stream_get_redirect_url() to get the default redirect url
 * @uses   wp_idea_stream_buffer_single_idea() to buffer the idea content to display
 * @uses   do_action() Calls 'wp_idea_stream_set_core_template' to perform actions once a core template is set
 *                     Calls 'wp_idea_stream_set_single_template' to perform actions relative to the single idea template
 *                     Calls 'wp_idea_stream_set_template' to perform actions when no template matched
 * @uses   apply_filters() Calls 'wp_idea_stream_template_args' to override template args in case of custom idea action
 *                         Calls 'wp_idea_stream_single_template_args' to override single template args
 * @return string $template.
 */
function wp_idea_stream_set_template($template = '')
{
    global $wp_query;
    /**
     * Bail if BuddyPress, we'll use its theme compatibility
     * feature.
     */
    if (function_exists('is_buddypress') && is_buddypress()) {
        return $template;
    }
    if (wp_idea_stream_get_idea_var('is_ideastream') && !is_404()) {
        // Try to see if the theme has a specific template for WP Idea Stream
        $template = get_query_template('ideastream');
        if (empty($template)) {
            // else Try the page template
            $template = get_query_template('page', array('page.php'));
        }
        if (empty($template)) {
            // finally fall back to the index template
            $template = get_index_template();
        }
        // Define it into plugin's vars
        wp_idea_stream_set_idea_var('template_file', $template);
        /**
         * First get results of the main query if not on a single idea.
         * and build plugin's main_query var.
         */
        if (!wp_idea_stream_is_single_idea()) {
            wp_idea_stream_set_idea_var('main_query', array('ideas' => $wp_query->posts, 'total' => $wp_query->found_posts, 'query_vars' => array('author' => $wp_query->query_vars['author'], 'per_page' => $wp_query->query_vars['posts_per_page'], 'page' => !empty($wp_query->query_vars['paged']) ? $wp_query->query_vars['paged'] : 1, 'search' => $wp_query->query_vars['s'], 'exclude' => $wp_query->query_vars['post__not_in'], 'include' => $wp_query->query_vars['post__in'], 'orderby' => !empty($wp_query->query_vars['orderby']) ? $wp_query->query_vars['orderby'] : 'date', 'order' => $wp_query->query_vars['order'], 'meta_query' => $wp_query->meta_query->queries, 'tax_query' => $wp_query->tax_query->queries)));
            // Resetting the 's' query var now we got main query's result.
            set_query_var('s', '');
            // Init template args
            $template_args = array('post_title' => '', 'comment_status' => 'closed', 'is_archive' => true, 'is_tax' => false, 'template_slug' => 'archive', 'template_name' => '', 'context' => '');
            // Main plugin's archive page
            if (is_post_type_archive(wp_idea_stream_get_post_type())) {
                $template_args['context'] = 'archive';
            }
            // Category / tag archive pages
            if (wp_idea_stream_get_idea_var('is_category') || wp_idea_stream_get_idea_var('is_tag')) {
                $template_args['is_tax'] = true;
                $template_args['context'] = 'taxonomy';
            }
            // User's profile pages
            if (wp_idea_stream_get_idea_var('is_user')) {
                $template_args['template_slug'] = 'user';
                $template_args['template_name'] = 'profile';
                $template_args['context'] = 'user-profile';
            }
            if (wp_idea_stream_get_idea_var('is_action')) {
                $template_args['is_archive'] = false;
                // New idea form
                if (wp_idea_stream_is_addnew()) {
                    $template_args['template_slug'] = 'idea';
                    $template_args['template_name'] = 'form';
                    $template_args['context'] = 'new-idea';
                } else {
                    if (wp_idea_stream_is_signup()) {
                        $template_args['template_slug'] = 'signup';
                        $template_args['context'] = 'signup';
                        // Allow plugins to add custom action
                    } else {
                        if (has_filter('wp_idea_stream_template_args')) {
                            /**
                             * Custom action ?
                             *
                             * @param array $template_args the template arguments used to reset the post
                             */
                            $template_args = apply_filters('wp_idea_stream_template_args', $template_args);
                        }
                    }
                }
            }
            // Reset WordPress $post global.
            wp_idea_stream_reset_post(array('ID' => 0, 'post_title' => wp_idea_stream_reset_post_title($template_args['context']), 'post_author' => 0, 'post_date' => 0, 'post_type' => 'ideas', 'post_status' => 'publish', 'is_archive' => $template_args['is_archive'], 'comment_status' => $template_args['comment_status'], 'post_password' => false, 'is_tax' => $template_args['is_tax']));
            /**
             * Internally used to redirect to BuddyPress member's profile
             * if needed
             *
             * @param  string $context to help choosing the best template to use
             */
            do_action('wp_idea_stream_set_core_template', $template_args['context'], $template_args);
        } else {
            $query_loop = new stdClass();
            $query_loop->idea = $wp_query->post;
            // Should we use a custom template for single ideas ?
            $specific_single_template = get_query_template('single-ideastream');
            if (!empty($specific_single_template)) {
                $template = $specific_single_template;
            }
            // Populate the global query loop with current idea
            wp_idea_stream_set_idea_var('query_loop', $query_loop);
            // Add the id to globals
            wp_idea_stream_set_idea_var('single_idea_id', $wp_query->post->ID);
            // Are we editing an idea ?
            if (wp_idea_stream_is_edit()) {
                // Check if the idea is currently being edited by someone else
                $user_is_editing = wp_idea_stream_ideas_lock_idea($query_loop->idea->ID);
                if (!empty($user_is_editing)) {
                    wp_idea_stream_add_message(array('type' => 'info', 'content' => sprintf(__('The idea: "%s" is already being edited by another user.', 'wp-idea-stream'), $query_loop->idea->post_title)));
                    // Redirect the user
                    wp_safe_redirect(wp_idea_stream_get_redirect_url());
                    exit;
                }
                // Bail if user can't edit the idea
                if (!wp_idea_stream_ideas_can_edit($query_loop->idea)) {
                    wp_idea_stream_add_message(array('type' => 'error', 'content' => __('You are not allowed to edit this idea.', 'wp-idea-stream')));
                    // Redirect the user
                    wp_safe_redirect(wp_idea_stream_get_redirect_url());
                    exit;
                }
                // Inform the idea is to display in an edit form
                $query_loop->idea->is_edit = true;
                $template_args = array('template_slug' => 'idea', 'template_name' => 'form', 'context' => 'edit-idea');
                $single_args = array('ID' => 0, 'post_title' => wp_idea_stream_reset_post_title($template_args['context']), 'post_author' => 0, 'post_date' => 0, 'post_type' => 'ideas', 'post_status' => 'publish', 'is_archive' => false, 'comment_status' => false, 'post_password' => false);
                // Or simply viewing one ?
            } else {
                $template_args = array('context' => 'single-idea');
                $single_args = array('is_single' => true);
            }
            /**
             * @param array $single_args the single arguments used to reset the post
             */
            wp_idea_stream_reset_post(apply_filters('wp_idea_stream_single_template_args', $single_args));
            /**
             * Internally used to redirect to Buddypress Group's
             * single idea template if needed
             *
             * @param  WP_Post $query_loop->idea the idea to display
             */
            do_action('wp_idea_stream_set_single_template', $query_loop->idea, $template_args);
        }
    }
    /**
     * No IdeaStream template matched
     */
    do_action('wp_idea_stream_set_template');
    return $template;
}
Exemple #2
0
 /**
  * Map IdeaStream needed vars to the group's context and prepare the
  * group's extension display method
  *
  * @package WP Idea Stream
  * @subpackage buddypress/groups
  *
  * @since  2.0.0
  *
  * @uses bp_is_group() to check a group is displayed
  * @uses bp_is_current_action() to check the group's current action
  * @uses wp_idea_stream_root_slug() to get the IdeaStream root slug
  * @uses WP_Idea_Stream_Group::group_get_option() to check for the group setting
  * @uses bp_get_current_group_id() to get current group's ID
  * @uses bp_core_redirect() to safely redirect the user
  * @uses bp_get_group_permalink() to get the group's permalink
  * @uses groups_get_current_group() to get the current group's object
  * @uses wp_idea_stream_buddypress_set_is_ideastream() to set a new IdeaStream territory for a later use
  * @uses bp_action_variables() to get all action variables at once
  * @uses wp_idea_stream_action_get_slug() to get IdeaStream's action slug
  * @uses wp_idea_stream_addnew_slug() to get IdeaStream's add slug
  * @uses wp_idea_stream_buddypress_set_is_new() to set IdeaStream global 'is_new' for a later use
  * @uses add_action() to add a field to the new idea form
  * @uses wp_idea_stream_edit_slug() to get the edit slug
  * @uses get_query_var() to get the value of a specific query var
  * @uses wp_idea_stream_get_post_type() to get the ideas post type identifier
  * @uses wp_idea_stream_ideas_get_idea_by_name() to get the idea object
  * @uses wp_idea_stream_ideas_lock_idea() to check if the idea is edited by another user
  * @uses wp_idea_stream_ideas_can_edit() to check if the user can edit the idea
  * @uses WP_Idea_Stream_Group->is_idea_attached_to_group() to check if the idea is attached to currrent group
  * @uses wp_idea_stream_set_idea_var() to set an IdeaStream global for a later use
  * @uses wp_idea_stream_buddypress_set_is_edit() to set IdeaStream global 'is_edit' for a later use
  * @uses wp_idea_stream_idea_get_slug() to get IdeaStream's idea slug
  * @uses wp_idea_stream_tag_get_slug() to get the ideas tag taxonomy slug
  * @uses wp_idea_stream_category_get_slug() to get the ideas category taxonomy slug
  * @uses set_query_var() to set some query var for a later use
  * @uses get_term_by() to get idea's term
  * @uses wp_idea_stream_paged_slug() to get the ideas paged slug
  * @uses wp_idea_stream_add_message() to add a feedback to display to the user once redirected
  * @uses WP_Idea_Stream_Group->group_ideas_archive_url() to get the group's IdeaStream archive page
  * @uses bp_is_current_component() to check for a BuddyPress component
  * @uses bp_current_item() to make sure a group item is requested
  * @uses bp_do_404() to set the WP Query to a 404.
  */
 public function maybe_set_ideastream()
 {
     if (bp_is_group() && bp_is_current_action(wp_idea_stream_root_slug())) {
         // Bail if group is not (more) using IdeaStream
         if (!self::group_get_option(bp_get_current_group_id(), '_group_ideastream_activate', false)) {
             bp_core_redirect(bp_get_group_permalink(groups_get_current_group()));
         }
         // Set is_ideastream to load main css file
         wp_idea_stream_buddypress_set_is_ideastream();
         $actions = array_map('sanitize_title', (array) bp_action_variables());
         $message = false;
         switch ($actions[0]) {
             // Adding a new idea
             case wp_idea_stream_action_get_slug():
                 if (wp_idea_stream_addnew_slug() == $actions[1]) {
                     $this->group_ideastream->is_action = 'new';
                     $this->group_ideastream->context = 'new-idea';
                     // Set is_new to load javascripts
                     wp_idea_stream_buddypress_set_is_new();
                     // Add the group_id field in the form
                     add_action('wp_idea_stream_ideas_the_idea_meta_edit', array($this, 'meta_group_id'));
                 } else {
                     if (wp_idea_stream_edit_slug() == $actions[1]) {
                         $idea_name = get_query_var(wp_idea_stream_get_post_type());
                         if (empty($idea_name)) {
                             $message = __('No idea was requested', 'wp-idea-stream');
                         }
                         // Get the idea thanks to its name
                         $idea = wp_idea_stream_ideas_get_idea_by_name($idea_name);
                         // Check if the idea is currently being edited by someone else
                         $user_is_editing = wp_idea_stream_ideas_lock_idea($idea->ID);
                         if (!empty($user_is_editing)) {
                             $message = sprintf(__('The idea: "%s" is already being edited by another user.', 'wp-idea-stream'), $idea->post_title);
                             break;
                         }
                         // Does the user can edit the idea ?
                         if (!wp_idea_stream_ideas_can_edit($idea)) {
                             $message = __('You are not allowed to edit this idea.', 'wp-idea-stream');
                             break;
                         }
                         if ($this->is_idea_attached_to_group($idea)) {
                             $this->group_ideastream->is_action = 'edit';
                             $this->group_ideastream->context = 'edit-idea';
                             // Set the query loop
                             $query_loop = new StdClass();
                             $query_loop->idea = $idea;
                             wp_idea_stream_set_idea_var('query_loop', $query_loop);
                             wp_idea_stream_set_idea_var('single_idea_id', $idea->ID);
                             // Set is_new to load javascripts
                             wp_idea_stream_buddypress_set_is_edit();
                             // Add the group_id field in the form
                             add_action('wp_idea_stream_ideas_the_idea_meta_edit', array($this, 'meta_group_id'));
                         } else {
                             $message = __('The idea was not found in this group.', 'wp-idea-stream');
                         }
                     } else {
                         $message = __('The action requested is not available', 'wp-idea-stream');
                     }
                 }
                 break;
                 // Viewing a single idea
             // Viewing a single idea
             case wp_idea_stream_idea_get_slug():
                 // No name, stop
                 if (empty($actions[1])) {
                     $message = __('No idea was requested', 'wp-idea-stream');
                     break;
                 }
                 // Get the idea thanks to its name
                 $idea = wp_idea_stream_ideas_get_idea_by_name($actions[1]);
                 if ($this->is_idea_attached_to_group($idea)) {
                     $this->group_ideastream->is_action = 'idea';
                     $this->group_ideastream->idea_name = $actions[1];
                     // Set the query loop
                     $query_loop = new StdClass();
                     $query_loop->idea = $idea;
                     wp_idea_stream_set_idea_var('query_loop', $query_loop);
                     wp_idea_stream_set_idea_var('single_idea_id', $idea->ID);
                 } else {
                     $message = __('The idea was not found in this group.', 'wp-idea-stream');
                 }
                 break;
             case wp_idea_stream_tag_get_slug():
             case wp_idea_stream_category_get_slug():
                 // No term name, stop
                 if (empty($actions[1])) {
                     $message = sprintf(__('No %s was requested', 'wp-idea-stream'), $actions[0]);
                     break;
                 }
                 // Does the group support categories ?
                 if ($actions[0] == wp_idea_stream_category_get_slug() && !self::group_get_option(bp_get_current_group_id(), '_group_ideastream_categories', true)) {
                     $message = sprintf(__('This group does not support the %s feature.', 'wp-idea-stream'), $actions[0]);
                     break;
                 }
                 // Using tag as default, as category can be disabled from group settings.
                 if ($actions[0] == wp_idea_stream_tag_get_slug()) {
                     $this->group_ideastream->current_taxonomy = wp_idea_stream_get_tag();
                     // Set tag as a query var.
                     set_query_var(wp_idea_stream_get_tag(), $actions[1]);
                 } else {
                     if ($actions[0] == wp_idea_stream_category_get_slug()) {
                         $this->group_ideastream->current_taxonomy = wp_idea_stream_get_category();
                         // Set category as a query var.
                         set_query_var(wp_idea_stream_get_category(), $actions[1]);
                     }
                 }
                 // Try to get the term with its slug
                 $this->group_ideastream->current_term = get_term_by('slug', $actions[1], $this->group_ideastream->current_taxonomy);
                 if (!empty($this->group_ideastream->current_term)) {
                     $this->group_ideastream->is_action = $actions[0];
                     $this->group_ideastream->context = 'taxonomy';
                     // Set the current term
                     wp_idea_stream_set_idea_var('current_term', $this->group_ideastream->current_term);
                 } else {
                     $message = sprintf(__('The %s was not found', 'wp-idea-stream'), $actions[0]);
                     break;
                 }
                 break;
             default:
                 $this->group_ideastream->is_action = 'archive';
                 $this->group_ideastream->context = 'archive';
                 break;
         }
         // Set pagination for taxonomy & archive page
         if (!empty($this->group_ideastream->context) && in_array($this->group_ideastream->context, array('taxonomy', 'archive'))) {
             $possible_page_number = array($actions[0]);
             if (!empty($actions[2])) {
                 $possible_page_number = array_merge($possible_page_number, array($actions[2]));
             }
             if (in_array(wp_idea_stream_paged_slug(), $possible_page_number)) {
                 if (is_numeric($actions[1])) {
                     $this->group_ideastream->is_paged = absint($actions[1]);
                 } else {
                     if (is_numeric($actions[3])) {
                         $this->group_ideastream->is_paged = absint($actions[3]);
                     } else {
                         $this->group_ideastream->is_paged = 0;
                     }
                 }
             }
         }
         if (!empty($message)) {
             wp_idea_stream_add_message(array('type' => 'error', 'content' => $message));
             bp_core_redirect($this->group_ideas_archive_url(groups_get_current_group(), true));
         }
         /**
          * Redirect to a 404 if needed
          *
          * It's the case when trying to see an idea attached to an hidden group while the user
          * is not a member of this group.
          */
     } else {
         if (bp_is_current_component('groups') && bp_is_current_action(wp_idea_stream_root_slug()) && bp_current_item()) {
             bp_do_404();
             return;
         }
     }
 }
/**
 * Handles updating an idea
 *
 * @package WP Idea Stream
 * @subpackage ideas/functions
 *
 * @since 2.0.0
 *
 * @uses   check_admin_referer() to check the request has been done from current site
 * @uses   wp_idea_stream_get_redirect_url() to get default redirect url
 * @uses   get_query_var() to get the value of a specific query var
 * @uses   wp_idea_stream_get_post_type() to get the ideas post type identifier
 * @uses   get_queried_object() to try to get the idea object WordPress built
 * @uses   wp_idea_stream_ideas_get_idea_by_name() to get an idea object out of its post name
 * @uses   wp_idea_stream_user_can() to check user's capability
 * @uses   wp_idea_stream_add_message() to add a feddback message to user
 * @uses   wp_safe_redirect() to safely redirect the user and avoid duplicates
 * @uses   wp_idea_stream_ideas_save_idea() to save the idea
 * @uses   wp_idea_stream_get_form_url() to get the add new form url
 * @uses   wp_idea_stream_ideas_get_idea_permalink() to get the idea link
 */
function wp_idea_stream_ideas_update_idea()
{
    global $wp_query;
    // Bail if not a post request
    if ('POST' != strtoupper($_SERVER['REQUEST_METHOD'])) {
        return;
    }
    // Bail if not a post idea request
    if (empty($_POST['wp_idea_stream']) || !is_array($_POST['wp_idea_stream'])) {
        return;
    }
    // Bail if it's not an update
    if (empty($_POST['wp_idea_stream']['_the_id'])) {
        return;
    }
    // Check nonce
    check_admin_referer('wp_idea_stream_save');
    $redirect = wp_idea_stream_get_redirect_url();
    // Get idea name
    $idea_name = get_query_var(wp_idea_stream_get_post_type());
    // Get Idea Object
    $idea = get_queried_object();
    // If queried object doesn't match or wasn't helpfull, try to get the idea using core function
    if (empty($idea->post_name) || empty($idea_name) || $idea_name != $idea->post_name) {
        $idea = wp_idea_stream_ideas_get_idea_by_name($idea_name);
    }
    // Found no idea, redirect and inform the user
    if (empty($idea->ID)) {
        wp_idea_stream_add_message(array('type' => 'error', 'content' => __('The idea you are trying to edit does not seem to exist.', 'wp-idea-stream')));
        // Redirect to main archive page
        wp_safe_redirect($redirect);
        exit;
    }
    // Checks if the user can edit the idea
    if (!wp_idea_stream_ideas_can_edit($idea)) {
        // Add feedback to the user
        wp_idea_stream_add_message(array('type' => 'error', 'content' => __('You are not allowed to edit this idea.', 'wp-idea-stream')));
        // Redirect to main archive page
        wp_safe_redirect($redirect);
        exit;
    }
    $updated = array_diff_key($_POST['wp_idea_stream'], array('save' => 'submit'));
    // Title & content are required
    if (empty($updated['_the_title']) || empty($updated['_the_content'])) {
        // Add feedback to the user
        wp_idea_stream_add_message(array('type' => 'error', 'content' => __('Title and description are required fields.', 'wp-idea-stream')));
        // Simply stop, so that the user keeps the posted values.
        return;
    }
    // Reset '_the_id' param to the ID of the idea found
    $updated['_the_id'] = $idea->ID;
    $feedback_message = array();
    $featured_error = __('There was a problem saving the featured image, sorry.', 'wp-idea-stream');
    $featured_type = 'info';
    // Take care of the featured image
    $thumbnail_id = (int) get_post_thumbnail_id($idea);
    if (!empty($updated['_the_thumbnail'])) {
        $thumbnail_src = key($updated['_the_thumbnail']);
        $thumbnail = reset($updated['_the_thumbnail']);
        // Update the Featured image
        if (!is_numeric($thumbnail) || $thumbnail_id !== (int) $thumbnail) {
            if (is_numeric($thumbnail)) {
                // validate the attachment
                if (!get_post($thumbnail)) {
                    $feedback_message[] = $featured_error;
                    // Set the new Featured image
                } else {
                    set_post_thumbnail($idea->ID, $thumbnail);
                }
            } else {
                $sideload = WP_Idea_Stream_Ideas_Thumbnail::start($thumbnail_src, $idea->ID);
                if (is_wp_error($sideload->result)) {
                    $feedback_message[] = $featured_error;
                }
            }
        }
        // Delete the featured image
    } elseif (!empty($thumbnail_id)) {
        delete_post_thumbnail($idea);
    }
    // Update the idea
    $id = wp_idea_stream_ideas_save_idea($updated);
    if (empty($id)) {
        // Set the feedback for the user
        $featured_type = 'error';
        $feedback_message = __('Something went wrong while trying to update your idea.', 'wp-idea-stream');
        // Redirect to the form
        $redirect = wp_idea_stream_get_form_url(wp_idea_stream_edit_slug(), $idea_name);
        // Redirect to the idea
    } else {
        $redirect = wp_idea_stream_ideas_get_idea_permalink($id);
    }
    if (!empty($feedback_message)) {
        // Add feedback to the user
        wp_idea_stream_add_message(array('type' => $featured_type, 'content' => join(' ', $feedback_message)));
    }
    wp_safe_redirect($redirect);
    exit;
}
/**
 * Signup a new user
 *
 * @package WP Idea Stream
 * @subpackage users/functions
 *
 * @since 2.1.0
 *
 * @param bool $exit whether to exit or not
 * @uses check_admin_referer()
 * @uses wp_idea_stream_get_redirect_url()
 * @uses wp_idea_stream_add_message()
 * @uses WP_Error()
 * @uses register_new_user()
 * @uses wp_update_user()
 * @uses wp_safe_redirect();
 * @uses apply_filters() Calls 'wp_idea_stream_users_is_signup_field_required' to force a contact method to be required
 *                       Calls 'wp_idea_stream_users_signup_userdata' to override the user data to update
 * @uses do_action() Calls 'wp_idea_stream_users_before_signup_field_required' to perform actions before required fields are checked
 *                   Calls 'wp_idea_stream_users_before_signup_user' to perform actions before signup is registered
 *                   Calls 'wp_idea_stream_users_after_signup_user' to perform actions after signup is registered
 *                   Calls 'wp_idea_stream_users_signup_user_created' to perform actions once the user created has been edited
 */
function wp_idea_stream_users_signup_user($exit = true)
{
    // Bail if not a post request
    if ('POST' != strtoupper($_SERVER['REQUEST_METHOD'])) {
        return;
    }
    // Bail if not a post idea request
    if (empty($_POST['wp_idea_stream_signup']) || !is_array($_POST['wp_idea_stream_signup'])) {
        return;
    }
    // Check nonce
    check_admin_referer('wp_idea_stream_signup');
    $redirect = wp_idea_stream_get_redirect_url();
    $is_multisite = is_multisite();
    /**
     * Before registering the user, check for required field
     */
    $required_errors = new WP_Error();
    $user_login = false;
    if (!empty($_POST['wp_idea_stream_signup']['user_login'])) {
        $user_login = $_POST['wp_idea_stream_signup']['user_login'];
    }
    // Force the login to exist and to be at least 4 characters long
    if (4 > mb_strlen($user_login)) {
        $required_errors->add('user_login_fourchars', __('Please choose a login having at least 4 characters.', 'wp-idea-stream'));
    }
    $user_email = false;
    if (!empty($_POST['wp_idea_stream_signup']['user_email'])) {
        $user_email = $_POST['wp_idea_stream_signup']['user_email'];
    }
    // Do we need to edit the user once created ?
    $edit_user = array_diff_key($_POST['wp_idea_stream_signup'], array('signup' => 'signup', 'user_login' => 'user_login', 'user_email' => 'user_email'));
    /**
     * Perform actions before the required fields check
     *
     * @param  string $user_login the user login
     * @param  string $user_email the user email
     * @param  array  $edit_user  all extra user fields
     */
    do_action('wp_idea_stream_users_before_signup_field_required', $user_login, $user_email, $edit_user);
    foreach ($edit_user as $key => $value) {
        if (!apply_filters('wp_idea_stream_users_is_signup_field_required', false, $key)) {
            continue;
        }
        if (empty($value) && 'empty_required_field' != $required_errors->get_error_code()) {
            $required_errors->add('empty_required_field', __('Please fill all required fields.', 'wp-idea-stream'));
        }
    }
    // Stop the process and ask to fill all fields.
    if ($required_errors->get_error_code()) {
        //Add feedback to the user
        wp_idea_stream_add_message(array('type' => 'error', 'content' => join(' ', array_map('strip_tags', $required_errors->get_error_messages()))));
        return;
    }
    /**
     * Perform actions before the user is created
     *
     * @param  string $user_login the user login
     * @param  string $user_email the user email
     * @param  array  $edit_user  all extra user fields
     */
    do_action('wp_idea_stream_users_before_signup_user', $user_login, $user_email, $edit_user);
    // Defaults to user name and user email
    $signup_array = array('user_name' => $user_login, 'user_email' => $user_email);
    // Sanitize the signup on multisite configs.
    if (true === (bool) $is_multisite) {
        $signup_array = wpmu_validate_user_signup($user_login, $user_email);
        if (is_wp_error($signup_array['errors']) && $signup_array['errors']->get_error_code()) {
            //Add feedback to the user
            wp_idea_stream_add_message(array('type' => 'error', 'content' => join(' ', array_map('strip_tags', $signup_array['errors']->get_error_messages()))));
            return;
        }
        // Filter the rp login url for WordPress 4.3
        add_filter('wp_mail', 'wp_idea_stream_multisite_user_notification', 10, 1);
    }
    // Register the user
    $user = register_new_user($signup_array['user_name'], $signup_array['user_email']);
    // Stop filtering the rp login url
    if (true === (bool) $is_multisite) {
        remove_filter('wp_mail', 'wp_idea_stream_multisite_user_notification', 10, 1);
    }
    /**
     * Perform actions after the user is created
     *
     * @param  string             $user_login the user login
     * @param  string             $user_email the user email
     * @param  array              $edit_user  all extra user fields
     * @param  mixed int|WP_Error $user the user id or an error
     */
    do_action('wp_idea_stream_users_after_signup_user', $user_login, $user_email, $edit_user, $user);
    if (is_wp_error($user)) {
        //Add feedback to the user
        wp_idea_stream_add_message(array('type' => 'error', 'content' => join(' ', array_map('strip_tags', $user->get_error_messages()))));
        return;
        // User is created, now we need to eventually edit him
    } else {
        if (!empty($edit_user)) {
            $userdata = new stdClass();
            $userdata = (object) $edit_user;
            $userdata->ID = $user;
            /**
             * Just before the user is updated, this will only be available
             * if custom fields/contact methods are used.
             *
             * @param object $userdata the userdata to update
             */
            $userdata = apply_filters('wp_idea_stream_users_signup_userdata', $userdata);
            // Edit the user
            if (wp_update_user($userdata)) {
                /**
                 * Any extra field not using contact methods or WordPress built in user fields can hook here
                 *
                 * @param int $user the user id
                 * @param array $edit_user the submitted user fields
                 */
                do_action('wp_idea_stream_users_signup_user_created', $user, $edit_user);
            }
        }
        // Make sure an entry is added to the $wpdb->signups table
        if (true === (bool) $is_multisite) {
            wp_idea_stream_users_update_signups_table($user);
        }
        // Finally invite the user to check his email.
        wp_idea_stream_add_message(array('type' => 'success', 'content' => __('Registration complete. Please check your e-mail.', 'wp-idea-stream')));
        wp_safe_redirect($redirect);
        if ($exit) {
            exit;
        }
    }
}