JoomlaPolska / jezyk-J4

Język polski dla Joomla 4
GNU General Public License v2.0
3 stars 5 forks source link

[5.1] SEF: Implementing trailing slash behavior #463

Closed joomlapl-bot closed 5 months ago

joomlapl-bot commented 6 months ago

PR w związku ze zmianą oryginału https://github.com/joomla/joomla-cms/pull/42702 Poniżej zmiany w oryginale:

Click to expand the diff! ```diff diff --git a/administrator/language/en-GB/plg_system_sef.ini b/administrator/language/en-GB/plg_system_sef.ini index 42e97e4a285c8..d4be5f41bca6c 100644 --- a/administrator/language/en-GB/plg_system_sef.ini +++ b/administrator/language/en-GB/plg_system_sef.ini @@ -5,5 +5,10 @@ PLG_SEF_DOMAIN_DESCRIPTION="If your site can be accessed through more than one domain enter the preferred (sometimes referred to as canonical) domain here.
Note: https://example.com and https://www.example.com are different domains." PLG_SEF_DOMAIN_LABEL="Site Domain" +PLG_SEF_TRAILINGSLASH_DESCRIPTION="Force Joomla to only use URLs with or without trailing slash. When set, this will force the right URL with redirects and is only applied when 'Add suffix to URL' is disabled." +PLG_SEF_TRAILINGSLASH_LABEL="Trailing slash for URLs" +PLG_SEF_TRAILINGSLASH_OPTION_NONE="No change" +PLG_SEF_TRAILINGSLASH_OPTION_NO_SLASH="Enforce URLs without trailing slash" +PLG_SEF_TRAILINGSLASH_OPTION_SLASH="Enforce URLs with trailing slash" PLG_SEF_XML_DESCRIPTION="Adds SEF support to links in the document. It operates directly on the HTML and does not require a special tag." PLG_SYSTEM_SEF="System - SEF" diff --git a/plugins/system/sef/sef.xml b/plugins/system/sef/sef.xml index bace5bea3b14c..39b87d9b42044 100644 --- a/plugins/system/sef/sef.xml +++ b/plugins/system/sef/sef.xml @@ -30,6 +30,19 @@ filter="url" validate="url" /> + + + + + + diff --git a/plugins/system/sef/src/Extension/Sef.php b/plugins/system/sef/src/Extension/Sef.php index 52a57213d1e7e..429cf5500c55a 100644 --- a/plugins/system/sef/src/Extension/Sef.php +++ b/plugins/system/sef/src/Extension/Sef.php @@ -10,9 +10,13 @@ namespace Joomla\Plugin\System\Sef\Extension; +use Joomla\CMS\Event\Router\AfterInitialiseRouterEvent; use Joomla\CMS\Plugin\CMSPlugin; use Joomla\CMS\Router\Route; +use Joomla\CMS\Router\Router; +use Joomla\CMS\Router\SiteRouter; use Joomla\CMS\Uri\Uri; +use Joomla\Event\SubscriberInterface; // phpcs:disable PSR1.Files.SideEffects \defined('_JEXEC') or die; @@ -23,8 +27,66 @@ * * @since 1.5 */ -final class Sef extends CMSPlugin +final class Sef extends CMSPlugin implements SubscriberInterface { + /** + * Application object. + * + * @var \Joomla\CMS\Application\CMSApplication + * @since __DEPLOY_VERSION__ + */ + protected $app; + + /** + * Returns an array of CMS events this plugin will listen to and the respective handlers. + * + * @return array + * + * @since __DEPLOY_VERSION__ + */ + public static function getSubscribedEvents(): array + { + /** + * Note that onAfterInitialise must be the first handlers to run for this + * plugin to operate as expected. These handlers load compatibility code which + * might be needed by other plugins + */ + return [ + 'onAfterInitialiseRouter' => 'onAfterInitialiseRouter', + 'onAfterDispatch' => 'onAfterDispatch', + 'onAfterRender' => 'onAfterRender', + ]; + } + + /** + * After initialise router. + * + * @return void + * + * @since __DEPLOY_VERSION__ + */ + public function onAfterInitialiseRouter(AfterInitialiseRouterEvent $event) + { + if ( + !is_a($event->getRouter(), SiteRouter::class) + || !$this->app->get('sef') + || $this->app->get('sef_suffix') + || !$this->params->get('trailingslash') + ) { + return; + } + + if ($this->params->get('trailingslash') == 1) { + // Remove trailingslash + $event->getRouter()->attachBuildRule([$this, 'removeTrailingSlash'], SiteRouter::PROCESS_AFTER); + } elseif ($this->params->get('trailingslash') == 2) { + // Add trailingslash + $event->getRouter()->attachBuildRule([$this, 'addTrailingSlash'], SiteRouter::PROCESS_AFTER); + } + + $event->getRouter()->attachParseRule([$this, 'enforceTrailingSlash'], SiteRouter::PROCESS_BEFORE); + } + /** * Add the canonical uri to the head. * @@ -188,6 +250,74 @@ function ($match) use ($base, $protocols) { $this->getApplication()->setBody($buffer); } + /** + * Remove any trailing slash from URLs built in Joomla + * + * @param Router &$router Router object. + * @param Uri &$uri Uri object. + * + * @return void + * + * @since __DEPLOY_VERSION__ + */ + public function removeTrailingSlash(&$router, &$uri) + { + $path = $uri->getPath(); + + if (substr($path, -1) == '/') { + $uri->setPath(substr($path, 0, -1)); + } + } + + /** + * Add trailing slash to URLs built in Joomla + * + * @param Router &$router Router object. + * @param Uri &$uri Uri object. + * + * @return void + * + * @since __DEPLOY_VERSION__ + */ + public function addTrailingSlash(&$router, &$uri) + { + $path = $uri->getPath(); + + if (substr($path, -1) !== '/') { + $uri->setPath($path . '/'); + } + } + + /** + * Redirect to a URL with or without trailing slash + * + * @param Router &$router Router object. + * @param Uri &$uri Uri object. + * + * @return void + * + * @since __DEPLOY_VERSION__ + */ + public function enforceTrailingSlash(&$router, &$uri) + { + // We only want to redirect on GET requests + if ($this->app->getInput()->getMethod() != 'GET') { + return; + } + + $originalUri = Uri::getInstance(); + + if ($this->params->get('trailingslash') == 1 && substr($originalUri->getPath(), -1) == '/' && $originalUri->toString() != Uri::root()) { + // Remove trailingslash + $originalUri->setPath(substr($originalUri->getPath(), 0, -1)); + $this->app->redirect($originalUri->toString(), 301); + } elseif ($this->params->get('trailingslash') == 2 && substr($originalUri->getPath(), -1) != '/') { + // Add trailingslash + $originalUri->setPath($originalUri->getPath() . '/'); + $this->app->redirect($originalUri->toString(), 301); + } + } + /** * Check the buffer. * ```
zwiastunsw commented 5 months ago

rozwiązane w 5.0.3