-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingle-newsletter.php
More file actions
68 lines (58 loc) · 2.4 KB
/
single-newsletter.php
File metadata and controls
68 lines (58 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
/**
* The template for displaying newsletter posts
*
* @link https://developer.wordpress.org/themes/basics/template-hierarchy/#single-post
*
* @package MinnPost Largo
*/
// use Pelago\Emogrifier\CssInliner;
use Pelago\Emogrifier\CssInliner;
use Pelago\Emogrifier\HtmlProcessor\CssToAttributeConverter;
use Pelago\Emogrifier\HtmlProcessor\HtmlPruner;
$newsletter_type = get_post_meta( get_the_ID(), '_mp_newsletter_type', true );
$is_legacy = apply_filters( 'minnpost_largo_newsletter_legacy', false, '', get_the_ID() );
$args = array(
'newsletter_type' => $newsletter_type,
'is_legacy' => $is_legacy,
);
ob_start();
if ( false === $is_legacy ) {
$css = wp_strip_all_tags( file_get_contents( get_stylesheet_directory() . '/email.css' ) );
$css = str_replace( 'margin', 'Margin', $css );
$css = str_replace( '*,:after,:before{box-sizing:inherit}', '', $css );
}
get_header( 'newsletter', $args );
while ( have_posts() ) :
the_post();
if ( false === $is_legacy ) {
get_template_part( 'template-parts/content-newsletter', $newsletter_type, $args );
} else {
get_template_part( 'template-parts/content-newsletter-legacy', $newsletter_type, $args );
}
endwhile; // End of the loop.
get_footer( 'newsletter', $args );
$html = ob_get_contents();
ob_end_clean();
if ( false === $is_legacy ) {
// get the HTML and inline the CSS.
$css_inliner = CssInliner::fromHtml( $html )->inlineCss( $css );
// make a DOMDocument out of it.
$dom_document = $css_inliner->getDomDocument();
// remove stuff from the HTML.
HtmlPruner::fromDomDocument( $dom_document )->removeElementsWithDisplayNone()->removeRedundantClassesAfterCssInlined( $css_inliner );
// convert some CSS to HTML attributes for older email clients.
$html = CssToAttributeConverter::fromDomDocument( $dom_document )->render();
// apply filter that turns the shortcodes into HTML after the CSS has been messed with.
$html = apply_filters( 'do_shortcodes_after_emogrifier', $html );
// remove newlines.
$html = preg_replace( "/\n/", '', $html );
// remove spaces between HTML tags. this causes issues for Outlook on windows, apparently.
// $html = preg_replace( '/>\s*</', '><', $html );
// wrap the HTML at 500 characters for email clients.
$html = wordwrap( $html, 500 );
} else {
// apply filter that turns the shortcodes into HTML after the CSS has been messed with.
$html = apply_filters( 'do_shortcodes_after_emogrifier', $html );
}
echo $html;