, 'key' => , 'type' => , 'value' => ]. * @param string $source Integration name, e.g. "Contact Form 7". * @param string $form_title Form title for the timeline. * @param int $form_id Form ID. * @param array $context Extra context for capture() (e.g. url). * @return int Contact ID or 0. */ public static function from_fields( array $fields, $source, $form_title = '', $form_id = 0, array $context = array() ) { $data = array(); $props = array(); $extra = array(); $submitted = array(); $message = ''; $consent = null; $consent_text = ''; foreach ( $fields as $f ) { $label = isset( $f['label'] ) && '' !== (string) $f['label'] ? (string) $f['label'] : (string) ( $f['key'] ?? '' ); $value = $f['value'] ?? ''; if ( is_array( $value ) ) { $value = implode( ', ', array_map( 'strval', array_filter( $value, 'is_scalar' ) ) ); } $value = trim( (string) $value ); if ( '' === $value ) { continue; } // A newsletter / marketing checkbox: record the consent (with its wording as proof), not a field. $opt = Nodebook_Subscription::from_field( $label, (string) ( $f['key'] ?? '' ), (string) ( $f['type'] ?? '' ), $value ); if ( null !== $opt ) { $submitted[ mb_substr( wp_strip_all_tags( $label ), 0, 80 ) ] = mb_substr( sanitize_textarea_field( $value ), 0, 2000 ); if ( true !== $consent ) { $consent = $opt; $consent_text = trim( wp_strip_all_tags( $label ) . ( preg_match( '/^(1|yes|on|true|checked|accepted|s[ií]|ja|oui|y)$/i', $value ) ? '' : ': ' . $value ) ); } continue; } $target = Nodebook_Fields::guess( $label, (string) ( $f['key'] ?? '' ), (string) ( $f['type'] ?? '' ) ); if ( 'skip' === $target ) { continue; } // A field you defined ("Budget", "Check-in") wins over the guess, except for who the person is. if ( ! in_array( $target, array( 'email', 'phone', 'first_name', 'last_name', 'name' ), true ) ) { $prop = Nodebook_Props::match( 'contact', $label, (string) ( $f['key'] ?? '' ) ); if ( $prop ) { $submitted[ mb_substr( wp_strip_all_tags( $label ), 0, 80 ) ] = mb_substr( sanitize_textarea_field( $value ), 0, 2000 ); if ( ! isset( $props[ $prop ] ) ) { $props[ $prop ] = $value; } continue; } } $submitted[ mb_substr( wp_strip_all_tags( $label ), 0, 80 ) ] = mb_substr( sanitize_textarea_field( $value ), 0, 2000 ); if ( 'message' === $target ) { $message = $message ? $message : $value; continue; } if ( 'name' === $target ) { if ( empty( $data['first_name'] ) && empty( $data['last_name'] ) ) { $parts = preg_split( '/\s+/', $value, 2 ); $data['first_name'] = $parts[0]; $data['last_name'] = isset( $parts[1] ) ? $parts[1] : ''; } continue; } if ( '' === $target || ( 'email' === $target && ! is_email( $value ) && ! empty( $data['email'] ) ) ) { $extra[ $label ] = $value; continue; } if ( empty( $data[ $target ] ) ) { $data[ $target ] = $value; } } // The form itself knows (the Nodebook form's own checkbox). if ( null === $consent && isset( $context['email_consent'] ) ) { $consent = (bool) $context['email_consent']; $consent_text = (string) ( $context['consent_text'] ?? '' ); } unset( $context['email_consent'], $context['consent_text'] ); // A form that exists to sign people up ("Newsletter") is the opt-in itself. if ( null === $consent && '' === $message && Nodebook_Subscription::is_signup_form( $form_title ) ) { $consent = true; $consent_text = (string) $form_title; } if ( null !== $consent ) { $data['email_consent'] = array( 'agreed' => $consent, 'text' => $consent_text, ); } if ( $extra ) { $data['meta'] = $extra; } if ( $props ) { $data['props'] = $props; } return self::capture( $data, $source, array_merge( $context, array( 'form_title' => $form_title, 'form_id' => $form_id, 'fields' => $submitted, 'message' => $message, ) ) ); } /** * Create/update the contact, log the activity and link the visitor. * * @param array $data Contact data (email, first_name, …, company, meta). * @param string $source Source label. * @param array $context form_title, form_id, fields, message, activity (bool, default true), url. * @return int Contact ID or 0. */ public static function capture( array $data, $source = '', array $context = array() ) { /** * Filter captured data before it is saved. Return false to skip. */ $data = apply_filters( 'nodebook_capture_data', $data, $source, $context ); if ( ! is_array( $data ) ) { return 0; } if ( isset( $data['email'] ) ) { $data['email'] = strtolower( sanitize_email( $data['email'] ) ); if ( ! is_email( $data['email'] ) ) { unset( $data['email'] ); } } if ( empty( $data['email'] ) && empty( $data['phone'] ) ) { return 0; } if ( $source && empty( $data['source'] ) ) { $data['source'] = $source; } // Free-form custom fields whose name matches one of your fields go into that field. $props = isset( $data['props'] ) && is_array( $data['props'] ) ? $data['props'] : array(); unset( $data['props'] ); if ( ! empty( $data['meta'] ) && is_array( $data['meta'] ) ) { list( $more, $rest ) = Nodebook_Props::split_meta( 'contact', $data['meta'] ); $props += $more; $data['meta'] = $rest; } // Email consent is applied after the contact exists, with its own rules (see Nodebook_Subscription::set()). $consent = $data['email_consent'] ?? null; $record = isset( $data['email_status'] ) ? Nodebook_Subscription::normalize( $data['email_status'] ) : null; unset( $data['email_consent'], $data['email_status'] ); $result = Nodebook_Contacts::upsert( $data, Nodebook_Settings::get( 'update_policy' ) ); $id = $result['id']; if ( ! $id ) { return 0; } if ( $props ) { Nodebook_Props::set( 'contact', $id, $props, $result['created'] || 'overwrite' === Nodebook_Settings::get( 'update_policy' ) ? 'overwrite_nonempty' : 'fill' ); } self::apply_consent( $id, $consent, $record, $source, $context ); if ( $result['created'] ) { Nodebook_Activities::add( $id, 'created', /* translators: %s: source, e.g. "Contact Form 7" */ sprintf( __( 'Contact created from %s', 'nodebook' ), $source ? $source : __( 'unknown source', 'nodebook' ) ), array( 'user_id' => 0 ) ); } if ( ! isset( $context['activity'] ) || $context['activity'] ) { $url = isset( $context['url'] ) ? $context['url'] : self::referer(); $item = self::item_for_url( $url ); $title = ! empty( $context['form_title'] ) /* translators: %s: form title */ ? sprintf( __( 'Submitted “%s”', 'nodebook' ), $context['form_title'] ) /* translators: %s: source */ : sprintf( __( 'Submitted a form (%s)', 'nodebook' ), $source ); if ( $item ) { // "Viewing request: Villa 26" / "Quote request: X200 blaster" / "Question about: Botox". $title = sprintf( (string) Nodebook_Niche::get( 'inquiry' ), get_the_title( $item ) ); } Nodebook_Activities::add( $id, 'form', $title, array( 'object_id' => (int) ( $context['form_id'] ?? 0 ), 'url' => $url, 'user_id' => 0, 'visitor_id' => Nodebook_Tracker::current_visitor_id(), 'data' => array_filter( array( 'source' => $source, 'form' => $context['form_title'] ?? '', 'item' => $item, 'fields' => $context['fields'] ?? array(), 'message' => isset( $context['message'] ) ? mb_substr( sanitize_textarea_field( $context['message'] ), 0, 5000 ) : '', ) ), ) ); } Nodebook_Tracker::identify( $id ); /** * Fires after a contact was captured from a source. * * @param int $id Contact ID. * @param array $data Data that was captured. * @param string $source Source label. * @param bool $created Whether the contact is new. */ do_action( 'nodebook_captured', $id, $data, $source, $result['created'] ); return $id; } /** * Apply consent captured with a submission. * * @param int $id Contact. * @param mixed $consent true / false / [ 'agreed' => bool, 'text' => string ]: the person's own choice. * @param string|null $record A status from another system (webhook, API), or null. * @param string $source Source label. * @param array $context form_title, url. */ private static function apply_consent( $id, $consent, $record, $source, array $context ) { $where = trim( $source . ( ! empty( $context['form_title'] ) ? ': ' . $context['form_title'] : '' ), ': ' ); $url = isset( $context['url'] ) ? (string) $context['url'] : self::referer(); if ( null !== $consent ) { $agreed = is_array( $consent ) ? ! empty( $consent['agreed'] ) : (bool) $consent; $text = is_array( $consent ) ? (string) ( $consent['text'] ?? '' ) : ''; if ( $agreed ) { Nodebook_Subscription::set( $id, 'subscribed', array( 'source' => $where, 'text' => $text, 'url' => $url, 'explicit' => true, 'user_id' => 0, ) ); } else { $c = Nodebook_Contacts::get( $id ); // A "no" only matters for someone who was subscribed; otherwise there is nothing to record. if ( $c && in_array( $c['email_status'], array( 'subscribed', 'pending' ), true ) ) { Nodebook_Subscription::set( $id, 'unsubscribed', array( 'source' => $where, 'text' => $text, 'url' => $url, 'user_id' => 0, ) ); } } return; } if ( null !== $record && '' !== $record ) { Nodebook_Subscription::set( $id, $record, array( 'source' => $where, 'user_id' => 0, ) ); } } /** The item (property, listing, product…) a page URL shows, or 0. */ public static function item_for_url( $url ) { if ( ! $url || wp_parse_url( $url, PHP_URL_HOST ) !== wp_parse_url( home_url(), PHP_URL_HOST ) ) { return 0; } $id = (int) url_to_postid( $url ); // Properties, products, services and listings are items; contact, about and other utility pages are not. return $id && Nodebook_Interests::is_item( $id ) ? $id : 0; } private static function referer() { $ref = wp_get_referer(); if ( ! $ref ) { return ''; } $home = wp_parse_url( home_url(), PHP_URL_HOST ); return wp_parse_url( $ref, PHP_URL_HOST ) === $home ? $ref : ''; } }