Normalize settings values
[MAILPOET-2823]
This commit is contained in:
committed by
Veljko V
parent
9c56c476d9
commit
0fdde2085f
@@ -1,4 +1,5 @@
|
|||||||
import { State, Settings } from './types';
|
import { State } from './types';
|
||||||
|
import normalizeSettings from './normalize_settings';
|
||||||
|
|
||||||
export default function makeDefaultState(window: any): State {
|
export default function makeDefaultState(window: any): State {
|
||||||
return {
|
return {
|
||||||
@@ -11,7 +12,7 @@ export default function makeDefaultState(window: any): State {
|
|||||||
woocommerce: !!window.mailpoet_woocommerce_active,
|
woocommerce: !!window.mailpoet_woocommerce_active,
|
||||||
newUser: !!window.mailpoet_is_new_user,
|
newUser: !!window.mailpoet_is_new_user,
|
||||||
},
|
},
|
||||||
data: window.mailpoet_settings,
|
data: normalizeSettings(window.mailpoet_settings),
|
||||||
segments: window.mailpoet_segments,
|
segments: window.mailpoet_segments,
|
||||||
pages: window.mailpoet_pages,
|
pages: window.mailpoet_pages,
|
||||||
};
|
};
|
||||||
|
223
assets/js/src/settings/store/normalize_settings.ts
Normal file
223
assets/js/src/settings/store/normalize_settings.ts
Normal file
@@ -0,0 +1,223 @@
|
|||||||
|
import _ from 'lodash';
|
||||||
|
import { t } from 'common/functions';
|
||||||
|
import { Settings } from './types';
|
||||||
|
|
||||||
|
export default function normalizeSettings(data: any): Settings {
|
||||||
|
const text = asString('');
|
||||||
|
const disabledCheckbox = asBoolean('1', '0', '0');
|
||||||
|
const enabledCheckbox = asBoolean('1', '0', '1');
|
||||||
|
const disabledRadio = asBoolean('1', '', '');
|
||||||
|
const enabledRadio = asBoolean('1', '', '1');
|
||||||
|
const emptyArray = asStringArray([]);
|
||||||
|
const smtpServer = asEnum(['server', 'manual', 'AmazonSES', 'SendGrid'], 'server');
|
||||||
|
|
||||||
|
const settingsSchema = asObject({
|
||||||
|
sender: asObject({ name: text, address: text }),
|
||||||
|
reply_to: asObject({ name: text, address: text }),
|
||||||
|
subscribe: asObject({
|
||||||
|
on_comment: asObject({ enabled: disabledCheckbox, label: asString(t('yesAddMe')), segments: emptyArray }),
|
||||||
|
on_register: asObject({ enabled: disabledCheckbox, label: asString(t('yesAddMe')), segments: emptyArray }),
|
||||||
|
}),
|
||||||
|
subscription: asObject({
|
||||||
|
pages: asObject({
|
||||||
|
manage: text,
|
||||||
|
unsubscribe: text,
|
||||||
|
confirmation: text,
|
||||||
|
captcha: text,
|
||||||
|
}),
|
||||||
|
segments: emptyArray,
|
||||||
|
}),
|
||||||
|
stats_notifications: asObject({
|
||||||
|
enabled: enabledCheckbox,
|
||||||
|
automated: enabledCheckbox,
|
||||||
|
address: text,
|
||||||
|
}),
|
||||||
|
subscriber_email_notification: asObject({ enabled: enabledRadio, address: text }),
|
||||||
|
cron_trigger: asObject({
|
||||||
|
method: asEnum(['WordPress', 'MailPoet', 'Linux Cron'], 'WordPress'),
|
||||||
|
}),
|
||||||
|
tracking: asObject({ enabled: enabledRadio }),
|
||||||
|
send_transactional_emails: enabledRadio,
|
||||||
|
deactivate_subscriber_after_inactive_days: asEnum(['', '90', '180', '365'], '180'),
|
||||||
|
analytics: asObject({ enabled: disabledRadio }),
|
||||||
|
captcha: asObject({
|
||||||
|
type: asEnum(['', 'built-in', 'recaptcha'], 'built-in'),
|
||||||
|
recaptcha_site_token: text,
|
||||||
|
recaptcha_secret_token: text,
|
||||||
|
}),
|
||||||
|
logging: asEnum(['everything', 'errors', 'nothing'], 'errors'),
|
||||||
|
mta_group: asEnum(['mailpoet', 'website', 'smtp'], 'website'),
|
||||||
|
mta: asObject({
|
||||||
|
method: asEnum([
|
||||||
|
'MailPoet',
|
||||||
|
'AmazonSES',
|
||||||
|
'SendGrid',
|
||||||
|
'PHPMail',
|
||||||
|
'SMTP',
|
||||||
|
], 'PHPMail'),
|
||||||
|
frequency: asObject({
|
||||||
|
emails: asString('25'),
|
||||||
|
interval: asString('5'),
|
||||||
|
}),
|
||||||
|
mailpoet_api_key: text,
|
||||||
|
host: text,
|
||||||
|
port: text,
|
||||||
|
region: asEnum([
|
||||||
|
'us-east-1',
|
||||||
|
'us-west-2',
|
||||||
|
'eu-west-1',
|
||||||
|
'eu-central-1',
|
||||||
|
'ap-south-1',
|
||||||
|
'ap-southeast-2',
|
||||||
|
], 'us-east-1'),
|
||||||
|
access_key: text,
|
||||||
|
secret_key: text,
|
||||||
|
api_key: text,
|
||||||
|
login: text,
|
||||||
|
password: text,
|
||||||
|
encryption: text,
|
||||||
|
authentication: asEnum(['1', '-1'], '1'),
|
||||||
|
mailpoet_api_key_state: asObject({
|
||||||
|
state: asEnum([
|
||||||
|
'valid',
|
||||||
|
'invalid',
|
||||||
|
'expiring',
|
||||||
|
'already_used',
|
||||||
|
'check_error',
|
||||||
|
], 'check_error'),
|
||||||
|
data: asIs,
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
mailpoet_smtp_provider: smtpServer,
|
||||||
|
smtp_provider: smtpServer,
|
||||||
|
web_host: asEnum([
|
||||||
|
'manual',
|
||||||
|
'1and1',
|
||||||
|
'bluehost',
|
||||||
|
'df',
|
||||||
|
'dreamhost',
|
||||||
|
'free',
|
||||||
|
'froghost',
|
||||||
|
'godaddy',
|
||||||
|
'goneo',
|
||||||
|
'googleapps',
|
||||||
|
'greengeeks',
|
||||||
|
'hawkhost',
|
||||||
|
'hivetec',
|
||||||
|
'hostgator',
|
||||||
|
'hosting2go',
|
||||||
|
'hostmonster',
|
||||||
|
'infomaniak',
|
||||||
|
'justhost',
|
||||||
|
'laughingsquid',
|
||||||
|
'lunarpages',
|
||||||
|
'mediatemple',
|
||||||
|
'netfirms',
|
||||||
|
'netissime',
|
||||||
|
'one',
|
||||||
|
'ovh',
|
||||||
|
'phpnet',
|
||||||
|
'planethoster',
|
||||||
|
'rochen',
|
||||||
|
'site5',
|
||||||
|
'siteground',
|
||||||
|
'synthesis',
|
||||||
|
'techark',
|
||||||
|
'vexxhost',
|
||||||
|
'vps',
|
||||||
|
'webcity',
|
||||||
|
'westhost',
|
||||||
|
'wpwebhost',
|
||||||
|
], 'manual'),
|
||||||
|
mailpoet_sending_frequency: asEnum(['auto', 'manual'], 'manual'),
|
||||||
|
signup_confirmation: asObject({
|
||||||
|
enabled: enabledRadio,
|
||||||
|
subject: text,
|
||||||
|
body: text,
|
||||||
|
}),
|
||||||
|
woocommerce: asObject({
|
||||||
|
transactional_email_id: text,
|
||||||
|
optin_on_checkout: asObject({
|
||||||
|
enabled: enabledRadio,
|
||||||
|
message: text,
|
||||||
|
}),
|
||||||
|
accept_cookie_revenue_tracking: asObject({
|
||||||
|
enabled: disabledRadio,
|
||||||
|
set: enabledRadio,
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
mailpoet_subscribe_old_woocommerce_customers: asObject({
|
||||||
|
enabled: enabledRadio,
|
||||||
|
}),
|
||||||
|
premium: asObject({
|
||||||
|
premium_key: text,
|
||||||
|
premium_key_state: asObject({
|
||||||
|
state: asEnum([
|
||||||
|
'valid',
|
||||||
|
'invalid',
|
||||||
|
'expiring',
|
||||||
|
'already_used',
|
||||||
|
'check_error',
|
||||||
|
], 'check_error'),
|
||||||
|
data: asIs,
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
return settingsSchema(data) as Settings;
|
||||||
|
}
|
||||||
|
|
||||||
|
function asString(defaultValue: string) {
|
||||||
|
return (value: any): string => {
|
||||||
|
if (value === undefined) return defaultValue;
|
||||||
|
if (!value) return '';
|
||||||
|
return `${value}`;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function asStringArray(defaultValue: string[]) {
|
||||||
|
return (value: any): string[] => {
|
||||||
|
if (!_.isArray(value)) return defaultValue;
|
||||||
|
return value.map(asString(''));
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function asBoolean<T, F>(trueValue: T, falseValue: F, defaultValue: T | F) {
|
||||||
|
return (value: any): T | F => {
|
||||||
|
if (value === undefined) return defaultValue;
|
||||||
|
if (value) return trueValue;
|
||||||
|
return falseValue;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function asEnum(choices: string[], defaultValue: string) {
|
||||||
|
return (value: any): string => {
|
||||||
|
if (!choices.includes(value)) return defaultValue;
|
||||||
|
return value;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
type Schema = {
|
||||||
|
[key: string]: ReturnType<
|
||||||
|
| typeof asString
|
||||||
|
| typeof asStringArray
|
||||||
|
| typeof asBoolean
|
||||||
|
| typeof asEnum
|
||||||
|
| typeof asObject
|
||||||
|
>
|
||||||
|
}
|
||||||
|
type SchemaResult<T extends Schema> = {
|
||||||
|
[key in keyof T]: ReturnType<T[key]>
|
||||||
|
}
|
||||||
|
function asObject<T extends Schema>(schema: T) {
|
||||||
|
return (value: any): SchemaResult<T> => {
|
||||||
|
const object = Object.keys(schema).reduce((result, field) => ({
|
||||||
|
[field]: schema[field](value ? value[field] : undefined),
|
||||||
|
...result,
|
||||||
|
}), {});
|
||||||
|
return object as SchemaResult<T>;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function asIs<T>(value: T): T {
|
||||||
|
return value;
|
||||||
|
}
|
@@ -37,9 +37,131 @@ export type Settings = {
|
|||||||
enabled: '' | '1'
|
enabled: '' | '1'
|
||||||
address: string
|
address: string
|
||||||
}
|
}
|
||||||
// ...
|
cron_trigger: {
|
||||||
|
method: 'WordPress' | 'MailPoet' | 'Linux Cron'
|
||||||
}
|
}
|
||||||
|
tracking: {
|
||||||
|
enabled: '' | '1'
|
||||||
|
}
|
||||||
|
send_transactional_emails: '' | '1'
|
||||||
|
deactivate_subscriber_after_inactive_days: '' | '90' | '180' | '365'
|
||||||
|
analytics: {
|
||||||
|
enabled: '' | '1'
|
||||||
|
}
|
||||||
|
captcha: {
|
||||||
|
type: 'built-in' | 'recaptcha' | ''
|
||||||
|
recaptcha_site_token: string
|
||||||
|
recaptcha_secret_token: string
|
||||||
|
}
|
||||||
|
logging: 'everything' | 'errors' | 'nothing'
|
||||||
|
mta_group: 'mailpoet' | 'website' | 'smtp'
|
||||||
|
mta: {
|
||||||
|
method: 'MailPoet' | 'AmazonSES' | 'SendGrid' | 'PHPMail' | 'SMTP'
|
||||||
|
frequency: {
|
||||||
|
emails: string
|
||||||
|
interval: string
|
||||||
|
}
|
||||||
|
mailpoet_api_key: string
|
||||||
|
host: string
|
||||||
|
port: string
|
||||||
|
region:
|
||||||
|
| 'us-east-1'
|
||||||
|
| 'us-west-2'
|
||||||
|
| 'eu-west-1'
|
||||||
|
| 'eu-central-1'
|
||||||
|
| 'ap-south-1'
|
||||||
|
| 'ap-southeast-2'
|
||||||
|
access_key: string
|
||||||
|
secret_key: string
|
||||||
|
api_key: string
|
||||||
|
login: string
|
||||||
|
password: string
|
||||||
|
encryption: string
|
||||||
|
authentication: '1' | '-1'
|
||||||
|
mailpoet_api_key_state: {
|
||||||
|
state:
|
||||||
|
| 'valid'
|
||||||
|
| 'invalid'
|
||||||
|
| 'expiring'
|
||||||
|
| 'already_used'
|
||||||
|
| 'check_error'
|
||||||
|
data: any
|
||||||
|
}
|
||||||
|
}
|
||||||
|
mailpoet_smtp_provider: 'server' | 'manual' | 'AmazonSES' | 'SendGrid'
|
||||||
|
smtp_provider: 'server' | 'manual' | 'AmazonSES' | 'SendGrid',
|
||||||
|
web_host:
|
||||||
|
| 'manual'
|
||||||
|
| '1and1'
|
||||||
|
| 'bluehost'
|
||||||
|
| 'df'
|
||||||
|
| 'dreamhost'
|
||||||
|
| 'free'
|
||||||
|
| 'froghost'
|
||||||
|
| 'godaddy'
|
||||||
|
| 'goneo'
|
||||||
|
| 'googleapps'
|
||||||
|
| 'greengeeks'
|
||||||
|
| 'hawkhost'
|
||||||
|
| 'hivetec'
|
||||||
|
| 'hostgator'
|
||||||
|
| 'hosting2go'
|
||||||
|
| 'hostmonster'
|
||||||
|
| 'infomaniak'
|
||||||
|
| 'justhost'
|
||||||
|
| 'laughingsquid'
|
||||||
|
| 'lunarpages'
|
||||||
|
| 'mediatemple'
|
||||||
|
| 'netfirms'
|
||||||
|
| 'netissime'
|
||||||
|
| 'one'
|
||||||
|
| 'ovh'
|
||||||
|
| 'phpnet'
|
||||||
|
| 'planethoster'
|
||||||
|
| 'rochen'
|
||||||
|
| 'site5'
|
||||||
|
| 'siteground'
|
||||||
|
| 'synthesis'
|
||||||
|
| 'techark'
|
||||||
|
| 'vexxhost'
|
||||||
|
| 'vps'
|
||||||
|
| 'webcity'
|
||||||
|
| 'westhost'
|
||||||
|
| 'wpwebhost'
|
||||||
|
mailpoet_sending_frequency: 'auto' | 'manual'
|
||||||
|
|
||||||
|
signup_confirmation: {
|
||||||
|
enabled: '1' | ''
|
||||||
|
subject: string
|
||||||
|
body: string
|
||||||
|
}
|
||||||
|
woocommerce: {
|
||||||
|
transactional_email_id: string,
|
||||||
|
optin_on_checkout: {
|
||||||
|
enabled: '1' | ''
|
||||||
|
message: string
|
||||||
|
}
|
||||||
|
accept_cookie_revenue_tracking: {
|
||||||
|
enabled: '1' | '',
|
||||||
|
set: '1' | ''
|
||||||
|
}
|
||||||
|
}
|
||||||
|
mailpoet_subscribe_old_woocommerce_customers: {
|
||||||
|
enabled: '1' | ''
|
||||||
|
}
|
||||||
|
premium: {
|
||||||
|
premium_key: string
|
||||||
|
premium_key_state: {
|
||||||
|
state:
|
||||||
|
| 'valid'
|
||||||
|
| 'invalid'
|
||||||
|
| 'expiring'
|
||||||
|
| 'already_used'
|
||||||
|
| 'check_error'
|
||||||
|
data: any
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
type Segment = {
|
type Segment = {
|
||||||
id: string
|
id: string
|
||||||
name: string
|
name: string
|
||||||
|
Reference in New Issue
Block a user