Skip to content

Commit

Permalink
fix(email): set scope correctly on admin emails
Browse files Browse the repository at this point in the history
  • Loading branch information
damienwebdev committed Mar 19, 2023
1 parent e53fd3a commit bc984a6
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 17 deletions.
21 changes: 6 additions & 15 deletions Plugins/EmailFilterStoreDirectiveModifierPlugin.php
Expand Up @@ -5,16 +5,14 @@
* See LICENSE.md for details.
*/

declare (strict_types = 1);
declare(strict_types=1);

namespace Graycore\Daffodil\Plugins;

use Graycore\Daffodil\Configuration\Configuration;
use Graycore\Daffodil\Router\Mapper;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Filter\Template\Tokenizer\Parameter;
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManagerInterface;

class EmailFilterStoreDirectiveModifierPlugin
{
Expand All @@ -27,24 +25,17 @@ class EmailFilterStoreDirectiveModifierPlugin

private $_mapper;

/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
private $_storeManager;

/**
* @param ScopeConfigInterface $scopeConfig
*/
public function __construct(
\Magento\Framework\UrlInterface $urlModel,
Configuration $configuration,
Mapper $mapper,
StoreManagerInterface $storeManager
Mapper $mapper
) {
$this->_configuration = $configuration;
$this->_urlModel = $urlModel;
$this->_mapper = $mapper;
$this->_storeManager = $storeManager;
}

/**
Expand All @@ -60,11 +51,11 @@ public function afterStoreDirective(\Magento\Email\Model\Template\Filter $subjec
if (!$this->_configuration->isActive()) {
return $result;
}
$domain = $this->_urlModel->setScope(
$this->_storeManager->getStore($subject->getStoreId())
)->getBaseUrl();

$result = $this->_mapper->mapDomain($result, $domain);
$result = $this->_mapper->mapDomain(
$result,
$this->_urlModel->getBaseUrl()
);

$parameters = $this->getParameters($construction[2]);

Expand Down
4 changes: 2 additions & 2 deletions Plugins/EmailTemplateUrlModifierPlugin.php
Expand Up @@ -5,7 +5,7 @@
* See LICENSE.md for details.
*/

declare (strict_types = 1);
declare(strict_types=1);

namespace Graycore\Daffodil\Plugins;

Expand Down Expand Up @@ -52,7 +52,7 @@ public function afterGetUrl(Template $subject, $result, Store $store, $route = '
if (!$this->_configuration->isActive()) {
return $result;
}
$domain = $this->_urlModel->setScope($store)->getBaseUrl();
$domain = $this->_urlModel->getBaseUrl();

$result = $this->_mapper->mapDomain($result, $domain);
$result = $this->_mapper->mapRoute($result, $route);
Expand Down
57 changes: 57 additions & 0 deletions Plugins/EmulatedAdminEmailUrls.php
@@ -0,0 +1,57 @@
<?php

/**
* Copyright © Graycore, LLC. All rights reserved.
* See LICENSE.md for details.
*/

declare(strict_types=1);

namespace Graycore\Daffodil\Plugins;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Graycore\Daffodil\Configuration\Configuration;
use Magento\Store\Model\StoreManagerInterface;

class EmulatedAdminEmailUrls
{
private $_configuration;
private $_urlModel;
private $_store;

private $originalScope;
/**
* @param ScopeConfigInterface $scopeConfig
*/
public function __construct(
\Magento\Framework\UrlInterface $urlModel,
Configuration $configuration,
StoreManagerInterface $store
) {
$this->_configuration = $configuration;
$this->_urlModel = $urlModel;
$this->_store = $store;
}

public function beforeGetTransport()
{
if ($this->_configuration->isActive()) {
$this->originalScope = $this->_urlModel->getScope();
$this->_urlModel->setScope($this->_store->getStore());
}

return null;
}

/**
*
*/
public function afterGetTransport($subject, $result)
{
if ($this->_configuration->isActive()) {
$this->_urlModel->setScope($this->originalScope);
}

return $result;
}
}
7 changes: 7 additions & 0 deletions etc/adminhtml/di.xml
@@ -0,0 +1,7 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Framework\Mail\Template\TransportBuilder">
<plugin
name="updateUrlsForFrontendEmailsSentFromAdmin" type="Graycore\Daffodil\Plugins\EmulatedAdminEmailUrls" />
</type>
</config>

0 comments on commit bc984a6

Please sign in to comment.