From 7dbe81bbb0463f4d6da53d4fd2ad0a808ef2da2e Mon Sep 17 00:00:00 2001 From: Maarten Bruna <14947039+ictbeheer@users.noreply.github.com> Date: Tue, 2 Jun 2026 08:29:28 +0200 Subject: [PATCH 1/9] feat: caching --- src/CommentData.php | 9 ++++++++- src/PostData.php | 20 ++++++++++++++++++-- src/TermData.php | 10 +++++++++- src/UserData.php | 10 +++++++++- 4 files changed, 44 insertions(+), 5 deletions(-) diff --git a/src/CommentData.php b/src/CommentData.php index e3b0352..555367d 100644 --- a/src/CommentData.php +++ b/src/CommentData.php @@ -34,7 +34,12 @@ public function __construct( public static function fromComment(\WP_Comment $comment): static { - return new static( + $cachedCommentData = wp_cache_get($comment->comment_ID, 'yard_comment_data', false, $found); + if ($found && $cachedCommentData instanceof static) { + return $cachedCommentData; + } + + $commentData = new static( id: (int) $comment->comment_ID, post: 0 !== (int) $comment->comment_post_ID && null !== get_post((int)$comment->comment_post_ID) ? PostData::fromPost(get_post((int)$comment->comment_post_ID)) : null, author: $comment->comment_author, @@ -50,5 +55,7 @@ public static function fromComment(\WP_Comment $comment): static parent: 0 !== (int) $comment->comment_parent && null !== get_comment((int) $comment->comment_parent) ? CommentData::fromComment(get_comment((int) $comment->comment_parent)) : null, user: 0 !== (int) $comment->user_id && false !== get_userdata((int) $comment->user_id) ? UserData::fromUser(get_userdata((int) $comment->user_id)) : null, ); + wp_cache_set($comment->comment_ID, $commentData, 'yard_comment_data'); + return $commentData; } } diff --git a/src/PostData.php b/src/PostData.php index 02239d6..6d6f9d9 100644 --- a/src/PostData.php +++ b/src/PostData.php @@ -56,7 +56,12 @@ public function __construct( public static function fromPost(\WP_Post $post): static { - return new (self::dataClass($post->post_type))( + $cachedPostData = wp_cache_get($post->ID, 'yard_post_data', false, $found); + if ($found && $cachedPostData instanceof static) { + return $cachedPostData; + } + + $postData = new (self::dataClass($post->post_type))( id: $post->ID, author: false !== get_userdata((int) $post->post_author) ? UserData::fromUser(get_userdata((int) $post->post_author)) : null, title: $post->post_title, @@ -70,11 +75,19 @@ public static function fromPost(\WP_Post $post): static thumbnail: get_post_thumbnail_id($post->ID) ? new ImageData(get_post_thumbnail_id($post->ID)) : null, commentCount: post_type_supports($post->post_type, 'comments') ? (int) $post->comment_count : null, ); + wp_cache_set($post->ID, $postData, 'yard_post_data'); + + return $postData; } public static function fromCorcel(Post $post): static { - return new (self::dataClass($post->post_type))( + $cachedPostData = wp_cache_get($post->ID, 'yard_post_data', false, $found); + if ($found && $cachedPostData instanceof static) { + return $cachedPostData; + } + + $postData = new (self::dataClass($post->post_type))( id: $post->ID, author: false !== get_userdata($post->post_author) ? UserData::fromUser(get_userdata($post->post_author)) : null, title: $post->post_title, @@ -88,6 +101,9 @@ public static function fromCorcel(Post $post): static thumbnail: get_post_thumbnail_id($post->ID) ? new ImageData(get_post_thumbnail_id($post->ID)) : null, commentCount: post_type_supports($post->post_type, 'comments') ? (int) $post->comment_count : null, ); + wp_cache_set($post->ID, $postData, 'yard_post_data'); + + return $postData; } /** diff --git a/src/TermData.php b/src/TermData.php index 7dd64a6..190b07e 100644 --- a/src/TermData.php +++ b/src/TermData.php @@ -26,12 +26,20 @@ public function __construct( public static function fromTerm(\WP_Term $term): TermData { - return new static( + $cachedTermData = wp_cache_get($term->term_id, 'yard_term_data', false, $found); + if ($found && $cachedTermData instanceof TermData) { + return $cachedTermData; + } + + $termData = new static( id: $term->term_id, name: $term->name, slug: $term->slug, taxonomy: $term->taxonomy, description: $term->description, ); + wp_cache_set($term->term_id, $termData, 'yard_term_data'); + + return $termData; } } diff --git a/src/UserData.php b/src/UserData.php index 7999b0a..540e8d2 100644 --- a/src/UserData.php +++ b/src/UserData.php @@ -30,7 +30,12 @@ public function __construct( public static function fromUser(\WP_User $user): self { - return new self( + $cachedUserData = wp_cache_get($user->ID, 'yard_user_data', false, $found); + if ($found && $cachedUserData instanceof self) { + return $cachedUserData; + } + + $userData = new self( id: $user->ID, login: $user->user_login, password: $user->user_pass, @@ -38,6 +43,9 @@ public static function fromUser(\WP_User $user): self email: $user->user_email, displayName: $user->display_name, ); + wp_cache_set($user->ID, $userData, 'yard_user_data'); + + return $userData; } /** From b03b0f25f198ea57ab475aead1aa63703f2df808 Mon Sep 17 00:00:00 2001 From: Yard workflows Date: Tue, 2 Jun 2026 06:29:55 +0000 Subject: [PATCH 2/9] style: apply php-cs-fixer changes --- src/CommentData.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/CommentData.php b/src/CommentData.php index 555367d..35eaf69 100644 --- a/src/CommentData.php +++ b/src/CommentData.php @@ -56,6 +56,7 @@ public static function fromComment(\WP_Comment $comment): static user: 0 !== (int) $comment->user_id && false !== get_userdata((int) $comment->user_id) ? UserData::fromUser(get_userdata((int) $comment->user_id)) : null, ); wp_cache_set($comment->comment_ID, $commentData, 'yard_comment_data'); + return $commentData; } } From 8a30e171c522a339e71337ad37f7b287d95375b5 Mon Sep 17 00:00:00 2001 From: Maarten Bruna <14947039+ictbeheer@users.noreply.github.com> Date: Tue, 2 Jun 2026 10:30:12 +0200 Subject: [PATCH 3/9] chore: copilot feedback --- src/CommentData.php | 10 +++++++--- src/PostData.php | 12 +++++++----- src/TermData.php | 8 ++++++-- src/UserData.php | 2 +- 4 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/CommentData.php b/src/CommentData.php index 35eaf69..57c2c66 100644 --- a/src/CommentData.php +++ b/src/CommentData.php @@ -11,6 +11,8 @@ /** @phpstan-consistent-constructor */ class CommentData extends Data { + public const CACHE_GROUP = 'yard_comment_data'; + use HasMeta; public function __construct( @@ -34,8 +36,10 @@ public function __construct( public static function fromComment(\WP_Comment $comment): static { - $cachedCommentData = wp_cache_get($comment->comment_ID, 'yard_comment_data', false, $found); - if ($found && $cachedCommentData instanceof static) { + wp_cache_add_non_persistent_groups([self::CACHE_GROUP]); + + $cachedCommentData = wp_cache_get($comment->comment_ID, self::CACHE_GROUP, false, $found); + if ($found && $cachedCommentData instanceof CommentData) { return $cachedCommentData; } @@ -55,7 +59,7 @@ public static function fromComment(\WP_Comment $comment): static parent: 0 !== (int) $comment->comment_parent && null !== get_comment((int) $comment->comment_parent) ? CommentData::fromComment(get_comment((int) $comment->comment_parent)) : null, user: 0 !== (int) $comment->user_id && false !== get_userdata((int) $comment->user_id) ? UserData::fromUser(get_userdata((int) $comment->user_id)) : null, ); - wp_cache_set($comment->comment_ID, $commentData, 'yard_comment_data'); + wp_cache_set($comment->comment_ID, $commentData, self::CACHE_GROUP); return $commentData; } diff --git a/src/PostData.php b/src/PostData.php index 6d6f9d9..7e54bfb 100644 --- a/src/PostData.php +++ b/src/PostData.php @@ -31,6 +31,8 @@ class PostData extends Data implements PostDataInterface { use HasMeta; + public const CACHE_GROUP = 'yard_post_data'; + public function __construct( #[MapInputName('ID')] public ?int $id, @@ -56,8 +58,8 @@ public function __construct( public static function fromPost(\WP_Post $post): static { - $cachedPostData = wp_cache_get($post->ID, 'yard_post_data', false, $found); - if ($found && $cachedPostData instanceof static) { + $cachedPostData = wp_cache_get($post->ID, self::CACHE_GROUP, false, $found); + if ($found && $cachedPostData instanceof PostData) { return $cachedPostData; } @@ -75,15 +77,15 @@ public static function fromPost(\WP_Post $post): static thumbnail: get_post_thumbnail_id($post->ID) ? new ImageData(get_post_thumbnail_id($post->ID)) : null, commentCount: post_type_supports($post->post_type, 'comments') ? (int) $post->comment_count : null, ); - wp_cache_set($post->ID, $postData, 'yard_post_data'); + wp_cache_set($post->ID, $postData, self::CACHE_GROUP); return $postData; } public static function fromCorcel(Post $post): static { - $cachedPostData = wp_cache_get($post->ID, 'yard_post_data', false, $found); - if ($found && $cachedPostData instanceof static) { + $cachedPostData = wp_cache_get($post->ID, self::CACHE_GROUP, false, $found); + if ($found && $cachedPostData instanceof PostData) { return $cachedPostData; } diff --git a/src/TermData.php b/src/TermData.php index 190b07e..815ec4c 100644 --- a/src/TermData.php +++ b/src/TermData.php @@ -13,6 +13,8 @@ class TermData extends Data { use HasMeta; + public const CACHE_GROUP = 'yard_term_data'; + public function __construct( #[MapInputName('term_id')] public int $id, @@ -26,7 +28,9 @@ public function __construct( public static function fromTerm(\WP_Term $term): TermData { - $cachedTermData = wp_cache_get($term->term_id, 'yard_term_data', false, $found); + wp_cache_add_non_persistent_groups([self::CACHE_GROUP]); + + $cachedTermData = wp_cache_get($term->term_id, self::CACHE_GROUP, false, $found); if ($found && $cachedTermData instanceof TermData) { return $cachedTermData; } @@ -38,7 +42,7 @@ public static function fromTerm(\WP_Term $term): TermData taxonomy: $term->taxonomy, description: $term->description, ); - wp_cache_set($term->term_id, $termData, 'yard_term_data'); + wp_cache_set($term->term_id, $termData, self::CACHE_GROUP); return $termData; } diff --git a/src/UserData.php b/src/UserData.php index 540e8d2..0e986f6 100644 --- a/src/UserData.php +++ b/src/UserData.php @@ -31,7 +31,7 @@ public function __construct( public static function fromUser(\WP_User $user): self { $cachedUserData = wp_cache_get($user->ID, 'yard_user_data', false, $found); - if ($found && $cachedUserData instanceof self) { + if ($found && $cachedUserData instanceof UserData) { return $cachedUserData; } From 9cba1f9facc0593158005f6898c4da3d52d28916 Mon Sep 17 00:00:00 2001 From: Yard workflows Date: Tue, 2 Jun 2026 08:30:49 +0000 Subject: [PATCH 4/9] style: apply php-cs-fixer changes --- src/CommentData.php | 4 ++-- src/PostData.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/CommentData.php b/src/CommentData.php index 57c2c66..16af26d 100644 --- a/src/CommentData.php +++ b/src/CommentData.php @@ -11,7 +11,7 @@ /** @phpstan-consistent-constructor */ class CommentData extends Data { - public const CACHE_GROUP = 'yard_comment_data'; + public const CACHE_GROUP = 'yard_comment_data'; use HasMeta; @@ -36,7 +36,7 @@ public function __construct( public static function fromComment(\WP_Comment $comment): static { - wp_cache_add_non_persistent_groups([self::CACHE_GROUP]); + wp_cache_add_non_persistent_groups([self::CACHE_GROUP]); $cachedCommentData = wp_cache_get($comment->comment_ID, self::CACHE_GROUP, false, $found); if ($found && $cachedCommentData instanceof CommentData) { diff --git a/src/PostData.php b/src/PostData.php index 7e54bfb..167e5b9 100644 --- a/src/PostData.php +++ b/src/PostData.php @@ -31,7 +31,7 @@ class PostData extends Data implements PostDataInterface { use HasMeta; - public const CACHE_GROUP = 'yard_post_data'; + public const CACHE_GROUP = 'yard_post_data'; public function __construct( #[MapInputName('ID')] From 28d334356f21182261b9a82500bc272683614746 Mon Sep 17 00:00:00 2001 From: Maarten Bruna <14947039+ictbeheer@users.noreply.github.com> Date: Tue, 2 Jun 2026 13:22:14 +0200 Subject: [PATCH 5/9] chore: phpstan --- src/PostData.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/PostData.php b/src/PostData.php index 167e5b9..6550de9 100644 --- a/src/PostData.php +++ b/src/PostData.php @@ -31,7 +31,7 @@ class PostData extends Data implements PostDataInterface { use HasMeta; - public const CACHE_GROUP = 'yard_post_data'; + public const CACHE_GROUP = 'yard_post_data'; public function __construct( #[MapInputName('ID')] @@ -60,6 +60,7 @@ public static function fromPost(\WP_Post $post): static { $cachedPostData = wp_cache_get($post->ID, self::CACHE_GROUP, false, $found); if ($found && $cachedPostData instanceof PostData) { + // @phpstan-ignore return.type return $cachedPostData; } @@ -86,6 +87,7 @@ public static function fromCorcel(Post $post): static { $cachedPostData = wp_cache_get($post->ID, self::CACHE_GROUP, false, $found); if ($found && $cachedPostData instanceof PostData) { + // @phpstan-ignore return.type return $cachedPostData; } From 3169ee0da12d555cfdc9b5c604368c9dc37bb554 Mon Sep 17 00:00:00 2001 From: Yard workflows Date: Tue, 2 Jun 2026 11:22:38 +0000 Subject: [PATCH 6/9] style: apply php-cs-fixer changes --- src/PostData.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PostData.php b/src/PostData.php index 6550de9..e60c472 100644 --- a/src/PostData.php +++ b/src/PostData.php @@ -31,7 +31,7 @@ class PostData extends Data implements PostDataInterface { use HasMeta; - public const CACHE_GROUP = 'yard_post_data'; + public const CACHE_GROUP = 'yard_post_data'; public function __construct( #[MapInputName('ID')] From e5462433b027e5f9dfd60168824741d50dcda394 Mon Sep 17 00:00:00 2001 From: Maarten Bruna <14947039+ictbeheer@users.noreply.github.com> Date: Tue, 2 Jun 2026 13:33:36 +0200 Subject: [PATCH 7/9] chore: phpstan --- phpstan.neon.dist | 13 ++++++++++++- src/PostData.php | 2 -- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/phpstan.neon.dist b/phpstan.neon.dist index a933ae0..a2f30c7 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -16,4 +16,15 @@ parameters: identifier: function.impossibleType path: src/Traits/HasMeta.php count: 3 - + - + message: '#Method Yard\\Data\\CommentData::fromComment\(\) should return static\(Yard\\Data\\CommentData\) but returns Yard\\Data\\CommentData.#' + path: src/CommentData.php + count: 1 + - + message: '#Method Yard\\Data\\PostData::fromPost\(\) should return static\(Yard\\Data\\PostData\) but returns Yard\\Data\\PostData.#' + path: src/PostData.php + count: 1 + - + message: '#Method Yard\\Data\\PostData::fromCorcel\(\) should return static\(Yard\\Data\\PostData\) but returns Yard\\Data\\PostData.#' + path: src/PostData.php + count: 1 diff --git a/src/PostData.php b/src/PostData.php index e60c472..167e5b9 100644 --- a/src/PostData.php +++ b/src/PostData.php @@ -60,7 +60,6 @@ public static function fromPost(\WP_Post $post): static { $cachedPostData = wp_cache_get($post->ID, self::CACHE_GROUP, false, $found); if ($found && $cachedPostData instanceof PostData) { - // @phpstan-ignore return.type return $cachedPostData; } @@ -87,7 +86,6 @@ public static function fromCorcel(Post $post): static { $cachedPostData = wp_cache_get($post->ID, self::CACHE_GROUP, false, $found); if ($found && $cachedPostData instanceof PostData) { - // @phpstan-ignore return.type return $cachedPostData; } From 2290e5eea92f9cc1435ee70fcda07b94eb47b02c Mon Sep 17 00:00:00 2001 From: Maarten Bruna <14947039+ictbeheer@users.noreply.github.com> Date: Fri, 12 Jun 2026 15:23:55 +0200 Subject: [PATCH 8/9] chore: only register non persistant cache groups once --- src/CommentData.php | 2 -- src/Providers/DataServiceProvider.php | 18 ++++++++++++++++++ src/TermData.php | 2 -- src/UserData.php | 6 ++++-- 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/CommentData.php b/src/CommentData.php index 16af26d..bfc8cc5 100644 --- a/src/CommentData.php +++ b/src/CommentData.php @@ -36,8 +36,6 @@ public function __construct( public static function fromComment(\WP_Comment $comment): static { - wp_cache_add_non_persistent_groups([self::CACHE_GROUP]); - $cachedCommentData = wp_cache_get($comment->comment_ID, self::CACHE_GROUP, false, $found); if ($found && $cachedCommentData instanceof CommentData) { return $cachedCommentData; diff --git a/src/Providers/DataServiceProvider.php b/src/Providers/DataServiceProvider.php index 95600a8..d80a593 100644 --- a/src/Providers/DataServiceProvider.php +++ b/src/Providers/DataServiceProvider.php @@ -5,6 +5,10 @@ namespace Yard\Data\Providers; use Illuminate\Support\ServiceProvider; +use Yard\Data\CommentData; +use Yard\Data\PostData; +use Yard\Data\TermData; +use Yard\Data\UserData; class DataServiceProvider extends ServiceProvider { @@ -31,5 +35,19 @@ public function boot() $this->publishes([ __DIR__.'/../../config/yard-data.php' => $this->app->configPath('yard-data.php'), ], 'config'); + + $this->registerNonPersistentCacheGroups(); + } + + protected function registerNonPersistentCacheGroups(): void + { + add_action('init', function () { + wp_cache_add_non_persistent_groups([ + PostData::CACHE_GROUP, + TermData::CACHE_GROUP, + UserData::CACHE_GROUP, + CommentData::CACHE_GROUP, + ]); + }); } } diff --git a/src/TermData.php b/src/TermData.php index 815ec4c..615cdb2 100644 --- a/src/TermData.php +++ b/src/TermData.php @@ -28,8 +28,6 @@ public function __construct( public static function fromTerm(\WP_Term $term): TermData { - wp_cache_add_non_persistent_groups([self::CACHE_GROUP]); - $cachedTermData = wp_cache_get($term->term_id, self::CACHE_GROUP, false, $found); if ($found && $cachedTermData instanceof TermData) { return $cachedTermData; diff --git a/src/UserData.php b/src/UserData.php index 0e986f6..1b955c8 100644 --- a/src/UserData.php +++ b/src/UserData.php @@ -15,6 +15,8 @@ #[MapInputName(UserPrefixMapper::class)] class UserData extends Data implements Castable { + public const CACHE_GROUP = 'yard_user_data'; + public function __construct( #[MapInputName('ID')] public int $id, @@ -30,7 +32,7 @@ public function __construct( public static function fromUser(\WP_User $user): self { - $cachedUserData = wp_cache_get($user->ID, 'yard_user_data', false, $found); + $cachedUserData = wp_cache_get($user->ID, self::CACHE_GROUP, false, $found); if ($found && $cachedUserData instanceof UserData) { return $cachedUserData; } @@ -43,7 +45,7 @@ public static function fromUser(\WP_User $user): self email: $user->user_email, displayName: $user->display_name, ); - wp_cache_set($user->ID, $userData, 'yard_user_data'); + wp_cache_set($user->ID, $userData, self::CACHE_GROUP); return $userData; } From 4bb2c53e54c392c3ecab97f13453a9b7c0bcf769 Mon Sep 17 00:00:00 2001 From: Maarten Bruna <14947039+ictbeheer@users.noreply.github.com> Date: Fri, 12 Jun 2026 15:34:51 +0200 Subject: [PATCH 9/9] chore: use less expensive method to check user existence --- phpstan.neon.dist | 8 ++++++++ src/CommentData.php | 2 +- src/PostData.php | 4 ++-- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/phpstan.neon.dist b/phpstan.neon.dist index a2f30c7..7fdc766 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -28,3 +28,11 @@ parameters: message: '#Method Yard\\Data\\PostData::fromCorcel\(\) should return static\(Yard\\Data\\PostData\) but returns Yard\\Data\\PostData.#' path: src/PostData.php count: 1 + - + message: '#Parameter \#1 $user of static method Yard\\Data\\UserData::fromUser\(\) expects WP_User, WP_User|false given.#' + path: src/PostData.php + count: 2 + - + message: '#Parameter \#1 $user of static method Yard\\Data\\UserData::fromUser\(\) expects WP_User, WP_User|false given.#' + path: src/CommentData.php + count: 1 diff --git a/src/CommentData.php b/src/CommentData.php index bfc8cc5..232452f 100644 --- a/src/CommentData.php +++ b/src/CommentData.php @@ -55,7 +55,7 @@ public static function fromComment(\WP_Comment $comment): static agent: $comment->comment_agent, type: $comment->comment_type, parent: 0 !== (int) $comment->comment_parent && null !== get_comment((int) $comment->comment_parent) ? CommentData::fromComment(get_comment((int) $comment->comment_parent)) : null, - user: 0 !== (int) $comment->user_id && false !== get_userdata((int) $comment->user_id) ? UserData::fromUser(get_userdata((int) $comment->user_id)) : null, + user: 0 !== (int) $comment->user_id && false !== \WP_User::get_data_by('id', (int) $comment->user_id) ? UserData::fromUser(get_userdata((int) $comment->user_id)) : null, ); wp_cache_set($comment->comment_ID, $commentData, self::CACHE_GROUP); diff --git a/src/PostData.php b/src/PostData.php index 167e5b9..9c8ad74 100644 --- a/src/PostData.php +++ b/src/PostData.php @@ -65,7 +65,7 @@ public static function fromPost(\WP_Post $post): static $postData = new (self::dataClass($post->post_type))( id: $post->ID, - author: false !== get_userdata((int) $post->post_author) ? UserData::fromUser(get_userdata((int) $post->post_author)) : null, + author: false !== \WP_User::get_data_by('id', (int) $post->post_author) ? UserData::fromUser(get_userdata((int) $post->post_author)) : null, title: $post->post_title, content: $post->post_content, excerpt: $post->post_excerpt, @@ -91,7 +91,7 @@ public static function fromCorcel(Post $post): static $postData = new (self::dataClass($post->post_type))( id: $post->ID, - author: false !== get_userdata($post->post_author) ? UserData::fromUser(get_userdata($post->post_author)) : null, + author: false !== \WP_User::get_data_by('id', $post->post_author) ? UserData::fromUser(get_userdata($post->post_author)) : null, title: $post->post_title, content: $post->post_content, excerpt: $post->post_excerpt,