/** * Activate a signup. * * Hook to 'wpmu_activate_user' or 'wpmu_activate_blog' for events * that should happen only when users or sites are self-created (since * those actions are not called when users and sites are created * by a Super Admin). * * @since MU * @uses wp_generate_password() * @uses wpmu_welcome_user_notification() * @uses add_user_to_blog() * @uses add_new_user_to_blog() * @uses wpmu_create_user() * @uses wpmu_create_blog() * @uses wpmu_welcome_notification() * * @param string $key The activation key provided to the user. * @return array An array containing information about the activated user and/or blog */ function wpmu_activate_signup($key) { global $wpdb, $current_site; $signup = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->signups} WHERE activation_key = %s", $key)); if (empty($signup)) { return new WP_Error('invalid_key', __('Invalid activation key.')); } if ($signup->active) { if (empty($signup->domain)) { return new WP_Error('already_active', __('The user is already active.'), $signup); } else { return new WP_Error('already_active', __('The site is already active.'), $signup); } } $meta = unserialize($signup->meta); $user_login = $wpdb->escape($signup->user_login); $user_email = $wpdb->escape($signup->user_email); $password = wp_generate_password(12, false); $user_id = username_exists($user_login); if (!$user_id) { $user_id = wpmu_create_user($user_login, $password, $user_email); } else { $user_already_exists = true; } if (!$user_id) { return new WP_Error('create_user', __('Could not create user'), $signup); } $now = current_time('mysql', true); if (empty($signup->domain)) { $wpdb->update($wpdb->signups, array('active' => 1, 'activated' => $now), array('activation_key' => $key)); if (isset($user_already_exists)) { return new WP_Error('user_already_exists', __('That username is already activated.'), $signup); } wpmu_welcome_user_notification($user_id, $password, $meta); add_new_user_to_blog($user_id, $user_email, $meta); do_action('wpmu_activate_user', $user_id, $password, $meta); return array('user_id' => $user_id, 'password' => $password, 'meta' => $meta); } $blog_id = wpmu_create_blog($signup->domain, $signup->path, $signup->title, $user_id, $meta, $wpdb->siteid); // TODO: What to do if we create a user but cannot create a blog? if (is_wp_error($blog_id)) { // If blog is taken, that means a previous attempt to activate this blog failed in between creating the blog and // setting the activation flag. Let's just set the active flag and instruct the user to reset their password. if ('blog_taken' == $blog_id->get_error_code()) { $blog_id->add_data($signup); $wpdb->update($wpdb->signups, array('active' => 1, 'activated' => $now), array('activation_key' => $key)); } return $blog_id; } $wpdb->update($wpdb->signups, array('active' => 1, 'activated' => $now), array('activation_key' => $key)); wpmu_welcome_notification($blog_id, $user_id, $password, $signup->title, $meta); do_action('wpmu_activate_blog', $blog_id, $user_id, $password, $signup->title, $meta); return array('blog_id' => $blog_id, 'user_id' => $user_id, 'password' => $password, 'title' => $signup->title, 'meta' => $meta); }
/** * Intersects with ``wpmu_activate_signup()`` through s2Member's Multisite Networking patch. * * This function should return the same array that `wpmu_activate_signup()` returns; with the assumption that ``$user_already_exists``. * Which is exactly where this function intersects inside the `/wp-includes/ms-functions.php`. * * This can ONLY be fired through `/wp-activate.php` on the front-side. * Or through `/activate` via BuddyPress. * * @package s2Member\Registrations * @since 3.5 * * @attaches-to ``add_filter("_wpmu_activate_existing_error_");`` * * @param obj $_error Expects a `WP_Error` object to be passed through by the Filter. * @param array $vars Expects the defined variables from the scope of the calling Filter. * @return obj|array If unable to add an existing User, the original ``$_error`` obj is returned. * Otherwise we return an array of User details for continued processing by the caller. */ public static function ms_activate_existing_user($_error = FALSE, $vars = FALSE) { foreach (array_keys(get_defined_vars()) as $__v) { $__refs[$__v] =& ${$__v}; } do_action("ws_plugin__s2member_before_ms_activate_existing_user", get_defined_vars()); unset($__refs, $__v); extract($vars); // Extract all variables from ``wpmu_activate_signup()`` function. if (is_multisite()) { // This event should ONLY be processed with Multisite Networking. if (!is_admin() && (preg_match("/\\/wp-activate\\.php/", $_SERVER["REQUEST_URI"]) || c_ws_plugin__s2member_utils_conds::bp_is_installed() && bp_is_activation_page())) { if (!empty($user_id) && !empty($user_login) && !empty($user_email) && !empty($password) && !empty($meta) && !empty($meta["add_to_blog"]) && !empty($meta["new_role"])) { if (!empty($user_already_exists) && c_ws_plugin__s2member_utils_users::ms_user_login_email_exists_but_not_on_blog($user_login, $user_email, $meta["add_to_blog"])) { add_user_to_blog($meta["add_to_blog"], $user_id, $meta["new_role"]); // Add this User to the specified Blog. wp_update_user(array("ID" => $user_id, "user_pass" => $password)); // Update Password so it's the same as in the following msg. wpmu_welcome_user_notification($user_id, $password, $meta); // Send welcome letter via email just like ``wpmu_activate_signup()`` does. do_action("wpmu_activate_user", $user_id, $password, $meta); // Process Hook that would have been fired inside ``wpmu_activate_signup()``. return apply_filters("ws_plugin__s2member_ms_activate_existing_user", array("user_id" => $user_id, "password" => $password, "meta" => $meta), get_defined_vars()); } } } } return apply_filters("ws_plugin__s2member_ms_activate_existing_user", $_error, get_defined_vars()); // Else, return the standardized error. }
/** * Activate a signup. * * Hook to 'wpmu_activate_user' or 'wpmu_activate_blog' for events * that should happen only when users or sites are self-created (since * those actions are not called when users and sites are created * by a Super Admin). * * @since MU * * @global wpdb $wpdb * * @param string $key The activation key provided to the user. * @return array|WP_Error An array containing information about the activated user and/or blog */ function wpmu_activate_signup($key) { global $wpdb; $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE activation_key = %s", $key) ); if ( empty( $signup ) ) return new WP_Error( 'invalid_key', __( 'Invalid activation key.' ) ); if ( $signup->active ) { if ( empty( $signup->domain ) ) return new WP_Error( 'already_active', __( 'The user is already active.' ), $signup ); else return new WP_Error( 'already_active', __( 'The site is already active.' ), $signup ); } $meta = maybe_unserialize($signup->meta); $password = wp_generate_password( 12, false ); $user_id = username_exists($signup->user_login); if ( ! $user_id ) $user_id = wpmu_create_user($signup->user_login, $password, $signup->user_email); else $user_already_exists = true; if ( ! $user_id ) return new WP_Error('create_user', __('Could not create user'), $signup); $now = current_time('mysql', true); if ( empty($signup->domain) ) { $wpdb->update( $wpdb->signups, array('active' => 1, 'activated' => $now), array('activation_key' => $key) ); if ( isset( $user_already_exists ) ) return new WP_Error( 'user_already_exists', __( 'That username is already activated.' ), $signup); wpmu_welcome_user_notification( $user_id, $password, $meta ); /** * Fires immediately after a new user is activated. * * @since MU * * @param int $user_id User ID. * @param int $password User password. * @param array $meta Signup meta data. */ do_action( 'wpmu_activate_user', $user_id, $password, $meta ); return array( 'user_id' => $user_id, 'password' => $password, 'meta' => $meta ); } $blog_id = wpmu_create_blog( $signup->domain, $signup->path, $signup->title, $user_id, $meta, $wpdb->siteid ); // TODO: What to do if we create a user but cannot create a blog? if ( is_wp_error($blog_id) ) { // If blog is taken, that means a previous attempt to activate this blog failed in between creating the blog and // setting the activation flag. Let's just set the active flag and instruct the user to reset their password. if ( 'blog_taken' == $blog_id->get_error_code() ) { $blog_id->add_data( $signup ); $wpdb->update( $wpdb->signups, array( 'active' => 1, 'activated' => $now ), array( 'activation_key' => $key ) ); } return $blog_id; } $wpdb->update( $wpdb->signups, array('active' => 1, 'activated' => $now), array('activation_key' => $key) ); wpmu_welcome_notification($blog_id, $user_id, $password, $signup->title, $meta); /** * Fires immediately after a site is activated. * * @since MU * * @param int $blog_id Blog ID. * @param int $user_id User ID. * @param int $password User password. * @param string $signup_title Site title. * @param array $meta Signup meta data. */ do_action( 'wpmu_activate_blog', $blog_id, $user_id, $password, $signup->title, $meta ); return array('blog_id' => $blog_id, 'user_id' => $user_id, 'password' => $password, 'title' => $signup->title, 'meta' => $meta); }
/** * Intersects with ``wpmu_activate_signup()`` through s2Member's Multisite Networking patch. * * This function should return the same array that `wpmu_activate_signup()` returns; with the assumption that ``$user_already_exists``. * Which is exactly where this function intersects inside the `/wp-includes/ms-functions.php`. * * This can ONLY be fired through `/wp-activate.php` on the front-side. * Or through `/activate` via BuddyPress. * * @package s2Member\Registrations * @since 3.5 * * @attaches-to ``add_filter('_wpmu_activate_existing_error_');`` * * @param WP_Error $_error Expects a `WP_Error` object to be passed through by the Filter. * @param array $vars Expects the defined variables from the scope of the calling Filter. * * @return WP_Error|array If unable to add an existing User, the original ``$_error`` obj is returned. * Otherwise we return an array of User details for continued processing by the caller. */ public static function ms_activate_existing_user($_error = NULL, $vars = array()) { foreach (array_keys(get_defined_vars()) as $__v) { $__refs[$__v] =& ${$__v}; } do_action('ws_plugin__s2member_before_ms_activate_existing_user', get_defined_vars()); unset($__refs, $__v); // Housekeeping. extract($vars); // Extract all variables from ``wpmu_activate_signup()`` function. $ci = $GLOBALS['WS_PLUGIN__']['s2member']['o']['ruris_case_sensitive'] ? '' : 'i'; if (is_multisite()) { // This event should ONLY be processed with Multisite Networking. if (!is_admin() && (preg_match('/\\/wp-activate\\.php/' . $ci, $_SERVER['REQUEST_URI']) || c_ws_plugin__s2member_utils_conds::bp_is_installed() && bp_is_activation_page())) { if (!empty($user_id) && !empty($user_login) && !empty($user_email) && !empty($password) && !empty($meta) && !empty($meta['add_to_blog']) && !empty($meta['new_role'])) { if (!empty($user_already_exists) && c_ws_plugin__s2member_utils_users::ms_user_login_email_exists_but_not_on_blog($user_login, $user_email, $meta['add_to_blog'])) { add_user_to_blog($meta['add_to_blog'], $user_id, $meta['new_role']); // Add this User to the specified Blog. wp_update_user(wp_slash(array('ID' => $user_id, 'user_pass' => $password))); // Update Password so it's the same as in the following msg. wpmu_welcome_user_notification($user_id, $password, $meta); // Send welcome letter via email just like ``wpmu_activate_signup()`` does. do_action('wpmu_activate_user', $user_id, $password, $meta); // Process Hook that would have been fired inside ``wpmu_activate_signup()``. return apply_filters('ws_plugin__s2member_ms_activate_existing_user', array('user_id' => $user_id, 'password' => $password, 'meta' => $meta), get_defined_vars()); } } } } return apply_filters('ws_plugin__s2member_ms_activate_existing_user', $_error, get_defined_vars()); // Else, return the standardized error. }
/** * Process queue for one blog * **/ function queue_process($blog_ID, $site_ID) { global $wpdb; $query = $wpdb->prepare("SELECT * FROM {$wpdb->base_prefix}add_new_users_queue WHERE add_new_users_site_ID = '%d' AND add_new_users_blog_ID = '%d' LIMIT 1", $site_ID, $blog_ID); $users = $wpdb->get_results($query, ARRAY_A); if (count($users) > 0) { foreach ($users as $user) { $user['add_new_users_user_password'] = '******' !== $user['add_new_users_user_password'] ? $user['add_new_users_user_password'] : wp_generate_password(); if (is_multisite()) { $user_id = wpmu_create_user($user['add_new_users_user_login'], $user['add_new_users_user_password'], $user['add_new_users_user_email']); } else { $user_id = wp_create_user($user['add_new_users_user_login'], $user['add_new_users_user_password'], $user['add_new_users_user_email']); } if ($user_id && !is_wp_error($user_id)) { if (is_multisite()) { add_user_to_blog($wpdb->blogid, $user_id, $user['add_new_users_user_role']); wpmu_welcome_user_notification($user_id, $user['add_new_users_user_password'], ''); } else { if (isset($user['add_new_users_user_role'])) { $user_object = new WP_User($user_id); $user_object->set_role($user['add_new_users_user_role']); } wp_new_user_notification($user_id, $user['add_new_users_user_password']); } $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->base_prefix}add_new_users_queue WHERE add_new_users_blog_ID = '%d' AND add_new_users_site_ID = '%d' AND add_new_users_ID = '%d'", $wpdb->blogid, $wpdb->siteid, $user['add_new_users_ID'])); } } } }