Esempio n. 1
0
<?php

$blogname = get_option('blogname');
$tran_success = false;
//getting purchase post title and & price - issue #981
$post_obj = mgm_get_post($post_id);
$purchase_cost = mgm_convert_to_currency($post_obj->purchase_cost);
$post = get_post($post_id);
$post_title = $post->post_title;
//issue #504
if ($user_id) {
    // issue#: 280 (2011 Feb 25)
    // subject
    $subject = $system_obj->get_template('payment_success_email_template_subject', array('blogname' => $blogname), true);
    // body
    $message = $system_obj->get_template('payment_success_email_template_body', array('blogname' => $blogname, 'name' => $user->display_name, 'post_title' => $post_title, 'purchase_cost' => $purchase_cost, 'email' => $user->user_email, 'admin_email' => $system_obj->setting['admin_email']), true);
}
//issue #504
if ($user_id) {
    // issue#: 280 (2011 Feb 25)
    // subject
    $subject = $system_obj->get_template('payment_failed_email_template_subject', array('blogname' => $blogname), true);
    // body
    $message = $system_obj->get_template('payment_failed_email_template_body', array('blogname' => $blogname, 'name' => $user->display_name, 'post_title' => $post_title, 'purchase_cost' => $purchase_cost, 'email' => $user->user_email, 'payment_type' => 'post purchase payment', 'reason' => $status_str, 'admin_email' => $system_obj->setting['admin_email']), true);
}
//issue #504
if ($user_id) {
    // issue#: 280 (2011 Feb 25)
    // subject
    $subject = $system_obj->get_template('payment_pending_email_template_subject', array('blogname' => $blogname), true);
    // body
 function _buy_post()
 {
     global $wpdb;
     // get system settings
     $system_obj = mgm_get_class('system');
     $dge = bool_from_yn($system_obj->get_setting('disable_gateway_emails'));
     $dpne = bool_from_yn($system_obj->get_setting('disable_payment_notify_emails'));
     // get passthrough, stop further process if fails to parse
     $custom = $this->_get_transaction_passthrough($_POST['M_custom']);
     // local var
     extract($custom);
     // find user
     //issue #504
     if ($user_id) {
         $user = get_userdata($user_id);
     }
     $blogname = get_option('blogname');
     $tran_success = false;
     //getting purchase post title and & price - issue #981
     $post_obj = mgm_get_post($post_id);
     $purchase_cost = mgm_convert_to_currency($post_obj->purchase_cost);
     $post = get_post($post_id);
     $post_title = $post->post_title;
     // errors
     $errors = array();
     // check
     switch ($_POST['transStatus']) {
         case "Y":
             // status success
             $tran_success = true;
             // status str
             $status_str = __('Last payment was successful', 'mgm');
             //issue #504
             if ($user_id) {
                 // subject
                 $subject = $system_obj->get_template('payment_success_email_template_subject', array('blogname' => $blogname), true);
                 // body
                 $message = $system_obj->get_template('payment_success_email_template_body', array('blogname' => $blogname, 'name' => $user->display_name, 'post_title' => $post_title, 'purchase_cost' => $purchase_cost, 'email' => $user->user_email, 'admin_email' => $system_obj->setting['admin_email']), true);
             }
             // transaction id
             $transaction_id = $this->_get_transaction_id('M_custom');
             // hook args
             $args = array('post_id' => $post_id, 'transaction_id' => $transaction_id);
             // user purchase
             if (isset($user_id) && (int) $user_id > 0) {
                 $args['user_id'] = $user_id;
             } else {
                 // guest purchase
                 $args['guest_token'] = $guest_token;
             }
             // after succesful payment hook
             do_action('mgm_buy_post_transaction_success', $args);
             // backward compatibility
             do_action('mgm_post_purchase_payment_success', $args);
             // new organized name
             break;
         case "C":
             // status
             $status_str = __('Last payment was refunded or denied', 'mgm');
             //issue #504
             if ($user_id) {
                 // subject
                 $subject = $system_obj->get_template('payment_failed_email_template_subject', array('blogname' => $blogname), true);
                 // body
                 $message = $system_obj->get_template('payment_failed_email_template_body', array('blogname' => $blogname, 'name' => $user->display_name, 'post_title' => $post_title, 'purchase_cost' => $purchase_cost, 'email' => $user->user_email, 'payment_type' => 'post purchase payment', 'reason' => $status_str, 'admin_email' => $system_obj->setting['admin_email']), true);
             }
             // error
             $errors[] = $status_str;
             break;
         case "F":
             // reason
             $reason = 'Unknown';
             // status
             $status_str = sprintf(__('Last payment is pending. Reason: %s', 'mgm'), $reason);
             //issue #504
             if ($user_id) {
                 // subject
                 $subject = $system_obj->get_template('payment_pending_email_template_subject', array('blogname' => $blogname), true);
                 // body
                 $message = $system_obj->get_template('payment_pending_email_template_body', array('blogname' => $blogname, 'name' => $user->display_name, 'post_title' => $post_title, 'purchase_cost' => $purchase_cost, 'email' => $user->user_email, 'reason' => $status_str, 'admin_email' => $system_obj->setting['admin_email']), true);
             }
             // error
             $errors[] = $status_str;
             break;
         default:
             // status
             $status_str = sprintf(__('Last payment status: %s', 'mgm'), $_POST['rawAuthMessage']);
             //issue #504
             if ($user_id) {
                 // subject
                 $subject = $system_obj->get_template('payment_unknown_email_template_subject', array('blogname' => $blogname), true);
                 // body
                 $message = $system_obj->get_template('payment_unknown_email_template_body', array('blogname' => $blogname, 'name' => $user->display_name, 'post_title' => $post_title, 'purchase_cost' => $purchase_cost, 'email' => $user->user_email, 'reason' => $status_str, 'admin_email' => $system_obj->setting['admin_email']), true);
             }
             // error
             $errors[] = $status_str;
     }
     // do action
     do_action('mgm_return_post_purchase_payment_' . $this->module, array('post_id' => $post_id));
     // new, individual
     do_action('mgm_return_post_purchase_payment', array('post_id' => $post_id));
     // new, global
     // notify user
     if (!$dpne) {
         if ($user_id && $this->send_payment_email($_POST['M_custom'])) {
             //issue #862
             $subject = mgm_replace_email_tags($subject, $user_id);
             $message = mgm_replace_email_tags($message, $user_id);
             mgm_mail($user->user_email, $subject, $message);
             //send an email to the buyer
             //update as email sent
             $this->update_paymentemail_sent($_POST['M_custom']);
         }
     }
     $status = __('Failed join', 'mgm');
     //overridden on a successful payment
     if ($tran_success) {
         //issue #1421
         if ($user_id) {
             do_action('mgm_update_coupon_usage', array('user_id' => $user_id));
         }
         // mark as purchased
         if (isset($guest_token)) {
             // issue #1421
             if (isset($coupon_id) && isset($coupon_code)) {
                 do_action('mgm_update_coupon_usage', array('guest_token' => $guest_token, 'coupon_id' => $coupon_id));
                 $this->_set_purchased(NULL, $post_id, $guest_token, $_POST['M_custom'], $coupon_code);
             } else {
                 $this->_set_purchased(NULL, $post_id, $guest_token, $_POST['M_custom']);
             }
         } else {
             $this->_set_purchased($user_id, $post_id, NULL, $_POST['M_custom']);
         }
         // status
         $status = __('The post was purchased successfully', 'mgm');
     }
     // transaction status
     mgm_update_transaction_status($_POST['M_custom'], $status, $status_str);
     // notify admin, only if gateway emails on
     if (!$dge) {
         // not for guest
         if ($user_id) {
             $subject = "[" . $blogname . "] Admin Notification: " . $user->user_email . " purchased post " . $post_id;
             $message = "User display name: {$user->display_name}<br />User email: {$user->user_email}<br />User ID: {$user->ID}<br />Status: " . $status . "<br />Action: Purchase post:" . $subject . "<br /><br />" . $message . "<br /><br /><pre>" . print_r($_POST, true) . '</pre>';
         } else {
             $subject = "[" . $blogname . "] Admin Notification: Guest[IP: " . mgm_get_client_ip_address() . "] purchased post " . $post_id;
             $message = "Guest Purchase";
         }
         mgm_mail($system_obj->setting['admin_email'], $subject, $message);
     }
     // error condition redirect
     if (count($errors) > 0) {
         $this->_meta_redirect(add_query_arg(array('status' => 'error', 'errors' => implode('|', $errors)), $this->_get_thankyou_url()));
     }
 }
/**
 * 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);
}
 /**
  * Update buy post response
  *
  */
 function _buy_post()
 {
     global $wpdb;
     //skip updates from IPN: {PPP will be an immediate update}
     if (isset($_POST['ipn_track_id'])) {
         exit;
     }
     // system
     $system_obj = mgm_get_class('system');
     $dge = bool_from_yn($system_obj->get_setting('disable_gateway_emails'));
     $dpne = bool_from_yn($system_obj->get_setting('disable_payment_notify_emails'));
     // get passthrough, stop further process if fails to parse
     $custom = $this->_get_transaction_passthrough($_POST['custom']);
     // local var
     extract($custom);
     // set user
     $user = null;
     // check
     if (isset($user_id) && (int) $user_id > 0) {
         $user = get_userdata($user_id);
     }
     $blogname = get_option('blogname');
     $tran_success = false;
     //getting purchase post title and & price - issue #981
     $post_obj = mgm_get_post($post_id);
     $purchase_cost = mgm_convert_to_currency($post_obj->purchase_cost);
     $post = get_post($post_id);
     $post_title = $post->post_title;
     // errors
     $errors = array();
     // purchase status
     $purchase_status = 'Error';
     // status
     $payment_status = isset($this->response['PAYMENTINFO_0_PAYMENTSTATUS']) ? $this->response['PAYMENTINFO_0_PAYMENTSTATUS'] : $this->response['PAYMENTSTATUS'];
     // status
     if ($this->status == 'test' && strtoupper($payment_status) == 'PENDING') {
         $payment_status = 'Completed';
     }
     // process on response code
     switch ($payment_status) {
         case 'Completed':
         case 'Processed':
             // status
             $status_str = __('Last payment was successful', 'mgm');
             // purchase status
             $purchase_status = 'Success';
             // transaction id
             $transaction_id = $this->_get_transaction_id();
             // hook args
             $args = array('post_id' => $post_id, 'transaction_id' => $transaction_id);
             // user purchase
             if (isset($user_id) && (int) $user_id > 0) {
                 $args['user_id'] = $user_id;
             } else {
                 // guest purchase
                 $args['guest_token'] = $guest_token;
             }
             // after succesful payment hook
             do_action('mgm_buy_post_transaction_success', $args);
             // backward compatibility
             do_action('mgm_post_purchase_payment_success', $args);
             // new organized name
             break;
         case 'Failed':
         case 'Refunded':
         case 'Denied':
         case 'In-Progress':
             // status
             $status_str = __('Last payment was refunded or denied', 'mgm');
             // purchase status
             $purchase_status = 'Failure';
             // error
             $errors[] = $status_str;
             break;
         case 'Pending':
             // reason
             if (isset($this->response['PAYMENTINFO_0_PENDINGREASON'])) {
                 $reason = $this->response['PAYMENTINFO_0_PENDINGREASON'];
             } else {
                 $reason = $payment_status;
             }
             // status
             $status_str = sprintf(__('Last payment is pending. Reason: %s', 'mgm'), $reason);
             // purchase status
             $purchase_status = 'Pending';
             // error
             $errors[] = $status_str;
             break;
         default:
             // status
             $status_str = sprintf(__('Last payment status: %s', 'mgm'), isset($payment_status) ? $payment_status : 'Unknown');
             // purchase status
             $purchase_status = 'Unknown';
             // error
             $errors[] = $status_str;
     }
     // do action
     do_action('mgm_return_post_purchase_payment_' . $this->module, array('post_id' => $post_id));
     // new, individual
     do_action('mgm_return_post_purchase_payment', array('post_id' => $post_id));
     // new, global
     // set as purchase
     $status = __('Failed join', 'mgm');
     //overridden on a successful payment
     // check status
     if ($purchase_status == 'Success') {
         // mark as purchased
         if (isset($user->ID)) {
             // purchased by user
             // call coupon action
             do_action('mgm_update_coupon_usage', array('user_id' => $user_id));
             // set as purchased
             $this->_set_purchased($user_id, $post_id, NULL, $_POST['custom']);
         } else {
             // purchased by guest
             if (isset($guest_token)) {
                 // issue #1421, used coupon
                 if (isset($coupon_id) && isset($coupon_code)) {
                     // call coupon action
                     do_action('mgm_update_coupon_usage', array('guest_token' => $guest_token, 'coupon_id' => $coupon_id));
                     // set as purchased
                     $this->_set_purchased(NULL, $post_id, $guest_token, $_POST['custom'], $coupon_code);
                 } else {
                     $this->_set_purchased(NULL, $post_id, $guest_token, $_POST['custom']);
                 }
             }
         }
         // status
         $status = __('The post was purchased successfully', 'mgm');
     }
     // transaction status
     mgm_update_transaction_status($_POST['custom'], $status, $status_str);
     // blog
     $blogname = get_option('blogname');
     // post being purchased
     $post = get_post($post_id);
     // notify user, only if gateway emails on
     if (!$dpne) {
         // notify user
         if (isset($user->ID)) {
             // mgm post setup object
             $post_obj = mgm_get_post($post_id);
             // check
             if ($this->send_payment_email($_POST['custom'])) {
                 // check
                 if (mgm_notify_user_post_purchase($blogname, $user, $post, $purchase_status, $system_obj, $post_obj, $status_str)) {
                     // update as email sent
                     $this->update_paymentemail_sent($_POST['custom']);
                 }
             }
         }
     }
     // notify admin, only if gateway emails on
     if (!$dge) {
         // notify admin,
         mgm_notify_admin_post_purchase($blogname, $user, $post, $status);
     }
     // if failure:
     if ($purchase_status != 'Success') {
         $errors = isset($this->response['L_ERRORCODE0']) && !empty($this->response['L_ERRORCODE0']) ? urlencode($this->response['L_ERRORCODE0'] . ': ' . $this->response['L_SHORTMESSAGE0'] . ' - ' . $this->response['L_LONGMESSAGE0']) : __('An error occured while porcessing payment.', 'mgm') . ': ' . $status_str;
         mgm_redirect(add_query_arg(array('status' => 'error', 'errors' => $errors), $this->_get_thankyou_url()));
         exit;
     }
     // default error condition redirect
     if (count($errors) > 0) {
         mgm_redirect(add_query_arg(array('status' => 'error', 'errors' => implode('|', $errors)), $this->_get_thankyou_url()));
     }
 }
/**
 * Send Email Notification to User on Post Purchase
 *
 * @uses mgm_notify_user()
 * @param object $blogname
 * @param object $user
 * @param object $post
 * @param string $status
 * @param object $system_obj
 * @param object $post_obj
 * @param string $status_str
 * @return bool $send
 */
function mgm_notify_user_post_purchase($blogname, $user, $post, $status, $system_obj, $post_obj, $status_str)
{
    // emails not for guest
    if (isset($user->ID)) {
        // purchase status
        switch ($status) {
            case 'Success':
                // subject
                $subject = $system_obj->get_template('payment_success_email_template_subject', array('blogname' => $blogname), true);
                // data
                $data = array('blogname' => $blogname, 'name' => $user->display_name, 'post_title' => $post->post_title, 'purchase_cost' => mgm_convert_to_currency($post_obj->purchase_cost), 'email' => $user->user_email, 'admin_email' => $system_obj->get_setting('admin_email'));
                // message
                $message = $system_obj->get_template('payment_success_email_template_body', $data, true);
                //
                break;
            case 'Failure':
                // subject
                $subject = $system_obj->get_template('payment_failed_email_template_subject', array('blogname' => $blogname), true);
                // data
                $data = array('blogname' => $blogname, 'name' => $user->display_name, 'post_title' => $post->post_title, 'purchase_cost' => mgm_convert_to_currency($post_obj->purchase_cost), 'email' => $user->user_email, 'payment_type' => 'post purchase payment', 'reason' => $status_str, 'admin_email' => $system_obj->get_setting('admin_email'));
                // message
                $message = $system_obj->get_template('payment_failed_email_template_body', $data, true);
                break;
            case '':
                // subject
                $subject = $system_obj->get_template('payment_pending_email_template_subject', array('blogname' => $blogname), true);
                // data
                $data = array('blogname' => $blogname, 'name' => $user->display_name, 'post_title' => $post->post_title, 'purchase_cost' => mgm_convert_to_currency($post_obj->purchase_cost), 'email' => $user->user_email, 'reason' => $status_str, 'admin_email' => $system_obj->get_setting('admin_email'));
                // message
                $message = $system_obj->get_template('payment_pending_email_template_body', $data, true);
                break;
            case 'Unknown':
            default:
                // subject
                $subject = $system_obj->get_template('payment_unknown_email_template_subject', array('blogname' => $blogname), true);
                // data
                $data = array('blogname' => $blogname, 'name' => $user->display_name, 'post_title' => $post->post_title, 'purchase_cost' => mgm_convert_to_currency($post_obj->purchase_cost), 'email' => $user->user_email, 'reason' => $status_str, 'admin_email' => $system_obj->get_setting('admin_email'));
                // message
                $message = $system_obj->get_template('payment_unknown_email_template_body', $data, true);
                break;
        }
        // replace tags
        $subject = mgm_replace_email_tags($subject, $user->ID);
        $message = mgm_replace_email_tags($message, $user->ID);
        // return
        return @mgm_notify_user($user->user_email, $subject, $message);
        // send an email to the buyer
    }
    // return
    return false;
}
Esempio n. 6
0
 function _buy_post()
 {
     global $wpdb;
     // system
     $system_obj = mgm_get_class('system');
     $dge = bool_from_yn($system_obj->get_setting('disable_gateway_emails'));
     $dpne = bool_from_yn($system_obj->get_setting('disable_payment_notify_emails'));
     // get passthrough, stop further process if fails to parse
     $custom = $this->_get_transaction_passthrough($_REQUEST['custom']);
     // local var
     extract($custom);
     // find user
     if ($user_id) {
         $user = get_userdata($user_id);
     }
     // blog
     $blogname = get_option('blogname');
     //getting purchase post title and & price - issue #981
     $post_obj = mgm_get_post($post_id);
     $purchase_cost = mgm_convert_to_currency($post_obj->purchase_cost);
     $post = get_post($post_id);
     $post_title = $post->post_title;
     // tran
     $tran_success = false;
     // status success
     $tran_success = true;
     // status
     $status_str = __('Last payment was successful', 'mgm');
     // emails not for guest
     if ($user_id) {
         // subject
         $subject = $system_obj->get_template('payment_success_email_template_subject', array('blogname' => $blogname), true);
         // body
         $message = $system_obj->get_template('payment_success_email_template_body', array('blogname' => $blogname, 'name' => $user->display_name, 'post_title' => $post_title, 'purchase_cost' => $purchase_cost, 'email' => $user->user_email, 'admin_email' => $system_obj->setting['admin_email']), true);
         //update coupon usage - issue #1421
         do_action('mgm_update_coupon_usage', array('user_id' => $user_id));
     }
     // transation id
     $transaction_id = $this->_get_transaction_id('custom', $_REQUEST);
     // hook args
     $args = array('post_id' => $post_id, 'transaction_id' => $transaction_id);
     // user purchase
     if (isset($user_id) && (int) $user_id > 0) {
         $args['user_id'] = $user_id;
     } else {
         // guest purchase
         $args['guest_token'] = $guest_token;
     }
     // after succesful payment hook
     do_action('mgm_buy_post_transaction_success', $args);
     // backward compatibility
     do_action('mgm_post_purchase_payment_success', $args);
     // new organized name
     // do action
     do_action('mgm_return_post_purchase_payment_' . $this->module, array('post_id' => $post_id));
     // new, individual
     do_action('mgm_return_post_purchase_payment', array('post_id' => $post_id));
     // new, global
     // notify user
     if (!$dpne) {
         // mail
         if ($user_id && $this->send_payment_email($_REQUEST['custom'])) {
             //issue #862
             $subject = mgm_replace_email_tags($subject, $user_id);
             $message = mgm_replace_email_tags($message, $user_id);
             // mail
             mgm_mail($user->user_email, $subject, $message);
             //send an email to the buyer
             //update as email sent
             $this->update_paymentemail_sent($_REQUEST['custom']);
         }
     }
     // mark as purchased
     if (isset($guest_token)) {
         // issue #1421
         if (isset($coupon_id) && isset($coupon_code)) {
             do_action('mgm_update_coupon_usage', array('guest_token' => $guest_token, 'coupon_id' => $coupon_id));
             $this->_set_purchased(NULL, $post_id, $guest_token, $_REQUEST['custom'], $coupon_code);
         } else {
             $this->_set_purchased(NULL, $post_id, $guest_token, $_REQUEST['custom']);
         }
     } else {
         $this->_set_purchased($user_id, $post_id, NULL, $_REQUEST['custom']);
     }
     // status
     $status = __('The post was purchased successfully', 'mgm');
     // transaction status
     mgm_update_transaction_status($_REQUEST['custom'], $status, $status_str);
     // notify admin, only if gateway emails on
     if (!$dge) {
         // not for guest
         if ($user_id) {
             // subject
             $subject = "[" . $blogname . "] Admin Notification: " . $user->user_email . " purchased post " . $post_id;
             // message
             $message = "User display name: {$user->display_name}<br />\r\r\n\t\t\t\t\t\t\tUser email: {$user->user_email}<br />\r\r\n\t\t\t\t\t\t\tUser ID: {$user->ID}<br />Status: " . $status . "<br />\r\r\n\t\t\t\t\t\t\tAction: Purchase post:" . $subject . "<br /><br />" . $message;
         } else {
             $subject = "[" . $blogname . "] Admin Notification: Guest[IP: " . mgm_get_client_ip_address() . "] purchased post " . $post_id;
             $message = "Guest Purchase";
         }
         // mail
         mgm_mail($system_obj->setting['admin_email'], $subject, $message);
     }
 }
/**
 * get post purchase buttons
 * final step for post purchase
 *
 * @param void
 * @return $html
 */
function mgm_get_post_purchase_buttons()
{
    // get current user data - issue #1421
    $user = wp_get_current_user();
    // pack
    $pack = NULL;
    // addon options
    if ($addon_option_ids = mgm_post_var('addon_options')) {
        $addon_options = mgm_get_addon_options_only($addon_option_ids);
        // mgm_pr($addon_options);
    }
    // post purchase
    if (isset($_POST['post_id'])) {
        //issue #1250
        if (isset($_POST['mgm_postpurchase_field']['coupon']) && !empty($_POST['mgm_postpurchase_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_postpurchase_field']['coupon']))) {
                    //redirect back to the form
                    $q_arg = array('error_field' => 'Coupon', 'error_type' => 'invalid', 'error_field_value' => $_POST['mgm_postpurchase_field']['coupon']);
                    $redirect = add_query_arg($q_arg, $_POST['form_action']);
                    mgm_redirect($redirect);
                    exit;
                }
            }
        }
        // post id
        $post_id = $_POST['post_id'];
        // gete mgm data
        $post_obj = mgm_get_post($post_id);
        $cost = mgm_convert_to_currency($post_obj->purchase_cost);
        $product = $post_obj->product;
        $allowed_modules = $post_obj->allowed_modules;
        // post data
        $post = get_post($post_id);
        $title = $post->post_title;
        // item name -issue #1380
        $item_name = apply_filters('mgm_post_purchase_itemname', sprintf(__('Purchase Post - %s', 'mgm'), $title));
        // set pack
        $pack = array('duration' => 1, 'item_name' => $item_name, 'buypost' => 1, 'cost' => $cost, 'title' => $title, 'product' => $product, 'post_id' => $post_id, 'allowed_modules' => $allowed_modules);
    } else {
        if (isset($_POST['postpack_id'])) {
            // post pack purchase
            //issue #1250
            if (isset($_POST['mgm_postpurchase_field']['coupon']) && !empty($_POST['mgm_postpurchase_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_postpurchase_field']['coupon']))) {
                        //redirect back to the form
                        $q_arg = array('error_field' => 'Coupon', 'error_type' => 'invalid', 'error_field_value' => $_POST['mgm_postpurchase_field']['coupon']);
                        $redirect = add_query_arg($q_arg, $_POST['form_action']);
                        mgm_redirect($redirect);
                        exit;
                    }
                }
            }
            // post pack purchase
            $postpack_id = $_POST['postpack_id'];
            // pcak id
            $postpack_post_id = $_POST['postpack_post_id'];
            // post id where pack is listed, redirect here
            // get pack
            $postpack = mgm_get_postpack($postpack_id);
            $cost = mgm_convert_to_currency($postpack->cost);
            $product = json_decode($postpack->product, true);
            $modules = json_decode($postpack->modules, true);
            //mgm_pr($postpack);
            // item name -issue #1380
            $item_name = apply_filters('mgm_postpack_purchase_itemname', sprintf(__('Purchase Post Pack - %s', 'mgm'), $postpack->name));
            // post id
            $post_id = mgm_get_postpack_posts_csv($postpack_id);
            // set pack
            $pack = array('duration' => 1, 'item_name' => $item_name, 'buypost' => 1, 'cost' => $cost, 'title' => $postpack->name, 'product' => $product, 'post_id' => $post_id, 'postpack_id' => $postpack_id, 'postpack_post_id' => $postpack_post_id, 'allowed_modules' => $modules);
        }
    }
    // check
    if (!$pack) {
        return __('Error in Payment! No data available ');
        exit;
    }
    // guest token	-issue #1421
    if (isset($_POST['guest_purchase']) && $_POST['guest_purchase'] == TRUE && $user->ID <= 0) {
        $pack['guest_token'] = sanitize_title_for_query(mgm_create_token());
    }
    // addon options
    if (isset($addon_options) && !empty($addon_options)) {
        $pack['addon_options'] = $addon_options;
    }
    // get coupon
    $post_purchase_coupon = mgm_save_partial_fields(array('on_postpurchase' => true), 'mgm_postpurchase_field', $pack['cost'], false, 'postpurchase');
    // alter
    mgm_get_post_purchase_coupon_pack($post_purchase_coupon, $pack);
    // Eg: $_POST['mgm_payment_gateways'] = mgm_paypal
    $cf_payment_gateways = isset($_POST['mgm_payment_gateways']) && !empty($_POST['mgm_payment_gateways']) ? $_POST['mgm_payment_gateways'] : null;
    // bypass step2 if payment gateway is submitted: issue #: 469
    if (!is_null($cf_payment_gateways)) {
        // get pack
        // mgm_get_upgrade_coupon_pack($member, $selected_pack);
        // cost
        if ((double) $pack['cost'] > 0) {
            //get an object of the payment gateway:
            $mod_obj = mgm_get_module($cf_payment_gateways, 'payment');
            // tran options
            $tran_options = array('user_id' => $user->ID);
            // is register & purchase
            if (isset($_POST['post_id'])) {
                $tran_options['post_id'] = (int) $_POST['post_id'];
            }
            // postpack id
            if (isset($_POST['postpack_id'])) {
                $tran_options['postpack_id'] = (int) $_POST['postpack_id'];
            }
            // is register & purchase postpack
            if (isset($_POST['postpack_post_id']) && isset($_POST['postpack_id'])) {
                $tran_options['postpack_post_id'] = (int) $_POST['postpack_post_id'];
                $tran_options['postpack_id'] = (int) $_POST['postpack_id'];
            }
            // create transaction
            $tran_id = mgm_add_transaction($pack, $tran_options);
            // bypass directly to process return if manual payment:
            if ($cf_payment_gateways == 'mgm_manualpay') {
                // set
                $_POST['custom'] = $tran_id;
                // direct call to module return function:
                $mod_obj->process_return();
                // exit
                exit;
            }
            // encode id:
            $tran_id = mgm_encode_id($tran_id);
            $redirect = $mod_obj->_get_endpoint('html_redirect', true);
            $redirect = add_query_arg(array('tran_id' => $tran_id), $redirect);
            // redirect
            mgm_redirect($redirect);
            // this goes to subscribe, mgm_functions.php/mgm_get_subscription_buttons
            // exit
            exit;
        }
    }
    // get payment modules
    $a_payment_modules = mgm_get_class('system')->get_active_modules('payment');
    // 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;
            }
            // store
            $payment_modules[] = $payment_module;
        }
    }
    // init
    $button = '';
    // transaction
    $tran_id = NULL;
    $button_printed = 0;
    // loop modules
    foreach ($payment_modules as $module) {
        // object
        $mod_obj = mgm_get_module($module, 'payment');
        // check buypost support
        if (in_array('buypost', $mod_obj->supported_buttons)) {
            // create transaction
            if (!$tran_id) {
                $tran_id = mgm_add_transaction($pack);
            }
            // button code
            if (isset($pack['allowed_modules'])) {
                // Issue #1562: If no payment module is selected, display all supported modules
                if (!empty($pack['allowed_modules']) && FALSE === in_array($module, $pack['allowed_modules'])) {
                    continue;
                }
            }
            $button_code = $mod_obj->get_button_buypost(array('pack' => $pack, 'tran_id' => $tran_id), true);
            $button_printed++;
            // get button
            $button .= "<div class='mgm_custom_filed_table'>" . $button_code . "</div>";
        }
    }
    // none active
    if ($button_printed == 0) {
        $button .= sprintf('<p class="mgm-no-module"> %s </p>', __('No Payment module active for this Content Purchase.', 'mgm'));
    }
    // if Cost is zero, then process using free module.: issue#: 883
    if ($tran_id && $pack['cost'] == 0 && in_array('mgm_free', $a_payment_modules) && mgm_get_module('mgm_free')->is_enabled()) {
        // module
        $module = 'mgm_free';
        // payments url
        $payments_url = mgm_get_custom_url('transactions');
        // query_args
        $query_args = array('method' => 'payment_return', 'module' => $module, 'custom' => $tran_id);
        // redirector
        if (isset($_REQUEST['redirector'])) {
            // set
            $query_args['redirector'] = $_REQUEST['redirector'];
        }
        // redirect to module to mark the payment as complete
        $redirect = add_query_arg($query_args, $payments_url);
        // redirect
        mgm_redirect($redirect);
    }
    // html
    $return = '<div class="post_purchase_select_gateway">' . __('Please Select a Payment Gateway.', 'mgm') . '</div>' . $button;
    // return
    return $return;
}
/**
 * membership accessible/purchasable contents 
 * 
 * @since 2.6.0
 *
 * @param array|string $membership_types
 * @param string $type ( accessible|purchasable )
 * @param int $user_id
 * @param string $posttype ( post|page|custom_post_type )
 * @param int $limit
 * @return array
 */
