/** * Invalidate full page and block cache * * @param \Magento\Framework\Event\Observer $observer * @return void * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function execute(\Magento\Framework\Event\Observer $observer) { if ($this->_config->isEnabled()) { $this->_typeList->invalidate(\Magento\PageCache\Model\Cache\Type::TYPE_IDENTIFIER); } $this->_typeList->invalidate(\Magento\Framework\App\Cache\Type\Block::TYPE_IDENTIFIER); }
/** * @param AbstractAction $subject * @param AbstractAction $result * @return AbstractAction * * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function afterExecute(AbstractAction $subject, AbstractAction $result) { if ($this->config->isEnabled()) { $this->typeList->invalidate('full_page'); } return $result; }
/** * Whether a cache type is enabled */ public function testIsEnabled() { $this->_cacheState->expects($this->at(0))->method('isEnabled')->with(\Magento\PageCache\Model\Cache\Type::TYPE_IDENTIFIER)->will($this->returnValue(true)); $this->_cacheState->expects($this->at(1))->method('isEnabled')->with(\Magento\PageCache\Model\Cache\Type::TYPE_IDENTIFIER)->will($this->returnValue(false)); $this->assertTrue($this->_model->isEnabled()); $this->assertFalse($this->_model->isEnabled()); }
/** * @param Observer $observer * @return void * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function execute(Observer $observer) { if ($this->moduleManager->isEnabled('Magento_PageCache') && $this->cacheConfig->isEnabled() && $this->taxHelper->isCatalogPriceDisplayAffectedByTax()) { /** @var \Magento\Customer\Model\Data\Customer $customer */ $customer = $observer->getData('customer'); $customerGroupId = $customer->getGroupId(); $customerGroup = $this->groupRepository->getById($customerGroupId); $customerTaxClassId = $customerGroup->getTaxClassId(); $this->customerSession->setCustomerTaxClassId($customerTaxClassId); /** @var \Magento\Customer\Api\Data\AddressInterface[] $addresses */ $addresses = $customer->getAddresses(); if (isset($addresses)) { $defaultShippingFound = false; $defaultBillingFound = false; foreach ($addresses as $address) { if ($address->isDefaultBilling()) { $defaultBillingFound = true; $this->customerSession->setDefaultTaxBillingAddress(['country_id' => $address->getCountryId(), 'region_id' => $address->getRegion() ? $address->getRegion()->getRegionId() : null, 'postcode' => $address->getPostcode()]); } if ($address->isDefaultShipping()) { $defaultShippingFound = true; $this->customerSession->setDefaultTaxShippingAddress(['country_id' => $address->getCountryId(), 'region_id' => $address->getRegion() ? $address->getRegion()->getRegionId() : null, 'postcode' => $address->getPostcode()]); } if ($defaultShippingFound && $defaultBillingFound) { break; } } } } }
/** * After generate Xml * * @param \Magento\Framework\View\LayoutInterface $subject * @param \Magento\Framework\View\LayoutInterface $result * @return \Magento\Framework\View\LayoutInterface */ public function afterGenerateXml(\Magento\Framework\View\LayoutInterface $subject, $result) { if ($this->moduleManager->isEnabled('Magento_PageCache') && $this->cacheConfig->isEnabled() && !$this->request->isAjax() && $subject->isCacheable()) { $this->checkoutSession->clearStorage(); } return $result; }
/** * Invalidate full page cache * * @return void */ public function execute() { if ($this->_config->isEnabled()) { $this->_typeList->invalidate('full_page'); } return $this; }
/** * Is full page cache enabled * * @return bool */ protected function isFullPageCacheEnabled() { if ($this->isFullPageCacheEnabled === null) { $this->isFullPageCacheEnabled = $this->_config->isEnabled(); } return $this->isFullPageCacheEnabled; }
/** * @param \Magento\Catalog\Model\Resource\Attribute $subject * @param callable $proceed * @param \Magento\Framework\Model\AbstractModel $attribute * @return mixed * * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function aroundSave(\Magento\Catalog\Model\Resource\Attribute $subject, \Closure $proceed, \Magento\Framework\Model\AbstractModel $attribute) { $result = $proceed($attribute); if ($this->config->isEnabled()) { $this->typeList->invalidate('full_page'); } return $result; }
/** * After generate Xml * * @param \Magento\Framework\View\LayoutInterface $subject * @param \Magento\Framework\View\LayoutInterface $result * @return \Magento\Framework\View\LayoutInterface */ public function afterGenerateXml(\Magento\Framework\View\LayoutInterface $subject, $result) { if ($this->moduleManager->isEnabled('Magento_PageCache') && $this->cacheConfig->isEnabled() && !$this->request->isAjax() && $subject->isCacheable()) { $this->eventManager->dispatch('depersonalize_clear_session'); session_write_close(); $this->messageSession->clearStorage(); } return $result; }
/** * Invalidate full page cache if content is changed * * @param \Magento\Framework\Event\Observer $observer * @return void */ public function execute(\Magento\Framework\Event\Observer $observer) { if ($this->config->isEnabled()) { $object = $observer->getEvent()->getObject(); if ($object instanceof \Magento\Framework\Object\IdentityInterface) { if ($object->getIdentities()) { $this->typeList->invalidate('full_page'); } } } }
/** * If Built-In caching is enabled it collects array of tags * of incoming object and asks to clean cache. * * @param \Magento\Framework\Event\Observer $observer * @return void */ public function execute(\Magento\Framework\Event\Observer $observer) { if ($this->_config->getType() == \Magento\PageCache\Model\Config::BUILT_IN && $this->_config->isEnabled()) { $object = $observer->getEvent()->getObject(); if ($object instanceof \Magento\Framework\DataObject\IdentityInterface) { $tags = $object->getIdentities(); if (!empty($tags)) { $this->getCache()->clean(\Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG, array_unique($tags)); } } } }
/** * If Built-In caching is enabled it collects array of tags * of incoming object and asks to clean cache. * * @param \Magento\Framework\Event\Observer $observer * @return void */ public function execute(\Magento\Framework\Event\Observer $observer) { if ($this->_config->getType() == \Magento\PageCache\Model\Config::BUILT_IN && $this->_config->isEnabled()) { $object = $observer->getEvent()->getObject(); if ($object instanceof \Magento\Framework\Object\IdentityInterface) { $tags = $object->getIdentities(); foreach ($tags as $tag) { $tags[] = preg_replace("~_\\d+\$~", '', $tag); } $this->_cache->clean(array_unique($tags)); } } }
/** * @param Observer $observer * @return void * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function execute(Observer $observer) { if ($this->moduleManager->isEnabled('Magento_PageCache') && $this->cacheConfig->isEnabled() && $this->taxHelper->isCatalogPriceDisplayAffectedByTax()) { /** @var $customerAddress Address */ $address = $observer->getCustomerAddress(); // Check if the address is either the default billing, shipping, or both if ($this->isDefaultBilling($address)) { $this->customerSession->setDefaultTaxBillingAddress(['country_id' => $address->getCountryId(), 'region_id' => $address->getRegion() ? $address->getRegionId() : null, 'postcode' => $address->getPostcode()]); } if ($this->isDefaultShipping($address)) { $this->customerSession->setDefaultTaxShippingAddress(['country_id' => $address->getCountryId(), 'region_id' => $address->getRegion() ? $address->getRegionId() : null, 'postcode' => $address->getPostcode()]); } } }
/** * @param \Magento\Framework\App\ActionInterface $subject * @param callable $proceed * @param \Magento\Framework\App\RequestInterface $request * @return mixed * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function aroundDispatch(\Magento\Framework\App\ActionInterface $subject, \Closure $proceed, \Magento\Framework\App\RequestInterface $request) { if (!$this->customerSession->isLoggedIn() || !$this->moduleManager->isEnabled('Magento_PageCache') || !$this->cacheConfig->isEnabled() || !$this->taxHelper->isCatalogPriceDisplayAffectedByTax()) { return $proceed($request); } $defaultBillingAddress = $this->customerSession->getDefaultTaxBillingAddress(); $defaultShippingAddress = $this->customerSession->getDefaultTaxShippingAddress(); $customerTaxClassId = $this->customerSession->getCustomerTaxClassId(); if (!empty($defaultBillingAddress) || !empty($defaultShippingAddress)) { $taxRates = $this->taxCalculation->getTaxRates($defaultBillingAddress, $defaultShippingAddress, $customerTaxClassId); $this->httpContext->setValue('tax_rates', $taxRates, 0); } return $proceed($request); }
/** * Set X-Cache-Tags header with all the Magento Cache Tags so * they can be purged by the CloudFlare API * * @param \Magento\Framework\View\Layout $subject * @param $result * @return mixed */ public function afterGetOutput(\Magento\Framework\View\Layout $subject, $result) { if (!$subject->isCacheable() || !$this->config->isEnabled()) { return $result; } $tags = []; foreach ($subject->getAllBlocks() as $block) { if ($block->getIdentities() !== null) { $tags = array_merge($tags, $block->getIdentities()); } } $tags = array_unique($tags); $this->cacheTagsUtil->setCloudFlareCacheTagsResponseHeader($this->response, $tags); return $result; }
/** * @param \Magento\Framework\App\ActionInterface $subject * @param callable $proceed * @param \Magento\Framework\App\RequestInterface $request * @return mixed * @SuppressWarnings(PHPMD.UnusedFormalParameter) * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function aroundExecute( \Magento\Framework\App\ActionInterface $subject, \Closure $proceed, \Magento\Framework\App\RequestInterface $request ) { if (!$this->weeeHelper->isEnabled() || !$this->customerSession->isLoggedIn() || !$this->moduleManager->isEnabled('Magento_PageCache') || !$this->cacheConfig->isEnabled()) { return $proceed($request); } $basedOn = $this->taxHelper->getTaxBasedOn(); if ($basedOn != 'shipping' && $basedOn != 'billing') { return $proceed($request); } $weeeTaxRegion = $this->getWeeeTaxRegion($basedOn); $websiteId = $this->storeManager->getStore()->getWebsiteId(); $countryId = $weeeTaxRegion['countryId']; $regionId = $weeeTaxRegion['regionId']; if (!$countryId && !$regionId) { // country and region does not exist return $proceed($request); } else if ($countryId && !$regionId) { // country exist and region does not exist $regionId = 0; $exist = $this->weeeTax->isWeeeInLocation( $countryId, $regionId, $websiteId ); } else { // country and region exist $exist = $this->weeeTax->isWeeeInLocation( $countryId, $regionId, $websiteId ); if (!$exist) { // just check the country for weee $regionId = 0; $exist = $this->weeeTax->isWeeeInLocation( $countryId, $regionId, $websiteId ); } } if ($exist) { $this->httpContext->setValue( 'weee_tax_region', ['countryId' => $countryId, 'regionId' => $regionId], 0 ); } return $proceed($request); }
/** * Retrieve all identities from blocks for further cache invalidation * * @param \Magento\Framework\View\Layout $subject * @param mixed $result * @return mixed */ public function afterGetOutput(\Magento\Framework\View\Layout $subject, $result) { if ($subject->isCacheable() && $this->config->isEnabled()) { $tags = []; foreach ($subject->getAllBlocks() as $block) { if ($block instanceof \Magento\Framework\DataObject\IdentityInterface) { $isEsiBlock = $block->getTtl() > 0; $isVarnish = $this->config->getType() == \Magento\PageCache\Model\Config::VARNISH; if ($isVarnish && $isEsiBlock) { continue; } $tags = array_merge($tags, $block->getIdentities()); } } $tags = array_unique($tags); $this->response->setHeader('X-Magento-Tags', implode(',', $tags)); } return $result; }
/** * Add comment cache containers to private blocks * Blocks are wrapped only if page is cacheable * * @param \Magento\Framework\Event\Observer $observer * @return void */ public function execute(\Magento\Framework\Event\Observer $observer) { $event = $observer->getEvent(); /** @var \Magento\Framework\View\Layout $layout */ $layout = $event->getLayout(); if ($layout->isCacheable() && $this->_config->isEnabled()) { $name = $event->getElementName(); $block = $layout->getBlock($name); $transport = $event->getTransport(); if ($block instanceof \Magento\Framework\View\Element\AbstractBlock) { $blockTtl = $block->getTtl(); $varnishIsEnabledFlag = $this->_config->getType() == \Magento\PageCache\Model\Config::VARNISH; $output = $transport->getData('output'); if ($varnishIsEnabledFlag && isset($blockTtl)) { $output = $this->_wrapEsi($block, $layout); } elseif ($block->isScopePrivate()) { $output = sprintf('<!-- BLOCK %1$s -->%2$s<!-- /BLOCK %1$s -->', $block->getNameInLayout(), $output); } $transport->setData('output', $output); } } }
/** * Address after save event handler * * @param \Magento\Framework\Event\Observer $observer * @return void * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function afterAddressSave($observer) { if ($this->moduleManager->isEnabled('Magento_PageCache') && $this->cacheConfig->isEnabled() && $this->weeeHelper->isEnabled()) { /** @var $customerAddress Address */ $address = $observer->getCustomerAddress(); // Check if the address is either the default billing, shipping, or both if ($address->getIsPrimaryBilling() || $address->getIsDefaultBilling()) { $this->customerSession->setDefaultTaxBillingAddress(['country_id' => $address->getCountryId(), 'region_id' => $address->getRegion() ? $address->getRegionId() : null, 'postcode' => $address->getPostcode()]); } if ($address->getIsPrimaryShipping() || $address->getIsDefaultShipping()) { $this->customerSession->setDefaultTaxShippingAddress(['country_id' => $address->getCountryId(), 'region_id' => $address->getRegion() ? $address->getRegionId() : null, 'postcode' => $address->getPostcode()]); } } }
/** * After generate Xml * * @param \Magento\Framework\View\LayoutInterface $subject * @param \Magento\Framework\View\LayoutInterface $result * @return \Magento\Framework\View\LayoutInterface */ public function afterGenerateXml(\Magento\Framework\View\LayoutInterface $subject, $result) { if ($this->moduleManager->isEnabled('Magento_PageCache') && $this->cacheConfig->isEnabled() && !$this->request->isAjax() && $subject->isCacheable()) { $this->visitor->setSkipRequestLogging(true); $this->visitor->unsetData(); $this->session->clearStorage(); $this->customerSession->clearStorage(); $this->session->setData(\Magento\Framework\Data\Form\FormKey::FORM_KEY, $this->formKey); $this->customerSession->setCustomerGroupId($this->customerGroupId); $this->customer->setGroupId($this->customerGroupId); $this->customerSession->setCustomer($this->customer); } return $result; }
/** * Invalidate full page cache * * @param \Magento\Framework\Event\Observer $observer * @return void * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function execute(\Magento\Framework\Event\Observer $observer) { if ($this->_config->isEnabled()) { $this->_typeList->invalidate('full_page'); } }
/** * Whether a cache type is enabled */ public function testIsEnabled() { $this->_cacheState->setEnabled(\Magento\PageCache\Model\Cache\Type::TYPE_IDENTIFIER, true); $this->_cacheState->expects($this->once())->method('isEnabled')->with(\Magento\PageCache\Model\Cache\Type::TYPE_IDENTIFIER)->will($this->returnValue(true)); $this->_model->isEnabled(); }