Robots.php 3.94 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Sitemap\Block;

use Magento\Framework\DataObject\IdentityInterface;
use Magento\Framework\View\Element\AbstractBlock;
use Magento\Framework\View\Element\Context;
use Magento\Robots\Model\Config\Value;
use Magento\Sitemap\Helper\Data as SitemapHelper;
use Magento\Sitemap\Model\ResourceModel\Sitemap\CollectionFactory;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Store\Model\StoreResolver;

/**
 * Prepares sitemap links to add to the robots.txt file
 *
 * @api
 * @since 100.1.5
 */
class Robots extends AbstractBlock implements IdentityInterface
{
    /**
     * @var CollectionFactory
     */
    private $sitemapCollectionFactory;

    /**
     * @var SitemapHelper
     */
    private $sitemapHelper;

    /**
     * @var StoreManagerInterface
     */
    private $storeManager;

    /**
     * @param Context $context
     * @param StoreResolver $storeResolver
     * @param CollectionFactory $sitemapCollectionFactory
     * @param SitemapHelper $sitemapHelper
     * @param StoreManagerInterface $storeManager
     * @param array $data
     *
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function __construct(
        Context $context,
        StoreResolver $storeResolver,
        CollectionFactory $sitemapCollectionFactory,
        SitemapHelper $sitemapHelper,
        StoreManagerInterface $storeManager,
        array $data = []
    ) {
        $this->sitemapCollectionFactory = $sitemapCollectionFactory;
        $this->sitemapHelper = $sitemapHelper;
        $this->storeManager = $storeManager;

        parent::__construct($context, $data);
    }

    /**
     * Prepare sitemap links to add to the robots.txt file
     *
     * Collects sitemap links for all stores of given website.
     * Detects if sitemap file information is required to be added to robots.txt
     * and adds links for this sitemap files into result data.
     *
     * @return string
     * @since 100.1.5
     */
    protected function _toHtml()
    {
        $defaultStore = $this->storeManager->getDefaultStoreView();

        /** @var \Magento\Store\Model\Website $website */
        $website = $this->storeManager->getWebsite($defaultStore->getWebsiteId());

        $storeIds = [];
        foreach ($website->getStoreIds() as $storeId) {
            if ((bool)$this->sitemapHelper->getEnableSubmissionRobots($storeId)) {
                $storeIds[] = (int)$storeId;
            }
        }

        $links = [];
        if ($storeIds) {
            $links = array_merge($links, $this->getSitemapLinks($storeIds));
        }

        return $links ? implode(PHP_EOL, $links) . PHP_EOL : '';
    }

    /**
     * Retrieve sitemap links for given store
     *
     * Gets the names of sitemap files that linked with given store,
     * and adds links for this sitemap files into result array.
     *
     * @param int[] $storeIds
     * @return array
     * @since 100.1.5
     */
    protected function getSitemapLinks(array $storeIds)
    {
        $sitemapLinks = [];

        /** @var \Magento\Sitemap\Model\ResourceModel\Sitemap\Collection $collection */
        $collection = $this->sitemapCollectionFactory->create();
        $collection->addStoreFilter($storeIds);

        foreach ($collection as $sitemap) {
            /** @var \Magento\Sitemap\Model\Sitemap $sitemap */
            $sitemapFilename = $sitemap->getSitemapFilename();
            $sitemapPath = $sitemap->getSitemapPath();

            $sitemapUrl = $sitemap->getSitemapUrl($sitemapPath, $sitemapFilename);
            $sitemapLinks[$sitemapUrl] = 'Sitemap: ' . $sitemapUrl;
        }

        return $sitemapLinks;
    }

    /**
     * Get unique page cache identities
     *
     * @return array
     * @since 100.1.5
     */
    public function getIdentities()
    {
        return [
            Value::CACHE_TAG . '_' . $this->storeManager->getDefaultStoreView()->getId(),
        ];
    }
}