Addons
2 years ago
License
1 year ago
Pro
2 years ago
VariationOptions
2 years ago
CategoriesService.php
5 years ago
ScheduledExport.php
3 years ago
SnippetParser.php
8 years ago
WooCommerceVersion.php
8 years ago
ScheduledExport.php
128 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Wpae\App\Service; |
| 4 | |
| 5 | |
| 6 | use Wpae\App\Service\Addons\AddonNotFoundException; |
| 7 | |
| 8 | class ScheduledExport |
| 9 | { |
| 10 | /** |
| 11 | * @param $export |
| 12 | * @return JsonResponse |
| 13 | */ |
| 14 | public function trigger($export) |
| 15 | { |
| 16 | if ((int)$export->executing) { |
| 17 | return new JsonResponse(array( |
| 18 | 'status' => 403, |
| 19 | 'message' => sprintf(esc_html__('Export #%s is currently in manually process. Request skipped.', 'wp_all_export_plugin'), $export->id) |
| 20 | )); |
| 21 | } |
| 22 | if ($export->processing and !$export->triggered) { |
| 23 | return new JsonResponse(array( |
| 24 | 'status' => 403, |
| 25 | 'message' => sprintf(esc_html__('Export #%s currently in process. Request skipped.', 'wp_all_export_plugin'), $export->id) |
| 26 | )); |
| 27 | } |
| 28 | if (!$export->processing and $export->triggered) { |
| 29 | return new JsonResponse(array( |
| 30 | 'status' => 403, |
| 31 | 'message' => sprintf('Export #%s already triggered. Request skipped.', $export->id) |
| 32 | )); |
| 33 | } |
| 34 | |
| 35 | $export->set(array( |
| 36 | 'triggered' => 1, |
| 37 | 'exported' => 0, |
| 38 | 'last_activity' => date('Y-m-d H:i:s') |
| 39 | ))->update(); |
| 40 | |
| 41 | return new JsonResponse(array( |
| 42 | 'status' => 200, |
| 43 | 'message' => sprintf(esc_html__('#%s Cron job triggered.', 'wp_all_export_plugin'), $export->id) |
| 44 | )); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @param $export |
| 49 | * @param $queue_exports |
| 50 | * @param $logger |
| 51 | */ |
| 52 | public function process($export, $queue_exports, $logger) |
| 53 | { |
| 54 | if ($export->processing == 1 and (time() - strtotime($export->registered_on)) > 120) { // it means processor crashed, so it will reset processing to false, and terminate. Then next run it will work normally. |
| 55 | $export->set(array( |
| 56 | 'processing' => 0 |
| 57 | ))->update(); |
| 58 | } |
| 59 | |
| 60 | // start execution imports that is in the cron process |
| 61 | if (!(int)$export->triggered) { |
| 62 | if (!empty($export->parent_id) or empty($queue_exports)) { |
| 63 | wp_send_json(array( |
| 64 | 'status' => 403, |
| 65 | 'message' => sprintf(esc_html__('Export #%s is not triggered. Request skipped.', 'wp_all_export_plugin'), $export->id) |
| 66 | )); |
| 67 | } |
| 68 | } elseif ((int)$export->executing) { |
| 69 | wp_send_json(array( |
| 70 | 'status' => 403, |
| 71 | 'message' => sprintf(esc_html__('Export #%s is currently in manually process. Request skipped.', 'wp_all_export_plugin'), $export->id) |
| 72 | )); |
| 73 | } elseif ((int)$export->triggered and !(int)$export->processing) { |
| 74 | try { |
| 75 | $response = $export->set(array('canceled' => 0))->execute($logger, true); |
| 76 | } catch (AddonNotFoundException $e) { |
| 77 | die($e->getMessage()); |
| 78 | } |
| 79 | if (!(int)$export->triggered and !(int)$export->processing) { |
| 80 | |
| 81 | // trigger update child exports with correct WHERE & JOIN filters |
| 82 | if (!empty($export->options['cpt']) and class_exists('WooCommerce') and in_array('shop_order', $export->options['cpt']) and empty($export->parent_id)) { |
| 83 | $queue_exports = XmlExportWooCommerceOrder::prepare_child_exports($export, true); |
| 84 | |
| 85 | if (empty($queue_exports)) { |
| 86 | delete_option('wp_all_export_queue_' . $export->id); |
| 87 | } else { |
| 88 | update_option('wp_all_export_queue_' . $export->id, $queue_exports); |
| 89 | } |
| 90 | } |
| 91 | // remove child export from queue |
| 92 | if (!empty($export->parent_id)) { |
| 93 | $queue_exports = get_option('wp_all_export_queue_' . $export->parent_id); |
| 94 | |
| 95 | if (!empty($queue_exports)) { |
| 96 | foreach ($queue_exports as $key => $queue_export) { |
| 97 | if ($queue_export == $export->id) { |
| 98 | unset($queue_exports[$key]); |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | if (empty($queue_exports)) { |
| 104 | delete_option('wp_all_export_queue_' . $export->parent_id); |
| 105 | } else { |
| 106 | update_option('wp_all_export_queue_' . $export->parent_id, $queue_exports); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | wp_send_json(array( |
| 111 | 'status' => 200, |
| 112 | 'message' => sprintf(esc_html__('Export #%s complete', 'wp_all_export_plugin'), $export->id) |
| 113 | )); |
| 114 | } else { |
| 115 | wp_send_json(array( |
| 116 | 'status' => 200, |
| 117 | 'message' => sprintf(esc_html__('Records Processed %s.', 'wp_all_export_plugin'), (int)$export->exported) |
| 118 | )); |
| 119 | } |
| 120 | |
| 121 | } else { |
| 122 | wp_send_json(array( |
| 123 | 'status' => 403, |
| 124 | 'message' => sprintf(esc_html__('Export #%s already processing. Request skipped.', 'wp_all_export_plugin'), $export->id) |
| 125 | )); |
| 126 | } |
| 127 | } |
| 128 | } |