/**
 * create purchase another button
 *
 * @param array userdata
 * @retun string html output
 */
function mgm_get_purchase_another_subscription_button($args = array())
{
    global $wpdb;
    //ceck settings
    $settings = mgm_get_class('system')->get_setting();
    // check
    if (!isset($settings['enable_multiple_level_purchase']) || isset($settings['enable_multiple_level_purchase']) && !bool_from_yn($settings['enable_multiple_level_purchase'])) {
        return;
    }
    // current user
    $user = wp_get_current_user();
    // validate
    if (!$user->ID) {
        // query string
        $user = mgm_get_user_from_querystring();
    }
    // validate
    if (!$user->ID) {
        return __('No such user', 'mgm');
    }
    // userdata
    $username = $user->user_login;
    $mgm_home = get_option('siteurl');
    $member = mgm_get_member($user->ID);
    $system_obj = mgm_get_class('system');
    $membership_type = mgm_get_user_membership_type($user->ID, 'code');
    $packs_obj = mgm_get_class('subscription_packs');
    $packs = $packs_obj->get_packs('upgrade');
    $duration_str = $packs_obj->duration_str;
    $trial_taken = $member->trial_taken;
    // pack_ids
    $pack_ids = mgm_get_members_packids($member);
    $pack_membership_types = mgm_get_subscribed_membershiptypes($user->ID, $member);
    // query_arg
    $form_action = mgm_get_custom_url('transactions', false, array('action' => 'purchase_another', 'username' => $username));
    //issue 1009
    if (isset($settings['membership_details_url'])) {
        $membership_details_url = $settings['membership_details_url'];
    } else {
        $membership_details_url = get_admin_url() . 'profile.php?page=mgm/profile';
    }
    // cancel
    $cancel_url = $membership_details_url;
    // $cancel_url = mgm_get_custom_url('membership_details');
    // active modules
    $a_payment_modules = $system_obj->get_active_modules('payment');
    // 	selected_subscription
    $selected_subs = mgm_get_selected_subscription($args);
    // second step, after post
    if (isset($_POST['submit'])) {
        // verify selected
        if (!isset($_POST['subs_opt'])) {
            // die
            return sprintf(__('Package not selected, <a href="%s">go back</a>.', 'mgm'), $_POST['form_action']);
            exit;
        }
        // get subs data
        $subs_opt_pack = mgm_decode_package($_POST['subs_opt']);
        extract($subs_opt_pack);
        // check
        $valid = false;
        // loop packs
        foreach ($packs as $pack) {
            // check
            //check pack id as well: issue#: 580
            if ($pack['cost'] == $cost && $pack['duration'] == $duration && $pack['duration_type'] == $duration_type && $membership_type == $pack['membership_type'] && $pack_id == $pack['id']) {
                $valid = true;
                break;
            }
        }
        // error
        if (!$valid) {
            return __('Invalid data passed', 'mgm');
            exit;
        }
        // get object
        $member = new mgm_member($user->ID);
        $temp_membership = $member->_default_fields();
        $temp_membership['membership_type'] = $membership_type;
        $temp_membership['pack_id'] = $pack_id;
        //issue #860
        //if (isset($_POST['mgm_upgrade_field']['autoresponder']) && ($_POST['mgm_upgrade_field']['autoresponder'])=='Y') {
        if (isset($_POST['mgm_upgrade_field']['autoresponder']) && substr($_POST['mgm_upgrade_field']['autoresponder'], 0, 1) == 'Y') {
            $temp_membership['subscribed'] = 'Y';
            $temp_membership['autoresponder'] = $system_obj->active_modules['autoresponder'];
        }
        //issue #1236
        if (isset($_POST['mgm_upgrade_field']['coupon']) && !empty($_POST['mgm_upgrade_field']['coupon'])) {
            //issue #1250 - Coupon validation
            if (!empty($_POST['form_action'])) {
                // check if its a valid coupon
                if (!($coupon = mgm_get_coupon_data($_POST['mgm_upgrade_field']['coupon']))) {
                    //redirect back to the form
                    $q_arg = array('error_field' => 'Coupon', 'error_type' => 'invalid', 'error_field_value' => $_POST['mgm_upgrade_field']['coupon']);
                    $redirect = add_query_arg($q_arg, $_POST['form_action']);
                    // redirect
                    mgm_redirect($redirect);
                    exit;
                } else {
                    // membership type check
                    // get subs
                    if ($subs_pack = mgm_decode_package(mgm_post_var('subs_opt'))) {
                        // values
                        $coupon_values = mgm_get_coupon_values(NULL, $coupon['value'], true);
                        // check
                        if (isset($coupon_values['new_membership_type']) && $coupon_values['new_membership_type'] != $subs_pack['membership_type']) {
                            $new_membership_type = mgm_get_membership_type_name($coupon_values['new_membership_type']);
                            $q_arg = array('error_field' => 'Coupon', 'error_type' => 'invalid', 'membership_type' => $coupon_values['new_membership_type'], 'error_field_value' => $_POST['mgm_upgrade_field']['coupon']);
                            $redirect = add_query_arg($q_arg, $_POST['form_action']);
                            // redirect
                            mgm_redirect($redirect);
                            exit;
                        }
                    }
                }
            }
        }
        //inserted an incomplete entry for the selected subscription type
        mgm_save_another_membership_fields($temp_membership, $user->ID);
        // save coupon fields and update member object
        $member = mgm_save_partial_fields_purchase_more($user->ID, $membership_type, $cost);
        // coupon
        $purchase_another_coupon = false;
        // array
        if (isset($member->upgrade)) {
            if (is_array($member->upgrade) && isset($member->upgrade['coupon']['id'])) {
                $purchase_another_coupon = $member->upgrade['coupon'];
            } elseif (is_object($member->upgrade) && isset($member->upgrade->coupon->id)) {
                $purchase_another_coupon = mgm_object2array($member->upgrade->coupon);
            }
            // coupon
            mgm_get_purchase_another_coupon_pack($purchase_another_coupon, $pack);
        }
        //save custom fields -issue #1285
        if (isset($_POST['mgm_upgrade_field']) && !empty($_POST['mgm_upgrade_field'])) {
            //member
            $cf_member = mgm_get_member($user->ID);
            //upgrade custom fileds
            $cfu_fileds = mgm_get_class('member_custom_fields')->get_fields_where(array('display' => array('on_multiple_membership_level_purchase' => true)));
            //loop fields
            foreach ($cfu_fileds as $cf_filed) {
                //skip coupon and autoresponder
                if (in_array($cf_filed['name'], array('coupon', 'autoresponder'))) {
                    continue;
                }
                //check
                if (isset($_POST['mgm_upgrade_field'][$cf_filed['name']])) {
                    // check upgrae and required
                    if ((bool) $cf_filed['attributes']['required'] === true) {
                        if (empty($_POST['mgm_upgrade_field'][$cf_filed['name']])) {
                            //redirect back to the form
                            $q_arg = array('error_field' => $cf_filed['label'], 'error_type' => 'empty', 'error_field_value' => $_POST['mgm_upgrade_field'][$cf_filed['name']]);
                            $redirect = add_query_arg($q_arg, $_POST['form_action']);
                            mgm_redirect($redirect);
                            exit;
                        }
                    }
                    //appending custom fields
                    if (isset($cf_member->custom_fields->{$cf_filed}['name'])) {
                        $cf_member->custom_fields->{$cf_filed}['name'] = $_POST['mgm_upgrade_field'][$cf_filed['name']];
                    } else {
                        $cf_member->custom_fields->{$cf_filed}['name'] = $_POST['mgm_upgrade_field'][$cf_filed['name']];
                    }
                }
            }
            $cf_member->save();
        }
        // start html
        $html = '<div>';
        // free
        if (($pack['cost'] == 0 || $membership_type == 'free') && in_array('mgm_free', $a_payment_modules) && mgm_get_module('mgm_free')->is_enabled()) {
            $html .= sprintf('<div>%s - %s</div>', __('Create a free account ', 'mgm'), ucwords($membership_type));
            $module = 'mgm_free';
            // payments url
            $payments_url = mgm_get_custom_url('transactions');
            // if tril module selected and cost is 0 and free moduleis not active
            if ($membership_type == 'trial') {
                if (in_array('mgm_trial', $a_payment_modules)) {
                    $module = 'mgm_trial';
                }
            }
            //Purchase Another Membership Level problem : issue #: 752
            $redirect = add_query_arg(array('method' => 'payment_return', 'module' => $module, 'custom' => $user->ID . '_' . $duration . '_' . $duration_type . '_' . $pack_id . '_Y', 'redirector' => $redirector), $payments_url);
            // redirect
            if (!headers_sent()) {
                @header('location: ' . $redirect);
                exit;
            }
            // js redirect
            $html .= sprintf('<script type="text/javascript">window.location = "%s";</script><div>%s</div>', $redirect, $packs_obj->get_pack_desc($pack));
        } else {
            $html .= sprintf('<div class="mgm_get_subs_btn">%s</div>', $packs_obj->get_pack_desc($pack));
            // coupon
            if (isset($purchase_another_coupon['id'])) {
                $html .= sprintf('<div class="mgm_get_subs_btn">%s</div>', sprintf(__('Using Coupon "%s" - %s', 'mgm'), $purchase_another_coupon['name'], $purchase_another_coupon['description']));
            }
            $html .= sprintf('<div class="mgm_get_subs_btn">%s</div>', __('Please Select from Available Payment Gateways', 'mgm'));
        }
        //bypass if payment gateway field is selected -issue #1764
        if ((double) $pack['cost'] > 0 && isset($_POST['mgm_payment_gateways']) && !empty($_POST['mgm_payment_gateways'])) {
            //init
            $tran_id = 0;
            if (!$tran_id) {
                $tran_id = mgm_add_transaction($pack, array('is_another_membership_purchase' => true, 'user_id' => $user->ID));
            }
            // module
            $mod_obj = mgm_get_module($_POST['mgm_payment_gateways'], 'payment');
            // module end point
            $redirect = $mod_obj->_get_endpoint('html_redirect', false);
            // encode id:
            //encode transaction id
            $encode_tran_id = mgm_encode_id($tran_id);
            //args
            $redirect = add_query_arg(array('tran_id' => $encode_tran_id), $redirect);
            // do the redirect to payment
            mgm_redirect($redirect);
        }
        // init
        $payment_modules = array();
        // when active
        if ($a_payment_modules) {
            // loop
            foreach ($a_payment_modules as $payment_module) {
                // not trial
                if (in_array($payment_module, array('mgm_free', 'mgm_trial'))) {
                    continue;
                }
                //consider only the modules assigned to pack
                if (isset($pack['modules']) && !in_array($payment_module, (array) $pack['modules'])) {
                    continue;
                }
                // store
                $payment_modules[] = $payment_module;
            }
        }
        // loop payment mods if not free
        if (count($payment_modules) && $cost) {
            // transaction
            $tran_id = 0;
            // loop
            foreach ($payment_modules as $module) {
                // module
                $mod_obj = mgm_get_module($module, 'payment');
                // create transaction
                // if(!$tran_id) $tran_id = $mod_obj->_create_transaction($pack, array('is_another_membership_purchase' => true, 'user_id' => $user->ID));
                if (!$tran_id) {
                    $tran_id = mgm_add_transaction($pack, array('is_another_membership_purchase' => true, 'user_id' => $user->ID));
                }
                // button
                $html .= sprintf('<div class="mgm_get_subs_btn">%s</div>', $mod_obj->get_button_subscribe(array('pack' => $pack, 'tran_id' => $tran_id)));
            }
        } else {
            if ($cost) {
                $html .= sprintf('<div class="mgm_get_subs_btn">%s</div>', __('There are no payment gateways available at this time.', 'mgm'));
            }
        }
        // html
        $html .= '</div>';
    } else {
        // first step show upgrade options
        // html
        $html = sprintf('<p class="message register">%s</p>', __('Please Select from Available Membership Packages', 'mgm'));
        // upgrade_packages
        $upgrade_packages = '';
        // pack to modules
        $pack_modules = array();
        // loop
        foreach ($packs as $pack) {
            // default
            $checked = '';
            // skip already purchased packs
            if (in_array($pack['id'], $pack_ids)) {
                continue;
            }
            //skip same membership level subscriptions
            if (in_array($pack['membership_type'], $pack_membership_types)) {
                continue;
            }
            // do not show trial or free as upgradre
            if ($pack['membership_type'] == 'trial' || $pack['membership_type'] == 'free') {
                continue;
            }
            // reset
            $checked = mgm_select_subscription($pack, $selected_subs);
            // skip other when a package sent as selected
            if ($selected_subs !== false) {
                if (empty($checked)) {
                    continue;
                }
            }
            // checked
            if (!$checked) {
                $checked = (int) $pack['default'] == 1 ? ' checked="checked"' : '';
            }
            // duration
            if ($pack['duration'] == 1) {
                $dur_str = rtrim($duration_str[$pack['duration_type']], 's');
            } else {
                $dur_str = $duration_str[$pack['duration_type']];
            }
            $css_group = mgm_get_css_group();
            // encode pack
            $subs_opt_enc = mgm_encode_package($pack);
            // set
            $pack_modules[$subs_opt_enc] = $pack['modules'];
            //issue #867
            if ($css_group != 'none') {
                //expand this if needed
                $css_link_format = '<link rel="stylesheet" href="%s" type="text/css" media="all" />';
                $css_file = MGM_ASSETS_URL . 'css/' . $css_group . '/mgm.form.fields.css';
                $upgrade_packages .= sprintf($css_link_format, $css_file);
            }
            // free
            if (($pack['cost'] == 0 || strtolower($pack['membership_type']) == 'free') && in_array('mgm_free', $a_payment_modules) && mgm_get_module('mgm_free')->enabled == 'Y') {
                // input
                $input = sprintf('<input type="radio" %s class="checkbox" name="subs_opt" value="%s" rel="mgm_subscription_options"/>', $checked, $subs_opt_enc);
                // html
                $upgrade_packages .= '  
							<div class="mgm_subs_wrapper ' . $pack['membership_type'] . '">
								<div class="mgm_subs_option ' . $pack['membership_type'] . '">
									' . $input . '
								</div>
								<div class="mgm_subs_pack_desc ' . $pack['membership_type'] . '">
									' . $packs_obj->get_pack_desc($pack) . '
								</div>
								 <div class="clearfix"></div>
								 <div class="mgm_subs_desc ' . $pack['membership_type'] . '">
									' . mgm_stripslashes_deep($pack['description']) . '
								 </div>
							</div>';
            } else {
                // input
                $input = sprintf('<input type="radio" %s class="checkbox" name="subs_opt" value="%s" rel="mgm_subscription_options"/>', $checked, $subs_opt_enc);
                // html
                $upgrade_packages .= '  
							<div class="mgm_subs_wrapper ' . $pack['membership_type'] . '">
								<div class="mgm_subs_option ' . $pack['membership_type'] . '">
									' . $input . '
								</div>
								<div class="mgm_subs_pack_desc ' . $pack['membership_type'] . '">
									' . $packs_obj->get_pack_desc($pack) . '
								</div>
								 <div class="clearfix"></div>
								 <div class="mgm_subs_desc ' . $pack['membership_type'] . '">
									' . mgm_stripslashes_deep($pack['description']) . '
								 </div>
							</div>';
            }
        }
        // add pack_modules as json data, may consider jquery data later
        if (!empty($pack_modules)) {
            $html .= sprintf('<script language="javascript">var mgm_pack_modules = %s</script>', json_encode($pack_modules));
        }
        // show error
        if (!$upgrade_packages) {
            // html
            $html .= '<div class="mgm_subs_wrapper">
						<div class="mgm_subs_pack_desc">
							' . __('Sorry, no packages available.', 'mgm') . '
						</div>
					 </div>
					 <p>						
						<input type="button" name="cancel" onclick="window.location=\'' . $cancel_url . '\'" value="' . __('Cancel', 'mgm') . '" class="button-primary" />&nbsp;					
					 </p>';
        } else {
            /*			$error_field = mgm_request_var('error_field'); 
            			if(!empty($error_field)) {
            				$errors = new WP_Error();
            				switch (mgm_request_var('error_type')) {
            					case 'empty':
            						$error_string = 'You must provide a ';
            						break;
            					case 'invalid':
            						$error_string = 'Invalid ';
            						break;	
            				}				
            				//issue #703
            				$errors->add( $error_field, __( '<strong>ERROR</strong>: '.$error_string, 'mgm' ).$error_field );
            				$html .= mgm_set_errors($errors, true);					
            			}*/
            // check errors if any:
            $html .= mgm_subscription_purchase_errors();
            // form
            $html .= '<form action="' . $form_action . '" method="post" class="mgm_form"><div class="mgm_get_pack_form_container">';
            $html .= '<input type="hidden" name="form_action" value="' . $form_action . '" />';
            $html .= $upgrade_packages;
            //issue #1285
            $html .= mgm_get_custom_fields($user->ID, array('on_multiple_membership_level_purchase' => true), 'mgm_upgrade_field');
            // html
            $html .= '<input type="hidden" name="ref" value="' . md5($member->amount . '_' . $member->duration . '_' . $member->duration_type . '_' . $member->membership_type) . '" />';
            // set
            $html .= '<p>						
						<input class="button" type="submit" name="submit" value="' . __('Next', 'mgm') . '" />&nbsp;&nbsp;
						<input class="button" type="button" name="cancel" onclick="window.location=\'' . $cancel_url . '\'" value="' . __('Cancel', 'mgm') . '"/>&nbsp;					
					  </p>';
            // html
            $html .= '</div></form>';
        }
    }
    // return
    return $html;
}
 function _get_purchased_contents($post_types, $id, $start, $rows, $gifted = false)
 {
     global $wpdb;
     // array or string
     if (!is_array($post_types)) {
         $post_types = array($post_types);
     }
     // impode
     $post_types_in = mgm_map_for_in($post_types);
     // gifted
     $gifted_sql = $gifted ? "AND is_gift = 'Y'" : "AND is_gift = 'N'";
     // from
     $sql_from = " FROM " . $wpdb->posts . " A JOIN " . TBL_MGM_POST_PURCHASES . " B ON(A.ID = B.post_id) \r\r\n\t\t\t\t\t  WHERE post_status = 'publish' AND post_type IN ( {$post_types_in} ) {$gifted_sql}";
     // sql
     $sql = "SELECT DISTINCT(A.ID), post_type, post_title, post_date, post_content, user_id,guest_token {$sql_from} \r\r\n\t\t        ORDER BY post_date DESC LIMIT {$start},{$rows}";
     // get posts
     $results = $wpdb->get_results($sql);
     // init
     $posts = array();
     // check
     if ($results) {
         // loop
         foreach ($results as $post) {
             // get object
             $post_obj = mgm_get_post($post->ID);
             // check
             if (mgm_post_is_purchasable($post->ID, $post_obj)) {
                 // stip short code
                 $post->post_content = mgm_strip_shortcode($post->post_content);
                 // access type
                 $access_types = $post_obj->get_access_membership_types();
                 // access delay
                 $access_delays = $post_obj->get_access_delay();
                 // init
                 $access_settings = array();
                 // loop
                 foreach ($access_types as $access_type) {
                     // delay
                     $delay = isset($access_delays[$access_type]) ? (int) $access_delays[$access_type] : 0;
                     // set
                     $access_settings[] = array('membership_type' => array('code' => $access_type, 'name' => mgm_get_membership_type_name($access_type)), 'access_delay' => sprintf(__('%d day', 'mgm'), $delay));
                 }
                 // access
                 $post->access_settings = $access_settings;
                 // user
                 if ((int) $post->user_id > 0) {
                     // user
                     $user = get_userdata($post->user_id);
                     $user_info = array('by' => 'user', 'id' => $post->user_id, 'username' => $user->user_login, 'email' => $user->user_email);
                     // gifted
                     if ($gifted) {
                         $post->gift = array_slice($user_info, 1);
                     } else {
                         $post->purchase = $user_info;
                     }
                 } else {
                     $post->purchase = array('by' => 'guest', 'token' => $post->guest_token);
                 }
                 // unset
                 unset($post->guest_token, $post->user_id);
                 // set
                 $posts[] = $post;
             }
         }
     }
     // return
     return $posts;
 }
