Skip to content
This repository has been archived by the owner on Feb 8, 2023. It is now read-only.

Commit

Permalink
Merge pull request #6 from mageplaza/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
haitv282 committed Jul 25, 2019
2 parents c9b4316 + 772db30 commit 6ed480b
Show file tree
Hide file tree
Showing 14 changed files with 173 additions and 79 deletions.
60 changes: 41 additions & 19 deletions Block/Sitemap.php
Expand Up @@ -28,7 +28,9 @@
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
use Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection;
use Magento\CatalogInventory\Helper\Stock;
use Magento\Cms\Model\Page;
use Magento\Cms\Model\ResourceModel\Page\Collection as PageCollection;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Mageplaza\Sitemap\Helper\Data as HelperConfig;
Expand All @@ -42,22 +44,22 @@ class Sitemap extends Template
const DEFAULT_PRODUCT_LIMIT = 100;

/**
* @var \Magento\Catalog\Helper\Category
* @var Category
*/
protected $_categoryHelper;

/**
* @var \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory
* @var CollectionFactory
*/
protected $_categoryCollection;

/**
* @var \Magento\Catalog\Model\ResourceModel\Category\Collection
* @var Collection
*/
protected $collection;

/**
* @var \Magento\Catalog\Model\CategoryRepository
* @var CategoryRepository
*/
protected $categoryRepository;

Expand All @@ -67,22 +69,22 @@ class Sitemap extends Template
protected $_helper;

/**
* @var \Magento\CatalogInventory\Helper\Stock
* @var Stock
*/
protected $_stockFilter;

/**
* @var \Magento\Catalog\Model\Product\Visibility
* @var ProductVisibility
*/
protected $productVisibility;

/**
* @var \Magento\Catalog\Model\ResourceModel\Product\Collection
* @var ProductCollection
*/
protected $productCollection;

/**
* @var \Magento\Cms\Model\ResourceModel\Page\Collection
* @var PageCollection
*/
protected $pageCollection;

