' . esc_html__( 'Kit → Settings → Developer → API keys (v4) → Add a new key.', 'fieldwren' ) . '
'; } public function credential_fields() { return array( 'api_key' => array( 'label' => __( 'API key (v4)', 'fieldwren' ), 'type' => 'password', 'required' => true, 'placeholder' => 'kit_…' ), ); } private function api( $method, $path, $body = null, $key = null ) { $args = array( 'headers' => array( 'X-Kit-Api-Key' => null === $key ? $this->cred( 'api_key' ) : $key ) ); if ( null !== $body ) { $args['body'] = $body; } return $this->http( $method, 'https://api.kit.com/v4' . $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 : (string) ( $b['account']['name'] ?? '' ); } public function options( $source, array $settings ) { switch ( $source ) { case 'forms': $b = self::body( $this->api( 'GET', '/forms?per_page=1000&status=active' ) ); return is_wp_error( $b ) ? $b : self::pluck( $b['forms'] ?? array(), 'id', 'name' ); case 'tags': $b = self::body( $this->api( 'GET', '/tags?per_page=1000' ) ); return is_wp_error( $b ) ? $b : self::pluck( $b['tags'] ?? array(), 'id', 'name' ); case 'fields': $b = self::body( $this->api( 'GET', '/custom_fields?per_page=1000' ) ); if ( is_wp_error( $b ) ) { return $b; } $out = array(); foreach ( (array) ( $b['custom_fields'] ?? array() ) as $f ) { $out[] = array( 'key' => $f['key'], 'label' => $f['label'], 'auto' => preg_match( '/last.?name/i', $f['key'] ) ? 'last_name' : ( preg_match( '/phone/i', $f['key'] ) ? 'phone' : '' ) ); } return $out; } return parent::options( $source, $settings ); } public function feed_fields( $form_id ) { return array_merge( array( array( 'key' => 'form_id', 'type' => 'select', 'source' => 'forms', 'label' => __( 'Kit form', 'fieldwren' ), 'desc' => __( 'Optional. The form\'s double opt-in and incentive email apply.', 'fieldwren' ) ) ), $this->contact_fields( array( 'email', 'first_name' ) ), array( array( 'key' => 'tags', 'type' => 'select', 'multiple' => true, 'source' => 'tags', 'label' => __( 'Tags', 'fieldwren' ) ), array( 'key' => 'map', 'type' => 'map', 'source' => 'fields', 'label' => __( 'Custom fields', 'fieldwren' ) ), array( 'key' => 'double_optin', 'type' => 'toggle', 'label' => __( 'Double opt-in', 'fieldwren' ), 'text' => __( 'With a Kit form: create new subscribers as inactive until they confirm the form\'s opt-in email', '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; } $body = array( 'email_address' => $c['email'] ); if ( $c['first_name'] ) { $body['first_name'] = $c['first_name']; } $fields = $this->mapped( 'map' ); if ( $fields ) { $body['fields'] = (object) $fields; } $form = (string) ( $s['form_id'] ?? '' ); if ( $form && ! empty( $s['double_optin'] ) ) { $body['state'] = 'inactive'; // Kit activates the subscriber when the form's opt-in email is confirmed. } $res = $this->api( 'POST', '/subscribers', $body ); if ( is_wp_error( $res ) ) { return $res; } if ( $form ) { $r = $this->api( 'POST', '/forms/' . rawurlencode( $form ) . '/subscribers', array( 'email_address' => $c['email'], 'referrer' => (string) ( $entry['source_url'] ?? '' ) ) ); if ( is_wp_error( $r ) ) { return $r; } } foreach ( (array) ( $s['tags'] ?? array() ) as $tag ) { $r = $this->api( 'POST', '/tags/' . rawurlencode( (string) $tag ) . '/subscribers', array( 'email_address' => $c['email'] ) ); if ( is_wp_error( $r ) ) { return $r; } } /* translators: %s email */ return sprintf( __( '%s saved.', 'fieldwren' ), $c['email'] ); } }