Skip to content

Commit 762a793

Browse files
Merge remote-tracking branch 'upstream/v1.19.7'
2 parents c084d1f + 8f680c9 commit 762a793

File tree

9 files changed

+232
-5
lines changed

9 files changed

+232
-5
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<?php
2+
namespace Cloudinary\Cloudinary\Controller\Adminhtml\Ajax;
3+
4+
use Cloudinary\Api\BaseApiClient;
5+
use Cloudinary\Cloudinary\Core\ConfigurationInterface;
6+
use Cloudinary\Cloudinary\Core\ConfigurationBuilder;
7+
use Cloudinary\Cloudinary\Core\Image\ImageFactory;
8+
use Cloudinary\Cloudinary\Core\UrlGenerator;
9+
use Cloudinary\Configuration\Configuration;
10+
use Magento\Framework\App\Filesystem\DirectoryList;
11+
use Magento\Store\Model\StoreManagerInterface;
12+
use Magento\Framework\UrlInterface;
13+
use Magento\Backend\App\Action;
14+
use Magento\Framework\Controller\Result\RawFactory as ResultRawFactory;
15+
use Magento\Backend\App\Action\Context;
16+
use Magento\Cms\Model\Wysiwyg\Images\GetInsertImageContent;
17+
use Magento\Framework\Filesystem as FileSysten;
18+
use Magento\Catalog\Helper\Image as CatalogImageHelper;
19+
use Cloudinary\Cloudinary\Core\Image;
20+
use Cloudinary\Asset\Media;
21+
use Cloudinary\Cloudinary\Core\Image\Transformation;
22+
23+
class UpdateAdminImage extends Action
24+
{
25+
/**
26+
* @var ConfigurationInterface
27+
*/
28+
protected $configuration;
29+
/**
30+
* @var UrlGenerator
31+
*/
32+
protected $urlGenerator;
33+
/**
34+
* @var ImageFactory
35+
*/
36+
protected $imageFactory;
37+
/**
38+
* @var StoreManagerInterface
39+
*/
40+
protected $storeManager;
41+
42+
/**
43+
* @var UrlInterface
44+
*/
45+
protected $urlInterface;
46+
47+
protected $resultFactory;
48+
49+
protected $imageContent;
50+
51+
protected $filesystem;
52+
53+
private $_authorised;
54+
55+
protected $configurationBuilder;
56+
57+
protected $transformation;
58+
59+
/**
60+
* @param ImageFactory $imageFactory
61+
* @param UrlGenerator $urlGenerator
62+
* @param ConfigurationInterface $configuration
63+
* @param StoreManagerInterface $storeManager
64+
* @param UrlInterface $urlInterface
65+
*/
66+
public function __construct(
67+
Context $context,
68+
ImageFactory $imageFactory,
69+
UrlGenerator $urlGenerator,
70+
ConfigurationInterface $configuration,
71+
StoreManagerInterface $storeManager,
72+
UrlInterface $urlInterface,
73+
ResultRawFactory $resultFactory,
74+
FileSysten $filesystem,
75+
ConfigurationBuilder $configurationBuilder,
76+
Transformation $transformation
77+
) {
78+
parent::__construct($context);
79+
$this->imageFactory = $imageFactory;
80+
$this->urlGenerator = $urlGenerator;
81+
$this->configuration = $configuration;
82+
$this->storeManager = $storeManager;
83+
$this->urlInterface = $urlInterface;
84+
$this->resultFactory = $resultFactory;
85+
$this->filesystem = $filesystem;
86+
$this->configurationBuilder = $configurationBuilder;
87+
$this->transformation = $transformation;
88+
}
89+
90+
private function authorise()
91+
{
92+
if (!$this->_authorised && $this->configuration->isEnabled()) {
93+
Configuration::instance($this->configurationBuilder->build());
94+
BaseApiClient::$userPlatform = $this->configuration->getUserPlatform();
95+
$this->_authorised = true;
96+
}
97+
}
98+
99+
public function execute()
100+
{
101+
$this->authorise();
102+
if ($this->configuration->isEnabled()) {
103+
try{
104+
$remoteImageUrl = $this->getRequest()->getParam('remote_image');
105+
$filedId = str_replace($this->storeManager->getStore()->getBaseUrl(), '', $remoteImageUrl);
106+
107+
$result = Media::fromParams(
108+
$filedId,
109+
[ 'transformation' => $this->transformation->build(),
110+
'secure' => true,
111+
'sign_url' => $this->configuration->getUseSignedUrls(),
112+
'version' => 1
113+
]
114+
) . '?_i=AB';
115+
116+
117+
118+
119+
} catch (\Exception $e) {
120+
$result = ['error' => $e->getMessage(), 'errorcode' => $e->getCode()];
121+
}
122+
}
123+
124+
125+
$response = $this->resultFactory->create();
126+
$response->setHeader('Content-type', 'text/plain');
127+
$response->setContents(json_encode($result));
128+
return $response;
129+
}
130+
}

