Skip to content

Commit 7eb1978

Browse files
committed
♻️ Refactoring(chat): move filter to admin file
and rename pages to avoid potential conflicts
1 parent a272a09 commit 7eb1978

7 files changed

Lines changed: 28 additions & 24 deletions

File tree

library/AcfFields/json/options-chat.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@
327327
{
328328
"key": "field_6a21301b299ff",
329329
"label": "Pages",
330-
"name": "pages",
330+
"name": "chat_assistant_pages",
331331
"aria-label": "",
332332
"type": "post_object",
333333
"instructions": "Pages to use this assistant on for the chat bubble.",

library/AcfFields/php/options-chat.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@
330330
2 => array(
331331
'key' => 'field_6a21301b299ff',
332332
'label' => __('Pages', 'municipio'),
333-
'name' => 'pages',
333+
'name' => 'chat_assistant_pages',
334334
'aria-label' => '',
335335
'type' => 'post_object',
336336
'instructions' => __('Pages to use this assistant on for the chat bubble.', 'municipio'),

library/Chat/Admin/ChatAdminPage.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,22 @@
66
use Municipio\HooksRegistrar\Hookable;
77
use WpService\Contracts\__;
88
use WpService\Contracts\AddAction;
9+
use WpService\Contracts\AddFilter;
10+
use WpService\Contracts\GetPostTypeObject;
11+
use WpService\Contracts\IsPostTypeHierarchical;
12+
use WpService\Contracts\IsPostTypeViewable;
913

1014
class ChatAdminPage implements Hookable
1115
{
1216
public function __construct(
13-
private __&AddAction $wpService,
17+
private __&AddAction&AddFilter&GetPostTypeObject&IsPostTypeHierarchical&IsPostTypeViewable $wpService,
1418
private AddOptionsPage $acfService,
1519
) {}
1620

1721
public function addHooks(): void
1822
{
1923
$this->wpService->addAction('init', [$this, 'register']);
24+
$this->wpService->addFilter('acf/fields/post_object/query/name=chat_assistant_pages', [$this, 'filterAssistantPagesField'], 10, 1);
2025
}
2126

2227
public function register(): void
@@ -32,4 +37,15 @@ public function register(): void
3237
'icon_url' => 'dashicons-format-chat',
3338
]);
3439
}
40+
41+
public function filterAssistantPagesField(array $args): array
42+
{
43+
$wpService = $this->wpService;
44+
$args['post_type'] = array_filter($args['post_type'] ?? [], static function ($postType) use ($wpService) {
45+
$postObj = $wpService->getPostTypeObject($postType);
46+
return $postObj && $wpService->isPostTypeHierarchical($postType) && $wpService->isPostTypeViewable($postObj);
47+
});
48+
49+
return $args;
50+
}
3551
}

library/Chat/ChatFeature.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ public function enable(): void
3535

3636
(new ChatAdminPage($this->wpService, $this->acfService))->addHooks();
3737

38-
$this->wpService->addFilter('acf/fields/post_object/query/name=pages', [$this, 'filterPostObjectField'], 10, 3);
39-
4038
if (!$config->isEnabled()) {
4139
return;
4240
}
@@ -56,15 +54,4 @@ public function enable(): void
5654

5755
(new ChatStatsMetaBox($this->wpService, $bladeRenderer))->addHooks();
5856
}
59-
60-
public function filterPostObjectField($args, $field, $postId)
61-
{
62-
$wpService = $this->wpService;
63-
$args['post_type'] = array_filter($args['post_type'] ?? [], static function ($postType) use ($wpService) {
64-
$postObj = $wpService->getPostTypeObject($postType);
65-
return $postObj && $wpService->isPostTypeHierarchical($postType) && $wpService->isPostTypeViewable($postObj);
66-
});
67-
68-
return $args;
69-
}
7057
}

library/Chat/ChatFeatureTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public function testClassCanBeInstantiated(): void
2727
public function testEnableCanBeCalled(): void
2828
{
2929
$wpService = new FakeWpService([
30+
'addFilter' => true,
3031
'addAction' => true,
3132
]);
3233
$acfService = new FakeAcfService([

library/Chat/Config/ChatConfig.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ public function getAssistantForActiveQuery(): ?array
4949

5050
foreach ($postChain as $queriedPostId) {
5151
foreach ($assistants as $assistant) {
52-
if (empty($assistant['pages']) || !is_array($assistant['pages'])) {
52+
if (empty($assistant['chat_assistant_pages']) || !is_array($assistant['chat_assistant_pages'])) {
5353
continue;
5454
}
5555

56-
if (in_array($queriedPostId, $assistant['pages'], true)) {
56+
if (in_array($queriedPostId, $assistant['chat_assistant_pages'], true)) {
5757
return $assistant;
5858
}
5959
}

library/Chat/Config/ChatConfigTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ public function testGetAssistantForActiveQueryReturnsDefaultWhenNoAssistantMatch
119119
public function testGetAssistantForActiveQueryReturnsMatchingAssistant(): void
120120
{
121121
$assistants = [
122-
['name' => 'Ava', 'pages' => [123]],
123-
['name' => 'Noah', 'pages' => [456]],
122+
['name' => 'Ava', 'chat_assistant_pages' => [123]],
123+
['name' => 'Noah', 'chat_assistant_pages' => [456]],
124124
];
125125

126126
$config = new ChatConfig(
@@ -135,8 +135,8 @@ public function testGetAssistantForActiveQueryReturnsMatchingAssistant(): void
135135
public function testGetAssistantForActiveQueryReturnsInheritedAssistant(): void
136136
{
137137
$assistants = [
138-
['name' => 'Ava', 'pages' => [123]],
139-
['name' => 'Noah', 'pages' => [456]],
138+
['name' => 'Ava', 'chat_assistant_pages' => [123]],
139+
['name' => 'Noah', 'chat_assistant_pages' => [456]],
140140
];
141141

142142
$config = new ChatConfig(
@@ -151,8 +151,8 @@ public function testGetAssistantForActiveQueryReturnsInheritedAssistant(): void
151151
public function testGetAssistantForActiveQueryReturnsMostSpecificMatch(): void
152152
{
153153
$assistants = [
154-
['name' => 'Ava', 'pages' => [123, 789]],
155-
['name' => 'Noah', 'pages' => [456, 789]],
154+
['name' => 'Ava', 'chat_assistant_pages' => [123, 789]],
155+
['name' => 'Noah', 'chat_assistant_pages' => [456, 789]],
156156
];
157157

158158
$config = new ChatConfig(

0 commit comments

Comments
 (0)