'; } public function description() { return __( 'Create persons, organizations, leads or deals and notes in Pipedrive.', 'fieldwren' ); } public function help() { return '

' . esc_html__( 'Pipedrive → your profile → Personal preferences → API → copy your personal API token.', 'fieldwren' ) . '

'; } public function credential_fields() { return array( 'api_token' => array( 'label' => __( 'API token', 'fieldwren' ), 'type' => 'password', 'required' => true ), ); } private function api( $method, $path, $body = null, $token = null ) { $args = array( 'headers' => array( 'x-api-token' => null === $token ? $this->cred( 'api_token' ) : $token ) ); if ( null !== $body ) { $args['body'] = $body; } return self::body( $this->http( $method, 'https://api.pipedrive.com/v1' . $path, $args ) ); } public function verify( array $creds ) { $b = $this->api( 'GET', '/users/me', null, (string) $creds['api_token'] ); return is_wp_error( $b ) ? $b : trim( ( $b['data']['name'] ?? '' ) . ' · ' . ( $b['data']['company_name'] ?? '' ), ' ·' ); } public function options( $source, array $settings ) { switch ( $source ) { case 'person_fields': $b = $this->api( 'GET', '/personFields?limit=500' ); if ( is_wp_error( $b ) ) { return $b; } $out = array(); foreach ( (array) ( $b['data'] ?? array() ) as $f ) { if ( ! empty( $f['edit_flag'] ) && in_array( $f['field_type'] ?? '', array( 'varchar', 'text', 'double', 'phone', 'date', 'varchar_auto', 'monetary', 'int', 'address', 'enum', 'time' ), true ) ) { $out[] = array( 'key' => $f['key'], 'label' => $f['name'] ); } } return $out; case 'stages': $b = $this->api( 'GET', '/stages' ); if ( is_wp_error( $b ) ) { return $b; } $out = array(); foreach ( (array) ( $b['data'] ?? array() ) as $st ) { $out[ (string) $st['id'] ] = ( $st['pipeline_name'] ?? '' ) . ' › ' . $st['name']; } return $out; case 'users': $b = $this->api( 'GET', '/users' ); return is_wp_error( $b ) ? $b : self::pluck( $b['data'] ?? array(), 'id', 'name' ); } return parent::options( $source, $settings ); } public function feed_fields( $form_id ) { $auto = self::auto_values( $form_id ); return array_merge( $this->contact_fields( array( 'email', 'name', 'phone' ) ), array( array( 'key' => 'organization', 'type' => 'value', 'label' => __( 'Organization', 'fieldwren' ), 'default' => 'auto:company', 'desc' => __( 'Found by name or created.', 'fieldwren' ) ), array( 'key' => 'map', 'type' => 'map', 'source' => 'person_fields', 'label' => __( 'Person custom fields', 'fieldwren' ) ), array( 'key' => 'owner', 'type' => 'select', 'source' => 'users', 'label' => __( 'Owner', 'fieldwren' ) ), array( 'key' => 'create', 'type' => 'select', 'label' => __( 'Also create', 'fieldwren' ), 'default' => 'lead', 'no_empty' => true, 'options' => array( 'none' => __( 'Nothing (person only)', 'fieldwren' ), 'lead' => __( 'Lead', 'fieldwren' ), 'deal' => __( 'Deal', 'fieldwren' ) ) ), array( 'key' => 'title', 'type' => 'text', 'label' => __( 'Lead / deal title', 'fieldwren' ), 'default' => '{form_title} – ' . ( $auto['name'] ? $auto['name'] : '#{entry.serial}' ), 'show_if' => array( 'create' => array( 'lead', 'deal' ) ) ), array( 'key' => 'value', 'type' => 'value', 'label' => __( 'Value', 'fieldwren' ), 'show_if' => array( 'create' => array( 'lead', 'deal' ) ) ), array( 'key' => 'currency', 'type' => 'text', 'label' => __( 'Currency', 'fieldwren' ), 'placeholder' => 'USD', 'smartcodes' => false, 'show_if' => array( 'create' => array( 'lead', 'deal' ) ) ), array( 'key' => 'stage', 'type' => 'select', 'source' => 'stages', 'label' => __( 'Deal stage', 'fieldwren' ), 'show_if' => array( 'create' => 'deal' ) ), array( 'key' => 'note', 'type' => 'textarea', 'label' => __( 'Note', 'fieldwren' ), 'default' => '{all_data_text}', 'rows' => 3, 'desc' => __( 'Added to the lead / deal (or the person). Leave empty for no note.', 'fieldwren' ) ), ) ); } /** @return int|\WP_Error Organization id (found or created), 0 without a name. */ private function org_id( $name, $owner ) { if ( '' === $name ) { return 0; } $b = $this->api( 'GET', '/organizations/search?exact_match=true&fields=name&limit=1&term=' . rawurlencode( $name ) ); if ( ! is_wp_error( $b ) && ! empty( $b['data']['items'][0]['item']['id'] ) ) { return (int) $b['data']['items'][0]['item']['id']; } $b = $this->api( 'POST', '/organizations', array_filter( array( 'name' => $name, 'owner_id' => $owner ) ) ); return is_wp_error( $b ) ? $b : (int) ( $b['data']['id'] ?? 0 ); } public function process( array $feed, array $entry, array $values ) { $s = $feed['settings']; $c = $this->contact(); $owner = (int) ( $s['owner'] ?? 0 ); if ( ! is_email( $c['email'] ) && '' === $c['name'] ) { return new \WP_Error( 'fwren_no_contact', __( 'No name or email in this entry; nothing was sent.', 'fieldwren' ) ); } $org = $this->org_id( $this->val( $s['organization'] ?? '' ), $owner ); if ( is_wp_error( $org ) ) { return $org; } $person = array_merge( $this->mapped( 'map' ), array( 'name' => '' !== $c['name'] ? $c['name'] : $c['email'] ) ); if ( $c['phone'] ) { $person['phone'] = array( array( 'value' => $c['phone'], 'primary' => true, 'label' => 'work' ) ); } if ( $org ) { $person['org_id'] = $org; } $pid = 0; if ( is_email( $c['email'] ) ) { $found = $this->api( 'GET', '/persons/search?exact_match=true&fields=email&limit=1&term=' . rawurlencode( $c['email'] ) ); $pid = is_wp_error( $found ) ? 0 : (int) ( $found['data']['items'][0]['item']['id'] ?? 0 ); } if ( $pid ) { $r = $this->api( 'PUT', '/persons/' . $pid, $person ); } else { if ( is_email( $c['email'] ) ) { $person['email'] = array( array( 'value' => $c['email'], 'primary' => true, 'label' => 'work' ) ); } if ( $owner ) { $person['owner_id'] = $owner; } $r = $this->api( 'POST', '/persons', $person ); $pid = is_wp_error( $r ) ? 0 : (int) ( $r['data']['id'] ?? 0 ); } if ( is_wp_error( $r ) ) { return $r; } /* translators: %d person id */ $out = array( sprintf( __( 'Person #%d saved.', 'fieldwren' ), $pid ) ); $what = (string) ( $s['create'] ?? 'lead' ); $title = $this->val( $s['title'] ?? '' ); $title = '' !== $title ? $title : $this->default_title(); $value = preg_replace( '/[^\d.]/', '', $this->val( $s['value'] ?? '' ) ); $cur = strtoupper( preg_replace( '/[^A-Za-z]/', '', (string) ( $s['currency'] ?? '' ) ) ); $cur = $cur ? $cur : 'USD'; $note = array( 'person_id' => $pid ); if ( 'lead' === $what ) { $lead = array_filter( array( 'title' => $title, 'person_id' => $pid, 'organization_id' => $org, 'owner_id' => $owner ) ); if ( '' !== $value ) { $lead['value'] = array( 'amount' => (float) $value, 'currency' => $cur ); } $l = $this->api( 'POST', '/leads', $lead ); if ( is_wp_error( $l ) ) { return $l; } $note = array( 'lead_id' => (string) ( $l['data']['id'] ?? '' ) ); $out[] = __( 'Lead created.', 'fieldwren' ); } elseif ( 'deal' === $what ) { $deal = array_filter( array( 'title' => $title, 'person_id' => $pid, 'org_id' => $org, 'user_id' => $owner, 'stage_id' => (int) ( $s['stage'] ?? 0 ) ) ); if ( '' !== $value ) { $deal['value'] = $value; $deal['currency'] = $cur; } $d = $this->api( 'POST', '/deals', $deal ); if ( is_wp_error( $d ) ) { return $d; } $note = array( 'deal_id' => (int) ( $d['data']['id'] ?? 0 ) ); /* translators: %d deal id */ $out[] = sprintf( __( 'Deal #%d created.', 'fieldwren' ), (int) ( $d['data']['id'] ?? 0 ) ); } $text = $this->val( $s['note'] ?? '' ); if ( '' !== $text ) { $n = $this->api( 'POST', '/notes', array_merge( $note, array( 'content' => nl2br( esc_html( $text ) ) ) ) ); if ( is_wp_error( $n ) ) { return $n; } } return implode( ' ', $out ); } }