PluginProbe
WPCargo Track & Trace / trunk
WPCargo Track & Trace vtrunk
8.0.5 8.0.3 8.0.4 trunk 6.10.0 6.10.1 6.10.2 6.10.3 6.10.4 6.10.5 6.10.6 6.10.8 6.11.0 6.12.2 6.13.0 6.13.1 6.13.3 6.13.4 6.13.5 6.13.6 6.9.0 6.9.1 6.9.2 6.9.3 6.9.4 All 38 releases
wpcargo / admin / classes / class-wpc-export.php

class-wpc-export.php in WPCargo Track & Trace trunk, at admin/classes/class-wpc-export.php

354 lines 11.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) { die; }
3 class WPC_Export{
4 function form_fields(){
5 $wpcargo_meta_data = array(
6 array(
7 'meta_key' => 'wpcargo_shipper_name',
8 'label' => esc_html__( 'Shipper Name', 'wpcargo' ),
9 'fields' => array()
10 ),
11 array(
12 'meta_key' => 'wpcargo_shipper_phone',
13 'label' => esc_html__( 'Phone Number', 'wpcargo' ),
14 'fields' => array()
15 ),
16 array(
17 'meta_key' => 'wpcargo_shipper_address',
18 'label' => esc_html__( 'Shipper Address', 'wpcargo' ),
19 'fields' => array()
20 ),
21 array(
22 'meta_key' => 'wpcargo_shipper_email',
23 'label' => esc_html__( 'Shipper Email', 'wpcargo' ),
24 'fields' => array()
25 ),
26 array(
27 'meta_key' => 'wpcargo_receiver_name',
28 'label' => esc_html__( 'Receiver Name', 'wpcargo' ),
29 'fields' => array()
30 ),
31 array(
32 'meta_key' => 'wpcargo_receiver_phone',
33 'label' => esc_html__( 'Phone Number', 'wpcargo' ),
34 'fields' => array()
35 ),
36 array(
37 'meta_key' => 'wpcargo_receiver_address',
38 'label' => esc_html__( 'Receiver Address', 'wpcargo' ),
39 'fields' => array()
40 ),
41 array(
42 'meta_key' => 'wpcargo_receiver_phone',
43 'label' => esc_html__( 'Phone Number', 'wpcargo' ),
44 'fields' => array()
45 ),
46 array(
47 'meta_key' => 'wpcargo_receiver_email',
48 'label' => esc_html__( 'Receiver Email', 'wpcargo' ),
49 'fields' => array()
50 ),
51 array(
52 'meta_key' => 'agent_fields',
53 'label' => esc_html__( 'Agent Name', 'wpcargo' ),
54 'fields' => array()
55 ),
56 array(
57 'meta_key' => 'wpcargo_type_of_shipment',
58 'label' => esc_html__( 'Type of Shipment', 'wpcargo' ),
59 'fields' => array()
60 ),
61 array(
62 'meta_key' => 'wpcargo_courier',
63 'label' => esc_html__( 'Courier', 'wpcargo' ),
64 'fields' => array()
65 ),
66 array(
67 'meta_key' => 'wpcargo_mode_field',
68 'label' => esc_html__( 'Mode', 'wpcargo' ),
69 'fields' => array()
70 ),
71 array(
72 'meta_key' => 'wpcargo_qty',
73 'label' => esc_html__( 'Quantity', 'wpcargo' ),
74 'fields' => array()
75 ),
76 array(
77 'meta_key' => 'wpcargo_total_freight',
78 'label' => esc_html__( 'Total Freight', 'wpcargo' ),
79 'fields' => array()
80 ),
81 array(
82 'meta_key' => 'wpcargo_carrier_ref_number',
83 'label' => esc_html__( 'Carrier Reference No.', 'wpcargo' ),
84 'fields' => array()
85 ),
86 array(
87 'meta_key' => 'wpcargo_origin_field',
88 'label' => esc_html__( 'Origin', 'wpcargo' ),
89 'fields' => array()
90 ),
91 array(
92 'meta_key' => 'wpcargo_pickup_date_picker',
93 'label' => esc_html__( 'Pickup Date', 'wpcargo' ),
94 'fields' => array()
95 ),
96 array(
97 'meta_key' => 'wpcargo_status',
98 'label' => esc_html__( 'Shipment Status', 'wpcargo' ),
99 'fields' => array()
100 ),
101 array(
102 'meta_key' => 'wpcargo_comments',
103 'label' => esc_html__( 'Comments', 'wpcargo' ),
104 'fields' => array()
105 ),
106 array(
107 'meta_key' => 'wpcargo_weight',
108 'label' => esc_html__( 'Weight', 'wpcargo' ),
109 'fields' => array()
110 ),
111 array(
112 'meta_key' => 'wpcargo_packages',
113 'label' => esc_html__( 'Packages', 'wpcargo' ),
114 'fields' => array()
115 ),
116 array(
117 'meta_key' => 'wpcargo_product',
118 'label' => esc_html__( 'Product', 'wpcargo' ),
119 'fields' => array()
120 ),
121 array(
122 'meta_key' => 'payment_wpcargo_mode_field',
123 'label' => esc_html__( 'Payment Mode', 'wpcargo' ),
124 'fields' => array()
125 ),
126 array(
127 'meta_key' => 'wpcargo_carrier_field',
128 'label' => esc_html__( 'Carrier', 'wpcargo' ),
129 'fields' => array()
130 ),
131 array(
132 'meta_key' => 'wpcargo_departure_time_picker',
133 'label' => esc_html__( 'Departure Time', 'wpcargo' ),
134 'fields' => array()
135 ),
136 array(
137 'meta_key' => 'wpcargo_destination',
138 'label' => esc_html__( 'Destination', 'wpcargo' ),
139 'fields' => array()
140 ),
141 array(
142 'meta_key' => 'wpcargo_pickup_time_picker',
143 'label' => esc_html__( 'Pickup Time', 'wpcargo' ),
144 'fields' => array()
145 ),
146 array(
147 'meta_key' => 'wpcargo_expected_delivery_date_picker',
148 'label' => esc_html__( 'Expected Delivery Date', 'wpcargo' ),
149 'fields' => array()
150 ),
151 array(
152 'meta_key' => 'wpcargo_shipments_update',
153 'label' => esc_html__( 'History', 'wpcargo' ),
154 'fields' => array()
155 ),
156 );
157 $form_fields = apply_filters( 'ie_registered_fields', $wpcargo_meta_data );
158 return $form_fields;
159 }
160 function wpc_export_request( ){
161 if ( isset( $_REQUEST['wpc_ie_nonce'] ) && wp_verify_nonce( $_REQUEST['wpc_ie_nonce'], 'wpc_import_ie_results_callback' ) ) {
162 ?>
163 <script type="text/javascript">
164 jQuery(document).ready(function($) {
165 $('body').append('<div class="wpcargo-loading">Loading...</div>');
166 });
167 </script>
168 <?php
169 $date_from = sanitize_text_field( $_REQUEST['date-from'] );
170 $date_to = strtotime( sanitize_text_field( $_REQUEST['date-to'] ) );
171 $meta_fields = array();
172 $meta_fields_default = array('ShipmentID', 'Shipment Title', 'Shipment Category');
173 $meta_fields_label = array();
174 $meta_fields_key = isset( $_REQUEST['meta-fields']) ? $_REQUEST['meta-fields'] : array();
175 $shipper_meta_query = array();
176 $status_meta_query = array();
177 $registered_shipper_query = array();
178 $form_fields = $this->form_fields();
179
180 //** Checked if meta field is not empty and add to defualt meta field
181 if( isset( $_REQUEST['meta-fields'] ) && !empty( $_REQUEST['meta-fields']) && !empty( $form_fields ) ){
182 foreach( $form_fields as $field ){
183 if( in_array( $field['meta_key'], $meta_fields_key ) ){
184 $meta_fields_label[$field['meta_key']] = esc_html( $field['label'] );
185 continue;
186 }
187 }
188 }
189 // Remove the Shipment Title metakey
190 // Already added in defualt meta keys
191 if ( ($key = array_search( 'shipment_title', $meta_fields_label)) !== false) {
192 unset($meta_fields_key[$key]);
193 }
194 //** Checked if meta field is not empty and add to defualt meta field
195 if( isset( $_REQUEST['search-shipper'] ) && !empty( $_REQUEST['search-shipper']) ){
196 $shipper_meta_query = array(
197 'key' => apply_filters( 'wpc_report_search_shipper_name_metakey', 'wpcargo_shipper_name' ),
198 'value' => sanitize_text_field( $_REQUEST['search-shipper'] ),
199 'compare' => '=',
200 );
201 }
202 //** Checked if meta field is not empty and add to defualt meta field
203 if( isset( $_REQUEST['wpcargo_status'] ) && !empty( $_REQUEST['wpcargo_status']) ){
204 $status_meta_query = array(
205 'key' => 'wpcargo_status',
206 'value' => sanitize_text_field( $_REQUEST['wpcargo_status'] ),
207 'compare' => '=',
208 );
209 }
210 if( isset( $_REQUEST['registered_shipper'] ) && !empty( $_REQUEST['registered_shipper']) ){
211 $registered_shipper_query = array(
212 'key' => 'registered_shipper',
213 'value' => (int)sanitize_text_field( $_REQUEST['registered_shipper'] ),
214 'compare' => '=',
215 );
216 }
217 //** Merge Header Details for excel import
218 $meta_label = array_merge($meta_fields_default, $meta_fields_label);
219 $meta_query = array( $shipper_meta_query, $status_meta_query, $registered_shipper_query );
220 $meta_query = apply_filters( 'wpc_ie_meta_query', $meta_query, $_REQUEST );
221
222 if( isset( $_REQUEST['tax_input']['wpcargo_shipment_cat'] ) && !empty( $_REQUEST['tax_input']['wpcargo_shipment_cat'] ) ) {
223 $wpc_ie_args = array(
224 'post_type' => $this->post_type,
225 'post_status' => 'publish',
226 'posts_per_page' => -1,
227 'meta_query' => $meta_query,
228 'tax_query' => array(
229 array(
230 'taxonomy' => $this->post_taxonomy,
231 'field' => 'id',
232 'terms' => sanitize_text_field( $_REQUEST['tax_input']['wpcargo_shipment_cat'] ),
233 'operator' => 'IN'
234 )
235 ),
236 'date_query' => array(
237 array(
238 'after' => $date_from,
239 'before' => array(
240 'year' => date('Y', $date_to ),
241 'month' => date('n', $date_to ),
242 'day' => date('j', $date_to ),
243 ),
244 'inclusive' => true,
245 ),
246 ),
247 );
248 }else {
249 $wpc_ie_args = array(
250 'post_type' => $this->post_type,
251 'post_status' => 'publish',
252 'posts_per_page' => -1,
253 'meta_query' => $meta_query,
254 'date_query' => array(
255 array(
256 'after' => $date_from,
257 'before' => array(
258 'year' => date('Y', $date_to ),
259 'month' => date('n', $date_to ),
260 'day' => date('j', $date_to ),
261 ),
262 'inclusive' => true,
263 ),
264 ),
265 );
266 }
267
268 $filename_unique = "shipment-export-".time().".csv";
269 $csv_file = fopen($filename_unique, "w");
270 fprintf($csv_file, chr(0xEF).chr(0xBB).chr(0xBF));
271 $wpc_ie_query = new WP_Query( $wpc_ie_args );
272 if ( $wpc_ie_query->have_posts() ) :
273 $wpc_hook_merge_header = apply_filters( 'wpc_hook_merge_export_header', array());
274 $header_labels = array_merge($meta_label, !empty($wpc_hook_merge_header) ? $wpc_hook_merge_header : array());
275 fputcsv( $csv_file, $header_labels );
276 while ( $wpc_ie_query->have_posts() ) : $wpc_ie_query->the_post();
277 $excel_data = array();
278 $wpc_hook_merge_fields = array();
279 $field_id = array();
280 $shipment_category = '';
281 $post_terms = wp_get_post_terms( get_the_ID(), $this->post_taxonomy );
282 $post_term_container = array();
283 if( !empty( $post_terms ) ) {
284 foreach( $post_terms as $post_term ){
285 $post_term_container[] = $post_term->name;
286 }
287 $shipment_category = implode(', ', $post_term_container);
288 }
289 $excel_data[] = get_the_ID();
290 $field_id[] = get_the_ID();
291 $excel_data[] = get_the_title();
292 $excel_data[] = $shipment_category;
293 foreach( $meta_fields_label as $meta_field_key => $meta_field ) {
294 $wpcargo_post_meta = get_post_meta( get_the_ID(), $meta_field_key, TRUE);
295 if(is_array($wpcargo_post_meta)) {
296 foreach($wpcargo_post_meta as $meta_val) {
297 $excel_data[] = join(' | ', $meta_val);
298 }
299 }else{
300 $get_meta_fields = get_post_meta( get_the_ID(), $meta_field_key, TRUE);
301 if( is_serialized($get_meta_fields) && $meta_field_key == 'wpcargo_shipments_update' ) {
302 $unserialize_meta_fields = unserialize($get_meta_fields);
303 $get_field_data = array();
304 foreach($unserialize_meta_fields as $field_data) {
305 $get_fields[] = $field_data;
306 if(!empty($field_data['date'])) {
307 $get_field_data[] = $field_data['date']. ', ' .$field_data['time']. ', ' .$field_data['location']. ', ' .$field_data['status']. ', ' .$field_data['remarks'];
308 }
309 }
310 $excel_data[] = join(" | ", esc_html($get_field_data) );
311 }elseif( is_serialized($get_meta_fields) && $meta_field_key != 'wpcargo_shipments_update' ) {
312 $unser_meta_fields = unserialize($get_meta_fields);
313 $get_data_fields = array();
314 foreach($unser_meta_fields as $data_fields){
315 $get_data_fields[] = $data_fields;
316 }
317 if(count($get_data_fields) > 1){
318 $excel_data[] = join(" | ", esc_html($unser_meta_fields) );
319 }else{
320 $excel_data[] = $unser_meta_fields[0].' | ';
321 }
322 }else{
323 $excel_data[] = get_post_meta( get_the_ID(), $meta_field_key, TRUE);
324 }
325 }
326 }
327 $wpc_hook_merge_fields = apply_filters( 'wpc_hook_merge_export_fields', array(), get_the_ID() );
328 fputcsv( $csv_file, array_merge($excel_data, !empty($wpc_hook_merge_fields) ? $wpc_hook_merge_fields : array()) );
329 endwhile;
330 fclose($csv_file);
331 ?>
332 <script>
333 jQuery(document).ready(function($) {
334 setTimeout(function(){
335 $('body .wpcargo-loading').remove();
336 window.location='<?php echo esc_html($filename_unique); ?>';
337 }, 3000);
338 });
339 </script>
340 <?php
341 else:
342 ?>
343 <div class="notice notice-error"><p><?php esc_html_e( 'No Result Found!', 'wpcargo' ); ?></p></div>
344 <script>
345 jQuery(document).ready(function($) {
346 $('body .wpcargo-loading').remove();
347 });
348 </script>
349 <?php
350 endif;
351 wp_reset_postdata();
352 }
353 }
354 }