$atts Argumente von wp_mail(). * @return array */ public static function redirect( $atts ) { if ( ! is_array( $atts ) || 'redirect' !== self::effective_mode() ) { return $atts; } $target = Wp360_Staging_Guard_Settings::get()['mail_redirect_to']; $parsed = Wp360_Staging_Guard_Mail_Normalizer::parse_headers( $atts['headers'] ?? '' ); $original = array(); foreach ( Wp360_Staging_Guard_Mail_Normalizer::parse_addresses( $atts['to'] ?? '' ) as $address ) { $original[] = $address['email']; } foreach ( array_merge( $parsed['cc'], $parsed['bcc'] ) as $address ) { $original[] = $address['email']; } $original = array_values( array_unique( $original ) ); // Alle Kopfzeilen bleiben unveraendert (auch Folgezeilen, etwa eine // Content-Type-Zeile mit boundary), nur Cc und Bcc entfallen. $atts['to'] = $target; $atts['headers'] = self::without_recipient_headers( $atts['headers'] ?? '' ); $atts['subject'] = self::subject_with_original( (string) ( $atts['subject'] ?? '' ), $original ); return $atts; } /** * Die Kopfzeilen ohne Cc und Bcc (samt umgebrochener Folgezeilen). * * @param mixed $headers Text oder Array. * @return string[] */ public static function without_recipient_headers( $headers ) { $lines = array(); foreach ( is_array( $headers ) ? $headers : array( $headers ) as $chunk ) { if ( is_string( $chunk ) ) { $lines = array_merge( $lines, preg_split( '/\r\n|\r|\n/', $chunk ) ); } } $kept = array(); $skipping = false; foreach ( $lines as $line ) { if ( preg_match( '/^(?:cc|bcc)\s*:/i', $line ) ) { $skipping = true; continue; } if ( $skipping && preg_match( '/^[ \t]/', $line ) ) { continue; // Folgezeile einer Cc-/Bcc-Kopfzeile. } $skipping = false; if ( '' !== trim( $line ) ) { $kept[] = $line; } } return $kept; } /** * Kurzschluss: Modus "auffangen" und "verwerfen" versenden nichts. * * @param null|bool $pre Vorab-Ergebnis eines anderen Plugins. * @param array $atts Argumente von wp_mail(). * @return null|bool */ public static function intercept( $pre, $atts ) { if ( null !== $pre ) { return $pre; } switch ( self::effective_mode() ) { case 'catch': Wp360_Staging_Guard_Mail_Store::insert( Wp360_Staging_Guard_Mail_Normalizer::normalize( is_array( $atts ) ? $atts : array() ) ); return true; case 'discard': return true; default: // redirect (laeuft nach der Umschreibung normal weiter), allow, inaktiv. return null; } } /** * Betreff mit den urspruenglichen Empfaengern, auf eine uebliche Laenge begrenzt. * * @param string $subject Betreff. * @param string[] $original Urspruengliche Adressen. * @return string */ public static function subject_with_original( $subject, array $original ) { if ( ! $original ) { return $subject; } $shown = array_slice( $original, 0, 3 ); $more = count( $original ) - count( $shown ); $label = implode( ', ', $shown ) . ( $more > 0 ? ' +' . $more : '' ); return '[' . $label . '] ' . $subject; } }