From b2d664c7d1032779907ab620629f2c7b998ad066 Mon Sep 17 00:00:00 2001 From: Pollora maintainer Date: Mon, 23 Nov 2026 17:17:57 +0100 Subject: [PATCH] fix(registry): refuse revisions: true on a post type without revisions support (#32) --- CHANGELOG.md | 4 +++ .../Adapters/WordPressMetaRegistry.php | 35 ++++++++++++++++++- tests/Fixtures/RevisionsOnPlainType.php | 21 +++++++++++ tests/Integration/TypedMetaTest.php | 18 ++++++++++ tests/Unit/MetaArraysAndObjectsTest.php | 1 + tests/Unit/StandaloneWiringTest.php | 1 + tests/Unit/WordPressMetaRegistryTest.php | 17 +++++++++ 7 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 tests/Fixtures/RevisionsOnPlainType.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 1fd8f7d..5d359b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- A meta declared with `#[Meta(revisions: true)]` on a post type that does not support `revisions` was silently not registered (`register_meta()` returns `false`, and its `_doing_it_wrong()` notice is hidden without `WP_DEBUG`). Registration now throws an `InvalidMetaDefinitionException` naming the class, the property and the post type, before any meta of the declaration is registered (#32) + ## [0.1.2] - 2026-11-23 ### Changed diff --git a/src/Infrastructure/Adapters/WordPressMetaRegistry.php b/src/Infrastructure/Adapters/WordPressMetaRegistry.php index 253b199..0c34e3c 100644 --- a/src/Infrastructure/Adapters/WordPressMetaRegistry.php +++ b/src/Infrastructure/Adapters/WordPressMetaRegistry.php @@ -8,6 +8,7 @@ use Pollora\Meta\Domain\Contracts\MetaRegistryInterface; use Pollora\Meta\Domain\Enums\MetaObjectType; use Pollora\Meta\Domain\Enums\MetaValueType; +use Pollora\Meta\Domain\Exceptions\InvalidMetaDefinitionException; use Pollora\Meta\Domain\Models\MetaDefinition; use Pollora\Meta\Domain\Models\MetaSchema; use Pollora\Meta\Domain\Services\MetaValueCaster; @@ -49,13 +50,45 @@ private function registerNow(MetaSchema $schema): void } // An empty subtype registers the meta for every object of the type. - foreach ($schema->subtypes === [] ? [''] : $schema->subtypes as $subtype) { + $subtypes = $schema->subtypes === [] ? [''] : $schema->subtypes; + + // Checked first: a refused declaration registers none of its meta. + foreach ($subtypes as $subtype) { + foreach ($schema->definitions as $definition) { + $this->assertRevisionsSupported($schema, $definition, $subtype); + } + } + + foreach ($subtypes as $subtype) { foreach ($schema->definitions as $definition) { \register_meta($schema->objectType->value, $definition->key, $this->argumentsFor($schema, $definition, $subtype)); } } } + /** + * Refuses a meta that asks for revisions on a post type that does not support them. + * + * `register_meta()` returns false for it (with a `_doing_it_wrong()` notice that is silent without WP_DEBUG) and the + * meta is never registered. Checked here, when the post types are registered, as WordPress checks it. + * + * @throws InvalidMetaDefinitionException + */ + private function assertRevisionsSupported(MetaSchema $schema, MetaDefinition $definition, string $subtype): void + { + if (! $definition->revisions || $schema->objectType !== MetaObjectType::Post || $subtype === '') { + return; + } + + if (! \post_type_supports($subtype, 'revisions')) { + throw InvalidMetaDefinitionException::forProperty($schema->declaringClass, $definition->property, sprintf( + 'revisions: true needs the post type "%s" to support "revisions", and it does not: WordPress would not register the meta "%s". Add "revisions" to the supports of the post type, or drop revisions: true.', + $subtype, + $definition->key + )); + } + } + /** * @return array */ diff --git a/tests/Fixtures/RevisionsOnPlainType.php b/tests/Fixtures/RevisionsOnPlainType.php new file mode 100644 index 0000000..678ca6e --- /dev/null +++ b/tests/Fixtures/RevisionsOnPlainType.php @@ -0,0 +1,21 @@ +and($unreadable[array_search('capacity', array_column($unreadable, 'key'), true)]['objects'])->toBe([$second]) ->and(array_column($undeclared, 'key'))->toContain('legacy_field'); }); + +it('refuses revisions: true on a post type without revisions support instead of dropping the meta silently', function (): void { + register_post_type('plain_type', ['public' => true, 'supports' => ['title']]); + + try { + // WordPress itself returns false and registers nothing: the failure used to be a notice that WP_DEBUG hides. + expect(register_meta('post', 'summary', ['object_subtype' => 'plain_type', 'revisions_enabled' => true]))->toBeFalse(); + + expect(fn () => registerTypedMeta($this, RevisionsOnPlainType::class)) + ->toThrow(InvalidMetaDefinitionException::class, 'RevisionsOnPlainType::$summary: revisions: true needs the post type "plain_type" to support "revisions"') + ->and(get_registered_meta_keys('post', 'plain_type'))->toBe([]); + } finally { + unregister_post_type('plain_type'); + $this->notices = []; + } +}); diff --git a/tests/Unit/MetaArraysAndObjectsTest.php b/tests/Unit/MetaArraysAndObjectsTest.php index d2258db..3b9ea5f 100644 --- a/tests/Unit/MetaArraysAndObjectsTest.php +++ b/tests/Unit/MetaArraysAndObjectsTest.php @@ -171,6 +171,7 @@ $calls = []; Functions\when('did_action')->justReturn(1); Functions\when('add_post_type_support')->justReturn(); + Functions\when('post_type_supports')->justReturn(true); Functions\when('sanitize_text_field')->alias(fn (string $value): string => strip_tags($value)); Functions\when('register_meta')->alias(function (string $objectType, string $key, array $args) use (&$calls): bool { $calls[$key] = $args; diff --git a/tests/Unit/StandaloneWiringTest.php b/tests/Unit/StandaloneWiringTest.php index 2bc3a08..8ca5022 100644 --- a/tests/Unit/StandaloneWiringTest.php +++ b/tests/Unit/StandaloneWiringTest.php @@ -24,6 +24,7 @@ $stored = []; Functions\when('did_action')->justReturn(1); Functions\when('add_post_type_support')->justReturn(); + Functions\when('post_type_supports')->justReturn(true); Functions\when('register_meta')->justReturn(true); Functions\when('wp_slash')->alias(fn (mixed $value): mixed => $value); Functions\when('get_metadata_raw')->alias(function (string $type, int $id, string $key) use (&$stored): mixed { diff --git a/tests/Unit/WordPressMetaRegistryTest.php b/tests/Unit/WordPressMetaRegistryTest.php index e8c02fe..10508b9 100644 --- a/tests/Unit/WordPressMetaRegistryTest.php +++ b/tests/Unit/WordPressMetaRegistryTest.php @@ -9,6 +9,7 @@ use Pollora\Attributes\Taxonomy; use Pollora\Hook\Domain\Contract\Action; use Pollora\Meta\Application\Services\MetaSchemaBuilder; +use Pollora\Meta\Domain\Exceptions\InvalidMetaDefinitionException; use Pollora\Meta\Domain\Models\MetaSchema; use Pollora\Meta\Domain\Services\MetaValueCaster; use Pollora\Meta\Infrastructure\Adapters\WordPressMetaRegistry; @@ -34,6 +35,7 @@ function registeredEventMeta(string $class = Event::class): array $calls = []; Functions\when('did_action')->justReturn(1); Functions\when('add_post_type_support')->justReturn(); + Functions\when('post_type_supports')->justReturn(true); Functions\when('register_meta')->alias(function (string $objectType, string $key, array $args) use (&$calls): bool { $calls[$key] = [$objectType, $args]; @@ -49,6 +51,7 @@ function registeredEventMeta(string $class = Event::class): array it('waits for init, after post types and taxonomies', function (): void { Functions\when('did_action')->justReturn(0); Functions\when('add_post_type_support')->justReturn(); + Functions\when('post_type_supports')->justReturn(true); Functions\expect('register_meta')->never(); $action = Mockery::mock(Action::class); @@ -125,10 +128,23 @@ function registeredEventMeta(string $class = Event::class): array ->and($meta['capacity'][1])->not->toHaveKey('revisions_enabled'); }); +it('refuses revisions: true on a post type that does not support revisions, naming the type and the meta', function (): void { + Functions\when('did_action')->justReturn(1); + Functions\when('add_post_type_support')->justReturn(); + Functions\when('post_type_supports')->alias(fn (string $postType, string $feature): bool => $feature !== 'revisions'); + Functions\expect('register_meta')->never(); + + $register = fn () => (new WordPressMetaRegistry(Mockery::mock(Action::class), new MetaValueCaster)) + ->register((new MetaSchemaBuilder)->build(Event::class)); + + expect($register)->toThrow(InvalidMetaDefinitionException::class, '#[Meta] on '.Event::class.'::$subtitle: revisions: true needs the post type "event" to support "revisions"'); +}); + it('registers the meta on each post type of the list, and for every user without a subtype', function (): void { $calls = []; Functions\when('did_action')->justReturn(1); Functions\when('add_post_type_support')->justReturn(); + Functions\when('post_type_supports')->justReturn(true); Functions\when('register_meta')->alias(function (string $objectType, string $key, array $args) use (&$calls): bool { $calls[] = [$objectType, $key, $args['object_subtype']]; @@ -153,6 +169,7 @@ function registeredEventMeta(string $class = Event::class): array Functions\when('did_action')->justReturn(1); Functions\when('register_meta')->justReturn(true); Functions\expect('add_post_type_support')->once()->with('event', 'custom-fields'); + Functions\when('post_type_supports')->justReturn(true); $registry = new WordPressMetaRegistry(Mockery::mock(Action::class), new MetaValueCaster); $registry->register((new MetaSchemaBuilder)->build(Event::class));