false, 'retry' => false, 'code' => 'invalid_email', 'message' => '', 'created' => false, ); } $r = PostaraMail_API::contacts_sync( array(), array( $email ) ); return array_merge( $r, array( 'created' => false ) ); } $email = isset( $item['email'] ) ? sanitize_email( $item['email'] ) : ''; if ( ! is_email( $email ) ) { return array( 'ok' => false, 'retry' => false, 'code' => 'invalid_email', 'message' => __( 'Please enter a valid email address.', 'postaramail' ), 'created' => false, ); } $start = (bool) get_option( 'postaramail_start_automations', 1 ); $r = PostaraMail_API::add_contact( $email, isset( $item['first'] ) ? $item['first'] : '', isset( $item['last'] ) ? $item['last'] : '', $start, ( isset( $item['source'] ) && 'woocommerce' === $item['source'] ) ? 'woocommerce' : 'WordPress', isset( $item['consent'] ) ? substr( (string) $item['consent'], 0, 380 ) : '', isset( $item['method'] ) ? $item['method'] : 'optin', isset( $item['country'] ) ? $item['country'] : '', isset( $item['ip'] ) ? $item['ip'] : '', ! empty( $item['confirmed'] ) ); if ( ! $r['ok'] ) { return array_merge( $r, array( 'created' => false ) ); } $created = ! empty( $r['data']['created'] ); // A specific automation (chosen in the form or in Settings). $automation = isset( $item['automation'] ) ? (int) $item['automation'] : 0; if ( $automation > 0 ) { $e = PostaraMail_API::enroll( $automation, $email ); // "Not active yet" is a setup issue: tell the admin, but the contact itself was added. if ( ! $e['ok'] && ! $e['retry'] ) { self::remember_error( $e['message'] ); } elseif ( ! $e['ok'] ) { return array_merge( $e, array( 'created' => $created ) ); } } if ( $created ) { delete_transient( 'postaramail_me' ); // Usage on the dashboard changes. } self::count_success( $created ); return array( 'ok' => true, 'retry' => false, 'code' => '', 'message' => '', 'created' => $created, ); } /** * Public entry point. Sends now; if PostaraMail is temporarily * unreachable, queues it for retry and still reports success to the visitor. */ public static function subscribe( $item ) { $r = self::send( $item ); if ( $r['ok'] ) { return $r; } if ( $r['retry'] ) { PostaraMail_Queue::push( $item ); self::remember_error( $r['message'] ? $r['message'] : $r['code'] ); return array_merge( $r, array( 'ok' => true, 'queued' => true, ) ); } if ( ! in_array( $r['code'], array( 'invalid_email', 'consent_required', 'contact_not_subscribed' ), true ) ) { self::remember_error( $r['message'] ? $r['message'] : $r['code'] ); // Setup problem the owner should see. } return $r; } /** Records that someone said no (or queues it for retry). They become "Declined" in PostaraMail. */ public static function decline( $email ) { $item = array( 'kind' => 'decline', 'email' => $email, ); $r = self::send( $item ); if ( ! $r['ok'] && $r['retry'] ) { PostaraMail_Queue::push( $item ); } return $r; } /** Friendly text for the visitor. Setup problems are never shown to them. */ public static function visitor_message( $r, $success_text ) { if ( ! empty( $r['ok'] ) ) { return $success_text; } switch ( isset( $r['code'] ) ? $r['code'] : '' ) { case 'invalid_email': return __( 'Please enter a valid email address.', 'postaramail' ); case 'contact_not_subscribed': return __( 'This email address has unsubscribed, so we can’t add it again.', 'postaramail' ); default: return __( 'Sorry, we couldn’t sign you up just now. Please try again in a little while.', 'postaramail' ); } } public static function remember_error( $message ) { update_option( 'postaramail_last_error', array( 'message' => substr( wp_strip_all_tags( (string) $message ), 0, 300 ), 'time' => time(), ), false ); } private static function count_success( $created ) { $s = get_option( 'postaramail_stats', array( 'added' => 0, 'last' => 0, ) ); if ( $created ) { $s['added'] = (int) $s['added'] + 1; } $s['last'] = time(); update_option( 'postaramail_stats', $s, false ); } /** Splits "Sara Khan" into first and last name. */ public static function split_name( $name ) { $name = trim( preg_replace( '/\s+/', ' ', (string) $name ) ); $parts = explode( ' ', $name, 2 ); return array( sanitize_text_field( $parts[0] ), isset( $parts[1] ) ? sanitize_text_field( $parts[1] ) : '' ); } }