From 45f373d533604841618032c0f41e7680b4c57b52 Mon Sep 17 00:00:00 2001 From: Maarten Bruna <14947039+ictbeheer@users.noreply.github.com> Date: Thu, 2 Apr 2026 10:07:32 +0200 Subject: [PATCH] wip --- composer.json | 1 + composer.lock | 54 ++++++++++++++++++++++++++++++++++++++++- src/Attributes/Meta.php | 6 ++--- src/Traits/HasMeta.php | 44 +++++++++++++++++++++++++++++++++ 4 files changed, 101 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 6f6a2a5..6cb172c 100644 --- a/composer.json +++ b/composer.json @@ -26,6 +26,7 @@ "larastan/larastan": "^2.0", "orchestra/testbench": "^8.23", "pestphp/pest": "^2.34", + "php-stubs/acf-pro-stubs": "^6.5", "szepeviktor/phpstan-wordpress": "^1.0", "yard/php-cs-fixer-rules": "^1.0" }, diff --git a/composer.lock b/composer.lock index d6b134c..088628c 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "8a5b912b7ce150d2dcc5d665c3cb46e8", + "content-hash": "cbad373bf95e633f114dae65d13604e6", "packages": [ { "name": "bordoni/phpass", @@ -7896,6 +7896,58 @@ }, "time": "2022-02-21T01:04:05+00:00" }, + { + "name": "php-stubs/acf-pro-stubs", + "version": "v6.5.0", + "source": { + "type": "git", + "url": "https://github.com/php-stubs/acf-pro-stubs.git", + "reference": "e54ba80a945fd1f6c9ebe761e3f19992fbaedb39" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-stubs/acf-pro-stubs/zipball/e54ba80a945fd1f6c9ebe761e3f19992fbaedb39", + "reference": "e54ba80a945fd1f6c9ebe761e3f19992fbaedb39", + "shasum": "" + }, + "require": { + "php-stubs/wordpress-stubs": "^4.7 || ^5.0 || ^6.0" + }, + "require-dev": { + "php": "~7.1 || ^8.0", + "php-stubs/generator": "^0.8", + "phpdocumentor/reflection-docblock": "^5.3" + }, + "suggest": { + "symfony/polyfill-php73": "Symfony polyfill backporting some PHP 7.3+ features to lower PHP versions", + "szepeviktor/phpstan-wordpress": "WordPress extensions for PHPStan" + }, + "type": "library", + "extra": { + "installer-paths": { + "source/{$name}/": [ + "type:wordpress-plugin" + ] + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Advanced Custom Fields PRO stubs for static analysis.", + "homepage": "https://github.com/php-stubs/acf-pro-stubs", + "keywords": [ + "PHPStan", + "acf", + "static analysis", + "wordpress" + ], + "support": { + "issues": "https://github.com/php-stubs/acf-pro-stubs/issues", + "source": "https://github.com/php-stubs/acf-pro-stubs/tree/v6.5.0" + }, + "time": "2025-09-05T00:47:01+00:00" + }, { "name": "php-stubs/wordpress-stubs", "version": "v6.9.0", diff --git a/src/Attributes/Meta.php b/src/Attributes/Meta.php index 08d0026..4d9e5f4 100644 --- a/src/Attributes/Meta.php +++ b/src/Attributes/Meta.php @@ -13,10 +13,10 @@ public function __construct(private ?string $metaKey = null) { } - public function getValue(string|int $objectID, string $metaKey, string $prefix): mixed + public function getValue(string|int $objectID, string $metaKey, string $prefix, bool $formatted = true): mixed { if (isset($this->metaKey)) { - if ($value = \get_field($this->metaKey, $objectID)) { + if ($value = \get_field($this->metaKey, $objectID, $formatted)) { return $value; } else { return null; @@ -31,7 +31,7 @@ public function getValue(string|int $objectID, string $metaKey, string $prefix): ]; foreach ($possibleKeys as $key) { - if ($value = \get_field($key, $objectID)) { + if ($value = \get_field($key, $objectID, $formatted)) { return $value; } } diff --git a/src/Traits/HasMeta.php b/src/Traits/HasMeta.php index 15e161f..faba43a 100644 --- a/src/Traits/HasMeta.php +++ b/src/Traits/HasMeta.php @@ -26,7 +26,13 @@ private function loadMeta(): void $metaAttributes = $property->getAttributes(Meta::class); foreach ($metaAttributes as $metaAttribute) { $meta = $metaAttribute->newInstance(); + + $rawMetaValue = $meta->getValue($this->objectID(), $property->name, $this->metaPrefix(), false); + $metaValue = $this->castRawMetaValue($rawMetaValue, $propertyTypeName ?? ''); + $metaValue = $meta->getValue($this->objectID(), $property->name, $this->metaPrefix()); + + $rawMetaValue = $meta->getValue($this->objectID(), $property->name, $this->metaPrefix(), false); if (null === $metaValue || null === $propertyTypeName) { continue; } @@ -36,8 +42,46 @@ private function loadMeta(): void } } + private function castRawMetaValue(mixed $rawMetaValue, string $type): mixed + { + if (is_a($type, PostData::class, true) && is_int($rawMetaValue)) { + $post = get_post($rawMetaValue); + if (! $post) { + return null; + } + + return PostData::fromPost($post); + } + + if (is_a($type, CarbonImmutable::class, true) && is_string($rawMetaValue)) { + if (CarbonImmutable::canBeCreatedFromFormat($rawMetaValue, 'Ymd')) { + return CarbonImmutable::createFromFormat('Ymd', $rawMetaValue); + } + if (CarbonImmutable::canBeCreatedFromFormat($rawMetaValue, 'Y-m-d H:i:s')) { + return CarbonImmutable::createFromFormat('Y-m-d H:i:s', $rawMetaValue); + } else { + return CarbonImmutable::parse($rawMetaValue); + } + } + + if (is_a($type, \BackedEnum::class, true) && (is_int($rawMetaValue) || is_string($rawMetaValue))) { + return $type::from($rawMetaValue); + } + + return $rawMetaValue; + } + private function castValue(mixed $value, string $type): mixed { + if (is_a($type, PostData::class, true) && is_int($value)) { + $post = get_post($value); + if (! $post) { + return null; + } + + return PostData::fromPost($post); + } + if (is_a($type, Data::class, true)) { return $type::from($value); }