config = $config; } public static function defaults(): array { return [ /* translators: {door} is replaced with the door number. Keep the braces. */ 'subject' => __( 'Your advent calendar gift – door {door}', 'advently' ), 'heading' => __( 'Door {door} is open!', 'advently' ), 'body' => __( "Here is your gift from today's door: {discount}.\n\nYour personal code: {coupon}\nValid until: {expiry}\n\nEnter it at checkout, or open the calendar again and press \"Apply to cart\": {calendar_url}\n\nSee you behind tomorrow's door!", 'advently' ), ]; } public function sendCoupon( string $email, array $coupon, int $doorNo ): void { $door = $this->config['door_list'][ $doorNo ]; /** * Filters the coupon email's subject, heading and body (plain text with tags). * * @param array{subject:string, heading:string, body:string} $texts Texts. * @param array $config Calendar config. */ $texts = array_merge( self::defaults(), (array) apply_filters( 'advently_coupon_email_texts', self::defaults(), $this->config ) ); $expiry = Label::expiry( $coupon['expires'] ); $plain = [ '{coupon}' => $coupon['code'], '{expiry}' => '' !== $expiry ? $expiry : __( 'the end of the calendar', 'advently' ), '{door}' => (string) $doorNo, '{discount}' => Label::discount( $door['coupon'] ), '{shop_url}' => wc_get_page_permalink( 'shop' ), '{calendar_url}' => Page::url( $this->config ) ? Page::url( $this->config ) : home_url( '/' ), ]; // The body is escaped first, then tags are swapped for escaped values; the // code itself gets its own visual block. $html = $plain; foreach ( $html as $tag => $value ) { $html[ $tag ] = esc_html( $value ); } $html['{coupon}'] = '' . esc_html( $coupon['code'] ) . ''; $html['{shop_url}'] = '' . esc_html( $plain['{shop_url}'] ) . ''; $html['{calendar_url}'] = '' . esc_html( $plain['{calendar_url}'] ) . ''; $subject = strtr( $texts['subject'], $plain ); $heading = strtr( $texts['heading'], $plain ); $body = strtr( wpautop( esc_html( $texts['body'] ) ), $html ); $mailer = WC()->mailer(); $message = $mailer->wrap_message( $heading, $body ); if ( ! $mailer->send( $email, wp_strip_all_tags( $subject ), $message, "Content-Type: text/html\r\n" ) ) { throw new \RuntimeException( 'Coupon email could not be sent.' ); } } }