function _do_export($export_format, $export_option)
 {
     global $wpdb;
     $status = 'error';
     $message = __('Error while exporting Coupons.', 'mgm');
     // users
     if ($export_option != 'unused') {
         //$users = mgm_get_all_userids(array('ID'), 'get_results');
         $start = 0;
         $limit = 1000;
         //user meta fields
         $fields = array('user_id', 'meta_value');
         // sql
         $user_sql = "SELECT count(*) FROM `{$wpdb->usermeta}` WHERE `meta_key` = 'mgm_member_options' AND `user_id` <> 1";
         $count = $wpdb->get_var($user_sql);
         $users = wp_cache_get('all_mgm_user_meta', 'users');
         // if empty read from db:
         if (empty($users)) {
             $users = array();
             if ($count) {
                 // read again
                 for ($i = $start; $i < $count; $i = $i + $limit) {
                     $users = array_merge($users, (array) mgm_patch_partial_user_member_options($i, $limit, $fields));
                     //a small delay of 0.01 second
                     usleep(10000);
                 }
                 wp_cache_set('all_mgm_user_meta', $users, 'users');
             }
         }
     }
     //mgm_log(mgm_array_dump($users,true),__FUNCTION__);
     //array
     $export_coupons = array();
     //export options issue #1494
     if ($export_option == 'used') {
         $query = " AND `used_count` > 0";
     } elseif ($export_option == 'unused') {
         $query = " AND `used_count` IS NULL";
     } else {
         $query = '';
     }
     // count
     $total = $wpdb->get_var("SELECT COUNT(*) FROM `" . TBL_MGM_COUPON . "` WHERE 1 " . $query);
     //
     $perpage = 50;
     // check
     if ($total > $perpage) {
         $pages = ceil($total / $perpage);
     } else {
         $pages = 1;
     }
     // loop pages
     for ($i = 1; $i <= $pages; $i++) {
         //@set_time_limit(300);//300s
         //@ini_set('memory_limit', 134217728);// 128M
         // offset
         $offset = ($i - 1) * $perpage;
         // coupons
         $sql = "SELECT `id`, `name` FROM `" . TBL_MGM_COUPON . "` WHERE 1 {$query} ORDER BY `create_dt` DESC LIMIT {$offset},{$perpage}";
         // coupons
         $coupons = $wpdb->get_results($sql);
         // log
         // mgm_log('Page: ' .$i. '. '. $wpdb->last_query, __FUNCTION__);
         // loop
         foreach ($coupons as $coupon) {
             // found
             if ($users && $export_option != 'unused') {
                 $coupon_used = false;
                 foreach ($users as $user) {
                     if ($user->user_id) {
                         // member
                         $user_id = $user->user_id;
                         $member = unserialize($user->meta_value);
                         $member = mgm_convert_array_to_memberobj($member, $user_id);
                         // show
                         $show = false;
                         // check
                         if (mgm_member_has_coupon($member, $coupon->id)) {
                             $show = true;
                         } else {
                             if (isset($member->other_membership_types) && !empty($member->other_membership_types) && count($member->other_membership_types) > 0) {
                                 // loop
                                 foreach ($member->other_membership_types as $key => $member_oth) {
                                     // as object
                                     if (is_array($member_oth)) {
                                         $o_mgm_member = mgm_convert_array_to_memberobj($member_oth, $user_id);
                                     } else {
                                         if (is_object($member_oth)) {
                                             $o_mgm_member = $member_oth;
                                         }
                                     }
                                     // check
                                     if (mgm_member_has_coupon($o_mgm_member, $coupon->id)) {
                                         $show = true;
                                         break;
                                     }
                                     // unset
                                     unset($o_mgm_member);
                                 }
                             }
                         }
                         // include
                         if ($show) {
                             //mgm_log(mgm_array_dump($member,true),__FUNCTION__);
                             // user
                             $user = get_userdata($user_id);
                             // int
                             $row = new stdClass();
                             // export fields
                             $row->coupon_id = $coupon->id;
                             $row->coupon_name = $coupon->name;
                             $row->user_id = $user->ID;
                             $row->user_login = $user->user_login;
                             $row->user_email = $user->user_email;
                             $row->user_membership_type = ucwords(str_replace('_', ' ', $member->membership_type));
                             // cache
                             $export_coupons[] = $row;
                             // unset
                             unset($row);
                             $coupon_used = true;
                         }
                         unset($user);
                         unset($member);
                     }
                 }
                 if (!$coupon_used && $export_option == 'all') {
                     // int
                     $row = new stdClass();
                     // export fields
                     $row->coupon_id = $coupon->id;
                     $row->coupon_name = $coupon->name;
                     $row->user_id = '';
                     $row->user_login = '';
                     $row->user_email = '';
                     $row->user_membership_type = '';
                     // cache
                     $export_coupons[] = $row;
                     // unset
                     unset($row);
                 }
             } else {
                 // int
                 $row = new stdClass();
                 // export fields
                 $row->coupon_id = $coupon->id;
                 $row->coupon_name = $coupon->name;
                 $row->user_id = '';
                 $row->user_login = '';
                 $row->user_email = '';
                 $row->user_membership_type = '';
                 // cache
                 $export_coupons[] = $row;
                 // unset
                 unset($row);
             }
         }
     }
     // log
     // mgm_log($export_coupons, 'export_coupons');
     // check
     if (count($export_coupons) > 0) {
         // success
         $success = count($export_coupons);
         // create
         if ($export_format == 'csv') {
             $filename = mgm_create_csv_file($export_coupons, 'export_coupons');
         } else {
             $filename = mgm_create_xls_file($export_coupons, 'export_coupons');
         }
         // src
         $file_src = MGM_FILES_EXPORT_URL . $filename;
         // message
         $message = sprintf(__('Successfully exported %d %s.', 'mgm'), $success, $success > 1 ? 'coupons' : 'coupon');
         //status
         $status = 'success';
         //return
         return array('status' => $status, 'message' => $message, 'action' => 'export', 'src' => $file_src);
     } else {
         $message .= ' ' . __('No coupons found with associated users.', 'mgm');
     }
     // return
     return array('status' => $status, 'message' => $message, 'action' => 'export');
 }
 function payment_history_export()
 {
     global $wpdb;
     // data
     $data = array();
     // filter
     $sql_filter = $payment_type = '';
     // check
     if (isset($_POST['search_field_name'])) {
         // issue#: 219
         $search_field_name = $_POST['search_field_name'];
         // for sql
         $search_field_value = mgm_escape($_POST['search_field_value']);
         // for sql
         $search_field_value = trim($search_field_value);
         // by field
         switch ($search_field_name) {
             case 'username':
                 // issue#: 347(LIKE SEARCH)
                 $sql_filter = " AND user.user_login LIKE '%{$search_field_value}%'";
                 break;
             case 'id':
                 $sql_filter = " AND user.ID = '" . (int) $search_field_value . "'";
                 break;
             case 'email':
                 // issue#: 347(LIKE SEARCH)
                 $sql_filter = " AND user.user_email LIKE '%{$search_field_value}%'";
                 break;
             case 'membership_type':
                 // members
                 $members = mgm_get_members_with('membership_type', $search_field_value);
                 // check
                 $members_in = count($members) == 0 ? 0 : implode(',', $members);
                 // set filter
                 $sql_filter = " AND user.ID IN ({$members_in})";
                 break;
             case 'payment_type':
                 $payment_type = $search_field_value;
                 break;
             case 'first_name':
             case 'last_name':
                 // members
                 $members = mgm_get_members_with($search_field_name, $search_field_value);
                 //check
                 $members_in = count($members) == 0 ? 0 : implode(',', $members);
                 // set filter
                 $sql_filter = " AND user.ID IN ({$members_in})";
                 break;
         }
     }
     if (!empty($payment_type)) {
         $con = " AND transaction.payment_type =  '" . $payment_type . "' ";
     } else {
         $con = '';
     }
     //payment success check
     $pay_succ = " AND transaction.status_text =  'Last payment was successful'";
     $transaction_sql = "SELECT SQL_CALC_FOUND_ROWS * FROM " . TBL_MGM_TRANSACTION . " transaction LEFT JOIN {$wpdb->users} user ON transaction.user_id = user.ID ";
     $transaction_sql .= "WHERE transaction.module IS NOT NULL";
     $transaction_sql .= $pay_succ;
     $transaction_sql .= $con;
     $transaction_sql .= $sql_filter;
     $transaction_sql .= " ORDER BY transaction.transaction_dt DESC {$sql_limit}";
     $data['transactions'] = $wpdb->get_results($transaction_sql);
     $export_transactions = array();
     if (count($data['transactions']) > 0) {
         foreach ($data['transactions'] as $tran_log) {
             $json_decoded = json_decode($tran_log->data);
             $user_obj = get_userdata($json_decoded->user_id);
             //empty obj
             $row = new stdClass();
             if (!empty($user_obj)) {
                 //export fields
                 $row->id = $user_obj->ID;
                 $row->username = $user_obj->user_login;
                 $row->firstname = $user_obj->first_name;
                 $row->lastname = $user_obj->last_name;
                 $row->email = $user_obj->user_email;
                 $row->payment_type = ucwords(str_replace('_', ' ', $tran_log->payment_type));
                 $row->module = ucwords($tran_log->module);
                 $row->amount = $json_decoded->trial_on ? $json_decoded->trial_cost : $json_decoded->cost;
                 $row->transaction_date = date(MGM_DATE_FORMAT_SHORT, strtotime($tran_log->transaction_dt));
             }
             // cache
             $export_transactions[] = $row;
             // unset
             unset($row);
         }
     }
     // message
     $message = __('Error while exporting transactions. Could not find any transaction with requested search parameters.', 'mgm');
     // default response
     $response = array('status' => 'error', 'message' => $message);
     // check
     if (count($export_transactions) > 0) {
         // success
         $success = count($export_transactions);
         // create
         if (mgm_post_var('export_format') == 'csv') {
             $filename = mgm_create_csv_file($export_transactions, 'export_transactions');
         } else {
             $filename = mgm_create_xls_file($export_transactions, 'export_transactions');
         }
         // src
         $file_src = MGM_FILES_EXPORT_URL . $filename;
         // message
         $response['message'] = sprintf(__('Successfully exported %d %s.', 'mgm'), $success, $success > 1 ? 'transactions' : 'transaction');
         $response['status'] = 'success';
         $response['src'] = $file_src;
     }
     // return response
     echo json_encode($response);
     exit;
 }
    function member_export()
    {
        global $wpdb;
        // error -- use WP_DEBUG with WP_DEBUG_LOG
        // if(!WP_DEBUG) error_reporting(0);
        // extract
        extract($_POST);
        // log
        // mgm_log($_POST, __FUNCTION__);
        // get format
        $sformat = mgm_get_date_format('date_format_short');
        // process
        if (isset($export_member_info)) {
            // init
            $success = 0;
            // type
            $membership_type = isset($bk_membership_type) ? $bk_membership_type : 'all';
            // status
            $membership_status = isset($bk_membership_status) ? $bk_membership_status : 'all';
            // date
            $date_start = isset($bk_date_start) ? $bk_date_start : '';
            $date_end = isset($bk_date_end) ? $bk_date_end : '';
            // query inut
            $query = '';
            // selected only
            if (isset($bk_only_selected)) {
                // check
                if (isset($bk_selected_members) && is_array($bk_selected_members)) {
                    $query = " AND `id` IN(" . mgm_map_for_in($bk_selected_members) . ")";
                }
            }
            // start date
            if ($date_start) {
                // Issue #700
                // convert to mysql date
                $date_start = strtotime(mgm_format_inputdate_to_mysql($date_start, $sformat));
                // end date
                if ($date_end) {
                    // Issue #700
                    // convert to mysql date
                    $date_end = mgm_format_inputdate_to_mysql($date_end, $sformat);
                    $date_end = strtotime($date_end);
                    // issue#" 492
                    $query .= " AND UNIX_TIMESTAMP(user_registered) >= '{$date_start}' \r\r\n\t\t\t\t\t            AND UNIX_TIMESTAMP(DATE_FORMAT(user_registered, '%Y-%m-%d')) <= '{$date_end}'";
                } else {
                    $query .= " AND UNIX_TIMESTAMP(user_registered) >= '{$date_start}'";
                }
            } else {
                if ($date_end) {
                    // Issue #700
                    // convert to mysql date
                    $date_end = strtotime(mgm_format_inputdate_to_mysql($date_end, $sformat));
                    // query
                    $query .= " AND UNIX_TIMESTAMP(DATE_FORMAT(user_registered, '%Y-%m-%d')) <= '{$date_end}' ";
                }
            }
            // all users
            $sql = 'SELECT ID, user_login, user_email, user_registered, display_name FROM `' . $wpdb->users . '` 
			        WHERE ID <> 1 ' . $query . ' ORDER BY `user_registered` ASC';
            // users
            $users = $wpdb->get_results($sql);
            // filter
            $export_users = array();
            // date
            $current_date = time();
            //issue #844
            $skip_fileds = array('subscription_introduction', 'coupon', 'privacy_policy', 'payment_gateways', 'terms_conditions', 'subscription_options', 'autoresponder', 'captcha');
            // check - issue #1382
            if (isset($bk_users_to_import)) {
                $custom_fields = mgm_get_class('member_custom_fields')->get_fields_where(array('display' => array('on_register' => true, 'on_profile' => true)));
                $import_user_fileds = array('user_login', 'user_email', 'pack_id', 'membership_type');
                foreach ($custom_fields as $field) {
                    if (!in_array($field['name'], $skip_fileds)) {
                        $import_user_fileds[] = $field['name'];
                    }
                }
            }
            // Custom fields
            $cf_profile_pg = mgm_get_class('member_custom_fields');
            $to_unserialize = array();
            foreach (array_unique($cf_profile_pg->sort_orders) as $id) {
                foreach ($cf_profile_pg->custom_fields as $field) {
                    // issue #954: show the field only if it is enabled for profile page
                    if ($field['id'] == $id && $field['type'] == 'checkbox') {
                        $to_unserialize[] = $field['name'];
                    }
                }
            }
            // loop
            foreach ($users as $user) {
                // user cloned
                $user_obj = clone $user;
                // member
                $member = mgm_get_member($user->ID);
                // check
                if (!isset($bk_inactive)) {
                    $bk_inactive = false;
                }
                // check search parameters:
                if ($this->_get_membership_details($member, $bk_msexp_dur_unit, $bk_msexp_dur, $membership_type, $current_date, $bk_inactive, $membership_status)) {
                    // merge
                    if (method_exists($member, 'merge_fields')) {
                        $user = $member->merge_fields($user);
                    }
                    // log
                    // mgm_log($user, __FUNCTION__);
                    // issue #844
                    foreach ($skip_fileds as $skip_filed) {
                        unset($user->{$skip_filed});
                    }
                    // format dates
                    $user->user_registered = date($sformat, strtotime($user->user_registered));
                    $user->last_pay_date = (int) $user->last_pay_date > 0 ? date($sformat, strtotime($user->last_pay_date)) : 'N/A';
                    $user->expire_date = !empty($user->expire_date) ? date($sformat, strtotime($user->expire_date)) : 'N/A';
                    $user->join_date = (int) $user->join_date > 0 ? date($sformat, $user->join_date) : 'N/A';
                    // issue#: 672
                    // DO not show actual password: #1002
                    // $user->user_password = mgm_decrypt_password($member->user_password, $user->ID);
                    $user->rss_token = $member->rss_token;
                    // unset password
                    unset($user->password, $user->password_conf);
                    // unserialize checkbox values
                    if (count($to_unserialize)) {
                        foreach ($to_unserialize as $chkname) {
                            if (isset($user->{$chkname}) && !empty($user->{$chkname})) {
                                $chk_val = @unserialize($user->{$chkname});
                                if (is_array($chk_val)) {
                                    $user->{$chkname} = implode("|", $chk_val);
                                }
                            }
                        }
                    }
                    // check - issue #1382
                    if (isset($bk_users_to_import)) {
                        $importuser = new stdClass();
                        foreach ($import_user_fileds as $import_user_filed) {
                            if (isset($user->{$import_user_filed})) {
                                $importuser->{$import_user_filed} = $user->{$import_user_filed};
                            }
                            if ($import_user_filed == 'pack_id') {
                                $importuser->{$import_user_filed} = $member->pack_id;
                            }
                        }
                        $export_users[] = $importuser;
                        unset($importuser);
                    } else {
                        $export_users[] = $user;
                    }
                }
                // consider multiple memberships as well:
                if (isset($member->other_membership_types) && is_array($member->other_membership_types) && count($member->other_membership_types) > 0) {
                    // loop
                    foreach ($member->other_membership_types as $key => $memtypes) {
                        // types
                        if (is_array($memtypes)) {
                            $memtypes = mgm_convert_array_to_memberobj($memtypes, $user->ID);
                        }
                        // check search parameters:
                        if ($this->_get_membership_details($memtypes, $bk_msexp_dur_unit, $bk_msexp_dur, $membership_type, $current_date, $bk_inactive, $membership_status)) {
                            // copy
                            $user_mem = clone $user_obj;
                            // add custom fields as well:
                            if (!empty($member->custom_fields)) {
                                // loop
                                foreach ($member->custom_fields as $index => $val) {
                                    // custom field
                                    if ($index == 'birthdate' && !empty($val)) {
                                        // convert saved date to input field format
                                        $val = mgm_get_datepicker_format('date', $val);
                                    }
                                    // set
                                    $user_mem->{$index} = $val;
                                }
                            }
                            // check types
                            if (is_object($memtypes) && method_exists($memtypes, 'merge_fields')) {
                                // merge
                                $user_mem = $memtypes->merge_fields($user_mem);
                            } else {
                                // convert to array
                                $data = mgm_object2array($memtypes);
                                // check payment
                                if (isset($memtypes->payment_info) && count($memtypes->payment_info) > 0) {
                                    // loop payments
                                    foreach ($memtypes->payment_info as $index => $val) {
                                        // set
                                        $data['payment_info_' . $index] = str_replace('mgm_', '', $val);
                                    }
                                }
                                // loop data
                                foreach ($data as $index => $val) {
                                    $user_mem->{$index} = $val;
                                }
                            }
                            //issue #844
                            foreach ($skip_fileds as $skip_filed) {
                                unset($user->{$skip_filed});
                            }
                            // format dates
                            $user_mem->user_registered = date($sformat, strtotime($user_mem->user_registered));
                            $user_mem->last_pay_date = (int) $memtypes->last_pay_date > 0 ? date($sformat, strtotime($memtypes->last_pay_date)) : 'N/A';
                            $user_mem->expire_date = !empty($memtypes->expire_date) ? date($sformat, strtotime($memtypes->expire_date)) : 'N/A';
                            $user_mem->join_date = (int) $memtypes->join_date > 0 ? date($sformat, $memtypes->join_date) : 'N/A';
                            // check - issue #1382
                            if (isset($bk_users_to_import)) {
                                $importuser = new stdClass();
                                foreach ($import_user_fileds as $import_user_filed) {
                                    if ($user_mem->{$import_user_filed}) {
                                        $importuser->{$import_user_filed} = $user_mem->{$import_user_filed};
                                    }
                                    if ($import_user_filed == 'pack_id') {
                                        $importuser->{$import_user_filed} = $memtypes->pack_id;
                                    }
                                }
                                $export_users[] = $importuser;
                                unset($importuser);
                            } else {
                                $export_users[] = $user_mem;
                            }
                            // unset
                            unset($user_mem);
                        }
                    }
                }
            }
            // end for
            mgm_log('export_users : ' . mgm_array_dump($export_users, true));
            // default response
            $response = array('status' => 'error', 'message' => __('Error while exporting members. Could not find any member with requested search parameters.', 'mgm'));
            // check
            if (($expcount = count($export_users)) > 0) {
                // Issue #1559: standardization of Membership type
                for ($k = 0; $k < $expcount; $k++) {
                    if (isset($export_users[$k]->membership_type)) {
                        $export_users[$k]->membership_type = strtolower($export_users[$k]->membership_type);
                    }
                }
                // success
                $success = count($export_users);
                // create
                if ($bk_export_format == 'csv') {
                    $filename = mgm_create_csv_file($export_users, 'export_users');
                } else {
                    $filename = mgm_create_xls_file($export_users, 'export_users');
                }
                // src
                $file_src = MGM_FILES_EXPORT_URL . $filename;
                // message
                $response['message'] = sprintf(__('Successfully exported %d %s.', 'mgm'), $success, $success > 1 ? 'users' : 'user');
                $response['status'] = 'success';
                $response['src'] = $file_src;
                // for download iframe
            }
            // return response
            echo json_encode($response);
            exit;
        }
        // data
        $data = array();
        // load template view
        $this->load->template('members/member/export', array('data' => $data));
    }
 function purchase_export()
 {
     global $wpdb;
     // data
     $data = array();
     // filter
     $sql_filter = $data['search_field_name'] = $data['search_field_value'] = '';
     $search_field_name = mgm_post_var('search_field_name');
     // check
     if (!empty($search_field_name)) {
         // post
         $search_field_value = mgm_post_var('search_field_value');
         $search_field_value_two = mgm_post_var('search_field_value_two');
         // view data
         $data['search_field_name'] = $search_field_name;
         // for display
         //issue #1281
         $data['search_field_value'] = htmlentities($search_field_value, ENT_QUOTES, "UTF-8");
         // for display
         $data['search_field_value_two'] = htmlentities($search_field_value_two, ENT_QUOTES, "UTF-8");
         // for display
         //searc value
         $search_field_value = esc_sql($search_field_value);
         // for sql
         // end date value
         $search_field_value_two = esc_sql($search_field_value_two);
         // for sql
         //current date
         $curr_date = mgm_get_current_datetime();
         $current_date = $curr_date['timestamp'];
         // by field
         switch ($search_field_name) {
             case 'user_login':
                 $sql_filter = " AND (`user_login` LIKE '%{$search_field_value}%')";
                 break;
             case 'addon_option':
                 $sql_filter = " AND (`option` LIKE '%{$search_field_value}%')";
                 break;
             case 'purchase_dt':
                 // date start
                 if (empty($search_field_value)) {
                     $search_field_value = date('Y-m-d', $current_date);
                 }
                 // date end
                 if (empty($search_field_value_two)) {
                     $search_field_value_two = date('Y-m-d', $current_date);
                 }
                 // convert
                 $search_field_value = mgm_format_inputdate_to_mysql($search_field_value);
                 $search_field_value_two = mgm_format_inputdate_to_mysql($search_field_value_two);
                 // set
                 $sql_filter = " AND (DATE_FORMAT(`purchase_dt`,'%Y-%m-%d') BETWEEN '{$search_field_value}' AND '{$search_field_value_two}')";
                 break;
         }
     }
     // order
     $sql_order = " ORDER BY `user_login`, `purchase_dt`";
     // sql
     $sql = "SELECT SQL_CALC_FOUND_ROWS A.id, A.purchase_dt, B.option AS addon_option, C.user_login\r\r\n\t\t        FROM `" . TBL_MGM_ADDON_PURCHASES . "` A \r\r\n\t\t\t\tLEFT JOIN " . TBL_MGM_ADDON_OPTION . " B ON (B.id = A.addon_option_id) \r\r\n\t\t\t\tLEFT JOIN " . $wpdb->users . " C ON (C.ID = A.user_id) WHERE 1 \r\r\n\t\t\t\t{$sql_filter} {$sql_order} ";
     // store
     $data['addon_purchases'] = $wpdb->get_results($sql);
     // date format
     $date_format = mgm_get_date_format('date_format');
     // init
     $purchases = array();
     // check
     if (count($data['addon_purchases']) > 0) {
         //purchases
         foreach ($data['addon_purchases'] as $purchase) {
             // int
             $row = new stdClass();
             // export fields
             $row->username = $purchase->user_login;
             $row->addon_option = $purchase->addon_option;
             $row->purchase_date = date($date_format, strtotime($purchase->purchase_dt));
             // cache
             $purchases[] = $row;
             // unset
             unset($row);
         }
     }
     // default response
     $response = array('status' => 'error', 'message' => __('Error while exporting addon purchases.', 'mgm'));
     // check
     if (count($purchases) > 0) {
         // success
         $success = count($purchases);
         // create
         if (mgm_post_var('export_format') == 'csv') {
             $filename = mgm_create_csv_file($purchases, 'addon_purchases');
         } else {
             $filename = mgm_create_xls_file($purchases, 'addon_purchases');
         }
         // src
         $file_src = MGM_FILES_EXPORT_URL . $filename;
         // message
         $message = sprintf(__('Successfully exported %d addon %s.', 'mgm'), $success, $success > 1 ? 'purchases' : 'purchase');
         // init
         $response = array('status' => 'success', 'message' => $message, 'src' => $file_src);
     }
     // return response
     echo json_encode($response);
     exit;
 }
 function post_purchase_export()
 {
     global $wpdb;
     // data
     $data = array();
     // filter
     $sql_filter = $data['search_field_name'] = $data['search_field_value'] = '';
     $search_field_name = mgm_post_var('search_field_name');
     // check
     if (!empty($search_field_name)) {
         // post
         $search_field_value = mgm_post_var('search_field_value');
         $search_field_value_two = mgm_post_var('search_field_value_two');
         // view data
         $data['search_field_name'] = $search_field_name;
         // for display
         //issue #1281
         $data['search_field_value'] = htmlentities($search_field_value, ENT_QUOTES, "UTF-8");
         // for display
         $data['search_field_value_two'] = htmlentities($search_field_value_two, ENT_QUOTES, "UTF-8");
         // for display
         //searc value
         $search_field_value = esc_sql($search_field_value);
         // for sql
         // end date value
         $search_field_value_two = esc_sql($search_field_value_two);
         // for sql
         //current date
         $curr_date = mgm_get_current_datetime();
         $current_date = $curr_date['timestamp'];
         // by field
         switch ($search_field_name) {
             case 'user_login':
                 $sql_filter = " AND (`user_login` LIKE '%{$search_field_value}%' OR `guest_token` LIKE '%{$search_field_value}%')";
                 break;
             case 'post_title':
                 $sql_filter = " AND (`post_title` LIKE '%{$search_field_value}%')";
                 break;
             case 'is_gift':
                 $sql_filter = " AND (`is_gift` = '{$search_field_value}')";
                 break;
             case 'purchase_dt':
                 // date start
                 if (empty($search_field_value)) {
                     $search_field_value = date('Y-m-d', $current_date);
                 }
                 // date end
                 if (empty($search_field_value_two)) {
                     $search_field_value_two = date('Y-m-d', $current_date);
                 }
                 // convert
                 $search_field_value = mgm_format_inputdate_to_mysql($search_field_value);
                 $search_field_value_two = mgm_format_inputdate_to_mysql($search_field_value_two);
                 // set
                 $sql_filter = " AND (DATE_FORMAT(`pp`.`purchase_dt`,'%Y-%m-%d') BETWEEN '{$search_field_value}' AND '{$search_field_value_two}')";
                 break;
         }
     }
     // order
     $sql_order = " ORDER BY u.user_login, p.post_title";
     // sql
     $sql = "SELECT SQL_CALC_FOUND_ROWS p.ID AS post_id, p.post_title, pp.purchase_dt, \r\r\n\t\t        IF(user_id IS NULL, CONCAT('guest-', guest_token), u.user_login) AS user_login, pp.id,\r\r\n\t\t        pp.is_gift,pp.is_expire FROM `" . TBL_MGM_POST_PURCHASES . "` pp \r\r\n\t\t\t\tLEFT JOIN " . $wpdb->posts . " p ON (p.id = pp.post_id) \r\r\n\t\t\t\tLEFT JOIN " . $wpdb->users . " u ON (u.ID = pp.user_id) WHERE 1 \r\r\n\t\t\t\t{$sql_filter} {$sql_order} ";
     // log
     // mgm_log($sql);
     // store
     $data['post_purchases'] = $wpdb->get_results($sql);
     // date format
     $date_format = mgm_get_date_format('date_format');
     // init
     $purchases = array();
     // check
     if (count($data['post_purchases']) > 0) {
         //purchases
         foreach ($data['post_purchases'] as $purchase) {
             // int
             $row = new stdClass();
             // type
             $type = $purchase->is_gift == 'Y' ? __('Gift', 'mgm') : __('Purchase', 'mgm');
             // check is_expiry
             if ($purchase->is_expire == 'N') {
                 $expiry = __('Indefinite', 'mgm');
             } else {
                 $expiry = mgm_get_post($purchase->post_id)->get_access_duration();
                 $expiry = !$expiry ? __('Indefinite', 'mgm') : date($date_format, 86400 * $expiry + strtotime($purchase->purchase_dt)) . " (" . $expiry . __(' D', 'mgm') . ")";
             }
             // member name
             if (preg_match('/^guest-/', $purchase->user_login)) {
                 // guest token
                 $guest_token = str_replace('guest-', '', $purchase->user_login);
                 // username
                 $username = __('Guest', 'mgm') . sprintf(' (%s)', $guest_token);
             } else {
                 // username
                 $username = $purchase->user_login;
             }
             //export fields
             $row->username = $username;
             $row->post = $purchase->post_title;
             $row->type = $type;
             $row->expire_date = $expiry;
             $row->purchase_or_gift_date = date($date_format, strtotime($purchase->purchase_dt));
             // cache
             $purchases[] = $row;
             // unset
             unset($row);
         }
     }
     // default response
     $response = array('status' => 'error', 'message' => __('Error while exporting post (purchase/gift)s.', 'mgm'));
     // check
     if (count($purchases) > 0) {
         // success
         $success = count($purchases);
         // create
         if (mgm_post_var('export_format') == 'csv') {
             $filename = mgm_create_csv_file($purchases, 'post_purchases');
         } else {
             $filename = mgm_create_xls_file($purchases, 'post_purchases');
         }
         // src
         $file_src = MGM_FILES_EXPORT_URL . $filename;
         // message
         $message = sprintf(__('Successfully exported %d post %s.', 'mgm'), $success, $success > 1 ? 'purchases' : 'purchase');
         // init
         $response = array('status' => 'success', 'message' => $message, 'src' => $file_src);
     }
     // return response
     echo json_encode($response);
     exit;
 }