/**
 * parse shortcodes
 *
 * @param array @args
 * @param string @content
 * @param string @tag
 * @return string $content
 */
function mgm_shortcode_parse($args, $content, $tag)
{
    // current_user
    $current_user = wp_get_current_user();
    // system
    $system_obj = mgm_get_class('system');
    // issue#: 859
    // add <p> to the beggining and </p> to the end of content
    // as WP pass $content with incomplete p tags
    $content = '<p>' . $content . '</p>';
    // remove any '<p></p> found
    $content = str_replace(array('<p></p>'), '', $content);
    // @todo test with force_balance_tags();
    // tag block
    switch ($tag) {
        case 'private':
            // [private] protected content [/private]
            if (mgm_protect_content() || mgm_post_is_purchasable()) {
                //issue #1687
                if (mgm_content_post_access_delay($args)) {
                    $content = mgm_replace_postdealy_content($content);
                } else {
                    $content = mgm_replace_content_tags($tag, $content, $args);
                }
            }
            break;
        case 'private_or':
            // [private_or#member] protected content [/private_or]
            // [private_or membership_type="member"] protected content [/private_or]
            $membership_type = isset($args['membership_type']) ? $args['membership_type'] : str_replace('#', '', mgm_array_shift($args));
            // match
            if ($membership_type) {
                $content = mgm_replace_content_tags($tag, $content, $membership_type);
            }
            break;
        case 'private_and':
            // [private_and#member] protected content [/private_and]
            // [private_and membership_type="member"] protected content [/private_and]
            $membership_type = isset($args['membership_type']) ? $args['membership_type'] : str_replace('#', '', mgm_array_shift($args));
            // match
            if ($membership_type) {
                $content = mgm_replace_content_tags($tag, $content, $membership_type);
            }
            break;
        case 'payperpost_pack':
            // [payperpost_pack#1] : 1 = pack_id, packs to be created in MGM -> PayPerPost -> Post Packs, use the id here
            // [payperpost_pack id=1] : 1 = pack_id
            $pack_id = isset($args['id']) ? $args['id'] : str_replace('#', '', mgm_array_shift($args));
            // match
            if ($pack_id) {
                $content = mgm_replace_content_tags($tag, $content, $pack_id);
            }
            break;
        case 'payperpost':
            // [payperpost#1] : 1 = post_id
            // [payperpost id=1] : 1 = post_id
            $pack_id = isset($args['id']) ? $args['id'] : str_replace('#', '', mgm_array_shift($args));
            // match
            if ($pack_id) {
                $content = mgm_replace_content_tags($tag, $content, $pack_id);
            }
            break;
        case 'subscription_packs':
            // subscription packs / payment gateways
            $content = mgm_sidebar_register_links($current_user->user_login, true, 'page');
            // @todo test
            break;
        case 'user_unsubscribe':
            // user unsubscribe
            $content = mgm_user_unsubscribe_info(null, $args);
            // view current user
            break;
        case 'user_other_subscriptions':
            // other subscriptions
            $content = mgm_user_other_subscriptions_info();
            break;
        case 'membership_details':
            // user subscription
            $content = mgm_membership_details();
            // view current user
            break;
        case 'user_upgrade':
            // user upgrade membership
            $content = mgm_get_upgrade_buttons($args);
            break;
        case 'user_purchase_another_membership':
            // purchase another subscription
            $content = mgm_get_purchase_another_subscription_button($args);
            break;
        case 'user_subscribe':
        case 'user_register':
            // named
            if ($method = mgm_get_var('method', '', true)) {
                // method
                switch ($method) {
                    case 'login':
                        $content = mgm_user_login_form(false);
                        break;
                    case 'lostpassword':
                        $content = mgm_user_lostpassword_form(false);
                        break;
                    default:
                        if (preg_match('/^payment/', $method)) {
                            $content = mgm_transactions_page($args);
                        }
                        break;
                }
            } else {
                $content = mgm_user_register_form($args);
            }
            break;
        case 'user_profile':
            // user profile
            $content = mgm_user_profile_form(NULL, false, $args);
            // view
            break;
        case 'user_public_profile':
            // user profile
            $content = mgm_user_public_profile($args);
            // view
            break;
        case 'transactions':
            // user payments/transactions
            $content = mgm_transactions_page($args);
            break;
        case 'user_contents_by_membership':
            // user contents by membership level
            $content = mgm_membership_content_page();
            break;
        case 'user_lostpassword':
            // user lost password form
            $content = mgm_user_lostpassword_form(false);
            break;
        case 'user_login':
            // user login form
            $content = mgm_user_login_form(false);
            break;
        case 'user_field':
            // user field
            $content = __('Experimental', 'mgm');
            break;
        case 'membership_contents':
            // membership contents
            $content = mgm_membership_contents();
            // view current user
            break;
        case 'logout_link':
            // custom logout link
            // [logout_link#Logout]
            // [logout_link label="Logout"]
            $label = isset($args['label']) ? $args['label'] : str_replace('#', '', mgm_array_shift($args));
            // match
            $content = mgm_logout_link($label);
            break;
        case 'membership_extend_link':
            //INCOMPLETE
            // membership extend link
            // [membership_extend_link#Extend]
            // [membership_extend_link label="Extend"]
            $label = isset($args['label']) ? $args['label'] : str_replace('#', '', mgm_array_shift($args));
            // match
            $content = mgm_membership_extend_link($label);
            break;
        case 'download_error':
            // content
            $content = isset($_GET['error_code']) ? mgm_download_error($_GET['error_code']) : '';
            break;
        case 'user_payment_history':
            // content
            $content = mgm_user_payment_history();
            // view current user
            break;
        case 'user_list':
            // content
            $content = mgm_generate_member_list($args);
            break;
        case 'user_facebook_login':
            // content
            $content = mgm_generate_facebook_login();
            break;
        case 'user_facebook_registration':
            // content
            $content = mgm_generate_facebook_registration();
            break;
        case 'user_purchased_contents':
            // content
            $content = mgm_generate_purchased_contents();
            break;
        case 'user_purchasable_contents':
            // content
            $content = mgm_generate_purchasable_contents();
            break;
            /*case 'addon':
            			// content
             			$content = mgm_purchase_addons($args);
            		break;*/
        /*case 'addon':
        			// content
         			$content = mgm_purchase_addons($args);
        		break;*/
        default:
            // default, which are not shortcode but content tags
            $args = str_replace('#', '', mgm_array_shift($args));
            // match
            $content = mgm_replace_content_tags($tag, $content, $args);
            break;
    }
    // return
    return $content;
}
/**
 * user subscription
 */
