SchedulingController.php
179 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(esc_html($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 | // phpcs:disable WordPress.Security.NonceVerification.Recommended -- access verified via export_key matching cron_job_key option |
| 133 | return |
| 134 | !empty($cron_job_key) and |
| 135 | !empty($_GET['export_id']) and |
| 136 | !empty($_GET['export_key']) and |
| 137 | hash_equals( $cron_job_key, sanitize_text_field( wp_unslash( $_GET['export_key'] ) ) ); |
| 138 | // phpcs:enable WordPress.Security.NonceVerification.Recommended |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * @param $export |
| 143 | */ |
| 144 | private function disableExportsThatDontHaveAddon($export) |
| 145 | { |
| 146 | $cpt = $export->options['cpt']; |
| 147 | if (!is_array($cpt)) { |
| 148 | $cpt = array($cpt); |
| 149 | } |
| 150 | |
| 151 | $addons = new AddonService(); |
| 152 | |
| 153 | if ( |
| 154 | ((in_array('users', $cpt) || in_array('shop_customer', $cpt)) && !$addons->isUserAddonActive()) |
| 155 | || |
| 156 | ($export->options['export_type'] == 'advanced' && $export->options['wp_query_selector'] == 'wp_user_query' && !$addons->isUserAddonActive()) |
| 157 | ) { |
| 158 | die(wp_kses_post(\__('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>', 'wp-all-export'))); |
| 159 | } |
| 160 | |
| 161 | if ( |
| 162 | (( (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()) |
| 163 | || |
| 164 | ($export->options['export_type'] == 'advanced' && in_array($export->options['exportquery']->query['post_type'], array('shop_coupon')) && !$addons->isWooCommerceAddonActive()) |
| 165 | || |
| 166 | ($export->options['export_type'] == 'advanced' && in_array($export->options['exportquery']->query['post_type'], array('shop_order')) && !$addons->isWooCommerceAddonActive() && !$addons->isWooCommerceOrderAddonActive()) |
| 167 | || |
| 168 | ($export->options['export_type'] == 'advanced' && in_array($export->options['exportquery']->query['post_type'], array(array('product', 'product_variation'), )) && !$addons->isWooCommerceAddonActive() && !$addons->isWooCommerceProductAddonActive()) |
| 169 | ) { |
| 170 | die(wp_kses_post(\__('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>', 'wp-all-export'))); |
| 171 | } |
| 172 | |
| 173 | if(in_array('acf', $export->options['cc_type']) && !$addons->isAcfAddonActive()) { |
| 174 | die(wp_kses_post(\__('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>', 'wp-all-export'))); |
| 175 | } |
| 176 | |
| 177 | } |
| 178 | |
| 179 | } |