/**
 * Activate a signup.
 *
 *
 * @param string $key The activation key provided to the user.
 * @return array An array containing information about the activated user and/or blog
 */
function wppb_activate_signup($key)
{
    global $wpdb;
    $bloginfo = get_bloginfo('name');
    $wppb_generalSettings = get_option('wppb_general_settings');
    if (is_multisite()) {
        $signup = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->signups} WHERE activation_key = %s", $key));
    } else {
        $signup = $wpdb->get_row($wpdb->prepare("SELECT * FROM " . $wpdb->prefix . "signups WHERE activation_key = %s", $key));
    }
    if (empty($signup)) {
        return $activateUserErrorMessage1 = apply_filters('wppb_register_activate_user_error_message1', '<p class="error">' . __('Invalid activation key!', 'profilebuilder') . '</p>');
    }
    if ($signup->active) {
        if (empty($signup->domain)) {
            return $activateUserErrorMessage2 = apply_filters('wppb_register_activate_user_error_message2', '<p class="error">' . __('The user is already active!', 'profilebuilder') . '</p>');
        }
    }
    $meta = unserialize($signup->meta);
    if (isset($wppb_generalSettings['loginWith']) && $wppb_generalSettings['loginWith'] == 'email') {
        $user_login = $wpdb->escape($signup->user_email);
    } else {
        $user_login = $wpdb->escape($signup->user_login);
    }
    $user_email = $wpdb->escape($signup->user_email);
    $password = base64_decode($meta['user_pass']);
    $user_id = username_exists($user_login);
    if (!$user_id) {
        $user_id = wppb_create_user($user_login, $password, $user_email);
    } else {
        $user_already_exists = true;
    }
    if (!$user_id) {
        return $activateUserErrorMessage4 = apply_filters('wppb_register_activate_user_error_message4', '<p class="error">' . __('Could not create user!', 'profilebuilder') . '</p>');
    } elseif (isset($user_already_exists)) {
        return $activateUserErrorMessage5 = apply_filters('wppb_register_activate_user_error_message5', '<p class="error">' . __('That username is already activated!', 'profilebuilder') . '</p>');
    } else {
        $now = current_time('mysql', true);
        if (is_multisite()) {
            $retVal = $wpdb->update($wpdb->signups, array('active' => 1, 'activated' => $now), array('activation_key' => $key));
        } else {
            $retVal = $wpdb->update($wpdb->prefix . 'signups', array('active' => 1, 'activated' => $now), array('activation_key' => $key));
        }
        wppb_add_meta_to_user_on_activation($user_id, '', $meta);
        // if admin approval is activated, then block the user untill he gets approved
        $wppb_generalSettings = get_option('wppb_general_settings');
        if ($wppb_generalSettings['adminApproval'] == 'yes') {
            wp_set_object_terms($user_id, array('unapproved'), 'user_status', false);
            clean_object_term_cache($user_id, 'user_status');
        }
        wppb_notify_user_registration_email($bloginfo, $user_login, $user_email, 'sending', $password, $wppb_generalSettings['adminApproval']);
        do_action('wppb_activate_user', $user_id, $password, $meta);
        if ($retVal) {
            return $registerFilterArray['successfullUserActivation'] = apply_filters('wppb_register_successfull_user_activation', '<p class="success">' . __('The user was successfully activated.', 'profilebuilder') . '</p><!-- .success -->');
        } else {
            return $registerFilterArray['successfullUserActivationFail'] = apply_filters('wppb_register_failed_user_activation', '<p class="error">' . __('There was an error while trying to activate the user.', 'profilebuilder') . '</p><!-- .error -->');
        }
    }
}
/**
 * Activate a signup.
 *
 *
 * @param string $activation_key The activation key provided to the user.
 * @return array An array containing information about the activated user and/or blog
 */
