From 583a8860ce606a4eb04dd766f4741341b04e4b25 Mon Sep 17 00:00:00 2001 From: Jesse Leite Date: Thu, 3 Aug 2023 18:15:39 -0400 Subject: [PATCH 1/4] Get actual urls off blinked paginator instance. --- src/Cascade.php | 50 ++++++++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/src/Cascade.php b/src/Cascade.php index 23af2662..923fe4ae 100644 --- a/src/Cascade.php +++ b/src/Cascade.php @@ -124,11 +124,15 @@ public function canonicalUrl() { $url = Str::trim($this->explicitUrl ?? $this->data->get('canonical_url')); - if (! app('request')->has('page')) { + if (! Str::startsWith($url, config('app.url'))) { return $url; } - $page = (int) app('request')->get('page'); + if (! $paginator = Blink::get('tag-paginator')) { + return $url; + } + + $page = $paginator->currentPage(); switch (true) { case config('statamic.seo-pro.pagination') === false: @@ -137,11 +141,7 @@ public function canonicalUrl() return $url; } - if (Str::startsWith($url, config('app.url'))) { - $url .= '?page='.$page; - } - - return $url; + return URL::makeAbsolute($paginator->url($page)); } protected function prevUrl() @@ -150,21 +150,25 @@ protected function prevUrl() return null; } + $url = Str::trim($this->data->get('canonical_url')); + + if (! Str::startsWith($url, config('app.url'))) { + return $url; + } + if (! $paginator = Blink::get('tag-paginator')) { return null; } - $url = Str::trim($this->data->get('canonical_url')); - - $page = $paginator->currentPage(); - - if (config('statamic.seo-pro.pagination.enabled_on_first_page') === false && $page === 2) { + if (config('statamic.seo-pro.pagination.enabled_on_first_page') === false && $paginator->currentPage() === 2) { return $url; } - return $page > 1 && $page <= $paginator->lastPage() - ? $url.'?page='.($page - 1) - : null; + if (! $prevUrl = $paginator->previousPageUrl()) { + return null; + } + + return URL::makeAbsolute($prevUrl); } protected function nextUrl() @@ -173,17 +177,21 @@ protected function nextUrl() return null; } + $url = Str::trim($this->data->get('canonical_url')); + + if (! Str::startsWith($url, config('app.url'))) { + return $url; + } + if (! $paginator = Blink::get('tag-paginator')) { return null; } - $url = Str::trim($this->data->get('canonical_url')); - - $page = $paginator->currentPage(); + if (! $nextUrl = $paginator->nextPageUrl()) { + return null; + } - return $page < $paginator->lastPage() - ? $url.'?page='.($page + 1) - : null; + return URL::makeAbsolute($nextUrl); } protected function parse($key, $item) From 1cafe6ee74ede53f99074188adce95cb011d69b5 Mon Sep 17 00:00:00 2001 From: Jesse Leite Date: Thu, 3 Aug 2023 18:41:04 -0400 Subject: [PATCH 2/4] Set base path on simulated paginator. --- tests/MetaTagTest.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/MetaTagTest.php b/tests/MetaTagTest.php index 9348cfa8..202e2410 100644 --- a/tests/MetaTagTest.php +++ b/tests/MetaTagTest.php @@ -767,8 +767,10 @@ protected function setCustomTwitterGlidePresetOnly($app) protected function simulatePageOutOfFive($currentPage) { - Blink::put('tag-paginator', new LengthAwarePaginator([], 15, 3, $currentPage)); + $url = '/about'; - return $this->call('GET', '/about', ['page' => $currentPage]); + Blink::put('tag-paginator', (new LengthAwarePaginator([], 15, 3, $currentPage))->setPath($url)); + + return $this->call('GET', $url, ['page' => $currentPage]); } } From 7886c1a54c3b89b756176d0b056725fac358db9a Mon Sep 17 00:00:00 2001 From: Jesse Leite Date: Thu, 3 Aug 2023 18:43:17 -0400 Subject: [PATCH 3/4] Allow pagination on canonical url itself if on same domain, to pass tests since we allowed that before. --- src/Cascade.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Cascade.php b/src/Cascade.php index 923fe4ae..f597c699 100644 --- a/src/Cascade.php +++ b/src/Cascade.php @@ -132,6 +132,8 @@ public function canonicalUrl() return $url; } + $paginator->setPath($url); + $page = $paginator->currentPage(); switch (true) { From c4d03afba9554ace434c23ee78c0570fe89dbad8 Mon Sep 17 00:00:00 2001 From: Jesse Leite Date: Fri, 4 Aug 2023 15:52:01 -0400 Subject: [PATCH 4/4] Add test coverage for custom pagination routing. --- tests/MetaTagTest.php | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/tests/MetaTagTest.php b/tests/MetaTagTest.php index 202e2410..447be3b8 100644 --- a/tests/MetaTagTest.php +++ b/tests/MetaTagTest.php @@ -5,6 +5,7 @@ use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\Route; +use Statamic\Extensions\Pagination\LengthAwarePaginator as StatamicLengthAwarePaginator; use Statamic\Facades\Blink; use Statamic\Facades\Collection; use Statamic\Facades\Config; @@ -594,6 +595,25 @@ public function it_can_apply_pagination_to_first_page_when_configured_as_unique_ $response->assertSee('', false); } + /** + * @test + * + * @dataProvider viewScenarioProvider + * + * @environment-setup useFakeSsgPaginator + */ + public function it_applies_custom_pagination_routing_to_meta_urls($viewType) + { + $this->withoutExceptionHandling(); + $this->prepareViews($viewType); + + $response = $this->simulatePageOutOfFive(3, FakeSsgPaginator::class); + $response->assertSee("

{$viewType}

", false); + $response->assertSee('', false); + $response->assertSee('', false); + $response->assertSee('', false); + } + /** * @test * @@ -765,12 +785,22 @@ protected function setCustomTwitterGlidePresetOnly($app) ]); } - protected function simulatePageOutOfFive($currentPage) + protected function simulatePageOutOfFive($currentPage, $customPaginatorClass = null) { $url = '/about'; - Blink::put('tag-paginator', (new LengthAwarePaginator([], 15, 3, $currentPage))->setPath($url)); + $paginatorClass = $customPaginatorClass ?? LengthAwarePaginator::class; + + Blink::put('tag-paginator', (new $paginatorClass([], 15, 3, $currentPage))->setPath($url)); return $this->call('GET', $url, ['page' => $currentPage]); } } + +class FakeSsgPaginator extends StatamicLengthAwarePaginator +{ + public function url($page) + { + return \Statamic\Facades\URL::makeRelative(sprintf('%s/page/%s', $this->path(), $page)); + } +}