Skip to content

Commit cf21177

Browse files
committed
feat: support multiple connections of the same relation
1 parent 291fbe0 commit cf21177

7 files changed

Lines changed: 29 additions & 27 deletions

File tree

‎assets/components/filters-controls/connection-control.js‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,12 @@ const ConnectionControl = ( props ) => {
9898
const newConnectionPosts = {};
9999

100100
connections.forEach( ( connection ) => {
101-
newConnectionPosts[ connection.value ] = {
102-
value: currentPostId,
103-
label: currentPostTitle,
104-
};
101+
newConnectionPosts[ connection.value ] = [
102+
{
103+
value: currentPostId,
104+
label: currentPostTitle,
105+
},
106+
];
105107
} );
106108

107109
setAttributes( {

‎assets/components/filters-controls/connection-select-control.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const ConnectionSelectControl = ( props ) => {
3232
enable={ true }
3333
handleChange={ onChange }
3434
isClearable={ true }
35-
isMulti={ false }
35+
isMulti={ true }
3636
label={ connection.label }
3737
value={ connectionPosts[ connection.value ] }
3838
/>

‎public/index.asset.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?php return array('dependencies' => array('react', 'react-dom', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-data', 'wp-element', 'wp-hooks', 'wp-i18n', 'wp-server-side-render'), 'version' => '88fe6e6ce5e533574a56');
1+
<?php return array('dependencies' => array('react', 'react-dom', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-data', 'wp-element', 'wp-hooks', 'wp-i18n', 'wp-server-side-render'), 'version' => '26d0e0eb85cb3ef18e38');

‎public/index.js‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/Block/BlockAttributes.php‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ class BlockAttributes extends Data
2525
* @param LabelValueArray|array{} $stickyPost
2626
* @param LabelValueArray|array{} $excludePosts
2727
* @param LabelValueArray|array{} $postParent
28-
* @param array{string: list<LabelValueArray>}|array{} $taxonomyTerms
29-
* @param array<string, LabelValueArray>|array{} $connectionPosts
28+
* @param array<string, list<LabelValueArray>>|array{} $taxonomyTerms
29+
* @param array<string, list<array{ label: string, value: int }>>|array{} $connectionPosts
3030
*/
3131
public function __construct(
3232
public array $postTypes = [],
@@ -86,7 +86,7 @@ public function enableConnection(): bool
8686
* @return list<array{
8787
* post_type: string,
8888
* meta_key: string,
89-
* meta_value: int,
89+
* meta_value: list<int>,
9090
* }>
9191
*/
9292
public function postConnections(): array
@@ -95,7 +95,7 @@ public function postConnections(): array
9595

9696
$connectionsConfig = $this->connectionsConfig();
9797

98-
foreach ($this->connectionPosts as $targetPostType => $connection) {
98+
foreach ($this->connectionPosts as $targetPostType => $connectionArray) {
9999
$config = $connectionsConfig->where('to', $targetPostType);
100100

101101
if ($config->count() === 0) {
@@ -106,7 +106,7 @@ public function postConnections(): array
106106
$result = [];
107107
$result['post_type'] = $configItem['from'];
108108
$result['meta_key'] = $configItem['meta_key'];
109-
$result['meta_value'] = (int)$connection['value'];
109+
$result['meta_value'] = array_map('intval', array_column($connectionArray, 'value'));
110110
$connections[] = $result;
111111
}
112112
}

‎src/Query/PostQuery.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function get(): Collection
7676
->whereHas('meta', function ($metaQuery) use ($connection) {
7777
$metaQuery
7878
->where('meta_key', $connection['meta_key'])
79-
->where('meta_value', $connection['meta_value']);
79+
->whereIn('meta_value', $connection['meta_value']);
8080
});
8181
}
8282
});

‎tests/Block/BlockAttributesTest.php‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@
272272
it('returns true for enableConnection when enabled and connectionPosts is not empty', function () {
273273
$attributes = BlockAttributes::from([
274274
'enableConnection' => true,
275-
'connectionPosts' => ['page' => ['label' => 'My Page', 'value' => '42']],
275+
'connectionPosts' => ['page' => [['label' => 'My Page', 'value' => '42']]],
276276
]);
277277
expect($attributes->enableConnection())->toBeTrue();
278278
});
@@ -288,7 +288,7 @@
288288
it('returns false for enableConnection when disabled even if connectionPosts is not empty', function () {
289289
$attributes = BlockAttributes::from([
290290
'enableConnection' => false,
291-
'connectionPosts' => ['page' => ['label' => 'My Page', 'value' => '42']],
291+
'connectionPosts' => ['page' => [['label' => 'My Page', 'value' => '42']]],
292292
]);
293293
expect($attributes->enableConnection())->toBeFalse();
294294
});
@@ -297,7 +297,7 @@
297297
config(['yard-query-block.connections' => []]);
298298
$attributes = BlockAttributes::from([
299299
'enableConnection' => true,
300-
'connectionPosts' => ['page' => ['label' => 'My Page', 'value' => '42']],
300+
'connectionPosts' => ['page' => [['label' => 'My Page', 'value' => '42']]],
301301
]);
302302
expect($attributes->postConnections())->toBe([]);
303303
});
@@ -308,7 +308,7 @@
308308
]]);
309309
$attributes = BlockAttributes::from([
310310
'enableConnection' => true,
311-
'connectionPosts' => ['page' => ['label' => 'My Page', 'value' => '42']],
311+
'connectionPosts' => ['page' => [['label' => 'My Page', 'value' => '42']]],
312312
]);
313313
expect($attributes->postConnections())->toBe([]);
314314
});
@@ -320,11 +320,11 @@
320320
]]);
321321
$attributes = BlockAttributes::from([
322322
'enableConnection' => true,
323-
'connectionPosts' => ['page' => ['label' => 'My Page', 'value' => '42']],
323+
'connectionPosts' => ['page' => [['label' => 'My Page', 'value' => '42']]],
324324
]);
325325
expect($attributes->postConnections())->toBe([
326-
['post_type' => 'post', 'meta_key' => 'related_page', 'meta_value' => 42],
327-
['post_type' => 'product', 'meta_key' => 'featured_page', 'meta_value' => 42],
326+
['post_type' => 'post', 'meta_key' => 'related_page', 'meta_value' => [42]],
327+
['post_type' => 'product', 'meta_key' => 'featured_page', 'meta_value' => [42]],
328328
]);
329329
});
330330

