' . esc_html__( 'GetResponse → Tools → Integrations and API → API → Generate API key. (GetResponse MAX accounts are not supported.)', 'fieldwren' ) . '
'; } public function credential_fields() { return array( 'api_key' => array( 'label' => __( 'API key', 'fieldwren' ), 'type' => 'password', 'required' => true ), ); } private function api( $method, $path, $body = null, $key = null ) { $args = array( 'headers' => array( 'X-Auth-Token' => 'api-key ' . ( null === $key ? $this->cred( 'api_key' ) : $key ) ) ); if ( null !== $body ) { $args['body'] = $body; } return $this->http( $method, 'https://api.getresponse.com/v3' . $path, $args ); } protected function error_message( $body, $code ) { if ( is_array( $body ) && isset( $body['message'] ) ) { $m = (string) $body['message']; foreach ( (array) ( $body['context'] ?? array() ) as $ctx ) { $m .= is_array( $ctx ) && isset( $ctx['errorDescription'] ) ? ' ' . $ctx['errorDescription'] : ( is_string( $ctx ) ? ' ' . $ctx : '' ); } return $m; } return parent::error_message( $body, $code ); } public function verify( array $creds ) { $b = self::body( $this->api( 'GET', '/accounts', null, (string) $creds['api_key'] ) ); return is_wp_error( $b ) ? $b : (string) ( $b['email'] ?? '' ); } public function options( $source, array $settings ) { switch ( $source ) { case 'campaigns': $b = self::body( $this->api( 'GET', '/campaigns?perPage=1000' ) ); return is_wp_error( $b ) ? $b : self::pluck( $b, 'campaignId', 'name' ); case 'tags': $b = self::body( $this->api( 'GET', '/tags?perPage=1000' ) ); return is_wp_error( $b ) ? $b : self::pluck( $b, 'tagId', 'name' ); case 'fields': $b = self::body( $this->api( 'GET', '/custom-fields?perPage=1000' ) ); if ( is_wp_error( $b ) ) { return $b; } $out = array(); foreach ( (array) $b as $f ) { $out[] = array( 'key' => $f['customFieldId'], 'label' => $f['name'], 'auto' => 'phone' === ( $f['type'] ?? '' ) ? 'phone' : ( 'company' === ( $f['name'] ?? '' ) ? 'company' : '' ) ); } return $out; } return parent::options( $source, $settings ); } public function feed_fields( $form_id ) { return array_merge( array( array( 'key' => 'campaign_id', 'type' => 'select', 'source' => 'campaigns', 'label' => __( 'List', 'fieldwren' ), 'required' => true ) ), $this->contact_fields( array( 'email', 'name' ) ), array( array( 'key' => 'map', 'type' => 'map', 'source' => 'fields', 'label' => __( 'Custom fields', 'fieldwren' ), 'desc' => __( 'Phone custom fields need the international format (+48…).', 'fieldwren' ) ), array( 'key' => 'tags', 'type' => 'select', 'multiple' => true, 'source' => 'tags', 'label' => __( 'Tags', 'fieldwren' ) ), array( 'key' => 'day_of_cycle', 'type' => 'number', 'label' => __( 'Autoresponder day', 'fieldwren' ), 'desc' => __( 'Optional: start the autoresponder cycle on this day (0 = first message).', 'fieldwren' ) ), ) ); } public function process( array $feed, array $entry, array $values ) { $s = $feed['settings']; $c = $this->contact(); $err = $this->require_email( $c ); if ( $err ) { return $err; } $list = (string) ( $s['campaign_id'] ?? '' ); if ( ! $list ) { return new \WP_Error( 'fwren_no_list', __( 'Choose a list.', 'fieldwren' ) ); } $body = array(); if ( '' !== $c['name'] ) { $body['name'] = self::cut( $c['name'], 128 ); } foreach ( $this->mapped( 'map' ) as $id => $v ) { $body['customFieldValues'][] = array( 'customFieldId' => (string) $id, 'value' => array( $v ) ); } foreach ( (array) ( $s['tags'] ?? array() ) as $t ) { $body['tags'][] = array( 'tagId' => (string) $t ); } if ( '' !== (string) ( $s['day_of_cycle'] ?? '' ) ) { $body['dayOfCycle'] = (string) (int) $s['day_of_cycle']; } $new = array_merge( array( 'email' => $c['email'], 'campaign' => array( 'campaignId' => $list ) ), $body ); if ( filter_var( $this->ip(), FILTER_VALIDATE_IP ) ) { $new['ipAddress'] = $this->ip(); } $res = $this->api( 'POST', '/contacts', $new ); if ( ! is_wp_error( $res ) ) { /* translators: %s email */ return sprintf( __( '%s added (GetResponse adds contacts in the background).', 'fieldwren' ), $c['email'] ); } if ( 'fwren_http_409' !== $res->get_error_code() ) { return $res; } $found = self::body( $this->api( 'GET', '/contacts?query[email]=' . rawurlencode( $c['email'] ) . '&query[campaignId]=' . rawurlencode( $list ) . '&fields=contactId' ) ); $id = is_wp_error( $found ) ? '' : (string) ( $found[0]['contactId'] ?? '' ); if ( ! $id ) { return __( 'Contact already exists (queued for adding).', 'fieldwren' ); } $up = $this->api( 'POST', '/contacts/' . rawurlencode( $id ), $body ? $body : array( 'campaign' => array( 'campaignId' => $list ) ) ); /* translators: %s email */ return is_wp_error( $up ) ? $up : sprintf( __( '%s updated.', 'fieldwren' ), $c['email'] ); } }