'; } public function description() { return __( 'Create Drip subscribers with tags, custom fields and email series.', 'fieldwren' ); } public function help() { return '

' . esc_html__( 'Drip → User settings → API token; the Account ID is in Settings → General Info (or in your Drip URL).', 'fieldwren' ) . '

'; } public function credential_fields() { return array( 'api_token' => array( 'label' => __( 'API token', 'fieldwren' ), 'type' => 'password', 'required' => true ), 'account_id' => array( 'label' => __( 'Account ID', 'fieldwren' ), 'type' => 'text', 'required' => true ), ); } private function api( $method, $path, $body = null, array $creds = null ) { $c = null === $creds ? $this->creds() : $creds; $args = array( 'auth' => array( (string) ( $c['api_token'] ?? '' ), '' ) ); if ( null !== $body ) { $args['body'] = $body; } $acct = preg_replace( '/\D/', '', (string) ( $c['account_id'] ?? '' ) ); return $this->http( $method, 'https://api.getdrip.com/v2' . ( '' === $path ? '/accounts' : '/' . $acct . $path ), $args ); } public function verify( array $creds ) { $b = self::body( $this->api( 'GET', '', null, $creds ) ); if ( is_wp_error( $b ) ) { return $b; } foreach ( (array) ( $b['accounts'] ?? array() ) as $a ) { if ( (string) $a['id'] === preg_replace( '/\D/', '', (string) $creds['account_id'] ) ) { return (string) $a['name']; } } return new \WP_Error( 'fwren_drip', __( 'The token works, but the Account ID was not found.', 'fieldwren' ) ); } public function options( $source, array $settings ) { if ( 'campaigns' === $source ) { $b = self::body( $this->api( 'GET', '/campaigns?status=active&per_page=1000' ) ); return is_wp_error( $b ) ? $b : self::pluck( $b['campaigns'] ?? array(), 'id', 'name' ); } return parent::options( $source, $settings ); } public function feed_fields( $form_id ) { return array_merge( $this->contact_fields(), array( array( 'key' => 'tags', 'type' => 'text', 'label' => __( 'Tags', 'fieldwren' ), 'desc' => __( 'Comma separated; smartcodes allowed.', 'fieldwren' ) ), array( 'key' => 'custom', 'type' => 'kv', 'label' => __( 'Custom fields', 'fieldwren' ), 'key_label' => __( 'Field identifier', 'fieldwren' ) ), array( 'key' => 'campaign_id', 'type' => 'select', 'source' => 'campaigns', 'label' => __( 'Email series', 'fieldwren' ), 'desc' => __( 'Optional: start this email series campaign.', 'fieldwren' ) ), array( 'key' => 'double_optin', 'type' => 'toggle', 'label' => __( 'Double opt-in', 'fieldwren' ), 'text' => __( 'Ask for confirmation before the series starts', '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; } $sub = array_filter( array( 'email' => $c['email'], 'first_name' => $c['first_name'], 'last_name' => $c['last_name'], 'phone' => $c['phone'], ), 'strlen' ); $tags = $this->list_of( $s['tags'] ?? '' ); if ( $tags ) { $sub['tags'] = $tags; } $custom = array_filter( $this->kv( 'custom' ), 'strlen' ); if ( $custom ) { $sub['custom_fields'] = (object) $custom; } if ( filter_var( $this->ip(), FILTER_VALIDATE_IP ) ) { $sub['ip_address'] = $this->ip(); } $res = $this->api( 'POST', '/subscribers', array( 'subscribers' => array( $sub ) ) ); if ( is_wp_error( $res ) ) { return $res; } if ( ! empty( $s['campaign_id'] ) ) { $r = $this->api( 'POST', '/campaigns/' . rawurlencode( (string) $s['campaign_id'] ) . '/subscribers', array( 'subscribers' => array( array( 'email' => $c['email'], 'double_optin' => ! empty( $s['double_optin'] ) ) ) ) ); if ( is_wp_error( $r ) ) { return $r; } } /* translators: %s email */ return sprintf( __( '%s saved.', 'fieldwren' ), $c['email'] ); } }