' . esc_html__( 'ActiveCampaign → Settings → Developer: copy the API URL and the API key.', 'fieldwren' ) . '
'; } public function credential_fields() { return array( 'api_url' => array( 'label' => __( 'API URL', 'fieldwren' ), 'type' => 'url', 'required' => true, 'placeholder' => 'https://youraccount.api-us1.com' ), 'api_key' => array( 'label' => __( 'API key', 'fieldwren' ), 'type' => 'password', 'required' => true ), ); } private function api( $method, $path, $body = null, array $creds = null ) { $c = null === $creds ? $this->creds() : $creds; $base = untrailingslashit( preg_replace( '#/api/3/?$#', '', (string) ( $c['api_url'] ?? '' ) ) ); $args = array( 'headers' => array( 'Api-Token' => (string) ( $c['api_key'] ?? '' ) ) ); if ( null !== $body ) { $args['body'] = $body; } return $this->http( $method, $base . '/api/3' . $path, $args ); } public function verify( array $creds ) { $b = self::body( $this->api( 'GET', '/users/me', null, $creds ) ); return is_wp_error( $b ) ? $b : (string) ( $b['user']['email'] ?? '' ); } public function options( $source, array $settings ) { if ( 'lists' === $source ) { $b = self::body( $this->api( 'GET', '/lists?limit=100' ) ); return is_wp_error( $b ) ? $b : self::pluck( $b['lists'] ?? array(), 'id', 'name' ); } if ( 'fields' === $source ) { $b = self::body( $this->api( 'GET', '/fields?limit=100' ) ); if ( is_wp_error( $b ) ) { return $b; } $out = array(); foreach ( (array) ( $b['fields'] ?? array() ) as $f ) { $out[] = array( 'key' => (string) $f['id'], 'label' => $f['title'] . ( ! empty( $f['perstag'] ) ? ' (%' . $f['perstag'] . '%)' : '' ), 'auto' => preg_match( '/compan/i', (string) $f['title'] ) ? 'company' : '' ); } 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' => __( 'List', 'fieldwren' ) ) ), $this->contact_fields(), array( array( 'key' => 'map', 'type' => 'map', 'source' => 'fields', 'label' => __( 'Custom fields', 'fieldwren' ) ), array( 'key' => 'tags', 'type' => 'text', 'label' => __( 'Tags', 'fieldwren' ), 'placeholder' => 'website lead', 'desc' => __( 'Comma separated; missing tags are created.', 'fieldwren' ) ), ) ); } /** @return int|\WP_Error Tag id (found or created). */ private function tag_id( $name ) { $b = self::body( $this->api( 'GET', '/tags?search=' . rawurlencode( $name ) ) ); if ( is_wp_error( $b ) ) { return $b; } foreach ( (array) ( $b['tags'] ?? array() ) as $t ) { if ( 0 === strcasecmp( (string) $t['tag'], $name ) ) { return (int) $t['id']; } } $b = self::body( $this->api( 'POST', '/tags', array( 'tag' => array( 'tag' => $name, 'tagType' => 'contact', 'description' => '' ) ) ) ); return is_wp_error( $b ) ? $b : (int) ( $b['tag']['id'] ?? 0 ); } public function process( array $feed, array $entry, array $values ) { $s = $feed['settings']; $c = $this->contact(); $err = $this->require_email( $c ); if ( $err ) { return $err; } $contact = array_filter( array( 'email' => $c['email'], 'firstName' => $c['first_name'], 'lastName' => $c['last_name'], 'phone' => $c['phone'], ), 'strlen' ); foreach ( $this->mapped( 'map' ) as $field => $v ) { $contact['fieldValues'][] = array( 'field' => (string) $field, 'value' => $v ); } $b = self::body( $this->api( 'POST', '/contact/sync', array( 'contact' => $contact ) ) ); if ( is_wp_error( $b ) ) { return $b; } $id = (int) ( $b['contact']['id'] ?? 0 ); if ( ! $id ) { return new \WP_Error( 'fwren_ac', __( 'ActiveCampaign returned no contact id.', 'fieldwren' ) ); } if ( ! empty( $s['list_id'] ) ) { $r = $this->api( 'POST', '/contactLists', array( 'contactList' => array( 'list' => (int) $s['list_id'], 'contact' => $id, 'status' => 1 ) ) ); if ( is_wp_error( $r ) ) { return $r; } } foreach ( $this->list_of( $s['tags'] ?? '' ) as $name ) { $tid = $this->tag_id( $name ); if ( is_wp_error( $tid ) ) { return $tid; } if ( $tid ) { $r = $this->api( 'POST', '/contactTags', array( 'contactTag' => array( 'contact' => $id, 'tag' => $tid ) ) ); if ( is_wp_error( $r ) ) { return $r; } } } /* translators: 1: email, 2: id */ return sprintf( __( '%1$s synced (contact #%2$d).', 'fieldwren' ), $c['email'], $id ); } }