diff --git a/libraries/src/Pagination/Pagination.php b/libraries/src/Pagination/Pagination.php index cd9002b7d67b0..1d2d3351dc4bc 100644 --- a/libraries/src/Pagination/Pagination.php +++ b/libraries/src/Pagination/Pagination.php @@ -15,6 +15,7 @@ use Joomla\CMS\Language\Text; use Joomla\CMS\Layout\LayoutHelper; use Joomla\CMS\Router\Route; +use Joomla\Filter\InputFilter; // phpcs:disable PSR1.Files.SideEffects \defined('JPATH_PLATFORM') or die; @@ -96,6 +97,31 @@ class Pagination */ protected $additionalUrlParams = []; + /** + * List of parameters that will be added from request automatically. + * When exists they will be added to the $additionalUrlParams list, while pagination initialisation. + * + * In format key => filter + * + * @var string[] + * + * @since __DEPLOY_VERSION__ + */ + protected $paramsFromRequest = [ + 'format' => 'CMD', + 'option' => 'CMD', + 'controller' => 'CMD', + 'view' => 'CMD', + 'layout' => 'STRING', + 'task' => 'CMD', + 'template' => 'CMD', + 'templateStyle' => 'INT', + 'tmpl' => 'CMD', + 'tpl' => 'CMD', + 'id' => 'STRING', + 'Itemid' => 'INT', + ]; + /** * @var CMSApplication The application object * @since 3.4 @@ -177,6 +203,40 @@ public function __construct($total, $limitstart, $limit, $prefix = '', CMSApplic if ($limit === 0) { $this->viewall = true; } + + $this->setUrlParamsFromRequest(); + } + + /** + * Set URL parameters from request. + * + * @return void + * + * @since __DEPLOY_VERSION__ + */ + protected function setUrlParamsFromRequest() + { + // Get the requested parameters from the router + $client = $this->app->getName(); + $router = Factory::getContainer()->get(ucfirst($client) . 'Router'); + $filter = new InputFilter(); + + // It is applicable only for CMS router. API router works differently. + if (!$router instanceof \Joomla\CMS\Router\Router) { + return; + } + + // Filter them and add to the params list + foreach ($router->getVars() as $key => $value) { + // Check if the parameter is allowed + if (empty($this->paramsFromRequest[$key])) { + continue; + } + + $filterMethod = $this->paramsFromRequest[$key]; + + $this->setAdditionalUrlParam($key, $filter->clean($value, $filterMethod)); + } } /** @@ -663,36 +723,8 @@ protected function _buildDataObject() { $data = new \stdClass(); - // Platform defaults - $defaultUrlParams = [ - 'format' => 'CMD', - 'option' => 'CMD', - 'controller' => 'CMD', - 'view' => 'CMD', - 'layout' => 'STRING', - 'task' => 'CMD', - 'template' => 'CMD', - 'templateStyle' => 'INT', - 'tmpl' => 'CMD', - 'tpl' => 'CMD', - 'id' => 'STRING', - 'Itemid' => 'INT', - ]; - // Prepare the routes $params = []; - $input = $this->app->getInput(); - - // Use platform defaults if parameter doesn't already exist. - foreach ($defaultUrlParams as $param => $filter) { - $value = $input->get($param, null, $filter); - - if ($value === null) { - continue; - } - - $params[$param] = $value; - } if (!empty($this->additionalUrlParams)) { foreach ($this->additionalUrlParams as $key => $value) {