Skip to content

Commit cfce47d

Browse files
2 parents cd520d2 + d07f0c3 commit cfce47d

26 files changed

+849
-61
lines changed

Block/VideoSettings.php

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
namespace Cloudinary\Cloudinary\Block;
3+
use Cloudinary\Cloudinary\Core\ConfigurationInterface;
4+
use Magento\Framework\View\Element\Template;
5+
6+
class VideoSettings extends template
7+
{
8+
/**
9+
* @var ConfigurationInterface
10+
*/
11+
protected $configuration;
12+
13+
/**
14+
* @param ConfigurationInterface $configuration
15+
* @param Template\Context $context
16+
* @param array $data
17+
*/
18+
public function __construct(
19+
ConfigurationInterface $configuration,
20+
Template\Context $context, array $data = []
21+
)
22+
{
23+
$this->configuration = $configuration;
24+
parent::__construct($context, $data);
25+
}
26+
27+
/**
28+
* @return array
29+
*/
30+
public function getVideoSettings() {
31+
$settings = [
32+
'player_type' => 'default'
33+
];
34+
$allSettings = $this->configuration->getAllVideoSettings();
35+
$videoFreeParamsArray = [];
36+
$sourceTypes = null;
37+
$videoPlayerEnabled = isset($allSettings['enabled']) && (bool)$allSettings['enabled'];
38+
$videoFreeParams = isset($allSettings['video_free_params']) ? $allSettings['video_free_params'] : null;
39+
if (!empty($videoFreeParams) && $videoFreeParams != "{}") {
40+
$videoFreeParamsArray = json_decode($videoFreeParams, true);
41+
$videoFreeParamsArray['player']['cloudName'] = $this->configuration->getCloud();
42+
$settings['settings'] = $videoFreeParamsArray['player'];
43+
$source = isset($videoFreeParamsArray['source']) ? $videoFreeParamsArray['source'] : null;
44+
$settings['source'] = $source ?? [];
45+
}
46+
47+
if (empty($videoFreeParamsArray)) {
48+
$videoFreeParams = false;
49+
}
50+
51+
// additional params
52+
if ($this->configuration->isEnabled() && $videoPlayerEnabled) {
53+
$settings['player_type'] = 'cloudinary';
54+
if (!$videoFreeParams) {
55+
56+
$transformation = [];
57+
58+
$autoplay = isset($allSettings['autoplay']) ? $allSettings['autoplay'] : 'never';
59+
$controls = isset($allSettings['controls']) ? $allSettings['controls'] : null;
60+
$isLoop = isset ($allSettings['loop']) ? (bool) $allSettings['loop'] : false;
61+
$playerSettings = [
62+
"cloudName" => $this->configuration->getCloud(),
63+
'controls' => ($controls == 'all'),
64+
'autoplay' => ($autoplay != 'never'),
65+
'loop' => $isLoop,
66+
'chapters' => false
67+
];
68+
69+
$playerSettings['muted'] = false;
70+
71+
if ($autoplay) {
72+
$playerSettings['autoplayMode'] = $autoplay;
73+
if ($autoplay != 'never') {
74+
$playerSettings['muted'] = true;
75+
}
76+
}
77+
78+
$streamMode = isset($allSettings['stream_mode']) ? $allSettings['stream_mode'] : null;
79+
80+
if ($streamMode == 'optimization') {
81+
$streamModeFormat = isset($allSettings['stream_mode_format']) ? $allSettings['stream_mode_format'] : null;
82+
$streamModeQuality = isset($allSettings['stream_mode_quality']) ? $allSettings['stream_mode_quality'] : null;
83+
$progressiveSourceTypes = isset($allSettings['progressive_sourcetypes']) ? $allSettings['progressive_sourcetypes'] : null;
84+
85+
if ($streamModeFormat == 'none' && $progressiveSourceTypes){
86+
$sourceTypes = explode(',',$progressiveSourceTypes);
87+
}
88+
if ($streamModeQuality){
89+
$transformation[]= $streamModeQuality;
90+
}
91+
}
92+
if ($streamMode == 'abr') {
93+
$sourceTypes = isset($allSettings['source_types']) ? [$allSettings['source_types']] : null;
94+
}
95+
96+
$settings = [
97+
'player_type' => ($this->configuration->isEnabledProductGallery()) ? 'default' : 'cloudinary',
98+
'settings' => $playerSettings
99+
];
100+
101+
if ($transformation && is_array($transformation)) {
102+
$settings['transformation'] = implode(',',$transformation);
103+
}
104+
105+
if ($sourceTypes) {
106+
$settings['source'] = ['sourceTypes' => $sourceTypes];
107+
}
108+
}
109+
}
110+
111+
return $settings;
112+
}
113+
}

