Skip to content
Open
Show file tree
Hide file tree
Changes from 7 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
13 changes: 12 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 13 additions & 1 deletion src/CommentData.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
/** @phpstan-consistent-constructor */
class CommentData extends Data
{
public const CACHE_GROUP = 'yard_comment_data';

use HasMeta;

public function __construct(
Expand All @@ -34,7 +36,14 @@ public function __construct(

public static function fromComment(\WP_Comment $comment): static
{
return new 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;
}

$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,
Expand All @@ -50,5 +59,8 @@ 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, self::CACHE_GROUP);

return $commentData;
}
}
22 changes: 20 additions & 2 deletions src/PostData.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -56,7 +58,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, 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,
title: $post->post_title,
Expand All @@ -70,11 +77,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, 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,
title: $post->post_title,
Expand All @@ -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;
}

/**
Expand Down
14 changes: 13 additions & 1 deletion src/TermData.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -26,12 +28,22 @@ public function __construct(

public static function fromTerm(\WP_Term $term): TermData
{
return new static(
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;
}

$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;
}
}
10 changes: 9 additions & 1 deletion src/UserData.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,22 @@ 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 UserData) {
return $cachedUserData;
}

$userData = new self(
id: $user->ID,
login: $user->user_login,
password: $user->user_pass,
nicename: $user->user_nicename,
email: $user->user_email,
displayName: $user->display_name,
);
wp_cache_set($user->ID, $userData, 'yard_user_data');

return $userData;
}

/**
Expand Down
Loading