<?php

/** 
 * Apply patch
 */
// get login_errmsg_date_range
$login_errmsg_date_range = mgm_get_template('login_errmsg_date_range', NULL, 'messages');
// append new if not added
if (!preg_match('/\\[subscription_url\\]/', $login_errmsg_date_range)) {
    // add
    $login_errmsg_date_range .= '<br /> Please make a new <a href=\\"[subscription_url]\\">subscription payment</a> to re-activate your account.';
    // update
    mgm_update_template('login_errmsg_date_range', $login_errmsg_date_range, 'messages');
}
// end of file
<?php

/** 
 * Apply patch
 */
// get login_errmsg_cancelled
$login_errmsg_cancelled = mgm_get_template('login_errmsg_cancelled', NULL, 'messages');
// append new if not added
if (!preg_match('/\\[subscription_url\\]/', $login_errmsg_cancelled)) {
    // add
    $login_errmsg_cancelled .= '<br /> Please make a new <a href=\\"[subscription_url]\\">subscription payment</a> to re-activate your account.';
    // update
    mgm_update_template('login_errmsg_cancelled', $login_errmsg_cancelled, 'messages');
}
// end of file
 function get_template($name, $data = array(), $parse = false)
 {
     // by name
     switch ($name) {
         case 'tos':
         case 'subs_intro':
         case 'text_guest_purchase_pre_button':
         case 'text_guest_purchase_pre_register':
             return mgm_get_template($name, NULL, 'messages');
             break;
         case 'private_text':
         case 'private_text_no_access':
         case 'private_text_purchasable':
         case 'private_text_purchasable_login':
         case 'private_text_purchasable_pack_login':
         case 'private_text_postdelay_no_access':
             // parse enabled
             if ($parse) {
                 // parse
                 $message_content = mgm_get_template($name, $data, 'messages');
                 // set template
                 $template = mgm_get_template('private_text_template', NULL, 'templates');
                 // return
                 return str_replace('[message]', $message_content, $template);
             } else {
                 // parse disabled
                 return mgm_get_template($name, NULL, 'messages');
             }
             break;
         case 'login_errmsg_null':
         case 'login_errmsg_expired':
         case 'login_errmsg_trial_expired':
         case 'login_errmsg_pending':
         case 'login_errmsg_cancelled':
         case 'login_errmsg_default':
         case 'login_errmsg_date_range':
         case 'login_errmsg_multiple_logins':
             // parse enabled
             if ($parse) {
                 // argas
                 $q_args = array('action' => '[[ACTION]]');
                 //
                 if (bool_from_yn($this->setting['enable_email_as_username'])) {
                     $q_args = array_merge($q_args, array('user_id' => '[[USERID]]'));
                 } else {
                     $q_args = array_merge($q_args, array('username' => '[[USERNAME]]'));
                 }
                 // set url data
                 $data['subscription_url'] = add_query_arg($q_args, mgm_get_custom_url('transactions'));
                 // return
                 return mgm_get_template($name, $data, 'messages');
             } else {
                 // parse disabled
                 return mgm_get_template($name, NULL, 'messages');
             }
             break;
         case 'pack_desc_template':
         case 'pack_desc_lifetime_template':
         case 'pack_desc_date_range_template':
         case 'ppp_pack_template':
         case 'register_form_row_template':
         case 'profile_form_row_template':
         case 'register_form_row_autoresponder_template':
             // separate
             // parse enabled
             if ($parse) {
                 // return
                 return mgm_get_template($name, $data, 'templates');
             } else {
                 // parse disabled
                 return mgm_get_template($name, NULL, 'templates');
             }
             break;
         case 'reminder_email_template_subject':
         case 'reminder_email_template_body':
         case 'registration_email_template_subject':
         case 'registration_email_template_body':
         case 'new_user_notification_email_template_subject':
         case 'new_user_notification_email_template_body':
         case 'user_upgrade_notification_email_template_subject':
         case 'user_upgrade_notification_email_template_body':
         case 'payment_success_email_template_subject':
         case 'payment_success_email_template_body':
         case 'payment_success_subscription_email_template_body':
         case 'payment_failed_email_template_subject':
         case 'payment_failed_email_template_body':
         case 'payment_active_email_template_subject':
         case 'payment_active_email_template_body':
         case 'payment_pending_email_template_subject':
         case 'payment_pending_email_template_body':
         case 'payment_error_email_template_subject':
         case 'payment_error_email_template_body':
         case 'payment_unknown_email_template_subject':
         case 'payment_unknown_email_template_body':
         case 'subscription_cancelled_email_template_subject':
         case 'subscription_cancelled_email_template_body':
         case 'retrieve_password_email_template_subject':
         case 'retrieve_password_email_template_body':
         case 'lost_password_email_template_subject':
         case 'lost_password_email_template_body':
             // parse enabled
             if ($parse) {
                 return mgm_get_template($name, $data, 'emails');
             } else {
                 // parse disabled
                 return mgm_get_template($name, NULL, 'emails');
             }
             break;
         case 'payment_success_title':
         case 'payment_success_message':
         case 'payment_failed_title':
         case 'payment_failed_message':
             // parse enabled
             if ($parse) {
                 // set urls
                 $data['home_url'] = trailingslashit(get_option('siteurl'));
                 $data['site_url'] = trailingslashit(site_url());
                 $data['register_url'] = trailingslashit(mgm_get_custom_url('register'));
                 // login or profile
                 $data['login_url'] = trailingslashit(mgm_get_custom_url(is_user_logged_in() ? 'profile' : 'login'));
                 // return
                 return mgm_get_template($name, $data, 'messages');
             } else {
                 // parse disabled
                 return mgm_get_template($name, NULL, 'messages');
             }
             break;
         default:
             return sprintf(__('%s not defined.', 'mgm'), $name);
             break;
     }
 }
