Skip to content

Commit

Permalink
Merge branch 'master' into CLI-COMMAND
Browse files Browse the repository at this point in the history
  • Loading branch information
mcspronko authored Nov 29, 2020
2 parents 08296c9 + 6832b13 commit d5dcb2a
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 34 deletions.
2 changes: 1 addition & 1 deletion Controller/Adminhtml/Cache/FlushInvalidated.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,6 @@ public function execute()
}

$resultRedirect = $this->resultRedirectFactory->create();
return $resultRedirect->setPath('adminhtml/cache');
return $resultRedirect->setPath($this->_redirect->getRefererUrl());
}
}
36 changes: 23 additions & 13 deletions Plugin/CacheOutdatedMessagePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@
* Copyright © Pronko Consulting (https://www.pronkoconsulting.com)
* See LICENSE for the license details.
*/
declare(strict_types = 1);
declare(strict_types=1);

namespace Pronko\SelectiveCache\Plugin;

use Magento\AdminNotification\Model\System\Message\CacheOutdated;
use Magento\Framework\AuthorizationInterface;
use Magento\Framework\Escaper;
use Magento\Framework\UrlInterface;

/**
* Class OutdatedCacheMessagePlugin
*/
class CacheOutdatedMessagePlugin
{
/**
Expand All @@ -27,33 +25,45 @@ class CacheOutdatedMessagePlugin
private $authorization;

/**
* OutdatedCacheMessagePlugin constructor.
*
* @param UrlInterface $urlBuilder
* @var Escaper
*/
private $escaper;

/**
* CacheOutdatedMessagePlugin constructor.
* @param UrlInterface $urlBuilder
* @param AuthorizationInterface $authorization
* @param Escaper $escaper
*/
public function __construct(
UrlInterface $urlBuilder,
AuthorizationInterface $authorization
AuthorizationInterface $authorization,
Escaper $escaper
) {
$this->urlBuilder = $urlBuilder;
$this->authorization = $authorization;
$this->escaper = $escaper;
}

/**
* @param CacheOutdated $subject
* @param string $result
*
* @return string
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterGetText(CacheOutdated $subject, string $result)
{
if ($this->authorization->isAllowed('Pronko_SelectiveCache::flush_invalidated_cache')) {
$result .= __(
'<br /> Additionally you can <a href="%1">Flush Invalidated Cache</a> directly',
$this->getFlushInvalidatedOnlyUrl()
$link = sprintf(
'<a href="%s">%s</a>',
$this->getFlushInvalidatedOnlyUrl(),
$this->escaper->escapeHtml(__('Flush Invalidated Cache'))
);

$result .= '<br />' . $this->escaper->escapeHtml(__(
'Additionally you can %1 directly.',
$link
), ['a']);
}

return $result;
Expand All @@ -62,7 +72,7 @@ public function afterGetText(CacheOutdated $subject, string $result)
/**
* @return string
*/
private function getFlushInvalidatedOnlyUrl()
private function getFlushInvalidatedOnlyUrl(): string
{
return $this->urlBuilder->getUrl('pronko_selectivecache/cache/flushInvalidated');
}
Expand Down
13 changes: 5 additions & 8 deletions Plugin/CachePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,16 @@ class CachePlugin
/**
* @var AuthorizationInterface
*/
private $_authorization;
private $authorization;

/**
* CachePlugin constructor.
*
* @param CacheButton $cacheButton
* @param AuthorizationInterface $authorization
*/
public function __construct(CacheButton $cacheButton, AuthorizationInterface $authorization)
{
$this->_authorization = $authorization;
$this->authorization = $authorization;
$this->cacheButton = $cacheButton;
}

Expand All @@ -44,11 +43,9 @@ public function __construct(CacheButton $cacheButton, AuthorizationInterface $au
* @param LayoutInterface $layout
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function beforeSetLayout(
Cache $subject,
LayoutInterface $layout
) {
if($this->_authorization->isAllowed('Pronko_SelectiveCache::flush_invalidated_cache')) {
public function beforeSetLayout(Cache $subject, LayoutInterface $layout)
{
if ($this->authorization->isAllowed('Pronko_SelectiveCache::flush_invalidated_cache')) {
$this->cacheButton->execute($subject);
}
}
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@ modman clone git@github.com:mcspronko/selective-cache.git

* Peter Herrmann
* Harshvardhan Malpani
* Serhii Koval
* Antonis Galanis
* Arshad Syed
* Andresa Martins
26 changes: 19 additions & 7 deletions Service/CacheButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Pronko\SelectiveCache\Service;

use Magento\Backend\Block\Cache;
use Magento\Framework\Escaper;
use Magento\Framework\UrlInterface;

/**
Expand All @@ -20,25 +21,34 @@ class CacheButton
*/
private $url;

/**
* @var Escaper
*/
private $escaper;

/**
* CacheButton constructor.
* @param UrlInterface $url
* @param Escaper $escaper
*/
public function __construct(UrlInterface $url)
{
public function __construct(
UrlInterface $url,
Escaper $escaper
) {
$this->url = $url;
$this->escaper = $escaper;
}

/**
* @param Cache $cache
*/
public function execute(Cache $cache)
public function execute(Cache $cache): void
{
$cache->addButton(
'refresh_invalidated_cache',
[
'label' => __('Refresh Invalidated Cache'),
'onclick' => 'setLocation(\'' . $this->getFlushInvalidatedOnlyUrl() . '\')',
'label' => $this->escaper->escapeHtml(__('Refresh Invalidated Cache')),
'onclick' => sprintf("setLocation('%s')", $this->getFlushInvalidatedOnlyUrl()),
'class' => 'primary flush-cache-magento'
]
);
Expand All @@ -47,8 +57,10 @@ public function execute(Cache $cache)
/**
* @return string
*/
private function getFlushInvalidatedOnlyUrl()
private function getFlushInvalidatedOnlyUrl(): string
{
return $this->url->getUrl('pronko_selectivecache/*/flushInvalidated');
return $this->escaper->escapeUrl(
$this->url->getUrl('pronko_selectivecache/*/flushInvalidated')
);
}
}
16 changes: 15 additions & 1 deletion Test/Unit/Plugin/CachePluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Pronko\SelectiveCache\Test\Unit\Plugin;

use Magento\Framework\AuthorizationInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Magento\Framework\View\LayoutInterface;
Expand Down Expand Up @@ -39,6 +40,11 @@ class CachePluginTest extends TestCase
*/
private $layout;

/**
* @var AuthorizationInterface|MockObject
*/
private $authorization;

protected function setUp()
{
$this->cacheButton = $this->getMockBuilder(CacheButton::class)
Expand All @@ -53,7 +59,15 @@ protected function setUp()
->disableOriginalConstructor()
->getMock();

$this->object = new CachePlugin($this->cacheButton);
$this->authorization = $this->getMockBuilder(AuthorizationInterface::class)
->disableOriginalConstructor()
->getMock();

$this->authorization->method('isAllowed')
->with('Pronko_SelectiveCache::flush_invalidated_cache')
->willReturn(true);

$this->object = new CachePlugin($this->cacheButton, $this->authorization);
}

public function testBeforeSetLayout()
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "pronko/selective-cache",
"description": "Selective Cache extension for Magento 2",
"version": "1.0.0",
"version": "1.1.0",
"type": "magento2-module",
"license": "MIT",
"authors": [
Expand All @@ -17,8 +17,8 @@
}
},
"require": {
"magento/framework": "101.0.*|102.0.*",
"magento/module-backend": "100.2.*|101.0.*"
"magento/framework": "~101|~102|~103",
"magento/module-backend": "~100|~101|~102"
},
"require-dev": {
"phpunit/phpunit": "~6.5",
Expand Down
4 changes: 3 additions & 1 deletion i18n/en_US.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"The following cache types have been successfully cleaned: %1","The following cache types have been successfully cleaned: %1"
"There are no invalidated cache types to be cleaned.","There are no invalidated cache types to be cleaned."
"Refresh Invalidated Cache","Refresh Invalidated Cache"
"<br /> Additionally you can <a href=""%1"">Flush Invalidated Cache</a> directly","<br /> Additionally you can <a href=""%1"">Flush Invalidated Cache</a> directly"
"Additionally you can %1 directly.","Additionally you can %1 directly."
"Refresh Invalidated Cache","Refresh Invalidated Cache"
"Flush Invalided Cache","Flush Invalided Cache"

0 comments on commit d5dcb2a

Please sign in to comment.