@@ -336,13 +336,13 @@
336336
$attributes = BlockAttributes::from([
337337
'enableConnection' => true,
338338
'connectionPosts' => [
339-
'page' => ['label' => 'My Page', 'value' => '42'],
340-
'category' => ['label' => 'My Category', 'value' => '10'],
339+
'page' => [['label' => 'My Page', 'value' => '42']],
340+
'category' => [['label' => 'My Category', 'value' => '10']],
341341
],
342342
]);
343343
expect($attributes->postConnections())->toBe([
344-
['post_type' => 'post', 'meta_key' => 'related_page', 'meta_value' => 42],
345-
['post_type' => 'post', 'meta_key' => 'related_category', 'meta_value' => 10],
344+
['post_type' => 'post', 'meta_key' => 'related_page', 'meta_value' => [42]],
345+
['post_type' => 'post', 'meta_key' => 'related_category', 'meta_value' => [10]],
346346
]);
347347
});
348348

@@ -353,11 +353,11 @@
353353
$attributes = BlockAttributes::from([
354354
'enableConnection' => true,
355355
'connectionPosts' => [
356-
'page' => ['label' => 'My Page', 'value' => '42'],
357-
'event' => ['label' => 'My Event', 'value' => '99'],
356+
'page' => [['label' => 'My Page', 'value' => '42']],
357+
'event' => [['label' => 'My Event', 'value' => '99']],
358358
],
359359
]);
360360
expect($attributes->postConnections())->toBe([
361-
['post_type' => 'post', 'meta_key' => 'related_page', 'meta_value' => 42],
361+
['post_type' => 'post', 'meta_key' => 'related_page', 'meta_value' => [42]],
362362
]);
363363
});

0 commit comments

Comments
 (0)