updated Custom Field endpoint + Unit tests + form editor update
This commit is contained in:
@ -1,34 +1,32 @@
|
||||
<?php
|
||||
namespace MailPoet\API\Endpoints;
|
||||
|
||||
use \MailPoet\API\Endpoint as APIEndpoint;
|
||||
use \MailPoet\API\Error as APIError;
|
||||
use \MailPoet\Models\CustomField;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
class CustomFields {
|
||||
function __construct() {
|
||||
}
|
||||
|
||||
class CustomFields extends APIEndpoint {
|
||||
function getAll() {
|
||||
$collection = CustomField::findMany();
|
||||
$custom_fields = array_map(function($custom_field) {
|
||||
return $custom_field->asArray();
|
||||
}, $collection);
|
||||
|
||||
return $custom_fields;
|
||||
return $this->successResponse($custom_fields);
|
||||
}
|
||||
|
||||
function delete($id) {
|
||||
function delete($data = array()) {
|
||||
$id = (isset($data['id']) ? (int)$data['id'] : null);
|
||||
$custom_field = CustomField::findOne($id);
|
||||
if($custom_field === false or !$custom_field->id()) {
|
||||
return array('result' => false);
|
||||
if($custom_field === false) {
|
||||
return $this->errorResponse(array(
|
||||
APIError::NOT_FOUND => __('This custom field does not exist.')
|
||||
));
|
||||
} else {
|
||||
$custom_field->delete();
|
||||
|
||||
return array(
|
||||
'result' => true,
|
||||
'field' => $custom_field->asArray()
|
||||
);
|
||||
return $this->successResponse($custom_field->asArray());
|
||||
}
|
||||
}
|
||||
|
||||
@ -37,24 +35,23 @@ class CustomFields {
|
||||
$errors = $custom_field->getErrors();
|
||||
|
||||
if(!empty($errors)) {
|
||||
return array(
|
||||
'result' => false,
|
||||
'errors' => $errors
|
||||
);
|
||||
return $this->badRequest($errors);
|
||||
} else {
|
||||
return array(
|
||||
'result' => true,
|
||||
'field' => $custom_field->asArray()
|
||||
return $this->successResponse(
|
||||
CustomField::findOne($custom_field->id)->asArray()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function get($id) {
|
||||
function get($data = array()) {
|
||||
$id = (isset($data['id']) ? (int)$data['id'] : null);
|
||||
$custom_field = CustomField::findOne($id);
|
||||
if($custom_field === false) {
|
||||
return false;
|
||||
return $this->errorResponse(array(
|
||||
APIError::NOT_FOUND => __('This custom field does not exist.')
|
||||
));
|
||||
} else {
|
||||
return $custom_field->asArray();
|
||||
return $this->successResponse($custom_field->asArray());
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user