Нема описа

PostContentManager.php 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace MailPoet\Newsletter\Editor;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoet\Util\pQuery\pQuery;
  5. use MailPoet\WooCommerce\Helper as WooCommerceHelper;
  6. use MailPoet\WP\Functions as WPFunctions;
  7. class PostContentManager {
  8. const WP_POST_CLASS = 'mailpoet_wp_post';
  9. public $maxExcerptLength = 60;
  10. /** @var WooCommerceHelper */
  11. private $woocommerceHelper;
  12. /** @var WPFunctions */
  13. private $wp;
  14. public function __construct(
  15. WooCommerceHelper $woocommerceHelper = null
  16. ) {
  17. $this->wp = new WPFunctions;
  18. $this->maxExcerptLength = $this->wp->applyFilters('mailpoet_newsletter_post_excerpt_length', $this->maxExcerptLength);
  19. $this->woocommerceHelper = $woocommerceHelper ?: new WooCommerceHelper();
  20. }
  21. public function getContent($post, $displayType) {
  22. if ($displayType === 'titleOnly') {
  23. return '';
  24. }
  25. if ($this->woocommerceHelper->isWooCommerceActive() && $this->wp->getPostType($post) === 'product') {
  26. $product = $this->woocommerceHelper->wcGetProduct($post->ID);
  27. if ($product) {
  28. return $this->getContentForProduct($product, $displayType);
  29. }
  30. }
  31. if ($displayType === 'excerpt') {
  32. if ($this->wp->hasExcerpt($post)) {
  33. return self::stripShortCodes($this->wp->getTheExcerpt($post));
  34. }
  35. return $this->generateExcerpt($this->wp->getTheContent(null, false, $post));
  36. }
  37. return self::stripShortCodes($this->wp->getTheContent(null, false, $post));
  38. }
  39. public function filterContent($content, $displayType, $withPostClass = true) {
  40. $content = self::convertEmbeddedContent($content);
  41. // convert h4 h5 h6 to h3
  42. $content = preg_replace('/<([\/])?h[456](.*?)>/', '<$1h3$2>', $content);
  43. // convert currency signs
  44. $content = str_replace(
  45. ['$', '€', '£', '¥'],
  46. ['&#36;', '&euro;', '&pound;', '&#165;'],
  47. $content
  48. );
  49. // strip useless tags
  50. $tagsNotBeingStripped = [
  51. '<p>', '<em>', '<span>', '<b>', '<strong>', '<i>',
  52. '<a>', '<ul>', '<ol>', '<li>', '<br>', '<blockquote>',
  53. ];
  54. if ($displayType === 'full') {
  55. $tagsNotBeingStripped = array_merge($tagsNotBeingStripped, ['<figure>', '<img>', '<h1>', '<h2>', '<h3>']);
  56. }
  57. if (is_array($content)) {
  58. $content = implode(' ', $content);
  59. }
  60. $content = strip_tags($content, implode('', $tagsNotBeingStripped));
  61. if ($withPostClass) {
  62. $dOMParser = new pQuery();
  63. $DOM = $dOMParser->parseStr(WPFunctions::get()->wpautop($content));
  64. $paragraphs = $DOM->query('p');
  65. foreach ($paragraphs as $paragraph) {
  66. // We replace the class attribute to avoid conflicts in the newsletter editor
  67. $paragraph->removeAttr('class');
  68. $paragraph->addClass(self::WP_POST_CLASS);
  69. }
  70. $content = $DOM->__toString();
  71. } else {
  72. $content = WPFunctions::get()->wpautop($content);
  73. }
  74. $content = trim($content);
  75. return $content;
  76. }
  77. private function getContentForProduct($product, $displayType) {
  78. if ($displayType === 'excerpt') {
  79. return $product->get_short_description();
  80. }
  81. return $product->get_description();
  82. }
  83. private function generateExcerpt($content) {
  84. // remove image captions in gutenberg
  85. $content = preg_replace(
  86. "/<figcaption.*?>.*?<\/figcaption>/",
  87. '',
  88. $content
  89. );
  90. // remove image captions in classic posts
  91. $content = preg_replace(
  92. "/\[caption.*?\](.*?)\[\/caption\]/",
  93. '',
  94. $content
  95. );
  96. $content = self::stripShortCodes($content);
  97. // if excerpt is empty then try to find the "more" tag
  98. $excerpts = explode('<!--more-->', $content);
  99. if (count($excerpts) > 1) {
  100. // <!--more--> separator was present
  101. return $excerpts[0];
  102. } else {
  103. // Separator not present, try to shorten long posts
  104. return WPFunctions::get()->wpTrimWords($content, $this->maxExcerptLength, ' &hellip;');
  105. }
  106. }
  107. private function stripShortCodes($content) {
  108. // remove captions
  109. $content = preg_replace(
  110. "/\[caption.*?\](.*<\/a>)(.*?)\[\/caption\]/",
  111. '$1',
  112. $content
  113. );
  114. // remove other shortcodes
  115. $content = preg_replace('/\[[^\[\]]*\]/', '', $content);
  116. return $content;
  117. }
  118. private function convertEmbeddedContent($content = '') {
  119. // remove embedded video and replace with links
  120. $content = preg_replace(
  121. '#<iframe.*?src=\"(.+?)\".*><\/iframe>#',
  122. '<a href="$1">' . __('Click here to view media.', 'mailpoet') . '</a>',
  123. $content
  124. );
  125. // replace youtube links
  126. $content = preg_replace(
  127. '#http://www.youtube.com/embed/([a-zA-Z0-9_-]*)#Ui',
  128. 'http://www.youtube.com/watch?v=$1',
  129. $content
  130. );
  131. return $content;
  132. }
  133. }