Skip to content

Commit b2cae37

Browse files
joomdonationlaoneo
andauthored
[5.3] Improve System - Language Filter plugin code (joomla#44841)
* Improve System - Language Filter plugin code * CS --------- Co-authored-by: Allon Moritz <[email protected]>
1 parent e47af2c commit b2cae37

File tree

1 file changed

+77
-53
lines changed

1 file changed

+77
-53
lines changed

plugins/system/languagefilter/src/Extension/LanguageFilter.php

Lines changed: 77 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
use Joomla\CMS\Application\CMSApplicationInterface;
1515
use Joomla\CMS\Association\AssociationServiceInterface;
1616
use Joomla\CMS\Component\ComponentHelper;
17+
use Joomla\CMS\Event\Application\AfterDispatchEvent;
18+
use Joomla\CMS\Event\Application\AfterInitialiseEvent;
19+
use Joomla\CMS\Event\Application\AfterRouteEvent;
20+
use Joomla\CMS\Event\Privacy\CollectCapabilitiesEvent;
1721
use Joomla\CMS\Event\User\AfterSaveEvent;
1822
use Joomla\CMS\Event\User\BeforeSaveEvent;
1923
use Joomla\CMS\Event\User\LoginEvent;
@@ -127,15 +131,17 @@ public function __construct(
127131

128132
$this->setApplication($app);
129133

134+
$app = $this->getApplication();
135+
130136
// Setup language data.
131-
$this->mode_sef = $this->getApplication()->get('sef', 0);
137+
$this->mode_sef = $app->get('sef', 0);
132138
$this->sefs = LanguageHelper::getLanguages('sef');
133139
$this->lang_codes = LanguageHelper::getLanguages('lang_code');
134140
$this->default_lang = ComponentHelper::getParams('com_languages')->get('site', 'en-GB');
135141

136142
// If language filter plugin is executed in a site page.
137-
if ($this->getApplication()->isClient('site')) {
138-
$levels = $this->getApplication()->getIdentity()->getAuthorisedViewLevels();
143+
if ($app->isClient('site')) {
144+
$levels = $app->getIdentity()->getAuthorisedViewLevels();
139145

140146
foreach ($this->sefs as $sef => $language) {
141147
// @todo: In Joomla 2.5.4 and earlier access wasn't set. Non modified Content Languages got 0 as access value
@@ -198,7 +204,7 @@ public static function getSubscribedEvents(): array
198204
*
199205
* @since 1.6
200206
*/
201-
public function onAfterInitialise()
207+
public function onAfterInitialise(AfterInitialiseEvent $event): void
202208
{
203209
$router = $this->getSiteRouter();
204210

@@ -223,7 +229,7 @@ public function onAfterInitialise()
223229
*
224230
* @since 3.4
225231
*/
226-
public function onAfterRoute()
232+
public function onAfterRoute(AfterRouteEvent $event): void
227233
{
228234
// Add custom site name.
229235
if ($this->getApplication()->isClient('site') && isset($this->lang_codes[$this->current_lang]) && $this->lang_codes[$this->current_lang]->sitename) {
@@ -327,6 +333,8 @@ public function postprocessNonSEFBuildRule(&$router, &$uri)
327333
*/
328334
public function parseRule(&$router, &$uri)
329335
{
336+
$app = $this->getApplication();
337+
330338
// Did we find the current and existing language yet?
331339
$found = false;
332340

@@ -416,10 +424,10 @@ public function parseRule(&$router, &$uri)
416424
// We are called via POST or the nolangfilter url parameter was set. We don't care about the language
417425
// and simply set the default language as our current language.
418426
if (
419-
$this->getApplication()->getInput()->getMethod() === 'POST'
420-
|| $this->getApplication()->getInput()->get('nolangfilter', 0) == 1
421-
|| \count($this->getApplication()->getInput()->post) > 0
422-
|| \count($this->getApplication()->getInput()->files) > 0
427+
$app->getInput()->getMethod() === 'POST'
428+
|| $app->getInput()->get('nolangfilter', 0) == 1
429+
|| \count($app->getInput()->post) > 0
430+
|| \count($app->getInput()->files) > 0
423431
) {
424432
$found = true;
425433

@@ -466,7 +474,7 @@ public function parseRule(&$router, &$uri)
466474

467475
$uri->setPath($path);
468476

469-
if (!$this->getApplication()->get('sef_rewrite')) {
477+
if (!$app->get('sef_rewrite')) {
470478
$uri->setPath('index.php/' . $uri->getPath());
471479
}
472480

@@ -484,26 +492,26 @@ public function parseRule(&$router, &$uri)
484492
$redirectHttpCode = 301;
485493

486494
// We cannot cache this redirect in browser. 301 is cacheable by default so we need to force to not cache it in browsers.
487-
$this->getApplication()->setHeader('Expires', 'Wed, 17 Aug 2005 00:00:00 GMT', true);
488-
$this->getApplication()->setHeader('Last-Modified', gmdate('D, d M Y H:i:s') . ' GMT', true);
489-
$this->getApplication()->setHeader('Cache-Control', 'no-store, no-cache, must-revalidate', false);
490-
$this->getApplication()->sendHeaders();
495+
$app->setHeader('Expires', 'Wed, 17 Aug 2005 00:00:00 GMT', true);
496+
$app->setHeader('Last-Modified', gmdate('D, d M Y H:i:s') . ' GMT', true);
497+
$app->setHeader('Cache-Control', 'no-store, no-cache, must-revalidate', false);
498+
$app->sendHeaders();
491499
}
492500

493501
// Redirect to language.
494-
$this->getApplication()->redirect($redirectUri, $redirectHttpCode);
502+
$app->redirect($redirectUri, $redirectHttpCode);
495503
}
496504

497505
// We have found our language and now need to set the cookie and the language value in our system
498506
$this->current_lang = $lang_code;
499507

500508
// Set the request var.
501-
$this->getApplication()->getInput()->set('language', $lang_code);
502-
$this->getApplication()->set('language', $lang_code);
503-
$language = $this->getApplication()->getLanguage();
509+
$app->getInput()->set('language', $lang_code);
510+
$app->set('language', $lang_code);
511+
$language = $app->getLanguage();
504512

505513
if ($language->getTag() !== $lang_code) {
506-
$language_new = $this->languageFactory->createLanguage($lang_code, (bool) $this->getApplication()->get('debug_lang'));
514+
$language_new = $this->languageFactory->createLanguage($lang_code, (bool) $app->get('debug_lang'));
507515

508516
foreach ($language->getPaths() as $extension => $files) {
509517
if (strpos($extension, 'plg_system') !== false) {
@@ -519,7 +527,7 @@ public function parseRule(&$router, &$uri)
519527
}
520528

521529
Factory::$language = $language_new;
522-
$this->getApplication()->loadLanguage($language_new);
530+
$app->loadLanguage($language_new);
523531
}
524532

525533
// Create a cookie.
@@ -531,19 +539,23 @@ public function parseRule(&$router, &$uri)
531539
/**
532540
* Reports the privacy related capabilities for this plugin to site administrators.
533541
*
534-
* @return array
542+
* @return void
535543
*
536544
* @since 3.9.0
537545
*/
538-
public function onPrivacyCollectAdminCapabilities()
546+
public function onPrivacyCollectAdminCapabilities(CollectCapabilitiesEvent $event): void
539547
{
540548
$this->loadLanguage();
541549

542-
return [
543-
$this->getApplication()->getLanguage()->_('PLG_SYSTEM_LANGUAGEFILTER') => [
544-
$this->getApplication()->getLanguage()->_('PLG_SYSTEM_LANGUAGEFILTER_PRIVACY_CAPABILITY_LANGUAGE_COOKIE'),
550+
$language = $this->getApplication()->getLanguage();
551+
552+
$event->addResult([
553+
$language->_('PLG_SYSTEM_LANGUAGEFILTER') => [
554+
$language->_(
555+
'PLG_SYSTEM_LANGUAGEFILTER_PRIVACY_CAPABILITY_LANGUAGE_COOKIE'
556+
),
545557
],
546-
];
558+
]);
547559
}
548560

549561
/**
@@ -559,7 +571,7 @@ public function onPrivacyCollectAdminCapabilities()
559571
*
560572
* @since 1.6
561573
*/
562-
public function onUserBeforeSave(BeforeSaveEvent $event)
574+
public function onUserBeforeSave(BeforeSaveEvent $event): void
563575
{
564576
$user = $event->getUser();
565577

@@ -600,14 +612,16 @@ public function onUserAfterSave(AfterSaveEvent $event): void
600612
$lang_code = $this->current_lang;
601613
}
602614

615+
$app = $this->getApplication();
616+
603617
if ($lang_code === $this->user_lang_code || !isset($this->lang_codes[$lang_code])) {
604-
if ($this->getApplication()->isClient('site')) {
605-
$this->getApplication()->setUserState('com_users.edit.profile.redirect', null);
618+
if ($app->isClient('site')) {
619+
$app->setUserState('com_users.edit.profile.redirect', null);
606620
}
607621
} else {
608-
if ($this->getApplication()->isClient('site')) {
609-
$this->getApplication()->setUserState('com_users.edit.profile.redirect', 'index.php?Itemid='
610-
. $this->getApplication()->getMenu()->getDefault($lang_code)->id . '&lang=' . $this->lang_codes[$lang_code]->sef);
622+
if ($app->isClient('site')) {
623+
$app->setUserState('com_users.edit.profile.redirect', 'index.php?Itemid='
624+
. $app->getMenu()->getDefault($lang_code)->id . '&lang=' . $this->lang_codes[$lang_code]->sef);
611625

612626
// Create a cookie.
613627
$this->setLanguageCookie($lang_code);
@@ -622,16 +636,18 @@ public function onUserAfterSave(AfterSaveEvent $event): void
622636
* @param array $user Holds the user data.
623637
* @param array $options Array holding options (remember, autoregister, group).
624638
*
625-
* @return null
639+
* @return void
626640
*
627641
* @since 1.5
628642
*/
629-
public function onUserLogin(LoginEvent $event)
643+
public function onUserLogin(LoginEvent $event): void
630644
{
631645
$user = $event->getArgument('subject');
632646

633-
if ($this->getApplication()->isClient('site')) {
634-
$menu = $this->getApplication()->getMenu();
647+
$app = $this->getApplication();
648+
649+
if ($app->isClient('site')) {
650+
$menu = $app->getMenu();
635651

636652
if ($this->params->get('automatic_change', 1)) {
637653
$assoc = Associations::isEnabled();
@@ -670,7 +686,7 @@ public function onUserLogin(LoginEvent $event)
670686
}
671687

672688
// Retrieves the Itemid from a login form.
673-
$uri = new Uri($this->getApplication()->getUserState('users.login.form.return'));
689+
$uri = new Uri($app->getUserState('users.login.form.return'));
674690

675691
if ($uri->getVar('Itemid')) {
676692
// The login form contains a menu item redirection. Try to get associations from that menu item.
@@ -681,7 +697,7 @@ public function onUserLogin(LoginEvent $event)
681697

682698
if (isset($associations[$lang_code]) && $menu->getItem($associations[$lang_code])) {
683699
$associationItemid = $associations[$lang_code];
684-
$this->getApplication()->setUserState('users.login.form.return', 'index.php?Itemid=' . $associationItemid);
700+
$app->setUserState('users.login.form.return', 'index.php?Itemid=' . $associationItemid);
685701
$foundAssociation = true;
686702
}
687703
} elseif (isset($associations[$lang_code]) && $menu->getItem($associations[$lang_code])) {
@@ -691,14 +707,14 @@ public function onUserLogin(LoginEvent $event)
691707
* We redirect to the user preferred site language associated page.
692708
*/
693709
$associationItemid = $associations[$lang_code];
694-
$this->getApplication()->setUserState('users.login.form.return', 'index.php?Itemid=' . $associationItemid);
710+
$app->setUserState('users.login.form.return', 'index.php?Itemid=' . $associationItemid);
695711
$foundAssociation = true;
696712
} elseif ($active->home) {
697713
// We are on a Home page, we redirect to the user preferred site language Home page.
698714
$item = $menu->getDefault($lang_code);
699715

700716
if ($item && $item->language !== $active->language && $item->language !== '*') {
701-
$this->getApplication()->setUserState('users.login.form.return', 'index.php?Itemid=' . $item->id);
717+
$app->setUserState('users.login.form.return', 'index.php?Itemid=' . $item->id);
702718
$foundAssociation = true;
703719
}
704720
}
@@ -715,8 +731,14 @@ public function onUserLogin(LoginEvent $event)
715731
$this->languageFactory->createLanguage($lang_code);
716732
}
717733
} else {
718-
if ($this->getApplication()->getUserState('users.login.form.return')) {
719-
$this->getApplication()->setUserState('users.login.form.return', Route::_($this->getApplication()->getUserState('users.login.form.return'), false));
734+
if ($app->getUserState('users.login.form.return')) {
735+
$app->setUserState(
736+
'users.login.form.return',
737+
Route::_(
738+
$app->getUserState('users.login.form.return'),
739+
false
740+
)
741+
);
720742
}
721743
}
722744
}
@@ -729,16 +751,17 @@ public function onUserLogin(LoginEvent $event)
729751
*
730752
* @since 1.7
731753
*/
732-
public function onAfterDispatch()
754+
public function onAfterDispatch(AfterDispatchEvent $event): void
733755
{
734-
$doc = $this->getApplication()->getDocument();
756+
$app = $this->getApplication();
757+
$doc = $app->getDocument();
735758

736-
if ($this->getApplication()->isClient('site') && $this->params->get('alternate_meta', 1) && $doc->getType() === 'html') {
759+
if ($app->isClient('site') && $this->params->get('alternate_meta', 1) && $doc->getType() === 'html') {
737760
$languages = $this->lang_codes;
738761
$homes = Multilanguage::getSiteHomePages();
739-
$menu = $this->getApplication()->getMenu();
762+
$menu = $app->getMenu();
740763
$active = $menu->getActive();
741-
$levels = $this->getApplication()->getIdentity()->getAuthorisedViewLevels();
764+
$levels = $app->getIdentity()->getAuthorisedViewLevels();
742765
$remove_default_prefix = $this->params->get('remove_default_prefix', 0);
743766
$server = Uri::getInstance()->toString(['scheme', 'host', 'port']);
744767
$is_home = false;
@@ -761,9 +784,9 @@ public function onAfterDispatch()
761784
}
762785

763786
// Load component associations.
764-
$option = $this->getApplication()->getInput()->get('option');
787+
$option = $app->getInput()->get('option');
765788

766-
$component = $this->getApplication()->bootComponent($option);
789+
$component = $app->bootComponent($option);
767790

768791
if ($component instanceof AssociationServiceInterface) {
769792
$cassociations = $component->getAssociationsExtension()->getAssociationsForItem();
@@ -853,14 +876,15 @@ private function setLanguageCookie($languageCode)
853876
// If is set to use language cookie for a year in plugin params, save the user language in a new cookie.
854877
if ((int) $this->params->get('lang_cookie', 0) === 1) {
855878
// Create a cookie with one year lifetime.
856-
$this->getApplication()->getInput()->cookie->set(
879+
$app = $this->getApplication();
880+
$app->getInput()->cookie->set(
857881
ApplicationHelper::getHash('language'),
858882
$languageCode,
859883
[
860884
'expires' => time() + 365 * 86400,
861-
'path' => $this->getApplication()->get('cookie_path', '/'),
862-
'domain' => $this->getApplication()->get('cookie_domain', ''),
863-
'secure' => $this->getApplication()->isHttpsForced(),
885+
'path' => $app->get('cookie_path', '/'),
886+
'domain' => $app->get('cookie_domain', ''),
887+
'secure' => $app->isHttpsForced(),
864888
'httponly' => true,
865889
]
866890
);

0 commit comments

Comments
 (0)