Skip to content

Commit

Permalink
Strip newlines in static context (some socials convert \n to <br> and…
Browse files Browse the repository at this point in the history
… displays them).
  • Loading branch information
Avatar4eg committed Aug 11, 2016
1 parent 919f5ea commit 9040c61
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
6 changes: 6 additions & 0 deletions src/Listener/AddClientAssets.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public function subscribe(Dispatcher $events)
$events->listen(ConfigureLocales::class, [$this, 'addLocales']);
}

/**
* @param ConfigureClientView $event
*/
public function addAssets(ConfigureClientView $event)
{
if($event->isForum()) {
Expand All @@ -35,6 +38,9 @@ public function addAssets(ConfigureClientView $event)
}
}

/**
* @param ConfigureLocales $event
*/
public function addLocales(ConfigureLocales $event)
{
foreach (new DirectoryIterator(__DIR__ .'/../../locale') as $file) {
Expand Down
34 changes: 29 additions & 5 deletions src/Listener/AddHeadData.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,18 @@ public function __construct(SettingsRepositoryInterface $settings, UrlGenerator
$this->urlGenerator = $urlGenerator;
}

/**
* @param Dispatcher $events
*/
public function subscribe(Dispatcher $events)
{
$events->listen(ConfigureClientView::class, [$this, 'addMetaTags']);
$events->listen(PrepareApiData::class, [$this, 'addDiscussionMetaTags']);
}


/**
* @param ConfigureClientView $event
*/
public function addMetaTags(ConfigureClientView $event)
{
if ($event->isForum()) {
Expand All @@ -63,7 +69,7 @@ public function addMetaTags(ConfigureClientView $event)
if ($this->openGraph || $this->twitterCard) {
$dataTitle = htmlspecialchars($this->settings->get('welcome_title'), ENT_QUOTES|ENT_HTML5|ENT_DISALLOWED|ENT_SUBSTITUTE, 'UTF-8');
$dataUrl = $this->urlGenerator->toBase();
$dataDescription = htmlspecialchars($this->settings->get('forum_description'), ENT_QUOTES|ENT_HTML5|ENT_DISALLOWED|ENT_SUBSTITUTE, 'UTF-8');
$dataDescription = $this->plainText($this->settings->get('forum_description'), 150);
}

if ($this->openGraph) {
Expand All @@ -82,6 +88,9 @@ public function addMetaTags(ConfigureClientView $event)
}
}

/**
* @param PrepareApiData $event
*/
public function addDiscussionMetaTags(PrepareApiData $event)
{
if ($this->clientView && $event->isController(ShowDiscussionController::class)) {
Expand All @@ -91,9 +100,7 @@ public function addDiscussionMetaTags(PrepareApiData $event)
$dataUrl = $this->urlGenerator->toRoute('discussion', ['id' => $event->data->id]);
}
if ($event->data->startPost) {
$dataDescription = strip_tags($event->data->startPost->content);
$dataDescription = strlen($dataDescription) > 150 ? substr($dataDescription, 0, 150) . '...' : $dataDescription;
$dataDescription = htmlspecialchars($dataDescription, ENT_QUOTES|ENT_HTML5|ENT_DISALLOWED|ENT_SUBSTITUTE, 'UTF-8');
$dataDescription = $this->plainText($event->data->startPost->content, 150);
}

//$this->clientView->addHeadString('<meta name="description" content="' . $dataDescription . '"/>', 'description');
Expand All @@ -109,4 +116,21 @@ public function addDiscussionMetaTags(PrepareApiData $event)
}
}
}

/**
* @param string $text
* @param int|null $length
* @return string
*/
protected function plainText($text, $length = null)
{
$text = strip_tags($text);
$text = preg_replace('/\s+/', ' ', $text);
$text = htmlspecialchars($text, ENT_QUOTES|ENT_HTML5|ENT_DISALLOWED|ENT_SUBSTITUTE, 'UTF-8');
if ($length !== null) {
$text = strlen($text) > $length ? substr($text, 0, $length) . '...' : $text;
}

return $text;
}
}

0 comments on commit 9040c61

Please sign in to comment.