' . esc_html__( 'Brevo → your name → SMTP & API → API keys → Generate a new API key.', 'fieldwren' ) . '
'; } public function credential_fields() { return array( 'api_key' => array( 'label' => __( 'API key (v3)', 'fieldwren' ), 'type' => 'password', 'required' => true, 'placeholder' => 'xkeysib-…' ), ); } private function api( $method, $path, $body = null, $key = null ) { $key = null === $key ? $this->cred( 'api_key' ) : $key; $args = array( 'headers' => array( 'api-key' => $key ) ); if ( null !== $body ) { $args['body'] = $body; } return $this->http( $method, 'https://api.brevo.com/v3' . $path, $args ); } public function verify( array $creds ) { $b = self::body( $this->api( 'GET', '/account', null, (string) $creds['api_key'] ) ); return is_wp_error( $b ) ? $b : trim( (string) ( $b['companyName'] ?? '' ) . ' ' . ( $b['email'] ?? '' ) ); } public function options( $source, array $settings ) { switch ( $source ) { case 'lists': $out = array(); for ( $offset = 0; $offset < 500; $offset += 50 ) { $b = self::body( $this->api( 'GET', '/contacts/lists?limit=50&offset=' . $offset ) ); if ( is_wp_error( $b ) ) { return $b; } $out += self::pluck( $b['lists'] ?? array(), 'id', 'name' ); if ( count( (array) ( $b['lists'] ?? array() ) ) < 50 ) { break; } } return $out; case 'attributes': $b = self::body( $this->api( 'GET', '/contacts/attributes' ) ); if ( is_wp_error( $b ) ) { return $b; } $auto = array( 'FIRSTNAME' => 'first_name', 'LASTNAME' => 'last_name', 'SMS' => 'phone', 'PRENOM' => 'first_name', 'NOM' => 'last_name' ); $out = array(); foreach ( (array) ( $b['attributes'] ?? array() ) as $a ) { if ( 'normal' === ( $a['category'] ?? '' ) ) { $out[] = array( 'key' => $a['name'], 'label' => $a['name'], 'auto' => $auto[ $a['name'] ] ?? '' ); } } return $out; case 'templates': $b = self::body( $this->api( 'GET', '/smtp/templates?templateStatus=true&limit=100' ) ); return is_wp_error( $b ) ? $b : self::pluck( $b['templates'] ?? array(), 'id', 'name' ); } return parent::options( $source, $settings ); } public function feed_fields( $form_id ) { return array_merge( array( array( 'key' => 'lists', 'type' => 'select', 'multiple' => true, 'source' => 'lists', 'label' => __( 'Lists', 'fieldwren' ), 'required' => true ) ), $this->contact_fields( array( 'email' ) ), array( array( 'key' => 'map', 'type' => 'map', 'source' => 'attributes', 'label' => __( 'Attributes', 'fieldwren' ), 'desc' => __( 'SMS must be in international format (+44…).', 'fieldwren' ) ), array( 'key' => 'double_optin', 'type' => 'toggle', 'label' => __( 'Double opt-in', 'fieldwren' ), 'text' => __( 'Send Brevo\'s confirmation email before adding the contact', 'fieldwren' ) ), array( 'key' => 'template_id', 'type' => 'select', 'source' => 'templates', 'label' => __( 'Opt-in template', 'fieldwren' ), 'show_if' => array( 'double_optin' => true ), 'desc' => __( 'A template tagged "optin" containing the {{ doubleoptin }} link.', 'fieldwren' ) ), array( 'key' => 'redirect', 'type' => 'text', 'url' => true, 'label' => __( 'After confirming, go to', 'fieldwren' ), 'placeholder' => home_url( '/' ), 'show_if' => array( 'double_optin' => true ) ), array( 'key' => 'update_existing', 'type' => 'toggle', 'label' => __( 'Existing contacts', 'fieldwren' ), 'text' => __( 'Update existing contacts', 'fieldwren' ), 'default' => true ), ) ); } public function process( array $feed, array $entry, array $values ) { $s = $feed['settings']; $c = $this->contact(); $err = $this->require_email( $c ); $lists = array_map( 'intval', (array) ( $s['lists'] ?? array() ) ); if ( $err ) { return $err; } $attrs = $this->mapped( 'map' ); if ( ! empty( $s['double_optin'] ) ) { if ( empty( $s['template_id'] ) || ! $lists ) { return new \WP_Error( 'fwren_doi', __( 'Double opt-in needs a list and an opt-in template.', 'fieldwren' ) ); } $res = $this->api( 'POST', '/contacts/doubleOptinConfirmation', array( 'email' => $c['email'], 'attributes' => (object) $attrs, 'includeListIds' => $lists, 'templateId' => (int) $s['template_id'], 'redirectionUrl' => $this->val( $s['redirect'] ?? '' ) ? $this->val( $s['redirect'] ) : home_url( '/' ), ) ); /* translators: %s email */ return is_wp_error( $res ) ? $res : sprintf( __( 'Confirmation email sent to %s.', 'fieldwren' ), $c['email'] ); } $body = array( 'email' => $c['email'], 'updateEnabled' => ! empty( $s['update_existing'] ) ); if ( $attrs ) { $body['attributes'] = (object) $attrs; } if ( $lists ) { $body['listIds'] = $lists; } $res = $this->api( 'POST', '/contacts', $body ); if ( is_wp_error( $res ) && false !== stripos( $res->get_error_message(), 'already exist' ) ) { return __( 'Contact already exists; not updated.', 'fieldwren' ); } /* translators: %s email */ return is_wp_error( $res ) ? $res : sprintf( __( '%s saved.', 'fieldwren' ), $c['email'] ); } }