Skip to content

Conversation

@ictbeheer
Copy link
Contributor

@ictbeheer ictbeheer commented Aug 28, 2025

This pull request refactors how meta fields are loaded for data objects, introducing a reusable trait to centralize and simplify meta loading logic. It also adds flexibility to taxonomy attribute handling and improves type compatibility for meta operations. The main changes are grouped below:

Meta loading refactor and code reuse:

  • Introduced a new HasMeta trait (src/Traits/HasMeta.php) that encapsulates the logic for loading meta fields, determining meta prefixes, and resolving IDs for both posts and terms. This trait is now used by both PostData and TermData, removing duplicated code and improving maintainability.
  • Updated PostData and TermData to use the HasMeta trait, and refactored their constructors to call loadMeta() without requiring an explicit ID parameter. This centralizes meta-loading behavior and ensures consistency. [1] [2] [3]
  • Removed the now-redundant meta loading and prefix methods from PostData, as this logic is handled by the new trait.

Attribute and type improvements:

  • Enhanced the Terms attribute to accept a custom data class for term collection, increasing flexibility for term data mapping. The getValue method now uses the specified data class instead of being hardcoded to TermData. [1] [2]
  • Updated the Meta attribute's getValue method to accept both string and integer post IDs, improving compatibility with different ID formats.

Other minor cleanup:

  • Removed unused imports from PostData.php for clarity.

@github-actions
Copy link

github-actions bot commented Aug 28, 2025

Coverage report for commit: 762360f
File: coverage.xml

Cover ┌─────────────────────────┐ Freq.
   0% │ ███████████████████████ │ 73.3%
  10% │ ███░░░░░░░░░░░░░░░░░░░░ │  6.7%
  20% │ ░░░░░░░░░░░░░░░░░░░░░░░ │  0.0%
  30% │ ░░░░░░░░░░░░░░░░░░░░░░░ │  0.0%
  40% │ ███░░░░░░░░░░░░░░░░░░░░ │  6.7%
  50% │ ░░░░░░░░░░░░░░░░░░░░░░░ │  0.0%
  60% │ ░░░░░░░░░░░░░░░░░░░░░░░ │  0.0%
  70% │ ░░░░░░░░░░░░░░░░░░░░░░░ │  0.0%
  80% │ ░░░░░░░░░░░░░░░░░░░░░░░ │  0.0%
  90% │ ░░░░░░░░░░░░░░░░░░░░░░░ │  0.0%
 100% │ █████░░░░░░░░░░░░░░░░░░ │ 13.3%
      └─────────────────────────┘
 *Legend:* █ = Current Distribution 
Summary - Lines: 11.30% | Methods: 5.88%
FilesLinesMethodsBranches
src/Attributes
   Meta.php--100.00%
   MetaPrefix.php--100.00%
   TaxonomyPrefix.php--100.00%
   Terms.php--100.00%
src/Contracts
   PostDataInterface.php100.00%100.00%100.00%
src/Enums
   PostStatus.php--100.00%
src
   ImageData.php--100.00%
   PostData.php8.93%3.85%100.00%
   TermData.php--100.00%
   UserData.php--100.00%
src/Mappers
   PostPrefixMapper.php--100.00%
   UserPrefixMapper.php--100.00%
src/Normalizers
   WPPostNormalizer.php--100.00%
src/Providers
   DataServiceProvider.php100.00%100.00%100.00%
src/Traits
   HasMeta.php36.00%-100.00%

🤖 comment via lucassabreu/comment-coverage-clover

@ictbeheer ictbeheer marked this pull request as ready for review August 28, 2025 13:43
@ictbeheer ictbeheer linked an issue Aug 28, 2025 that may be closed by this pull request
}

public function getValue(int $postID, string $metaKey, string $prefix): mixed
public function getValue(string|int $postID, string $metaKey, string $prefix): mixed

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Goeie, dit om ACF niet te laten breken toch?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

@SimonvanWijhe SimonvanWijhe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nog wel even een voorbeeld aan de README.md toevoegen.

return $metaPrefixAttribute?->newInstance()->prefix ?? '';
}

private function postID(): string|int
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

postID() is hier niet helemaal juist. WordPress noemt dit $object_id https://developer.wordpress.org/reference/functions/get_metadata dus ik zou deze method objectID noemen.

Copy link
Contributor

@YvetteNikolov YvetteNikolov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jeej 🎉

Kun je de README updaten? En dan specifiek wat voorbeelden van hoe je het kan gebruiken in PHP / Blade templates voor de domme frontenders? https://github.com/yardinternet/yard-data#reading-terms-from-your-data-object

@ictbeheer ictbeheer merged commit 0a91272 into main Sep 1, 2025
6 checks passed
@ictbeheer ictbeheer deleted the feat/termdata branch September 1, 2025 09:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TermData voor taxonomy

5 participants