-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
210 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
<?php | ||
|
||
namespace Jonnitto\PrettyEmbedVideoPlatforms\FusionObjects; | ||
|
||
use Jonnitto\PrettyEmbedHelper\Service\ApiService; | ||
use Jonnitto\PrettyEmbedHelper\Service\ParseIDService; | ||
use Jonnitto\PrettyEmbedHelper\Service\YoutubeService; | ||
use Jonnitto\PrettyEmbedHelper\Utility\Utility; | ||
use Neos\Flow\Annotations as Flow; | ||
use Neos\Fusion\FusionObjects\AbstractFusionObject; | ||
use function strpos; | ||
use function preg_match_all; | ||
use function str_replace; | ||
|
||
/** | ||
* Replace embeded iframes with the video | ||
*/ | ||
class ReplaceIframesImplementation extends AbstractFusionObject | ||
{ | ||
/** | ||
* @Flow\Inject | ||
* @var YoutubeService | ||
*/ | ||
protected $youtubeService; | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getContent(): string | ||
{ | ||
return (string) $this->fusionValue('content'); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getOrigin(): string | ||
{ | ||
return (string) $this->fusionValue('origin'); | ||
} | ||
|
||
/** | ||
* Render Replace embeded iframes with the pretty embeded markup | ||
* | ||
* @return string | ||
*/ | ||
public function evaluate(): string | ||
{ | ||
$content = $this->getContent(); | ||
|
||
if (!strpos($content, '<iframe') && (!strpos($content, 'https://www.youtube.com/embed') || !strpos($content, 'https://www.youtube-nocookie.com/embed') || !strpos($content, 'https://player.vimeo.com/video'))) { | ||
return $content; | ||
} | ||
|
||
preg_match_all('/<iframe[^>]*?src=\"(https:\/\/www\.youtube(?:-nocookie)?\.com\/embed\/[^"]*)(?:(?!<\/iframe>).)+<\/iframe>/im', $content, $youtubeIframeMatcher, PREG_SET_ORDER); | ||
preg_match_all('/<iframe[^>]*?src=\"(https:\/\/player\.vimeo\.com\/video\/[^"]*)(?:(?!<\/iframe>).)+<\/iframe>/im', $content, $vimeoIframeMatcher, PREG_SET_ORDER); | ||
$parseID = new ParseIDService(); | ||
|
||
foreach ($youtubeIframeMatcher as $array) { | ||
$iframe = $array[0] ?? null; | ||
$url = $array[1] ?? null; | ||
if (!$iframe || !$url) { | ||
continue; | ||
} | ||
$videoID = $parseID->youtube($url); | ||
if (!$videoID) { | ||
continue; | ||
} | ||
$type = $this->youtubeService->type($url); | ||
$replacement = $this->buildYoutube($videoID, $type); | ||
$content = str_replace($iframe, $replacement, $content); | ||
} | ||
|
||
foreach ($vimeoIframeMatcher as $array) { | ||
$iframe = $array[0] ?? null; | ||
$url = $array[1] ?? null; | ||
if (!$iframe || !$url) { | ||
continue; | ||
} | ||
$videoID = $parseID->vimeo($url); | ||
if (!$videoID) { | ||
continue; | ||
} | ||
$replacement = $this->buildVimeo($videoID); | ||
$content = str_replace($iframe, $replacement, $content); | ||
} | ||
|
||
return $content; | ||
} | ||
|
||
protected function buildYoutube(string $videoID, string $type): string | ||
{ | ||
$data = $this->youtubeService->getBestPossibleYoutubeImage($videoID); | ||
$poster = null; | ||
if (isset($data)) { | ||
$utility = new Utility(); | ||
$poster = $utility->removeProtocolFromUrl($data['image'] ?? null); | ||
} | ||
$this->runtime->pushContextArray([ | ||
'videoID' => $videoID, | ||
'type' => $type, | ||
'poster' => $poster, | ||
'origin' => $this->getOrigin(), | ||
]); | ||
$html = $this->runtime->render($this->path . '/itemYoutubeRenderer'); | ||
$this->runtime->popContext(); | ||
return $html; | ||
} | ||
|
||
protected function buildVimeo(string $videoID): string | ||
{ | ||
$api = new ApiService(); | ||
$data = $api->vimeo($videoID); | ||
$poster = null; | ||
if (isset($data)) { | ||
$utility = new Utility(); | ||
$poster = $utility->removeProtocolFromUrl($data['thumbnail_url'] ?? null); | ||
} | ||
|
||
$this->runtime->pushContextArray([ | ||
'videoID' => $videoID, | ||
'poster' => $poster | ||
]); | ||
$html = $this->runtime->render($this->path . '/itemVimeoRenderer'); | ||
$this->runtime->popContext(); | ||
return $html; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
prototype(Jonnitto.PrettyEmbedVideoPlatforms:ReplaceIframes) { | ||
@class = 'Jonnitto\\PrettyEmbedVideoPlatforms\\FusionObjects\\ReplaceIframesImplementation' | ||
|
||
// The content to parse | ||
content = ${value} | ||
|
||
origin = Neos.Neos:NodeUri { | ||
node = ${site} | ||
absolute = true | ||
@process.onlyTheDomain = ${String.pregMatch(value, '(https?:\/\/[^\/]+)')[0]} | ||
} | ||
|
||
// This property is used internally by `ReplaceIframesImplementation` to render each YouTube iframe. | ||
// It can be modified to change behaviour for all rendered YouTube items. | ||
itemYoutubeRenderer = Jonnitto.PrettyEmbedVideoPlatforms:Component.Video { | ||
type = ${type} | ||
videoID = ${videoID} | ||
platform = 'youtube' | ||
origin = ${origin} | ||
poster = ${poster} | ||
|
||
// Set this to replace the preview image | ||
content = null | ||
} | ||
|
||
// This property is used internally by `ReplaceIframesImplementation` to render each Vimeo iframe. | ||
// It can be modified to change behaviour for all rendered Vimeo items. | ||
itemVimeoRenderer = Jonnitto.PrettyEmbedVideoPlatforms:Component.Video { | ||
videoID = ${videoID} | ||
type = 'video' | ||
platform = 'vimeo' | ||
poster = ${poster} | ||
|
||
// Set this to replace the preview image | ||
content = null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters