custom fields (create and update)

This commit is contained in:
Jonathan Labreuille
2015-11-05 19:43:02 +01:00
parent b12f7f29de
commit 1ce4b16327
4 changed files with 185 additions and 230 deletions

View File

@ -29,21 +29,33 @@ class CustomFields {
wp_send_json($result);
}
function save($data = array()) {
$custom_field = CustomField::createOrUpdate($data);
if($custom_field === false) {
$result = array(
'result' => false,
'errors' => array(
__('The custom field could not be created.')
)
);
} else {
$errors = $custom_field->getValidationErrors();
if(!empty($errors)) {
$result = array(
'result' => false,
'errors' => $errors
);
} else {
$result = array(
'result' => true,
'field' => $custom_field->asArray()
);
}
}
wp_send_json($result);
}
function get($id) {
$custom_field = CustomField::findOne($id);
@ -54,111 +66,4 @@ class CustomFields {
wp_send_json($custom_field);
}
}
function create() {
// create new form
$custom_field_data = array(
'name' => __('New form'),
'body' => array(
array(
'name' => __('Email'),
'type' => 'input',
'field' => 'email',
'static' => true,
'params' => array(
'label' => __('Email'),
'required' => true
)
),
array(
'name' => __('Submit'),
'type' => 'submit',
'field' => 'submit',
'static' => true,
'params' => array(
'label' => __('Subscribe!')
)
)
),
'settings' => array(
'on_success' => 'message',
'success_message' => __('Check your inbox or spam folder now to confirm your subscription.'),
'segments' => null,
'segments_selected_by' => 'admin'
)
);
$custom_field = CustomField::createOrUpdate($custom_field_data);
if($custom_field !== false && $custom_field->id()) {
wp_send_json(
admin_url('admin.php?page=mailpoet-form-editor&id='.$custom_field->id())
);
} else {
wp_send_json(false);
}
}
function save($data = array()) {
$custom_field = CustomField::createOrUpdate($data);
if($custom_field !== false && $custom_field->id()) {
wp_send_json($custom_field->id());
} else {
wp_send_json($custom_field);
}
}
function restore($id) {
$result = false;
$custom_field = CustomField::findOne($id);
if($custom_field !== false) {
$result = $custom_field->restore();
}
wp_send_json($result);
}
function trash($id) {
$result = false;
$custom_field = CustomField::findOne($id);
if($custom_field !== false) {
$result = $custom_field->trash();
}
wp_send_json($result);
}
}
}