Merge pull request #1413 from mailpoet/full-posts-fix

Fix rendering of posts in newsletter [MAILPOET-1416]
This commit is contained in:
Michelle Shull
2018-06-25 09:06:26 -04:00
committed by GitHub
3 changed files with 79 additions and 37 deletions

View File

@ -30,7 +30,7 @@ class PostContentManager {
} }
} }
function filterContent($content, $with_post_class = true) { function filterContent($content, $display_type, $with_post_class = true) {
$content = self::convertEmbeddedContent($content); $content = self::convertEmbeddedContent($content);
// convert h4 h5 h6 to h3 // convert h4 h5 h6 to h3
@ -48,6 +48,10 @@ class PostContentManager {
'<p>', '<em>', '<span>', '<b>', '<strong>', '<i>', '<p>', '<em>', '<span>', '<b>', '<strong>', '<i>',
'<a>', '<ul>', '<ol>', '<li>', '<br>', '<blockquote>' '<a>', '<ul>', '<ol>', '<li>', '<br>', '<blockquote>'
); );
if($display_type === 'full') {
$tags_not_being_stripped = array_merge($tags_not_being_stripped, array('<img>', '<h1>', '<h2>', '<h3>'));
}
$content = strip_tags($content, implode('', $tags_not_being_stripped)); $content = strip_tags($content, implode('', $tags_not_being_stripped));
if($with_post_class) { if($with_post_class) {
$content = str_replace('<p', '<p class="' . self::WP_POST_CLASS .'"', wpautop($content)); $content = str_replace('<p', '<p class="' . self::WP_POST_CLASS .'"', wpautop($content));

View File

@ -38,36 +38,37 @@ class PostTransformer {
} }
private function getStructure($post) { private function getStructure($post) {
$content = $this->getContent($post, true); $content = $this->getContent($post, true, $this->args['displayType']);
$title = $this->getTitle($post); $title = $this->getTitle($post);
$featured_image = $this->getFeaturedImage($post); $featured_image = $this->getFeaturedImage($post);
$featured_image_position = $this->args['featuredImagePosition'];
$image_position = $this->args['featuredImagePosition']; if($featured_image && $featured_image_position === 'belowTitle' && $this->args['displayType'] === 'excerpt') {
if($featured_image && $image_position === 'belowTitle' && $this->args['displayType'] === 'excerpt') {
array_unshift($content, $title, $featured_image); array_unshift($content, $title, $featured_image);
return $content;
}
if($content[0]['type'] === 'text') {
$content[0]['text'] = $title['text'] . $content[0]['text'];
} else { } else {
if($content[0]['type'] === 'text') { array_unshift($content, $title);
$content[0]['text'] = $title['text'] . $content[0]['text']; }
} else {
array_unshift($content, $title); if($featured_image && $this->args['displayType'] === 'excerpt') {
} array_unshift($content, $featured_image);
if($featured_image && $this->args['displayType'] === 'excerpt') {
array_unshift($content, $featured_image);
}
} }
return $content; return $content;
} }
private function getStructureWithLayout($post) { private function getStructureWithLayout($post) {
$content = $this->getContent($post, false); $content = $this->getContent($post, false, $this->args['displayType']);
$title = $this->getTitle($post); $title = $this->getTitle($post);
$featured_image = $this->getFeaturedImage($post); $featured_image = $this->getFeaturedImage($post);
$position = $this->args['featuredImagePosition']; $featured_image_position = $this->args['featuredImagePosition'];
if(!$featured_image || $position === 'none' || $this->args['displayType'] !== 'excerpt') { if(!$featured_image || $featured_image_position === 'none' || $this->args['displayType'] !== 'excerpt') {
array_unshift($content, $title); array_unshift($content, $title);
return array( return array(
@ -77,11 +78,11 @@ class PostTransformer {
); );
} }
if($position === 'aboveTitle' || $position === 'belowTitle') { if($featured_image_position === 'aboveTitle' || $featured_image_position === 'belowTitle') {
$position = 'centered'; $featured_image_position = 'centered';
} }
if($position === 'centered') { if($featured_image_position === 'centered') {
array_unshift($content, $title, $featured_image); array_unshift($content, $title, $featured_image);
return array( return array(
LayoutHelper::row(array( LayoutHelper::row(array(
@ -90,11 +91,11 @@ class PostTransformer {
); );
} }
if($position === 'alternate') { if($featured_image_position === 'alternate') {
$position = $this->nextImagePosition(); $featured_image_position = $this->nextImagePosition();
} }
$content = ($position === 'left') $content = ($featured_image_position === 'left')
? array( ? array(
LayoutHelper::col(array($featured_image)), LayoutHelper::col(array($featured_image)),
LayoutHelper::col($content) LayoutHelper::col($content)
@ -117,13 +118,13 @@ class PostTransformer {
return $this->image_position; return $this->image_position;
} }
private function getContent($post, $with_post_class) { private function getContent($post, $with_post_class, $display_type) {
$content_manager = new PostContentManager(); $content_manager = new PostContentManager();
$meta_manager = new MetaInformationManager(); $meta_manager = new MetaInformationManager();
$content = $content_manager->getContent($post, $this->args['displayType']); $content = $content_manager->getContent($post, $this->args['displayType']);
$content = $meta_manager->appendMetaInformation($content, $post, $this->args); $content = $meta_manager->appendMetaInformation($content, $post, $this->args);
$content = $content_manager->filterContent($content, $with_post_class); $content = $content_manager->filterContent($content, $display_type, $with_post_class);
$structure_transformer = new StructureTransformer(); $structure_transformer = new StructureTransformer();
$content = $structure_transformer->transform($content, $this->args['imageFullWidth'] === true); $content = $structure_transformer->transform($content, $this->args['imageFullWidth'] === true);

View File

@ -13,42 +13,51 @@ class PostContentManagerTest extends \MailPoetTest {
function testFilterContentRetainsStructuralTags() { function testFilterContentRetainsStructuralTags() {
$html = '<p>some paragraph text</p>'; $html = '<p>some paragraph text</p>';
expect($this->post_content->filterContent($html))->equals( expect($this->post_content->filterContent($html, 'full'))->equals(
'<p class="' . PostContentManager::WP_POST_CLASS . '">some paragraph text</p>' '<p class="' . PostContentManager::WP_POST_CLASS . '">some paragraph text</p>'
); );
$html = '<span>spanning</span>'; $html = '<span>spanning</span>';
expect($this->post_content->filterContent($html))->equals( expect($this->post_content->filterContent($html, 'full'))->equals(
'<p class="' . PostContentManager::WP_POST_CLASS . '">' . $html . '</p>' '<p class="' . PostContentManager::WP_POST_CLASS . '">' . $html . '</p>'
); );
$html = '<blockquote>do not strip this</blockquote>'; $html = '<blockquote>do not strip this</blockquote>';
expect($this->post_content->filterContent($html))->equals( expect($this->post_content->filterContent($html, 'full'))->equals(
'<blockquote><p class="' . PostContentManager::WP_POST_CLASS . '">do not strip this</p></blockquote>' '<blockquote><p class="' . PostContentManager::WP_POST_CLASS . '">do not strip this</p></blockquote>'
); );
$html = '<ul><li>First item</li><li>Second item</li></ul>'; $html = '<ul><li>First item</li><li>Second item</li></ul>';
expect($this->post_content->filterContent($html))->equals( expect($this->post_content->filterContent($html, 'full'))->equals(
"<ul>\n<li>First item</li>\n<li>Second item</li>\n</ul>" "<ul>\n<li>First item</li>\n<li>Second item</li>\n</ul>"
); );
$html = '<ol><li>First item</li><li>Second item</li></ol>'; $html = '<ol><li>First item</li><li>Second item</li></ol>';
expect($this->post_content->filterContent($html))->equals( expect($this->post_content->filterContent($html, 'full'))->equals(
"<ol>\n<li>First item</li>\n<li>Second item</li>\n</ol>" "<ol>\n<li>First item</li>\n<li>Second item</li>\n</ol>"
); );
} }
function testFilterContentRetainsHeadings() { function testFilterContentRetainsHeadings() {
$html = '<h1>heading 1</h1>'; $html = '<h1>heading 1</h1>';
expect($this->post_content->filterContent($html)) expect($this->post_content->filterContent($html, 'full'))->equals($html);
$html = '<h2>heading 2</h2>';
expect($this->post_content->filterContent($html, 'full'))->equals($html);
$html = '<h3>heading 3</h3>';
expect($this->post_content->filterContent($html, 'full'))->equals($html);
$html = '<h1>heading 1</h1>';
expect($this->post_content->filterContent($html, 'excerpt'))
->equals('<p class="' . PostContentManager::WP_POST_CLASS . '">heading 1</p>'); ->equals('<p class="' . PostContentManager::WP_POST_CLASS . '">heading 1</p>');
$html = '<h2>heading 2</h2>'; $html = '<h2>heading 2</h2>';
expect($this->post_content->filterContent($html)) expect($this->post_content->filterContent($html, 'excerpt'))
->equals('<p class="' . PostContentManager::WP_POST_CLASS . '">heading 2</p>'); ->equals('<p class="' . PostContentManager::WP_POST_CLASS . '">heading 2</p>');
$html = '<h3>heading 3</h3>'; $html = '<h3>heading 3</h3>';
expect($this->post_content->filterContent($html)) expect($this->post_content->filterContent($html, 'excerpt'))
->equals('<p class="' . PostContentManager::WP_POST_CLASS . '">heading 3</p>'); ->equals('<p class="' . PostContentManager::WP_POST_CLASS . '">heading 3</p>');
} }
@ -61,15 +70,20 @@ class PostContentManagerTest extends \MailPoetTest {
'Text<br />new line' 'Text<br />new line'
); );
foreach($text_tags as $html) { foreach($text_tags as $html) {
expect($this->post_content->filterContent($html))->equals( expect($this->post_content->filterContent($html, 'full'))->equals(
'<p class="' . PostContentManager::WP_POST_CLASS . '">' . $html . '</p>' '<p class="' . PostContentManager::WP_POST_CLASS . '">' . $html . '</p>'
); );
} }
} }
function testFilterContentRetainsLinks() { function testFilterContentRetainsImagesAndLinks() {
$html = '<img src="#" alt="some alt" />';
expect($this->post_content->filterContent($html, 'full'))->equals(
'<p class="' . PostContentManager::WP_POST_CLASS . '"><img src="#" alt="some alt" /></p>'
);
$html = '<a href="#" title="link title">some link</a>'; $html = '<a href="#" title="link title">some link</a>';
expect($this->post_content->filterContent($html))->equals( expect($this->post_content->filterContent($html, 'full'))->equals(
'<p class="' . PostContentManager::WP_POST_CLASS . '">' . $html . '</p>' '<p class="' . PostContentManager::WP_POST_CLASS . '">' . $html . '</p>'
); );
} }
@ -86,14 +100,37 @@ class PostContentManagerTest extends \MailPoetTest {
'<table></table>', '<table></table>',
'<tr></tr>', '<tr></tr>',
'<td></td>', '<td></td>',
'<img src="#" alt="some alt" />',
); );
foreach($undesirable_tags as $html) { foreach($undesirable_tags as $html) {
expect($this->post_content->filterContent($html))->equals(''); expect($this->post_content->filterContent($html, 'full'))->equals('');
} }
} }
function testFilterContentStripsUndesirableTagsForExcerpts() {
$undesirable_tags = array(
'<embed src="#" />',
'<iframe src="#" />',
'<form></form>',
'<input type="text" />',
'<hr />',
'<script></script>',
'<style></style>',
'<table></table>',
'<tr></tr>',
'<td></td>',
'<img src="#" alt="some alt" />',
'<h1></h1>',
'<h2></h2>',
'<h3></h3>',
);
foreach($undesirable_tags as $html) {
expect($this->post_content->filterContent($html, 'excerpt'))->equals('');
}
}
function testItAppliesCustomMaxExcerptLenghViaHook() { function testItAppliesCustomMaxExcerptLenghViaHook() {
$post_content_manager = new PostContentManager(); $post_content_manager = new PostContentManager();
$post = (object)array( $post = (object)array(