Skip to content

Commit 955091e

Browse files
feat(polylang): move font attachment query handling to integration
Agent-Logs-Url: https://github.com/helsingborg-stad/Municipio/sessions/26b2264a-26a1-4a7d-91fa-b5aceaa535e8 Co-authored-by: sebastianthulin <797129+sebastianthulin@users.noreply.github.com>
1 parent 56b5c7e commit 955091e

4 files changed

Lines changed: 243 additions & 24 deletions

File tree

library/App.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,11 @@ public function __construct(
227227
$resolvePageTreeMenuPageIds = new \Municipio\Integrations\Polylang\ResolvePageTreeMenuPageIds($this->wpService);
228228
$resolveNavigationCacheKey = new \Municipio\Integrations\Polylang\ResolveNavigationCacheKey($this->wpService);
229229
$resolvePdfNotFoundUrl = new \Municipio\Integrations\Polylang\ResolvePdfNotFoundUrl($this->wpService);
230+
$resolveFontAttachmentQueries = new \Municipio\Integrations\Polylang\ResolveFontAttachmentQueries($this->wpService);
230231
$this->hooksRegistrar->register($resolvePageTreeMenuPageIds);
231232
$this->hooksRegistrar->register($resolveNavigationCacheKey);
232233
$this->hooksRegistrar->register($resolvePdfNotFoundUrl);
234+
$this->hooksRegistrar->register($resolveFontAttachmentQueries);
233235

234236
/* Admin uploads */
235237
$uploads = new \Municipio\Admin\Uploads();

library/Content/PostFilters.php

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ class PostFilters
1414
*/
1515
public function __construct()
1616
{
17-
add_action('pre_get_posts', array($this, 'suppressFiltersOnFontAttachments'));
18-
1917
add_action('parse_query', array($this, 'handleQuery'));
2018

2119
remove_filter('content_save_pre', 'wp_filter_post_kses');
@@ -35,26 +33,4 @@ public function handleQuery($query)
3533
}
3634
}
3735