function wppb_manual_activate_signup($activation_key)
{
    global $wpdb;
    if (is_multisite()) {
        $signup = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->signups} WHERE activation_key = %s", $activation_key));
    } else {
        $signup = $wpdb->get_row($wpdb->prepare("SELECT * FROM " . $wpdb->prefix . "signups WHERE activation_key = %s", $activation_key));
    }
    if (!empty($signup) && !$signup->active) {
        $meta = unserialize($signup->meta);
        $user_login = esc_sql($signup->user_login);
        $user_email = esc_sql($signup->user_email);
        $password = base64_decode($meta['user_pass']);
        $user_id = username_exists($user_login);
        if (!$user_id) {
            $user_id = wppb_create_user($user_login, $password, $user_email);
        } else {
            $user_already_exists = true;
        }
        if (!$user_id) {
            return __('Could not create user!', 'profilebuilder');
        } elseif (isset($user_already_exists) && $user_already_exists == true) {
            return __('That username is already activated!', 'profilebuilder');
        } else {
            $now = current_time('mysql', true);
            $retVal = is_multisite() ? $wpdb->update($wpdb->signups, array('active' => 1, 'activated' => $now), array('activation_key' => $activation_key)) : $wpdb->update($wpdb->prefix . 'signups', array('active' => 1, 'activated' => $now), array('activation_key' => $activation_key));
            wppb_add_meta_to_user_on_activation($user_id, '', $meta);
            // if admin approval is activated, then block the user untill he gets approved
            $wppb_general_settings = get_option('wppb_general_settings');
            if (isset($wppb_general_settings['adminApproval']) && $wppb_general_settings['adminApproval'] == 'yes') {
                wp_set_object_terms($user_id, array('unapproved'), 'user_status', false);
                clean_object_term_cache($user_id, 'user_status');
            }
            wppb_notify_user_registration_email(get_bloginfo('name'), $user_login, $user_email, 'sending', $password, isset($wppb_general_settings['adminApproval']) ? $wppb_general_settings['adminApproval'] : 'no');
            do_action('wppb_activate_user', $user_id, $password, $meta);
            return $retVal ? 'ok' : __('There was an error while trying to activate the user', 'profilebuilder');
        }
    }
}
/**
 * Activate a signup.
 *
 *
 * @param string $key The activation key provided to the user.
 * @return array An array containing information about the activated user and/or blog
 */
function wppb_activate_signup($key)
{
    global $wpdb;
    $bloginfo = get_bloginfo('name');
    $wppb_general_settings = get_option('wppb_general_settings');
    $signup = is_multisite() ? $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->signups} WHERE activation_key = %s", $key)) : $wpdb->get_row($wpdb->prepare("SELECT * FROM " . $wpdb->base_prefix . "signups WHERE activation_key = %s", $key));
    if (empty($signup)) {
        return apply_filters('wppb_register_activate_user_error_message1', '<p class="error">' . __('Invalid activation key!', 'profilebuilder') . '</p>');
    }
    if ($signup->active) {
        if (empty($signup->domain)) {
            return apply_filters('wppb_register_activate_user_error_message2', '<p class="error">' . __('This username is now active!', 'profilebuilder') . '</p>');
        }
    }
    $meta = unserialize($signup->meta);
    $user_login = isset($wppb_general_settings['loginWith']) && $wppb_general_settings['loginWith'] == 'email' ? trim($signup->user_email) : trim($signup->user_login);
    $user_email = esc_sql($signup->user_email);
    /* the password is in hashed form in the signup table so we will add it later */
    $password = NULL;
    $user_id = username_exists($user_login);
    if (!$user_id) {
        $user_id = wppb_create_user($user_login, $password, $user_email);
    } else {
        $user_already_exists = true;
    }
    if (!$user_id) {
        return apply_filters('wppb_register_activate_user_error_message4', '<p class="error">' . __('Could not create user!', 'profilebuilder') . '</p>');
    } elseif (isset($user_already_exists) && $user_already_exists == true) {
        return apply_filters('wppb_register_activate_user_error_message5', '<p class="error">' . __('This username is already activated!', 'profilebuilder') . '</p>');
    } else {
        $inserted_user = is_multisite() ? $wpdb->update($wpdb->signups, array('active' => 1, 'activated' => current_time('mysql', true)), array('activation_key' => $key)) : $wpdb->update($wpdb->base_prefix . 'signups', array('active' => 1, 'activated' => current_time('mysql', true)), array('activation_key' => $key));
        wppb_add_meta_to_user_on_activation($user_id, '', $meta);
        // if admin approval is activated, then block the user untill he gets approved
        $wppb_generalSettings = get_option('wppb_general_settings');
        if (isset($wppb_generalSettings['adminApproval']) && $wppb_generalSettings['adminApproval'] == 'yes') {
            $user_data = get_userdata($user_id);
            if ($wppb_generalSettings != 'not_found' && !empty($wppb_generalSettings['adminApprovalOnUserRole'])) {
                foreach ($user_data->roles as $role) {
                    if (in_array($role, $wppb_generalSettings['adminApprovalOnUserRole'])) {
                        wp_set_object_terms($user_id, array('unapproved'), 'user_status', false);
                        clean_object_term_cache($user_id, 'user_status');
                    } else {
                        add_filter('wppb_register_success_message', 'wppb_noAdminApproval_successMessage');
                    }
                }
            } else {
                wp_set_object_terms($user_id, array('unapproved'), 'user_status', false);
                clean_object_term_cache($user_id, 'user_status');
            }
        }
        if (!isset($wppb_generalSettings['adminApproval'])) {
            $wppb_generalSettings['adminApproval'] = 'no';
        }
        /* copy the hashed password from signup meta to wp user table */
        if (!empty($meta['user_pass'])) {
            /* we might still have the base64 encoded password in signups and not the hash */
            if (base64_encode(base64_decode($meta['user_pass'], true)) === $meta['user_pass']) {
                $meta['user_pass'] = wp_hash_password($meta['user_pass']);
            }
            $wpdb->update($wpdb->users, array('user_pass' => $meta['user_pass']), array('ID' => $user_id));
        }
        wppb_notify_user_registration_email($bloginfo, $user_login, $user_email, 'sending', $password, $wppb_generalSettings['adminApproval']);
        do_action('wppb_activate_user', $user_id, $password, $meta);
        if ($inserted_user) {
            $success_message = apply_filters('wppb_success_email_confirmation', '<p class="wppb-success">' . __('Your email was successfully confirmed.', 'profilebuilder') . '</p><!-- .success -->');
            $admin_approval_message = apply_filters('wppb_email_confirmation_with_admin_approval', '<p class="wppb-success">' . __('Before you can access your account, an administrator needs to approve it. You will be notified via email.', 'profilebuilder') . '</p>');
            $wppb_general_settings = get_option('wppb_general_settings', 'false');
            if (!empty($wppb_general_settings['adminApproval']) && $wppb_general_settings['adminApproval'] == 'yes') {
                $user_data = get_userdata($user_id);
                if ($wppb_general_settings != 'not_found' && !empty($wppb_general_settings['adminApprovalOnUserRole'])) {
                    foreach ($user_data->roles as $role) {
                        if (in_array($role, $wppb_general_settings['adminApprovalOnUserRole'])) {
                            return $success_message . $admin_approval_message;
                        } else {
                            wp_set_object_terms($user_id, NULL, 'user_status');
                            clean_object_term_cache($user_id, 'user_status');
                            return $success_message;
                        }
                    }
                } else {
                    return $success_message . $admin_approval_message;
                }
            } else {
                wp_set_object_terms($user_id, NULL, 'user_status');
                clean_object_term_cache($user_id, 'user_status');
                return $success_message;
            }
        } else {
            return apply_filters('wppb_register_failed_user_activation', '<p class="error">' . __('There was an error while trying to activate the user.', 'profilebuilder') . '</p><!-- .error -->');
        }
    }
}
/**
 * Activate a signup.
 *
 *
 * @param string $activation_key The activation key provided to the user.
 * @return array An array containing information about the activated user and/or blog
 */
