wp = new WPFunctions; $this->maxExcerptLength = $this->wp->applyFilters('mailpoet_newsletter_post_excerpt_length', $this->maxExcerptLength); $this->woocommerceHelper = $woocommerceHelper ?: new WooCommerceHelper($this->wp); } public function getContent($post, $displayType) { if ($displayType === 'titleOnly') { return ''; } if ($this->woocommerceHelper->isWooCommerceActive() && $this->wp->getPostType($post) === 'product') { $product = $this->woocommerceHelper->wcGetProduct($post->ID); if ($product) { return $this->getContentForProduct($product, $displayType); } } if ($displayType === 'excerpt') { if ($this->wp->hasExcerpt($post)) { return self::stripShortCodes($this->wp->getTheExcerpt($post)); } return $this->generateExcerpt($post->post_content); // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps } return self::stripShortCodes($post->post_content); // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps } public function filterContent($content, $displayType, $withPostClass = true) { $content = self::convertEmbeddedContent($content); // convert h4 h5 h6 to h3 $content = preg_replace('/<([\/])?h[456](.*?)>/', '<$1h3$2>', $content); // convert currency signs $content = str_replace( ['$', '€', '£', '¥'], ['$', '€', '£', '¥'], $content ); // strip useless tags $tagsNotBeingStripped = [ '
', '', '', '', '', '',
'', '', '
', '
', '',
];
if ($displayType === 'full') {
$tagsNotBeingStripped = array_merge($tagsNotBeingStripped, ['
', '
', '
', '
', '
']);
}
if (is_array($content)) {
$content = implode(' ', $content);
}
$content = strip_tags($content, implode('', $tagsNotBeingStripped));
if ($withPostClass) {
$dOMParser = new pQuery();
$DOM = $dOMParser->parseStr(WPFunctions::get()->wpautop($content));
$paragraphs = $DOM->query('p');
foreach ($paragraphs as $paragraph) {
// We replace the class attribute to avoid conflicts in the newsletter editor
$paragraph->removeAttr('class');
$paragraph->addClass(self::WP_POST_CLASS);
}
$content = $DOM->__toString();
} else {
$content = WPFunctions::get()->wpautop($content);
}
$content = trim($content);
return $content;
}
private function getContentForProduct($product, $displayType) {
if ($displayType === 'excerpt') {
return $product->get_short_description();
}
return $product->get_description();
}
private function generateExcerpt($content) {
// remove image captions in gutenberg
$content = preg_replace(
"/