PluginProbe
Export All Posts, Products, Orders & Users | WP Ultimate Exporter | WordPress CSV Export / trunk
Export All Posts, Products, Orders & Users | WP Ultimate Exporter | WordPress CSV Export vtrunk
3.0 2.24.2 2.24.1 1.7.2 1.7.3 1.7.4 1.7.5 1.7.6 1.7.7 1.7.8 1.7.9 2.0 2.0.1 2.1 2.1.1 2.1.2 2.10 2.11 2.12 2.13 2.14 2.15 2.16 2.16.1 2.16.2 All 96 releases
wp-ultimate-exporter / exportExtensions / EDDExport.php

EDDExport.php in Export All Posts, Products, Orders & Users | WP Ultimate Exporter | WordPress CSV Export trunk, at exportExtensions/EDDExport.php

797 lines 30.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /******************************************************************************************
4 * Copyright (C) Smackcoders. - All Rights Reserved under Smackcoders Proprietary License
5 * Unauthorized copying of this file, via any medium is strictly prohibited
6 * Proprietary and confidential
7 * You can contact Smackcoders at email address info@smackcoders.com.
8 *******************************************************************************************/
9
10 namespace Smackcoders\SMEXP;
11
12 if (!defined('ABSPATH'))
13 exit; // Exit if accessed directly
14
15 /**
16 * Class EDDExport
17 * Handles all Easy Digital Downloads export functionality
18 * @package Smackcoders\WCSV
19 */
20 class EDDExport
21 {
22 protected static $instance = null, $export_instance;
23 public $totalRowCount;
24 public $plugin;
25
26 public static function getInstance()
27 {
28 if (null == self::$instance) {
29 self::$instance = new self;
30 EDDExport::$export_instance = ExportExtension::getInstance();
31 }
32 return self::$instance;
33 }
34 /**
35 * Check if EDD 3.0+ is active (uses custom order tables)
36 *
37 * @return bool True if EDD 3.0+, false otherwise
38 */
39 private function is_edd_3_0_plus()
40 {
41 if (!class_exists('Easy_Digital_Downloads')) {
42 return false;
43 }
44
45 // Check for EDD 3.0+ Order class
46 if (class_exists('\\EDD\\Orders\\Order')) {
47 return true;
48 }
49
50 // Check for Order_Query class
51 if (class_exists('\\EDD\\Orders\\Order_Query')) {
52 return true;
53 }
54
55 // Check version if function exists
56 if (function_exists('edd_get_version')) {
57 $version = edd_get_version();
58 if ($version && version_compare($version, '3.0', '>=')) {
59 return true;
60 }
61 }
62
63 return false;
64 }
65
66
67
68
69 /**
70 * EDDExport constructor.
71 */
72 public function __construct()
73 {
74 $this->plugin = Plugin::getInstance();
75 }
76
77 /**
78 * Export EDD Downloads data
79 *
80 * @param int $id Download ID
81 * @return void
82 */
83 public function getEDDDownloadDataMaster($id)
84 {
85 error_log("EDD Export function started. ID: " . $id);
86 global $wpdb;
87
88 // Validate download post type
89 $post_type = $wpdb->get_var(
90 $wpdb->prepare(
91 "SELECT post_type FROM {$wpdb->posts} WHERE ID = %d",
92 $id
93 )
94 );
95
96 if ($post_type !== 'download') {
97 return;
98 }
99
100
101 $download_files_raw = get_post_meta($id, 'edd_download_files', true);
102 $download_files_output = '';
103
104 if (!empty($download_files_raw) && is_array($download_files_raw)) {
105 foreach ($download_files_raw as $file) {
106 if (!empty($file['file'])) {
107 $download_files_output .=
108 'file_name:' . ($file['name'] ?? '') .
109 ',file_url:' . ($file['file'] ?? '') . ' | ';
110 }
111 }
112 $download_files_output = rtrim($download_files_output, ' | ');
113 }
114
115
116 $variable_prices_raw = get_post_meta($id, 'edd_variable_prices', true);
117 $variable_pricing = (int) get_post_meta($id, '_variable_pricing', true);
118 $variable_prices_output = '';
119
120 if ($variable_pricing === 1 && !empty($variable_prices_raw) && is_array($variable_prices_raw)) {
121 foreach ($variable_prices_raw as $price_id => $price) {
122 $variable_prices_output .=
123 'price_id:' . $price_id .
124 ',name:' . ($price['name'] ?? '') .
125 ',amount:' . ($price['amount'] ?? '') .
126 ',sale_price:' . ($price['sale_price'] ?? '') . ' | ';
127 }
128 $variable_prices_output = rtrim($variable_prices_output, ' | ');
129 }
130
131
132 $faq_raw = get_post_meta($id, 'edd_faq', true);
133 $faq_output = '';
134
135 if (!empty($faq_raw) && is_array($faq_raw)) {
136 foreach ($faq_raw as $faq) {
137 $faq_output .=
138 'question:' . ($faq['question'] ?? '') .
139 ',answer:' . ($faq['answer'] ?? '') . ' | ';
140 }
141 $faq_output = rtrim($faq_output, ' | ');
142 }
143
144
145 $thumbnail_id = (int) get_post_thumbnail_id($id);
146 $price = get_post_meta($id, 'edd_price', true);
147 $sales = (int) get_post_meta($id, '_edd_download_sales', true);
148 $earnings = get_post_meta($id, '_edd_download_earnings', true);
149 $download_limit = (int) get_post_meta($id, '_edd_download_limit', true);
150 $button_behavior = get_post_meta($id, '_edd_button_behavior', true);
151 $sku = get_post_meta($id, 'edd_sku', true);
152 $featured = (int) get_post_meta($id, '_edd_featured', true);
153 $product_notes = get_post_meta($id, 'edd_product_notes', true);
154 $default_price_id = (int) get_post_meta($id, '_edd_default_price_id', true);
155 $price_options_mode = (int) get_post_meta($id, '_edd_price_options_mode', true);
156 $refund_window = (int) get_post_meta($id, '_edd_refund_window', true);
157 $hide_purchase_link = (int) get_post_meta($id, '_edd_hide_purchase_link', true);
158 $bundle_conditions = get_post_meta($id, '_edd_bundled_products_conditions', true);
159 $bundle_products = get_post_meta($id, '_edd_bundled_products', true);
160 $edit_lock = get_post_meta($id, '_edit_lock', true);
161 $edit_last = get_post_meta($id, '_edit_last', true);
162
163 $bundle_conditions = is_array($bundle_conditions)
164 ? $bundle_conditions
165 : (strlen($bundle_conditions) ? [$bundle_conditions] : []);
166
167 $bundle_products = is_array($bundle_products)
168 ? $bundle_products
169 : (strlen($bundle_products) ? [$bundle_products] : []);
170
171 // Convert to comma-separated strings
172 $bundle_conditions = implode('|', $bundle_conditions);
173 $bundle_products = implode('|', $bundle_products);
174
175 // Log
176 error_log('Bundle conditions: ' . $bundle_conditions);
177 error_log('Bundle products: ' . $bundle_products);
178
179 $product_type = get_post_meta($id, '_edd_product_type', true);
180 $feature_download = (int) get_post_meta($id, 'edd_feature_download', true);
181
182 $status = get_post_status($id);
183 $publish_date = get_post_field('post_date', $id);
184
185
186 $categories = wp_get_post_terms($id, 'download_category', ['fields' => 'names']);
187 $tags = wp_get_post_terms($id, 'download_tag', ['fields' => 'names']);
188
189
190
191
192 EDDExport::$export_instance->data[$id]['thumbnail_id'] = $thumbnail_id ?: 0;
193 EDDExport::$export_instance->data[$id]['price'] = $price ?: '';
194 EDDExport::$export_instance->data[$id]['variable_pricing'] = $variable_pricing ?: 0;
195 EDDExport::$export_instance->data[$id]['variable_prices'] = $variable_prices_output ?: '';
196 EDDExport::$export_instance->data[$id]['default_price_id'] = $default_price_id ?: 0;
197 EDDExport::$export_instance->data[$id]['price_options_mode'] = $price_options_mode ?: 0;
198 EDDExport::$export_instance->data[$id]['download_sales'] = $sales ?: 0;
199 EDDExport::$export_instance->data[$id]['download_earnings'] = $earnings ?: '';
200 EDDExport::$export_instance->data[$id]['download_limit'] = $download_limit ?: 0;
201 EDDExport::$export_instance->data[$id]['refund_window'] = $refund_window ?: 0;
202 EDDExport::$export_instance->data[$id]['hide_purchase_link'] = $hide_purchase_link ?: 0;
203 EDDExport::$export_instance->data[$id]['button_behavior'] = $button_behavior ?: '';
204 EDDExport::$export_instance->data[$id]['sku'] = $sku ?: '';
205 EDDExport::$export_instance->data[$id]['product_type'] = $product_type ?: '';
206 EDDExport::$export_instance->data[$id]['featured'] = $featured ?: 0;
207 EDDExport::$export_instance->data[$id]['feature_download'] = $feature_download ?: 0;
208 EDDExport::$export_instance->data[$id]['product_notes'] = $product_notes ?: '';
209 EDDExport::$export_instance->data[$id]['download_files'] = $download_files_output ?: '';
210 EDDExport::$export_instance->data[$id]['bundled_products_conditions'] = $bundle_conditions ?: '';
211 EDDExport::$export_instance->data[$id]['bundled_products'] = $bundle_products ?: '';
212 EDDExport::$export_instance->data[$id]['edit_lock'] = $edit_lock ?: '';
213 EDDExport::$export_instance->data[$id]['edit_last'] = $edit_last ?: '';
214
215 EDDExport::$export_instance->data[$id]['download_category'] = !empty($categories) ? implode(',', $categories) : '';
216 EDDExport::$export_instance->data[$id]['download_tag'] = !empty($tags) ? implode(',', $tags) : '';
217 EDDExport::$export_instance->data[$id]['faq'] = $faq_output ?: '';
218
219 error_log("EDD Export completed for ID: " . $id);
220 }
221
222 /**
223 * Export EDD Customers data
224 *
225 * @param int $id Customer ID
226 * @return void
227 */
228 public function getEDDCustomerDataMaster($id)
229 {
230 global $wpdb;
231
232 if (!class_exists('Easy_Digital_Downloads')) {
233 return;
234 }
235
236 $customers_table = $wpdb->prefix . 'edd_customers';
237 $customer = $wpdb->get_row($wpdb->prepare("SELECT * FROM $customers_table WHERE id = %d", $id));
238
239 if (!$customer) {
240 return;
241 }
242
243 // Split name into first and last name
244 $name_parts = explode(' ', trim($customer->name), 2);
245 $first_name = $name_parts[0] ?? '';
246 $last_name = $name_parts[1] ?? '';
247
248 // Get customer address from wp_edd_customer_addresses
249 $addresses_table = $wpdb->prefix . 'edd_customer_addresses';
250 $address_data = $wpdb->get_row($wpdb->prepare(
251 "SELECT * FROM $addresses_table WHERE customer_id = %d AND type = 'billing' AND is_primary = 1 LIMIT 1",
252 $id
253 ));
254
255 // Get phone number and notes from customermeta
256 $meta_table = $wpdb->prefix . 'edd_customermeta';
257 $phone_meta = $wpdb->get_var($wpdb->prepare(
258 "SELECT meta_value FROM $meta_table WHERE edd_customer_id = %d AND meta_key = 'phone'",
259 $id
260 ));
261
262 // Get customer notes from wp_edd_notes table (EDD 3.0+)
263 $notes_table = $wpdb->prefix . 'edd_notes';
264 $notes_data = $wpdb->get_results($wpdb->prepare(
265 "SELECT content FROM $notes_table WHERE object_id = %d AND object_type = 'customer' ORDER BY date_created ASC",
266 $id
267 ));
268
269 $notes_array = array();
270 foreach ($notes_data as $note) {
271 $notes_array[] = $note->content;
272 }
273 $notes = implode('|', $notes_array);
274
275
276 // Get purchase history (order IDs) - Support both EDD 2.x and 3.0+
277 $order_ids = array();
278
279 if ($this->is_edd_3_0_plus()) {
280 // EDD 3.0+ - Query from custom tables
281 $orders_table = $wpdb->prefix . 'edd_orders';
282 $orders = $wpdb->get_results($wpdb->prepare(
283 "SELECT id FROM $orders_table WHERE customer_id = %d ORDER BY id ASC",
284 $id
285 ));
286 foreach ($orders as $order) {
287 $order_ids[] = $order->id;
288 }
289 } else {
290 // Legacy EDD 2.x - Query from posts table
291 $payments = $wpdb->get_results($wpdb->prepare(
292 "SELECT ID FROM {$wpdb->posts} WHERE post_type = 'edd_payment'
293 AND ID IN (SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = '_edd_payment_customer_id' AND meta_value = %d)",
294 $id
295 ));
296 foreach ($payments as $payment) {
297 $order_ids[] = $payment->ID;
298 }
299 }
300
301
302 // Get WordPress user data if user_id exists
303 $user_data = null;
304 $username = '';
305 $user_status = '';
306 if ($customer->user_id > 0) {
307 $user_data = get_userdata($customer->user_id);
308 if ($user_data) {
309 $username = $user_data->user_login;
310 // Get user status - check if user exists and is not deleted
311 $user_status = $user_data ? 'active' : 'inactive';
312 }
313 }
314
315 // Export all fields
316 EDDExport::$export_instance->data[$id]['id'] = $customer->id;
317 EDDExport::$export_instance->data[$id]['email'] = $customer->email;
318 EDDExport::$export_instance->data[$id]['name'] = $customer->name;
319 EDDExport::$export_instance->data[$id]['first_name'] = $first_name;
320 EDDExport::$export_instance->data[$id]['last_name'] = $last_name;
321 EDDExport::$export_instance->data[$id]['user_id'] = $customer->user_id;
322 EDDExport::$export_instance->data[$id]['username'] = $username;
323 EDDExport::$export_instance->data[$id]['user_status'] = $user_status;
324 EDDExport::$export_instance->data[$id]['status'] = $customer->status ?: '';
325 EDDExport::$export_instance->data[$id]['phone'] = $phone_meta ?: '';
326 EDDExport::$export_instance->data[$id]['address'] = $address_data ? $address_data->address : '';
327 EDDExport::$export_instance->data[$id]['address2'] = $address_data ? $address_data->address2 : '';
328 EDDExport::$export_instance->data[$id]['city'] = $address_data ? $address_data->city : '';
329 EDDExport::$export_instance->data[$id]['region'] = $address_data ? $address_data->region : '';
330 EDDExport::$export_instance->data[$id]['postal_code'] = $address_data ? $address_data->postal_code : '';
331 EDDExport::$export_instance->data[$id]['country'] = $address_data ? $address_data->country : '';
332 EDDExport::$export_instance->data[$id]['purchase_count'] = $customer->purchase_count;
333 EDDExport::$export_instance->data[$id]['purchase_value'] = $customer->purchase_value;
334 EDDExport::$export_instance->data[$id]['date_created'] = $customer->date_created;
335 EDDExport::$export_instance->data[$id]['order_ids'] = implode(',', $order_ids);
336 EDDExport::$export_instance->data[$id]['notes'] = $notes ?: '';
337 }
338
339 /**
340 * Export EDD Discounts data
341 *
342 * @param int $id Discount ID
343 * @return void
344 */
345 public function getEDDDiscountDataMaster($id)
346 {
347 global $wpdb;
348 error_log('DEBUG: getEDDDiscountDataMaster called for ID: ' . $id);
349
350 if (!class_exists('Easy_Digital_Downloads')) {
351 return;
352 }
353
354
355 // Check EDD version and use appropriate API
356 if ($this->is_edd_3_0_plus()) {
357 // EDD 3.0+ - Query from wp_edd_adjustments table
358 try {
359 $adjustments_table = $wpdb->prefix . 'edd_adjustments';
360 $discount = $wpdb->get_row($wpdb->prepare(
361 "SELECT * FROM $adjustments_table WHERE id = %d AND type = 'discount'",
362 $id
363 ));
364
365 if (!$discount) {
366 // Try alternative: check if discount code exists
367 $discount_by_code = $wpdb->get_row($wpdb->prepare(
368 "SELECT * FROM $adjustments_table WHERE code = %s AND type = 'discount'",
369 $id
370 ));
371 if ($discount_by_code) {
372 $discount = $discount_by_code;
373 } else {
374 return;
375 }
376 }
377
378 // Get discount meta from wp_edd_adjustmentmeta
379 $meta_table = $wpdb->prefix . 'edd_adjustmentmeta';
380 $discount_meta = $wpdb->get_results($wpdb->prepare(
381 "SELECT meta_key, meta_value FROM $meta_table WHERE edd_adjustment_id = %d",
382 $discount->id
383 ));
384
385 $product_requirements = array();
386 $excluded_products = array();
387 $category_requirements = array();
388 $excluded_categories = array();
389 $min_price = $discount->min_charge_amount ?: '';
390 $max_price = '';
391 $max_uses_per_user = $discount->once_per_customer ?: '';
392 $product_condition = '';
393
394 foreach ($discount_meta as $meta) {
395 switch ($meta->meta_key) {
396 case 'product_requirements':
397 case 'product_requirement':
398 $val = maybe_unserialize($meta->meta_value);
399 $product_requirements = is_array($val) ? $val : array($val);
400 break;
401 case 'excluded_products':
402 case 'excluded_product':
403 $val = maybe_unserialize($meta->meta_value);
404 $excluded_products = is_array($val) ? $val : array($val);
405 break;
406 case 'category_requirements':
407 case 'categories':
408 $val = maybe_unserialize($meta->meta_value);
409 $category_requirements = is_array($val) ? $val : array($val);
410 break;
411 case 'excluded_categories':
412 case 'excluded_category':
413 $val = maybe_unserialize($meta->meta_value);
414 $excluded_categories = is_array($val) ? $val : array($val);
415 break;
416 case 'min_price':
417 $min_price = $meta->meta_value;
418 break;
419 case 'max_price':
420 $max_price = $meta->meta_value;
421 break;
422 case 'max_uses_per_user':
423 $max_uses_per_user = $meta->meta_value;
424 break;
425 case 'product_condition':
426 $product_condition = $meta->meta_value;
427 break;
428 }
429 }
430
431 // Map adjustment fields to discount fields
432 EDDExport::$export_instance->data[$id]['name'] = $discount->name ?: '';
433 EDDExport::$export_instance->data[$id]['code'] = $discount->code ?: '';
434 EDDExport::$export_instance->data[$id]['amount'] = $discount->amount ?: 0;
435 EDDExport::$export_instance->data[$id]['type'] = $discount->amount_type ?: 'percentage';
436 EDDExport::$export_instance->data[$id]['status'] = $discount->status ?: 'active';
437 EDDExport::$export_instance->data[$id]['start_date'] = $discount->start_date ?: '';
438 EDDExport::$export_instance->data[$id]['end_date'] = $discount->end_date ?: '';
439 EDDExport::$export_instance->data[$id]['max_uses'] = $discount->max_uses ?: 0;
440 EDDExport::$export_instance->data[$id]['use_count'] = $discount->use_count ?: 0;
441 EDDExport::$export_instance->data[$id]['product_requirements'] = is_array($product_requirements) ? implode(',', $product_requirements) : '';
442 EDDExport::$export_instance->data[$id]['product_condition'] = $product_condition;
443 EDDExport::$export_instance->data[$id]['excluded_products'] = is_array($excluded_products) ? implode(',', $excluded_products) : '';
444 EDDExport::$export_instance->data[$id]['category_requirements'] = is_array($category_requirements) ? implode(',', $category_requirements) : '';
445 EDDExport::$export_instance->data[$id]['min_price'] = $min_price;
446 EDDExport::$export_instance->data[$id]['max_uses_per_user'] = $max_uses_per_user;
447
448 // Get discount notes from wp_edd_notes
449 $notes_table = $wpdb->prefix . 'edd_notes';
450 $notes_data = $wpdb->get_results($wpdb->prepare(
451 "SELECT content FROM $notes_table WHERE object_id = %d AND object_type = 'discount'",
452 $discount->id
453 ));
454 $discount_notes = array();
455 foreach ($notes_data as $note) {
456 $discount_notes[] = $note->content;
457 }
458 EDDExport::$export_instance->data[$id]['notes'] = implode('|', $discount_notes);
459
460 return;
461 } catch (Exception $e) {
462 // Fallback to legacy method
463 }
464 }
465
466 // Legacy EDD 2.x - Query from wp_edd_discounts table
467 $discounts_table = $wpdb->prefix . 'edd_discounts';
468
469 // Legacy EDD 2.x - Query from wp_edd_discounts table
470
471 $discounts_table = $wpdb->prefix . 'edd_discounts';
472
473 if (!$wpdb->get_var("SHOW TABLES LIKE '$discounts_table'")) {
474 return;
475 }
476
477 $discount = $wpdb->get_row($wpdb->prepare("SELECT * FROM $discounts_table WHERE id = %d", $id));
478
479 if (!$discount) {
480 return;
481 }
482
483 // Get discount meta for restrictions
484 $meta_table = $wpdb->prefix . 'edd_discountmeta';
485 $discount_meta = $wpdb->get_results($wpdb->prepare(
486 "SELECT meta_key, meta_value FROM $meta_table WHERE discount_id = %d",
487 $id
488 ));
489
490 $product_requirements = array();
491 $excluded_products = array();
492 $category_requirements = array();
493 $excluded_categories = array();
494 $min_price = '';
495 $max_price = '';
496 $max_uses_per_user = '';
497 $product_condition = '';
498
499 foreach ($discount_meta as $meta) {
500 if (empty($meta->meta_key))
501 continue;
502
503 switch ($meta->meta_key) {
504 case '_edd_discount_product_reqs':
505 $product_requirements = maybe_unserialize($meta->meta_value);
506 break;
507 case '_edd_discount_excluded_products':
508 $excluded_products = maybe_unserialize($meta->meta_value);
509 break;
510 case '_edd_discount_product_condition':
511 $product_condition = $meta->meta_value;
512 break;
513 case '_edd_discount_category_reqs':
514 $category_requirements = maybe_unserialize($meta->meta_value);
515 break;
516 case '_edd_discount_excluded_categories':
517 $excluded_categories = maybe_unserialize($meta->meta_value);
518 break;
519 case '_edd_discount_min_price':
520 $min_price = $meta->meta_value;
521 break;
522 case '_edd_discount_max_price':
523 $max_price = $meta->meta_value;
524 break;
525 case '_edd_discount_max_uses_per_user':
526 $max_uses_per_user = $meta->meta_value;
527 break;
528 }
529 }
530
531 EDDExport::$export_instance->data[$id]['code'] = $discount->code;
532 EDDExport::$export_instance->data[$id]['amount'] = $discount->amount;
533 EDDExport::$export_instance->data[$id]['type'] = $discount->type;
534 EDDExport::$export_instance->data[$id]['status'] = $discount->status;
535 EDDExport::$export_instance->data[$id]['start_date'] = $discount->start_date;
536 EDDExport::$export_instance->data[$id]['end_date'] = $discount->end_date;
537 EDDExport::$export_instance->data[$id]['max_uses'] = $discount->max_uses;
538 EDDExport::$export_instance->data[$id]['use_count'] = $discount->use_count;
539 EDDExport::$export_instance->data[$id]['excluded_products'] = is_array($excluded_products) ? implode(',', $excluded_products) : '';
540 EDDExport::$export_instance->data[$id]['category_requirements'] = is_array($category_requirements) ? implode(',', $category_requirements) : '';
541 EDDExport::$export_instance->data[$id]['excluded_categories'] = is_array($excluded_categories) ? implode(',', $excluded_categories) : '';
542 EDDExport::$export_instance->data[$id]['min_price'] = $min_price;
543 EDDExport::$export_instance->data[$id]['max_uses_per_user'] = $max_uses_per_user;
544 EDDExport::$export_instance->data[$id]['notes'] = get_post_meta($id, '_edd_discount_notes', true);
545 }
546
547 /**
548 * Export EDD Reports (Sales / Earnings / Tax / Fees)
549 *
550 * @param string $report_type Type of report (sales, earnings, tax, fees, gateway_wise, product_wise)
551 * @param string $start_date Start date for report
552 * @param string $end_date End date for report
553 * @return void
554 */
555 public function getEDDReportsDataMaster($report_type = 'sales', $start_date = '', $end_date = '')
556 {
557 global $wpdb;
558
559 if (!class_exists('Easy_Digital_Downloads')) {
560 return;
561 }
562
563 $payments_table = $wpdb->prefix . 'posts';
564 $postmeta_table = $wpdb->prefix . 'postmeta';
565 $customers_table = $wpdb->prefix . 'edd_customers';
566
567 // Set default date range if not provided
568 if (empty($start_date)) {
569 $start_date = date('Y-m-d', strtotime('-30 days'));
570 }
571 if (empty($end_date)) {
572 $end_date = current_time('mysql');
573 }
574
575 $date_condition = $wpdb->prepare(
576 "AND p.post_date >= %s AND p.post_date <= %s",
577 $start_date,
578 $end_date
579 );
580
581 $reports_data = array();
582 $index = 0;
583
584 switch ($report_type) {
585 case 'sales':
586 case 'earnings':
587 // Get sales/earnings by date
588 $query = "
589 SELECT
590 DATE(p.post_date) as date,
591 COUNT(DISTINCT p.ID) as order_count,
592 SUM(CAST(pm_total.meta_value AS DECIMAL(10,2))) as total_earnings,
593 SUM(CAST(pm_tax.meta_value AS DECIMAL(10,2))) as total_tax,
594 SUM(CAST(pm_fee.meta_value AS DECIMAL(10,2))) as total_fees
595 FROM $payments_table p
596 LEFT JOIN $postmeta_table pm_total ON p.ID = pm_total.post_id AND pm_total.meta_key = '_edd_payment_total'
597 LEFT JOIN $postmeta_table pm_tax ON p.ID = pm_tax.post_id AND pm_tax.meta_key = '_edd_payment_tax'
598 LEFT JOIN $postmeta_table pm_fee ON p.ID = pm_fee.post_id AND pm_fee.meta_key = '_edd_payment_fee'
599
600 // Check EDD version and use appropriate tables
601 if ($this->is_edd_3_0_plus() && class_exists('\EDD\Orders\Order_Query')) {
602 // EDD 3.0+ - Query from custom tables
603 $orders_table = $wpdb->prefix . 'edd_orders';
604 $order_items_table = $wpdb->prefix . 'edd_order_items';
605 $adjustments_table = $wpdb->prefix . 'edd_adjustments';
606
607 // Update queries to use custom tables instead of posts
608 // This is a placeholder - actual implementation depends on report type
609 // For now, we'll use the Order_Query class
610 $order_query = new \EDD\Orders\Order_Query();
611 $order_query->set('number', -1); // Get all orders
612 if (!empty($start_date)) {
613 $order_query->set('date_query', array(
614 array(
615 'after' => $start_date,
616 'before' => $end_date,
617 'inclusive' => true,
618 ),
619 ));
620 }
621 $orders = $order_query->get_orders();
622
623 // Process orders for reports
624 // Note: This is a simplified version - full implementation would
625 // need to handle all report types (sales, earnings, gateway-wise, etc.)
626 return; // Early return to use EDD 3.0+ data
627 }
628
629 // Legacy EDD 2.x - Query from posts table
630
631 WHERE p.post_type = 'edd_payment'
632 AND p.post_status = 'publish'
633 $date_condition
634 GROUP BY DATE(p.post_date)
635 ORDER BY date DESC
636 ";
637 $results = $wpdb->get_results($query);
638 foreach ($results as $row) {
639 EDDExport::$export_instance->data[$index]['report_date'] = $row->date;
640 EDDExport::$export_instance->data[$index]['order_count'] = $row->order_count;
641 EDDExport::$export_instance->data[$index]['total_earnings'] = $row->total_earnings;
642 EDDExport::$export_instance->data[$index]['total_tax'] = $row->total_tax ?: 0;
643 EDDExport::$export_instance->data[$index]['total_fees'] = $row->total_fees ?: 0;
644 $index++;
645 }
646 break;
647
648 case 'gateway_wise':
649 // Get sales by payment gateway
650 $query = "
651 SELECT
652 pm_gateway.meta_value as gateway,
653 COUNT(DISTINCT p.ID) as order_count,
654 SUM(CAST(pm_total.meta_value AS DECIMAL(10,2))) as total_earnings
655 FROM $payments_table p
656 LEFT JOIN $postmeta_table pm_total ON p.ID = pm_total.post_id AND pm_total.meta_key = '_edd_payment_total'
657 LEFT JOIN $postmeta_table pm_gateway ON p.ID = pm_gateway.post_id AND pm_gateway.meta_key = '_edd_payment_gateway'
658 WHERE p.post_type = 'edd_payment'
659 AND p.post_status = 'publish'
660 $date_condition
661 GROUP BY pm_gateway.meta_value
662 ORDER BY total_earnings DESC
663 ";
664 $results = $wpdb->get_results($query);
665 foreach ($results as $row) {
666 EDDExport::$export_instance->data[$index]['gateway'] = $row->gateway ?: 'manual';
667 EDDExport::$export_instance->data[$index]['order_count'] = $row->order_count;
668 EDDExport::$export_instance->data[$index]['total_earnings'] = $row->total_earnings;
669 $index++;
670 }
671 break;
672
673 case 'product_wise':
674 // Get sales by product
675 $payments = $wpdb->get_results($wpdb->prepare(
676 "SELECT p.ID, pm_total.meta_value as total, pm_meta.meta_value as meta
677 FROM $payments_table p
678 LEFT JOIN $postmeta_table pm_total ON p.ID = pm_total.post_id AND pm_total.meta_key = '_edd_payment_total'
679 LEFT JOIN $postmeta_table pm_meta ON p.ID = pm_meta.post_id AND pm_meta.meta_key = '_edd_payment_meta'
680 WHERE p.post_type = 'edd_payment' AND p.post_status = 'publish' $date_condition
681 LIMIT 1000"
682 ));
683
684 $product_stats = array();
685 foreach ($payments as $payment) {
686 $meta = maybe_unserialize($payment->meta);
687 if (isset($meta['cart_details']) && is_array($meta['cart_details'])) {
688 foreach ($meta['cart_details'] as $item) {
689 $download_id = isset($item['id']) ? $item['id'] : 0;
690 if ($download_id > 0) {
691 if (!isset($product_stats[$download_id])) {
692 $product_stats[$download_id] = array(
693 'download_id' => $download_id,
694 'download_name' => get_the_title($download_id),
695 'order_count' => 0,
696 'total_earnings' => 0
697 );
698 }
699 $product_stats[$download_id]['order_count']++;
700 $product_stats[$download_id]['total_earnings'] += floatval($payment->total);
701 }
702 }
703 }
704 }
705
706 foreach ($product_stats as $stat) {
707 EDDExport::$export_instance->data[$index]['download_id'] = $stat['download_id'];
708 EDDExport::$export_instance->data[$index]['download_name'] = $stat['download_name'];
709 EDDExport::$export_instance->data[$index]['order_count'] = $stat['order_count'];
710 EDDExport::$export_instance->data[$index]['total_earnings'] = $stat['total_earnings'];
711 $index++;
712 }
713 break;
714
715 case 'tax':
716 case 'fees':
717 // Get tax/fee breakdown
718 $query = "
719 SELECT
720 DATE(p.post_date) as date,
721 SUM(CAST(pm_tax.meta_value AS DECIMAL(10,2))) as total_tax,
722 SUM(CAST(pm_fee.meta_value AS DECIMAL(10,2))) as total_fees
723 FROM $payments_table p
724 LEFT JOIN $postmeta_table pm_tax ON p.ID = pm_tax.post_id AND pm_tax.meta_key = '_edd_payment_tax'
725 LEFT JOIN $postmeta_table pm_fee ON p.ID = pm_fee.post_id AND pm_fee.meta_key = '_edd_payment_fee'
726 WHERE p.post_type = 'edd_payment'
727 AND p.post_status = 'publish'
728 $date_condition
729 GROUP BY DATE(p.post_date)
730 ORDER BY date DESC
731 ";
732 $results = $wpdb->get_results($query);
733 foreach ($results as $row) {
734 EDDExport::$export_instance->data[$index]['report_date'] = $row->date;
735 EDDExport::$export_instance->data[$index]['total_tax'] = $row->total_tax ?: 0;
736 EDDExport::$export_instance->data[$index]['total_fees'] = $row->total_fees ?: 0;
737 $index++;
738 }
739 break;
740 }
741 }
742
743 /**
744 * Export EDD Orders data
745 * @param int $id
746 */
747 public function getEDDOrderDataMaster($id)
748 {
749 error_log("Exporting EDD Order ID: " . $id);
750
751 if ($this->is_edd_3_0_plus()) {
752 try {
753 $order = edd_get_order($id);
754 if (!$order) {
755 error_log("Order not found for ID: " . $id);
756 return;
757 }
758
759 EDDExport::$export_instance->data[$id]['order_id'] = $order->id;
760 EDDExport::$export_instance->data[$id]['order_number'] = $order->number;
761 EDDExport::$export_instance->data[$id]['status'] = $order->status;
762 EDDExport::$export_instance->data[$id]['customer_id'] = $order->customer_id;
763 EDDExport::$export_instance->data[$id]['payment_method'] = $order->payment_method;
764 EDDExport::$export_instance->data[$id]['transaction_id'] = $order->transaction_id;
765 EDDExport::$export_instance->data[$id]['ip'] = $order->ip;
766 EDDExport::$export_instance->data[$id]['total'] = $order->total;
767 EDDExport::$export_instance->data[$id]['tax'] = $order->tax;
768 EDDExport::$export_instance->data[$id]['currency'] = $order->currency;
769 EDDExport::$export_instance->data[$id]['date_created'] = $order->date_created;
770 EDDExport::$export_instance->data[$id]['completed_date'] = $order->completed_date;
771 EDDExport::$export_instance->data[$id]['mode'] = $order->mode;
772 EDDExport::$export_instance->data[$id]['parent_payment_id'] = $order->parent_payment_id;
773 EDDExport::$export_instance->data[$id]['user_id'] = $order->user_id;
774
775 // Address (if available)
776 if (isset($order->billing_address)) {
777 EDDExport::$export_instance->data[$id]['billing_address_1'] = isset($order->billing_address['line1']) ? $order->billing_address['line1'] : '';
778 EDDExport::$export_instance->data[$id]['billing_address_2'] = isset($order->billing_address['line2']) ? $order->billing_address['line2'] : '';
779 EDDExport::$export_instance->data[$id]['billing_city'] = isset($order->billing_address['city']) ? $order->billing_address['city'] : '';
780 EDDExport::$export_instance->data[$id]['billing_zip'] = isset($order->billing_address['zip']) ? $order->billing_address['zip'] : '';
781 EDDExport::$export_instance->data[$id]['billing_state'] = isset($order->billing_address['state']) ? $order->billing_address['state'] : '';
782 EDDExport::$export_instance->data[$id]['billing_country'] = isset($order->billing_address['country']) ? $order->billing_address['country'] : '';
783 }
784
785 error_log("Successfully exported data for Order ID: " . $id);
786
787 } catch (\Exception $e) {
788 error_log("Error exporting EDD Order ID " . $id . ": " . $e->getMessage());
789 }
790 } else {
791 // Fallback for older EDD versions (if needed, but usually wp_posts based)
792 // For now assume EDD 3.0+ as per task context
793 }
794 }
795 }
796
797