Skip to content

Commit

Permalink
Fixed logout from admin when css is not deployed yet. (Move LayoutInt…
Browse files Browse the repository at this point in the history
…erface out of Helper)
  • Loading branch information
vovayatsyuk committed May 6, 2022
1 parent f54794c commit 4c3f18c
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 152 deletions.
6 changes: 3 additions & 3 deletions Block/Adminhtml/System/Config/Form/Field/Available.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ class Available extends \Magento\Config\Block\System\Config\Form\Field
protected $_template = 'config-field/available.phtml';

/**
* @var \Swissup\ThemeEditor\Helper\Data
* @var \Swissup\ThemeEditor\Helper\Header
*/
protected $helper;

/**
* @param \Magento\Backend\Block\Template\Context $context
* @param \Swissup\ThemeEditor\Helper\Data $helper
* @param \Swissup\ThemeEditor\Helper\Header $helper
* @param array $data
*/
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Swissup\ThemeEditor\Helper\Data $helper,
\Swissup\ThemeEditor\Helper\Header $helper,
array $data = []
) {
$this->helper = $helper;
Expand Down
6 changes: 3 additions & 3 deletions Controller/Header/Preview.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
class Preview extends \Magento\Framework\App\Action\Action
{
/**
* @var \Swissup\ThemeEditor\Helper\Data
* @var \Swissup\ThemeEditor\Helper\Header
*/
protected $helper;

/**
* @param \Magento\Framework\App\Action\Context $context
* @param \Swissup\ThemeEditor\Helper\Data $helper
* @param \Swissup\ThemeEditor\Helper\Header $helper
*/
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Swissup\ThemeEditor\Helper\Data $helper
\Swissup\ThemeEditor\Helper\Header $helper
) {
$this->helper = $helper;
parent::__construct($context);
Expand Down
142 changes: 2 additions & 140 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,11 @@
namespace Swissup\ThemeEditor\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Store\Model\ScopeInterface;
use Magento\Framework\View\Design\ThemeInterface;
use Magento\Framework\App\Area;
use Magento\Store\Model\ScopeInterface;

class Data extends AbstractHelper
{
/**
* Configurable header preview action name
*/
const PREVIEW_ACTION = 'swissupeditor_header_preview';

/**
* Configurable header enabled
*/
const PATH_HEADER_ENABLED = '/header_config/enabled';

/**
* Configurable header layout
*/
const PATH_HEADER_LAYOUT = '/header_config/layout';

/**
* Configurable header preview hash
*/
const PATH_HEADER_PREVIEW_HASH = '/header_config/preview_hash';

/**
* Configurable header preview expires
*/
const PATH_HEADER_PREVIEW_EXPIRES = '/header_config/preview_expires';

/**
* Configurable header preview layout
*/
const PATH_HEADER_LAYOUT_PREVIEW = '/header_config/preview';

/**
* Configurable header disable on pages
*/
const PATH_HEADER_EXCLUDE = '/header_config/exclude';

/**
* @var \Magento\Framework\View\ConfigInterface
*/
Expand All @@ -54,18 +18,11 @@ class Data extends AbstractHelper
*/
private $configView;

/**
* @var \Magento\Framework\View\LayoutInterface
*/
private $layout;

public function __construct(
\Magento\Framework\App\Helper\Context $context,
\Magento\Framework\View\ConfigInterface $viewConfig,
\Magento\Framework\View\LayoutInterface $layout
\Magento\Framework\View\ConfigInterface $viewConfig
) {
$this->viewConfig = $viewConfig;
$this->layout = $layout;
parent::__construct($context);
}

Expand Down Expand Up @@ -114,78 +71,6 @@ public function getViewConfigValue($path, $module = 'Swissup_ThemeEditor')
return $this->getViewConfig()->getVarValue($module, $path);
}

/**
* Check if configurable header enabled
*
* @param ThemeInterface $theme
* @return boolean
*/
public function isHeaderEnabled(ThemeInterface $theme = null)
{
if (in_array(
'swissupeditor_header_preview',
$this->layout->getUpdate()->getHandles())
) {
return true;
}

if (!$theme) {
$theme = $this->layout->getUpdate()->getTheme();
}
$themeConfig = $this->themeCodeToConfigPath($theme);

$action = $this->_getRequest()->getFullActionName();
$exclude = explode(
"\n",
$this->getConfigValue($themeConfig . self::PATH_HEADER_EXCLUDE) ?: ''
);
$exclude = array_map('trim', $exclude);

if (in_array($action, $exclude)) {
return false;
}

return $this->getConfigValue($themeConfig . self::PATH_HEADER_ENABLED);
}

/**
* Get header layout config
*
* @param ThemeInterface $theme
* @return array
*/
public function getHeaderLayout(ThemeInterface $theme = null)
{
$scope = ScopeInterface::SCOPE_STORE;
$scopeCode = null;
if ($theme) {
$section = $this->themeCodeToConfigPath($theme);
} else if ($this->_getRequest()->getRouteName() == Area::AREA_ADMINHTML) {
$section = $this->_getRequest()->getParam('section');

if ($this->_getRequest()->getParam('store') !== null) {
$scopeCode = $this->_getRequest()->getParam('store');
}

if ($this->_getRequest()->getParam('website') !== null) {
$scope = ScopeInterface::SCOPE_WEBSITE;
$scopeCode = $this->_getRequest()->getParam('website');
}
} else {
$theme = $this->layout->getUpdate()->getTheme();
$section = $this->themeCodeToConfigPath($theme);
}

if ($this->_getRequest()->getFullActionName() == self::PREVIEW_ACTION) {
$configPath = $section . self::PATH_HEADER_LAYOUT_PREVIEW;
} else {
$configPath = $section . self::PATH_HEADER_LAYOUT;
}
$config = $this->getConfigValue($configPath, $scope, $scopeCode);

return $config ? json_decode($config, true) : [];
}

public function getLessStyles(ThemeInterface $theme): string
{
return (string) $this->getConfigValue(
Expand Down Expand Up @@ -241,29 +126,6 @@ public function getThemeEditorCode(ThemeInterface $theme = null)
return $code;
}

/**
* Check if preview can be displayed for current theme
*
* @param String $hash
* @return bool
*/
public function validatePreviewHash($hash)
{
$theme = $this->layout->getUpdate()->getTheme();
$section = $this->themeCodeToConfigPath($theme);
$hashConfig = $this->getConfigValue($section . self::PATH_HEADER_PREVIEW_HASH);

if (isset($hashConfig) && $hash == $hashConfig) {
$expire = $this->getConfigValue($section . self::PATH_HEADER_PREVIEW_EXPIRES);

if (isset($expire)) {
return $expire >= time();
}
}

return false;
}

/**
* Convert capital letters to hyphens in string
* Example:
Expand Down
166 changes: 166 additions & 0 deletions Helper/Header.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
<?php

namespace Swissup\ThemeEditor\Helper;

use Magento\Framework\App\Helper\Context;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Area;
use Magento\Store\Model\ScopeInterface;

class Header extends AbstractHelper
{
/**
* Configurable header preview action name
*/
const PREVIEW_ACTION = 'swissupeditor_header_preview';

/**
* Configurable header enabled
*/
const PATH_HEADER_ENABLED = '/header_config/enabled';

/**
* Configurable header layout
*/
const PATH_HEADER_LAYOUT = '/header_config/layout';

/**
* Configurable header preview hash
*/
const PATH_HEADER_PREVIEW_HASH = '/header_config/preview_hash';

/**
* Configurable header preview expires
*/
const PATH_HEADER_PREVIEW_EXPIRES = '/header_config/preview_expires';

/**
* Configurable header preview layout
*/
const PATH_HEADER_LAYOUT_PREVIEW = '/header_config/preview';

/**
* Configurable header disable on pages
*/
const PATH_HEADER_EXCLUDE = '/header_config/exclude';

/**
* @var \Magento\Framework\View\LayoutInterface
*/
private $layout;

/**
* @var Data
*/
private $helper;

/**
* @param Context $context
* @param Data $helper
* @param \Magento\Framework\View\LayoutInterface $layout
*/
public function __construct(
Context $context,
Data $helper,
\Magento\Framework\View\LayoutInterface $layout
) {
$this->helper = $helper;
$this->layout = $layout;
parent::__construct($context);
}

/**
* Check if configurable header enabled
*
* @param ThemeInterface $theme
* @return boolean
*/
public function isHeaderEnabled(ThemeInterface $theme = null)
{
if (in_array(
'swissupeditor_header_preview',
$this->layout->getUpdate()->getHandles())
) {
return true;
}

if (!$theme) {
$theme = $this->layout->getUpdate()->getTheme();
}
$themeConfig = $this->helper->themeCodeToConfigPath($theme);

$action = $this->_getRequest()->getFullActionName();
$exclude = explode(
"\n",
$this->helper->getConfigValue($themeConfig . self::PATH_HEADER_EXCLUDE) ?: ''
);
$exclude = array_map('trim', $exclude);

if (in_array($action, $exclude)) {
return false;
}

return $this->helper->getConfigValue($themeConfig . self::PATH_HEADER_ENABLED);
}

/**
* Get header layout config
*
* @param ThemeInterface $theme
* @return array
*/
public function getHeaderLayout(ThemeInterface $theme = null)
{
$scope = ScopeInterface::SCOPE_STORE;
$scopeCode = null;
if ($theme) {
$section = $this->helper->themeCodeToConfigPath($theme);
} else if ($this->_getRequest()->getRouteName() == Area::AREA_ADMINHTML) {
$section = $this->_getRequest()->getParam('section');

if ($this->_getRequest()->getParam('store') !== null) {
$scopeCode = $this->_getRequest()->getParam('store');
}

if ($this->_getRequest()->getParam('website') !== null) {
$scope = ScopeInterface::SCOPE_WEBSITE;
$scopeCode = $this->_getRequest()->getParam('website');
}
} else {
$theme = $this->layout->getUpdate()->getTheme();
$section = $this->helper->themeCodeToConfigPath($theme);
}

if ($this->_getRequest()->getFullActionName() == self::PREVIEW_ACTION) {
$configPath = $section . self::PATH_HEADER_LAYOUT_PREVIEW;
} else {
$configPath = $section . self::PATH_HEADER_LAYOUT;
}
$config = $this->helper->getConfigValue($configPath, $scope, $scopeCode);

return $config ? json_decode($config, true) : [];
}

/**
* Check if preview can be displayed for current theme
*
* @param String $hash
* @return bool
*/
public function validatePreviewHash($hash)
{
$theme = $this->layout->getUpdate()->getTheme();
$section = $this->helper->themeCodeToConfigPath($theme);
$hashConfig = $this->helper->getConfigValue($section . self::PATH_HEADER_PREVIEW_HASH);

if (isset($hashConfig) && $hash == $hashConfig) {
$expire = $this->helper->getConfigValue($section . self::PATH_HEADER_PREVIEW_EXPIRES);

if (isset($expire)) {
return $expire >= time();
}
}

return false;
}
}
Loading

0 comments on commit 4c3f18c

Please sign in to comment.