SchedulingController.php
7.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
namespace Wpae\App\UnsecuredController;
use Wpae\App\Service\Addons\AddonNotFoundException;
use Wpae\App\Service\Addons\AddonService;
use Wpae\Controller\BaseController;
use Wpae\Http\Request;
use Wpae\Scheduling\Export;
use Wpae\Http\JsonResponse;
class SchedulingController extends BaseController
{
/** Scheduling API Version */
const VERSION = 1;
/** @var Export */
private $scheduledExportService;
public function __construct($container)
{
parent::__construct($container);
$this->scheduledExportService = new Export();
}
public function triggerAction(Request $request)
{
if (!$this->isRequestValid()) {
return new JsonResponse(array('message' => 'Export hash is invalid'), 401);
}
$exportId = intval($request->get('export_id'));
$export = new \PMXE_Export_Record();
$export->getById($exportId);
$this->disableExportsThatDontHaveAddon($export);
if ($export->isEmpty()) {
return new JsonResponse(array('message' => 'Export not found'), 404);
}
if ((int)$export->executing) {
return new JsonResponse(array("message" => "Export #" . $export->id . " is currently in manually process. Request skipped."), 409);
}
if ($export->processing and !$export->triggered) {
return new JsonResponse(array("message" => "Export #" . $export->id . " currently in process. Request skipped."), 409);
}
if (!$export->processing and $export->triggered) {
return new JsonResponse(array("message" => "Export #" . $export->id . " already triggered. Request skipped."), 409);
}
if (!$export->processing and !$export->triggered) {
$this->scheduledExportService->trigger($export);
return new JsonResponse(array('message' => "#" . $export->id . " Cron job triggered."));
}
return new JsonResponse(array("message" => "Can't process"), 500);
}
public function processAction(Request $request)
{
if (!$this->isRequestValid()) {
return new JsonResponse(array('message' => 'Export hash is invalid'), 401);
}
$exportId = intval($request->get('export_id'));
$export = new \PMXE_Export_Record();
$export->getById($exportId);
$this->disableExportsThatDontHaveAddon($export);
if ($export->isEmpty()) {
return new JsonResponse(array('message' => 'Export not found'), 404);
}
$logger = function($m) {
echo "<p>" . wp_kses_post($m) . "</p>\\n";
};
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.
$export->set(array(
'processing' => 0
))->update();
}
// start execution imports that is in the cron process
if (!(int)$export->triggered) {
if (!empty($export->parent_id) or empty($queue_exports)) {
return new JsonResponse(array("message" => 'Export #' . $exportId . ' is not triggered. Request skipped.'), 400);
}
} elseif ((int)$export->executing) {
return new JsonResponse(array('message' => 'Export #' . $exportId . ' is currently in manually process. Request skipped.'), 409);
} elseif ((int)$export->triggered and !(int)$export->processing) {
try {
$export->set(array('canceled' => 0))->execute($logger, true);
} catch (AddonNotFoundException $e) {
die($e->getMessage());
}
if (!(int)$export->triggered and !(int)$export->processing) {
$this->scheduledExportService->process($export);
return new JsonResponse(array('Export #' . $exportId . ' complete'), 201);
} else {
return new JsonResponse(array('message' => 'Records Processed ' . (int)$export->exported . '.'));
}
} else {
return new JsonResponse(array('message' => 'Export #' . $exportId . ' already processing. Request skipped.'), 409);
}
return new JsonResponse(array("message" => "Can't process"), 500);
}
public function versionAction()
{
return new JsonResponse(array('version' => self::VERSION));
}
/**
* @return bool
*/
private function isRequestValid()
{
$cron_job_key = \PMXE_Plugin::getInstance()->getOption('cron_job_key');
return
!empty($cron_job_key) and
!empty($_GET['export_id']) and
!empty($_GET['export_key']) and
$_GET['export_key'] == $cron_job_key;
}
/**
* @param $export
*/
private function disableExportsThatDontHaveAddon($export)
{
$cpt = $export->options['cpt'];
if (!is_array($cpt)) {
$cpt = array($cpt);
}
$addons = new AddonService();
if (
((in_array('users', $cpt) || in_array('shop_customer', $cpt)) && !$addons->isUserAddonActive())
||
($export->options['export_type'] == 'advanced' && $export->options['wp_query_selector'] == 'wp_user_query' && !$addons->isUserAddonActive())
) {
die(\__('The User Export Add-On Pro is required to run this export. You can download the add-on here: <a href="http://www.wpallimport.com/portal/" target="_blank">http://www.wpallimport.com/portal/</a>', \PMXE_Plugin::LANGUAGE_DOMAIN));
}
if (
(( (in_array('product', $cpt) && \class_exists('WooCommerce') && !$addons->isWooCommerceProductAddonActive()) || (in_array('shop_order', $cpt) && !$addons->isWooCommerceOrderAddonActive()) || in_array('shop_coupon', $cpt) || in_array('shop_review', $cpt) ) && !$addons->isWooCommerceAddonActive())
||
($export->options['export_type'] == 'advanced' && in_array($export->options['exportquery']->query['post_type'], array('shop_coupon')) && !$addons->isWooCommerceAddonActive())
||
($export->options['export_type'] == 'advanced' && in_array($export->options['exportquery']->query['post_type'], array('shop_order')) && !$addons->isWooCommerceAddonActive() && !$addons->isWooCommerceOrderAddonActive())
||
($export->options['export_type'] == 'advanced' && in_array($export->options['exportquery']->query['post_type'], array(array('product', 'product_variation'), )) && !$addons->isWooCommerceAddonActive() && !$addons->isWooCommerceProductAddonActive())
) {
die(\__('The WooCommerce Export Add-On Pro is required to run this export. You can download the add-on here: <a href="http://www.wpallimport.com/portal/" target="_blank">http://www.wpallimport.com/portal/</a>', \PMXE_Plugin::LANGUAGE_DOMAIN));
}
if(in_array('acf', $export->options['cc_type']) && !$addons->isAcfAddonActive()) {
die(\__('The ACF Export Add-On Pro is required to run this export. You can download the add-on here: <a href="http://www.wpallimport.com/portal/" target="_blank">http://www.wpallimport.com/portal/</a>', \PMXE_Plugin::LANGUAGE_DOMAIN));
}
}
}