From 28c2364fabc6536b5219fe26d77ae70c8c54b314 Mon Sep 17 00:00:00 2001 From: Simon Date: Mon, 19 Jan 2026 12:12:11 +0100 Subject: [PATCH] feat: implement connection handling in block attributes and queries --- config/yard-query-block.php | 14 ++++++++++++ phpstan.neon.dist | 2 ++ src/Block/Block.php | 34 +++++++++++++++++++++++------ src/Block/BlockAttributes.php | 36 +++++++++++++++++++++++++++++++ src/Query/PostQuery.php | 4 ++++ src/QueryBlockServiceProvider.php | 1 + 6 files changed, 85 insertions(+), 6 deletions(-) create mode 100644 config/yard-query-block.php diff --git a/config/yard-query-block.php b/config/yard-query-block.php new file mode 100644 index 0000000..2f4b790 --- /dev/null +++ b/config/yard-query-block.php @@ -0,0 +1,14 @@ + [ + // [ + // 'label' => 'Project', + // 'from' => 'news', + // 'meta_key' => 'news_connected_project', + // 'to' => 'project', + // ], + ], +]; diff --git a/phpstan.neon.dist b/phpstan.neon.dist index d0db2a8..2cb0f08 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -7,3 +7,5 @@ parameters: paths: - src tmpDir: build/phpstan + ignoreErrors: + - message: '#Call to an undefined method Corcel\\Model\\Builder\\PostBuilder::hasMeta\(\)#' diff --git a/src/Block/Block.php b/src/Block/Block.php index 2f9b92a..73747ac 100644 --- a/src/Block/Block.php +++ b/src/Block/Block.php @@ -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 + */ + 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) { @@ -122,9 +132,21 @@ public function blockSettings(): WP_REST_Response ]; } - return new WP_REST_Response([ - 'templates' => $templates, - ]); + return $templates; + } + + /** + * @return array + */ + public function connections(): array + { + $config = config('yard-query-block.connections', []); + + if (! is_array($config)) { + return []; + } + + return $config; } /** diff --git a/src/Block/BlockAttributes.php b/src/Block/BlockAttributes.php index c7c7020..bdcfa82 100644 --- a/src/Block/BlockAttributes.php +++ b/src/Block/BlockAttributes.php @@ -25,9 +25,12 @@ class BlockAttributes extends Data * @param LabelValueArray|array{} $excludePosts * @param LabelValueArray|array{} $postParent * @param array{string: list}|array{} $taxonomyTerms + * @param array{string: list}|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, @@ -73,6 +76,39 @@ public function postTypes(): array ->all(); } + public function enableConnection(): bool + { + return $this->enableConnection; + } + + /** + * @return array} + */ + 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 */ diff --git a/src/Query/PostQuery.php b/src/Query/PostQuery.php index dfe3d94..749ff7d 100644 --- a/src/Query/PostQuery.php +++ b/src/Query/PostQuery.php @@ -68,6 +68,10 @@ public function get(): Collection } } + if ($this->attributes->enableConnection()) { + $query->hasMeta($this->attributes->connectedPost()); + } + $query = $this->order($query); /** diff --git a/src/QueryBlockServiceProvider.php b/src/QueryBlockServiceProvider.php index a0a7d99..47dd8b8 100644 --- a/src/QueryBlockServiceProvider.php +++ b/src/QueryBlockServiceProvider.php @@ -15,6 +15,7 @@ public function configurePackage(Package $package): void $package ->name('yard-query-block') ->hasViews() + ->hasConfigFile() ->hasRoute('web'); }