88
99namespace Magento \Framework \Translate ;
1010
11- class Inline implements \Magento \Framework \Translate \InlineInterface
11+ use Magento \Framework \App \Area ;
12+ use Magento \Framework \App \ObjectManager ;
13+ use Magento \Framework \App \ScopeInterface ;
14+ use Magento \Framework \App \ScopeResolverInterface ;
15+ use Magento \Framework \App \State ;
16+ use Magento \Framework \Exception \LocalizedException ;
17+ use Magento \Framework \Translate \Inline \ConfigInterface ;
18+ use Magento \Framework \Translate \Inline \ParserInterface ;
19+ use Magento \Framework \Translate \Inline \StateInterface ;
20+ use Magento \Framework \UrlInterface ;
21+ use Magento \Framework \View \Element \Template ;
22+ use Magento \Framework \View \LayoutInterface ;
23+
24+ /**
25+ * Translate Inline Class
26+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
27+ */
28+ class Inline implements InlineInterface
1229{
1330 /**
1431 * Indicator to hold state of whether inline translation is allowed
@@ -18,7 +35,7 @@ class Inline implements \Magento\Framework\Translate\InlineInterface
1835 protected $ isAllowed ;
1936
2037 /**
21- * @var \Magento\Framework\Translate\Inline\ ParserInterface
38+ * @var ParserInterface
2239 */
2340 protected $ parser ;
2441
@@ -30,22 +47,22 @@ class Inline implements \Magento\Framework\Translate\InlineInterface
3047 protected $ isScriptInserted = false ;
3148
3249 /**
33- * @var \Magento\Framework\ UrlInterface
50+ * @var UrlInterface
3451 */
3552 protected $ url ;
3653
3754 /**
38- * @var \Magento\Framework\View\ LayoutInterface
55+ * @var LayoutInterface
3956 */
4057 protected $ layout ;
4158
4259 /**
43- * @var \Magento\Framework\Translate\Inline\ ConfigInterface
60+ * @var ConfigInterface
4461 */
4562 protected $ config ;
4663
4764 /**
48- * @var \Magento\Framework\App\ ScopeResolverInterface
65+ * @var ScopeResolverInterface
4966 */
5067 protected $ scopeResolver ;
5168
@@ -70,28 +87,39 @@ class Inline implements \Magento\Framework\Translate\InlineInterface
7087 protected $ state ;
7188
7289 /**
73- * Initialize inline translation model
74- *
75- * @param \Magento\Framework\App\ScopeResolverInterface $scopeResolver
76- * @param \Magento\Framework\UrlInterface $url
77- * @param \Magento\Framework\View\LayoutInterface $layout
90+ * @var array
91+ */
92+ private $ allowedAreas = [Area::AREA_FRONTEND , Area::AREA_ADMINHTML ];
93+
94+ /**
95+ * @var State
96+ */
97+ private $ appState ;
98+
99+ /**
100+ * @param ScopeResolverInterface $scopeResolver
101+ * @param UrlInterface $url
102+ * @param LayoutInterface $layout
78103 * @param Inline\ConfigInterface $config
79104 * @param Inline\ParserInterface $parser
80105 * @param Inline\StateInterface $state
81106 * @param string $templateFileName
82107 * @param string $translatorRoute
83- * @param null $scope
108+ * @param string|null $scope
109+ * @param State|null $appState
110+ * @SuppressWarnings(PHPMD.ExcessiveParameterList)
84111 */
85112 public function __construct (
86- \ Magento \ Framework \ App \ ScopeResolverInterface $ scopeResolver ,
87- \ Magento \ Framework \ UrlInterface $ url ,
88- \ Magento \ Framework \ View \ LayoutInterface $ layout ,
89- \ Magento \ Framework \ Translate \ Inline \ ConfigInterface $ config ,
90- \ Magento \ Framework \ Translate \ Inline \ ParserInterface $ parser ,
91- \ Magento \ Framework \ Translate \ Inline \ StateInterface $ state ,
113+ ScopeResolverInterface $ scopeResolver ,
114+ UrlInterface $ url ,
115+ LayoutInterface $ layout ,
116+ ConfigInterface $ config ,
117+ ParserInterface $ parser ,
118+ StateInterface $ state ,
92119 $ templateFileName = '' ,
93120 $ translatorRoute = '' ,
94- $ scope = null
121+ $ scope = null ,
122+ ?State $ appState = null
95123 ) {
96124 $ this ->scopeResolver = $ scopeResolver ;
97125 $ this ->url = $ url ;
@@ -102,6 +130,7 @@ public function __construct(
102130 $ this ->templateFileName = $ templateFileName ;
103131 $ this ->translatorRoute = $ translatorRoute ;
104132 $ this ->scope = $ scope ;
133+ $ this ->appState = $ appState ?: ObjectManager::getInstance ()->get (State::class);
105134 }
106135
107136 /**
@@ -112,12 +141,13 @@ public function __construct(
112141 public function isAllowed ()
113142 {
114143 if ($ this ->isAllowed === null ) {
115- if (!$ this ->scope instanceof \Magento \Framework \App \ScopeInterface) {
116- $ scope = $ this ->scopeResolver ->getScope ($ this ->scope );
117- }
144+ $ scope = $ this ->scope instanceof ScopeInterface ? null : $ this ->scopeResolver ->getScope ($ this ->scope );
145+
118146 $ this ->isAllowed = $ this ->config ->isActive ($ scope )
119- && $ this ->config ->isDevAllowed ($ scope );
147+ && $ this ->config ->isDevAllowed ($ scope )
148+ && $ this ->isAreaAllowed ();
120149 }
150+
121151 return $ this ->state ->isEnabled () && $ this ->isAllowed ;
122152 }
123153
@@ -134,7 +164,7 @@ public function getParser()
134164 /**
135165 * Replace translation templates with HTML fragments
136166 *
137- * @param array|string & $body
167+ * @param array|string $body
138168 * @param bool $isJson
139169 * @return $this
140170 */
@@ -189,7 +219,9 @@ protected function addInlineScript()
189219 return ;
190220 }
191221 if (!$ this ->isScriptInserted ) {
192- $ this ->getParser ()->setContent (str_ireplace ('</body> ' , $ this ->getInlineScript () . '</body> ' , $ content ));
222+ $ this ->getParser ()->setContent (
223+ str_ireplace ('</body> ' , $ this ->getInlineScript () . '</body> ' , $ content )
224+ );
193225 $ this ->isScriptInserted = true ;
194226 }
195227 }
@@ -204,8 +236,8 @@ protected function addInlineScript()
204236 */
205237 protected function getInlineScript ()
206238 {
207- /** @var $block \Magento\Framework\View\Element\ Template */
208- $ block = $ this ->layout ->createBlock (\ Magento \ Framework \ View \ Element \ Template::class);
239+ /** @var $block Template */
240+ $ block = $ this ->layout ->createBlock (Template::class);
209241
210242 $ block ->setAjaxUrl ($ this ->getAjaxUrl ());
211243 $ block ->setTemplate ($ this ->templateFileName );
@@ -238,15 +270,24 @@ protected function stripInlineTranslations(&$body)
238270 foreach ($ body as &$ part ) {
239271 $ this ->stripInlineTranslations ($ part );
240272 }
241- } else {
242- if (is_string ($ body )) {
243- $ body = preg_replace (
244- '# ' . \Magento \Framework \Translate \Inline \ParserInterface::REGEXP_TOKEN . '# ' ,
245- '$1 ' ,
246- $ body
247- );
248- }
273+ } elseif (is_string ($ body )) {
274+ $ body = preg_replace ('# ' . ParserInterface::REGEXP_TOKEN . '# ' , '$1 ' , $ body );
249275 }
276+
250277 return $ this ;
251278 }
279+
280+ /**
281+ * Indicates whether the current area is valid for inline translation
282+ *
283+ * @return bool
284+ */
285+ private function isAreaAllowed (): bool
286+ {
287+ try {
288+ return in_array ($ this ->appState ->getAreaCode (), $ this ->allowedAreas , true );
289+ } catch (LocalizedException $ e ) {
290+ return false ;
291+ }
292+ }
252293}
0 commit comments