/**
  * API helper method auto login after subscribed
  *
  * @param int $user_id / transaction_id
  * @return bool
  */
 function _auto_login($id)
 {
     // get setting
     $setting = mgm_get_class('system')->get_setting();
     // if autologin not enabled
     if (!isset($setting['enable_autologin']) || isset($setting['enable_autologin']) && !bool_from_yn($setting['enable_autologin'])) {
         return false;
     }
     // check when enabled
     if (is_numeric($id)) {
         // user id
         $user_id = null;
         // get custom param
         $custom = $this->_get_transaction_passthrough($id);
         // verify if not subs purchase
         if ($custom['payment_type'] != 'subscription_purchase' || (!isset($custom['is_registration']) || isset($custom['is_registration']) && !bool_from_yn($custom['is_registration']))) {
             return false;
         }
         // fetch user id
         if (is_numeric($custom['user_id']) && $custom['user_id'] > 0) {
             // return id // #711, autologin is done in background
             mgm_auto_login($id);
             // redirect url for register & purchase
             if (isset($custom['post_id'])) {
                 return get_permalink($custom['post_id']);
             } elseif (isset($custom['postpack_post_id']) && isset($custom['postpack_id'])) {
                 return get_permalink($custom['postpack_post_id']);
             } else {
                 return true;
             }
         }
     }
     // return false
     return false;
 }
/**
 * try auto login if bypassed
 */
function mgm_try_auto_login()
{
    // check
    if (isset($_GET['auto_login']) && isset($_GET['trans_ref']) && isset($_GET['redirect_to'])) {
        // read transaction id
        if ($id = mgm_decode_id(strip_tags($_GET['trans_ref']))) {
            // process login
            if (mgm_auto_login($id)) {
                // no headers
                if (!headers_sent()) {
                    @header(sprintf('Refresh: %d;url=%s', 5, strip_tags($_GET['redirect_to'])));
                } else {
                    return sprintf('<script language="javascript">window.setTimeout(function(){window.location.href="%s";}, %d)</script>', strip_tags($_GET['redirect_to']), 5 * 5);
                }
                // exit;
                exit;
            }
        }
    }
}