38-
/**
39-
* Suppress filters on font attachments
40-
*/
41-
public function suppressFiltersOnFontAttachments($query)
42-
{
43-
/**
44-
* Suppress filters for font attachments in queries
45-
*
46-
* @param WP_Query $query
47-
* @return void
48-
*/
49-
if (
50-
$query->get('post_type') == 'attachment' && is_array($query->get('post_mime_type')) &&
51-
!empty(array_filter($query->get('post_mime_type'), function ($item) {
52-
return strpos($item, 'font') !== false;
53-
}))
54-
) {
55-
$query->set('suppress_filters', true);
56-
}
57-
58-
return $query;
59-
}
6036
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Municipio\Integrations\Polylang;
6+
7+
use Closure;
8+
use Municipio\HooksRegistrar\Hookable;
9+
use WP_Query;
10+
use WpService\Contracts\AddAction;
11+
12+
/**
13+
* Makes font attachment queries language agnostic when Polylang is active.
14+
*/
15+
class ResolveFontAttachmentQueries implements Hookable
16+
{
17+
/**
18+
* Constructor.
19+
*
20+
* @param AddAction $wpService The WordPress service.
21+
* @param ?Closure $polylangIsActiveResolver Optional Polylang availability resolver.
22+
*/
23+
public function __construct(
24+
private AddAction $wpService,
25+
private ?Closure $polylangIsActiveResolver = null
26+
) {
27+
}
28+
29+
/**
30+
* @inheritDoc
31+
*/
32+
public function addHooks(): void
33+
{
34+
$this->wpService->addAction('pre_get_posts', [$this, 'makeFontAttachmentQueryLanguageAgnostic'], 1);
35+
}
36+
37+
/**
38+
* Make font attachment queries language agnostic.
39+
*
40+
* @param WP_Query $query The query to inspect.
41+
*
42+
* @return void
43+
*/
44+
public function makeFontAttachmentQueryLanguageAgnostic(WP_Query $query): void
45+
{
46+
if (!$this->isPolylangActive() || !$this->isFontAttachmentQuery($query)) {
47+
return;
48+
}
49+
50+
$query->set('lang', '');
51+
$query->set('suppress_filters', true);
52+
}
53+
54+
/**
55+
* Determine whether the query targets font attachments.
56+
*
57+
* @param WP_Query $query The query to inspect.
58+
*
59+
* @return bool True when the query targets font attachments, otherwise false.
60+
*/
61+
private function isFontAttachmentQuery(WP_Query $query): bool
62+
{
63+
if ($query->get('post_type') !== 'attachment') {
64+
return false;
65+
}
66+
67+
$postMimeType = $query->get('post_mime_type');
68+
69+
if (is_string($postMimeType)) {
70+
return str_contains($postMimeType, 'font');
71+
}
72+
73+
if (!is_array($postMimeType)) {
74+
return false;
75+
}
76+
77+
foreach ($postMimeType as $mimeType) {
78+
if (is_string($mimeType) && str_contains($mimeType, 'font')) {
79+
return true;
80+
}
81+
}
82+
83+
return false;
84+
}
85+
86+
/**
87+
* Determine whether Polylang is active.
88+
*
89+
* @return bool True when Polylang is active, otherwise false.
90+
*/
91+
private function isPolylangActive(): bool
92+
{
93+
return $this->getPolylangIsActiveResolver()?->__invoke() ?? false;
94+
}
95+
96+
/**
97+
* Get the Polylang availability resolver.
98+
*
99+
* @return ?Closure The Polylang availability resolver.
100+
*/
101+
private function getPolylangIsActiveResolver(): ?Closure
102+
{
103+
if ($this->polylangIsActiveResolver instanceof Closure) {
104+
return $this->polylangIsActiveResolver;
105+
}
106+
107+
if (!is_callable('pll_current_language')) {
108+
return null;
109+
}
110+
111+
return static fn (): bool => true;
112+
}
113+
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Municipio\Integrations\Polylang;
6+
7+
use Closure;
8+
use PHPUnit\Framework\Attributes\TestDox;
9+
use PHPUnit\Framework\TestCase;
10+
use WP_Query;
11+
use WpService\Implementations\FakeWpService;
12+
13+
class ResolveFontAttachmentQueriesTest extends TestCase
14+
{
15+
#[TestDox('class can be instantiated')]
16+
public function testCanBeInstantiated(): void
17+
{
18+
static::assertInstanceOf(ResolveFontAttachmentQueries::class, $this->getSut());
19+
}
20+
21+
#[TestDox('addHooks() registers the pre_get_posts action')]
22+
public function testAddHooksRegistersPreGetPostsAction(): void
23+
{
24+
$wpService = new FakeWpService([
25+
'addAction' => true,
26+
]);
27+
28+
$sut = new ResolveFontAttachmentQueries($wpService);
29+
30+
$sut->addHooks();
31+
32+
static::assertSame('pre_get_posts', $wpService->methodCalls['addAction'][0][0]);
33+
static::assertSame(1, $wpService->methodCalls['addAction'][0][2]);
34+
}
35+
36+
#[TestDox('makeFontAttachmentQueryLanguageAgnostic() updates font attachment queries with array mime types')]
37+
public function testMakeFontAttachmentQueryLanguageAgnosticUpdatesArrayMimeTypeQuery(): void
38+
{
39+
$query = new WP_Query();
40+
$query->query_vars = [
41+
'post_type' => 'attachment',
42+
'post_mime_type' => ['application/font-woff', 'font/woff2'],
43+
];
44+
45+
$sut = $this->getSut(
46+
polylangIsActiveResolver: static fn (): bool => true
47+
);
48+
49+
$sut->makeFontAttachmentQueryLanguageAgnostic($query);
50+
51+
static::assertSame('', $query->get('lang'));
52+
static::assertTrue($query->get('suppress_filters'));
53+
}
54+
55+
#[TestDox('makeFontAttachmentQueryLanguageAgnostic() updates font attachment queries with string mime types')]
56+
public function testMakeFontAttachmentQueryLanguageAgnosticUpdatesStringMimeTypeQuery(): void
57+
{
58+
$query = new WP_Query();
59+
$query->query_vars = [
60+
'post_type' => 'attachment',
61+
'post_mime_type' => 'application/font-woff',
62+
];
63+
64+
$sut = $this->getSut(
65+
polylangIsActiveResolver: static fn (): bool => true
66+
);
67+
68+
$sut->makeFontAttachmentQueryLanguageAgnostic($query);
69+
70+
static::assertSame('', $query->get('lang'));
71+
static::assertTrue($query->get('suppress_filters'));
72+
}
73+
74+
#[TestDox('makeFontAttachmentQueryLanguageAgnostic() does not update non-font attachment queries')]
75+
public function testMakeFontAttachmentQueryLanguageAgnosticDoesNotUpdateNonFontQueries(): void
76+
{
77+
$query = new WP_Query();
78+
$query->query_vars = [
79+
'post_type' => 'attachment',
80+
'post_mime_type' => 'image/jpeg',
81+
];
82+
83+
$sut = $this->getSut(
84+
polylangIsActiveResolver: static fn (): bool => true
85+
);
86+
87+
$sut->makeFontAttachmentQueryLanguageAgnostic($query);
88+
89+
static::assertNull($query->get('lang'));
90+
static::assertNull($query->get('suppress_filters'));
91+
}
92+
93+
#[TestDox('makeFontAttachmentQueryLanguageAgnostic() does not update queries when Polylang is unavailable')]
94+
public function testMakeFontAttachmentQueryLanguageAgnosticDoesNotUpdateQueriesWhenPolylangIsUnavailable(): void
95+
{
96+
$query = new WP_Query();
97+
$query->query_vars = [
98+
'post_type' => 'attachment',
99+
'post_mime_type' => 'application/font-woff',
100+
];
101+
102+
$sut = $this->getSut(
103+
polylangIsActiveResolver: static fn (): bool => false
104+
);
105+
106+
$sut->makeFontAttachmentQueryLanguageAgnostic($query);
107+
108+
static::assertNull($query->get('lang'));
109+
static::assertNull($query->get('suppress_filters'));
110+
}
111+
112+
/**
113+
* Get the system under test.
114+
*
115+
* @param ?Closure $polylangIsActiveResolver Optional Polylang availability resolver.
116+
*
117+
* @return ResolveFontAttachmentQueries The system under test.
118+
*/
119+
private function getSut(?Closure $polylangIsActiveResolver = null): ResolveFontAttachmentQueries
120+
{
121+
return new ResolveFontAttachmentQueries(
122+
new FakeWpService([
123+
'addAction' => true,
124+
]),
125+
$polylangIsActiveResolver
126+
);
127+
}
128+
}

0 commit comments

Comments
 (0)