CustomFieldsBulkActionEndpoint.php
2 months ago
CustomFieldsDuplicateEndpoint.php
2 months ago
CustomFieldsEndpoint.php
1 month ago
CustomFieldsGetEndpoint.php
1 month ago
CustomFieldsPostEndpoint.php
2 months ago
CustomFieldsPutEndpoint.php
2 months ago
index.php
2 months ago
CustomFieldsEndpoint.php
59 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\CustomFields\RestApi\Endpoints; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\API\REST\Endpoint; |
| 9 | use MailPoet\Config\AccessControl; |
| 10 | use MailPoet\Entities\CustomFieldEntity; |
| 11 | use MailPoet\WP\Functions as WPFunctions; |
| 12 | |
| 13 | abstract class CustomFieldsEndpoint extends Endpoint { |
| 14 | private const DATE_FORMAT = 'Y-m-d H:i:s'; |
| 15 | |
| 16 | public function checkPermissions(): bool { |
| 17 | return WPFunctions::get()->currentUserCan(AccessControl::PERMISSION_MANAGE_SUBSCRIBERS); |
| 18 | } |
| 19 | |
| 20 | protected function buildItem(CustomFieldEntity $customField): array { |
| 21 | $params = $customField->getParams(); |
| 22 | $label = isset($params['label']) && is_scalar($params['label']) ? (string)$params['label'] : $customField->getName(); |
| 23 | return [ |
| 24 | 'id' => (int)$customField->getId(), |
| 25 | 'name' => $customField->getName(), |
| 26 | 'label' => $label, |
| 27 | 'type' => $customField->getType(), |
| 28 | 'params' => $params, |
| 29 | 'required' => (bool)($params['required'] ?? false), |
| 30 | 'subscribers_count' => 0, |
| 31 | 'forms_count' => 0, |
| 32 | 'dynamic_segments_count' => 0, |
| 33 | 'created_at' => ($createdAt = $customField->getCreatedAt()) ? $createdAt->format(self::DATE_FORMAT) : null, |
| 34 | 'updated_at' => ($updatedAt = $customField->getUpdatedAt()) ? $updatedAt->format(self::DATE_FORMAT) : null, |
| 35 | 'deleted_at' => ($deletedAt = $customField->getDeletedAt()) ? $deletedAt->format(self::DATE_FORMAT) : null, |
| 36 | ]; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * @param array{id: int, name: string, label: string, type: string, params: array, required: bool, subscribers_count: int, forms_count: int, dynamic_segments_count: int, created_at: ?\DateTimeInterface, updated_at: ?\DateTimeInterface, deleted_at: ?\DateTimeInterface} $row |
| 41 | */ |
| 42 | protected function buildItemFromRow(array $row): array { |
| 43 | return [ |
| 44 | 'id' => (int)$row['id'], |
| 45 | 'name' => (string)$row['name'], |
| 46 | 'label' => (string)$row['label'], |
| 47 | 'type' => (string)$row['type'], |
| 48 | 'params' => $row['params'], |
| 49 | 'required' => (bool)$row['required'], |
| 50 | 'subscribers_count' => (int)$row['subscribers_count'], |
| 51 | 'forms_count' => (int)$row['forms_count'], |
| 52 | 'dynamic_segments_count' => (int)$row['dynamic_segments_count'], |
| 53 | 'created_at' => $row['created_at'] instanceof \DateTimeInterface ? $row['created_at']->format(self::DATE_FORMAT) : null, |
| 54 | 'updated_at' => $row['updated_at'] instanceof \DateTimeInterface ? $row['updated_at']->format(self::DATE_FORMAT) : null, |
| 55 | 'deleted_at' => $row['deleted_at'] instanceof \DateTimeInterface ? $row['deleted_at']->format(self::DATE_FORMAT) : null, |
| 56 | ]; |
| 57 | } |
| 58 | } |
| 59 |