diff --git a/phpstan.neon.dist b/phpstan.neon.dist index a933ae0..7fdc766 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -16,4 +16,23 @@ 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 + - + 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 e3b0352..232452f 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,7 +36,12 @@ public function __construct( public static function fromComment(\WP_Comment $comment): static { - return new static( + $cachedCommentData = wp_cache_get($comment->comment_ID, self::CACHE_GROUP, false, $found); + if ($found && $cachedCommentData instanceof CommentData) { + 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, @@ -48,7 +55,10 @@ 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); + + return $commentData; } } diff --git a/src/PostData.php b/src/PostData.php index 02239d6..9c8ad74 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,9 +58,14 @@ public function __construct( public static function fromPost(\WP_Post $post): static { - return new (self::dataClass($post->post_type))( + $cachedPostData = wp_cache_get($post->ID, self::CACHE_GROUP, false, $found); + if ($found && $cachedPostData instanceof PostData) { + 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, + 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, @@ -70,13 +77,21 @@ 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, self::CACHE_GROUP); + + return $postData; } public static function fromCorcel(Post $post): static { - return new (self::dataClass($post->post_type))( + $cachedPostData = wp_cache_get($post->ID, self::CACHE_GROUP, false, $found); + if ($found && $cachedPostData instanceof PostData) { + 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, + 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, @@ -88,6 +103,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/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 7dd64a6..615cdb2 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,12 +28,20 @@ public function __construct( public static function fromTerm(\WP_Term $term): TermData { - return new static( + $cachedTermData = wp_cache_get($term->term_id, self::CACHE_GROUP, 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, self::CACHE_GROUP); + + return $termData; } } diff --git a/src/UserData.php b/src/UserData.php index 7999b0a..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,12 @@ public function __construct( public static function fromUser(\WP_User $user): self { - return new self( + $cachedUserData = wp_cache_get($user->ID, self::CACHE_GROUP, false, $found); + if ($found && $cachedUserData instanceof UserData) { + return $cachedUserData; + } + + $userData = new self( id: $user->ID, login: $user->user_login, password: $user->user_pass, @@ -38,6 +45,9 @@ public static function fromUser(\WP_User $user): self email: $user->user_email, displayName: $user->display_name, ); + wp_cache_set($user->ID, $userData, self::CACHE_GROUP); + + return $userData; } /**