function pmpro_membership_level_profile_fields_update()
{
    //get the user id
    global $wpdb, $current_user, $user_ID;
    get_currentuserinfo();
    if (!empty($_REQUEST['user_id'])) {
        $user_ID = $_REQUEST['user_id'];
    }
    if (!current_user_can('edit_user', $user_ID)) {
        return false;
    }
    //level change
    if (isset($_REQUEST['membership_level'])) {
        if (pmpro_changeMembershipLevel($_REQUEST['membership_level'], $user_ID)) {
            //it changed. send email
            $level_changed = true;
        }
    }
    //expiration change
    if (!empty($_REQUEST['expires'])) {
        //update the expiration date
        $expiration_date = intval($_REQUEST['expires_year']) . "-" . intval($_REQUEST['expires_month']) . "-" . intval($_REQUEST['expires_day']);
        $sqlQuery = "UPDATE {$wpdb->pmpro_memberships_users} SET enddate = '" . $expiration_date . "' WHERE status = 'active' AND user_id = '" . $user_ID . "' LIMIT 1";
        if ($wpdb->query($sqlQuery)) {
            $expiration_changed = true;
        }
    } elseif (isset($_REQUEST['expires'])) {
        //already blank? have to check for null or '0000-00-00 00:00:00' or '' here.
        $sqlQuery = "SELECT user_id FROM {$wpdb->pmpro_memberships_users} WHERE (enddate IS NULL OR enddate = '' OR enddate = '0000-00-00 00:00:00') AND status = 'active' AND user_id = '" . $user_ID . "' LIMIT 1";
        $blank = $wpdb->get_var($sqlQuery);
        if (empty($blank)) {
            //null out the expiration
            $sqlQuery = "UPDATE {$wpdb->pmpro_memberships_users} SET enddate = NULL WHERE status = 'active' AND user_id = '" . $user_ID . "' LIMIT 1";
            if ($wpdb->query($sqlQuery)) {
                $expiration_changed = true;
            }
        }
    }
    //send email
    if (!empty($level_changed) || !empty($expiration_changed)) {
        //email to member
        $pmproemail = new PMProEmail();
        if (!empty($expiration_changed)) {
            $pmproemail->expiration_changed = true;
        }
        $pmproemail->sendAdminChangeEmail(get_userdata($user_ID));
        //email to admin
        $pmproemail = new PMProEmail();
        if (!empty($expiration_changed)) {
            $pmproemail->expiration_changed = true;
        }
        $pmproemail->sendAdminChangeAdminEmail(get_userdata($user_ID));
    }
}
function pmpro_membership_level_profile_fields_update()
{
    //get the user id
    global $wpdb, $current_user, $user_ID;
    get_currentuserinfo();
    if (!empty($_REQUEST['user_id'])) {
        $user_ID = $_REQUEST['user_id'];
    }
    $membership_level_capability = apply_filters("pmpro_edit_member_capability", "manage_options");
    if (!current_user_can($membership_level_capability)) {
        return false;
    }
    //level change
    if (isset($_REQUEST['membership_level'])) {
        //if the level is being set to 0 by the admin, it's a cancellation.
        $changed_or_cancelled = '';
        if ($_REQUEST['membership_level'] === 0 || $_REQUEST['membership_level'] === '0' || $_REQUEST['membership_level'] == '') {
            $changed_or_cancelled = 'admin_cancelled';
        } else {
            $changed_or_cancelled = 'admin_changed';
        }
        //if the cancel at gateway box is not checked, don't cancel
        if (empty($_REQUEST['cancel_subscription'])) {
            add_filter('pmpro_cancel_previous_subscriptions', 'pmpro_cancel_previous_subscriptions_false');
        }
        //do the change
        if (pmpro_changeMembershipLevel($_REQUEST['membership_level'], $user_ID, $changed_or_cancelled)) {
            //it changed. send email
            $level_changed = true;
        }
        //remove filter after ward
        if (empty($_REQUEST['cancel_subscription'])) {
            remove_filter('pmpro_cancel_previous_subscriptions', 'pmpro_cancel_previous_subscriptions_false');
        }
    }
    //expiration change
    if (!empty($_REQUEST['expires'])) {
        //update the expiration date
        $expiration_date = intval($_REQUEST['expires_year']) . "-" . str_pad(intval($_REQUEST['expires_month']), 2, "0", STR_PAD_LEFT) . "-" . str_pad(intval($_REQUEST['expires_day']), 2, "0", STR_PAD_LEFT);
        $sqlQuery = "UPDATE {$wpdb->pmpro_memberships_users} SET enddate = '" . $expiration_date . "' WHERE status = 'active' AND membership_id = '" . intval($_REQUEST['membership_level']) . "' AND user_id = '" . $user_ID . "' LIMIT 1";
        if ($wpdb->query($sqlQuery)) {
            $expiration_changed = true;
        }
    } elseif (isset($_REQUEST['expires'])) {
        //already blank? have to check for null or '0000-00-00 00:00:00' or '' here.
        $sqlQuery = "SELECT user_id FROM {$wpdb->pmpro_memberships_users} WHERE (enddate IS NULL OR enddate = '' OR enddate = '0000-00-00 00:00:00') AND status = 'active' AND user_id = '" . $user_ID . "' LIMIT 1";
        $blank = $wpdb->get_var($sqlQuery);
        if (empty($blank)) {
            //null out the expiration
            $sqlQuery = "UPDATE {$wpdb->pmpro_memberships_users} SET enddate = NULL WHERE status = 'active' AND membership_id = '" . intval($_REQUEST['membership_level']) . "' AND user_id = '" . $user_ID . "' LIMIT 1";
            if ($wpdb->query($sqlQuery)) {
                $expiration_changed = true;
            }
        }
    }
    //emails if there was a change
    if (!empty($level_changed) || !empty($expiration_changed)) {
        //email to admin
        $pmproemail = new PMProEmail();
        if (!empty($expiration_changed)) {
            $pmproemail->expiration_changed = true;
        }
        $pmproemail->sendAdminChangeAdminEmail(get_userdata($user_ID));
        //send email
        if (!empty($_REQUEST['send_admin_change_email'])) {
            //email to member
            $pmproemail = new PMProEmail();
            if (!empty($expiration_changed)) {
                $pmproemail->expiration_changed = true;
            }
            $pmproemail->sendAdminChangeEmail(get_userdata($user_ID));
        }
    }
}