/** * @param $item * * @return string */ public function column_list($item) { $list_names = array(); $list_ids = array_map('trim', explode(',', $item->list_ids)); foreach ($list_ids as $list_id) { $list = $this->mailchimp->get_list($list_id); $list_names[] = sprintf('<a href="%s" target="_blank">' . $list->name . '</a>', $list->get_web_url()); } return join(', ', $list_names); }
/** * @return array|bool */ public function map_lists_fields() { $map = array(); // loop through selected lists foreach ($this->lists as $list_id) { $list = $this->mailchimp->get_list($list_id, false, true); // skip this list if it's unexisting if (!is_object($list) || !isset($list->merge_vars)) { continue; } // generate map for this given list $list_map = $this->map_data_to_list($list); if ($list_map === false) { return false; } $map[$list_id] = $list_map; } return $map; }
/** * @param $list_id * * @return array * @throws Exception */ public function map_list($list_id) { $list = $this->mailchimp->get_list($list_id, true); // skip this list if it's unexisting if (!is_object($list) || !isset($list->merge_vars)) { return array(); } $map = array(); // loop through list fields foreach ($list->merge_vars as $field) { $map[$field->tag] = $this->map_list_field($field); } // loop through list interest groupings if (!empty($list->groupings)) { $map['GROUPINGS'] = array_map(array($this, 'map_list_grouping'), $list->groupings); $map['GROUPINGS'] = array_filter($map['GROUPINGS']); } $map = array_filter($map); return $map; }
/** * @param array $list_ids * @return MC4WP_MailChimp_List[] */ protected function fetch_lists(array $list_ids) { $mailchimp = new MC4WP_MailChimp(); $lists = array(); foreach ($list_ids as $id) { $list = $mailchimp->get_list($id, true); if ($list instanceof MC4WP_MailChimp_List) { $lists[$id] = $list; } } return $lists; }
/** * @return MC4WP_MailChimp_Subscriber[] */ public function map() { $mailchimp = new MC4WP_MailChimp(); $map = array(); foreach ($this->list_ids as $list_id) { $list = $mailchimp->get_list($list_id); if ($list instanceof MC4WP_MailChimp_List) { $map[$list_id] = $this->map_list($list); } } return $map; }
/** * @param $list_id * * @return array * @throws Exception */ protected function map_list_fields($list_id) { $list = $this->mailchimp->get_list($list_id, true); // skip this list if it's unexisting if (!$list instanceof MC4WP_MailChimp_List) { return array(); } $map = array(); // loop through list fields foreach ($list->merge_vars as $field) { $map[$field->tag] = $this->map_list_field($field); } // loop through list interest groupings if (!empty($list->groupings)) { $map['GROUPINGS'] = array_map(array($this, 'map_list_grouping'), $list->groupings); $map['GROUPINGS'] = array_filter($map['GROUPINGS']); } // add global fields (fields belong to ALL lists automatically) $map = array_merge($map, $this->global_fields); // filter out empty values $map = array_filter($map); return $map; }
/** * @param MC4WP_Form $form * * @return string */ public function column_lists(MC4WP_Form $form) { $list_ids = $form->settings['lists']; if (empty($list_ids)) { $content = '<a href="' . mc4wp_get_edit_form_url($form->ID, 'settings') . '" style="color: red;">' . __('No lists selected.', 'mailchimp-for-wp') . '</a>'; return $content; } $names = array(); foreach ($list_ids as $list_id) { $list = $this->mailchimp->get_list($list_id); if ($list instanceof MC4WP_MailChimp_List) { $names[] = sprintf('<a href="https://admin.mailchimp.com/lists/members/?id=%s" target="_blank">', $list->web_id) . esc_html($list->name) . '</a>'; } } return join('<br />', $names); }
/** * Validates the posted fields against merge_vars of selected list(s) * * @param array $data * * @return array|boolean Array of data on success, false on error */ private function validate_merge_vars( array $data ) { $list_ids = $this->get_lists(); $mailchimp = new MC4WP_MailChimp(); foreach( $list_ids as $list_id ) { $list = $mailchimp->get_list( $list_id, false, true ); // make sure list was found if( ! is_object( $list ) ) { continue; } // loop through list fields foreach( $list->merge_vars as $merge_var ) { // skip email field, it's validated elsewhere if( $merge_var->tag === 'EMAIL' ) { continue; } $posted_value = ( isset( $data[ $merge_var->tag ] ) && '' !== $data[ $merge_var->tag ] ) ? $data[ $merge_var->tag ] : ''; // check if required field is given if( $merge_var->req && '' === $posted_value ) { $this->error_code = 'required_field_missing'; return false; } // format birthday fields in MM/DD format, required by MailChimp if( $merge_var->field_type === 'birthday' && $posted_value !== '' ) { $data[ $merge_var->tag ] = date( 'm/d', strtotime( $data[ $merge_var->tag ] ) ); } // format address fields if( $merge_var->field_type === 'address' && $posted_value !== '' ) { if( ! isset( $posted_value['addr1'] ) ) { // addr1, addr2, city, state, zip, country $address_pieces = explode( ',', $posted_value ); // try to fill it.... this is a long shot $data[ $merge_var->tag ] = array( 'addr1' => $address_pieces[0], 'city' => ( isset( $address_pieces[1] ) ) ? $address_pieces[1] : '', 'state' => ( isset( $address_pieces[2] ) ) ? $address_pieces[2] : '', 'zip' => ( isset( $address_pieces[3] ) ) ? $address_pieces[3] : '' ); } else { // form contains the necessary fields already: perfection $data[ $merge_var->tag ] = $posted_value; } } } } return $data; }
/** * Maps the received data to MailChimp lists * * @return array */ private function map_data($data) { $map = array(); $mapped_fields = array('EMAIL'); $unmapped_fields = array(); $mailchimp = new MC4WP_MailChimp(); // loop through selected lists foreach ($this->get_lists() as $list_id) { $list = $mailchimp->get_list($list_id, false, true); // skip this list if it's unexisting if (!is_object($list) || !isset($list->merge_vars)) { continue; } // start with empty list map $list_map = array(); // loop through other list fields foreach ($list->merge_vars as $field) { // skip EMAIL field if ($field->tag === 'EMAIL') { continue; } // check if field is required if ($field->req) { if (!isset($data[$field->tag]) || '' === $data[$field->tag]) { $this->error_code = 'required_field_missing'; return false; } } // if field is not set, continue. if (!isset($data[$field->tag])) { continue; } // grab field value from data $field_value = $data[$field->tag]; // format field value according to its type $field_value = $this->format_field_value($field_value, $field->field_type); // add field value to map $mapped_fields[] = $field->tag; $list_map[$field->tag] = $field_value; } // loop through list groupings if GROUPINGS data was sent if (isset($data['GROUPINGS']) && is_array($data['GROUPINGS']) && !empty($list->interest_groupings)) { $list_map['GROUPINGS'] = array(); foreach ($list->interest_groupings as $grouping) { // check if data for this group was sent if (isset($data['GROUPINGS'][$grouping->id])) { $group_data = $data['GROUPINGS'][$grouping->id]; } elseif (isset($data['GROUPINGS'][$grouping->name])) { $group_data = $data['GROUPINGS'][$grouping->name]; } else { // no data for this grouping was sent, just continue. continue; } // format new grouping $grouping = array('id' => $grouping->id, 'groups' => $group_data); // make sure groups is an array if (!is_array($grouping['groups'])) { $grouping['groups'] = sanitize_text_field($grouping['groups']); $grouping['groups'] = explode(',', $grouping['groups']); } $list_map['GROUPINGS'][] = $grouping; } // unset GROUPINGS if no grouping data was found for this list if (0 === count($list_map['GROUPINGS'])) { unset($list_map['GROUPINGS']); } } // add to total map $map[$list_id] = $list_map; } // map global fields $global_field_names = array('MC_LOCATION', 'MC_NOTES', 'MC_LANGUAGE'); foreach ($global_field_names as $field_name) { if (isset($data[$field_name])) { $this->global_fields[$field_name] = $data[$field_name]; } } // is there still unmapped data left? $total_fields_mapped = count($mapped_fields) + count($this->global_fields); if ($total_fields_mapped < count($data)) { foreach ($data as $field_key => $field_value) { if ($this->is_internal_var($field_key)) { continue; } if (!in_array($field_key, $mapped_fields)) { $unmapped_fields[$field_key] = $field_value; } } } // add built arrays to instance properties $this->unmapped_fields = $unmapped_fields; $this->lists_fields_map = $map; return true; }
/** * Show the forms settings page */ public function show_form_settings() { $opts = mc4wp_get_options('form'); $mailchimp = new MC4WP_MailChimp(); $lists = $mailchimp->get_lists(); // create array of missing form fields $missing_form_fields = array(); // check if form contains EMAIL field $search = preg_match('/<(input|textarea)(?=[^>]*name="EMAIL")[^>]*>/i', $opts['markup']); if (!$search) { $missing_form_fields[] = sprintf(__('An EMAIL field. Example: <code>%s</code>', 'mailchimp-for-wp'), '<input type="email" name="EMAIL" />'); } // check if form contains submit button $search = preg_match('/<(input|button)(?=[^>]*type="submit")[^>]*>/i', $opts['markup']); if (!$search) { $missing_form_fields[] = sprintf(__('A submit button. Example: <code>%s</code>', 'mailchimp-for-wp'), '<input type="submit" value="' . __('Sign Up', 'mailchimp-for-wp') . '" />'); } // loop through selected list ids if (isset($opts['lists']) && is_array($opts['lists'])) { foreach ($opts['lists'] as $list_id) { // get list object $list = $mailchimp->get_list($list_id); if (!is_object($list)) { continue; } // loop through merge vars of this list foreach ($list->merge_vars as $merge_var) { // if field is required, make sure it's in the form mark-up if (!$merge_var->req || $merge_var->tag === 'EMAIL') { continue; } // search for field tag in form mark-up using 'name="FIELD_NAME' without closing " because of array fields $search = stristr($opts['markup'], 'name="' . $merge_var->tag); if (false === $search) { $missing_form_fields[] = sprintf(__('A \'%s\' field', 'mailchimp-for-wp'), $merge_var->tag); } } } } require MC4WP_LITE_PLUGIN_DIR . 'includes/views/form-settings.php'; }