Skip to content

Commit

Permalink
Merge branch 'main' of github.com:gebruederheitz/wp-easy-cpt into main
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasMaros committed Jan 15, 2024
2 parents 1db9405 + d942504 commit 166ee8a
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/CustomPostType/PostType.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ protected function registerMetabox(): void
static::$prettyName . ' Details',
[$this, 'renderMetabox'],
static::$postTypeName,
self::$context,
static::$context,
'low',
);
}
Expand Down
41 changes: 26 additions & 15 deletions src/Domain/AbstractRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
* @phpstan-template T of StorableEntity
* @template T of StorableEntity
* @implements EntityRepository<T>
* @phpstan-type ResultArray array<int, array{item: T, dirty: bool}>
* //phpstan-type ResultArray<T> array<int, array{item: T, dirty: bool}>
* ^ this is not currently possible in phpstan and can not be imported: https://github.com/phpstan/phpstan/issues/6099
*/
abstract class AbstractRepository extends Singleton implements
EntityRepository,
Expand All @@ -20,7 +21,7 @@ abstract class AbstractRepository extends Singleton implements
/** @var string */
public static $metaKey;

/** @var ResultArray */
/** @var array<int, array{item: T, dirty: bool}> */
protected $entities = [];

/** @var class-string<T> FQCN of an entity class implementing StorableEntity */
Expand Down Expand Up @@ -125,24 +126,13 @@ public function refresh(): EntityRepository
* Retrieve all posts fetched by getPosts() directly from the database and
* instantiate them into StorableEntities using entityFromPostId().
*
* @return ResultArray
* @return array<int, array{item: T, dirty: bool}>
*/
protected static function getAllFromDB(): array
{
$result = [];

$posts = static::getPosts();

foreach ($posts as $post) {
$entity = static::entityFromPostId($post->ID, $post);

$result[$post->ID] = [
'item' => $entity,
'dirty' => false,
];
}

return $result;
return self::mapPostsToEntities($posts);
}

/**
Expand Down Expand Up @@ -222,6 +212,27 @@ protected function getById(?int $postId, &$isDirty = null): ?StorableEntity
*/
abstract protected static function getPosts(): array;

/**
* @param array<WP_Post> $posts
*
* @return array<int, array{item: T, dirty: bool}>
*/
protected static function mapPostsToEntities(array $posts): array
{
$entities = [];

foreach ($posts as $post) {
$entity = static::entityFromPostId($post->ID, $post);

$entities[$post->ID] = [
'item' => $entity,
'dirty' => false,
];
}

return $entities;
}

/**
* Instantiates an entity instance from a post ID.
*
Expand Down
16 changes: 13 additions & 3 deletions src/Domain/CustomPostTypeRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,26 @@ class CustomPostTypeRepository extends AbstractRepository
* ordering etc., you should override this method the fetch the list of
* posts relevant to you
*
* @param ?array<string, mixed> $customQuery
*
* @return WP_Post[]
*/
protected static function getPosts(): array
protected static function getPosts(?array $customQuery = null): array
{
return get_posts([
$baseQuery = [
'post_type' => call_user_func([
static::$postTypeClass,
'getPostTypeName',
]),
'numberposts' => -1,
]);
];

if ($customQuery) {
$query = array_merge($baseQuery, $customQuery);
} else {
$query = $baseQuery;
}

return get_posts($query);
}
}
4 changes: 2 additions & 2 deletions util/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion util/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gebruederheitz/wp-easy-cpt-util",
"version": "2.1.0",
"version": "2.3.2",
"description": "Utilities for the gebruederheitz/wp-easy-cpt PHP composer package",
"main": "",
"engines": {
Expand Down

0 comments on commit 166ee8a

Please sign in to comment.