admin
8 years ago
api
8 years ago
forms
8 years ago
integrations
8 years ago
mailchimp
8 years ago
views
8 years ago
class-array-bag.php
8 years ago
class-container.php
10 years ago
class-debug-log-reader.php
9 years ago
class-debug-log.php
9 years ago
class-dynamic-content-tags.php
9 years ago
class-field-formatter.php
9 years ago
class-field-guesser.php
8 years ago
class-list-data-mapper.php
8 years ago
class-mailchimp.php
8 years ago
class-plugin.php
10 years ago
class-queue-job.php
10 years ago
class-queue.php
9 years ago
class-request.php
10 years ago
class-tools.php
9 years ago
default-actions.php
9 years ago
default-filters.php
9 years ago
deprecated-functions.php
8 years ago
functions.php
8 years ago
class-list-data-mapper.php
161 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 | * @param array $data |
| 29 | * @param array $list_ids |
| 30 | */ |
| 31 | public function __construct( array $data, array $list_ids ) { |
| 32 | $this->data = array_change_key_case( $data, CASE_UPPER ); |
| 33 | $this->list_ids = $list_ids; |
| 34 | $this->formatter = new MC4WP_Field_Formatter(); |
| 35 | |
| 36 | if( ! isset( $this->data['EMAIL'] ) ) { |
| 37 | throw new InvalidArgumentException( 'Data needs at least an EMAIL key.' ); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * @return MC4WP_MailChimp_Subscriber[] |
| 43 | */ |
| 44 | public function map() { |
| 45 | $mailchimp = new MC4WP_MailChimp(); |
| 46 | $map = array(); |
| 47 | |
| 48 | foreach( $this->list_ids as $list_id ) { |
| 49 | $list = $mailchimp->get_list( $list_id, true ); |
| 50 | |
| 51 | if( $list instanceof MC4WP_MailChimp_List ) { |
| 52 | $map[ $list_id ] = $this->map_list( $list ); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | return $map; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * @param MC4WP_MailChimp_List $list |
| 61 | * |
| 62 | * @return MC4WP_MailChimp_Subscriber |
| 63 | */ |
| 64 | protected function map_list( MC4WP_MailChimp_List $list ) { |
| 65 | |
| 66 | $subscriber = new MC4WP_MailChimp_Subscriber(); |
| 67 | $subscriber->email_address = $this->data['EMAIL']; |
| 68 | |
| 69 | // find merge fields |
| 70 | foreach( $list->merge_fields as $merge_field ) { |
| 71 | |
| 72 | // skip EMAIL field as that is handled separately (see above) |
| 73 | if( $merge_field->tag === 'EMAIL' ) { |
| 74 | continue; |
| 75 | } |
| 76 | |
| 77 | if( ! isset( $this->data[ $merge_field->tag ] ) ) { |
| 78 | continue; |
| 79 | } |
| 80 | |
| 81 | // format field value |
| 82 | $value = $this->data[ $merge_field->tag ]; |
| 83 | $value = $this->format_merge_field_value( $value, $merge_field->field_type ); |
| 84 | |
| 85 | // add to map |
| 86 | $subscriber->merge_fields[ $merge_field->tag ] = $value; |
| 87 | } |
| 88 | |
| 89 | // find interest categories |
| 90 | if( ! empty( $this->data['INTERESTS'] ) ) { |
| 91 | foreach( $list->interest_categories as $interest_category ) { |
| 92 | foreach( $interest_category->interests as $interest_id => $interest_name ) { |
| 93 | |
| 94 | // straight lookup by ID as key with value copy. |
| 95 | if( isset( $this->data['INTERESTS'][ $interest_id ] ) ) { |
| 96 | $subscriber->interests[ $interest_id ] = $this->formatter->boolean( $this->data['INTERESTS'][ $interest_id ] ); |
| 97 | } |
| 98 | |
| 99 | // straight lookup by ID as top-level value |
| 100 | if( in_array( $interest_id, $this->data['INTERESTS'], false ) ) { |
| 101 | $subscriber->interests[ $interest_id ] = true; |
| 102 | } |
| 103 | |
| 104 | // look in array with category ID as key. |
| 105 | if( isset( $this->data['INTERESTS'][ $interest_category->id ] ) ) { |
| 106 | $value = $this->data['INTERESTS'][ $interest_category->id ]; |
| 107 | $values = is_array( $value ) ? $value : array_map( 'trim', explode( '|', $value ) ); |
| 108 | |
| 109 | // find by category ID + interest ID |
| 110 | if( in_array( $interest_id, $values, false ) ) { |
| 111 | $subscriber->interests[ $interest_id ] = true; |
| 112 | } |
| 113 | |
| 114 | // find by category ID + interest name |
| 115 | if( in_array( $interest_name, $values ) ) { |
| 116 | $subscriber->interests[ $interest_id ] = true; |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | // find language |
| 124 | /* @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 */ |
| 125 | if( ! empty( $this->data['MC_LANGUAGE'] ) ) { |
| 126 | $subscriber->language = $this->formatter->language( $this->data['MC_LANGUAGE'] ); |
| 127 | } |
| 128 | |
| 129 | return $subscriber; |
| 130 | } |
| 131 | |
| 132 | |
| 133 | /** |
| 134 | * @param mixed $field_value |
| 135 | * @param string $field_type |
| 136 | * |
| 137 | * @return mixed |
| 138 | */ |
| 139 | private function format_merge_field_value( $field_value, $field_type ) { |
| 140 | $field_type = strtolower( $field_type ); |
| 141 | |
| 142 | if( method_exists( $this->formatter, $field_type ) ) { |
| 143 | $field_value = call_user_func( array( $this->formatter, $field_type ), $field_value ); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Filters the value of a field after it is formatted. |
| 148 | * |
| 149 | * Use this to format a field value according to the field type (in MailChimp). |
| 150 | * |
| 151 | * @since 3.0 |
| 152 | * @param string $field_value The value |
| 153 | * @param string $field_type The type of the field (in MailChimp) |
| 154 | */ |
| 155 | $field_value = apply_filters( 'mc4wp_format_field_value', $field_value, $field_type ); |
| 156 | |
| 157 | return $field_value; |
| 158 | } |
| 159 | |
| 160 | } |
| 161 |