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 4.8.6 4.8.7 4.8.8 4.8.9 4.9.0 4.9.1 4.9.10 4.9.11 4.9.12 4.9.13 4.9.14 4.9.15 4.9.16 4.9.17 4.9.18 4.9.19 4.9.2 4.9.20 4.9.21 4.9.3 4.9.4 4.9.5 4.9.6 4.9.7 4.9.8 4.9.9 trunk 1.1.5 1.2.1 1.2.3 1.2.4 1.2.5 1.3 1.3.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8
mailchimp-for-wp / includes / class-list-data-mapper.php
mailchimp-for-wp / includes Last commit date
admin 1 year ago api 1 year ago forms 1 year ago integrations 1 year ago views 1 year ago class-container.php 1 year ago class-debug-log-reader.php 1 year ago class-debug-log.php 1 year ago class-dynamic-content-tags.php 1 year ago class-field-formatter.php 1 year ago class-field-guesser.php 1 year ago class-list-data-mapper.php 1 year ago class-mailchimp-subscriber.php 1 year ago class-mailchimp.php 1 year ago class-plugin.php 1 year ago class-queue-job.php 1 year ago class-queue.php 1 year ago class-tools.php 1 year ago default-actions.php 1 year ago default-filters.php 1 year ago deprecated-functions.php 3 years ago functions.php 1 year ago
class-list-data-mapper.php
181 lines
1 <?php
2
3 /**
4 * Class MC4WP_Field_Map
5 *
6 * @access private
7 * @since 4.0
8 * @ignore
9 */
10 class MC4WP_List_Data_Mapper
11 {
12 /**
13 * @var array
14 */
15 private $data = array();
16
17 /**
18 * @var array
19 */
20 private $list_ids = array();
21
22 /**
23 * @var MC4WP_Field_Formatter
24 */
25 private $formatter;
26
27 /**
28 * @var MC4WP_MailChimp
29 */
30 private $mailchimp;
31
32 /**
33 * @param array $data
34 * @param array $list_ids
35 */
36 public function __construct(array $data, array $list_ids)
37 {
38 $this->data = array_change_key_case($data, CASE_UPPER);
39 if (! isset($this->data['EMAIL'])) {
40 throw new InvalidArgumentException('Data needs at least an EMAIL key.');
41 }
42
43 $this->list_ids = $list_ids;
44 $this->formatter = new MC4WP_Field_Formatter();
45 $this->mailchimp = new MC4WP_MailChimp();
46 }
47
48 /**
49 * @return MC4WP_MailChimp_Subscriber[]
50 */
51 public function map()
52 {
53 $map = array();
54
55 foreach ($this->list_ids as $list_id) {
56 $map[ "$list_id" ] = $this->map_list($list_id);
57 }
58
59 return $map;
60 }
61
62 /**
63 * @param string $list_id
64 * @return MC4WP_MailChimp_Subscriber
65 * @throws Exception
66 */
67 protected function map_list($list_id)
68 {
69 $subscriber = new MC4WP_MailChimp_Subscriber();
70 $subscriber->email_address = $this->data['EMAIL'];
71
72 // find merge fields
73 $merge_fields = $this->mailchimp->get_list_merge_fields($list_id);
74 foreach ($merge_fields as $merge_field) {
75 // skip EMAIL field as that is handled separately (see above)
76 if ($merge_field->tag === 'EMAIL') {
77 continue;
78 }
79
80 // use empty() here to skip empty field values
81 if (empty($this->data[ $merge_field->tag ])) {
82 continue;
83 }
84
85 // format field value
86 $value = $this->data[ $merge_field->tag ];
87 $value = $this->format_merge_field_value($merge_field, $value);
88
89 // add to map
90 $subscriber->merge_fields[ $merge_field->tag ] = $value;
91 }
92
93 // find interest categories
94 if (! empty($this->data['INTERESTS'])) {
95 $interest_categories = $this->mailchimp->get_list_interest_categories($list_id);
96 foreach ($interest_categories as $interest_category) {
97 foreach ($interest_category->interests as $interest_id => $interest_name) {
98 // straight lookup by ID as key with value copy.
99 if (isset($this->data['INTERESTS'][ $interest_id ])) {
100 $subscriber->interests[ $interest_id ] = $this->formatter->boolean($this->data['INTERESTS'][ $interest_id ]);
101 }
102
103 // straight lookup by ID as top-level value
104 if (in_array($interest_id, $this->data['INTERESTS'], false)) {
105 $subscriber->interests[ $interest_id ] = true;
106 }
107
108 // look in array with category ID as key.
109 if (isset($this->data['INTERESTS'][ $interest_category->id ])) {
110 $value = $this->data['INTERESTS'][ $interest_category->id ];
111 $values = is_array($value) ? $value : array_map('trim', explode('|', $value));
112
113 // find by category ID + interest ID
114 if (in_array($interest_id, $values, false)) {
115 $subscriber->interests[ $interest_id ] = true;
116 }
117
118 // find by category ID + interest name
119 if (in_array($interest_name, $values, true)) {
120 $subscriber->interests[ $interest_id ] = true;
121 }
122 }
123 }
124 }
125 }
126
127 // add GDPR marketing permissions
128 if (! empty($this->data['MARKETING_PERMISSIONS'])) {
129 $values = $this->data['MARKETING_PERMISSIONS'];
130 $values = is_array($values) ? $values : explode(',', $values);
131 $values = array_map('trim', $values);
132 $marketing_permissions = $this->mailchimp->get_list_marketing_permissions($list_id);
133 foreach ($marketing_permissions as $mp) {
134 if (in_array($mp->marketing_permission_id, $values, true) || in_array($mp->text, $values, true)) {
135 $subscriber->marketing_permissions[] = (object) array(
136 'marketing_permission_id' => $mp->marketing_permission_id,
137 'enabled' => true,
138 );
139 }
140 }
141 }
142
143 // find language
144 /* @see http://kb.mailchimp.com/lists/managing-subscribers/view-and-edit-subscriber-languages?utm_source=mc-api&utm_medium=docs&utm_campaign=apidocs&_ga=1.211519638.2083589671.1469697070 */
145 if (! empty($this->data['MC_LANGUAGE'])) {
146 $subscriber->language = $this->formatter->language($this->data['MC_LANGUAGE']);
147 }
148
149 return $subscriber;
150 }
151
152
153 /**
154 * @param object $merge_field
155 * @param string $value
156 *
157 * @return mixed
158 */
159 private function format_merge_field_value($merge_field, $value)
160 {
161 $field_type = strtolower($merge_field->type);
162
163 if (method_exists($this->formatter, $field_type)) {
164 $value = call_user_func(array( $this->formatter, $field_type ), $value, $merge_field->options);
165 }
166
167 /**
168 * Filters the value of a field after it is formatted.
169 *
170 * Use this to format a field value according to the field type (in Mailchimp).
171 *
172 * @since 3.0
173 * @param string $value The value
174 * @param string $field_type The type of the field (in Mailchimp)
175 */
176 $value = apply_filters('mc4wp_format_field_value', $value, $field_type);
177
178 return $value;
179 }
180 }
181