function wppb_manual_activate_signup($activation_key)
{
    global $wpdb;
    if (is_multisite()) {
        $signup = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->signups} WHERE activation_key = %s", $activation_key));
    } else {
        $signup = $wpdb->get_row($wpdb->prepare("SELECT * FROM " . $wpdb->prefix . "signups WHERE activation_key = %s", $activation_key));
    }
    if (!empty($signup) && !$signup->active) {
        $meta = unserialize($signup->meta);
        $user_login = esc_sql($signup->user_login);
        $user_email = esc_sql($signup->user_email);
        /* the password is in hashed form in the signup table and we will copy it later to the user */
        $password = NULL;
        $user_id = username_exists($user_login);
        if (!$user_id) {
            $user_id = wppb_create_user($user_login, $password, $user_email);
        } else {
            $user_already_exists = true;
        }
        if (!$user_id) {
            return __('Could not create user!', 'profile-builder');
        } elseif (isset($user_already_exists) && $user_already_exists == true) {
            return __('That username is already activated!', 'profile-builder');
        } else {
            $now = current_time('mysql', true);
            $retVal = is_multisite() ? $wpdb->update($wpdb->signups, array('active' => 1, 'activated' => $now), array('activation_key' => $activation_key)) : $wpdb->update($wpdb->base_prefix . 'signups', array('active' => 1, 'activated' => $now), array('activation_key' => $activation_key));
            wppb_add_meta_to_user_on_activation($user_id, '', $meta);
            /* copy the hashed password from signup meta to wp user table */
            if (!empty($meta['user_pass'])) {
                /* we might still have the base64 encoded password in signups and not the hash */
                if (base64_encode(base64_decode($meta['user_pass'], true)) === $meta['user_pass']) {
                    $meta['user_pass'] = wp_hash_password($meta['user_pass']);
                }
                $wpdb->update($wpdb->users, array('user_pass' => $meta['user_pass']), array('ID' => $user_id));
            }
            wppb_notify_user_registration_email(get_bloginfo('name'), $user_login, $user_email, 'sending', $password, isset($wppb_general_settings['adminApproval']) ? $wppb_general_settings['adminApproval'] : 'no');
            do_action('wppb_activate_user', $user_id, $password, $meta);
            return $retVal ? 'ok' : __('There was an error while trying to activate the user', 'profile-builder');
        }
    }
}