/**
 * parse and output post buy button
 *
 * @since 2.0
 *
 * @param mixed $post_id
 * @return string $html
 */
function mgm_parse_post_template($post_id = null)
{
    global $wpdb;
    //issue #1397
    if (!isset($post_id) || $post_id == null || empty($post_id)) {
        $post_id = get_the_ID();
    }
    // post
    $post = get_post($post_id);
    // issue #1397
    // post title
    // $post_title = sprintf('<ul><li><a href="%s">%s</a></li></ul>', get_permalink($post_id), $post->post_title);
    // logged in
    if (is_user_logged_in()) {
        // return
        //return sprintf('<div>%s</div>%s', $post->post_title, mgm_get_post_purchase_button($post_id));
        //issue #1706
        return sprintf('%s', mgm_get_post_purchase_button($post_id));
    }
    // login url
    $login_url = $post->ID > 0 ? mgm_get_custom_url('login', false, array('redirect_to' => get_permalink($post->ID))) : '';
    // template
    $template = mgm_get_template('private_text_template', array(), 'templates');
    // message
    $message = mgm_get_template('private_text_purchasable_login', array(), 'messages');
    //issue #863
    $message = str_replace('[purchase_options]', '', $message);
    $message = __($message, 'mgm');
    $message .= '[purchase_options]';
    // message
    $message = mgm_get_content_purchase_options($message, $login_url, $post_id);
    // message
    $message = str_replace('[message]', $message, $template);
    // return
    //return sprintf('<div>%s</div><div class="mgm_private_no_access">%s</div>', $post->post_title, $message);
    //issue #1706
    return sprintf('<div class="mgm_private_no_access">%s</div>', $message);
}
function mgm_user_payment_history()
{
    global $user_ID, $current_user, $wpdb;
    $html = '';
    $css_group = mgm_get_css_group();
    //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.pages.css';
        $html .= sprintf($css_link_format, $css_file);
    }
    $data = array();
    if ($user_ID) {
        //$user_ID =258;
        $pattern_one = '"user_id":' . $user_ID . ',';
        $pattern_two = '"user_id":"' . $user_ID . '",';
        //payment success check
        $pay_succ = " AND `status_text` =  'Last payment was successful' ";
        $user_check = "(`data` LIKE '%" . $pattern_one . "%' OR `data` LIKE '%" . $pattern_two . "%')";
        $sql = "SELECT SQL_CALC_FOUND_ROWS * FROM `" . TBL_MGM_TRANSACTION . "` WHERE `module` IS NOT NULL AND (" . $user_check . ") " . $pay_succ . " ORDER BY `transaction_dt` DESC";
        $data['transactions'] = $wpdb->get_results($sql);
        $html .= '<div class="postbox mgm_margin10px0px"><h4>' . __("Payment History", "mgm") . '</h4></div>';
        $html .= '<div>';
        $html .= '<div class="table mgm_payment_history_container">
			<div class="row br_bottom">
				<div class="cell th_div width50px maxwidth50px mgm_text_align_left"><b>' . __("S.No", "mgm") . '</b></div>
				<div class="cell th_div width140px maxwidth140px mgm_text_align_left"><b>' . __("Type", "mgm") . '</b></div>
				<div class="cell th_div width140px maxwidth140px mgm_text_align_left"><b>' . __("Module", "mgm") . '</b></div>
				<div class="cell th_div width100px maxwidth100px mgm_text_align_left"><b>' . __("Amount", "mgm") . '</b></div>					
				<div class="cell th_div width100px maxwidth100px mgm_text_align_left"><b>' . __("Transaction Date", "mgm") . '</b></div>
			</div>';
        if (count($data['transactions']) > 0) {
            $i = 1;
            foreach ($data['transactions'] as $tran_log) {
                $html .= '<div class="row br_bottom">';
                $json_decoded = json_decode($tran_log->data);
                //$user_obj = $data[$json_decoded->user_id];
                //echo $user_obj->user_login;
                $html .= '
					<div class="cell width50px maxwidth50px mgm_text_align_left">' . ucwords($i++) . ' </div>
			   		<div class="cell width140px maxwidth140px mgm_text_align_left">' . ucwords(str_replace("_", " ", $tran_log->payment_type)) . ' </div>
			   		<div class="cell width140px maxwidth140px mgm_text_align_left">' . ucwords($tran_log->module) . '</div>
			   		<div class="cell width100px maxwidth100px mgm_text_align_left">' . $json_decoded->cost . ' </div>
			   		<div class="cell width100px maxwidth100px mgm_text_align_left">' . date(MGM_DATE_FORMAT_SHORT, strtotime($tran_log->transaction_dt)) . '  </div>
					</div> ';
            }
        } else {
            $html .= '<div class="row br_bottom"><div class="cell mgm_text_align_center">' . __("No transactions found..!", "mgm") . '</div></div>';
        }
        $html .= '</div>';
        $html .= '</div>';
    } else {
        $template = mgm_get_template('private_text_template', array(), 'templates');
        $html = 'You need to be logged in to access this page.';
        $html .= sprintf(__(' Please <a href="%s"><b>login</b> here.</a>', 'mgm'), mgm_get_custom_url('login', false, array('redirect_to' => get_permalink($post->ID))));
        $html = str_replace('[message]', $html, $template);
    }
    $html = apply_filters('mgm_user_payment_history_html', $html);
    return $html;
}
/**
 * print template content
 *
 */
function mgm_print_template_content($template, $type = 'messages')
{
    // return
    return mgm_get_template($template, NULL, $type);
}