function mgm_get_membership_contents($membership_types, $type = 'accessible', $user_id = NULL, $post_type = NULL, $limit = NULL)
{
    global $wpdb;
    // issue #920
    $user = wp_get_current_user();
    $temp_member = new stdClass();
    $extended_protection = mgm_get_class('system')->setting['content_hide_by_membership'];
    // membership types
    if (!is_array($membership_types)) {
        $membership_types = array($membership_types);
    }
    // sql per page
    $limit_per_page = 50;
    $limit_clause = '';
    // limit
    if (!$limit || !isset($_GET['section']) || isset($_GET['section']) && $_GET['section'] != $type) {
        $limit_clause = 'LIMIT ' . $limit_per_page;
    }
    // get types
    $post_types_in = $post_type ? mgm_map_for_in(array($post_type)) : mgm_get_post_types(true);
    // from
    $sql_from = " FROM " . $wpdb->posts . " A JOIN " . $wpdb->postmeta . " B ON (A.ID = B.post_id ) \r\r\n\t\t\t      WHERE post_status = 'publish' AND B.meta_key LIKE '_mgm_post%' AND post_type IN ({$post_types_in}) ";
    // get count first
    $total_post_rows = $wpdb->get_var("SELECT COUNT(* ) AS total_post_rows {$sql_from}");
    // update limit if less posts availble
    if (!empty($limit_clause) && $total_post_rows > $limit_per_page) {
        $limit_clause = 'LIMIT ' . $total_post_rows;
    }
    // get posts
    $results = $wpdb->get_results("SELECT DISTINCT(ID), post_name, post_title, post_date, post_content {$sql_from} ORDER BY post_date DESC {$limit_clause}");
    // for purchasable only, get purchased posts
    if ($type == 'purchasable') {
        // sql
        $sql = $wpdb->prepare("SELECT `post_id` FROM `" . TBL_MGM_POST_PURCHASES . "` WHERE `user_id` = %d", $user_id);
        // purchased
        $purchased = $wpdb->get_results($sql);
        // init
        $purchased_posts = array();
        // check
        if (count($purchased) > 0) {
            // loop
            foreach ($purchased as $id => $obj) {
                // set
                $purchased_posts[] = $obj->post_id;
            }
        }
    }
    // init
    $posts = array();
    // store
    if (count($results) > 0) {
        // set counter
        $total_posts = 0;
        // per page
        $posts_per_page = 5;
        // loop
        foreach ($results as $id => $obj) {
            // post object
            $post_obj = mgm_get_post($obj->ID);
            //access delay - issue #920
            $access_delay = $post_obj->access_delay;
            // post access membership types
            $access_membership_types = $post_obj->get_access_membership_types();
            //issue #1376
            $post_category_access_membership_types = mgm_get_post_category_access_membership_types($obj->ID);
            //merging category access/ post accesss
            $access_membership_types = array_merge($access_membership_types, $post_category_access_membership_types);
            //gettign unique access members
            $access_membership_types = array_unique($access_membership_types);
            // branch
            switch ($type) {
                case 'accessible':
                    // multiple membership level purchase(issue#: 400) modification
                    if (array_diff($access_membership_types, $membership_types) != $access_membership_types) {
                        //if any match found
                        // issue #920
                        $access = true;
                        if ($extended_protection == 'Y') {
                            $temp_member->membership_type = $membership_types[0];
                            if (mgm_check_post_access_delay($temp_member, $user, $access_delay)) {
                                //okey
                            } else {
                                $access = false;
                            }
                        }
                        if ($access) {
                            // increment
                            $total_posts++;
                            // store
                            if ($limit != '' && $total_posts <= $posts_per_page || $limit == '') {
                                $posts[] = $obj;
                            }
                        }
                    }
                    break;
                case 'purchasable':
                    // multiple membership level purchase(issue#: 400) modification
                    if (bool_from_yn($post_obj->purchasable) && array_diff($access_membership_types, $membership_types) == $access_membership_types) {
                        //if no match
                        // not purchased
                        if (!in_array($obj->ID, $purchased_posts)) {
                            // issue #920
                            $access = true;
                            if ($extended_protection == 'Y') {
                                $temp_member->membership_type = $membership_types[0];
                                if (mgm_check_post_access_delay($temp_member, $user, $access_delay)) {
                                    //okey
                                } else {
                                    $access = false;
                                }
                            }
                            if ($access) {
                                // increment
                                $total_posts++;
                                // store
                                if ($limit != '' && $total_posts <= $posts_per_page || $limit == '') {
                                    // fetch post price
                                    $obj->purchase_cost = mgm_convert_to_currency($post_obj->purchase_cost);
                                    // store
                                    $posts[] = $obj;
                                }
                            }
                        }
                    }
                    break;
            }
            // unset
            unset($post_obj);
        }
    }
    // reset total
    if (empty($posts)) {
        $total_posts = 0;
    }
    // pager
    $pager = '';
    /*if($total_post_rows > $limit_per_page){	
    		$pager 	= sprintf('<a href="%s">%s</a>', mgm_get_custom_url('membership_contents', false, array('page'=>2)), __('next','mgm'));
    	}*/
    // return
    return array('total_posts' => $total_posts, 'posts' => $posts, 'total_post_rows' => $total_post_rows, 'pager' => $pager);
}
/**
 * generate guest purchase purchase options
 *
 * @param object post 
 * @patam string message, pre_button|pre_register
 * @return string html
 */