Expand Down Expand Up @@ -131,7 +133,7 @@ public function __construct(
*/
public function getProductCollection()
{
$limit = $this->_helper->getProductLimit() ? $this->_helper->getProductLimit() : self::DEFAULT_PRODUCT_LIMIT;
$limit = $this->_helper->getProductLimit() ?: self::DEFAULT_PRODUCT_LIMIT;
$collection = $this->productCollection
->setVisibility($this->productVisibility->getVisibleInCatalogIds())
->addMinimalPrice()
Expand All @@ -150,14 +152,14 @@ public function getProductCollection()
*/
public function getCategoryCollection()
{
return $this->_categoryHelper->getStoreCategories(false, true, true);
return $this->_categoryHelper->getStoreCategories(false, true);
}

/**
* @param $categoryId
*
* @return string
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @throws NoSuchEntityException
*/
public function getCategoryUrl($categoryId)
{
Expand All @@ -170,7 +172,7 @@ public function getCategoryUrl($categoryId)
*/
public function getPageCollection()
{
return $this->pageCollection->addFieldToFilter('is_active', \Magento\Cms\Model\Page::STATUS_ENABLED)
return $this->pageCollection->addFieldToFilter('is_active', Page::STATUS_ENABLED)
->addFieldToFilter('identifier', [
'nin' => $this->getExcludedPages()
]);
Expand Down Expand Up @@ -228,7 +230,7 @@ public function renderLinkElement($link, $title)
* @param $collection
*
* @return string
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @throws NoSuchEntityException
*/
public function renderSection($section, $config, $title, $collection)
{
Expand All @@ -245,7 +247,7 @@ public function renderSection($section, $config, $title, $collection)
break;
case 'page':
if (in_array($item->getIdentifier(), $this->getExcludedPages())) {
continue;
continue 2;
}
$html .= $this->renderLinkElement($this->getUrl($item->getIdentifier()), $item->getTitle());
break;
Expand All @@ -267,15 +269,35 @@ public function renderSection($section, $config, $title, $collection)

/**
* @return string
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @throws NoSuchEntityException
*/
public function renderHtmlSitemap()
{
$htmlSitemap = '';
$htmlSitemap .= $this->renderSection('category', $this->_helper->isEnableCategorySitemap(), 'Categories', $this->getCategoryCollection());
$htmlSitemap .= $this->renderSection('page', $this->_helper->iisEnablePageSitemap(), 'Pages', $this->getPageCollection());
$htmlSitemap .= $this->renderSection('product', $this->_helper->isEnableProductSitemap(), 'Products', $this->getProductCollection());
$htmlSitemap .= $this->renderSection('link', $this->_helper->isEnableAddLinksSitemap(), 'Additional links', $this->getAdditionLinksCollection());
$htmlSitemap .= $this->renderSection(
'category',
$this->_helper->isEnableCategorySitemap(),
'Categories',
$this->getCategoryCollection()
);
$htmlSitemap .= $this->renderSection(
'page',
$this->_helper->isEnablePageSitemap(),
'Pages',
$this->getPageCollection()
);
$htmlSitemap .= $this->renderSection(
'product',
$this->_helper->isEnableProductSitemap(),
'Products',
$this->getProductCollection()
);
$htmlSitemap .= $this->renderSection(
'link',
$this->_helper->isEnableAddLinksSitemap(),
'Additional links',
$this->getAdditionLinksCollection()
);

return $htmlSitemap;
}
Expand Down
7 changes: 4 additions & 3 deletions Controller/Index/Index.php
Expand Up @@ -24,6 +24,7 @@
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\Exception\NotFoundException;
use Magento\Framework\View\Result\Page;
use Magento\Framework\View\Result\PageFactory;
use Mageplaza\Sitemap\Helper\Data as HelperConfig;

Expand All @@ -34,7 +35,7 @@
class Index extends Action
{
/**
* @var \Magento\Framework\View\Result\PageFactory
* @var PageFactory
*/
protected $pageFactory;

Expand All @@ -59,8 +60,8 @@ public function __construct(Context $context, PageFactory $pageFactory, HelperCo
}

/**
* @return \Magento\Framework\View\Result\Page
* @throws \Magento\Framework\Exception\NotFoundException
* @return Page
* @throws NotFoundException
*/
public function execute()
{
Expand Down
12 changes: 9 additions & 3 deletions Helper/Data.php
Expand Up @@ -52,7 +52,10 @@ public function isEnableHtmlSiteMap($storeId = null)
*/
public function getHtmlSitemapConfig($code, $storeId = null)
{
return $this->getConfigValue(self::CONFIG_MODULE_PATH . '/' . self::HTML_SITEMAP_CONFIGUARATION . $code, $storeId);
return $this->getConfigValue(
self::CONFIG_MODULE_PATH . '/' . self::HTML_SITEMAP_CONFIGUARATION . $code,
$storeId
);
}

/**
Expand All @@ -68,7 +71,7 @@ public function isEnableCategorySitemap()
* Is enable page site map
* @return mixed
*/
public function iisEnablePageSitemap()
public function isEnablePageSitemap()
{
return $this->getHtmlSitemapConfig('page');
}
Expand Down Expand Up @@ -136,7 +139,10 @@ public function getProductLimit()
*/
public function getXmlSitemapConfig($code, $storeId = null)
{
return $this->getConfigValue(self::CONFIG_MODULE_PATH . '/' . self::XML_SITEMAP_CONFIGUARATION . $code, $storeId);
return $this->getConfigValue(
self::CONFIG_MODULE_PATH . '/' . self::XML_SITEMAP_CONFIGUARATION . $code,
$storeId
);
}

/**
Expand Down
62 changes: 31 additions & 31 deletions Model/Sitemap.php
Expand Up @@ -21,14 +21,17 @@

namespace Mageplaza\Sitemap\Model;

use Exception;
use Magento\Catalog\Model\CategoryFactory;
use Magento\Catalog\Model\ProductFactory;
use Magento\CatalogInventory\Model\Stock\Item;
use Magento\Cms\Model\PageFactory;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Data\Collection\AbstractDb;
use Magento\Framework\DataObject;
use Magento\Framework\Escaper;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Filesystem;
use Magento\Framework\Model\Context;
use Magento\Framework\Model\ResourceModel\AbstractResource;
Expand All @@ -55,17 +58,17 @@ class Sitemap extends CoreSitemap
const HOMEPAGE_PATH = 'web/default/cms_home_page';

/**
* @var \Magento\Catalog\Model\CategoryFactory
* @var CategoryFactory
*/
protected $_coreCategoryFactory;

/**
* @var \Magento\Catalog\Model\ProductFactory
* @var ProductFactory
*/
protected $_coreProductFactory;

/**
* @var \Magento\Cms\Model\PageFactory
* @var PageFactory
*/
protected $_corePageFactory;

Expand All @@ -75,7 +78,7 @@ class Sitemap extends CoreSitemap
protected $helperConfig;

/**
* @var \Magento\CatalogInventory\Model\Stock\Item
* @var Item
*/
protected $stockItem;

Expand Down Expand Up @@ -159,7 +162,7 @@ public function _initSitemapItems()
$helper = $this->_sitemapData;
$storeId = $this->getStoreId();
$this->_sitemapItems = null;
$this->_sitemapItems[] = new \Magento\Framework\DataObject(
$this->_sitemapItems[] = new DataObject(
[
'changefreq' => $helper->getCategoryChangefreq($storeId),
'priority' => $helper->getCategoryPriority($storeId),
Expand All @@ -168,7 +171,7 @@ public function _initSitemapItems()
]
);

$this->_sitemapItems[] = new \Magento\Framework\DataObject(
$this->_sitemapItems[] = new DataObject(
[
'changefreq' => $helper->getProductChangefreq($storeId),
'priority' => $helper->getProductPriority($storeId),
Expand All @@ -177,7 +180,7 @@ public function _initSitemapItems()
]
);

$this->_sitemapItems[] = new \Magento\Framework\DataObject(
$this->_sitemapItems[] = new DataObject(
[
'changefreq' => $helper->getPageChangefreq($storeId),
'priority' => $helper->getPagePriority($storeId),
Expand All @@ -187,7 +190,7 @@ public function _initSitemapItems()
);

if ($this->helperConfig->isEnableAdditionalLinks($storeId)) {
$this->_sitemapItems[] = new \Magento\Framework\DataObject(
$this->_sitemapItems[] = new DataObject(
[
'changefreq' => $this->helperConfig->getFrequency($storeId),
'priority' => $this->helperConfig->getPriority($storeId),
Expand All @@ -200,13 +203,13 @@ public function _initSitemapItems()

/**
* @return $this
* @throws \Exception
* @throws \Magento\Framework\Exception\LocalizedException
* @throws Exception
* @throws LocalizedException
*/
public function generateXml()
{
$this->_initSitemapItems();
/** @var $sitemapItem \Magento\Framework\DataObject */
/** @var $sitemapItem DataObject */
foreach ($this->_sitemapItems as $item) {
$changefreq = $item->getChangefreq();
$priority = $item->getPriority();
Expand Down Expand Up @@ -236,12 +239,8 @@ public function generateXml()

if ($this->_sitemapIncrement == 1) {
// In case when only one increment file was created use it as default sitemap
$path = rtrim(
$this->getSitemapPath(),
'/'
) . '/' . $this->_getCurrentSitemapFilename(
$this->_sitemapIncrement
);
$path = rtrim($this->getSitemapPath(), '/') . '/'
. $this->_getCurrentSitemapFilename($this->_sitemapIncrement);
$destination = rtrim($this->getSitemapPath(), '/') . '/' . $this->getSitemapFilename();

$this->_directory->renameFile($path, $destination);
Expand Down Expand Up @@ -273,8 +272,14 @@ public function generateXml()
*
* @return string
*/
protected function getSitemapRow($url, $urlType, $lastmod = null, $changefreq = null, $priority = null, $images = null)
{
protected function getSitemapRow(
$url,
$urlType,
$lastmod = null,
$changefreq = null,
$priority = null,
$images = null
) {
if ($urlType == self::URL) {
$url = $this->_getUrl($url);
} else {
Expand Down Expand Up @@ -304,9 +309,8 @@ protected function getSitemapRow($url, $urlType, $lastmod = null, $changefreq =
// Add PageMap image for Google web search
$row .= '<PageMap xmlns="http://www.google.com/schemas/sitemap-pagemap/1.0"><DataObject type="thumbnail">';
$row .= '<Attribute name="name" value="' . htmlspecialchars($images->getTitle()) . '"/>';
$row .= '<Attribute name="src" value="' . htmlspecialchars(
$this->_getMediaUrl($images->getThumbnail())
) . '"/>';
$row .= '<Attribute name="src" value="' . htmlspecialchars($this->_getMediaUrl($images->getThumbnail()))
. '"/>';
$row .= '</DataObject></PageMap>';
}

Expand All @@ -325,8 +329,8 @@ public function getLinkCollectionAdded($storeId)
$id = 1;
$collection = [];
foreach ($this->helperConfig->getXmlAdditionalLinks($storeId) as $item) {
if ($item != null) {
$obj = ObjectManager::getInstance()->create('\Magento\Framework\DataObject');
if ($item !== null) {
$obj = ObjectManager::getInstance()->create(\Magento\Framework\DataObject::class);
$obj->setData('id', $id++);
$obj->setData('url', $item);
$obj->setData('updated_at', $this->getSitemapTime());
Expand All @@ -347,6 +351,7 @@ public function getLinkCollectionAdded($storeId)
public function _getCategoryCollection($storeId)
{
$collection = [];

foreach ($this->_categoryFactory->create()->getCollection($storeId) as $item) {
if ($this->_coreCategoryFactory->create()->load($item->getId())->getData('mp_exclude_sitemap') == 1) {
continue;
Expand Down Expand Up @@ -428,12 +433,7 @@ public function convertUrl($url)
*/
public function optimizeHomepage($storeId, $page)
{
if ($this->helperConfig->isEnableHomepageOptimization($storeId) == 1) {
if ($this->helperConfig->getConfigValue(self::HOMEPAGE_PATH, $storeId) == $page->getUrl()) {
return true;
}
}

return false;
return $this->helperConfig->isEnableHomepageOptimization($storeId) == 1
&& $this->helperConfig->getConfigValue(self::HOMEPAGE_PATH, $storeId) == $page->getUrl();
}
}

0 comments on commit 6ed480b

Please sign in to comment.