|
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() || null === $articleMedia->getImage()) { |
|
39
|
|
|
return; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$body = $article->getBody(); |
|
43
|
|
|
$mediaId = str_replace('/', '\\/', $articleMedia->getKey()); |
|
44
|
|
|
$assetId = $articleMedia->getImage()->getAssetId(); |
|
45
|
|
|
$assetId = str_replace('_', '/', $assetId); |
|
46
|
|
|
|
|
47
|
|
|
$crawler = new Crawler(); |
|
48
|
|
|
$crawler->addHtmlContent($body); |
|
49
|
|
|
$item = $crawler->filterXPath('//div[@class="media-block"]/img[contains(@src, "'.$assetId.'")]'); |
|
50
|
|
|
|
|
51
|
|
|
$imgElement = $item->first()->getNode(0); |
|
52
|
|
|
if (null === $imgElement) { |
|
53
|
|
|
return; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$mediaBlockElement = $imgElement->parentNode; |
|
57
|
|
|
$captionText = $mediaBlockElement->getElementsByTagName('span')[0]->textContent; |
|
58
|
|
|
$editor3MediaBlock = $mediaBlockElement->ownerDocument->saveHTML($mediaBlockElement); |
|
59
|
|
|
$newNodeHtml = '<!-- EMBED START Image {id: "'.$mediaId.'"} --><figure><img src="'.$item->first()->attr('src').'" alt="'.$item->first()->attr('alt').'" /><figcaption>'.$captionText.'</figcaption></figure><!-- EMBED END Image {id: "'.$mediaId.'"} -->'; |
|
60
|
|
|
|
|
61
|
|
|
$article->setBody(str_replace($editor3MediaBlock, $newNodeHtml, $crawler->html())); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function supports(string $type): bool |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->fileExtensionChecker->isImage($type); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|