PluginProbe ʕ •ᴥ•ʔ
WP All Export – Drag & Drop Export to Any Custom CSV, XML & Excel / 1.3.6
WP All Export – Drag & Drop Export to Any Custom CSV, XML & Excel v1.3.6
trunk 0.9.0 0.9.1 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.2.0 1.2.1 1.2.10 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.10 1.4.11 1.4.12 1.4.13 1.4.14 1.4.15 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.5.0
wp-all-export / src / App / UnsecuredController / SchedulingController.php
wp-all-export / src / App / UnsecuredController Last commit date
SchedulingController.php 4 years ago
SchedulingController.php
177 lines
1 <?php
2
3 namespace Wpae\App\UnsecuredController;
4
5
6 use Wpae\App\Service\Addons\AddonNotFoundException;
7 use Wpae\App\Service\Addons\AddonService;
8 use Wpae\Controller\BaseController;
9 use Wpae\Http\Request;
10 use Wpae\Scheduling\Export;
11 use Wpae\Http\JsonResponse;
12
13 class SchedulingController extends BaseController
14 {
15 /** Scheduling API Version */
16 const VERSION = 1;
17
18 /** @var Export */
19 private $scheduledExportService;
20
21 public function __construct($container)
22 {
23 parent::__construct($container);
24 $this->scheduledExportService = new Export();
25 }
26
27 public function triggerAction(Request $request)
28 {
29 if (!$this->isRequestValid()) {
30 return new JsonResponse(array('message' => 'Export hash is invalid'), 401);
31 }
32
33 $exportId = intval($request->get('export_id'));
34
35 $export = new \PMXE_Export_Record();
36 $export->getById($exportId);
37
38 $this->disableExportsThatDontHaveAddon($export);
39
40
41 if ($export->isEmpty()) {
42 return new JsonResponse(array('message' => 'Export not found'), 404);
43 }
44
45 if ((int)$export->executing) {
46 return new JsonResponse(array("message" => "Export #" . $export->id . " is currently in manually process. Request skipped."), 409);
47 }
48 if ($export->processing and !$export->triggered) {
49 return new JsonResponse(array("message" => "Export #" . $export->id . " currently in process. Request skipped."), 409);
50
51 }
52 if (!$export->processing and $export->triggered) {
53 return new JsonResponse(array("message" => "Export #" . $export->id . " already triggered. Request skipped."), 409);
54 }
55
56 if (!$export->processing and !$export->triggered) {
57 $this->scheduledExportService->trigger($export);
58
59 return new JsonResponse(array('message' => "#" . $export->id . " Cron job triggered."));
60 }
61
62 return new JsonResponse(array("message" => "Can't process"), 500);
63 }
64
65 public function processAction(Request $request)
66 {
67 if (!$this->isRequestValid()) {
68 return new JsonResponse(array('message' => 'Export hash is invalid'), 401);
69 }
70
71 $exportId = intval($request->get('export_id'));
72
73 $export = new \PMXE_Export_Record();
74 $export->getById($exportId);
75
76 $this->disableExportsThatDontHaveAddon($export);
77
78 if ($export->isEmpty()) {
79 return new JsonResponse(array('message' => 'Export not found'), 404);
80 }
81
82 $logger = function($m) {
83 echo "<p>" . wp_kses_post($m) . "</p>\\n";
84 };
85
86 if ($export->processing == 1 and (time() - strtotime($export->registered_on)) > 120) {
87 // it means processor crashed, so it will reset processing to false, and terminate. Then next run it will work normally.
88 $export->set(array(
89 'processing' => 0
90 ))->update();
91 }
92
93 // start execution imports that is in the cron process
94 if (!(int)$export->triggered) {
95 if (!empty($export->parent_id) or empty($queue_exports)) {
96 return new JsonResponse(array("message" => 'Export #' . $exportId . ' is not triggered. Request skipped.'), 400);
97 }
98 } elseif ((int)$export->executing) {
99 return new JsonResponse(array('message' => 'Export #' . $exportId . ' is currently in manually process. Request skipped.'), 409);
100 } elseif ((int)$export->triggered and !(int)$export->processing) {
101
102 try {
103 $export->set(array('canceled' => 0))->execute($logger, true);
104 } catch (AddonNotFoundException $e) {
105 die($e->getMessage());
106 }
107 if (!(int)$export->triggered and !(int)$export->processing) {
108 $this->scheduledExportService->process($export);
109 return new JsonResponse(array('Export #' . $exportId . ' complete'), 201);
110 } else {
111 return new JsonResponse(array('message' => 'Records Processed ' . (int)$export->exported . '.'));
112 }
113
114 } else {
115 return new JsonResponse(array('message' => 'Export #' . $exportId . ' already processing. Request skipped.'), 409);
116 }
117
118 return new JsonResponse(array("message" => "Can't process"), 500);
119 }
120
121 public function versionAction()
122 {
123 return new JsonResponse(array('version' => self::VERSION));
124 }
125
126 /**
127 * @return bool
128 */
129 private function isRequestValid()
130 {
131 $cron_job_key = \PMXE_Plugin::getInstance()->getOption('cron_job_key');
132 return
133 !empty($cron_job_key) and
134 !empty($_GET['export_id']) and
135 !empty($_GET['export_key']) and
136 $_GET['export_key'] == $cron_job_key;
137 }
138
139 /**
140 * @param $export
141 */
142 private function disableExportsThatDontHaveAddon($export)
143 {
144 $cpt = $export->options['cpt'];
145 if (!is_array($cpt)) {
146 $cpt = array($cpt);
147 }
148
149 $addons = new AddonService();
150
151 if (
152 ((in_array('users', $cpt) || in_array('shop_customer', $cpt)) && !$addons->isUserAddonActive())
153 ||
154 ($export->options['export_type'] == 'advanced' && $export->options['wp_query_selector'] == 'wp_user_query' && !$addons->isUserAddonActive())
155 ) {
156 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));
157 }
158
159 if (
160 (( (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())
161 ||
162 ($export->options['export_type'] == 'advanced' && in_array($export->options['exportquery']->query['post_type'], array('shop_coupon')) && !$addons->isWooCommerceAddonActive())
163 ||
164 ($export->options['export_type'] == 'advanced' && in_array($export->options['exportquery']->query['post_type'], array('shop_order')) && !$addons->isWooCommerceAddonActive() && !$addons->isWooCommerceOrderAddonActive())
165 ||
166 ($export->options['export_type'] == 'advanced' && in_array($export->options['exportquery']->query['post_type'], array(array('product', 'product_variation'), )) && !$addons->isWooCommerceAddonActive() && !$addons->isWooCommerceProductAddonActive())
167 ) {
168 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));
169 }
170
171 if(in_array('acf', $export->options['cc_type']) && !$addons->isAcfAddonActive()) {
172 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));
173 }
174
175 }
176
177 }