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'); }
$show = false; // check if (mgm_member_has_coupon($member, $data['coupon']->id)) { $show = true; } else { if (isset($member->other_membership_types) && !empty($member->other_membership_types)) { // log // mgm_log($member->other_membership_types, 'other_membership_types_'.$user->ID); // loop foreach ($member->other_membership_types as $key => $member_oth) { // as object $o_mgm_member = mgm_convert_array_to_memberobj($member_oth, $user->ID); // log // mgm_log($o_mgm_member, 'coupon_users'); // check if (mgm_member_has_coupon($o_mgm_member, $data['coupon']->id)) { $show = true; break; } // unset unset($o_mgm_member); } } } if ($show) { $users_count++; ?> <div class="row brBottom <?php echo $alt = $alt == '' ? 'alternate' : ''; ?>