function mgm_user_subscription_info($user_id = NULL, $args = array())
{
    // current user
    if (!$user_id) {
        $user = wp_get_current_user();
    } else {
        // by user id
        $user = get_userdata($user_id);
    }
    // return when no user
    if (!isset($user->ID) || isset($user->ID) && (int) $user->ID == 0) {
        return sprintf(__('Please <a href="%s">login</a> to see your subscriptions.', 'mgm'), mgm_get_custom_url('login'));
    }
    // settings
    $settings = mgm_get_class('system')->get_setting();
    // packs
    $subscription_packs = mgm_get_class('subscription_packs');
    $duration_str = $subscription_packs->duration_str;
    //issue #946
    $duration_str_plu = $subscription_packs->duration_str_plu;
    // member
    $member = mgm_get_member($user->ID);
    //mgm_pr($member);
    // pack
    $pack_id = $member->pack_id;
    $pack = $subscription_packs->get_pack($pack_id);
    //mgm_pr($pack);
    $extend_link = '';
    $subs_package = 'N/A';
    // allow renewal
    if ($pack) {
        // dsc
        $subs_package = $pack['description'];
        //issue#: 478
        $num_cycles = isset($member->active_num_cycles) && !empty($member->active_num_cycles) ? $member->active_num_cycles : $pack['num_cycles'];
        // check cycles
        if ($num_cycles > 0 && mgm_pack_extend_allowed($pack)) {
            $extend_link = ' (<a href="' . mgm_get_custom_url('transactions', false, array('action' => 'extend', 'pack_id' => $pack_id, 'username' => $user->user_login)) . '">' . __('Extend', 'mgm') . '</a>)';
        }
    }
    // set others
    $sformat = mgm_get_date_format('date_format_short');
    //issue #946
    // dur, #1452
    if ($member->trial_on) {
        $durstr = $member->trial_duration == 1 ? $duration_str[$member->trial_duration_type] : $duration_str_plu[$member->trial_duration_type];
        $durstr .= ' ( ' . __('Trial', 'mgm') . ' )';
    } else {
        $durstr = $member->duration == 1 ? $duration_str[$member->duration_type] : $duration_str_plu[$member->duration_type];
    }
    // $durstr   = ($member->duration == 1) ? rtrim($duration_str[$member->duration_type]) : $duration_str[$member->duration_type];
    $amount = is_numeric($member->amount) ? sprintf(__('%1$s %2$s', 'mgm'), number_format($member->amount, 2, '.', null), $user->currency) : 'N/A';
    $last_pay = $member->last_pay_date ? date($sformat, strtotime($member->last_pay_date)) : 'N/A';
    $expiry = $member->expire_date ? date($sformat, strtotime($member->expire_date)) : 'N/A';
    //issue #946
    // #1452
    if ($member->trial_on) {
        $duration = $member->trial_duration ? $member->trial_duration_type == 'l' ? $durstr : $member->trial_duration . ' ' . $durstr : 'N/A';
    } else {
        $duration = $member->duration ? $member->duration_type == 'l' ? $durstr : $member->duration . ' ' . $durstr : 'N/A';
    }
    // $duration = $member->duration ? (($member->duration_type == 'l') ? $durstr : $member->duration . ' ' . $durstr .($member->duration > 1 ? 's' :'')): 'N/A';
    $membership_type = $member->membership_type;
    // init
    $html = '';
    // html
    $html .= '<div class="table width100 br">';
    // row counter
    $row_ctr = 0;
    // row
    $html .= '
		<div class="row alternate br_bottom">
			<div class="cell width25 padding10px">
				<strong>' . __('Access Duration', 'mgm') . '</strong>
			</div>
			<div class="cell width2 padding10px"><strong>:</strong></div>
			<div class="cell width73 padding10px">' . esc_html($duration) . '</div>
		</div>
		<div class="row br_bottom">
			<div class="cell width25 padding10px"><strong>' . __('Last Payment Date', 'mgm') . '</strong></div>
			<div class="cell width2 padding10px" ><strong>:</strong></div>
			<div class="cell width73 padding10px">' . esc_html($last_pay) . '</div>
		</div>';
    // counter
    $row_ctr = 2;
    // duration
    if ($member->duration_type != 'l') {
        $html .= '
			<div class="row alternate br_bottom">
				<div class="cell width25 padding10px"><strong>' . __('Expiry Date', 'mgm') . '</strong></div>
				<div class="cell width2 padding10px"><strong>:</strong></div>
				<div class="cell width73 padding10px">' . esc_html($expiry) . $extend_link . '</div>
			</div>';
        // counter
        $row_ctr++;
    }
    // cost
    $html .= '
		<div class="row ' . ($row_ctr++ % 2 == 0 ? 'alternate' : '') . ' br_bottom">
			<div class="cell width25 padding10px"><strong>' . __('Membership Cost', 'mgm') . '</strong></div>
			<div class="cell width2 padding10px"><strong>:</strong></div>
			<div class="cell width73 padding10px">' . ((is_super_admin() ? 'N/A' : esc_html($amount)) . ' ' . mgm_get_class('system')->setting['currency']) . '</div>
		</div>
		
		<div class="row ' . ($row_ctr++ % 2 == 0 ? 'alternate' : '') . ' br_bottom">
			<div class="cell width25 padding10px"><strong>' . __('Membership Level', 'mgm') . '</strong></div>
			<div class="cell width2 padding10px"><strong>:</strong></div>
			<div class="cell width73 padding10px">' . ((is_super_admin() ? 'N/A' : mgm_stripslashes_deep(esc_html(mgm_get_user_membership_type($user->ID)))) . ' (<a href="' . mgm_get_custom_url('transactions', false, array('action' => 'upgrade', 'username' => $user->user_login)) . '">' . __('Upgrade', 'mgm') . '</a>)') . '</div>
		</div>
				
		<div class="row ' . ($row_ctr++ % 2 == 0 ? 'alternate' : '') . ' br_bottom">
			<div class="cell width25 padding10px"><strong>' . __('Subscribed Package', 'mgm') . '</strong></div>
			<div class="cell width2 padding10px"><strong>:</strong></div>
			<div class="cell width73 padding10px">' . mgm_stripslashes_deep(esc_html($subs_package)) . '</div>
		</div>';
    // append
    if (isset($settings['enable_multiple_level_purchase']) && bool_from_yn($settings['enable_multiple_level_purchase']) && mgm_check_purchasable_level_exists($user->ID, $member)) {
        $html .= '
				<div class="row ' . ($row_ctr++ % 2 == 0 ? '' : 'alternate') . ' br_bottom">
					<div class="cell width25 padding10px"><strong>' . __('Other Membership Level(s)', 'mgm') . '</strong></div>
					<div class="cell width2 padding10px"><strong>:</strong></div>
					<div class="cell width73 padding10px">' . (is_super_admin() ? __('N/A', 'mgm') : '<a href="' . mgm_get_custom_url('transactions', false, array("action" => "purchase_another", "username" => $user->user_login)) . '">' . __('Purchase', 'mgm') . ' </a>') . '</div>
				</div>';
    }
    // end
    $html .= '</div>';
    // init
    $unsubscribe = 0;
    // via short code
    if (!empty($args)) {
        $unsubscribe = isset($args['unsubscribe']) ? $args['unsubscribe'] : str_replace('#', '', mgm_array_shift($args));
    }
    // get button
    if ($unsubscribe == 'unsubscribe') {
        // stat
        $html .= '<br/><div class="table width100">';
        // button
        $html .= mgm_get_unsubscribe_status_button($member, $user);
        // end
        $html .= '</div>';
    }
    // apply filter
    return apply_filters('mgm_user_subscription_html', $html, $user->ID);
}