/**
 * get payment processed page html
 *
 * @param void
 * @return string
 * @since 1.5
 */
function mgm_get_payment_processed_page_html()
{
    // home url
    $home_url = trailingslashit(get_option('siteurl'));
    // current module
    $module = mgm_request_var('module', '', true);
    // check
    if (!mgm_is_valid_module($module) || empty($module)) {
        // redirect
        mgm_redirect($home_url);
    }
    // init
    $html = '';
    // refresh wait time
    $refresh_wait_time = 5;
    //in seconds
    // redirect url
    $redirect_url = '';
    // redirect
    $do_redirect = true;
    // refresh header for post redirecr
    if (isset($_GET['post_redirect'])) {
        // redirect url
        $redirect_url = strip_tags($_GET['post_redirect']);
    } elseif (isset($_GET['register_redirect'])) {
        // redirect url, if 1/true, redirect to profile, else its register & redirect url
        if ($_GET['register_redirect'] != 1) {
            $redirect_url = strip_tags($_GET['register_redirect']);
        } else {
            // auto login
            $system_obj = mgm_get_class('system');
            //issue# 1392
            $current_user_id = get_current_user_id();
            // check if set
            if ($autologin_redirect_url = $system_obj->get_setting('autologin_redirect_url')) {
                $page_title = '';
                $redirect_url = $autologin_redirect_url;
                //short code support
                if (!empty($current_user_id)) {
                    $user = get_userdata($current_user_id);
                    $redirect_url = str_replace('[username]', $user->user_login, $redirect_url);
                }
            } elseif (mgm_get_user_package_redirect_url($current_user_id) && $current_user_id) {
                $page_title = '';
                $redirect_url = mgm_get_user_package_redirect_url($current_user_id);
            } else {
                $page_title = 'Profile';
                $redirect_url = mgm_get_custom_url('profile');
            }
        }
        // check not logged in, #948 paypal fails to redirect
        if (!is_user_logged_in()) {
            // user login
            if (isset($_GET['trans_ref'])) {
                // re construct redirect url
                $redirect_url = mgm_get_custom_url('login', false, array('trans_ref' => strip_tags($_GET['trans_ref']), 'auto_login' => true, 'redirect_to' => $redirect_url));
            }
        }
    }
    // check and set
    if (!empty($redirect_url) && $do_redirect) {
        // alter
        $redirect_url = apply_filters('mgm_register_redirect', $redirect_url);
        // no headers
        if (!headers_sent()) {
            @header(sprintf('Refresh: %d;url=%s', $refresh_wait_time, $redirect_url));
        } else {
            $html .= sprintf('<script language="javascript">window.setTimeout(function(){window.location.href="%s";}, %d)</script>', $redirect_url, (int) $refresh_wait_time * 5);
        }
    }
    // module object
    $module_object = mgm_get_module($module, 'payment');
    // [domain]/subscribe/?method=payment_processed&module=mgm_paypal&status=success
    // [domain]/subscribe/?method=payment_processed&module=mgm_paypal&status=cancel
    // status and message
    $arr_shortcodes = array('transaction_amount' => '');
    // check
    if (!isset($_GET['status']) || $_GET['status'] == 'success') {
        // mgm_replace_oldlinks_with_tag is a patch for replacing the old link
        $message = $module_object->setting['success_message'] ? mgm_replace_oldlinks_with_tag($module_object->setting['success_message'], 'payment_success_message') : $system_obj->get_template('payment_success_message', array(), true);
        // get price
        if (isset($_GET['trans_ref'])) {
            // tarns
            $_GET['trans_ref'] = mgm_decode_id(strip_tags($_GET['trans_ref']));
            // get transaction data
            $trans = mgm_get_transaction($_GET['trans_ref']);
            // set amount
            if ($trans['module'] == 'manualpay') {
                $arr_shortcodes['transaction_amount'] = $trans['data']['cost'] . ' ' . $trans['data']['currency'];
            }
            // update googe analytics:
            $html .= apply_filters('mgm_payment_processed_page_analytics', $trans);
            // @todo, callback in template function
            // mgm_update_google_analytics($trans);	deprecated, use hook
        }
    } else {
        if (!isset($_GET['status']) || $_GET['status'] == 'cancel') {
            // set message
            $message = __('You have cancelled the transaction.', 'mgm');
        } else {
            // mgm_replace_oldlinks_with_tag is a patch for replacing the old link
            $message = $module_object->setting['failed_message'] ? mgm_replace_oldlinks_with_tag($module_object->setting['failed_message'], 'payment_failed_message') : $system_obj->get_template('payment_failed_message', array(), true);
        }
    }
    // parse short codes:
    // [transaction_amount] = amount paid
    foreach ($arr_shortcodes as $code => $value) {
        $message = str_replace('[' . $code . ']', $value, $message);
    }
    // html
    $html .= mgm_stripslashes_deep(mgm_get_message_template($message));
    // get error
    if (isset($_GET['errors'])) {
        // get errors
        $errors = explode('|', strip_tags($_GET['errors']));
        // html
        $html .= sprintf('<h3> %s </h3><div><ul>', __('Messages', 'mgm'));
        // loop
        foreach ($errors as $error) {
            $html .= sprintf('<li> %s </li>', $error);
        }
        // end
        $html .= '</ul></div>';
    }
    // auto redirect to post purchased
    if (isset($_GET['post_redirect'])) {
        // message
        $m = sprintf(__('You will be automatically redirected to the post you purchased within %d seconds. Please <a href="%s"> click here </a> to go to the page. ', 'mgm'), $refresh_wait_time, strip_tags($_GET['post_redirect']));
        // set
        $html .= sprintf('<b>%s</b>', $m);
    } elseif (isset($_GET['register_redirect'])) {
        // auto login redirect
        // message
        $m = sprintf(__('You will be automatically redirected to your %s page within %d seconds. Please <a href="%s"> click here </a> to go to the page. ', 'mgm'), $_GET['register_redirect'] == 1 ? __($page_title, 'mgm') : __('Post', 'mgm'), $refresh_wait_time, $redirect_url);
        // set
        $html .= sprintf('<b>%s</b>', $m);
    }
    // return
    return apply_filters('mgm_payment_processed_page_html', $html);
}
/**
 * get template
 *
 */
