Skip to content

Commit

Permalink
Merge pull request #123 from cloudinary/v1.19.2
Browse files Browse the repository at this point in the history
V1.19.2
  • Loading branch information
adamonsoon committed Oct 25, 2022
2 parents db0b229 + c142a8f commit c198e23
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 3 deletions.
9 changes: 9 additions & 0 deletions Api/ProductGalleryManagementInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,13 @@ public function getProductMedia($sku);
* @return string
*/
public function getProductsMedia($skus);


/** Remove Product images by publicIds
* @param string $sku
* @param mixed $urls
* @param bool | int | null $delete_all_gallery
* @return string
*/
public function removeProductMedia($sku, $urls, $delete_all_gallery = 0);
}
86 changes: 86 additions & 0 deletions Model/Api/ProductGalleryManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\DataObject;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Filesystem;
use Magento\Framework\HTTP\Adapter\Curl;
use Magento\Framework\Image\AdapterFactory as ImageAdapterFactory;
Expand Down Expand Up @@ -233,6 +234,88 @@ public function __construct(
$this->resourceConnection = $resourceConnection;
}


/**
* Remove Gallery Item
* @param $product
* @param $url
* @param $removeAllGallery
* @return int|void
* @throws NoSuchEntityException .
*/
private function removeGalleryItem($product, $url, $removeAllGallery)
{
$unlinked = 0;
if ($product) {
$mediaGalleryEntries = $product->getMediaGalleryEntries();

if (is_array($mediaGalleryEntries)) {
foreach ($mediaGalleryEntries as $key => $entry) {
$image = $entry->getFile();
$url = preg_replace('/\?.*/', '', $url);
$image = $this->storeManager->getStore()->getBaseUrl() . 'pub/media/catalog/product' . $image;
$basename = basename($image);
$ext = pathinfo($image, PATHINFO_EXTENSION);
// removing unique id from original filename
$pattern = '/^' . $this->configuration::CLD_UNIQID_PREFIX . '.*?_/';
$basename = preg_replace($pattern, '', $basename);
$filename = basename($url);
$filename = preg_replace($pattern, '', $filename);
// $filename = $url . '.' . $ext;

if ($basename == $filename || $removeAllGallery) {
unset($mediaGalleryEntries[$key]);
$this->mediaGalleryProcessor->removeImage($product, $image);
$unlinked++;
}
}
}
if ($unlinked) {
$product->setMediaGalleryEntries($mediaGalleryEntries);
try {
$product = $this->productRepository->save($product);
} catch (\Exception $e) {
$message = ['type' => 'error', 'message' => 'Falied Delete Image Error: ' . $e->getMessage() . ' line ' . $e->getLine()];
}
}
return $unlinked;
}
}

/**
* {@inheritdoc}
*/
public function removeProductMedia($sku, $urls, $delete_all_gallery = 0)
{
$result = [
"passed" => 0,
"failed" => [
"count" => 0,
"urls" => []
]
];

$urls = (array) $urls;

try {
$product = $this->productRepository->get($sku);
$this->checkEnvHeader();
$this->checkEnabled();

foreach ($urls as $i => $url) {
$unlinked = $this->removeGalleryItem($product, $url, $delete_all_gallery);
}

$result["passed"] = $unlinked;
} catch (\Exception $e) {
$result["failed"]["count"]++;
$result["error"] = $e->getMessage();
$result["failed"]["public_id"][] = $url;
}

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

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -487,6 +570,7 @@ public function addGalleryItem($url, $sku, $publicId = null, $roles = null, $lab
);

$mediaGalleryData = $product->getMediaGallery();
// last gallery value from array
$galItem = array_pop($mediaGalleryData["images"]);

if ($this->parsedRemoteFileUrl["type"] === "video") {
Expand Down Expand Up @@ -812,4 +896,6 @@ private function emulateAdminhtmlArea($force = true)
$this->startEnvironmentEmulation(0, Area::AREA_ADMINHTML, $force);
return $this;
}


}
9 changes: 8 additions & 1 deletion Model/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -471,9 +471,16 @@ public function isLazyloadAutoReplaceCmsBlocks()
return (bool) $this->configReader->getValue(self::XML_PATH_LAZYLOAD_AUTO_REPLACE_CMS_BLOCKS);
}

/**
* @return array|null
*/
public function getLazyloadIgnoredCmsBlocksArray()
{
return (array) explode(',', $this->configReader->getValue(self::XML_PATH_LAZYLOAD_IGNORED_CMS_BLOCKS));
$value = ($this->configReader->getValue(self::XML_PATH_LAZYLOAD_IGNORED_CMS_BLOCKS))
? (array) explode(',', $this->configReader->getValue(self::XML_PATH_LAZYLOAD_IGNORED_CMS_BLOCKS))
: null;

return $value;
}

public function getLazyloadThreshold()
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.19.1",
"version": "1.19.2",
"license": "MIT",
"require": {
"cloudinary/cloudinary_php": ">=2.7 <2.8.1",
Expand Down
1 change: 1 addition & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@
<comment>List of CMS blocks to exclude from Lazyloading. Use this if there are conflicts on some of the blocks.</comment>
<config_path>cloudinary/lazyload/ignored_cms_blocks</config_path>
<source_model>Cloudinary\Cloudinary\Model\Config\Source\Dropdown\CmsBlocks</source_model>
<can_be_empty>1</can_be_empty>
<depends>
<field id="lazyload_enabled">1</field>
<field id="lazyload_auto_replace_cms_blocks">1</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.19.0">
<module name="Cloudinary_Cloudinary" setup_version="1.19.2">
<sequence>
<module name="Magento_ProductVideo"/>
<module name="Magento_PageBuilder"/>
Expand Down
9 changes: 9 additions & 0 deletions etc/webapi.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,13 @@
</route>
<!--/ cloudinary/products/:sku/media -->

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

</routes>

0 comments on commit c198e23

Please sign in to comment.