Skip to content

Commit 44c46b3

Browse files
committed
Set canonicalURL in AbstractGridViewPage
1 parent d9e4b29 commit 44c46b3

1 file changed

Lines changed: 63 additions & 1 deletion

File tree

wcfsetup/install/files/lib/page/AbstractGridViewPage.class.php

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use wcf\system\request\LinkHandler;
77
use wcf\system\gridView\AbstractGridView;
88
use wcf\system\WCF;
9+
use wcf\util\StringUtil;
910

1011
/**
1112
* Abstract implementation of a page that is rendering a grid view.
@@ -49,6 +50,8 @@ public function readParameters()
4950
if (isset($_REQUEST['filters']) && \is_array($_REQUEST['filters'])) {
5051
$this->filters = $_REQUEST['filters'];
5152
}
53+
54+
$this->canonicalURL = $this->getCanonicalUrl();
5255
}
5356

5457
#[\Override]
@@ -66,6 +69,7 @@ public function assignVariables()
6669

6770
WCF::getTPL()->assign([
6871
'gridView' => $this->gridView,
72+
'headContent' => $this->getHeadContent(),
6973
]);
7074
}
7175

@@ -88,7 +92,65 @@ protected function initGridView(): void
8892
if ($this->pageNo != 1) {
8993
$this->gridView->setPageNo($this->pageNo);
9094
}
91-
$this->gridView->setBaseUrl(LinkHandler::getInstance()->getControllerLink(static::class));
95+
$this->gridView->setBaseUrl(
96+
LinkHandler::getInstance()->getControllerLink(static::class, $this->getBaseUrlParameters())
97+
);
98+
}
99+
100+
/**
101+
* @return array<string, mixed>
102+
*/
103+
protected function getBaseUrlParameters(): array
104+
{
105+
return [];
106+
}
107+
108+
protected function getHeadContent(): string
109+
{
110+
$linkTags = [];
111+
if ($this->gridView->getPageNo() < $this->gridView->countPages()) {
112+
$linkTags[] = \sprintf(
113+
'<link rel="next" href="%s">',
114+
StringUtil::encodeHTML(
115+
LinkHandler::getInstance()->getControllerLink(static::class, \array_merge(
116+
$this->getBaseUrlParameters(),
117+
[
118+
'pageNo' => $this->gridView->getPageNo() + 1,
119+
'sortField' => $this->sortField ?: null,
120+
'sortOrder' => $this->sortOrder ?: null,
121+
]
122+
))
123+
)
124+
);
125+
}
126+
127+
if ($this->gridView->getPageNo() > 1) {
128+
$linkTags[] = \sprintf(
129+
'<link rel="prev" href="%s">',
130+
StringUtil::encodeHTML(
131+
LinkHandler::getInstance()->getControllerLink(static::class, \array_merge(
132+
$this->getBaseUrlParameters(),
133+
[
134+
'pageNo' => $this->gridView->getPageNo() !== 2 ? $this->gridView->getPageNo() - 1 : null,
135+
'sortField' => $this->sortField ?: null,
136+
'sortOrder' => $this->sortOrder ?: null,
137+
]
138+
))
139+
)
140+
);
141+
}
142+
143+
return \implode("\n", $linkTags);
144+
}
145+
146+
protected function getCanonicalUrl(): string
147+
{
148+
return LinkHandler::getInstance()->getControllerLink(static::class, \array_merge(
149+
$this->getBaseUrlParameters(),
150+
[
151+
'pageNo' => $this->pageNo !== 1 ? $this->pageNo : null,
152+
]
153+
));
92154
}
93155

94156
/**

0 commit comments

Comments
 (0)