Export
2 months ago
Import
2 weeks ago
PersonalDataExporters
2 months ago
ImportExportFactory.php
2 months ago
ImportExportRepository.php
2 months ago
index.php
3 years ago
ImportExportFactory.php
196 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Subscribers\ImportExport; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\CustomFields\CustomFieldsRepository; |
| 9 | use MailPoet\DI\ContainerWrapper; |
| 10 | use MailPoet\Entities\SegmentEntity; |
| 11 | use MailPoet\Entities\TagEntity; |
| 12 | use MailPoet\Segments\SegmentsSimpleListRepository; |
| 13 | use MailPoet\Tags\TagRepository; |
| 14 | use MailPoet\Util\Helpers; |
| 15 | |
| 16 | class ImportExportFactory { |
| 17 | const IMPORT_ACTION = 'import'; |
| 18 | const EXPORT_ACTION = 'export'; |
| 19 | |
| 20 | /** @var string|null */ |
| 21 | public $action; |
| 22 | |
| 23 | /** @var SegmentsSimpleListRepository */ |
| 24 | private $segmentsListRepository; |
| 25 | |
| 26 | /** @var CustomFieldsRepository */ |
| 27 | private $customFieldsRepository; |
| 28 | |
| 29 | /** @var TagRepository */ |
| 30 | private $tagRepository; |
| 31 | |
| 32 | public function __construct( |
| 33 | $action = null |
| 34 | ) { |
| 35 | $this->action = $action; |
| 36 | $this->segmentsListRepository = ContainerWrapper::getInstance()->get(SegmentsSimpleListRepository::class); |
| 37 | $this->customFieldsRepository = ContainerWrapper::getInstance()->get(CustomFieldsRepository::class); |
| 38 | $this->tagRepository = ContainerWrapper::getInstance()->get(TagRepository::class); |
| 39 | } |
| 40 | |
| 41 | public function getSegments() { |
| 42 | if ($this->action === self::IMPORT_ACTION) { |
| 43 | $segments = $this->segmentsListRepository->getListWithSubscribedSubscribersCounts([SegmentEntity::TYPE_DEFAULT]); |
| 44 | } else { |
| 45 | $segments = $this->segmentsListRepository->getListWithAssociatedSubscribersCounts(); |
| 46 | $segments = $this->segmentsListRepository->addVirtualSubscribersWithoutListSegment($segments); |
| 47 | $segments = array_values(array_filter($segments, function($segment) { |
| 48 | return $segment['subscribers'] > 0; |
| 49 | })); |
| 50 | } |
| 51 | |
| 52 | return array_map(function($segment) { |
| 53 | return [ |
| 54 | 'id' => $segment['id'], |
| 55 | 'name' => esc_attr($segment['name']), |
| 56 | 'count' => $segment['subscribers'], |
| 57 | ]; |
| 58 | }, $segments); |
| 59 | } |
| 60 | |
| 61 | public function getSubscriberFields() { |
| 62 | $fields = [ |
| 63 | 'email' => __('Email', 'mailpoet'), |
| 64 | 'first_name' => __('First name', 'mailpoet'), |
| 65 | 'last_name' => __('Last name', 'mailpoet'), |
| 66 | 'subscribed_ip' => __('Subscription IP', 'mailpoet'), |
| 67 | 'created_at' => __('Subscription time', 'mailpoet'), |
| 68 | 'confirmed_at' => __('Confirmation time', 'mailpoet'), |
| 69 | 'confirmed_ip' => __('Confirmation IP', 'mailpoet'), |
| 70 | ]; |
| 71 | if ($this->action === 'export') { |
| 72 | $fields = array_merge( |
| 73 | $fields, |
| 74 | [ |
| 75 | 'last_subscribed_at' => __('Last subscribed on', 'mailpoet'), |
| 76 | 'list_status' => _x('List status', 'Subscription status', 'mailpoet'), |
| 77 | 'global_status' => _x('Global status', 'Subscription status', 'mailpoet'), |
| 78 | ] |
| 79 | ); |
| 80 | } |
| 81 | return $fields; |
| 82 | } |
| 83 | |
| 84 | public function formatSubscriberFields($subscriberFields) { |
| 85 | return array_map(function($fieldId, $fieldName) { |
| 86 | return [ |
| 87 | 'id' => $fieldId, |
| 88 | 'name' => $fieldName, |
| 89 | 'text' => $fieldName, // Required for select2 default functionality |
| 90 | 'type' => in_array($fieldId, ['confirmed_at', 'created_at', 'last_subscribed_at'], true) ? 'date' : null, |
| 91 | 'custom' => false, |
| 92 | ]; |
| 93 | }, array_keys($subscriberFields), $subscriberFields); |
| 94 | } |
| 95 | |
| 96 | public function getSubscriberCustomFields() { |
| 97 | return $this->customFieldsRepository->findAllAsArray(); |
| 98 | } |
| 99 | |
| 100 | public function formatSubscriberCustomFields($subscriberCustomFields) { |
| 101 | return array_map(function($field) { |
| 102 | return [ |
| 103 | 'id' => $field['id'], |
| 104 | 'name' => $field['name'], |
| 105 | 'text' => $field['name'], // Required for select2 default functionality |
| 106 | 'type' => $field['type'], |
| 107 | 'params' => unserialize($field['params']), |
| 108 | 'custom' => true, |
| 109 | ]; |
| 110 | }, $subscriberCustomFields); |
| 111 | } |
| 112 | |
| 113 | public function formatFieldsForSelect2( |
| 114 | $subscriberFields, |
| 115 | $subscriberCustomFields |
| 116 | ) { |
| 117 | $actions = ($this->action === 'import') ? |
| 118 | [ |
| 119 | [ |
| 120 | 'id' => 'ignore', |
| 121 | 'name' => __('Ignore field...', 'mailpoet'), |
| 122 | 'text' => __('Ignore field...', 'mailpoet'), // Required for select2 default functionality |
| 123 | ], |
| 124 | [ |
| 125 | 'id' => 'create', |
| 126 | 'name' => __('Create new field...', 'mailpoet'), |
| 127 | 'text' => __('Create new field...', 'mailpoet'), // Required for select2 default functionality |
| 128 | ], |
| 129 | ] : |
| 130 | [ |
| 131 | [ |
| 132 | 'id' => 'select', |
| 133 | 'name' => __('Select all...', 'mailpoet'), |
| 134 | 'text' => __('Select all...', 'mailpoet'), // Required for select2 default functionality |
| 135 | ], |
| 136 | [ |
| 137 | 'id' => 'deselect', |
| 138 | 'name' => __('Deselect all...', 'mailpoet'), |
| 139 | 'text' => __('Deselect all...', 'mailpoet'), // Required for select2 default functionality |
| 140 | ], |
| 141 | ]; |
| 142 | $select2Fields = [ |
| 143 | [ |
| 144 | 'name' => __('Actions', 'mailpoet'), |
| 145 | 'text' => __('Actions', 'mailpoet'), // Required for select2 default functionality |
| 146 | 'children' => $actions, |
| 147 | ], |
| 148 | [ |
| 149 | 'name' => __('System fields', 'mailpoet'), |
| 150 | 'text' => __('System fields', 'mailpoet'), // Required for select2 default functionality |
| 151 | 'children' => $this->formatSubscriberFields($subscriberFields), |
| 152 | ], |
| 153 | ]; |
| 154 | if ($subscriberCustomFields) { |
| 155 | array_push($select2Fields, [ |
| 156 | 'name' => __('User fields', 'mailpoet'), |
| 157 | 'text' => __('User fields', 'mailpoet'), // Required for select2 default functionality |
| 158 | 'children' => $this->formatSubscriberCustomFields( |
| 159 | $subscriberCustomFields |
| 160 | ), |
| 161 | ]); |
| 162 | } |
| 163 | return $select2Fields; |
| 164 | } |
| 165 | |
| 166 | public function bootstrap() { |
| 167 | $subscriberFields = $this->getSubscriberFields(); |
| 168 | $subscriberCustomFields = $this->getSubscriberCustomFields(); |
| 169 | $data['segments'] = json_encode($this->getSegments()); |
| 170 | $data['subscriberFieldsSelect2'] = json_encode( |
| 171 | $this->formatFieldsForSelect2( |
| 172 | $subscriberFields, |
| 173 | $subscriberCustomFields |
| 174 | ) |
| 175 | ); |
| 176 | if ($this->action === 'import') { |
| 177 | $data['subscriberFields'] = json_encode( |
| 178 | array_merge( |
| 179 | $this->formatSubscriberFields($subscriberFields), |
| 180 | $this->formatSubscriberCustomFields($subscriberCustomFields) |
| 181 | ) |
| 182 | ); |
| 183 | $data['maxPostSizeBytes'] = Helpers::getMaxPostSize('bytes'); |
| 184 | $data['maxPostSize'] = Helpers::getMaxPostSize(); |
| 185 | $data['tags'] = array_map(function (TagEntity $tag): array { |
| 186 | return [ |
| 187 | 'id' => $tag->getId(), |
| 188 | 'name' => $tag->getName(), |
| 189 | ]; |
| 190 | }, $this->tagRepository->findAll()); |
| 191 | } |
| 192 | $data['zipExtensionLoaded'] = extension_loaded('zip'); |
| 193 | return $data; |
| 194 | } |
| 195 | } |
| 196 |