/**
 * get postpack template
 *
 * @param int $postpack_id
 * @param bool $guest_purchase
 * @return string $template
 */
function mgm_get_postpack_template($postpack_id, $guest_purchase = false, $postpack_post_id = '', $message = 'pre_button')
{
    // current_user
    $current_user = wp_get_current_user();
    // system
    $system_obj = mgm_get_class('system');
    // currency
    $currency = $system_obj->setting['currency'];
    $pack_template = $system_obj->get_template('ppp_pack_template');
    //issue #1177
    $currency_sign = mgm_get_currency_symbols($system_obj->setting['currency']);
    // get pack
    $postpack = mgm_get_postpack($postpack_id);
    // default
    if (!$pack_template) {
        $pack_template = '<div><div><h3>[pack_name] - [pack_cost] [pack_currency]</h3></div><div>[pack_description]</div><div>[pack_posts]</div><div>[pack_buy_button]</div></div>';
    }
    // post
    $post_string = '';
    $cost = mgm_convert_to_currency($postpack->cost);
    $show_button = false;
    // if all posts purchased, dont show button
    // template
    $template = str_replace('[pack_name]', $postpack->name, $pack_template);
    $template = str_replace('[pack_cost]', $cost, $template);
    $template = str_replace('[pack_description]', $postpack->description, $template);
    $template = str_replace('[pack_currency]', $currency, $template);
    //issue #1177
    $template = str_replace('[currency_sign]', $currency_sign, $template);
    // user or guest
    $user_id = isset($current_user->ID) ? $current_user->ID : null;
    // list of posts
    if ($postpack_posts = mgm_get_postpack_posts($postpack_id)) {
        // log
        // mgm_log($postpack_posts, __FUNCTION__);
        // init string
        $post_string = '<ul>';
        // loop
        foreach ($postpack_posts as $i => $pack_post) {
            // check if user has purchased
            $access = mgm_user_has_purchased_post($pack_post->post_id, $user_id);
            // set button mode
            if (!$access) {
                // enable button
                $show_button = true;
            }
            // get post
            $post = get_post($pack_post->post_id);
            // append
            $post_string .= sprintf('<li><a href="%s">%s</a></li>', get_permalink($post->ID), $post->post_title);
        }
        // end
        $post_string .= '</ul>';
    } else {
        $post_string .= __('No posts added to this Pack', 'mgm');
    }
    // display
    $template = str_replace('[pack_posts]', $post_string, $template);
    // get button
    $buy_button = $show_button ? mgm_get_postpack_purchase_button($postpack_id, $guest_purchase, $postpack_posts, $postpack_post_id) : '';
    // is register & purchase postpack
    if ($message == 'pre_register') {
        // template
        return str_replace('[pack_buy_button]', '', $template);
    }
    // template
    return str_replace('[pack_buy_button]', $buy_button, $template);
}
/**
 * Getting member purchasable post packs for current user
 */
function mgm_member_purchasable_postpacks($pagetype = 'admin')
{
    global $wpdb;
    // current_user
    $current_user = wp_get_current_user();
    // snippet
    $snippet_length = 200;
    // purchased
    $purchasable_postpacks = mgm_get_member_postpacks($current_user->ID, 'purchasable');
    // posts
    $postpacks = $purchasable_postpacks['postpacks'];
    // total_posts
    $total_postpacks = $purchasable_postpacks['total_postpacks'];
    // init
    $html = $alt = '';
    // start output
    $html .= '<div class="table width100 br">' . '<div class="row br_bottom">' . '<div class="cell th_div width25 padding10px"><b>' . __('Post Pack Title', 'mgm') . '</b></div>' . '<div class="cell th_div width45 padding10px"><b>' . __('Post Pack Description', 'mgm') . '</b></div>' . '<div class="cell th_div width15 padding10px"><b>' . __('Price', 'mgm') . '</b></div>' . '<div class="cell th_div width15 padding10px"><b></b></div>' . '</div>';
    // check
    $currency = mgm_get_setting('currency');
    // id, name, description,
    if ($total_postpacks > 0) {
        // loop
        foreach ($postpacks as $id => $obj) {
            // set
            $title = $obj->name;
            $content = $obj->description;
            if (function_exists('qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage')) {
                $title = qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage($title);
                $content = qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage($content);
            }
            $content = preg_replace("'\\[/?\\s?private\\s?\\]'i", '', $content);
            $content = preg_replace("/\\[.*?\\]/", '', $content);
            $content = substr(strip_tags($content), 0, $snippet_length);
            $content .= strlen($content) > $snippet_length ? '...' : '';
            $html .= '<div class="row br_bottom ' . ($alt = $alt == '' ? 'alternate' : '') . '">' . '<div class="cell width25 padding10px"><a href="#">' . $title . '</a></div>' . '<div class="cell width45 padding10px">' . $content . '</div>' . '<div class="cell width15 padding10px">' . $obj->cost . ' ' . $currency . '</div>' . '<div class="cell width15 padding10px">' . mgm_get_postpack_purchase_button($obj->id) . '</div>' . '</div>';
        }
    } else {
        $html .= '<div class="row br_bottom' . ($alt = $alt == '' ? 'alternate' : '') . '">' . '<div class="cell mgm_text_align_center">' . __('No purchased post packs', 'mgm') . '</div>' . '</div>';
    }
    $html .= '</div>';
    //return $html;
    if ($total_postpacks > 0) {
        $html .= '<div class="mgm_margin10px">';
        if (isset($_GET['section']) && $_GET['section'] == 'purchasable_postpacks') {
            $html .= '<div class="mgm_content_back_link_div">' . '<a href="' . ($pagetype == 'admin' ? admin_url('profile.php?page=mgm/membership/content') : mgm_get_custom_url('membership_contents')) . '" class="button">' . __('Back', 'mgm') . '</a>' . '</div>';
        }
        $html .= '<div class="mgm_content_total_post_div">' . sprintf(__('You have a total of %d %s you can purchase and access.', 'mgm'), $total_postpacks, $total_postpacks == 1 ? __('Post Pack', 'mgm') : __('Post Packs', 'mgm')) . '</div>';
        $html .= '<div class="mgm_content_total_publish_div">';
        if (isset($_GET['section']) && $_GET['section'] == 'purchasable_postpacks') {
            $html .= '<span class="pager">' . $purchasable_postpacks['pager'] . '</span>';
            //}elseif($total_post_rows > $total_posts) {
            //Do not show See All if number of records are <= $total_posts
        } elseif ($total_postpacks > count($postpacks)) {
            $html .= '<a href="' . ($pagetype == 'admin' ? admin_url('profile.php?page=mgm/membership/content&section=purchasable_postpacks') : mgm_get_custom_url('membership_contents', false, array('section' => 'purchasable_postpacks'))) . '" class="button">' . __('See All', 'mgm') . '</a>';
        }
        $html .= '</div>';
        $html .= '<br/><div class="clearfix"></div>';
        $html .= '</div>';
    }
    return $html;
}