Addons
2 years ago
License
1 year ago
Pro
2 years ago
VariationOptions
2 years ago
CategoriesService.php
5 years ago
ScheduledExport.php
4 weeks ago
SnippetParser.php
8 years ago
WooCommerceVersion.php
8 years ago
ScheduledExport.php
136 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 | /* translators: %s: export ID */ |
| 20 | 'message' => sprintf(esc_html__('Export #%s is currently in manually process. Request skipped.', 'wp-all-export'), $export->id) |
| 21 | )); |
| 22 | } |
| 23 | if ($export->processing and !$export->triggered) { |
| 24 | return new JsonResponse(array( |
| 25 | 'status' => 403, |
| 26 | /* translators: %s: export ID */ |
| 27 | 'message' => sprintf(esc_html__('Export #%s currently in process. Request skipped.', 'wp-all-export'), $export->id) |
| 28 | )); |
| 29 | } |
| 30 | if (!$export->processing and $export->triggered) { |
| 31 | return new JsonResponse(array( |
| 32 | 'status' => 403, |
| 33 | 'message' => sprintf('Export #%s already triggered. Request skipped.', $export->id) |
| 34 | )); |
| 35 | } |
| 36 | |
| 37 | $export->set(array( |
| 38 | 'triggered' => 1, |
| 39 | 'exported' => 0, |
| 40 | 'last_activity' => date('Y-m-d H:i:s') // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date -- DB timestamp must match local-timezone format used by Manage Exports UI readers (mysql2date / strtotime / human_time_diff) |
| 41 | ))->update(); |
| 42 | |
| 43 | return new JsonResponse(array( |
| 44 | 'status' => 200, |
| 45 | /* translators: %s: export ID */ |
| 46 | 'message' => sprintf(esc_html__('#%s Cron job triggered.', 'wp-all-export'), $export->id) |
| 47 | )); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * @param $export |
| 52 | * @param $queue_exports |
| 53 | * @param $logger |
| 54 | */ |
| 55 | public function process($export, $queue_exports, $logger) |
| 56 | { |
| 57 | 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. |
| 58 | $export->set(array( |
| 59 | 'processing' => 0 |
| 60 | ))->update(); |
| 61 | } |
| 62 | |
| 63 | // start execution imports that is in the cron process |
| 64 | if (!(int)$export->triggered) { |
| 65 | if (!empty($export->parent_id) or empty($queue_exports)) { |
| 66 | wp_send_json(array( |
| 67 | 'status' => 403, |
| 68 | /* translators: %s: export ID */ |
| 69 | 'message' => sprintf(esc_html__('Export #%s is not triggered. Request skipped.', 'wp-all-export'), $export->id) |
| 70 | )); |
| 71 | } |
| 72 | } elseif ((int)$export->executing) { |
| 73 | wp_send_json(array( |
| 74 | 'status' => 403, |
| 75 | /* translators: %s: export ID */ |
| 76 | 'message' => sprintf(esc_html__('Export #%s is currently in manually process. Request skipped.', 'wp-all-export'), $export->id) |
| 77 | )); |
| 78 | } elseif ((int)$export->triggered and !(int)$export->processing) { |
| 79 | try { |
| 80 | $response = $export->set(array('canceled' => 0))->execute($logger, true); |
| 81 | } catch (AddonNotFoundException $e) { |
| 82 | die(esc_html($e->getMessage())); |
| 83 | } |
| 84 | if (!(int)$export->triggered and !(int)$export->processing) { |
| 85 | |
| 86 | // trigger update child exports with correct WHERE & JOIN filters |
| 87 | if (!empty($export->options['cpt']) and class_exists('WooCommerce') and in_array('shop_order', $export->options['cpt']) and empty($export->parent_id)) { |
| 88 | $queue_exports = XmlExportWooCommerceOrder::prepare_child_exports($export, true); |
| 89 | |
| 90 | if (empty($queue_exports)) { |
| 91 | delete_option('wp_all_export_queue_' . $export->id); |
| 92 | } else { |
| 93 | update_option('wp_all_export_queue_' . $export->id, $queue_exports); |
| 94 | } |
| 95 | } |
| 96 | // remove child export from queue |
| 97 | if (!empty($export->parent_id)) { |
| 98 | $queue_exports = get_option('wp_all_export_queue_' . $export->parent_id); |
| 99 | |
| 100 | if (!empty($queue_exports)) { |
| 101 | foreach ($queue_exports as $key => $queue_export) { |
| 102 | if ($queue_export == $export->id) { |
| 103 | unset($queue_exports[$key]); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | if (empty($queue_exports)) { |
| 109 | delete_option('wp_all_export_queue_' . $export->parent_id); |
| 110 | } else { |
| 111 | update_option('wp_all_export_queue_' . $export->parent_id, $queue_exports); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | wp_send_json(array( |
| 116 | 'status' => 200, |
| 117 | /* translators: %s: export ID */ |
| 118 | 'message' => sprintf(esc_html__('Export #%s complete', 'wp-all-export'), $export->id) |
| 119 | )); |
| 120 | } else { |
| 121 | wp_send_json(array( |
| 122 | 'status' => 200, |
| 123 | /* translators: %s: number of records processed */ |
| 124 | 'message' => sprintf(esc_html__('Records Processed %s.', 'wp-all-export'), (int)$export->exported) |
| 125 | )); |
| 126 | } |
| 127 | |
| 128 | } else { |
| 129 | wp_send_json(array( |
| 130 | 'status' => 403, |
| 131 | /* translators: %s: export ID */ |
| 132 | 'message' => sprintf(esc_html__('Export #%s already processing. Request skipped.', 'wp-all-export'), $export->id) |
| 133 | )); |
| 134 | } |
| 135 | } |
| 136 | } |