Helper/MediaLibraryHelper.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ public function getCloudinaryMLshowOptions($resourceType = null, $path = "")
106106
*/
107107
public function getCname()
108108
{
109-
$cname = isset($this->configuration->getCredentials()['cname']) ? $this->configuration->getCredentials()['cname'] : null;
110-
return $cname;
109+
return $this->configuration->isEnabled() && isset($this->configuration->getCredentials()['cname']) ? $this->configuration->getCredentials()['cname'] : null;
111110
}
112111
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "cloudinary/cloudinary-magento2",
33
"description": "Cloudinary Magento 2 Integration.",
44
"type": "magento2-module",
5-
"version": "1.19.6",
5+
"version": "1.19.7",
66
"license": "MIT",
77
"require": {
88
"cloudinary/cloudinary_php": ">=2.7 <2.8.1",

etc/module.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0"?>
22
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
3-
<module name="Cloudinary_Cloudinary" setup_version="1.19.6">
3+
<module name="Cloudinary_Cloudinary" setup_version="1.19.7">
44
<sequence>
55
<module name="Magento_ProductVideo"/>
66
<module name="Magento_PageBuilder"/>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
9+
<body>
10+
<referenceContainer name="content">
11+
<block class="Magento\Backend\Block\Template" template="Cloudinary_Cloudinary::cms/images.phtml" name="cms_block_edit_images_update" />
12+
</referenceContainer>
13+
</body>
14+
</page>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
9+
<update handle="editor"/>
10+
<body>
11+
<referenceContainer name="content">
12+
<block class="Magento\Backend\Block\Template" template="Cloudinary_Cloudinary::cms/images.phtml" name="cms_page_edit_images_update" />
13+
</referenceContainer>
14+
</body>
15+
</page>

view/adminhtml/requirejs-config.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ var config = {
88
cloudinarySpinsetModal: 'Cloudinary_Cloudinary/js/cloudinary-spinset-modal',
99
cldspinsetDialog: 'Cloudinary_Cloudinary/js/cloudinary-spinset-dialog',
1010
productGallery: 'Cloudinary_Cloudinary/js/product-gallery',
11-
cloudinaryLazyload: 'Cloudinary_Cloudinary/js/cloudinary-lazyload'
11+
cloudinaryLazyload: 'Cloudinary_Cloudinary/js/cloudinary-lazyload',
12+
updateCmsImages: 'Cloudinary_Cloudinary/js/cms/preview-update',
1213
}
1314
},
1415
paths: {
@@ -24,6 +25,9 @@ var config = {
2425
'uiComponent': {
2526
deps: ['jquery']
2627
},
28+
'Cloudinary_Cloudinary/js/cms/preview-update': {
29+
deps: ['jquery']
30+
}
2731
},
2832
config: {
2933
mixins: {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
/**
3+
* @var Magento\Backend\Block\Template $block
4+
*/
5+
?>
6+
<script type="text/x-magento-init">
7+
{"*": {
8+
"updateCmsImages": { "ajaxUrl": "<?= $block->getUrl('cloudinary/ajax/updateadminimage') ?>" }
9+
}}
10+
11+
</script>
12+
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
define(
2+
['jquery','Magento_PageBuilder/js/events','mage/url'],
3+
function($,_PBEvents, urlBuilder){
4+
'use strict'
5+
return function (config, element) {
6+
var updateHandler = {
7+
images: [],
8+
init: function () {
9+
let self = this;
10+
_PBEvents.on('image:renderAfter', function (event){
11+
let elem = event.element, key = event.id;
12+
let image = $(elem).find('img');
13+
let src = {'remote_image': image.attr('src')};
14+
self.images.push(src);
15+
16+
self.update(key);
17+
});
18+
$('#save-button').on('click', function (e){
19+
self.images.each(function(elem){
20+
if (elem.cld_image) {
21+
let cld_src = elem.cld_image;
22+
let img = $('img[src="' + cld_src +'"]');
23+
return (img.length) ? img.attr('src', elem.remote_image) : '';
24+
}
25+
});
26+
});
27+
},
28+
update: function(key) {
29+
let self = this;
30+
this.images.each(function(elem,ind){
31+
$.ajax({
32+
url: config.ajaxUrl,
33+
type: 'POST',
34+
dataType: 'json',
35+
data: elem,
36+
success: function(image) {
37+
self.images[ind].cld_image = image;
38+
let img = $('img[src="' + self.images[ind].remote_image +'"]');
39+
if (img.length) {
40+
$('img[src="' + self.images[ind].remote_image +'"]').attr('src', self.images[ind].cld_image);
41+
}
42+
},
43+
error: function(xhr, textStatus, errorThrown) {
44+
console.log('Error:', textStatus, errorThrown);
45+
}
46+
});
47+
48+
})
49+
}
50+
};
51+
return updateHandler.init();
52+
}
53+
});

0 commit comments

Comments
 (0)