Skip to content

Commit

Permalink
Merge pull request #96 from cloudinary/v1.14.13
Browse files Browse the repository at this point in the history
v1.14.13
  • Loading branch information
Pniel (Pini) Cohen authored Jun 17, 2021
2 parents 8f61236 + 2d74305 commit be2f817
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 37 deletions.
16 changes: 16 additions & 0 deletions Api/ProductGalleryManagementInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,20 @@ public function addItems($items);
* @return string
*/
public function addProductMedia($sku, $urls);

/**
* Get product gallery items as Cloudinary URLs.
* @method getProductMedia
* @param string $sku
* @return string
*/
public function getProductMedia($sku);

/**
* Get products gallery items as Cloudinary URLs.
* @method getProductsMedia
* @param mixed $skus
* @return string
*/
public function getProductsMedia($skus);
}
9 changes: 0 additions & 9 deletions Helper/MediaLibraryHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,6 @@ public function getCloudinaryMLOptions($multiple = false, $refresh = true)
'platform' => "{$this->configuration->getMagentoPlatformName()} {$this->configuration->getMagentoPlatformEdition()} {$this->configuration->getMagentoPlatformVersion()}"
]
];
if (($this->credentials["username"] = $this->configuration->getAutomaticLoginUser())) {
$this->cloudinaryMLoptions["timestamp"] = $this->timestamp;
$this->cloudinaryMLoptions["username"] = $this->credentials["username"];
$this->cloudinaryMLoptions["signature"] = $this->signature = hash('sha256', urldecode(http_build_query([
'cloud_name' => $this->credentials['cloud_name'],
'timestamp' => $this->timestamp,
'username' => $this->credentials['username'],
])) . $this->credentials['api_secret']);
}
}
}
if ($this->cloudinaryMLoptions) {
Expand Down
106 changes: 96 additions & 10 deletions Model/Api/ProductGalleryManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,7 @@ public function addProductMedia($sku, $urls)
];
try {
$this->checkEnvHeader();
if (!$this->configuration->isEnabled()) {
throw new LocalizedException(
__("Cloudinary module is disabled. Please enable it first in order to use this API.")
);
}
$this->checkEnabled();
$urls = (array)$urls;
foreach ($urls as $i => $url) {
try {
Expand Down Expand Up @@ -284,6 +280,50 @@ public function addProductMedia($sku, $urls)
return $this->jsonHelper->jsonEncode($result);
}

/**
* {@inheritdoc}
*/
public function getProductMedia($sku)
{
return $this->_getProductMedia($sku);
}

/**
* {@inheritdoc}
*/
public function getProductsMedia($skus)
{
return $this->_getProductMedia($skus);
}

/**
* [_getProductMedia description]
* @method _getProductMedia
* @param mixed $sku
* @return string (json result)
*/
private function _getProductMedia($sku)
{
$result = ["data" => []];

try {
$this->checkEnvHeader();
$this->checkEnabled();
if (is_array($sku) || is_object($sku)) {
foreach ($sku as $key => $_sku) {
$result['data'][$_sku] = $this->getProductCldUrlsBySku($_sku);
}
} else {
$result['data'] = $this->getProductCldUrlsBySku($sku);
}
} catch (\Exception $e) {
$result["error"] = 1;
$result["message"] = $e->getMessage();
}

return $this->jsonHelper->jsonEncode($result);
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -312,11 +352,7 @@ public function addItems($items)
];
try {
$this->checkEnvHeader();
if (!$this->configuration->isEnabled()) {
throw new LocalizedException(
__("Cloudinary module is disabled. Please enable it first in order to use this API.")
);
}
$this->checkEnabled();
$items = (array)$items;
foreach ($items as $i => $item) {
try {
Expand Down Expand Up @@ -568,6 +604,20 @@ private function checkEnvHeader()
return $this;
}

/**
* @method checkEnabled
* @return $this
*/
private function checkEnabled()
{
if (!$this->configuration->isEnabled()) {
throw new LocalizedException(
__("Cloudinary module is disabled. Please enable it first in order to use this API.")
);
}
return $this;
}

private function getLocalTmpFileName($remoteFileUrl)
{
$localFileName = Uploader::getCorrectFileName(basename($remoteFileUrl));
Expand Down Expand Up @@ -682,6 +732,42 @@ private function saveCloudinaryMapping()
->save();
}

/**
* @method getProductMediaBySku
* @param string $sku
* @return array
*/
private function getProductCldUrlsBySku($sku)
{
$urls = [
'image' => null,
'small_image' => null,
'thumbnail' => null,
'media_gallery' => [],
];
try {
$product = $this->productRepository->get($sku);
foreach ($product->getMediaGalleryImages() as $gallItem) {
$urls['media_gallery'][] = $gallItem->getUrl();
if ($product->getData('image') === $gallItem->getFile()) {
$urls['image'] = $gallItem->getUrl();
}
if ($product->getData('small_image') === $gallItem->getFile()) {
$urls['small_image'] = $gallItem->getUrl();
}
if ($product->getData('thumbnail') === $gallItem->getFile()) {
$urls['thumbnail'] = $gallItem->getUrl();
}
}
} catch (\Exception $e) {
$urls = [
'error' => 1,
'message' => $e->getMessage(),
];
}
return $urls;
}

///////////////////////////////
// App Environment Emulation //
///////////////////////////////
Expand Down
9 changes: 0 additions & 9 deletions Model/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class Configuration implements ConfigurationInterface
//= Basics
const CONFIG_PATH_ENABLED = 'cloudinary/cloud/cloudinary_enabled';
const CONFIG_PATH_ENVIRONMENT_VARIABLE = 'cloudinary/setup/cloudinary_environment_variable';
const CONFIG_PATH_AUTOMATIC_LOGIN_USER = 'cloudinary/setup/cloudinary_automatic_login_user';
const CONFIG_PATH_CDN_SUBDOMAIN = 'cloudinary/configuration/cloudinary_cdn_subdomain';

//= Transformations
Expand Down Expand Up @@ -229,14 +228,6 @@ public function getCredentials()
return $this->getEnvironmentVariable()->getCredentials();
}

/**
* @return string
*/
public function getAutomaticLoginUser()
{
return (string) $this->configReader->getValue(self::CONFIG_PATH_AUTOMATIC_LOGIN_USER);
}

/**
* @return Transformation
*/
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "cloudinary/cloudinary-magento2",
"description": "Cloudinary Magento 2 Integration.",
"type": "magento2-module",
"version": "1.14.11",
"version": "1.14.13",
"license": "MIT",
"require": {
"cloudinary/cloudinary_php": "^1.20.0"
Expand Down
7 changes: 1 addition & 6 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@
</comment>
<backend_model>Cloudinary\Cloudinary\Model\Config\Backend\Credentials</backend_model>
</field>
<field id="cloudinary_automatic_login_user" translate="label" type="text" sortOrder="2" showInDefault="1" showInWebsite="0" showInStore="0" canRestore="1">
<label>Automatic Login</label>
<comment><![CDATA[Enter the email address of the user to be automatically logged in to the Media Library. If you have multiple users for your Cloudinary account, enter the primary user, or leave blank to prompt each user to login when the Media Library opens.<input name="disable_autofill_envvar" type="text" value="******" style="display:none!important;"><input name="disable_autofill_loginuser" type="password" value="******" style="display:none!important;">]]></comment>
<validate>validate-email</validate>
</field>
</group>
<group id="configuration" translate="label" sortOrder="3" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Cloudinary Setup</label>
Expand Down Expand Up @@ -86,7 +81,7 @@
<attribute type="expanded">0</attribute>
<field id="pg_enabled" translate="label comment" type="select" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0" canRestore="1">
<label>Enable Cloudinary's Product Gallery</label>
<comment><![CDATA[Enable Cloudinary's Product Gallery for product detail pages (override Magento's default). Supports Images, Videos and Spinsets. See <a href="https://cloudinary.com/documentation/product_gallery" target="_blank" title="Product Gallery documentation">Product Gallery documentation</a> for information on the configuration options.]]></comment>
<comment><![CDATA[Enable Cloudinary's Product Gallery for product detail pages (override Magento's default). Supports Images, Videos and Spinsets. See <a href="https://cloudinary.com/documentation/product_gallery" target="_blank" title="Product Gallery documentation">Product Gallery documentation</a> for information on the configuration options.<br>*To enable the Product Gallery to access and load your media, ensure that you remove any restrictions on listing resources. To verify this, open the <a href="https://cloudinary.com/console/" title="Cloudinary Console">Cloudinary Console</a>, navigate to Settings and select the Security tab. From here, make sure that “Resource List” is unchecked in the “Restricted Media Types” option group.]]></comment>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<config_path>cloudinary/product_gallery/enabled</config_path>
</field>
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Cloudinary_Cloudinary" setup_version="1.14.11">
<module name="Cloudinary_Cloudinary" setup_version="1.14.13">
<sequence>
<module name="Magento_ProductVideo"/>
<module name="Magento_PageBuilder"/>
Expand Down
18 changes: 18 additions & 0 deletions etc/webapi.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,24 @@
</route>
<!--/ cloudinary/productGallery/addItems -->

<!-- cloudinary/products/media -->
<route method="POST" url="/V1/cloudinary/products/media/get">
<service class="Cloudinary\Cloudinary\Api\ProductGalleryManagementInterface" method="getProductsMedia"/>
<resources>
<resource ref="Magento_Catalog::products" />
</resources>
</route>
<!--/ cloudinary/products/media -->

<!-- cloudinary/products/:sku/media -->
<route method="GET" url="/V1/cloudinary/products/:sku/media">
<service class="Cloudinary\Cloudinary\Api\ProductGalleryManagementInterface" method="getProductMedia"/>
<resources>
<resource ref="Magento_Catalog::products" />
</resources>
</route>
<!--/ cloudinary/products/:sku/media -->

<!-- cloudinary/products/:sku/media -->
<route method="POST" url="/V1/cloudinary/products/:sku/media">
<service class="Cloudinary\Cloudinary\Api\ProductGalleryManagementInterface" method="addProductMedia"/>
Expand Down
2 changes: 1 addition & 1 deletion marketplace.composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "cloudinary/cloudinary",
"description": "Cloudinary Magento 2 Integration.",
"type": "magento2-module",
"version": "1.14.11",
"version": "1.14.13",
"license": "MIT",
"require": {
"cloudinary/cloudinary_php": "^1.20.0"
Expand Down

0 comments on commit be2f817

Please sign in to comment.