Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions config/yard-query-block.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

return [
'connections' => [
// [
// 'label' => 'Project',
// 'from' => 'news',
// 'meta_key' => 'news_connected_project',
// 'to' => 'project',
// ],
],
];
2 changes: 2 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ parameters:
paths:
- src
tmpDir: build/phpstan
ignoreErrors:
- message: '#Call to an undefined method Corcel\\Model\\Builder\\PostBuilder::hasMeta\(\)#'
34 changes: 28 additions & 6 deletions src/Block/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,25 @@ public function registerBlock(): void
}

public function blockSettings(): WP_REST_Response
{
return new WP_REST_Response([
'version' => self::VERSION,
'templates' => $this->templates(),
'connections' => $this->connections(),
]);
}

/**
* @return array<array{label: string, value: string}>
*/
private function templates(): array
{
$path = resource_path('views/vendor/yard-query-block/templates');
$files = scandir($path);
$templates = [];

if (false === $files) {
return new WP_REST_Response([
'templates' => [],
]);
return [];
}

foreach ($files as $file) {
Expand All @@ -122,9 +132,21 @@ public function blockSettings(): WP_REST_Response
];
}

return new WP_REST_Response([
'templates' => $templates,
]);
return $templates;
}

/**
* @return array<string, string>
*/
public function connections(): array
{
$config = config('yard-query-block.connections', []);

if (! is_array($config)) {
return [];
}

return $config;
}

/**
Expand Down
36 changes: 36 additions & 0 deletions src/Block/BlockAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ class BlockAttributes extends Data
* @param LabelValueArray|array{} $excludePosts
* @param LabelValueArray|array{} $postParent
* @param array{string: list<LabelValueArray>}|array{} $taxonomyTerms
* @param array{string: list<LabelValueArray>}|array{} $connectionPosts
*/
public function __construct(
public array $postTypes = [],
public bool $enableConnection = false,
public array $connectionPosts = [],
public array $postStatus = [['label' => 'Gepubliceerd', 'value' => 'publish']],
#[WithCast(IntCast::class)]
public int $postsPerPage = 3,
Expand Down Expand Up @@ -73,6 +76,39 @@ public function postTypes(): array
->all();
}

public function enableConnection(): bool
{
return $this->enableConnection;
}

/**
* @return array<string, int>}
*/
public function connectedPost(): array
{
$connections = [];

foreach ($this->connectionPosts as $postType => $connection) {
$metaKey = $this->connectionMetaKey($postType);
$connections[$metaKey] = (int)$connection[0]['value'];
}

return $connections;
}

private function connectionMetaKey(string $postType): string
{
$config = config('yard-query-block.connections', []);

if (! is_array($config) || empty($config)) {
return '';
}

$metaKey = collect($config)->firstWhere('to', $postType);

return $metaKey['meta_key'];
}

/**
* @return list<string>
*/
Expand Down
4 changes: 4 additions & 0 deletions src/Query/PostQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ public function get(): Collection
}
}

if ($this->attributes->enableConnection()) {
$query->hasMeta($this->attributes->connectedPost());
}

$query = $this->order($query);

/**
Expand Down
1 change: 1 addition & 0 deletions src/QueryBlockServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public function configurePackage(Package $package): void
$package
->name('yard-query-block')
->hasViews()
->hasConfigFile()
->hasRoute('web');
}

Expand Down