|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Superdesk Web Publisher Content Bundle. |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright 2018 Sourcefabric z.ú. and contributors. |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please see the |
|
11
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
|
12
|
|
|
* |
|
13
|
|
|
* @copyright 2018 Sourcefabric z.ú |
|
14
|
|
|
* @license http://www.superdesk.org/license |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
namespace SWP\Bundle\ContentBundle\Processor; |
|
18
|
|
|
|
|
19
|
|
|
use SWP\Bundle\ContentBundle\File\FileExtensionCheckerInterface; |
|
20
|
|
|
use SWP\Bundle\ContentBundle\Model\ArticleInterface; |
|
21
|
|
|
use SWP\Bundle\ContentBundle\Model\ArticleMediaInterface; |
|
22
|
|
|
use Symfony\Component\DomCrawler\Crawler; |
|
23
|
|
|
|
|
24
|
|
|
final class EmbeddedMediaBlockProcessor implements ArticleBodyProcessorInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var FileExtensionCheckerInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
private $fileExtensionChecker; |
|
30
|
|
|
|
|
31
|
|
|
public function __construct(FileExtensionCheckerInterface $fileExtensionChecker) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->fileExtensionChecker = $fileExtensionChecker; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function process(ArticleInterface $article, ArticleMediaInterface $articleMedia): void |
|
37
|
|
|
{ |
|
38
|
|
|
if (ArticleInterface::KEY_FEATURE_MEDIA === $articleMedia->getKey()) { |
|
39
|
|
|
return; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$body = $article->getBody(); |
|
43
|
|
|
$mediaId = str_replace('/', '\\/', $articleMedia->getKey()); |
|
44
|
|
|
|
|
45
|
|
|
preg_match( |
|
46
|
|
|
'/(<div class="media-block">)(.+?)(<\/div>)/im', |
|
47
|
|
|
str_replace(PHP_EOL, '', $body), |
|
48
|
|
|
$embeds |
|
49
|
|
|
); |
|
50
|
|
|
|
|
51
|
|
|
if (empty($embeds)) { |
|
52
|
|
|
return; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
[0 => $mediaBlockDiv, 2 => $figureString] = $embeds; |
|
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
$crawler = new Crawler($figureString); |
|
58
|
|
|
$src = $crawler->filterXPath('//img')->attr('src'); |
|
59
|
|
|
$alt = $crawler->filterXPath('//img')->attr('alt'); |
|
60
|
|
|
$captionNode = $crawler->filterXPath('//span[@class="media-block__description"]')->first(); |
|
61
|
|
|
$caption = $captionNode->getNode(0)->textContent; |
|
62
|
|
|
|
|
63
|
|
|
$html = '<!-- EMBED START Image {id: "'.$mediaId.'"} --><figure><img src="'.$src.'" alt="'.$alt.'"/><figcaption>'.$caption.'</figcaption></figure><!-- EMBED END Image {id: "'.$mediaId.'"} -->'; |
|
64
|
|
|
|
|
65
|
|
|
$article->setBody(str_replace($mediaBlockDiv, $html, $body)); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function supports(string $type): bool |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->fileExtensionChecker->isImage($type); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.