Skip to content

Commit

Permalink
43303 Improve pagination (#738)
Browse files Browse the repository at this point in the history
* 43303 improve pagination

* 43303 update unit test

* 43303 update unit test

* 43303 refactor code

---------

Co-authored-by: Frederic Alpers <88546396+fredericalpers@users.noreply.github.com>
Co-authored-by: Anja Möller <22166320+andernath@users.noreply.github.com>
  • Loading branch information
3 people authored May 6, 2024
1 parent 51814fd commit 3b21550
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public function render(array $attributes): string
$pSortListModel = $this->_pSortListBuilder->build($pListView);
$pListViewWithSortParams = $this->listViewWithSortParams($pListView, $pSortListModel);

$this->registerNewPageLinkArgs($pListViewWithSortParams, $pSortListModel);
$this->registerNewPageLinkArgs($pListViewWithSortParams, $pSortListModel, false);
$pListViewFilterBuilder = $this->_pDefaultFilterBuilderFactory
->buildDefaultListViewFilter($pListViewWithSortParams);

Expand All @@ -130,11 +130,14 @@ public function render(array $attributes): string
$pEstateList->setUnitsViewName($attributes['units']);
$pEstateList->setGeoSearchBuilder($pGeoSearchBuilder);

$pEstateList->loadEstates($this->_pWPQueryWrapper->getWPQuery()->get('paged', 1) ?: 1);
$pEstateList->loadEstates($this->_pWPQueryWrapper->getWPQuery($pListView->getId())->get('paged', 1) ?: 1);
$pTemplate = $this->_pTemplate
->withTemplateName($pListViewWithSortParams->getTemplate())
->withEstateList($pEstateList);
$result = $pTemplate->render();
$result = '<div id="'.str_replace(' ', '-', $attributes['view']).'">';
$result .= $pTemplate->render();
$result .= '</div>';
$this->registerNewPageLinkArgs($pListViewWithSortParams, $pSortListModel, true);
}
return $result;
}
Expand All @@ -156,14 +159,15 @@ private function listViewWithSortParams(DataListView $pDataView,
/**
* @param DataListView $pListView
* @param SortListDataModel $pSortListDataModel
* @param bool $pCheckPaginationTheme
* @throws DependencyException
* @throws NotFoundException
* @throws UnknownFieldException
*/
private function registerNewPageLinkArgs(DataListView $pListView, SortListDataModel $pSortListDataModel)
private function registerNewPageLinkArgs(DataListView $pListView, SortListDataModel $pSortListDataModel, bool $pCheckPaginationTheme = false)
{
$pModel = $this->_pSearchParametersModelBuilderEstate
->buildSearchParametersModel($pListView, $pSortListDataModel);
$this->_pSearchParameters->registerNewPageLinkArgs($pModel);
$this->_pSearchParameters->registerNewPageLinkArgs($pModel, $pListView->getId(), $pCheckPaginationTheme);
}
}
22 changes: 22 additions & 0 deletions plugin/Controller/SearchParametersModelBuilderEstate.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ public function buildSearchParametersModel(
if ($pSortListDataModel->isAdjustableSorting()) {
$this->addForAdjustableSorting($pModel, $pDataView->getId());
}
$this->addParametersForPagination($pModel);

return $pModel;
}

Expand All @@ -103,6 +105,26 @@ private function addForAdjustableSorting(SearchParametersModel $pSearchParameter
}
}

/**
* @param SearchParametersModel $pSearchParametersModel
*/
private function addParametersForPagination(SearchParametersModel $pSearchParametersModel)
{
$pageParameters = [];

foreach ($_GET as $key => $value) {
if (strpos($key, 'page_of_id_') === 0) {
$pageParameters[] = $key;
}
}

foreach ($pageParameters as $urlParameter) {
$pSearchParametersModel->addAllowedGetParameter($urlParameter);
$pSearchParametersModel->setParameter($urlParameter,
$this->_pRequestVariablesSanitizer->getFilteredGet($urlParameter));
}
}

