templates = $templates; $this->settings = $settings; } /** * Renders a template. * * @since 0.1.0 * * @param string $key Template key. * @param array $vars Merge tag values. * @param array $override Optional `subject` and `body` to use instead of the stored ones (for previews). * @return array{subject: string, html: string, text: string}|null Null for an unknown template. */ public function render( string $key, array $vars, array $override = array() ): ?array { $template = $this->templates->get( $key ); if ( null === $template ) { return null; } $subject = isset( $override['subject'] ) && '' !== $override['subject'] ? $override['subject'] : $template['subject']; $body = isset( $override['body'] ) && '' !== $override['body'] ? $override['body'] : $template['body']; $subject = trim( preg_replace( '/\s+/', ' ', wp_strip_all_tags( MergeTags::apply( $subject, $vars ) ) ) ); $text = trim( preg_replace( "/\n{3,}/", "\n\n", MergeTags::apply( $body, $vars ) ) ); $content = make_clickable( wpautop( esc_html( $text ) ) ); $html = View::render( 'emails/layout', array( 'subject' => $subject, 'content' => $content, 'business' => (string) $this->settings->get( 'business_name' ), 'logo' => (string) $this->settings->get( 'business_logo_url' ), 'footer' => (string) $this->settings->get( 'invoice_footer' ), ) ); return array( 'subject' => $subject, 'html' => $html, 'text' => $text, ); } }