Adds method to return subscriber fields

This commit is contained in:
Vlad
2017-05-10 20:49:14 -04:00
parent cedd94550f
commit 5e34bbf9d5
3 changed files with 36 additions and 2 deletions

View File

@ -1,7 +1,35 @@
<?php
namespace MailPoet\API\MP\v1;
use MailPoet\Models\CustomField;
if(!defined('ABSPATH')) exit;
class API {
function getSubscriberFields() {
$data = array(
array(
'id' => 'email',
'name' => __('Email', 'mailpoet')
),
array(
'id' => 'first_name',
'name' => __('First name', 'mailpoet')
),
array(
'id' => 'last_name',
'name' => __('Last name', 'mailpoet')
)
);
$custom_fields = CustomField::selectMany(array('id', 'name'))->findMany();
foreach($custom_fields as $custom_field) {
$data[] = array(
'id' => 'cf_' . $custom_field->id,
'name' => $custom_field->name
);
}
return $data;
}
}

View File

@ -7,6 +7,12 @@ if(!defined('ABSPATH')) exit;
class CustomField extends Model {
public static $_table = MP_CUSTOM_FIELDS_TABLE;
const TYPE_DATE = 'date';
const TYPE_TEXT = 'text';
const TYPE_TEXTAREA = 'textarea';
const TYPE_RADIO = 'radio';
const TYPE_CHECKBOX = 'checkbox';
const TYPE_SELECT = 'select';
function __construct() {
parent::__construct();
@ -43,7 +49,7 @@ class CustomField extends Model {
function formatValue($value = null) {
// format custom field data depending on type
if(is_array($value) && $this->type === 'date' ) {
if(is_array($value) && $this->type === self::TYPE_DATE) {
$custom_field_data = $this->asArray();
$date_format = $custom_field_data['params']['date_format'];
$date_type = (isset($custom_field_data['params']['date_type'])

View File

@ -8,7 +8,7 @@ class CustomFieldTest extends MailPoetTest {
function _before() {
$this->data = array(
'name' => 'City',
'type' => 'text',
'type' => CustomField::TYPE_TEXT,
'params' => array(
'label' => 'What is your city?'
)