Skip to content

Commit

Permalink
Use WP table prefix in User capabilities field mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
rpkamp authored and williarin committed Oct 10, 2022
1 parent 049107a commit 22a9603
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/Bridge/Repository/AbstractEntityRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@
use function Williarin\WordpressInterop\Util\String\property_to_field;
use function Williarin\WordpressInterop\Util\String\select_from_eav;

/**
* @method getMappedFields(): array
*/
abstract class AbstractEntityRepository implements EntityRepositoryInterface
{
use FindByTrait;
use EntityPropertiesTrait;

/** @deprecated Implement getMappedFields() method instead */
protected const MAPPED_FIELDS = [];
protected const TABLE_NAME = 'posts';
protected const TABLE_META_NAME = 'postmeta';
Expand Down Expand Up @@ -216,10 +220,14 @@ public function persist(mixed $entity): void

public function getMappedMetaKey(string $fieldName, string $entityClassName = null): string
{
$mappedFields = $entityClassName ? (new \ReflectionClassConstant(
$this->entityManager->getRepository($entityClassName),
'MAPPED_FIELDS',
))->getValue() : static::MAPPED_FIELDS;
$targetClass = $entityClassName ? $this->entityManager->getRepository($entityClassName) : $this;

if (method_exists($targetClass, 'getMappedFields')) {
$mappedFields = $targetClass->getMappedFields();
} else {
// BC layer, to be removed when MAPPED_FIELDS constant is removed
$mappedFields = (new \ReflectionClassConstant($targetClass, 'MAPPED_FIELDS',))->getValue();
}

if (
!is_array($mappedFields)
Expand All @@ -240,10 +248,14 @@ public function getMappedMetaKey(string $fieldName, string $entityClassName = nu

public function isFieldMapped(string $fieldName, string $entityClassName = null): bool
{
$mappedFields = $entityClassName ? (new \ReflectionClassConstant(
$this->entityManager->getRepository($entityClassName),
'MAPPED_FIELDS',
))->getValue() : static::MAPPED_FIELDS;
$targetClass = $entityClassName ? $this->entityManager->getRepository($entityClassName) : $this;

if (method_exists($targetClass, 'getMappedFields')) {
$mappedFields = $targetClass->getMappedFields();
} else {
// BC layer, to be removed when MAPPED_FIELDS constant is removed
$mappedFields = (new \ReflectionClassConstant($targetClass, 'MAPPED_FIELDS',))->getValue();
}

if (
!is_array($mappedFields)
Expand Down
6 changes: 6 additions & 0 deletions src/Bridge/Repository/AttachmentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
*/
class AttachmentRepository extends AbstractEntityRepository
{
/** @deprecated Left for BC reasons only, use getMappedFields instead */
protected const MAPPED_FIELDS = [
'_wp_attached_file' => 'attached_file',
'_wp_attachment_metadata' => 'attachment_metadata',
Expand All @@ -78,4 +79,9 @@ protected function getPostType(): string
{
return 'attachment';
}

protected function getMappedFields(): array
{
return self::MAPPED_FIELDS;
}
}
6 changes: 6 additions & 0 deletions src/Bridge/Repository/PageRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
*/
class PageRepository extends AbstractEntityRepository
{
/** @deprecated Left for BC reasons only, use getMappedFields instead */
protected const MAPPED_FIELDS = ['thumbnail_id'];

public function __construct()
Expand All @@ -67,4 +68,9 @@ protected function getPostType(): string
{
return 'page';
}

protected function getMappedFields(): array
{
return self::MAPPED_FIELDS;
}
}
6 changes: 6 additions & 0 deletions src/Bridge/Repository/PostRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,16 @@
*/
class PostRepository extends AbstractEntityRepository
{
/** @deprecated Left for BC reasons only, use getMappedFields instead */
protected const MAPPED_FIELDS = ['thumbnail_id'];

public function __construct()
{
parent::__construct(Post::class);
}

protected function getMappedFields(): array
{
return self::MAPPED_FIELDS;
}
}
6 changes: 6 additions & 0 deletions src/Bridge/Repository/ProductRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
*/
class ProductRepository extends AbstractEntityRepository
{
/** @deprecated Left for BC reasons only, use getMappedFields instead */
protected const MAPPED_FIELDS = [
'sku',
'sale_price_dates_from',
Expand Down Expand Up @@ -155,4 +156,9 @@ protected function getPostType(): string
{
return 'product';
}

protected function getMappedFields(): array
{
return self::MAPPED_FIELDS;
}
}
6 changes: 6 additions & 0 deletions src/Bridge/Repository/ShopOrderRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
*/
class ShopOrderRepository extends AbstractEntityRepository
{
/** @deprecated Left for BC reasons only, use getMappedFields instead */
protected const MAPPED_FIELDS = [
'payment_method',
'billing_email',
Expand Down Expand Up @@ -94,4 +95,9 @@ public function getPostType(): string
{
return 'shop_order';
}

protected function getMappedFields(): array
{
return self::MAPPED_FIELDS;
}
}
15 changes: 15 additions & 0 deletions src/Bridge/Repository/UserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class UserRepository extends AbstractEntityRepository
protected const TABLE_META_IDENTIFIER = 'user_id';
protected const FALLBACK_ENTITY = User::class;

/** @deprecated Left for BC reasons only, use getMappedFields instead */
protected const MAPPED_FIELDS = [
'billing_address_1' => 'billing_address_1',
'billing_address_2' => 'billing_address_2',
Expand All @@ -53,4 +54,18 @@ public function __construct()
{
parent::__construct(User::class);
}

protected function getMappedFields(): array
{
$capabilitiesKey = sprintf('%scapabilities', $this->entityManager->getTablesPrefix());

return [
'billing_address_1' => 'billing_address_1',
'billing_address_2' => 'billing_address_2',
'shipping_address_1' => 'shipping_address_1',
'shipping_address_2' => 'shipping_address_2',
$capabilitiesKey => 'capabilities',
'wp_last_active' => 'last_active',
];
}
}
17 changes: 17 additions & 0 deletions test/Test/Bridge/Repository/UserRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Williarin\WordpressInterop\Bridge\Entity\User;
use Williarin\WordpressInterop\Bridge\Repository\UserRepository;
use Williarin\WordpressInterop\EntityManager;
use Williarin\WordpressInterop\Exception\EntityNotFoundException;
use Williarin\WordpressInterop\Test\TestCase;

Expand Down Expand Up @@ -100,4 +101,20 @@ public function testFindOneByEavAttribute(): void
$user = $this->repository->findOneByShippingState('North Dakota');
self::assertSame(4, $user->id);
}

public function testFieldMapping(): void
{
$repository = new UserRepository();
$repository->setEntityManager(new class extends EntityManager {
public function __construct() {}
public function getTablesPrefix(): string { return 'foo_'; }
});

$this->assertEquals('billing_address_1', $repository->getMappedMetaKey('billing_address_1'));
$this->assertEquals('billing_address_2', $repository->getMappedMetaKey('billing_address_2'));
$this->assertEquals('shipping_address_1', $repository->getMappedMetaKey('shipping_address_1'));
$this->assertEquals('shipping_address_2', $repository->getMappedMetaKey('shipping_address_2'));
$this->assertEquals('foo_capabilities', $repository->getMappedMetaKey('capabilities'));
$this->assertEquals('wp_last_active', $repository->getMappedMetaKey('last_active'));
}
}

0 comments on commit 22a9603

Please sign in to comment.