141 lines
4.0 KiB
PHP
Executable File
141 lines
4.0 KiB
PHP
Executable File
<?php
|
|
|
|
namespace Mageplaza\Seo\Observer;
|
|
|
|
use Magento\Framework\Event\ObserverInterface;
|
|
use Magento\Framework\View\Page\Config;
|
|
use Magento\Framework\Event\Observer;
|
|
use Magento\Framework\ObjectManagerInterface;
|
|
|
|
class DuplicateContent implements ObserverInterface
|
|
{
|
|
|
|
|
|
/**
|
|
* @var \Magento\Framework\View\Page\Config
|
|
*/
|
|
protected $page;
|
|
|
|
/**
|
|
* @var \Psr\Log\LoggerInterface
|
|
*/
|
|
protected $logger;
|
|
|
|
/**
|
|
* @var \Magento\Framework\ObjectManagerInterface
|
|
*/
|
|
protected $objectManager;
|
|
|
|
/**
|
|
* DuplicateContent constructor.
|
|
* @param \Magento\Framework\View\Page\Config $page
|
|
* @param \Psr\Log\LoggerInterface $logger
|
|
* @param \Magento\Framework\ObjectManagerInterface $objectManager
|
|
*/
|
|
public function __construct(
|
|
Config $page,
|
|
\Psr\Log\LoggerInterface $logger,
|
|
ObjectManagerInterface $objectManager
|
|
) {
|
|
|
|
$this->page = $page;
|
|
$this->logger = $logger;
|
|
$this->objectManager = $objectManager;
|
|
}
|
|
|
|
/**
|
|
* Ref: https://support.google.com/webmasters/answer/139066?hl=en
|
|
* @param \Magento\Framework\Event\Observer $observer
|
|
*/
|
|
public function execute(Observer $observer)
|
|
{
|
|
/* @var \Magento\Framework\View\LayoutInterface $layout */
|
|
$layout = $observer->getEvent()->getLayout();
|
|
$action = $observer->getEvent()->getFullActionName();
|
|
|
|
$canonicalUrl = '';
|
|
|
|
|
|
switch ($action) {
|
|
|
|
/**
|
|
* product
|
|
* http://example.com/dresses/cocktail?gclid=ABCD get consolidated with
|
|
* links to https://www.example.com/dresses/green/greendress.html
|
|
*/
|
|
case 'catalog_product_view':
|
|
$canonicalUrl = $this->objectManager->create(
|
|
'Mageplaza\Seo\Model\Canonical\Product'
|
|
)->getCanonicalUrl();
|
|
|
|
break;
|
|
|
|
/**
|
|
* Category issues
|
|
* category.html?p=2 or category.html?p=2&product_list_order=name
|
|
* cocktail.html?gclid=ABCD
|
|
*
|
|
* Params:
|
|
* - product_list_order
|
|
* - product_list_mode
|
|
* - p
|
|
*/
|
|
case 'catalog_category_view':
|
|
$canonicalUrl = $this->objectManager->create(
|
|
'Mageplaza\Seo\Model\Canonical\Category'
|
|
)->getCanonicalUrl();
|
|
break;
|
|
|
|
/**
|
|
* LN issue: TODO
|
|
* @truongnh
|
|
*/
|
|
case 'catalog_category_view_type_default':
|
|
break;
|
|
|
|
default:
|
|
$urlObject = $this->objectManager
|
|
->get('Magento\Framework\UrlInterface');
|
|
|
|
$canonicalUrl = $urlObject->getCurrentUrl();
|
|
|
|
/**
|
|
* clean up param:
|
|
* - layered navigation: ?price=200-300
|
|
* - category: ?p=2
|
|
*/
|
|
$position = strpos($canonicalUrl, '?');
|
|
if ($position !== false) {
|
|
$canonicalUrl = substr($canonicalUrl, 0, $position);
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
if ($canonicalUrl) {
|
|
$this->page->addRemotePageAsset(
|
|
$canonicalUrl,
|
|
$contentType = 'canonical',
|
|
$properties = ['attributes' =>
|
|
['rel' => 'canonical']
|
|
],
|
|
$name = 'Canonical link tag generated by Mageplaza_Seo'
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get block by name
|
|
*
|
|
* @param string $name
|
|
* @param \Magento\Framework\View\LayoutInterface $layout
|
|
* @return \Magento\Framework\View\Element\AbstractBlock|bool
|
|
*/
|
|
public function getBlock($name, $layout)
|
|
{
|
|
$blocks = $layout->getAllBlocks();
|
|
|
|
return isset($blocks[$name]) ? $blocks[$name] : false;
|
|
}
|
|
}
|