Skip to content

Commit

Permalink
General refactor following Reviewscouk#49
Browse files Browse the repository at this point in the history
  • Loading branch information
bartoszkubicki committed Jul 15, 2024
1 parent 1dab5ca commit 60ecd72
Showing 22 changed files with 593 additions and 561 deletions.
6 changes: 0 additions & 6 deletions 1

This file was deleted.

6 changes: 2 additions & 4 deletions Block/Product/CustomRatingSnippet.php
Original file line number Diff line number Diff line change
@@ -4,17 +4,15 @@

use Magento\Framework\View\Element\Template;

class CustomRatingSnippet extends \Reviewscouk\Reviews\Block\Product\RatingSnippet
class CustomRatingSnippet extends RatingSnippet
{
/**
* Get the template for the block based on the admin setting
*
* @return string
*/
public function getTemplate()
{
$isEnabled = $this->_scopeConfig->isSetFlag('reviewscouk_reviews_onpage/widget/category_rating_snippet_widget_enabled');

if ($isEnabled) {
return 'Reviewscouk_Reviews::category/list.phtml';
}
60 changes: 27 additions & 33 deletions Block/Product/RatingSnippet.php
Original file line number Diff line number Diff line change
@@ -2,51 +2,41 @@

namespace Reviewscouk\Reviews\Block\Product;

use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Catalog\Block\Product\Context;
use Magento\Catalog\Block\Product\ListProduct;
use Framework\Registry as Registry;
use Magento\Framework\Escaper as Escaper;
// use Reviewscouk\Reviews\Helper\Config as ReviewsConfig;
use Magento\Framework as Framework;
use Magento\Catalog\Model\CategoryFactory;
use Magento\Catalog\Model\Layer\Resolver;
use Magento\Customer\Model\Session;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Data\Helper\PostHelper;
use Magento\Framework\Escaper;
use Magento\Framework\Url\Helper\Data;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\App\Helper\Context as ContextHelper;
use \Reviewscouk\Reviews\Helper\Config as ReviewsConfig;

use Reviewscouk\Reviews\Helper\Config as ReviewsConfig;

class RatingSnippet extends ListProduct
{
protected $_customerSession;
protected $categoryFactory;
//protected $reviewsConfig = Reviews\Helper\Config;
// protected $_storeManager = StoreManagerInterface::class;
protected $store;
protected $reviewsConfig;
/**
* ListProduct constructor.
* @param \Magento\Catalog\Block\Product\Context $context
* @param \Magento\Framework\Data\Helper\PostHelper $postDataHelper
* @param \Magento\Catalog\Model\Layer\Resolver $layerResolver
* @param \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository
* @param \Magento\Framework\Url\Helper\Data $urlHelper
* @param Helper $helper
* @param array $data
* @param \Magento\Customer\Model\Session $customerSession
* @param \Magento\Catalog\Model\CategoryFactory $categoryFactory
*/

public function __construct(
\Magento\Catalog\Block\Product\Context $context,
\Magento\Framework\Data\Helper\PostHelper $postDataHelper,
\Magento\Catalog\Model\Layer\Resolver $layerResolver,
\Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository,
\Magento\Framework\Url\Helper\Data $urlHelper,
Context $context,
PostHelper $postDataHelper,
Resolver $layerResolver,
CategoryRepositoryInterface $categoryRepository,
Data $urlHelper,
array $data = [],
\Magento\Customer\Model\Session $customerSession = null,
\Magento\Catalog\Model\CategoryFactory $categoryFactory = null,
Session $customerSession = null,
CategoryFactory $categoryFactory = null,
ReviewsConfig $reviewsConfigHelper = null,
StoreManagerInterface $storeManager = null
) {
$this->_customerSession = $customerSession;
$this->categoryFactory = $categoryFactory;
$this->reviewsConfig = $reviewsConfigHelper ?: \Magento\Framework\App\ObjectManager::getInstance()->get(ReviewsConfig::class);
$this->reviewsConfig = $reviewsConfigHelper ?: ObjectManager::getInstance()->get(ReviewsConfig::class);
$this->store = $storeManager;

parent::__construct(
@@ -63,22 +53,24 @@ public function __construct(
private function getProductSkus($product)
{
$sku = $product->getSku();
$type = $product->getTypeID();
$type = (string) $product->getTypeID();

$productSkus = [$sku];
if ($type == 'configurable') {

if ($type === 'configurable') {
$usedProducts = $product->getTypeInstance()->getUsedProducts($product);
foreach ($usedProducts as $usedProduct) {
$productSkus[] = $usedProduct->getSku();
}
}

if ($type == 'grouped') {
if ($type === 'grouped') {
$usedProducts = $product->getTypeInstance()->getAssociatedProducts($product);
foreach ($usedProducts as $usedProduct) {
$productSkus[] = $usedProduct->getSku();
}
}

return $productSkus;
}

@@ -88,9 +80,11 @@ public function getRatingSnippet($product)
$ratingSnippetEnabled = $this->reviewsConfig->isCategoryRatingSnippetWidgetEnabled($this->store);
$skus = $this->getProductSkus($product);
$html = '';
if($ratingSnippetEnabled) {

if ($ratingSnippetEnabled) {
$html = '<div class="ruk_rating_snippet" data-sku="' . $escaper->escapeHtml((!empty($skus) ? implode(';', $skus) : '')) . '"></div>';
}

return $html;
}

9 changes: 5 additions & 4 deletions Block/Product/Reviewwidget.php
Original file line number Diff line number Diff line change
@@ -2,12 +2,11 @@

namespace Reviewscouk\Reviews\Block\Product;

use Reviewscouk\Reviews as Reviews;
use Magento\Framework as Framework;
use Reviewscouk\Reviews;
use Magento\Framework;

class Reviewwidget extends Framework\View\Element\Template
{

private $configHelper;
private $dataHelper;
private $registry;
@@ -126,7 +125,7 @@ protected function getWidgetColor()
{
$colour = $this->configHelper->getProductWidgetColour($this->store->getId());
// people will sometimes put hash and sometimes they will forgot so we need to check for this error
if (isset($colour) && strpos($colour, '#') === false) {
if (isset($colour) && !str_contains($colour, '#')) {
$colour = '#' . $colour;
}
// checking to see if we hare a valid colour. If not then we change it to reviews default hex colour
@@ -140,9 +139,11 @@ protected function getWidgetURL()
{
$region = $this->configHelper->getRegion($this->store->getId());
$api_url = 'widget.reviews.co.uk';

if ($region == 'US') {
$api_url = 'widget.reviews.io';
}

return $api_url;
}
}
33 changes: 16 additions & 17 deletions Block/Richsnippet.php
Original file line number Diff line number Diff line change
@@ -2,10 +2,10 @@

namespace Reviewscouk\Reviews\Block;

use Magento\Backend as Backend;
use Magento\Directory as Directory;
use Magento\Framework as Framework;
use Reviewscouk\Reviews as Reviews;
use Magento\Backend;
use Magento\Directory;
use Magento\Framework;
use Reviewscouk\Reviews;

class Richsnippet extends Framework\View\Element\Template
{
@@ -21,26 +21,24 @@ public function __construct(
Reviews\Helper\Data $dataHelper,
Framework\View\Element\Template\Context $context,
Framework\Registry $registry,
Backend\Block\Template\Context $backend,

Directory\Model\Currency $currency,

array $data = []
) {
parent::__construct($context, $data);

$this->configHelper = $config;
$this->dataHelper = $dataHelper;
$this->registry = $registry;
$this->currency = $currency;
$this->dataHelper = $dataHelper;
$this->registry = $registry;
$this->currency = $currency;

$this->store = $this->_storeManager->getStore();
}

public function autoRichSnippet()
{
$merchant_enabled = $this->configHelper->isMerchantRichSnippetsEnabled($this->store->getId());
$product_enabled = $this->configHelper->isProductRichSnippetsEnabled($this->store->getId());
$product_enabled = $this->configHelper->isProductRichSnippetsEnabled($this->store->getId());

$current_product = $this->registry->registry('current_product');

@@ -56,19 +54,20 @@ public function autoRichSnippet()
}

$product = [
'availability' => $productAvailability,
'price' => $current_product->getFinalPrice(),
'url' => $current_product->getProductUrl(),
'description' => $current_product->getMetaDescription(),
'availability' => $productAvailability,
'price' => $current_product->getFinalPrice(),
'url' => $current_product->getProductUrl(),
'description' => $current_product->getMetaDescription(),
'mpn' => ($current_product->hasData('mpn') ? $current_product->getData('mpn') : $current_product->getSku()),
'priceCurrency' => $this->store->getDefaultCurrencyCode(),
'brand' => ($current_product->hasData('manufacturer') ? $current_product->getAttributeText('manufacturer') : ($current_product->hasData('brand') ? $current_product->getAttributeText('brand') : 'Not Available')),
];

return $this->getRichSnippet($sku, $product);
} else if ($merchant_enabled) {
} elseif ($merchant_enabled) {
return $this->getRichSnippet();
}

return '';
}

@@ -78,9 +77,9 @@ public function getRichSnippet($sku = null, $product = null)
$sku = implode(';', $sku);
}

$region = $this->configHelper->getRegion($this->store->getId());
$region = $this->configHelper->getRegion($this->store->getId());
$storeName = $this->configHelper->getStoreId($this->store->getId());
$url = $region == 'us' ? 'https://widget.reviews.io/rich-snippet/dist.js' : 'https://widget.reviews.co.uk/rich-snippet/dist.js';
$url = $region == 'us' ? 'https://widget.reviews.io/rich-snippet/dist.js' : 'https://widget.reviews.co.uk/rich-snippet/dist.js';

$output = '<script src="' . $url . '"></script>';
$output .= '<script>
Loading

0 comments on commit 60ecd72

Please sign in to comment.