Analytics.php
3 years ago
AutomatedLatestContent.php
2 months ago
AutomaticEmails.php
3 years ago
Captcha.php
1 year ago
Coupons.php
2 years ago
CustomFields.php
2 months ago
DynamicProducts.php
1 year ago
DynamicSegments.php
2 months ago
FeatureFlags.php
3 years ago
Forms.php
2 months ago
Help.php
1 year ago
ImportExport.php
2 months ago
Mailer.php
1 year ago
NewsletterLinks.php
2 months ago
NewsletterTemplates.php
2 months ago
Newsletters.php
2 months ago
Premium.php
10 months ago
RedirectResponse.php
1 year ago
Segments.php
2 months ago
SendingQueue.php
2 months ago
Services.php
6 months ago
Settings.php
2 months ago
Setup.php
1 year ago
StatisticsExport.php
3 months ago
SubscriberStats.php
1 month ago
Subscribers.php
2 months ago
Tags.php
3 years ago
UserFlags.php
2 years ago
WoocommerceProductVariations.php
2 months ago
WoocommerceSettings.php
3 years ago
index.php
3 years ago
ImportExport.php
199 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\API\JSON\v1; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\API\JSON\Endpoint as APIEndpoint; |
| 9 | use MailPoet\API\JSON\Error as APIError; |
| 10 | use MailPoet\API\JSON\ResponseBuilders\SegmentsResponseBuilder; |
| 11 | use MailPoet\Config\AccessControl; |
| 12 | use MailPoet\ConflictException; |
| 13 | use MailPoet\Cron\CronWorkerScheduler; |
| 14 | use MailPoet\Cron\Workers\WooCommerceSync; |
| 15 | use MailPoet\CustomFields\CustomFieldsRepository; |
| 16 | use MailPoet\Doctrine\Validator\ValidationException; |
| 17 | use MailPoet\Newsletter\Options\NewsletterOptionsRepository; |
| 18 | use MailPoet\Segments\SegmentSaveController; |
| 19 | use MailPoet\Segments\SegmentsRepository; |
| 20 | use MailPoet\Segments\WP; |
| 21 | use MailPoet\Services\Validator; |
| 22 | use MailPoet\Subscribers\ImportExport\Export\Export; |
| 23 | use MailPoet\Subscribers\ImportExport\Import\Import; |
| 24 | use MailPoet\Subscribers\ImportExport\Import\MailChimp; |
| 25 | use MailPoet\Subscribers\ImportExport\ImportExportRepository; |
| 26 | use MailPoet\Subscribers\SubscribersRepository; |
| 27 | use MailPoet\Tags\TagRepository; |
| 28 | |
| 29 | class ImportExport extends APIEndpoint { |
| 30 | |
| 31 | /** @var WP */ |
| 32 | private $wpSegment; |
| 33 | |
| 34 | /** @var CustomFieldsRepository */ |
| 35 | private $customFieldsRepository; |
| 36 | |
| 37 | /** @var ImportExportRepository */ |
| 38 | private $importExportRepository; |
| 39 | |
| 40 | /** @var NewsletterOptionsRepository */ |
| 41 | private $newsletterOptionsRepository; |
| 42 | |
| 43 | /** @var SegmentsRepository */ |
| 44 | private $segmentsRepository; |
| 45 | |
| 46 | /** @var SubscribersRepository */ |
| 47 | private $subscriberRepository; |
| 48 | |
| 49 | /** @var SegmentSaveController */ |
| 50 | private $segmentSavecontroller; |
| 51 | |
| 52 | /** @var SegmentsResponseBuilder */ |
| 53 | private $segmentsResponseBuilder; |
| 54 | |
| 55 | /** @var TagRepository */ |
| 56 | private $tagRepository; |
| 57 | |
| 58 | /** @var Validator */ |
| 59 | private $validator; |
| 60 | |
| 61 | /** @var CronWorkerScheduler */ |
| 62 | private $cronWorkerScheduler; |
| 63 | |
| 64 | public $permissions = [ |
| 65 | 'global' => AccessControl::PERMISSION_MANAGE_SUBSCRIBERS, |
| 66 | ]; |
| 67 | |
| 68 | public function __construct( |
| 69 | WP $wpSegment, |
| 70 | CustomFieldsRepository $customFieldsRepository, |
| 71 | ImportExportRepository $importExportRepository, |
| 72 | NewsletterOptionsRepository $newsletterOptionsRepository, |
| 73 | SegmentsRepository $segmentsRepository, |
| 74 | SegmentSaveController $segmentSavecontroller, |
| 75 | SegmentsResponseBuilder $segmentsResponseBuilder, |
| 76 | CronWorkerScheduler $cronWorkerScheduler, |
| 77 | SubscribersRepository $subscribersRepository, |
| 78 | TagRepository $tagRepository, |
| 79 | Validator $validator |
| 80 | ) { |
| 81 | $this->wpSegment = $wpSegment; |
| 82 | $this->customFieldsRepository = $customFieldsRepository; |
| 83 | $this->importExportRepository = $importExportRepository; |
| 84 | $this->newsletterOptionsRepository = $newsletterOptionsRepository; |
| 85 | $this->segmentsRepository = $segmentsRepository; |
| 86 | $this->subscriberRepository = $subscribersRepository; |
| 87 | $this->segmentSavecontroller = $segmentSavecontroller; |
| 88 | $this->cronWorkerScheduler = $cronWorkerScheduler; |
| 89 | $this->segmentsResponseBuilder = $segmentsResponseBuilder; |
| 90 | $this->tagRepository = $tagRepository; |
| 91 | $this->validator = $validator; |
| 92 | } |
| 93 | |
| 94 | public function getMailChimpLists($data) { |
| 95 | try { |
| 96 | $mailChimp = new MailChimp($data['api_key']); |
| 97 | $lists = $mailChimp->getLists(); |
| 98 | return $this->successResponse($lists); |
| 99 | } catch (\Exception $e) { |
| 100 | return $this->errorResponse([ |
| 101 | $e->getCode() => $e->getMessage(), |
| 102 | ]); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | public function getMailChimpSubscribers($data) { |
| 107 | try { |
| 108 | $mailChimp = new MailChimp($data['api_key']); |
| 109 | $subscribers = $mailChimp->getSubscribers($data['lists']); |
| 110 | return $this->successResponse($subscribers); |
| 111 | } catch (\Exception $e) { |
| 112 | return $this->errorResponse([ |
| 113 | $e->getCode() => $e->getMessage(), |
| 114 | ]); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | public function addSegment($data) { |
| 119 | try { |
| 120 | $data['name'] = isset($data['name']) ? sanitize_text_field($data['name']) : ''; |
| 121 | $data['description'] = isset($data['description']) ? sanitize_textarea_field($data['description']) : ''; |
| 122 | $segment = $this->segmentSavecontroller->save($data); |
| 123 | $response = $this->segmentsResponseBuilder->build($segment); |
| 124 | return $this->successResponse($response); |
| 125 | } catch (ValidationException $exception) { |
| 126 | return $this->badRequest([ |
| 127 | APIError::BAD_REQUEST => __('Please specify a name.', 'mailpoet'), |
| 128 | ]); |
| 129 | } catch (ConflictException $exception) { |
| 130 | return $this->badRequest([ |
| 131 | APIError::BAD_REQUEST => __('Another record already exists. Please specify a different "name".', 'mailpoet'), |
| 132 | ]); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | public function processImport($data) { |
| 137 | try { |
| 138 | if (!is_string($data)) { |
| 139 | return $this->badRequest([APIError::BAD_REQUEST => __('Invalid import payload.', 'mailpoet')]); |
| 140 | } |
| 141 | $decoded = json_decode($data, true); |
| 142 | if (!is_array($decoded)) { |
| 143 | return $this->badRequest([APIError::BAD_REQUEST => __('Invalid import payload.', 'mailpoet')]); |
| 144 | } |
| 145 | $import = new Import( |
| 146 | $this->wpSegment, |
| 147 | $this->customFieldsRepository, |
| 148 | $this->importExportRepository, |
| 149 | $this->newsletterOptionsRepository, |
| 150 | $this->subscriberRepository, |
| 151 | $this->tagRepository, |
| 152 | $this->validator, |
| 153 | $decoded |
| 154 | ); |
| 155 | $process = $import->process(); |
| 156 | return $this->successResponse($process); |
| 157 | } catch (\Exception $e) { |
| 158 | return $this->errorResponse([ |
| 159 | $e->getCode() => $e->getMessage(), |
| 160 | ]); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | public function processExport($data) { |
| 165 | try { |
| 166 | if (!is_string($data)) { |
| 167 | return $this->badRequest([APIError::BAD_REQUEST => __('Invalid export payload.', 'mailpoet')]); |
| 168 | } |
| 169 | $decoded = json_decode($data, true); |
| 170 | if (!is_array($decoded)) { |
| 171 | return $this->badRequest([APIError::BAD_REQUEST => __('Invalid export payload.', 'mailpoet')]); |
| 172 | } |
| 173 | $export = new Export( |
| 174 | $this->customFieldsRepository, |
| 175 | $this->importExportRepository, |
| 176 | $this->segmentsRepository, |
| 177 | $decoded |
| 178 | ); |
| 179 | $process = $export->process(); |
| 180 | return $this->successResponse($process); |
| 181 | } catch (\Exception $e) { |
| 182 | return $this->errorResponse([ |
| 183 | $e->getCode() => $e->getMessage(), |
| 184 | ]); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | public function setupWooCommerceInitialImport() { |
| 189 | try { |
| 190 | $this->cronWorkerScheduler->scheduleImmediatelyIfNotRunning(WooCommerceSync::TASK_TYPE); |
| 191 | return $this->successResponse(); |
| 192 | } catch (\Exception $e) { |
| 193 | return $this->errorResponse([ |
| 194 | $e->getCode() => $e->getMessage(), |
| 195 | ]); |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 |