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);
// convert h4 h5 h6 to h3
@ -48,6 +48,10 @@ class PostContentManager {
'<p>', '<em>', '<span>', '<b>', '<strong>', '<i>',
'<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));
if($with_post_class) {
$content = str_replace('<p', '<p class="' . self::WP_POST_CLASS .'"', wpautop($content));

View File

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

View File

@ -13,42 +13,51 @@ class PostContentManagerTest extends \MailPoetTest {
function testFilterContentRetainsStructuralTags() {
$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>'
);
$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>'
);
$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>'
);
$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>"
);
$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>"
);
}
function testFilterContentRetainsHeadings() {
$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>');
$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>');
$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>');
}
@ -61,15 +70,20 @@ class PostContentManagerTest extends \MailPoetTest {
'Text<br />new line'
);
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>'
);
}
}
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>';
expect($this->post_content->filterContent($html))->equals(
expect($this->post_content->filterContent($html, 'full'))->equals(
'<p class="' . PostContentManager::WP_POST_CLASS . '">' . $html . '</p>'
);
}
@ -86,14 +100,37 @@ class PostContentManagerTest extends \MailPoetTest {
'<table></table>',
'<tr></tr>',
'<td></td>',
'<img src="#" alt="some alt" />',
);
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() {
$post_content_manager = new PostContentManager();
$post = (object)array(