<?php namespace MailPoet\Models; if (!defined('ABSPATH')) exit; use MailPoet\Entities\CustomFieldEntity; use MailPoet\Util\DateConverter; use MailPoet\WP\Functions as WPFunctions; /** * @property string $name * @property string $type * @property string|array|null $params */ class CustomField extends Model { public static $_table = MP_CUSTOM_FIELDS_TABLE; // phpcs:ignore PSR2.Classes.PropertyDeclaration const TYPE_DATE = CustomFieldEntity::TYPE_DATE; const TYPE_TEXT = CustomFieldEntity::TYPE_TEXT; const TYPE_TEXTAREA = CustomFieldEntity::TYPE_TEXTAREA; const TYPE_RADIO = CustomFieldEntity::TYPE_RADIO; const TYPE_CHECKBOX = CustomFieldEntity::TYPE_CHECKBOX; const TYPE_SELECT = CustomFieldEntity::TYPE_SELECT; public function __construct() { parent::__construct(); $this->addValidations('name', [ 'required' => WPFunctions::get()->__('Please specify a name.', 'mailpoet'), ]); $this->addValidations('type', [ 'required' => WPFunctions::get()->__('Please specify a type.', 'mailpoet'), ]); } public function asArray() { $model = parent::asArray(); if (isset($model['params'])) { $model['params'] = (is_array($this->params)) ? $this->params : ($this->params !== null ? unserialize($this->params) : false); } return $model; } public function save() { if (is_null($this->params)) { $this->params = []; } $this->set('params', ( is_array($this->params) ? serialize($this->params) : $this->params )); return parent::save(); } public function formatValue($value = null) { // format custom field data depending on type if (is_array($value) && $this->type === self::TYPE_DATE) { $customFieldData = $this->asArray(); $dateFormat = $customFieldData['params']['date_format']; $dateType = (isset($customFieldData['params']['date_type']) ? $customFieldData['params']['date_type'] : 'year_month_day' ); $dateParts = explode('_', $dateType); switch ($dateType) { case 'year_month_day': $value = str_replace( ['DD', 'MM', 'YYYY'], [$value['day'], $value['month'], $value['year']], $dateFormat ); break; case 'year_month': $value = str_replace( ['MM', 'YYYY'], [$value['month'], $value['year']], $dateFormat ); break; case 'month': if ((int)$value['month'] === 0) { $value = ''; } else { $value = sprintf( '%s', $value['month'] ); } break; case 'day': if ((int)$value['day'] === 0) { $value = ''; } else { $value = sprintf( '%s', $value['day'] ); } break; case 'year': if ((int)$value['year'] === 0) { $value = ''; } else { $value = sprintf( '%04d', $value['year'] ); } break; } if (!empty($value) && is_string($value)) { $value = (new DateConverter())->convertDateToDatetime($value, $dateFormat); } } return $value; } public function subscribers() { return $this->hasManyThrough( __NAMESPACE__ . '\Subscriber', __NAMESPACE__ . '\SubscriberCustomField', 'custom_field_id', 'subscriber_id' )->selectExpr(MP_SUBSCRIBER_CUSTOM_FIELD_TABLE . '.value'); } public static function createOrUpdate($data = []) { // set name as label by default if (empty($data['params']['label']) && isset($data['name'])) { $data['params']['label'] = $data['name']; } return parent::_createOrUpdate($data); } }
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
CustomField.php | File | 3.79 KB | 0644 |
|
DynamicSegment.php | File | 4.05 KB | 0644 |
|
DynamicSegmentFilter.php | File | 1.35 KB | 0644 |
|
Form.php | File | 4.02 KB | 0644 |
|
MappingToExternalEntities.php | File | 378 B | 0644 |
|
Model.php | File | 13.58 KB | 0644 |
|
ModelValidator.php | File | 2.44 KB | 0644 |
|
Newsletter.php | File | 15.14 KB | 0644 |
|
NewsletterLink.php | File | 530 B | 0644 |
|
NewsletterOption.php | File | 670 B | 0644 |
|
NewsletterOptionField.php | File | 939 B | 0644 |
|
NewsletterSegment.php | File | 312 B | 0644 |
|
ScheduledTask.php | File | 6.58 KB | 0644 |
|
ScheduledTaskSubscriber.php | File | 5.31 KB | 0644 |
|
Segment.php | File | 10.89 KB | 0644 |
|
SendingQueue.php | File | 4.31 KB | 0644 |
|
StatisticsClicks.php | File | 1.08 KB | 0644 |
|
StatisticsForms.php | File | 1.85 KB | 0644 |
|
StatisticsNewsletters.php | File | 1.61 KB | 0644 |
|
StatisticsOpens.php | File | 807 B | 0644 |
|
StatisticsUnsubscribes.php | File | 821 B | 0644 |
|
StatisticsWooCommercePurchases.php | File | 1.5 KB | 0644 |
|
Subscriber.php | File | 20.83 KB | 0644 |
|
SubscriberCustomField.php | File | 2.5 KB | 0644 |
|
SubscriberIP.php | File | 1.12 KB | 0644 |
|
SubscriberSegment.php | File | 5.2 KB | 0644 |
|
index.php | File | 0 B | 0644 |
|