/**
* @param string $filterableField
* @param FieldsCollection $pFieldsCollection
Expand Down
30 changes: 25 additions & 5 deletions plugin/Filter/SearchParameters/SearchParameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,13 @@ class SearchParameters
* @param string $link
* @param int $i
* @param SearchParametersModel $pModel
* @param int $pListViewId
* @param bool $pCheckPaginationTheme
* @return string
* @global int $page
* @global bool $more
*/
public function linkPagesLink(string $link, int $i, SearchParametersModel $pModel): string
public function linkPagesLink(string $link, int $i, SearchParametersModel $pModel, int $pListViewId=0, bool $pCheckPaginationTheme=false): string
{
global $paged, $more;

Expand All @@ -65,7 +67,9 @@ public function linkPagesLink(string $link, int $i, SearchParametersModel $pMode
$link = $linkparams['link_before'].str_replace('%', $i, $linkparams['pagelink'])
.$linkparams['link_after'];
if ($i != $paged || ! $more && 1 == $paged) {
$url = $this->geturl( $i, $pModel->getParameters() );
$url = !$pCheckPaginationTheme && !empty($pListViewId) ?
$this->getUrlByListViewId($i, $pModel->getParameters(), $pListViewId) :
$this->geturl($i, $pModel->getParameters());
$output .= '<a href="'.esc_url($url).'">'.$link.'</a>';
} else {
$output .= $link;
Expand Down Expand Up @@ -104,13 +108,29 @@ private function geturl($i, array $parameters): string
return add_query_arg($parameters, $url);
}

/**
* @param int $i
* @param array $parameters
* @param int $id
* @return string
*/
private function getUrlByListViewId(int $i, array $parameters, int $pListViewId): string
{
$url = get_permalink();
$parameter = $parameters;
$parameter['page_of_id_'.$pListViewId] = $i;
return add_query_arg($parameter, $url);
}

/**
* @param SearchParametersModel $pSearchParametersModel
* @param int $pListViewId
* @param bool $pCheckPaginationTheme
*/
public function registerNewPageLinkArgs(SearchParametersModel $pSearchParametersModel)
public function registerNewPageLinkArgs(SearchParametersModel $pSearchParametersModel, int $pListViewId, bool $pCheckPaginationTheme)
{
add_filter('wp_link_pages_link', function(string $link, int $i) use ($pSearchParametersModel): string {
return $this->linkPagesLink($link, $i, $pSearchParametersModel);
add_filter('wp_link_pages_link', function(string $link, int $i) use ($pSearchParametersModel, $pListViewId, $pCheckPaginationTheme): string {
return $this->linkPagesLink($link, $i, $pSearchParametersModel, $pListViewId, $pCheckPaginationTheme);
}, 10, 2);
add_filter('wp_link_pages_args', [$pSearchParametersModel, 'populateDefaultLinkParams']);
}
Expand Down
1 change: 0 additions & 1 deletion plugin/RequestVariablesSanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public function getFilteredGet(string $name, int $filter = FILTER_DEFAULT, $opti
return $this->getFiltered($_GET, $name, $filter, $option);
}


/**
*
* @param string $name
Expand Down
8 changes: 6 additions & 2 deletions plugin/WP/WPQueryWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,21 @@ class WPQueryWrapper
{
/**
*
* @param int $pListViewId
* @global \WP_Query $wp_query
* @return WP_Query
*
*/

public function getWPQuery(): WP_Query
public function getWPQuery(int $pListViewId = null): WP_Query
{
global $wp_query, $paged;
$wpquery = clone $wp_query;

if (get_query_var( 'paged' )) {
$pageParameter = 'page_of_id_' . $pListViewId;
if (isset($_GET[$pageParameter]) && is_numeric($_GET[$pageParameter])) {
$paged = (int) $_GET[$pageParameter];
} elseif (get_query_var( 'paged' )) {
$paged = get_query_var('paged');
}
elseif ( get_query_var('page')) {
Expand Down
15 changes: 15 additions & 0 deletions tests/TestClassWPQueryWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,19 @@ public function testGetWpQueryWithPagedAndPage()
$this->assertEquals(3, $pWPQueryWrapper->getWPQuery()->get('paged'));
wp_reset_query();
}

/**
*
*/
public function testGetWpQueryWithMultiplePage()
{
$_GET = [
'page_of_id_4' => 4
];
global $wp_query;
$wp_query = new WP_Query(['page' => 2, 'paged'=> 3]);
$pWPQueryWrapper = new WPQueryWrapper();
$this->assertEquals(4, $pWPQueryWrapper->getWPQuery(4)->get('paged'));
wp_reset_query();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#: Show Details
<div id="test_view_list">#: Show Details
label-objekttitel: flach begrüntes Grundstück
label-objektart: Grundstück
label-objekttyp: Wohnen
Expand All @@ -12,3 +12,4 @@ label-objektbeschreibung: große Freifläche
label-lage: Das Grundstück liegt am Waldrand und ist über einen geteerten Feldweg erreichbar.
label-ausstatt_beschr: teilweise mit einer alten Mauer aus Findlingen umgeben
label-sonstige_angaben: Vereinbaren Sie noch heute einen Besichtigungstermin
</div>

0 comments on commit 3b21550

Please sign in to comment.