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 |