Skip to content
Open
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
21 changes: 20 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 12 additions & 2 deletions 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,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,
Expand All @@ -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;
}
}
26 changes: 22 additions & 4 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,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,
Expand All @@ -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,
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
18 changes: 18 additions & 0 deletions src/Providers/DataServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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,
]);
});
}
}
12 changes: 11 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,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;
}
}
12 changes: 11 additions & 1 deletion src/UserData.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -30,14 +32,22 @@ 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,
nicename: $user->user_nicename,
email: $user->user_email,
displayName: $user->display_name,
);
wp_cache_set($user->ID, $userData, self::CACHE_GROUP);

return $userData;
}

/**
Expand Down
Loading