function mgm_get_post_purchase_options($post, $message = 'pre_button')
{
    // post ot post id
    if (!is_object($post) && is_numeric($post)) {
        $post =& get_post($post);
    }
    // get post purchase options
    $post_obj = mgm_get_post($post->ID);
    // membership_types
    $membership_types_obj = mgm_get_class('mgm_membership_types');
    // system
    $system_obj = mgm_get_class('system');
    //Issue #794
    $currency = $system_obj->get_setting('currency');
    // symbol
    if (($currency_symbol = mgm_get_currency_symbols($currency)) != $currency) {
        $purchase_cost = $currency_symbol . mgm_convert_to_currency($post_obj->purchase_cost);
    } else {
        $purchase_cost = mgm_convert_to_currency($post_obj->purchase_cost) . ' ' . $currency;
    }
    // types
    $membership_types = array();
    // acc
    $accessible_membership_types = $post_obj->get_access_membership_types();
    // loop
    foreach ($accessible_membership_types as $membership_type) {
        // url
        $membership_register_url = mgm_get_custom_url('register', false, array('membership' => base64_encode($membership_type), 'post_id' => $post->ID));
        // name
        $membership_name = $membership_types_obj->get_type_name($membership_type);
        // set
        $membership_types[] = sprintf('<li><a href="%s" target="_blank">%s</a></li>', $membership_register_url, $membership_name);
    }
    // template
    $template = mgm_stripslashes_deep($system_obj->get_template('text_guest_purchase_' . $message, array(), true));
    // the template is twice used
    // replace tags
    $html = str_replace('[post_title]', $post->post_title, $template);
    //Issue #794
    $html = str_replace('[purchase_cost]', $purchase_cost, $html);
    // membership_types_options
    $membership_types_options = count($membership_types) > 0 ? sprintf('<ul>%s</ul>', implode('', $membership_types)) : __('None available<br>', 'mgm');
    // set
    $html = str_replace('[membership_types]', $membership_types_options, $html);
    // return
    return $html;
}