Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
NotificationRenderer.php
Go to the documentation of this file.
1 <?php
8 
10 
15 {
19  private $escaper;
20 
24  public function __construct(
25  Escaper $escaper
26  ) {
27  $this->escaper = $escaper;
28  }
29 
36  public function getNotificationTitle(array $page)
37  {
38  $title = $this->escaper->escapeHtml($page['mainContent']['title']);
39  $imageUrl = $this->escaper->escapeUrl($page['mainContent']['imageUrl']);
40  $content = "";
41 
42  if (!empty($imageUrl)) {
43  $content .= "<div class='release-notification-title-with-image' style='background-image: url(\"" . $imageUrl
44  . "\")'>";
45  $content .= $title;
46  $content .= "</div>";
47  } else {
48  $content = $title;
49  }
50 
51  return $content;
52  }
53 
60  public function getNotificationContent(array $page)
61  {
62  $content = $this->buildMainContent($page['mainContent']);
63  $content .= $this->buildSubHeadings($page['subHeading']);
64  $content .= $this->buildFooter($page['footer']);
65 
66  return $content;
67  }
68 
75  private function buildMainContent(array $mainContent)
76  {
77  $content = $this->buildContentTextAreas($mainContent['content']);
78  $content .= $this->buildLists($mainContent['lists']);
79 
80  return $this->formatContentWithLinks($content);
81  }
82 
89  private function buildContentTextAreas(array $contentAreas)
90  {
91  $content = "";
92  $lastContentArea = end($contentAreas);
93 
94  foreach ($contentAreas as $contentArea) {
95  $content .= "<p>";
96  $content .= $this->escaper->escapeHtml($contentArea['text']);
97  $content .= "</p>";
98  if ($contentArea != $lastContentArea) {
99  $content .= "<br />";
100  }
101  }
102 
103  return $content;
104  }
105 
112  private function buildLists(array $lists)
113  {
114  $content = "<ul>";
115 
116  foreach ($lists as $listItem) {
117  $content .= "<li><span>";
118  $content .= $this->escaper->escapeHtml($listItem['text']);
119  $content .= "</span></li>";
120  }
121 
122  $content .= "</ul>";
123 
124  return $content;
125  }
126 
133  private function buildSubHeadings(array $subHeadings)
134  {
135  $content = "";
136 
137  foreach ($subHeadings as $subHeading) {
138  if (!empty($subHeading['imageUrl'])) {
139  $content .= "<div class='highlight-item' style='background-image: url(\""
140  . $this->escaper->escapeUrl($subHeading['imageUrl']) . "\")'>";
141  } else {
142  $content .= "<div class='highlight-item-no-image'>";
143  }
144 
145  $content .= "<h3>";
146  $content .= $this->escaper->escapeHtml($subHeading['title']);
147  $content .= "</h3>";
148  $content .= "<p>";
149  $content .= $this->formatContentWithLinks($subHeading['content']);
150  $content .= "</p>";
151  $content .= "</div>";
152  }
153 
154  return $content;
155  }
156 
163  private function buildFooter(array $footer)
164  {
165  $content = "<p>";
166  $content .= $this->escaper->escapeHtml($footer['content']);
167  $content .= "</p>";
168 
169  return $this->formatContentWithLinks($content);
170  }
171 
179  private function formatContentWithLinks($content)
180  {
181  $urlRegex = '#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#';
182  $urlTextRegex = '/\[(.*?)\]/';
183 
184  preg_match_all($urlRegex, $content, $urlMatches);
185  preg_match_all($urlTextRegex, $content, $urlTextMatches);
186 
187  foreach ($urlMatches[0] as $key => $urlMatch) {
188  if (!empty($urlTextMatches[0])) {
189  $linkMatch = $urlMatch . " " . $urlTextMatches[0][$key];
190  $content = str_replace(
191  $linkMatch,
192  "<a target='_blank' href='{$this->escaper->escapeUrl($urlMatch)}'>
193  {$this->escaper->escapeHtml($urlTextMatches[1][$key])}</a>",
194  $content
195  );
196  } else {
197  $content = str_replace(
198  $urlMatch,
199  "<a target='_blank' href='{$this->escaper->escapeUrl($urlMatch)}'>
200  {$this->escaper->escapeUrl($urlMatch)}</a>",
201  $content
202  );
203  }
204  }
205 
206  return $content;
207  }
208 }
$title
Definition: default.phtml:14
$page
Definition: pages.php:8
if($_imageTitle=$block->escapeHtml($block->getCurrentImage() ->getLabel())) $imageUrl
Definition: gallery.phtml:28