init
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Review\Plugin\Adminhtml\Review;
|
||||
|
||||
use Esun\Review\Block\Adminhtml\Review\Images;
|
||||
use Magento\Review\Block\Adminhtml\Edit\Form;
|
||||
|
||||
class FormImagesPlugin
|
||||
{
|
||||
public function afterToHtml(Form $subject, string $html): string
|
||||
{
|
||||
$reviewId = (int)$subject->getRequest()->getParam('id');
|
||||
if ($reviewId <= 0) {
|
||||
return $html;
|
||||
}
|
||||
|
||||
$imagesHtml = $subject->getLayout()
|
||||
->createBlock(Images::class)
|
||||
->setTemplate('Esun_Review::review/images.phtml')
|
||||
->setData('review_id', $reviewId)
|
||||
->toHtml();
|
||||
|
||||
return $imagesHtml ? $html . $imagesHtml : $html;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Review\Plugin\Review;
|
||||
|
||||
use Closure;
|
||||
use Magento\Customer\Model\Session as CustomerSession;
|
||||
use Magento\Framework\App\RequestInterface;
|
||||
use Magento\Framework\Controller\ResultFactory;
|
||||
use Magento\Framework\Message\ManagerInterface;
|
||||
use Magento\Review\Controller\Product\Post;
|
||||
|
||||
class PostGuestGuardPlugin
|
||||
{
|
||||
public function __construct(
|
||||
private readonly CustomerSession $customerSession,
|
||||
private readonly ResultFactory $resultFactory,
|
||||
private readonly ManagerInterface $messageManager,
|
||||
private readonly RequestInterface $request
|
||||
) {
|
||||
}
|
||||
|
||||
public function aroundExecute(Post $subject, Closure $proceed)
|
||||
{
|
||||
if ($this->customerSession->isLoggedIn()) {
|
||||
return $proceed();
|
||||
}
|
||||
|
||||
$referer = (string)$this->request->getServer('HTTP_REFERER');
|
||||
if ($referer !== '') {
|
||||
$this->customerSession->setBeforeAuthUrl($referer);
|
||||
}
|
||||
$this->messageManager->addErrorMessage(__('Please sign in to write a review.'));
|
||||
|
||||
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
|
||||
return $resultRedirect->setPath('customer/account/login');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Review\Plugin\Review;
|
||||
|
||||
use Magento\Framework\App\RequestInterface;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\Framework\App\ResourceConnection;
|
||||
use Magento\Framework\Filesystem;
|
||||
use Magento\MediaStorage\Model\File\UploaderFactory;
|
||||
use Magento\Review\Model\Review;
|
||||
use Magento\Customer\Model\Session as CustomerSession;
|
||||
use Esun\Review\Model\Order\ReviewEligibilityService;
|
||||
|
||||
class SaveImagesPlugin
|
||||
{
|
||||
public function __construct(
|
||||
private readonly RequestInterface $request,
|
||||
private readonly UploaderFactory $uploaderFactory,
|
||||
private readonly Filesystem $filesystem,
|
||||
private readonly ResourceConnection $resourceConnection,
|
||||
private readonly CustomerSession $customerSession,
|
||||
private readonly ReviewEligibilityService $eligibilityService
|
||||
) {
|
||||
}
|
||||
|
||||
public function beforeValidate(Review $subject): void
|
||||
{
|
||||
if ($this->request->getFullActionName() !== 'review_product_post') {
|
||||
return;
|
||||
}
|
||||
|
||||
$detail = trim((string)$subject->getDetail());
|
||||
if (!trim((string)$subject->getTitle())) {
|
||||
$subject->setTitle($detail !== '' ? mb_substr($detail, 0, 120) : 'Review');
|
||||
}
|
||||
|
||||
if (!trim((string)$subject->getNickname())) {
|
||||
$customer = $this->customerSession->getCustomer();
|
||||
$name = trim((string)$customer->getFirstname() . ' ' . (string)$customer->getLastname());
|
||||
$subject->setNickname($name !== '' ? $name : 'Customer');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Frontend reviews must belong to a logged-in customer.
|
||||
*
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
public function beforeSave(Review $subject): void
|
||||
{
|
||||
if (!(int)$subject->getCustomerId()) {
|
||||
throw new LocalizedException(__('Please sign in to write a review.'));
|
||||
}
|
||||
if ($this->request->getFullActionName() === 'review_product_post') {
|
||||
$orderItemId = (int)$this->request->getParam('order_item_id');
|
||||
$item = $this->eligibilityService->getItem($orderItemId, (int)$subject->getCustomerId());
|
||||
if ((int)$item->getProductId() !== (int)$subject->getEntityPkValue()) {
|
||||
throw new LocalizedException(__('The reviewed product does not match the order item.'));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function afterSave(Review $subject, Review $result): Review
|
||||
{
|
||||
$this->savePurchaseLink($subject);
|
||||
if (!$subject->getId() || empty($_FILES['review_images']['name'][0])) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
$files = $this->normalizeFiles($_FILES['review_images']);
|
||||
$mediaDirectory = $this->filesystem->getDirectoryWrite('media');
|
||||
$target = 'esun/review/' . (int)$subject->getId();
|
||||
$mediaDirectory->create($target);
|
||||
$connection = $this->resourceConnection->getConnection();
|
||||
$table = $this->resourceConnection->getTableName('esun_review_image');
|
||||
|
||||
foreach ($files as $index => $file) {
|
||||
if (($file['error'] ?? UPLOAD_ERR_NO_FILE) !== UPLOAD_ERR_OK) {
|
||||
continue;
|
||||
}
|
||||
$_FILES['review_image_single'] = $file;
|
||||
try {
|
||||
$uploader = $this->uploaderFactory->create(['fileId' => 'review_image_single']);
|
||||
$uploader->setAllowedExtensions(['jpg', 'jpeg', 'png', 'gif', 'webp', 'mp4', 'webm', 'mov']);
|
||||
$uploader->setAllowRenameFiles(true);
|
||||
$saved = $uploader->save($mediaDirectory->getAbsolutePath($target));
|
||||
if (empty($saved['file'])) {
|
||||
continue;
|
||||
}
|
||||
$path = $target . '/' . ltrim((string)$saved['file'], '/');
|
||||
$connection->insert($table, [
|
||||
'review_id' => (int)$subject->getId(),
|
||||
'customer_id' => (int)$subject->getCustomerId() ?: null,
|
||||
'image_path' => $path,
|
||||
'thumbnail_path' => $path,
|
||||
'media_type' => in_array(strtolower((string)pathinfo($path, PATHINFO_EXTENSION)), ['mp4', 'webm', 'mov'], true) ? 'video' : 'image',
|
||||
'status' => 1,
|
||||
'sort_order' => $index,
|
||||
]);
|
||||
} catch (\Throwable) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
unset($_FILES['review_image_single']);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function savePurchaseLink(Review $review): void
|
||||
{
|
||||
if ($this->request->getFullActionName() !== 'review_product_post' || !(int)$review->getId()) { return; }
|
||||
$orderItemId = (int)$this->request->getParam('order_item_id');
|
||||
$item = $this->eligibilityService->getItemForLink($orderItemId, (int)$review->getCustomerId());
|
||||
$this->resourceConnection->getConnection()->insertOnDuplicate($this->resourceConnection->getTableName('esun_review_purchase'), [
|
||||
'review_id' => (int)$review->getId(), 'customer_id' => (int)$review->getCustomerId(), 'order_id' => (int)$item->getOrderId(),
|
||||
'order_item_id' => (int)$item->getItemId(), 'product_id' => (int)$item->getProductId(), 'eligibility_type' => 'payment_success'
|
||||
], []);
|
||||
}
|
||||
|
||||
private function normalizeFiles(array $files): array
|
||||
{
|
||||
$normalized = [];
|
||||
foreach ((array)$files['name'] as $index => $name) {
|
||||
$normalized[] = [
|
||||
'name' => $name,
|
||||
'type' => $files['type'][$index] ?? '',
|
||||
'tmp_name' => $files['tmp_name'][$index] ?? '',
|
||||
'error' => $files['error'][$index] ?? UPLOAD_ERR_NO_FILE,
|
||||
'size' => $files['size'][$index] ?? 0,
|
||||
];
|
||||
}
|
||||
|
||||
return $normalized;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Review\Plugin;
|
||||
|
||||
use Closure;
|
||||
use Magento\Catalog\Block\Product\ReviewRendererInterface;
|
||||
use Magento\Catalog\Model\Product;
|
||||
use Magento\Framework\App\ResourceConnection;
|
||||
use Magento\Review\Block\Product\ReviewRenderer;
|
||||
use Magento\Review\Model\Review;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
|
||||
class ReviewRendererPlugin
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ResourceConnection $resourceConnection,
|
||||
private readonly StoreManagerInterface $storeManager
|
||||
) {
|
||||
}
|
||||
|
||||
public function aroundGetReviewsSummaryHtml(
|
||||
ReviewRenderer $subject,
|
||||
Closure $proceed,
|
||||
Product $product,
|
||||
$templateType = ReviewRendererInterface::DEFAULT_VIEW,
|
||||
$displayIfNoReviews = false
|
||||
): string {
|
||||
$result = $proceed($product, $templateType, $displayIfNoReviews);
|
||||
|
||||
if ($templateType === ReviewRendererInterface::SHORT_VIEW) {
|
||||
$subject->setTemplate('Esun_Review::helper/summary_short.phtml');
|
||||
$subject->setProduct($product);
|
||||
$subject->setDisplayIfEmpty($displayIfNoReviews);
|
||||
|
||||
return $subject->toHtml();
|
||||
}
|
||||
|
||||
if (!$displayIfNoReviews) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
$summary = $this->getApprovedReviewSummary((int)$product->getId());
|
||||
$reviewsCount = $summary['reviews_count'] ?: (int)$subject->getReviewsCount();
|
||||
$ratingSummary = $summary['rating_summary'] ?: (int)$subject->getRatingSummary();
|
||||
|
||||
$subject->setTemplate('Esun_Review::review/summary-empty-rating.phtml');
|
||||
$subject->setProduct($product);
|
||||
$subject->setDisplayIfEmpty($displayIfNoReviews);
|
||||
$subject->setData('esun_reviews_count', $reviewsCount);
|
||||
$subject->setData('esun_rating_summary', $ratingSummary);
|
||||
|
||||
return $subject->toHtml();
|
||||
}
|
||||
|
||||
private function getApprovedReviewSummary(int $productId): array
|
||||
{
|
||||
$connection = $this->resourceConnection->getConnection();
|
||||
$storeId = (int)$this->storeManager->getStore()->getId();
|
||||
|
||||
$select = $connection->select()
|
||||
->from(['r' => $this->resourceConnection->getTableName('review')], [])
|
||||
->join(
|
||||
['rs' => $this->resourceConnection->getTableName('review_store')],
|
||||
'rs.review_id = r.review_id',
|
||||
[]
|
||||
)
|
||||
->joinLeft(
|
||||
['v' => $this->resourceConnection->getTableName('rating_option_vote')],
|
||||
'v.review_id = r.review_id',
|
||||
[]
|
||||
)
|
||||
->where('r.entity_pk_value = ?', $productId)
|
||||
->where('r.status_id = ?', Review::STATUS_APPROVED)
|
||||
->where('rs.store_id IN (?)', [0, $storeId])
|
||||
->columns([
|
||||
'reviews_count' => new \Zend_Db_Expr('COUNT(DISTINCT r.review_id)'),
|
||||
'rating_summary' => new \Zend_Db_Expr('ROUND(AVG(v.percent))'),
|
||||
]);
|
||||
|
||||
$summary = $connection->fetchRow($select) ?: [];
|
||||
|
||||
return [
|
||||
'reviews_count' => (int)($summary['reviews_count'] ?? 0),
|
||||
'rating_summary' => (int)($summary['rating_summary'] ?? 0),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user