From 27636a39d9b7e6ab582aef33f045b7978976604a Mon Sep 17 00:00:00 2001 From: bdtrungit Date: Mon, 18 Feb 2019 16:57:37 +0700 Subject: [PATCH 1/3] update mp_exclude_sitemap in cms_page table --- Setup/UpgradeSchema.php | 53 +++++++++++++++++++++++++++++++++++++++++ etc/module.xml | 2 +- 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 Setup/UpgradeSchema.php diff --git a/Setup/UpgradeSchema.php b/Setup/UpgradeSchema.php new file mode 100644 index 0000000..763374a --- /dev/null +++ b/Setup/UpgradeSchema.php @@ -0,0 +1,53 @@ +startSetup(); + $connection = $installer->getConnection(); + + if (version_compare($context->getVersion(), '2.0.1', '<')) { + if ($installer->tableExists('cms_page')) { + $connection->modifyColumn($installer->getTable('cms_page'), 'mp_exclude_sitemap', ['type' => Table::TYPE_INTEGER, 'nullable' => true]); + } + } + + $installer->endSetup(); + } +} diff --git a/etc/module.xml b/etc/module.xml index 3d01400..17dcbc0 100755 --- a/etc/module.xml +++ b/etc/module.xml @@ -21,7 +21,7 @@ */ --> - + From 6d09f8069592ae80168b85f85590614b8063b539 Mon Sep 17 00:00:00 2001 From: prime Date: Tue, 2 Jul 2019 09:29:55 +0700 Subject: [PATCH 2/3] validate limit product --- etc/adminhtml/system.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index cf72aac..c3ed08a 100755 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -46,6 +46,7 @@ + validate-digits validate-not-negative-number 1 From 772db30644a511859f18b1585177f80df8bd6765 Mon Sep 17 00:00:00 2001 From: Brian Date: Thu, 25 Jul 2019 10:53:58 +0700 Subject: [PATCH 3/3] Clean code --- Block/Sitemap.php | 60 ++++++++++++++++++-------- Controller/Index/Index.php | 7 +-- Helper/Data.php | 12 ++++-- Model/Sitemap.php | 62 +++++++++++++-------------- Model/Source/Page.php | 16 ++++--- Setup/InstallData.php | 16 +++---- Setup/InstallSchema.php | 4 +- Setup/UpgradeSchema.php | 6 ++- composer.json | 4 +- i18n/en_US.csv | 3 ++ registration.php | 6 ++- view/frontend/templates/sitemap.phtml | 2 +- 12 files changed, 119 insertions(+), 79 deletions(-) diff --git a/Block/Sitemap.php b/Block/Sitemap.php index c92314a..c5e73f7 100755 --- a/Block/Sitemap.php +++ b/Block/Sitemap.php @@ -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; @@ -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; @@ -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; @@ -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() @@ -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) { @@ -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() ]); @@ -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) { @@ -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; @@ -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; } diff --git a/Controller/Index/Index.php b/Controller/Index/Index.php index 36901fd..169152d 100755 --- a/Controller/Index/Index.php +++ b/Controller/Index/Index.php @@ -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; @@ -34,7 +35,7 @@ class Index extends Action { /** - * @var \Magento\Framework\View\Result\PageFactory + * @var PageFactory */ protected $pageFactory; @@ -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() { diff --git a/Helper/Data.php b/Helper/Data.php index 448b58a..c6d9e01 100755 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -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 + ); } /** @@ -68,7 +71,7 @@ public function isEnableCategorySitemap() * Is enable page site map * @return mixed */ - public function iisEnablePageSitemap() + public function isEnablePageSitemap() { return $this->getHtmlSitemapConfig('page'); } @@ -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 + ); } /** diff --git a/Model/Sitemap.php b/Model/Sitemap.php index 43a04f3..fa06571 100755 --- a/Model/Sitemap.php +++ b/Model/Sitemap.php @@ -21,6 +21,7 @@ namespace Mageplaza\Sitemap\Model; +use Exception; use Magento\Catalog\Model\CategoryFactory; use Magento\Catalog\Model\ProductFactory; use Magento\CatalogInventory\Model\Stock\Item; @@ -28,7 +29,9 @@ 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; @@ -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; @@ -75,7 +78,7 @@ class Sitemap extends CoreSitemap protected $helperConfig; /** - * @var \Magento\CatalogInventory\Model\Stock\Item + * @var Item */ protected $stockItem; @@ -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), @@ -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), @@ -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), @@ -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), @@ -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(); @@ -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); @@ -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 { @@ -304,9 +309,8 @@ protected function getSitemapRow($url, $urlType, $lastmod = null, $changefreq = // Add PageMap image for Google web search $row .= ''; $row .= ''; - $row .= ''; + $row .= ''; $row .= ''; } @@ -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()); @@ -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; @@ -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(); } } diff --git a/Model/Source/Page.php b/Model/Source/Page.php index fdecaa5..ee25d42 100755 --- a/Model/Source/Page.php +++ b/Model/Source/Page.php @@ -21,7 +21,8 @@ namespace Mageplaza\Sitemap\Model\Source; -use Magento\Cms\Model\PageFactory; +use Magento\Cms\Model\ResourceModel\Page\Collection; +use Magento\Cms\Model\ResourceModel\Page\CollectionFactory; /** * Class Page @@ -30,18 +31,18 @@ class Page { /** - * @var \Magento\Cms\Model\PageFactory + * @var CollectionFactory */ - protected $_pageFactory; + protected $_pageCollectionFactory; /** * Page constructor. * - * @param \Magento\Cms\Model\PageFactory $pageFactory + * @param CollectionFactory $pageCollectionFactory */ - public function __construct(PageFactory $pageFactory) + public function __construct(CollectionFactory $pageCollectionFactory) { - $this->_pageFactory = $pageFactory; + $this->_pageCollectionFactory = $pageCollectionFactory; } /** @@ -51,7 +52,8 @@ public function __construct(PageFactory $pageFactory) public function toOptionArray() { $options = []; - $collection = $this->_pageFactory->create()->getCollection(); + /** @var Collection $collection */ + $collection = $this->_pageCollectionFactory->create(); foreach ($collection as $item) { $options[] = ['value' => $item->getIdentifier(), 'label' => $item->getTitle()]; } diff --git a/Setup/InstallData.php b/Setup/InstallData.php index 7cb6110..339dec0 100755 --- a/Setup/InstallData.php +++ b/Setup/InstallData.php @@ -37,20 +37,20 @@ class InstallData implements InstallDataInterface { /** - * @var \Magento\Catalog\Setup\CategorySetupFactory + * @var CategorySetupFactory */ protected $categorySetupFactory; /** - * @var \Magento\Eav\Setup\EavSetupFactory + * @var EavSetupFactory */ protected $eavSetupFactory; /** * InstallData constructor. * - * @param \Magento\Eav\Setup\EavSetupFactory $eavSetupFactory - * @param \Magento\Catalog\Setup\CategorySetupFactory $categorySetupFactory + * @param EavSetupFactory $eavSetupFactory + * @param CategorySetupFactory $categorySetupFactory */ public function __construct( EavSetupFactory $eavSetupFactory, @@ -61,8 +61,8 @@ public function __construct( } /** - * @param \Magento\Framework\Setup\ModuleDataSetupInterface $setup - * @param \Magento\Framework\Setup\ModuleContextInterface $context + * @param ModuleDataSetupInterface $setup + * @param ModuleContextInterface $context */ public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { @@ -81,7 +81,7 @@ public function install(ModuleDataSetupInterface $setup, ModuleContextInterface 'note' => 'Added by Mageplaza Sitemap', 'input' => 'select', 'class' => '', - 'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean', + 'source' => \Magento\Eav\Model\Entity\Attribute\Source\Boolean::class, 'global' => ScopedAttributeInterface::SCOPE_GLOBAL, 'visible' => true, 'required' => false, @@ -108,7 +108,7 @@ public function install(ModuleDataSetupInterface $setup, ModuleContextInterface 'type' => 'int', 'label' => '', 'input' => 'select', - 'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean', + 'source' => \Magento\Eav\Model\Entity\Attribute\Source\Boolean::class, 'required' => false, 'sort_order' => 100, 'global' => ScopedAttributeInterface::SCOPE_STORE, diff --git a/Setup/InstallSchema.php b/Setup/InstallSchema.php index d97228f..0e00bfd 100755 --- a/Setup/InstallSchema.php +++ b/Setup/InstallSchema.php @@ -33,8 +33,8 @@ class InstallSchema implements InstallSchemaInterface { /** - * @param \Magento\Framework\Setup\SchemaSetupInterface $setup - * @param \Magento\Framework\Setup\ModuleContextInterface $context + * @param SchemaSetupInterface $setup + * @param ModuleContextInterface $context */ public function install( SchemaSetupInterface $setup, diff --git a/Setup/UpgradeSchema.php b/Setup/UpgradeSchema.php index 763374a..3ef4615 100644 --- a/Setup/UpgradeSchema.php +++ b/Setup/UpgradeSchema.php @@ -44,7 +44,11 @@ public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $con if (version_compare($context->getVersion(), '2.0.1', '<')) { if ($installer->tableExists('cms_page')) { - $connection->modifyColumn($installer->getTable('cms_page'), 'mp_exclude_sitemap', ['type' => Table::TYPE_INTEGER, 'nullable' => true]); + $connection->modifyColumn( + $installer->getTable('cms_page'), + 'mp_exclude_sitemap', + ['type' => Table::TYPE_INTEGER, 'nullable' => true] + ); } } diff --git a/composer.json b/composer.json index 4d297a3..cde7b39 100755 --- a/composer.json +++ b/composer.json @@ -2,11 +2,11 @@ "name": "mageplaza/module-sitemap", "description": "Magento 2 Google XML Sitemap extension", "require": { - "mageplaza/module-core": "^1.4.0", + "mageplaza/module-core": "^1.4.4", "mageplaza/magento-2-seo-extension": "^2.1.0" }, "type": "magento2-module", - "version": "1.0.1", + "version": "1.0.2", "license": "proprietary", "authors": [ { diff --git a/i18n/en_US.csv b/i18n/en_US.csv index 8afde3a..9505297 100755 --- a/i18n/en_US.csv +++ b/i18n/en_US.csv @@ -15,8 +15,11 @@ "Homepage Optimization","Homepage Optimization" "In sitemap file, it will remove the link of the CMS page using for homepage.","In sitemap file, it will remove the link of the CMS page using for homepage." "Yes, if you want to add more custom links into sitemap XML file.","Yes, if you want to add more custom links into sitemap XML file." +"One link per line.","One link per line." Frequency,Frequency +"One of the following value: always, hourly, daily, weekly, monthly, yearly, never. Learn more.","One of the following value: always, hourly, daily, weekly, monthly, yearly, never. Learn more." Priority,Priority +"From 0 to 1. E.g: 0.5 or 0.8","From 0 to 1. E.g: 0.5 or 0.8" "Exclude Sitemap","Exclude Sitemap" "Added by Mageplaza Sitemap","Added by Mageplaza Sitemap" Sitemap,Sitemap diff --git a/registration.php b/registration.php index dd037a3..65471ea 100755 --- a/registration.php +++ b/registration.php @@ -19,8 +19,10 @@ * @license https://www.mageplaza.com/LICENSE.txt */ -\Magento\Framework\Component\ComponentRegistrar::register( - \Magento\Framework\Component\ComponentRegistrar::MODULE, +use Magento\Framework\Component\ComponentRegistrar; + +ComponentRegistrar::register( + ComponentRegistrar::MODULE, 'Mageplaza_Sitemap', __DIR__ ); diff --git a/view/frontend/templates/sitemap.phtml b/view/frontend/templates/sitemap.phtml index e50e7d3..5fad0f3 100755 --- a/view/frontend/templates/sitemap.phtml +++ b/view/frontend/templates/sitemap.phtml @@ -23,6 +23,6 @@ ?>
- renderHtmlSiteMap(); ?> + renderHtmlSiteMap() ?>