' . esc_html__( 'Setup → Company Information → Salesforce.com Organization ID (starts with 00D). Web-to-Lead must be enabled (Setup → Web-to-Lead). Custom fields use their field ID (00N…), visible in the HTML Web-to-Lead generates.', 'fieldwren' ) . '
' . esc_html__( 'Salesforce does not confirm Web-to-Lead posts; turn on the debug email while testing.', 'fieldwren' ) . '
'; } public function credential_fields() { return array( 'oid' => array( 'label' => __( 'Organization ID', 'fieldwren' ), 'type' => 'text', 'required' => true, 'placeholder' => '00D…' ), 'env' => array( 'label' => __( 'Environment', 'fieldwren' ), 'type' => 'select', 'default' => 'production', 'options' => array( 'production' => __( 'Production', 'fieldwren' ), 'sandbox' => __( 'Sandbox', 'fieldwren' ) ) ), ); } public function verify_label() { return __( 'Check Organization ID', 'fieldwren' ); } public function verify( array $creds ) { return preg_match( '/^00D[A-Za-z0-9]{12}([A-Za-z0-9]{3})?$/', (string) $creds['oid'] ) ? __( 'ID format OK', 'fieldwren' ) : new \WP_Error( 'fwren_oid', __( 'An Organization ID has 15 or 18 characters and starts with 00D.', 'fieldwren' ) ); } public function feed_fields( $form_id ) { $std = array( 'first_name' => array( __( 'First name', 'fieldwren' ), 'first_name' ), 'last_name' => array( __( 'Last name', 'fieldwren' ), 'last_name' ), 'email' => array( __( 'Email', 'fieldwren' ), 'email' ), 'company' => array( __( 'Company', 'fieldwren' ), 'company' ), 'phone' => array( __( 'Phone', 'fieldwren' ), 'phone' ), 'mobile' => array( __( 'Mobile', 'fieldwren' ), '' ), 'title' => array( __( 'Title', 'fieldwren' ), '' ), 'URL' => array( __( 'Website', 'fieldwren' ), 'url' ), 'street' => array( __( 'Street', 'fieldwren' ), '' ), 'city' => array( __( 'City', 'fieldwren' ), '' ), 'state' => array( __( 'State / province', 'fieldwren' ), '' ), 'zip' => array( __( 'Zip', 'fieldwren' ), '' ), 'country' => array( __( 'Country', 'fieldwren' ), '' ), 'description' => array( __( 'Description', 'fieldwren' ), 'message' ), 'lead_source' => array( __( 'Lead source', 'fieldwren' ), '' ), 'industry' => array( __( 'Industry', 'fieldwren' ), '' ), ); $remote = array(); foreach ( $std as $k => $d ) { $remote[] = array( 'key' => $k, 'label' => $d[0], 'auto' => $d[1], 'required' => in_array( $k, array( 'last_name', 'company' ), true ) ); } return array( array( 'key' => 'map', 'type' => 'map', 'remote' => $remote, 'custom' => true, 'label' => __( 'Lead fields', 'fieldwren' ), 'desc' => __( 'Add custom fields with "Add field" and their ID (00N…).', 'fieldwren' ) ), array( 'key' => 'campaign', 'type' => 'text', 'label' => __( 'Campaign ID', 'fieldwren' ), 'smartcodes' => false, 'desc' => __( 'Optional: adds the lead to this campaign (701…).', 'fieldwren' ) ), array( 'key' => 'debug', 'type' => 'text', 'label' => __( 'Debug email', 'fieldwren' ), 'smartcodes' => false, 'desc' => __( 'While testing, Salesforce emails the result of each post to this address.', 'fieldwren' ) ), ); } public function process( array $feed, array $entry, array $values ) { $s = $feed['settings']; $body = $this->mapped( 'map' ); $c = $this->contact(); foreach ( array( 'email' => $c['email'], 'last_name' => $c['last_name'] ? $c['last_name'] : $c['name'], 'first_name' => $c['last_name'] ? $c['first_name'] : '' ) as $k => $v ) { if ( empty( $body[ $k ] ) && '' !== $v ) { $body[ $k ] = $v; } } if ( empty( $body['last_name'] ) && empty( $body['email'] ) ) { return new \WP_Error( 'fwren_no_contact', __( 'No name or email in this entry; nothing was sent.', 'fieldwren' ) ); } $body['oid'] = $this->cred( 'oid' ); $body['retURL'] = home_url( '/' ); if ( ! empty( $s['campaign'] ) ) { $body['Campaign_ID'] = (string) $s['campaign']; } if ( is_email( (string) ( $s['debug'] ?? '' ) ) ) { $body['debug'] = '1'; $body['debugEmail'] = (string) $s['debug']; } $host = 'sandbox' === $this->cred( 'env' ) ? 'https://test.salesforce.com' : 'https://webto.salesforce.com'; $res = $this->http( 'POST', $host . '/servlet/servlet.WebToLead?encoding=UTF-8', array( 'form' => true, 'body' => $body, 'redirection' => 0, 'headers' => array( 'Accept' => 'text/html' ) ) ); return is_wp_error( $res ) ? $res : __( 'Lead posted to Salesforce (Web-to-Lead does not confirm delivery).', 'fieldwren' ); } }