__( 'Not asked', 'nodebook' ), 'subscribed' => __( 'Subscribed', 'nodebook' ), 'pending' => __( 'Waiting for confirmation', 'nodebook' ), 'unsubscribed' => __( 'Unsubscribed', 'nodebook' ), 'bounced' => __( 'Bounced', 'nodebook' ), 'complained' => __( 'Marked as spam', 'nodebook' ), ); } /** Normalise a status from anywhere (CSV, webhook, other CRMs). Returns null when unknown. */ public static function normalize( $value ) { $v = strtolower( trim( (string) $value ) ); $map = array( 'subscribed' => 'subscribed', 'subscriber' => 'subscribed', 'opted in' => 'subscribed', 'opt-in' => 'subscribed', 'optin' => 'subscribed', 'confirmed' => 'subscribed', 'active' => 'subscribed', 'yes' => 'subscribed', 'true' => 'subscribed', '1' => 'subscribed', 'pending' => 'pending', 'unconfirmed' => 'pending', 'unsubscribed' => 'unsubscribed', 'opted out' => 'unsubscribed', 'opt-out' => 'unsubscribed', 'optout' => 'unsubscribed', 'no' => 'unsubscribed', 'false' => 'unsubscribed', '0' => 'unsubscribed', 'bounced' => 'bounced', 'bounce' => 'bounced', 'hard bounce' => 'bounced', 'complained' => 'complained', 'complaint' => 'complained', 'spam' => 'complained', 'spammed' => 'complained', 'not asked' => '', '' => '', ); return array_key_exists( $v, $map ) ? $map[ $v ] : null; } /** Whether marketing email may be sent to this contact. */ public static function can_email( array $c ) { return 'subscribed' === ( $c['email_status'] ?? '' ) && ! empty( $c['email'] ); } /** * Change a contact's email status, keeping proof of the change on the timeline. * * Rules: bounced and spam complaints stay until an admin changes them. Someone who unsubscribed is only * subscribed again by a new, explicit opt-in (a ticked box, a signup form), never by an import or a sync. * * @param int $contact_id Contact. * @param string $status New status (see statuses()). * @param array $args { * @type string $source Where the change came from ("Contact Form 7: Contact us"). * @type string $text The wording the person agreed to (checkbox label), kept as proof. * @type string $url Page where it happened. * @type bool $explicit A fresh opt-in by the person (may lift an earlier unsubscribe). * @type bool $force An admin's manual change (may change anything). * @type string $at When it happened (GMT, for imports). Default now. * @type int $user_id Who made the change. Default the current user. * } * @return bool True when the status changed. */ public static function set( $contact_id, $status, array $args = array() ) { global $wpdb; $status = (string) $status; if ( ! array_key_exists( $status, self::statuses() ) ) { return false; } $c = Nodebook_Contacts::get( $contact_id ); if ( ! $c ) { return false; } $old = (string) ( $c['email_status'] ?? '' ); if ( $old === $status ) { return false; } $force = ! empty( $args['force'] ); $explicit = ! empty( $args['explicit'] ); if ( ! $force ) { if ( in_array( $old, self::LOCKED, true ) ) { return false; } if ( 'unsubscribed' === $old && in_array( $status, array( 'subscribed', 'pending' ), true ) && ! $explicit ) { return false; } // "Not asked" is never set by a source other than an admin: it would erase a recorded decision. if ( '' === $status ) { return false; } } $at = ! empty( $args['at'] ) && strtotime( (string) $args['at'] ) ? gmdate( 'Y-m-d H:i:s', strtotime( (string) $args['at'] . ( preg_match( '/[zZ]|[+-]\d\d:?\d\d$/', (string) $args['at'] ) ? '' : ' UTC' ) ) ) : current_time( 'mysql', true ); $source = mb_substr( sanitize_text_field( (string) ( $args['source'] ?? '' ) ), 0, 190 ); $wpdb->update( Nodebook_Contacts::table(), array( 'email_status' => $status, 'email_status_at' => $at, 'consent_source' => $source, 'updated_at' => current_time( 'mysql', true ), ), array( 'id' => (int) $c['id'] ) ); $labels = self::statuses(); Nodebook_Activities::add( $c['id'], 'subscription', /* translators: 1: new email status, 2: where it came from */ $source ? sprintf( __( 'Email: %1$s (%2$s)', 'nodebook' ), $labels[ $status ], $source ) : sprintf( /* translators: %s: new email status */ __( 'Email: %s', 'nodebook' ), $labels[ $status ] ), array( 'user_id' => isset( $args['user_id'] ) ? (int) $args['user_id'] : get_current_user_id(), 'url' => isset( $args['url'] ) ? (string) $args['url'] : '', 'created_at' => $at, 'data' => array_filter( array( 'from' => $old, 'to' => $status, 'source' => $source, 'text' => isset( $args['text'] ) ? mb_substr( sanitize_text_field( (string) $args['text'] ), 0, 300 ) : '', ), 'strlen' ), ) ); Nodebook_Contacts::flush_caches(); /** * Fires when a contact's email status changes. * * @param int $contact_id Contact. * @param string $status New status. * @param string $old Previous status. * @param array $args Source, text, url… */ do_action( 'nodebook_email_status_changed', (int) $c['id'], $status, $old, $args ); return true; } /* ------------------------------------------------------------------ * * Recognising consent in form submissions * ------------------------------------------------------------------ */ /** Words that mean "send me marketing email", in the languages Nodebook speaks. */ private static function marketing_pattern() { return '/newsletter|nieuwsbrief|bolet[ií]n|subscri|suscri|abonn|inscr[ií]b|aanmeld|mailing ?list|marketing|promo|offers|ofertas|aanbieding|angebot|\bnews\b|noticias|nieuws|neuigkeiten|novedades|actualizaciones|\bupdates\b|keep me (posted|informed|updated)|mantenerme informad|stay in touch|e-?mail me|email updates|opt.?in|quiero recibir|wil .{0,30}ontvangen|m[oö]chte .{0,30}erhalten/iu'; } /** * Is this form field a marketing consent box? Returns true (agreed), false (explicitly declined) or null (not a consent field). * * @param string $label Field label. * @param string $key Field name. * @param string $type Field type. * @param mixed $value Submitted value. */ public static function from_field( $label, $key = '', $type = '', $value = '' ) { if ( is_array( $value ) ) { $value = implode( ', ', array_map( 'strval', array_filter( $value, 'is_scalar' ) ) ); } $value = trim( (string) $value ); $type = strtolower( (string) $type ); $text = trim( $label . ' ' . str_replace( array( '_', '-' ), ' ', (string) $key ) ); if ( '' === $value || mb_strlen( $value ) > 200 ) { return null; } $choice_type = in_array( $type, array( 'checkbox', 'acceptance', 'consent', 'gdpr', 'gdpr-agreement', 'gdpr_agreement', 'terms', 'terms_and_condition', 'radio', 'select', 'toggle', 'boolean', 'yesno', 'switch', 'listcheckbox', 'listradio', 'listselect', 'single_checkbox' ), true ); $boolish = (bool) preg_match( '/^(1|0|yes|no|on|off|true|false|checked|unchecked|accepted|agree|agreed|s[ií]|ja|nee|nein|oui|non|y|n)$/i', $value ); $marketing = static function ( $s ) { return (bool) preg_match( self::marketing_pattern(), (string) $s ); }; // The label asks about marketing email and the answer is a choice… if ( $marketing( $text ) && ( $choice_type || $boolish ) ) { return ! preg_match( '/^(0|no|off|false|unchecked|nee|nein|non|n)$/i', $value ); } // …or a ticked checkbox whose own option text is the marketing promise ("Yes, send me the newsletter"). if ( $choice_type && $marketing( $value ) && false === strpos( $value, '@' ) ) { return ! preg_match( '/\b(no|not|don.?t|nee|niet|nicht|kein)\b/i', $value ); } return null; } /** A form that exists to sign people up ("Newsletter", "Subscribe"), so submitting it is the opt-in. */ public static function is_signup_form( $title ) { return (bool) preg_match( '/newsletter|nieuwsbrief|bolet[ií]n|subscribe|suscr[ií]b|abonn(ieren|ez|eer)|inscr[ií]bete|mailing ?list|aanmelden voor/iu', (string) $title ); } /* ------------------------------------------------------------------ * * Merge * ------------------------------------------------------------------ */ /** Which status the merged contact keeps. A "no" from the same person wins over a "yes"; a bounce belongs to its own address. */ public static function merged( $keep, $merge ) { $keep = (string) $keep; $merge = (string) $merge; if ( '' === $keep && 'bounced' !== $merge ) { return $merge; } if ( in_array( $keep, array( 'subscribed', 'pending' ), true ) && in_array( $merge, array( 'unsubscribed', 'complained' ), true ) ) { return $merge; } return $keep; } /** Counts per status (for the list filter and the dashboard). */ public static function counts() { global $wpdb; $rows = $wpdb->get_results( $wpdb->prepare( 'SELECT email_status s, COUNT(*) n FROM %i GROUP BY email_status', Nodebook_Contacts::table() ), ARRAY_A ); $out = array_fill_keys( array_keys( self::statuses() ), 0 ); foreach ( (array) $rows as $r ) { if ( isset( $out[ (string) $r['s'] ] ) ) { $out[ (string) $r['s'] ] = (int) $r['n']; } } return $out; } }