Skip to content
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 34 additions & 1 deletion src/Infrastructure/Adapters/WordPressMetaRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<string, mixed>
*/
Expand Down
21 changes: 21 additions & 0 deletions tests/Fixtures/RevisionsOnPlainType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Pollora\Meta\Tests\Fixtures;

use Pollora\Attributes\Meta;
use Pollora\Attributes\PostMeta;

/**
* Asks for revisions on a post type that does not support them (registered by the test that uses it).
*/
#[PostMeta('plain_type')]
class RevisionsOnPlainType
{
#[Meta]
public int $pages = 0;

#[Meta(revisions: true)]
public ?string $summary = null;
}
18 changes: 18 additions & 0 deletions tests/Integration/TypedMetaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Pollora\Meta\Application\Services\MetaSchemaBuilder;
use Pollora\Meta\Application\Services\MetaSchemaRepository;
use Pollora\Meta\Domain\Enums\MetaObjectType;
use Pollora\Meta\Domain\Exceptions\InvalidMetaDefinitionException;
use Pollora\Meta\Domain\Services\MetaValueCaster;
use Pollora\Meta\Infrastructure\Adapters\WordPressMetaInventory;
use Pollora\Meta\Infrastructure\Adapters\WordPressMetaRegistry;
Expand All @@ -20,6 +21,7 @@
use Pollora\Meta\Tests\Fixtures\EventStatus;
use Pollora\Meta\Tests\Fixtures\MemberProfile;
use Pollora\Meta\Tests\Fixtures\RatedEvent;
use Pollora\Meta\Tests\Fixtures\RevisionsOnPlainType;
use Spatie\StructureDiscoverer\Data\DiscoveredClass;

require_once ABSPATH.'wp-admin/includes/user.php';
Expand Down Expand Up @@ -296,3 +298,19 @@ function typedMetaRest(string $method, string $route, array $params = []): WP_RE
->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 = [];
}
});
1 change: 1 addition & 0 deletions tests/Unit/MetaArraysAndObjectsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/StandaloneWiringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
17 changes: 17 additions & 0 deletions tests/Unit/WordPressMetaRegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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];

Expand All @@ -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);
Expand Down Expand Up @@ -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']];

Expand All @@ -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));
Expand Down
Loading