function mgm_get_template($name, $data = array(), $type = 'messages')
{
    global $wpdb;
    // check from db first
    if (mgm_is_activated()) {
        $content = $wpdb->get_var($wpdb->prepare("SELECT `content` FROM `" . TBL_MGM_TEMPLATE . "` WHERE `name`=%s AND `type`=%s", $name, $type));
    }
    // not in db
    if (!isset($content) || isset($content) && empty($content)) {
        // check old content
        $content = mgm_get_old_template_content($name);
        // stil empty, take from file
        if (empty($content)) {
            // template file
            $template_file = MGM_CORE_DIR . MGM_DS . 'html' . MGM_DS . $type . MGM_DS . $name . '.html';
            // get content
            if (file_exists($template_file)) {
                $content = @file_get_contents($template_file);
            }
        }
        // insert
        if ($content) {
            // strp first
            $content = mgm_stripslashes_deep($content);
            // and update database
            if (mgm_is_activated()) {
                $wpdb->insert(TBL_MGM_TEMPLATE, array('name' => $name, 'type' => $type, 'content' => addslashes($content), 'create_dt' => date('Y-m-d H:i:s')));
            }
        }
    }
    // check template parser
    if ($content) {
        //patch to update old users message:
        $content = mgm_replace_oldlinks_with_tag($content, $name);
        // check
        if (is_array($data)) {
            foreach ($data as $key => $value) {
                $content = str_replace('[' . $key . ']', $value, $content);
            }
        }
        // return
        return mgm_stripslashes_deep($content);
    }
    // return
    return '';
}