'; } public function description() { return __( 'Add or update audience members with merge fields, groups, tags and double opt-in.', 'fieldwren' ); } public function help() { return '

' . esc_html__( 'Mailchimp → Profile → Extras → API keys → Create A Key. The key ends with your data centre, e.g. -us21.', 'fieldwren' ) . '

'; } public function credential_fields() { return array( 'api_key' => array( 'label' => __( 'API key', 'fieldwren' ), 'type' => 'password', 'required' => true, 'placeholder' => 'xxxxxxxxxxxxxxxx-us21' ), ); } /** API call. */ private function api( $method, $path, $body = null, array $creds = null ) { $key = trim( (string) ( null === $creds ? $this->cred( 'api_key' ) : ( $creds['api_key'] ?? '' ) ) ); $dc = false !== strpos( $key, '-' ) ? preg_replace( '/[^a-z0-9]/', '', substr( strrchr( $key, '-' ), 1 ) ) : ''; if ( ! $dc ) { return new \WP_Error( 'fwren_key', __( 'The Mailchimp API key must end with the data centre, e.g. -us21.', 'fieldwren' ) ); } $this->secrets[] = $key; $args = array( 'auth' => array( 'fwren', $key ) ); if ( null !== $body ) { $args['body'] = $body; } return $this->http( $method, 'https://' . $dc . '.api.mailchimp.com/3.0' . $path, $args ); } protected function error_message( $body, $code ) { if ( is_array( $body ) && ! empty( $body['detail'] ) ) { $m = (string) $body['detail']; foreach ( (array) ( $body['errors'] ?? array() ) as $e ) { $m .= ' ' . ( $e['field'] ?? '' ) . ': ' . ( $e['message'] ?? '' ); } return $m; } return parent::error_message( $body, $code ); } public function verify( array $creds ) { $b = self::body( $this->api( 'GET', '/?fields=account_name', null, $creds ) ); return is_wp_error( $b ) ? $b : (string) ( $b['account_name'] ?? '' ); } public function options( $source, array $settings ) { $list = rawurlencode( (string) ( $settings['list_id'] ?? '' ) ); switch ( $source ) { case 'lists': $b = self::body( $this->api( 'GET', '/lists?count=1000&fields=lists.id,lists.name' ) ); return is_wp_error( $b ) ? $b : self::pluck( $b['lists'] ?? array(), 'id', 'name' ); case 'merge_fields': $b = self::body( $this->api( 'GET', '/lists/' . $list . '/merge-fields?count=1000&fields=merge_fields.tag,merge_fields.name,merge_fields.required,merge_fields.type' ) ); if ( is_wp_error( $b ) ) { return $b; } $auto = array( 'FNAME' => 'first_name', 'LNAME' => 'last_name', 'PHONE' => 'phone', 'COMPANY' => 'company' ); $out = array(); foreach ( (array) ( $b['merge_fields'] ?? array() ) as $m ) { if ( 'address' === ( $m['type'] ?? '' ) ) { continue; } $out[] = array( 'key' => $m['tag'], 'label' => $m['name'], 'required' => ! empty( $m['required'] ), 'auto' => $auto[ $m['tag'] ] ?? '' ); } return $out; case 'interests': $b = self::body( $this->api( 'GET', '/lists/' . $list . '/interest-categories?count=100&fields=categories.id,categories.title' ) ); if ( is_wp_error( $b ) ) { return $b; } $out = array(); foreach ( (array) ( $b['categories'] ?? array() ) as $c ) { $i = self::body( $this->api( 'GET', '/lists/' . $list . '/interest-categories/' . rawurlencode( $c['id'] ) . '/interests?count=100&fields=interests.id,interests.name' ) ); foreach ( is_wp_error( $i ) ? array() : (array) ( $i['interests'] ?? array() ) as $it ) { $out[ $it['id'] ] = $c['title'] . ' › ' . $it['name']; } } return $out; } return parent::options( $source, $settings ); } public function feed_fields( $form_id ) { return array_merge( array( array( 'key' => 'list_id', 'type' => 'select', 'source' => 'lists', 'label' => __( 'Audience', 'fieldwren' ), 'required' => true ) ), $this->contact_fields( array( 'email' ) ), array( array( 'key' => 'map', 'type' => 'map', 'source' => 'merge_fields', 'depends' => array( 'list_id' ), 'label' => __( 'Merge fields', 'fieldwren' ) ), array( 'key' => 'interests', 'type' => 'select', 'multiple' => true, 'source' => 'interests', 'depends' => array( 'list_id' ), 'label' => __( 'Groups', 'fieldwren' ) ), array( 'key' => 'tags', 'type' => 'text', 'label' => __( 'Tags', 'fieldwren' ), 'placeholder' => 'lead, website', 'desc' => __( 'Comma separated; smartcodes allowed.', 'fieldwren' ) ), array( 'key' => 'double_optin', 'type' => 'toggle', 'label' => __( 'Double opt-in', 'fieldwren' ), 'text' => __( 'New contacts must confirm by email (status "pending")', 'fieldwren' ) ), array( 'key' => 'update_existing', 'type' => 'toggle', 'label' => __( 'Existing contacts', 'fieldwren' ), 'text' => __( 'Update existing contacts', 'fieldwren' ), 'default' => true ), array( 'key' => 'gdpr', 'type' => 'toggle', 'label' => __( 'GDPR', 'fieldwren' ), 'text' => __( 'Enable the audience\'s marketing permissions when the consent field is ticked', 'fieldwren' ) ), array( 'key' => 'consent_field', 'type' => 'field', 'label' => __( 'Consent field', 'fieldwren' ), 'show_if' => array( 'gdpr' => true ) ), ) ); } /** @return string[] Marketing permission ids of an audience (read from one member, cached). */ private function permissions( $list ) { $key = 'fwren_mc_perm_' . md5( $list ); $ids = get_transient( $key ); if ( is_array( $ids ) ) { return $ids; } $b = self::body( $this->api( 'GET', '/lists/' . rawurlencode( $list ) . '/members?count=1&fields=members.marketing_permissions' ) ); $ids = array(); foreach ( is_wp_error( $b ) ? array() : (array) ( $b['members'][0]['marketing_permissions'] ?? array() ) as $p ) { $ids[] = (string) $p['marketing_permission_id']; } set_transient( $key, $ids, DAY_IN_SECONDS ); return $ids; } public function process( array $feed, array $entry, array $values ) { $s = $feed['settings']; $list = (string) ( $s['list_id'] ?? '' ); $c = $this->contact(); if ( ! $list ) { return new \WP_Error( 'fwren_no_list', __( 'Choose an audience.', 'fieldwren' ) ); } $err = $this->require_email( $c ); if ( $err ) { return $err; } $hash = md5( strtolower( $c['email'] ) ); $status = ! empty( $s['double_optin'] ) ? 'pending' : 'subscribed'; $body = array( 'email_address' => $c['email'] ); $merge = $this->mapped( 'map' ); if ( $merge ) { $body['merge_fields'] = (object) $merge; } $interests = array(); foreach ( (array) ( $s['interests'] ?? array() ) as $i ) { $interests[ $i ] = true; } if ( $interests ) { $body['interests'] = (object) $interests; } if ( filter_var( $this->ip(), FILTER_VALIDATE_IP ) ) { $body['ip_signup'] = $this->ip(); } if ( ! empty( $s['gdpr'] ) && ! empty( $s['consent_field'] ) && $this->truthy( $this->raw( $s['consent_field'] ) ) ) { $perms = array(); foreach ( $this->permissions( $list ) as $pid ) { $perms[] = array( 'marketing_permission_id' => $pid, 'enabled' => true ); } if ( $perms ) { $body['marketing_permissions'] = $perms; } } if ( ! empty( $s['update_existing'] ) ) { $body['status_if_new'] = $status; $res = $this->api( 'PUT', '/lists/' . rawurlencode( $list ) . '/members/' . $hash, $body ); } else { $body['status'] = $status; $res = $this->api( 'POST', '/lists/' . rawurlencode( $list ) . '/members', $body ); if ( is_wp_error( $res ) && false !== stripos( $res->get_error_message(), 'already a list member' ) ) { return __( 'Already in the audience; not updated.', 'fieldwren' ); } } if ( is_wp_error( $res ) ) { return $res; } $tags = $this->list_of( $s['tags'] ?? '' ); if ( $tags ) { $t = array(); foreach ( $tags as $name ) { $t[] = array( 'name' => self::cut( $name, 100 ), 'status' => 'active' ); } $tr = $this->api( 'POST', '/lists/' . rawurlencode( $list ) . '/members/' . $hash . '/tags', array( 'tags' => $t ) ); if ( is_wp_error( $tr ) ) { return $tr; } } $st = is_array( $res['body'] ) ? (string) ( $res['body']['status'] ?? $status ) : $status; /* translators: 1: email, 2: status */ return sprintf( __( '%1$s saved (%2$s).', 'fieldwren' ), $c['email'], $st ); } }