Controller/Adminhtml/Ajax/UpdateAdminImage.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ private function authorise()
9999
public function execute()
100100
{
101101
$this->authorise();
102+
$result = [];
102103
if ($this->configuration->isEnabled()) {
103104
try{
104105
$remoteImageUrl = $this->getRequest()->getParam('remote_image');
@@ -113,9 +114,6 @@ public function execute()
113114
]
114115
) . '?_i=AB';
115116

116-
117-
118-
119117
} catch (\Exception $e) {
120118
$result = ['error' => $e->getMessage(), 'errorcode' => $e->getCode()];
121119
}

Helper/ProductGalleryHelper.php

+63
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,69 @@ public function getCloudinaryPGOptions($refresh = false, $ignoreDisabled = false
6666
{
6767
if ((is_null($this->cloudinaryPGoptions) || $refresh) && ($ignoreDisabled || ($this->configuration->isEnabled() && $this->configuration->isEnabledProductGallery()))) {
6868
$this->cloudinaryPGoptions = $this->configuration->getProductGalleryAll();
69+
70+
71+
if ($this->configuration->isEnabledCldVideo()){
72+
73+
$transformation = [];
74+
$videoSettings = $this->configuration->getAllVideoSettings();
75+
$videoFreeParams = $videoSettings['video_free_params'] ?? null;
76+
if ($videoFreeParams) {
77+
$config = json_decode($videoFreeParams, true);
78+
$config = array_shift($config);
79+
unset($config['cloudName']);
80+
}
81+
82+
83+
$config['playerType'] = 'cloudinary';
84+
85+
86+
if (!$videoFreeParams || $videoFreeParams == "{}") {
87+
$config = [
88+
'playerType' => 'cloudinary',
89+
'controls' => $videoSettings['controls'],
90+
'chapters' => false,
91+
'muted' => false
92+
93+
];
94+
$autoplayMode = $videoSettings['autoplay'] ?? null;
95+
$config['autoplayMode'] = $videoSettings['autoplay'];
96+
if ($autoplayMode && $autoplayMode != 'never') {
97+
$config['autoplay'] = true;
98+
99+
$config['muted'] = true;
100+
101+
} else {
102+
$config['autoplay'] = false;
103+
}
104+
105+
$streamMode = $videoSettings['stream_mode'] ?? null;
106+
107+
if ($streamMode == 'optimization') {
108+
$streamModeFormat = $videoSettings['stream_mode_format'] ?? null;
109+
$streamModeQuality = $videoSettings['stream_mode_quality'] ?? null;
110+
if ($streamModeFormat) {
111+
$transformation[] = $streamModeFormat;
112+
}
113+
114+
if ($streamModeQuality) {
115+
$transformation[] = $streamModeQuality;
116+
}
117+
}
118+
if ($streamMode == 'abr') {
119+
if (isset($videoSettings['source_types'])) {
120+
$config['sourceTypes'] = ['auto'];
121+
$transformation[] = 'f_' . $videoSettings['source_types'];
122+
}
123+
}
124+
125+
if ($transformation && is_array($transformation)) {
126+
$config['transformation'] = implode(',', $transformation);
127+
}
128+
}
129+
$this->cloudinaryPGoptions['videoProps'] = $config;
130+
}
131+
69132
foreach ($this->cloudinaryPGoptions as $key => $value) {
70133
//Change casting
71134
if (isset($this->_casting[$key])) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
<?php
2+
3+
namespace Cloudinary\Cloudinary\Model\Config\Backend;
4+
5+
use Magento\Framework\App\Cache\TypeListInterface;
6+
use Magento\Framework\App\Config\ReinitableConfigInterface;
7+
use Magento\Framework\App\Config\ScopeConfigInterface;
8+
use Magento\Framework\Data\Collection\AbstractDb;
9+
use Magento\Framework\Message\ManagerInterface;
10+
use Magento\Framework\Model\Context;
11+
use Magento\Framework\Model\ResourceModel\AbstractResource;
12+
use Magento\Framework\Registry;
13+
14+
class VideoSettingsFreeParams extends \Magento\Framework\App\Config\Value
15+
{
16+
const BAD_JSON_ERROR_MESSAGE = "Json error on 'Video free parameters' please correct.";
17+
18+
const JSON_ERRORS = [
19+
JSON_ERROR_NONE => 'No error',
20+
JSON_ERROR_DEPTH => 'Maximum stack depth exceeded',
21+
JSON_ERROR_STATE_MISMATCH => 'State mismatch (invalid or malformed JSON)',
22+
JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
23+
JSON_ERROR_SYNTAX => 'Syntax error',
24+
JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded'
25+
];
26+
27+
/**
28+
* @var ManagerInterface
29+
*/
30+
private $messageManager;
31+
32+
/**
33+
* Application config
34+
*
35+
* @var ScopeConfigInterface
36+
*/
37+
protected $appConfig;
38+
39+
public function __construct(
40+
Context $context,
41+
Registry $registry,
42+
ScopeConfigInterface $config,
43+
TypeListInterface $cacheTypeList,
44+
AbstractResource $resource = null,
45+
AbstractDb $resourceCollection = null,
46+
ManagerInterface $messageManager,
47+
ReinitableConfigInterface $appConfig,
48+
array $data = []
49+
) {
50+
$this->messageManager = $messageManager;
51+
$this->appConfig = $appConfig;
52+
53+
parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
54+
}
55+
56+
57+
protected function jsonDecode($jsonString)
58+
{
59+
$json = preg_replace('/\r|\n/','',trim($jsonString));
60+
$json = str_replace(
61+
array('"', "'"),
62+
array('\"', '"'),
63+
$json
64+
);
65+
66+
$start = strpos($json, 'logoImageUrl');
67+
// Find the end of the URL (first comma after the start of logoImageUrl)
68+
$end = strpos($json, ',', $start);
69+
$logoUrl = null;
70+
if ($start) {
71+
72+
$logoUrl = substr($json, $start, $end - $start);
73+
$json = substr($json, 0, $start) . substr($json, $end + 1);
74+
}
75+
$linkStart = strpos($json, 'logoOnclickUrl');
76+
$linkEnd = strpos($json, ',',$linkStart);
77+
$linkUrl = null;
78+
if ($linkStart) {
79+
$linkUrl = substr($json, $linkStart, $linkEnd - $linkStart);
80+
$json = substr($json, 0, $linkStart) . substr($json, $linkEnd +1);
81+
}
82+
83+
// Extract the logoImageUrl part
84+
85+
$json = preg_replace('/(\w+):/', '"$1":', $json);
86+
$arr = json_decode($json, true);
87+
88+
if (is_array($arr)) {
89+
if ($logoUrl) {
90+
$url = str_replace("logoImageUrl:", "", $logoUrl);
91+
$url = str_replace('"', "", $url);
92+
$arr['player']['logoImageUrl'] = $url;
93+
}
94+
if ($linkUrl) {
95+
$url = str_replace("logoOnclickUrl:", "", $linkUrl);
96+
$url = str_replace('"', "", $url);
97+
$arr['player']['logoOnclickUrl'] = $url;
98+
}
99+
$json = json_encode($arr);
100+
}
101+
// $json = str_replace("'", '"', $json);
102+
return (json_decode($json)) ? $json : $jsonString;
103+
}
104+
105+
function isJson($string) {
106+
json_decode($string);
107+
return json_last_error() === JSON_ERROR_NONE;
108+
}
109+
110+
public function beforeSave()
111+
{
112+
$rawValue = $this->getValue();
113+
114+
parent::beforeSave();
115+
116+
$this->cacheTypeList->cleanType(\Magento\Framework\App\Cache\Type\Config::TYPE_IDENTIFIER);
117+
$this->appConfig->reinit();
118+
119+
if ($rawValue) {
120+
121+
$data = $this->jsonDecode($rawValue);
122+
if ($data === null || $data === false) {
123+
$this->setValue('{}');
124+
try {
125+
if (json_last_error() !== JSON_ERROR_NONE) {
126+
$this->messageManager->addError(self::BAD_JSON_ERROR_MESSAGE . ' (' . self::JSON_ERRORS[json_last_error()] . ')');
127+
}
128+
} catch (\Exception $e) {
129+
$this->messageManager->addError(self::BAD_JSON_ERROR_MESSAGE);
130+
}
131+
} else {
132+
if ($this->isJson($data)) {
133+
134+
return $this->setValue($data);
135+
}
136+
$this->setValue('{}');
137+
}
138+
} else {
139+
$this->setValue('{}');
140+
}
141+
}
142+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Cloudinary\Cloudinary\Model\Config\Source\Dropdown\Video\ABR;
4+
5+
use Magento\Framework\UrlInterface;
6+
use Magento\Config\Model\Config\CommentInterface;
7+
8+
9+
class Comment implements CommentInterface
10+
{
11+
/**
12+
* @var UrlInterface
13+
*/
14+
protected $urlInterface;
15+
16+
/**
17+
* @param UrlInterface $urlInterface
18+
*/
19+
public function __construct(
20+
UrlInterface $urlInterface
21+
) {
22+
$this->urlInterface = $urlInterface;
23+
}
24+
25+
public function getCommentText($elementValue)
26+
{
27+
28+
if ($elementValue == 'optimization') {
29+
$comment = '';
30+
}
31+
return $comment;
32+
}
33+
}

0 commit comments

Comments
 (0)