/**
 * register post, validate required custom fields
 *
 * @param string $sanitized_user_login 
 * @param string $user_email
 * @param object $errors
 * @param boolean $show_fields
 * @return object $errors
 */
function mgm_register_post($sanitized_user_login = '', $user_email = '', $errors = null, $show_fields = null)
{
    // get mgm_system
    $system_obj = mgm_get_class('system');
    // hide
    $hide_custom_fields = $system_obj->get_setting('hide_custom_fields');
    // error
    if (is_null($errors)) {
        $errors = new WP_Error();
    }
    // unset old errors
    unset($errors->errors);
    // errors
    $error_codes = $errors->get_error_codes();
    // user_login
    if (array_key_exists('user_login', $_POST)) {
        $sanitized_user_login = sanitize_user($_POST['user_login']);
        if ($sanitized_user_login == '') {
            if (!in_array('empty_username', $error_codes)) {
                $errors->add('empty_username', __('<strong>ERROR</strong>: Please enter a username.', 'mgm'));
            }
        } elseif (!validate_username($sanitized_user_login)) {
            if (!in_array('invalid_username', $error_codes)) {
                $errors->add('invalid_username', __('<strong>ERROR</strong>: This username is invalid because it uses illegal characters. Please enter a valid username.', 'mgm'));
            }
            $sanitized_user_login = '';
        } elseif (!mgm_validate_username($sanitized_user_login)) {
            if (!in_array('invalid_username', $error_codes)) {
                $errors->add('invalid_username', __('<strong>ERROR</strong>: This username is invalid because it uses illegal characters, spaces are not allowed. Please enter a valid username.', 'mgm'));
            }
            $sanitized_user_login = '';
        } elseif (username_exists($sanitized_user_login)) {
            if (!in_array('username_exists', $error_codes)) {
                $errors->add('username_exists', __('<strong>ERROR</strong>: This username is already registered, please choose another one.', 'mgm'));
            }
        }
    }
    // user_email
    if (array_key_exists('user_email', $_POST)) {
        $user_email = apply_filters('user_registration_email', $_POST['user_email']);
        // Check the e-mail address
        if ($user_email == '') {
            if (!in_array('empty_email', $error_codes)) {
                $errors->add('empty_email', __('<strong>ERROR</strong>: Please type your e-mail address.', 'mgm'));
            }
        } elseif (!is_email($user_email)) {
            if (!in_array('invalid_email', $error_codes)) {
                $errors->add('invalid_email', __('<strong>ERROR</strong>: The email address isn&#8217;t correct.', 'mgm'));
            }
            $user_email = '';
        } elseif (email_exists($user_email)) {
            if (!in_array('email_exists', $error_codes)) {
                $errors->add('email_exists', __('<strong>ERROR</strong>: This email is already registered, please choose another one.', 'mgm'));
            }
        }
    }
    // check email only #1106
    if (in_array('email_exists', $errors->get_error_codes())) {
        $label = 'email';
        $url = mgm_get_complete_registration_url('email', $user_email);
        // check
        if ($url !== FALSE) {
            // unset old errors
            unset($errors->errors);
            // set
            $errors->add('unfinished_registration', sprintf(__('<strong>ERROR</strong>: This %s has an unfinished registration. Click here to <a href="%s">complete</a>.', 'mgm'), $label, $url));
            // return form here
            return $errors;
        }
    }
    // get custom fields
    $cf_register_page = mgm_get_class('member_custom_fields')->get_fields_where(array('display' => array('on_register' => true)));
    //#739 modified starts
    if (empty($show_fields)) {
        $cf_alias_fields = mgm_get_class('member_custom_fields')->get_fields_where(array('attributes' => array('capture_only' => true)));
        if (!empty($cf_alias_fields)) {
            foreach ($cf_alias_fields as $key => $array) {
                unset($cf_register_page[$key]);
            }
        }
    }
    // #739 modified ends
    // #740 starts
    $args_fields = "";
    // Show fields in short code to filter the registration form #Issue 740
    if (isset($show_fields)) {
        $package = isset($args['package']) ? $args['package'] : NULL;
        $args_fields = $show_fields;
        if (!empty($args_fields)) {
            $cf_register_page = mgm_show_fields_result($args_fields, $cf_register_page, $package);
        }
    }
    // #740 ends
    $check = 0;
    if (isset($_POST['mgm_widget_active'])) {
        if (isset($_POST['mgm_custom_fields']) && $_POST['mgm_custom_fields'] == 1) {
            $check = $_POST['mgm_custom_fields'];
        }
    } else {
        $check = 1;
    }
    // wordpress register
    $wordpres_form = mgm_check_wordpress_login();
    // check
    if ($check) {
        // loop
        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 are required
                if (!in_array($field['name'], array('subscription_options', 'payment_gateways'))) {
                    continue;
                }
            }
            // skip default fields, validated already
            if (in_array($field['name'], array('username', 'email'))) {
                continue;
            }
            // by name
            switch ($field['name']) {
                case 'terms_conditions':
                    // terms & conditions
                    if (!isset($_POST['mgm_tos']) || empty($_POST['mgm_tos'])) {
                        $errors->add('mgm_tos', __('<strong>ERROR</strong>: You must accept the Terms and Conditions.', 'mgm'));
                    }
                    break;
                case 'subscription_options':
                    // subscription options
                    if (!isset($_POST['mgm_subscription']) || empty($_POST['mgm_subscription'])) {
                        $errors->add('mgm_subscription', __('<strong>ERROR</strong>: You must select a Subscription Type.', 'mgm'));
                    }
                    break;
                case 'payment_gateways':
                    // payment gateways
                    if (isset($_POST['mgm_subscription'])) {
                        // pack
                        $sub_pack = mgm_decode_package($_POST['mgm_subscription']);
                        // check
                        if (isset($sub_pack['pack_id'])) {
                            $pack = mgm_get_class('subscription_packs')->get_pack($sub_pack['pack_id']);
                            $pack_modules = array_diff($pack['modules'], array('mgm_free', 'mgm_trial'));
                            // take paid module
                            // validate
                            if (!empty($pack_modules) && (!isset($_POST['mgm_payment_gateways']) || isset($_POST['mgm_payment_gateways']) && empty($_POST['mgm_payment_gateways']))) {
                                $errors->add('mgm_subscription', __('<strong>ERROR</strong>: You must select a Payment Gateway.', 'mgm'));
                            }
                        }
                    }
                    break;
                case 'coupon':
                    if (isset($_POST['mgm_register_field']['coupon']) && !empty($_POST['mgm_register_field']['coupon'])) {
                        // coupon
                        if ($coupon_code = trim($_POST['mgm_register_field']['coupon'])) {
                            // check if its a valid coupon
                            if (!($coupon = mgm_get_coupon_data($coupon_code))) {
                                $errors->add('mgm_coupon', sprintf(__('<strong>ERROR</strong>: Coupon Code "%s" is not valid, use a valid coupon only.', 'mgm'), $coupon_code));
                            } else {
                                // get subs
                                if ($subs_pack = mgm_decode_package(mgm_post_var('mgm_subscription'))) {
                                    // values
                                    $coupon_values = mgm_get_coupon_values(NULL, $coupon['value'], true);
                                    // check
                                    if (isset($coupon_values['new_membership_type']) && $coupon_values['new_membership_type'] != $subs_pack['membership_type']) {
                                        $new_membership_type = mgm_get_membership_type_name($coupon_values['new_membership_type']);
                                        $errors->add('mgm_coupon', sprintf(__('<strong>ERROR</strong>: Coupon Code "%s" is only available with Membership Type "%s".', 'mgm'), $coupon_code, $new_membership_type));
                                    }
                                }
                            }
                        }
                    } elseif ((bool) $field['attributes']['required'] === true) {
                        $errors->add('mgm_coupon', sprintf(__('<strong>ERROR</strong>: Please enter a valid coupon code.', 'mgm')));
                    }
                    break;
                case 'birthdate':
                    // validate age
                    if (isset($_POST['mgm_register_field'][$field['name']]) && !empty($_POST['mgm_register_field'][$field['name']])) {
                        // format
                        $short_format = mgm_get_date_format('date_format_short');
                        // date
                        $birthdate = mgm_format_inputdate_to_mysql($_POST['mgm_register_field'][$field['name']], $short_format);
                        // current date
                        $current_date = mgm_get_current_datetime('Y-m-d H:i:s');
                        // add
                        if (strtotime($birthdate) > $current_date['timestamp']) {
                            $errors->add($field['name'], __('<strong>ERROR</strong>: Birthdate should not be in future.', 'mgm'));
                        } else {
                            if (isset($field['attributes']['verify_age'])) {
                                // age
                                $unit = (int) $field['attributes']['verify_age_unit'];
                                $period = $field['attributes']['verify_age_period'];
                                // check
                                if ($field['attributes']['verify_age'] == 1 && (int) $field['attributes']['verify_age_unit'] > 0) {
                                    // verify_age_period
                                    $birthdate_should = strtotime(sprintf('-%d %s', $unit, $period), $current_date['timestamp']);
                                    if (strtotime($birthdate) > $birthdate_should) {
                                        $errors->add($field['name'], sprintf(__('<strong>ERROR</strong>: Birthdate should be on or before %s.', 'mgm'), date($short_format, $birthdate_should)));
                                    }
                                }
                            }
                        }
                    }
                    // left other process run
                // left other process run
                default:
                    // on type
                    switch ($field['type']) {
                        case 'captcha':
                            // captcha
                            if (!isset($_POST['recaptcha_response_field']) || empty($_POST['recaptcha_response_field'])) {
                                $errors->add('mgm_captcha', __('<strong>ERROR</strong>: You must enter the Captcha String.', 'mgm'));
                            } else {
                                $captcha = mgm_get_class('recaptcha')->recaptcha_check_answer($_POST['recaptcha_challenge_field'], $_POST['recaptcha_response_field']);
                                if (!isset($captcha->is_valid) || !$captcha->is_valid) {
                                    $errors->add('mgm_captcha', __('<strong>ERROR</strong>: ' . (!empty($captcha->error) ? $captcha->error : 'The Captcha String isn\'t correct.'), 'mgm'));
                                }
                            }
                            break;
                        default:
                            // check register and required
                            if ((bool) $field['attributes']['required'] === true) {
                                // error
                                $error_codes = $errors->get_error_codes();
                                // validate other
                                // confirm password
                                if ($field['name'] == 'password' || $field['name'] == 'password_conf') {
                                    if ($field['name'] == 'password' && (!isset($_POST['user_password']) || empty($_POST['user_password'])) || $field['name'] == 'password_conf' && (!isset($_POST['user_password_conf']) || empty($_POST['user_password_conf']))) {
                                        // issue #703
                                        $errors->add($field['name'], __('<strong>ERROR</strong>: You must provide a ', 'mgm') . mgm_stripslashes_deep($field['label']) . '.');
                                    } elseif ($field['name'] == 'password' && !empty($_POST['user_password']) && !empty($_POST['user_password_conf']) && (isset($field['attributes']['password_min_length']) && $field['attributes']['password_min_length'] == true || isset($field['attributes']['password_min_length']) && $field['attributes']['password_max_length'] == true)) {
                                        if (strlen($_POST['user_password']) < $field['attributes']['password_min_length_field_alias'] || strlen($_POST['user_password_conf']) < $field['attributes']['password_min_length_field_alias']) {
                                            $errors->add($field['name'], sprintf(__('<strong>ERROR</strong>:%s is too short, minimum %d characters.', 'mgm'), mgm_stripslashes_deep($field['label']), $field['attributes']['password_min_length_field_alias']));
                                        } elseif (strlen($_POST['user_password']) > $field['attributes']['password_max_length_field_alias'] || strlen($_POST['user_password_conf']) > $field['attributes']['password_max_length_field_alias']) {
                                            $errors->add($field['name'], sprintf(__('<strong>ERROR</strong>:%s is too long, maximum %d characters.', 'mgm'), mgm_stripslashes_deep($field['label']), $field['attributes']['password_max_length_field_alias']));
                                        } elseif ($field['name'] == 'password' && !empty($_POST['user_password']) && !empty($_POST['user_password_conf']) && $_POST['user_password'] != $_POST['user_password_conf']) {
                                            $errors->add($field['name'], __('<strong>ERROR</strong>: Password does not match. Please re-type.', 'mgm'));
                                        }
                                    } elseif ($field['name'] == 'password' && !empty($_POST['user_password']) && !empty($_POST['user_password_conf']) && $_POST['user_password'] != $_POST['user_password_conf']) {
                                        $errors->add($field['name'], __('<strong>ERROR</strong>: Password does not match. Please re-type.', 'mgm'));
                                    }
                                } else {
                                    //issue #1315
                                    if ($field['name'] == 'user_email' || $field['name'] == 'email_conf') {
                                        if ($field['name'] == 'email_conf' && (!isset($_POST['user_email_conf']) || empty($_POST['user_email_conf']))) {
                                            $errors->add($field['name'], __('<strong>ERROR</strong>: Please type your confirm e-mail address.', 'mgm'));
                                        } elseif (!is_email($_POST['user_email_conf'])) {
                                            $errors->add('invalid_email_conf', __('<strong>ERROR</strong>: The confirm email address isn&#8217;t correct.', 'mgm'));
                                        } elseif (email_exists($_POST['user_email_conf'])) {
                                            $errors->add('email_conf_exists', __('<strong>ERROR</strong>: This confirm email is already registered, please choose another one.', 'mgm'));
                                        } elseif (is_email($_POST['user_email']) && $_POST['user_email_conf'] != $_POST['user_email']) {
                                            $errors->add($field['name'], __('<strong>ERROR</strong>: E-mail does not match. Please re-type.', 'mgm'));
                                        }
                                    } elseif (!isset($_POST['mgm_register_field'][$field['name']]) || empty($_POST['mgm_register_field'][$field['name']])) {
                                        //issue #703
                                        $errors->add($field['name'], __('<strong>ERROR</strong>: You must provide a ', 'mgm') . mgm_stripslashes_deep($field['label']) . '.');
                                    }
                                }
                            }
                            break;
                    }
                    break;
            }
        }
    }
    // return
    return $errors;
}