Skip to content

Commit 2274b54

Browse files
refactor(polylang): remove redundant resolvers, add archive link resolver, harden for non-polylang
- Remove ResolveHomeUrl (dead code; home_url already handled by Polylang core) - Remove ResolvePageTreeMenuPageIds (option_page_on_front/page_for_posts handled natively by Polylang; cache key duplicated ResolveNavigationCacheKey) - Remove ResolveCurrentPostId (primary branch was a no-op — option_page_for_<pt> is already translated by ResolveTranslatedPostTypeLink before the helper reads it) - Unregister removed resolvers from App.php - Add ResolveTranslatedPostTypeArchiveLink to translate post_type_archive_link when the wp-page-for-posttype plugin bakes the default-language slug into the registered post type rewrite (with full unit tests) - Early-return in ResolveTranslatedPostTypeLink::registerPostTypeHooks when Polylang is not active to avoid iterating all public post types on non-Polylang sites - Skip registering the pre_get_posts action in ResolveFontAttachmentQueries when Polylang is not active so non-Polylang sites pay no per-query cost Agent-Logs-Url: https://github.com/helsingborg-stad/Municipio/sessions/16905c3e-4839-4e00-bf43-e1cf0cd39e72 Co-authored-by: sebastianthulin <797129+sebastianthulin@users.noreply.github.com>
1 parent 9e90431 commit 2274b54

13 files changed

Lines changed: 367 additions & 639 deletions

library/App.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -510,12 +510,11 @@ private function setUpPolylangIntegration(): void
510510

511511
$resolvers = [
512512
new \Municipio\Integrations\Polylang\ResolveLanguageMenuItems($this->wpService),
513-
new \Municipio\Integrations\Polylang\ResolvePageTreeMenuPageIds($this->wpService),
514-
new \Municipio\Integrations\Polylang\ResolveCurrentPostId($this->wpService),
515513
new \Municipio\Integrations\Polylang\ResolvePageTreeTranslatedChildren($this->wpService),
516514
new \Municipio\Integrations\Polylang\ResolveNavigationItemsLanguage($this->wpService),
517515
new \Municipio\Integrations\Polylang\ResolveTranslatedPageLink($this->wpService),
518516
new \Municipio\Integrations\Polylang\ResolveTranslatedPostTypeLink($this->wpService),
517+
new \Municipio\Integrations\Polylang\ResolveTranslatedPostTypeArchiveLink($this->wpService),
519518
new \Municipio\Integrations\Polylang\ResolveNavigationCacheKey($this->wpService),
520519
new \Municipio\Integrations\Polylang\ResolveNavigationFetchUrlLanguage($this->wpService),
521520
new \Municipio\Integrations\Polylang\ResolvePdfNotFoundUrl($this->wpService),

library/Integrations/Polylang/ResolveCurrentPostId.php

Lines changed: 0 additions & 175 deletions
This file was deleted.

library/Integrations/Polylang/ResolveCurrentPostIdTest.php

Lines changed: 0 additions & 103 deletions
This file was deleted.

library/Integrations/Polylang/ResolveFontAttachmentQueries.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ public function __construct(
3131
*/
3232
public function addHooks(): void
3333
{
34+
// Skip registering the pre_get_posts listener entirely on non-Polylang
35+
// sites so non-Polylang requests pay no per-query cost.
36+
if (!$this->isPolylangActive()) {
37+
return;
38+
}
39+
3440
$this->wpService->addAction('pre_get_posts', [$this, 'makeFontAttachmentQueryLanguageAgnostic'], 1);
3541
}
3642

library/Integrations/Polylang/ResolveFontAttachmentQueriesTest.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,41 @@ public function testCanBeInstantiated(): void
1818
static::assertInstanceOf(ResolveFontAttachmentQueries::class, $this->getSut());
1919
}
2020

21-
#[TestDox('addHooks() registers the pre_get_posts action')]
21+
#[TestDox('addHooks() registers the pre_get_posts action when Polylang is active')]
2222
public function testAddHooksRegistersPreGetPostsAction(): void
2323
{
2424
$wpService = new FakeWpService([
2525
'addAction' => true,
2626
]);
2727

28-
$sut = new ResolveFontAttachmentQueries($wpService);
28+
$sut = new ResolveFontAttachmentQueries(
29+
$wpService,
30+
static fn (): bool => true
31+
);
2932

3033
$sut->addHooks();
3134

3235
static::assertSame('pre_get_posts', $wpService->methodCalls['addAction'][0][0]);
3336
static::assertSame(1, $wpService->methodCalls['addAction'][0][2]);
3437
}
3538

39+
#[TestDox('addHooks() does not register the pre_get_posts action when Polylang is unavailable')]
40+
public function testAddHooksSkipsPreGetPostsActionWhenPolylangInactive(): void
41+
{
42+
$wpService = new FakeWpService([
43+
'addAction' => true,
44+
]);
45+
46+
$sut = new ResolveFontAttachmentQueries(
47+
$wpService,
48+
static fn (): bool => false
49+
);
50+
51+
$sut->addHooks();
52+
53+
static::assertArrayNotHasKey('addAction', $wpService->methodCalls);
54+
}
55+
3656
#[TestDox('makeFontAttachmentQueryLanguageAgnostic() updates font attachment queries with array mime types')]
3757
public function testMakeFontAttachmentQueryLanguageAgnosticUpdatesArrayMimeTypeQuery(): void
3858
{

0 commit comments

Comments
 (0)