/**
 * edit custom fields
 */
function mgm_edit_custom_fields($user_ID = false, $submit_row = false, $return = false)
{
    // get user
    if (!$user_ID) {
        $user_ID = mgm_get_user_id();
    }
    // get form object
    if (is_object($user_ID)) {
        $user_ID = $user_ID->ID;
    }
    //check logged in user is super admin:
    $is_admin = is_super_admin() ? true : false;
    // system
    $system_obj = mgm_get_class('system');
    // get custom_fields
    $profile_fields = mgm_get_config('default_profile_fields', array());
    // get active custom fields on profile page
    //$cf_profile_page = mgm_get_class('member_custom_fields')->get_fields_where(array('display'=>array('on_profile'=>true)));
    //issue #844 - get active custom fields for profile page
    $cf_profile_pg = mgm_get_class('member_custom_fields');
    $cf_profile_page = array();
    foreach (array_unique($cf_profile_pg->sort_orders) as $id) {
        foreach ($cf_profile_pg->custom_fields as $field) {
            // issue #954: show the field only if it is enabled for profile page
            if ($field['id'] == $id && ($field['display']['on_profile'] || $is_admin)) {
                $cf_profile_page[] = $field;
            }
        }
    }
    $member = mgm_get_member($user_ID);
    //this is a fix for issue#: 589, see the notes for details:
    //This is to read saved coupons as array in order to fix the fatal error on some servers.
    //This will change the object on each users profile view.
    //Also this will avoid using patch for batch update,
    $arr_coupon = array('upgrade', 'extend');
    $oldcoupon_found = 0;
    foreach ($arr_coupon as $cpn_type) {
        if (isset($member->{$cpn_type}['coupon']) && is_object($member->{$cpn_type}['coupon'])) {
            $member->{$cpn_type}['coupon'] = (array) $member->{$cpn_type}['coupon'];
            $oldcoupon_found++;
        }
    }
    if ($oldcoupon_found) {
        $member->save();
    }
    // user
    $user = get_userdata($user_ID);
    // init
    $html = '';
    // capture
    $fields = array();
    //default and readonly fields:
    $default_readonly = array();
    $arr_images = array();
    //issue #844
    $skip_fileds = array('subscription_introduction', 'coupon', 'privacy_policy', 'payment_gateways', 'terms_conditions', 'subscription_options', 'autoresponder', 'captcha', 'show_public_profile');
    // loop fields
    foreach ($cf_profile_page as $field) {
        // issue#: 255
        if (in_array($field['name'], array_keys($profile_fields))) {
            //if custom field = defualt field, is read only
            if ($field['attributes']['readonly'] && !$is_admin) {
                $default_readonly[] = $profile_fields[$field['name']]['id'];
                //email and url id is different than custom fields:
                if (in_array($field['name'], array('email', 'url'))) {
                    $default_readonly[] = $field['name'];
                }
            }
            continue;
        }
        //issue #844
        if (in_array($field['name'], $skip_fileds)) {
            continue;
        }
        // init value
        $value = '';
        //disable readonly for admin user(issue#: 515)
        $ro = $field['attributes']['readonly'] == true && !$is_admin ? 'readonly="readonly"' : false;
        // value
        if (isset($member->custom_fields->{$field}['name'])) {
            $value = $member->custom_fields->{$field}['name'];
        }
        // date
        if ($field['name'] == 'birthdate') {
            if ($value) {
                //convert saved date to input field format
                $value = mgm_get_datepicker_format('date', $value);
            } else {
                $value = '';
            }
            $element = '<input type="text" name="mgm_profile_field[' . $field['name'] . ']" value="' . $value . '" ' . $ro . ' class="text ' . ($ro ? '' : 'mgm_date') . ' mgm_custom_profile_birthdate"/>';
        } else {
            if ($field['name'] == 'country') {
                $countries = mgm_field_values(TBL_MGM_COUNTRY, 'code', 'name');
                if ($ro) {
                    $countries = !empty($value) ? array($value => $countries[$value]) : array(" " => "&nbsp;");
                }
                //issue #1782
                $value = !empty($value) ? $value : 'US';
                $options = mgm_make_combo_options($countries, $value, MGM_KEY_VALUE);
                $element = '<select name="mgm_profile_field[' . $field['name'] . ']" > ' . $options . ' </select>';
            } else {
                if ($field['type'] == 'text') {
                    $element = '<input type="text" name="mgm_profile_field[' . $field['name'] . ']" value="' . $value . '" ' . $ro . ' class="text mgm_custom_profile_password"/>';
                } else {
                    if ($field['type'] == 'password') {
                        continue;
                    } else {
                        if ($field['type'] == 'textarea') {
                            $element = '<textarea name="mgm_profile_field[' . $field['name'] . ']" cols="40" rows="5" ' . $ro . '>' . $value . '</textarea>';
                        } else {
                            if ($field['type'] == 'checkbox') {
                                $options = preg_split('/[;,]/', $field['options']);
                                //$values  = preg_split('/[;,\s]/', $value);
                                $values = @unserialize($value);
                                // pass " " as value to prevent the default value getting selected, if no option is selected
                                $values = empty($values) ? " " : $values;
                                //Issue # 694
                                $element = mgm_make_checkbox_group('mgm_profile_field[' . $field['name'] . '][]', $options, $values, MGM_VALUE_ONLY, '', 'div');
                            } else {
                                if ($field['type'] == 'checkboxg') {
                                    $options = preg_split('/[;,]/', $field['options']);
                                    if (!is_array($value)) {
                                        $values = @unserialize($value);
                                    } else {
                                        $values = $value;
                                    }
                                    $values = empty($values) ? " " : $values;
                                    $element = mgm_make_checkbox_group('mgm_profile_field[' . $field['name'] . '][]', $options, $values, MGM_VALUE_ONLY, '', 'div');
                                } else {
                                    if ($field['type'] == 'radio') {
                                        $options = preg_split('/[;,]/', $field['options']);
                                        $element = mgm_make_radio_group('mgm_profile_field[' . $field['name'] . ']', $options, $value, MGM_VALUE_ONLY);
                                    } else {
                                        if ($field['type'] == 'select') {
                                            $element = '<select name="mgm_profile_field[' . $field['name'] . ']" ' . $ro . '>';
                                            $options = preg_split('/[;,]/', $field['options']);
                                            if ($ro) {
                                                $options = !empty($value) ? array($value => $value) : array(" " => "&nbsp;");
                                            }
                                            $element .= mgm_make_combo_options($options, $value, MGM_VALUE_ONLY);
                                            $element .= '</select>';
                                        } else {
                                            if ($field['type'] == 'selectm') {
                                                $element = '<select name="mgm_profile_field[' . $field['name'] . '][]" ' . $ro . ' multiple>';
                                                $options = preg_split('/[;,]/', $field['options']);
                                                if ($ro) {
                                                    $options = !empty($value) ? array($value => $value) : array(" " => "&nbsp;");
                                                }
                                                $element .= mgm_make_combo_options($options, $value, MGM_VALUE_ONLY);
                                                $element .= '</select>';
                                            } else {
                                                if ($field['type'] == 'html') {
                                                    $element = '';
                                                    $element .= '<div class="mgm_custom_subs_introduction">' . html_entity_decode(mgm_stripslashes_deep($field['value'])) . '</div>';
                                                } else {
                                                    if ($field['type'] == 'image') {
                                                        $form_fields = new mgm_form_fields();
                                                        $element = $form_fields->get_field_element($field, 'mgm_profile_field', $value);
                                                        if (!in_array($field['name'], $arr_images)) {
                                                            $arr_images[] = $field['name'];
                                                        }
                                                        //issue #1258
                                                    } else {
                                                        if ($field['type'] == 'label') {
                                                            $form_fields = new mgm_form_fields();
                                                            $element = $form_fields->get_field_element($field, 'mgm_profile_field', $value);
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        // set array
        if ($element) {
            $fields[] = array('name' => $field['name'], 'label' => $field['label'], 'field' => $element);
        }
    }
    // if fields - issue #1782
    if (count($fields)) {
        $html .= '<table class="form-table">';
        foreach ($fields as $i => $row) {
            $html .= '<tr><th><label>' . mgm_stripslashes_deep($row['label']) . '</label></th>';
            $html .= '<td>' . $row['field'] . '</td></tr>';
        }
        // button
        if ($submit_row) {
            $html .= '<tr>
				<td colspan="2">
					<input class="button" type="submit" name="submit" value="' . __('Update your profile', 'mgm') . '"/>
					<input type="hidden" name="update_mgm_custom_fields_submit" value="1" />
			</td></tr>';
        }
        $html .= '</table>';
        $html .= mgm_attach_scripts(true, array());
        $yearRange = mgm_get_calendar_year_range();
        //include scripts for image upload:
        if (!empty($arr_images)) {
            $html .= mgm_upload_script_js('your-profile', $arr_images);
        }
        $html .= '<script language="javascript">jQuery(document).ready(function(){try{mgm_date_picker(".mgm_date",false,{yearRange:"' . $yearRange . '", dateFormat: "' . mgm_get_datepicker_format() . '"});}catch(x){}});</script>';
    }
    if (!empty($default_readonly)) {
        $html .= '<script language="javascript">';
        $html .= 'jQuery(document).ready(function(){try{';
        $html .= 'jQuery.each(' . json_encode($default_readonly) . ', function(){jQuery("#"+this).attr("readonly", true)})';
        $html .= '}catch(x){}})';
        $html .= '</script>';
    }
    // return
    if ($return) {
        return $html;
    } else {
        echo $html;
    }
}
/**
 * generate register form html
 *
 * @param string $form_template
 * @param array $form_params
 * @return string $form_html
 * @since 2.6
 */
function mgm_generate_register_form_html($form_template, $form_params)
{
    // get mgm_system
    $system_obj = mgm_get_class('system');
    // no custom fields
    $hide_custom_fields = $system_obj->get_setting('hide_custom_fields');
    // extract params
    extract($form_params);
    // init images
    $cf_images = array();
    // init
    $form_html = $form_template;
    // loop to create form html
    foreach ($cf_register_page as $field) {
        // skip custom fields by settings call
        if ($hide_custom_fields == 'Y' || $hide_custom_fields == 'W' && $wordpres_form || $hide_custom_fields == 'C' && !$wordpres_form) {
            // some fields required irespective of settings
            if (!in_array($field['name'], array('subscription_options', 'payment_gateways'))) {
                continue;
            }
        }
        // skip username
        if ($field['name'] == 'username' && bool_from_yn($system_obj->get_setting('enable_email_as_username'))) {
            continue;
        }
        // image field
        if ($field['type'] == 'image') {
            if (!in_array($field['name'], $cf_images)) {
                $cf_images[] = $field['name'];
            }
        }
        // field wrapper
        $wrapper_ph = sprintf('[user_field_wrapper_%s]', $field['name']);
        // field label
        $label_ph = sprintf('[user_field_label_%s]', $field['name']);
        // field/html element
        $element_ph = sprintf('[user_field_element_%s]', $field['name']);
        // replace wrapper
        $form_html = str_replace($wrapper_ph, $field['name'] . '_box', $form_html);
        // replace label
        $form_html = str_replace($label_ph, $field['attributes']['hide_label'] ? '' : mgm_stripslashes_deep($field['label']), $form_html);
        // replace element
        $form_html = str_replace($element_ph, $form_fields->get_field_element($field, 'mgm_register_field'), $form_html);
    }
    // years
    $yearRange = mgm_get_calendar_year_range();
    // append script
    $form_html .= '<script language="javascript">jQuery(document).ready(function(){
					try{mgm_date_picker(".mgm_date",false,{yearRange:"' . $yearRange . '", 
					dateFormat: "' . mgm_get_datepicker_format() . '"});}catch(x){}});</script>';
    // deault action
    $form_action = mgm_get_custom_url('register');
    // membership
    if ($membership = mgm_request_var('membership', '', true)) {
        $form_action = add_query_arg(array('membership' => $membership), $form_action);
    }
    // package
    if ($package = mgm_request_var('package', '', true)) {
        $form_action = add_query_arg(array('package' => $package), $form_action);
    }
    // add script for m/p
    if (!empty($package) || !empty($membership)) {
        //i ssue#: 482
        $form_html .= '<script language="javascript">jQuery(document).ready(function(){jQuery(\'#registerform\').attr(\'action\',\'' . $form_action . '\')});</script>';
    }
    // include scripts for image upload:
    if (!empty($cf_images)) {
        $form_html .= mgm_attach_scripts(true, array()) . mgm_upload_script_js('registerform', $cf_images);
    }
    // return
    return $form_html;
}
/**
 * Custom user profile form
 */
function mgm_user_profile_form($user_id = NULL, $temp_edit = false, $args = array())
{
    global $wpdb;
    // get mgm_system
    $system_obj = mgm_get_class('system');
    // current user
    $current_user = $user_id ? get_userdata($user_id) : wp_get_current_user();
    // current or voew
    if ($current_user->ID) {
        // current
        $user = mgm_get_userdata($current_user->ID);
    } else {
        // query string
        $user = mgm_get_user_from_querystring();
    }
    // if no user
    if (!isset($user) || !$user->ID || is_super_admin($user->ID)) {
        return mgm_user_login_form();
        exit;
    }
    // mgm member
    $member = mgm_get_member($user->ID);
    // edit mode, on for current user
    $edit_mode = $current_user->ID == $user->ID ? true : false;
    $temp = 0;
    // form action
    $form_action = get_permalink();
    // reset
    if ($form_action == null) {
        $form_action = mgm_get_current_url();
        $form_action = str_replace(array('&updated=true', '?updated=true'), '', $form_action);
    }
    //init - issue #1573
    $show_membership_fields_arr = array();
    if (isset($args['membership']) && !empty($args['membership'])) {
        // membership
        $membership = $args['membership'];
        // get active custom fields on register
        $cf_profile_by_membership_types = mgm_get_class('member_custom_fields')->get_fields_where(array('attributes' => array('profile_by_membership_types' => true)));
        //check
        if (!empty($cf_profile_by_membership_types)) {
            //loop
            foreach ($cf_profile_by_membership_types as $cf_profile_by_membership_type) {
                //membership_type
                $membership_types_string = isset($cf_profile_by_membership_type['attributes']['profile_membership_types_field_alias']) ? $cf_profile_by_membership_type['attributes']['profile_membership_types_field_alias'] : null;
                //check
                if (preg_match('/\\b' . $membership . '\\b/', $membership_types_string) && $membership_types_string != null) {
                    $show_fields_arr[] = $cf_profile_by_membership_type['name'];
                    $show_membership_fields_arr[] = $cf_profile_by_membership_type;
                    if ($cf_profile_by_membership_type['name'] == 'password') {
                        foreach ($cf_profile_by_membership_types as $cf_profile_by_membership) {
                            if ($cf_profile_by_membership['name'] == 'password_conf') {
                                $show_membership_fields_arr[] = $cf_profile_by_membership;
                            }
                        }
                    }
                }
            }
        }
    }
    // get default fields
    $profile_fields = mgm_get_config('default_profile_fields', array());
    // get active custom fields on profile page
    $cf_profile_page = mgm_get_class('member_custom_fields')->get_fields_where(array('display' => array('on_profile' => true)));
    $cf_noton_profile = mgm_get_class('member_custom_fields')->get_fields_where(array('display' => array('on_profile' => false)));
    //merge - issue #1573
    if (isset($show_membership_fields_arr) && is_array($show_membership_fields_arr) && !empty($show_membership_fields_arr)) {
        $cf_profile_page = array_merge($cf_profile_page, $show_membership_fields_arr);
        $cf_noton_profile = array_merge($cf_noton_profile, $show_membership_fields_arr);
    }
    $error_html = '';
    //issue #867
    $css_group = mgm_get_css_group();
    if ($css_group != 'none') {
        // error_html
        $error_html .= '<link rel="stylesheet" href="' . MGM_ASSETS_URL . 'css/' . $css_group . '/mgm.messages.css' . '" type="text/css" media="all" />';
    }
    // update
    if ($edit_mode) {
        // updated
        if (isset($_POST['method']) && $_POST['method'] == 'update_user') {
            // check security before processing form
            if (!wp_verify_nonce(mgm_post_var('_mgmnonce_user_profile'), 'user_profile')) {
                mgm_security_error('user_profile');
            }
            // user lib
            if (mgm_compare_wp_version('3.1', '<')) {
                // only before 3.1
                require_once ABSPATH . WPINC . '/registration.php';
            }
            // callback
            do_action('personal_options_update', $current_user->ID);
            // not multisite, duplicate email allowed ?
            if (!is_multisite()) {
                // save
                $errors = mgm_user_profile_update($current_user->ID);
            } else {
                // multi site
                // get user
                $user = get_userdata($current_user->ID);
                // update here:
                // Update the email address, if present. duplicate check
                if ($user->user_login && isset($_POST['user_email']) && is_email($_POST['user_email']) && $wpdb->get_var($wpdb->prepare("SELECT user_login FROM {$wpdb->signups} WHERE user_login = %s", $user->user_login))) {
                    $wpdb->query($wpdb->prepare("UPDATE {$wpdb->signups} SET user_email = %s WHERE user_login = %s", $_POST['user_email'], $user->user_login));
                }
                // edit
                if (!isset($errors) || isset($errors) && is_object($errors) && false == $errors->get_error_codes()) {
                    $errors = mgm_user_profile_update($current_user->ID);
                }
            }
            // trap erros
            if (!is_wp_error($errors)) {
                // redirect
                mgm_redirect(add_query_arg(array('updated' => 'true'), $form_action));
            }
            // errors
            if (isset($errors) && !is_numeric($errors)) {
                // get error
                $error_html .= mgm_set_errors($errors, true);
            }
        }
    }
    // updated
    if ($edit_mode && isset($_GET['updated'])) {
        $error_html .= '<div class="mgm_message_success">';
        $message = apply_filters('mgm_profile_edit_message', __('User updated.', 'mgm'));
        $error_html .= '<div><strong>' . $message . '</strong></div></div>';
    }
    // 	get row row template
    $form_row_template = $system_obj->get_template('profile_form_row_template');
    // get template row filter, mgm_profile_form_row_template for edit, mgm_profile_form_row_template_view for public view
    $form_row_template = apply_filters('mgm_profile_form_row_template' . (!$edit_mode ? '_view' : ''), $form_row_template);
    $cf_order = array();
    foreach ($cf_profile_page as $fld) {
        $cf_order[] = array('field' => $fld['name']);
    }
    // auto generate form template
    // form_template
    $form_template = '';
    // captured
    $fields_captured = array();
    // get field_groups
    $field_groups = mgm_get_config('profile_field_groups', array());
    // loop groups
    foreach ($field_groups as $group => $group_fields) {
        if ($group == 'Photo') {
            $photo_exists = false;
            foreach ($cf_profile_page as $photo) {
                if ($photo['name'] == 'photo') {
                    $photo_exists = true;
                    break;
                }
            }
            if (!$photo_exists) {
                continue;
            }
        }
        $fields_with_order = array();
        //issue #1197
        $css_title = function_exists('mb_strtolower') ? @mb_strtolower($group) : strtolower($group);
        $css_title = str_replace(' ', '_', $css_title);
        // group
        // PREV CODE
        $form_template .= sprintf('<span class="profile_group_%s">%s</span>', $css_title, $group);
        // loop to create form template
        foreach ($group_fields as $group_field) {
            // skip password
            //if(!$edit_mode && $group_field == 'password') continue;
            if (!$edit_mode && in_array($group_field, array('password', 'password_conf'))) {
                continue;
            }
            // set not found
            $captured = false;
            // first check if in custom fields
            foreach ($cf_profile_page as $field) {
                // skip password in non edit mode
                if ($field['name'] == $group_field) {
                    // set found
                    $captured = true;
                    // skip password
                    //if(!$edit_mode && $field['name'] == 'password') continue;
                    if (!$edit_mode && in_array($field['name'], array('password', 'password_conf'))) {
                        continue;
                    }
                    // store for no repeat
                    $fields_captured[] = $field['name'];
                    // field wrapper
                    $wrapper_ph = sprintf('[user_field_wrapper_%s]', $field['name']);
                    // field label
                    $label_ph = sprintf('[user_field_label_%s]', $field['name']);
                    // field/html element
                    $element_ph = sprintf('[user_field_element_%s]', $field['name']);
                    // set element name
                    // PREV CODE
                    //$form_template .= str_replace(array('[user_field_wrapper]','[user_field_label]','[user_field_element]'),array($wrapper_ph,$label_ph,$element_ph),$form_row_template);
                    // Issue #1149
                    foreach ($cf_order as $index => $cfo) {
                        if ($cfo['field'] == $field['name']) {
                            $fields_with_order[$index] = str_replace(array('[user_field_wrapper]', '[user_field_label]', '[user_field_element]'), array($wrapper_ph, $label_ph, $element_ph), $form_row_template);
                            break;
                        }
                    }
                    // break;
                    break;
                }
            }
            // if not captured
            if (!$captured) {
                $continue = false;
                foreach ($cf_noton_profile as $cffield) {
                    if ($cffield['name'] == $group_field) {
                        $continue = true;
                        break;
                    }
                }
                // break;
                if ($continue) {
                    continue;
                }
                // check set
                if (!isset($profile_fields[$group_field]['name'])) {
                    continue;
                }
                // field wrapper
                $wrapper_ph = sprintf('[user_field_wrapper_%s]', $profile_fields[$group_field]['name']);
                // field label
                $label_ph = sprintf('[user_field_label_%s]', $profile_fields[$group_field]['name']);
                // field/html element
                $element_ph = sprintf('[user_field_element_%s]', $profile_fields[$group_field]['name']);
                // set element name
                // PREV CODE
                // $form_template .= str_replace(array('[user_field_wrapper]','[user_field_label]','[user_field_element]'),array($wrapper_ph,$label_ph,$element_ph),$form_row_template);
                // Issue #1149
                $field_added = false;
                foreach ($cf_order as $index => $cfo) {
                    if ($cfo['field'] == $group_field) {
                        $fields_with_order[$index] = str_replace(array('[user_field_wrapper]', '[user_field_label]', '[user_field_element]'), array($wrapper_ph, $label_ph, $element_ph), $form_row_template);
                        $field_added = true;
                        break;
                    }
                }
                if (!$field_added) {
                    /*$fields_with_order[$index+1] = str_replace(array('[user_field_wrapper]','[user_field_label]','[user_field_element]'),array($wrapper_ph,$label_ph,$element_ph),$form_row_template);*/
                    // default profile instalation fields - issue #1207
                    if (in_array($group_field, array('username', 'email', 'password', 'password', 'password_conf'))) {
                        $fields_with_order[$temp++] = str_replace(array('[user_field_wrapper]', '[user_field_label]', '[user_field_element]'), array($wrapper_ph, $label_ph, $element_ph), $form_row_template);
                    }
                }
            }
        }
        // Issue #1149
        // Process custom field form_template with order
        if (!empty($fields_with_order)) {
            // Sort by key
            ksort($fields_with_order);
            // Loop through fields and attach html
            foreach ($fields_with_order as $fworder) {
                $form_template .= $fworder;
            }
        }
    }
    // other
    $other_header = false;
    // loop to create form template
    foreach ($cf_profile_page as $field) {
        // skip password in non edit mode
        //if(!$edit_mode && $field['name'] == 'password') continue;
        if (!$edit_mode && in_array($field['name'], array('password', 'password_conf'))) {
            continue;
        }
        // skip captured
        if (in_array($field['name'], $fields_captured)) {
            continue;
        }
        // header
        if (!$other_header) {
            // rest
            $form_template .= sprintf('<span class="profile_group_others">%s</span>', __('Others', 'mgm'));
            $other_header = true;
        }
        // field wrapper
        $wrapper_ph = sprintf('[user_field_wrapper_%s]', $field['name']);
        // field label
        $label_ph = sprintf('[user_field_label_%s]', $field['name']);
        // field/html element
        $element_ph = sprintf('[user_field_element_%s]', $field['name']);
        // template for show_public_profile
        if ($field['name'] == 'show_public_profile') {
            // template
            $form_row_template_pf = $system_obj->get_template('register_form_row_autoresponder_template');
            // set element place holders
            $form_template .= str_replace(array('[user_field_wrapper]', '[user_field_label]', '[user_field_element]'), array($wrapper_ph, $label_ph, $element_ph), $form_row_template_pf);
        } else {
            // set element name
            $form_template .= str_replace(array('[user_field_wrapper]', '[user_field_label]', '[user_field_element]'), array($wrapper_ph, $label_ph, $element_ph), $form_row_template);
        }
    }
    // get template filter, mgm_profile_form_template for edit, mgm_profile_form_template_view for public view
    $form_template = apply_filters('mgm_profile_form_template' . (!$edit_mode ? '_view' : ''), $form_template);
    // now replace and create the fields
    $form_html = $form_template;
    // get mgm_form_fields generator
    $form_fields = new mgm_form_fields(array('wordpres_form' => false));
    $arr_images = array();
    // loop custom fields to replace form labels/elements
    foreach ($cf_profile_page as $field) {
        // skip password in non edit mode
        //if(!$edit_mode && $field['name'] == 'password') continue;
        if (!$edit_mode && in_array($field['name'], array('password', 'password_conf'))) {
            continue;
        }
        if ($edit_mode && $field['type'] == 'image') {
            if (!in_array($field['name'], $arr_images)) {
                $arr_images[] = $field['name'];
            }
        }
        // field wrapper
        $wrapper_ph = sprintf('[user_field_wrapper_%s]', $field['name']);
        // field label
        $label_ph = sprintf('[user_field_label_%s]', $field['name']);
        // field/html element
        $element_ph = sprintf('[user_field_element_%s]', $field['name']);
        // edit mode
        if ($edit_mode) {
            // for username
            if ($field['name'] == 'username') {
                //localazing the label  issue# 617
                $label_lcz = mgm_stripslashes_deep($field['label']);
                $label_lcz = __($label_lcz, 'mgm');
                $field['label'] = sprintf('%s (<em>%s</em>)', $label_lcz, __('Username not changeable', 'mgm'));
            } elseif ($field['name'] == 'password') {
                //localazing the label  issue# 617
                $label_lcz = mgm_stripslashes_deep($field['label']);
                $label_lcz = __($label_lcz, 'mgm');
                $field['label'] = sprintf('%s (<em>%s</em>)', $label_lcz, __('Leave blank if don\'t wish to update', 'mgm'));
            }
        } else {
            // for display_name
            if ($field['name'] == 'display_name') {
                $field['label'] = __('Display Name', 'mgm');
            }
        }
        // replace wrapper
        $form_html = str_replace($wrapper_ph, $field['name'] . '_box', $form_html);
        //localazing the label  issue# 617
        $label_lcz = mgm_stripslashes_deep($field['label']);
        $label_lcz = __($label_lcz, 'mgm');
        // replace label(hidden) - issue #1050
        $form_html = str_replace($label_ph, $field['attributes']['hide_label'] ? '' : mgm_stripslashes_deep($field['label']), $form_html);
        // replace label
        $form_html = str_replace($label_ph, $label_lcz, $form_html);
        // selected value
        if (isset($profile_fields[$field['name']]) && isset($user->{$profile_fields}[$field['name']]['name'])) {
            // wp alias'
            // value
            $value = $user->{$profile_fields}[$field['name']]['name'];
            // birthdate
            if ($field['name'] == 'birthdate') {
                // convert saved date to input field format
                $value = mgm_get_datepicker_format('date', $value);
            } elseif ($field['type'] == 'checkbox') {
                //$options = preg_split('/[;,]/', $field['options']);
                //$value  = preg_split('/[;,\s]/', $value);
                //issue #1070
                $value = @unserialize($value);
                // pass " " as value to prevent the default value getting selected, if no option is selected
                $value = empty($value) ? " " : $value;
            }
        } else {
            if (isset($member->custom_fields->{$field}['name'])) {
                // custom field
                // value
                $value = $member->custom_fields->{$field}['name'];
                // birthdate
                if ($field['name'] == 'birthdate') {
                    // convert saved date to input field format
                    $value = mgm_get_datepicker_format('date', $value);
                } elseif ($field['type'] == 'checkbox') {
                    //$options = preg_split('/[;,]/', $field['options']);
                    //$value  = preg_split('/[;,\s]/', $value);
                    //issue #1070
                    $value = @unserialize($value);
                    // pass " " as value to prevent the default value getting selected, if no option is selected
                    $value = empty($value) ? " " : $value;
                }
                //issue #1484
                if ($field['name'] == 'show_public_profile' && $field['type'] == 'checkbox') {
                    $value = $member->custom_fields->{$field}['name'];
                }
            } else {
                if (isset($user->{$field}['name'])) {
                    // object var
                    // value
                    $value = $user->{$field}['name'];
                } else {
                    // none
                    // default
                    $value = '';
                }
            }
        }
        // dont set value for password
        if (in_array($field['name'], array('password', 'password_conf'))) {
            $value = '';
        }
        // disable username
        if ($field['name'] == 'username') {
            $field['attributes']['readonly'] = true;
        }
        // nickname
        if ($field['name'] == 'nickname') {
            $field['attributes']['required'] = true;
        }
        // edit mode
        if ($edit_mode) {
            if ($field['name'] == 'show_public_profile') {
                //echo "xxx".$form_fields->get_field_element($field,'mgm_profile_field',$value);
            }
            // replace element
            $form_html = str_replace($element_ph, $form_fields->get_field_element($field, 'mgm_profile_field', $value), $form_html);
        } else {
            // view
            // country
            if ($field['name'] == 'country') {
                $value = mgm_country_from_code($value);
            } elseif ($field['name'] == 'photo' && !empty($value)) {
                $value = sprintf('<img src="%s" alt="%s" >', $value, basename($value));
            }
            // replace element
            $form_html = str_replace($element_ph, $value, $form_html);
        }
    }
    // loop default fields to replace form elements
    foreach ($profile_fields as $field_key => $field) {
        // skip password in non edit mode
        //if(!$edit_mode && $field['name'] == 'user_password') continue;
        if (!$edit_mode && in_array($field['name'], array('user_password', 'user_password_conf'))) {
            continue;
        }
        $continue = false;
        foreach ($cf_noton_profile as $cffield) {
            if ($cffield['name'] == $field['name']) {
                $continue = true;
                break;
            }
        }
        if ($continue) {
            continue;
        }
        // field wrapper
        $wrapper_ph = sprintf('[user_field_wrapper_%s]', $field['name']);
        // field label
        $label_ph = sprintf('[user_field_label_%s]', $field['name']);
        // field/html element
        $element_ph = sprintf('[user_field_element_%s]', $field['name']);
        // edit mode
        if ($edit_mode) {
            // for username
            if ($field['name'] == 'user_login') {
                //localazing the label  issue# 617
                $label_lcz = mgm_stripslashes_deep($field['label']);
                $label_lcz = __($label_lcz, 'mgm');
                $field['label'] = sprintf('%s (<em>%s</em>)', $label_lcz, __('Username not changeable', 'mgm'));
            } elseif ($field['name'] == 'user_password') {
                //localazing the label  issue# 617
                $label_lcz = mgm_stripslashes_deep($field['label']);
                $label_lcz = __($label_lcz, 'mgm');
                $field['label'] = sprintf('%s (<em>%s</em>)', $label_lcz, __('Leave blank if don\'t wish to update', 'mgm'));
            }
        } else {
            // for display_name
            if ($field['name'] == 'display_name') {
                $field['label'] = __('Display Name', 'mgm');
            }
        }
        // replace wrapper
        $form_html = str_replace($wrapper_ph, $field['name'] . '_box', $form_html);
        //localazing the label  issue# 617
        $label_lcz = mgm_stripslashes_deep($field['label']);
        $label_lcz = __($label_lcz, 'mgm');
        // replace label
        $form_html = str_replace($label_ph, $label_lcz, $form_html);
        // selected value
        if (isset($user->{$field}['name'])) {
            // wp alias
            $value = $user->{$field}['name'];
        } else {
            if (isset($member->custom_fields->{$field_key})) {
                // custom field
                $value = $member->custom_fields->{$field_key};
            } else {
                // none
                $value = '';
            }
        }
        // dont set value for password
        //if($field['name'] == 'user_password') $value = '';
        if (in_array($field['name'], array('user_password', 'user_password_conf'))) {
            $value = '';
        }
        // edit mode
        if ($edit_mode) {
            // replace element
            $form_html = str_replace($element_ph, $form_fields->get_field_element($field, 'mgm_profile_field', $value), $form_html);
        } else {
            // country
            if ($field_key == 'country') {
                $value = mgm_country_from_code($value);
            }
            // set
            $form_html = str_replace($element_ph, $value, $form_html);
        }
    }
    // attach scripts
    $form_html .= mgm_attach_scripts(true, array());
    // range
    $yearRange = mgm_get_calendar_year_range();
    // append script
    $form_html .= '<script language="javascript">jQuery(document).ready(function(){try{mgm_date_picker(".mgm_date",false,{yearRange:"' . $yearRange . '", dateFormat: "' . mgm_get_datepicker_format() . '"});}catch(x){}});</script>';
    //include scripts for image upload:
    if (!empty($arr_images)) {
        $form_html .= mgm_upload_script_js('profileform', $arr_images);
    }
    // buttun
    $button_html = '';
    // button on edit
    if ($edit_mode && !$temp_edit) {
        // default
        $button_html = '<div><input class="button mgm-profile-button" type="submit" name="wp-submit" id="wp-submit" value="' . __('Update', 'mgm') . '" /></div>';
        // apply button filter
        $button_html = apply_filters('mgm_profile_form_button', $button_html);
    }
    //profile by membership - issue #1573
    if (isset($args['membership']) && !empty($args['membership'])) {
        // hidden
        $button_html .= sprintf('<input type="hidden" name="membership" value="%s">', $args['membership']);
    }
    // hidden
    $button_html .= '<input type="hidden" name="method" value="update_user">';
    // nonce
    $button_html .= wp_nonce_field('user_profile', '_mgmnonce_user_profile', true, false);
    // temp
    if (!$temp_edit) {
        // open
        $form_open = sprintf('<form class="mgm_form" name="profileform" id="profileform" action="%s" method="post">', $form_action);
        $form_close = '</form>';
    } else {
        $form_open = $form_close = '';
    }
    // output form
    $html = sprintf('<div class="mgm_prifile_form">%s %s %s %s %s</div>', $error_html, $form_open, $form_html, $button_html, $form_close);
    //issue #1113
    $html = mgm_stripslashes_deep($html);
    // filter
    $html = apply_filters('mgm_user_profile_form_html', $html, $current_user);
    //issue #1635
    $user_profile_html = '<div class="mgm_user_profile_container">' . $html . '</div>';
    // return
    return $user_profile_html;
}