Use short array syntax

[MAILPOET-2090]
This commit is contained in:
Pavel Dohnal
2019-05-20 13:48:48 +02:00
committed by M. Shull
parent 9f16cb6a5f
commit 5da7110eb6
385 changed files with 40133 additions and 40131 deletions

View File

@@ -16,11 +16,11 @@ class API {
private $_request_method;
private $_request_token;
private $_request_endpoint_class;
private $_request_data = array();
private $_endpoint_namespaces = array();
private $_available_api_versions = array(
'v1'
);
private $_request_data = [];
private $_endpoint_namespaces = [];
private $_available_api_versions = [
'v1',
];
/** @var ContainerInterface */
private $container;
@@ -58,24 +58,24 @@ class API {
// admin security token and API version
WPFunctions::get()->addAction(
'admin_head',
array($this, 'setTokenAndAPIVersion')
[$this, 'setTokenAndAPIVersion']
);
// ajax (logged in users)
WPFunctions::get()->addAction(
'wp_ajax_mailpoet',
array($this, 'setupAjax')
[$this, 'setupAjax']
);
// ajax (logged out users)
WPFunctions::get()->addAction(
'wp_ajax_nopriv_mailpoet',
array($this, 'setupAjax')
[$this, 'setupAjax']
);
}
function setupAjax() {
$this->wp->doAction('mailpoet_api_setup', array($this));
$this->wp->doAction('mailpoet_api_setup', [$this]);
$this->setRequestData($_POST);
$ignoreToken = (
@@ -129,19 +129,19 @@ class API {
}
$this->_request_data = isset($data['data'])
? WPFunctions::get()->stripslashesDeep($data['data'])
: array();
: [];
// remove reserved keywords from data
if (is_array($this->_request_data) && !empty($this->_request_data)) {
// filter out reserved keywords from data
$reserved_keywords = array(
$reserved_keywords = [
'token',
'endpoint',
'method',
'api_version',
'mailpoet_method', // alias of 'method'
'mailpoet_redirect'
);
'mailpoet_redirect',
];
$this->_request_data = array_diff_key(
$this->_request_data,
array_flip($reserved_keywords)
@@ -221,10 +221,10 @@ class API {
function createErrorResponse($error_type, $error_message, $response_status) {
$error_response = new ErrorResponse(
array(
$error_type => $error_message
),
array(),
[
$error_type => $error_message,
],
[],
$response_status
);
return $error_response;

View File

@@ -8,33 +8,33 @@ use MailPoet\WP\Functions as WPFunctions;
if (!defined('ABSPATH')) exit;
abstract class Endpoint {
public $permissions = array(
public $permissions = [
'global' => AccessControl::PERMISSION_MANAGE_SETTINGS,
'methods' => array()
);
'methods' => [],
];
function successResponse(
$data = array(), $meta = array(), $status = Response::STATUS_OK
$data = [], $meta = [], $status = Response::STATUS_OK
) {
return new SuccessResponse($data, $meta, $status);
}
function errorResponse(
$errors = array(), $meta = array(), $status = Response::STATUS_NOT_FOUND
$errors = [], $meta = [], $status = Response::STATUS_NOT_FOUND
) {
if (empty($errors)) {
$errors = array(
Error::UNKNOWN => WPFunctions::get()->__('An unknown error occurred.', 'mailpoet')
);
$errors = [
Error::UNKNOWN => WPFunctions::get()->__('An unknown error occurred.', 'mailpoet'),
];
}
return new ErrorResponse($errors, $meta, $status);
}
function badRequest($errors = array(), $meta = array()) {
function badRequest($errors = [], $meta = []) {
if (empty($errors)) {
$errors = array(
Error::BAD_REQUEST => WPFunctions::get()->__('Invalid request parameters', 'mailpoet')
);
$errors = [
Error::BAD_REQUEST => WPFunctions::get()->__('Invalid request parameters', 'mailpoet'),
];
}
return new ErrorResponse($errors, $meta, Response::STATUS_BAD_REQUEST);
}

View File

@@ -6,25 +6,25 @@ use MailPoet\WP\Functions as WPFunctions;
class ErrorResponse extends Response {
public $errors;
function __construct($errors = array(), $meta = array(), $status = self::STATUS_NOT_FOUND) {
function __construct($errors = [], $meta = [], $status = self::STATUS_NOT_FOUND) {
parent::__construct($status, $meta);
$this->errors = $this->formatErrors($errors);
}
function getData() {
return (empty($this->errors)) ? null : array('errors' => $this->errors);
return (empty($this->errors)) ? null : ['errors' => $this->errors];
}
function formatErrors($errors = array()) {
function formatErrors($errors = []) {
return array_map(function($error, $message) {
// sanitize SQL error
if (preg_match('/^SQLSTATE/i', $message)) {
$message = WPFunctions::get()->__('An unknown error occurred.', 'mailpoet');
}
return array(
return [
'error' => $error,
'message' => $message
);
'message' => $message,
];
}, array_keys($errors), array_values($errors));
}
}

View File

@@ -14,7 +14,7 @@ abstract class Response {
public $status;
public $meta;
function __construct($status, $meta = array()) {
function __construct($status, $meta = []) {
$this->status = $status;
$this->meta = $meta;
}
@@ -23,7 +23,7 @@ abstract class Response {
WPFunctions::get()->statusHeader($this->status);
$data = $this->getData();
$response = array();
$response = [];
if (!empty($this->meta)) {
$response['meta'] = $this->meta;

View File

@@ -6,7 +6,7 @@ if (!defined('ABSPATH')) exit;
class SuccessResponse extends Response {
public $data;
function __construct($data = array(), $meta = array(), $status = self::STATUS_OK) {
function __construct($data = [], $meta = [], $status = self::STATUS_OK) {
parent::__construct($status, $meta);
$this->data = $data;
}
@@ -14,8 +14,8 @@ class SuccessResponse extends Response {
function getData() {
if ($this->data === null) return null;
return array(
'data' => $this->data
);
return [
'data' => $this->data,
];
}
}

View File

@@ -13,9 +13,9 @@ class Analytics extends APIEndpoint {
/** @var Reporter */
private $reporter;
public $permissions = array(
'global' => AccessControl::NO_ACCESS_RESTRICTION
);
public $permissions = [
'global' => AccessControl::NO_ACCESS_RESTRICTION,
];
function __construct(Reporter $reporter) {
$this->reporter = $reporter;

View File

@@ -13,9 +13,9 @@ class AutomatedLatestContent extends APIEndpoint {
/** @var \MailPoet\Newsletter\AutomatedLatestContent */
public $ALC;
private $wp;
public $permissions = array(
'global' => AccessControl::PERMISSION_MANAGE_EMAILS
);
public $permissions = [
'global' => AccessControl::PERMISSION_MANAGE_EMAILS,
];
function __construct(\MailPoet\Newsletter\AutomatedLatestContent $alc, WPFunctions $wp) {
$this->ALC = $alc;
@@ -24,17 +24,17 @@ class AutomatedLatestContent extends APIEndpoint {
function getPostTypes() {
$post_types = array_map(function($post_type) {
return array(
return [
'name' => $post_type->name,
'label' => $post_type->label
);
}, WPPosts::getTypes(array(), 'objects'));
'label' => $post_type->label,
];
}, WPPosts::getTypes([], 'objects'));
return $this->successResponse(
array_filter($post_types)
);
}
function getTaxonomies($data = array()) {
function getTaxonomies($data = []) {
$post_type = (isset($data['postType'])) ? $data['postType'] : 'post';
$all_taxonomies = WPFunctions::get()->getObjectTaxonomies($post_type, 'objects');
$taxonomies_with_label = array_filter($all_taxonomies, function($taxonomy) {
@@ -43,20 +43,20 @@ class AutomatedLatestContent extends APIEndpoint {
return $this->successResponse($taxonomies_with_label);
}
function getTerms($data = array()) {
$taxonomies = (isset($data['taxonomies'])) ? $data['taxonomies'] : array();
function getTerms($data = []) {
$taxonomies = (isset($data['taxonomies'])) ? $data['taxonomies'] : [];
$search = (isset($data['search'])) ? $data['search'] : '';
$limit = (isset($data['limit'])) ? (int)$data['limit'] : 100;
$page = (isset($data['page'])) ? (int)$data['page'] : 1;
$args = array(
$args = [
'taxonomy' => $taxonomies,
'hide_empty' => false,
'search' => $search,
'number' => $limit,
'offset' => $limit * ($page - 1),
'orderby' => 'name',
'order' => 'ASC'
);
'order' => 'ASC',
];
$args = $this->wp->applyFilters('mailpoet_search_terms_args', $args);
$terms = WPPosts::getTerms($args);
@@ -64,22 +64,22 @@ class AutomatedLatestContent extends APIEndpoint {
return $this->successResponse(array_values($terms));
}
function getPosts($data = array()) {
function getPosts($data = []) {
return $this->successResponse(
$this->ALC->getPosts($data)
);
}
function getTransformedPosts($data = array()) {
function getTransformedPosts($data = []) {
$posts = $this->ALC->getPosts($data);
return $this->successResponse(
$this->ALC->transformPosts($data, $posts)
);
}
function getBulkTransformedPosts($data = array()) {
$used_posts = array();
$rendered_posts = array();
function getBulkTransformedPosts($data = []) {
$used_posts = [];
$rendered_posts = [];
foreach ($data['blocks'] as $block) {
$posts = $this->ALC->getPosts($block, $used_posts);

View File

@@ -11,9 +11,9 @@ use MailPoet\WP\Functions as WPFunctions;
if (!defined('ABSPATH')) exit;
class CustomFields extends APIEndpoint {
public $permissions = array(
'global' => AccessControl::PERMISSION_MANAGE_FORMS
);
public $permissions = [
'global' => AccessControl::PERMISSION_MANAGE_FORMS,
];
function getAll() {
$collection = CustomField::orderByAsc('created_at')->findMany();
@@ -24,7 +24,7 @@ class CustomFields extends APIEndpoint {
return $this->successResponse($custom_fields);
}
function delete($data = array()) {
function delete($data = []) {
$id = (isset($data['id']) ? (int)$data['id'] : null);
$custom_field = CustomField::findOne($id);
if ($custom_field instanceof CustomField) {
@@ -32,13 +32,13 @@ class CustomFields extends APIEndpoint {
return $this->successResponse($custom_field->asArray());
} else {
return $this->errorResponse(array(
APIError::NOT_FOUND => WPFunctions::get()->__('This custom field does not exist.', 'mailpoet')
));
return $this->errorResponse([
APIError::NOT_FOUND => WPFunctions::get()->__('This custom field does not exist.', 'mailpoet'),
]);
}
}
function save($data = array()) {
function save($data = []) {
$custom_field = CustomField::createOrUpdate($data);
$errors = $custom_field->getErrors();
@@ -50,14 +50,14 @@ class CustomFields extends APIEndpoint {
return $this->successResponse($custom_field->asArray());
}
function get($data = array()) {
function get($data = []) {
$id = (isset($data['id']) ? (int)$data['id'] : null);
$custom_field = CustomField::findOne($id);
if ($custom_field instanceof CustomField) {
return $this->successResponse($custom_field->asArray());
}
return $this->errorResponse(array(
APIError::NOT_FOUND => WPFunctions::get()->__('This custom field does not exist.', 'mailpoet')
));
return $this->errorResponse([
APIError::NOT_FOUND => WPFunctions::get()->__('This custom field does not exist.', 'mailpoet'),
]);
}
}

View File

@@ -22,9 +22,9 @@ class Forms extends APIEndpoint {
/** @var Listing\Handler */
private $listing_handler;
public $permissions = array(
'global' => AccessControl::PERMISSION_MANAGE_FORMS
);
public $permissions = [
'global' => AccessControl::PERMISSION_MANAGE_FORMS,
];
function __construct(
Listing\BulkActionController $bulk_action,
@@ -34,21 +34,21 @@ class Forms extends APIEndpoint {
$this->listing_handler = $listing_handler;
}
function get($data = array()) {
function get($data = []) {
$id = (isset($data['id']) ? (int)$data['id'] : false);
$form = Form::findOne($id);
if ($form instanceof Form) {
return $this->successResponse($form->asArray());
}
return $this->errorResponse(array(
APIError::NOT_FOUND => WPFunctions::get()->__('This form does not exist.', 'mailpoet')
));
return $this->errorResponse([
APIError::NOT_FOUND => WPFunctions::get()->__('This form does not exist.', 'mailpoet'),
]);
}
function listing($data = array()) {
function listing($data = []) {
$listing_data = $this->listing_handler->get('\MailPoet\Models\Form', $data);
$data = array();
$data = [];
foreach ($listing_data['items'] as $form) {
$form = $form->asArray();
@@ -57,56 +57,56 @@ class Forms extends APIEndpoint {
$form['segments'] = (
!empty($form['settings']['segments'])
? $form['settings']['segments']
: array()
: []
);
$data[] = $form;
}
return $this->successResponse($data, array(
return $this->successResponse($data, [
'count' => $listing_data['count'],
'filters' => $listing_data['filters'],
'groups' => $listing_data['groups']
));
'groups' => $listing_data['groups'],
]);
}
function create() {
// create new form
$form_data = array(
$form_data = [
'name' => WPFunctions::get()->__('New form', 'mailpoet'),
'body' => array(
array(
'body' => [
[
'id' => 'email',
'name' => WPFunctions::get()->__('Email', 'mailpoet'),
'type' => 'text',
'static' => true,
'params' => array(
'params' => [
'label' => WPFunctions::get()->__('Email', 'mailpoet'),
'required' => true
)
),
array(
'required' => true,
],
],
[
'id' => 'submit',
'name' => WPFunctions::get()->__('Submit', 'mailpoet'),
'type' => 'submit',
'static' => true,
'params' => array(
'label' => WPFunctions::get()->__('Subscribe!', 'mailpoet')
)
)
),
'settings' => array(
'params' => [
'label' => WPFunctions::get()->__('Subscribe!', 'mailpoet'),
],
],
],
'settings' => [
'on_success' => 'message',
'success_message' => Form::getDefaultSuccessMessage(),
'segments' => null,
'segments_selected_by' => 'admin'
)
);
'segments_selected_by' => 'admin',
],
];
return $this->save($form_data);
}
function save($data = array()) {
function save($data = []) {
$form = Form::createOrUpdate($data);
$errors = $form->getErrors();
@@ -118,7 +118,7 @@ class Forms extends APIEndpoint {
return $this->badRequest($errors);
}
function previewEditor($data = array()) {
function previewEditor($data = []) {
// html
$html = FormRenderer::renderHTML($data);
@@ -128,29 +128,29 @@ class Forms extends APIEndpoint {
// styles
$css = new Util\Styles(FormRenderer::getStyles($data));
return $this->successResponse(array(
return $this->successResponse([
'html' => $html,
'css' => $css->render()
));
'css' => $css->render(),
]);
}
function exportsEditor($data = array()) {
function exportsEditor($data = []) {
$id = (isset($data['id']) ? (int)$data['id'] : false);
$form = Form::findOne($id);
if ($form instanceof Form) {
$exports = Util\Export::getAll($form->asArray());
return $this->successResponse($exports);
}
return $this->errorResponse(array(
APIError::NOT_FOUND => WPFunctions::get()->__('This form does not exist.', 'mailpoet')
));
return $this->errorResponse([
APIError::NOT_FOUND => WPFunctions::get()->__('This form does not exist.', 'mailpoet'),
]);
}
function saveEditor($data = array()) {
function saveEditor($data = []) {
$form_id = (isset($data['id']) ? (int)$data['id'] : 0);
$name = (isset($data['name']) ? $data['name'] : WPFunctions::get()->__('New form', 'mailpoet'));
$body = (isset($data['body']) ? $data['body'] : array());
$settings = (isset($data['settings']) ? $data['settings'] : array());
$body = (isset($data['body']) ? $data['body'] : []);
$settings = (isset($data['settings']) ? $data['settings'] : []);
$styles = (isset($data['styles']) ? $data['styles'] : '');
// check if the form is used as a widget
@@ -194,13 +194,13 @@ class Forms extends APIEndpoint {
$settings['segments_selected_by'] = 'admin';
}
$form = Form::createOrUpdate(array(
$form = Form::createOrUpdate([
'id' => $form_id,
'name' => $name,
'body' => $body,
'settings' => $settings,
'styles' => $styles
));
'styles' => $styles,
]);
$errors = $form->getErrors();
@@ -211,11 +211,11 @@ class Forms extends APIEndpoint {
if(!$form instanceof Form) return $this->errorResponse();
return $this->successResponse(
$form->asArray(),
array('is_widget' => $is_widget)
['is_widget' => $is_widget]
);
}
function restore($data = array()) {
function restore($data = []) {
$id = (isset($data['id']) ? (int)$data['id'] : false);
$form = Form::findOne($id);
if ($form instanceof Form) {
@@ -224,16 +224,16 @@ class Forms extends APIEndpoint {
if(!$form instanceof Form) return $this->errorResponse();
return $this->successResponse(
$form->asArray(),
array('count' => 1)
['count' => 1]
);
} else {
return $this->errorResponse(array(
APIError::NOT_FOUND => WPFunctions::get()->__('This form does not exist.', 'mailpoet')
));
return $this->errorResponse([
APIError::NOT_FOUND => WPFunctions::get()->__('This form does not exist.', 'mailpoet'),
]);
}
}
function trash($data = array()) {
function trash($data = []) {
$id = (isset($data['id']) ? (int)$data['id'] : false);
$form = Form::findOne($id);
if ($form instanceof Form) {
@@ -242,37 +242,37 @@ class Forms extends APIEndpoint {
if(!$form instanceof Form) return $this->errorResponse();
return $this->successResponse(
$form->asArray(),
array('count' => 1)
['count' => 1]
);
} else {
return $this->errorResponse(array(
APIError::NOT_FOUND => WPFunctions::get()->__('This form does not exist.', 'mailpoet')
));
return $this->errorResponse([
APIError::NOT_FOUND => WPFunctions::get()->__('This form does not exist.', 'mailpoet'),
]);
}
}
function delete($data = array()) {
function delete($data = []) {
$id = (isset($data['id']) ? (int)$data['id'] : false);
$form = Form::findOne($id);
if ($form instanceof Form) {
$form->delete();
return $this->successResponse(null, array('count' => 1));
return $this->successResponse(null, ['count' => 1]);
} else {
return $this->errorResponse(array(
APIError::NOT_FOUND => WPFunctions::get()->__('This form does not exist.', 'mailpoet')
));
return $this->errorResponse([
APIError::NOT_FOUND => WPFunctions::get()->__('This form does not exist.', 'mailpoet'),
]);
}
}
function duplicate($data = array()) {
function duplicate($data = []) {
$id = (isset($data['id']) ? (int)$data['id'] : false);
$form = Form::findOne($id);
if ($form instanceof Form) {
$data = array(
'name' => sprintf(__('Copy of %s', 'mailpoet'), $form->name)
);
$data = [
'name' => sprintf(__('Copy of %s', 'mailpoet'), $form->name),
];
$duplicate = $form->duplicate($data);
$errors = $duplicate->getErrors();
@@ -283,24 +283,24 @@ class Forms extends APIEndpoint {
if(!$duplicate instanceof Form) return $this->errorResponse();
return $this->successResponse(
$duplicate->asArray(),
array('count' => 1)
['count' => 1]
);
}
} else {
return $this->errorResponse(array(
APIError::NOT_FOUND => WPFunctions::get()->__('This form does not exist.', 'mailpoet')
));
return $this->errorResponse([
APIError::NOT_FOUND => WPFunctions::get()->__('This form does not exist.', 'mailpoet'),
]);
}
}
function bulkAction($data = array()) {
function bulkAction($data = []) {
try {
$meta = $this->bulk_action->apply('\MailPoet\Models\Form', $data);
return $this->successResponse(null, $meta);
} catch (\Exception $e) {
return $this->errorResponse(array(
$e->getCode() => $e->getMessage()
));
return $this->errorResponse([
$e->getCode() => $e->getMessage(),
]);
}
}
}

View File

@@ -13,9 +13,9 @@ use MailPoet\Subscribers\ImportExport\Import\MailChimp;
if (!defined('ABSPATH')) exit;
class ImportExport extends APIEndpoint {
public $permissions = array(
'global' => AccessControl::PERMISSION_MANAGE_SUBSCRIBERS
);
public $permissions = [
'global' => AccessControl::PERMISSION_MANAGE_SUBSCRIBERS,
];
function getMailChimpLists($data) {
try {
@@ -23,9 +23,9 @@ class ImportExport extends APIEndpoint {
$lists = $mailChimp->getLists();
return $this->successResponse($lists);
} catch (\Exception $e) {
return $this->errorResponse(array(
$e->getCode() => $e->getMessage()
));
return $this->errorResponse([
$e->getCode() => $e->getMessage(),
]);
}
}
@@ -35,9 +35,9 @@ class ImportExport extends APIEndpoint {
$subscribers = $mailChimp->getSubscribers($data['lists']);
return $this->successResponse($subscribers);
} catch (\Exception $e) {
return $this->errorResponse(array(
$e->getCode() => $e->getMessage()
));
return $this->errorResponse([
$e->getCode() => $e->getMessage(),
]);
}
}
@@ -62,9 +62,9 @@ class ImportExport extends APIEndpoint {
$process = $import->process();
return $this->successResponse($process);
} catch (\Exception $e) {
return $this->errorResponse(array(
$e->getCode() => $e->getMessage()
));
return $this->errorResponse([
$e->getCode() => $e->getMessage(),
]);
}
}
@@ -76,9 +76,9 @@ class ImportExport extends APIEndpoint {
$process = $export->process();
return $this->successResponse($process);
} catch (\Exception $e) {
return $this->errorResponse(array(
$e->getCode() => $e->getMessage()
));
return $this->errorResponse([
$e->getCode() => $e->getMessage(),
]);
}
}

View File

@@ -8,9 +8,9 @@ use MailPoet\Config\AccessControl;
if (!defined('ABSPATH')) exit;
class MP2Migrator extends APIEndpoint {
public $permissions = array(
'global' => AccessControl::PERMISSION_MANAGE_SETTINGS
);
public $permissions = [
'global' => AccessControl::PERMISSION_MANAGE_SETTINGS,
];
private $MP2Migrator;
public function __construct() {
@@ -28,9 +28,9 @@ class MP2Migrator extends APIEndpoint {
$process = $this->MP2Migrator->import($data);
return $this->successResponse($process);
} catch (\Exception $e) {
return $this->errorResponse(array(
$e->getCode() => $e->getMessage()
));
return $this->errorResponse([
$e->getCode() => $e->getMessage(),
]);
}
}
@@ -45,9 +45,9 @@ class MP2Migrator extends APIEndpoint {
$process = $this->MP2Migrator->stopImport();
return $this->successResponse($process);
} catch (\Exception $e) {
return $this->errorResponse(array(
$e->getCode() => $e->getMessage()
));
return $this->errorResponse([
$e->getCode() => $e->getMessage(),
]);
}
}
@@ -62,9 +62,9 @@ class MP2Migrator extends APIEndpoint {
$process = $this->MP2Migrator->skipImport();
return $this->successResponse($process);
} catch (\Exception $e) {
return $this->errorResponse(array(
$e->getCode() => $e->getMessage()
));
return $this->errorResponse([
$e->getCode() => $e->getMessage(),
]);
}
}

View File

@@ -11,11 +11,11 @@ use MailPoet\WP\Functions as WPFunctions;
if (!defined('ABSPATH')) exit;
class Mailer extends APIEndpoint {
public $permissions = array(
'global' => AccessControl::PERMISSION_MANAGE_EMAILS
);
public $permissions = [
'global' => AccessControl::PERMISSION_MANAGE_EMAILS,
];
function send($data = array()) {
function send($data = []) {
try {
$mailer = new \MailPoet\Mailer\Mailer(
(isset($data['mailer'])) ? $data['mailer'] : false,
@@ -24,9 +24,9 @@ class Mailer extends APIEndpoint {
);
$result = $mailer->send($data['newsletter'], $data['subscriber']);
} catch (\Exception $e) {
return $this->errorResponse(array(
$e->getCode() => $e->getMessage()
));
return $this->errorResponse([
$e->getCode() => $e->getMessage(),
]);
}
if ($result['response'] === false) {
@@ -34,7 +34,7 @@ class Mailer extends APIEndpoint {
WPFunctions::get()->__('The email could not be sent: %s', 'mailpoet'),
$result['error']->getMessage()
);
return $this->errorResponse(array(APIError::BAD_REQUEST => $error));
return $this->errorResponse([APIError::BAD_REQUEST => $error]);
} else {
return $this->successResponse(null);
}

View File

@@ -11,11 +11,11 @@ use MailPoet\WP\Functions as WPFunctions;
if (!defined('ABSPATH')) exit;
class NewsletterTemplates extends APIEndpoint {
public $permissions = array(
'global' => AccessControl::PERMISSION_MANAGE_EMAILS
);
public $permissions = [
'global' => AccessControl::PERMISSION_MANAGE_EMAILS,
];
function get($data = array()) {
function get($data = []) {
$id = (isset($data['id']) ? (int)$data['id'] : false);
$template = NewsletterTemplate::findOne($id);
if ($template instanceof NewsletterTemplate) {
@@ -23,9 +23,9 @@ class NewsletterTemplates extends APIEndpoint {
$template->asArray()
);
} else {
return $this->errorResponse(array(
APIError::NOT_FOUND => WPFunctions::get()->__('This template does not exist.', 'mailpoet')
));
return $this->errorResponse([
APIError::NOT_FOUND => WPFunctions::get()->__('This template does not exist.', 'mailpoet'),
]);
}
}
@@ -43,7 +43,7 @@ class NewsletterTemplates extends APIEndpoint {
return $this->successResponse($templates);
}
function save($data = array()) {
function save($data = []) {
ignore_user_abort(true);
if (!empty($data['newsletter_id'])) {
$template = NewsletterTemplate::whereEqual('newsletter_id', $data['newsletter_id'])->findOne();
@@ -68,16 +68,16 @@ class NewsletterTemplates extends APIEndpoint {
}
}
function delete($data = array()) {
function delete($data = []) {
$id = (isset($data['id']) ? (int)$data['id'] : false);
$template = NewsletterTemplate::findOne($id);
if ($template instanceof NewsletterTemplate) {
$template->delete();
return $this->successResponse(null, array('count' => 1));
return $this->successResponse(null, ['count' => 1]);
} else {
return $this->errorResponse(array(
APIError::NOT_FOUND => WPFunctions::get()->__('This template does not exist.', 'mailpoet')
));
return $this->errorResponse([
APIError::NOT_FOUND => WPFunctions::get()->__('This template does not exist.', 'mailpoet'),
]);
}
}
}

View File

@@ -47,9 +47,9 @@ class Newsletters extends APIEndpoint {
/** @var FeaturesController */
private $features_controller;
public $permissions = array(
'global' => AccessControl::PERMISSION_MANAGE_EMAILS
);
public $permissions = [
'global' => AccessControl::PERMISSION_MANAGE_EMAILS,
];
function __construct(
Listing\BulkActionController $bulk_action,
@@ -67,7 +67,7 @@ class Newsletters extends APIEndpoint {
$this->features_controller = $features_controller;
}
function get($data = array()) {
function get($data = []) {
$id = (isset($data['id']) ? (int)$data['id'] : false);
$newsletter = Newsletter::findOne($id);
if ($newsletter instanceof Newsletter) {
@@ -85,22 +85,22 @@ class Newsletters extends APIEndpoint {
$newsletter = $this->wp->applyFilters('mailpoet_api_newsletters_get_after', $newsletter->asArray());
return $this->successResponse($newsletter, ['preview_url' => $preview_url]);
} else {
return $this->errorResponse(array(
APIError::NOT_FOUND => WPFunctions::get()->__('This newsletter does not exist.', 'mailpoet')
));
return $this->errorResponse([
APIError::NOT_FOUND => WPFunctions::get()->__('This newsletter does not exist.', 'mailpoet'),
]);
}
}
function save($data = array()) {
function save($data = []) {
$data = $this->wp->applyFilters('mailpoet_api_newsletters_save_before', $data);
$segments = array();
$segments = [];
if (isset($data['segments'])) {
$segments = $data['segments'];
unset($data['segments']);
}
$options = array();
$options = [];
if (isset($data['options'])) {
$options = $data['options'];
unset($data['options']);
@@ -149,11 +149,11 @@ class Newsletters extends APIEndpoint {
foreach ($option_fields as $option_field) {
if (isset($options[$option_field->name])) {
$newsletter_option = NewsletterOption::createOrUpdate(
array(
[
'newsletter_id' => $newsletter->id,
'option_field_id' => $option_field->id,
'value' => $options[$option_field->name]
)
'value' => $options[$option_field->name],
]
);
}
}
@@ -175,7 +175,7 @@ class Newsletters extends APIEndpoint {
}
$queue = $newsletter->getQueue();
if ($queue && !in_array($newsletter->type, array(Newsletter::TYPE_NOTIFICATION, Newsletter::TYPE_NOTIFICATION_HISTORY))) {
if ($queue && !in_array($newsletter->type, [Newsletter::TYPE_NOTIFICATION, Newsletter::TYPE_NOTIFICATION_HISTORY])) {
// if newsletter was previously scheduled and is now unscheduled, set its status to DRAFT and delete associated queue record
if ($newsletter->status === Newsletter::STATUS_SCHEDULED && isset($options['isScheduled']) && empty($options['isScheduled'])) {
$queue->delete();
@@ -197,25 +197,25 @@ class Newsletters extends APIEndpoint {
Subscriber::getCurrentWPUser()
);
return $this->successResponse($newsletter->asArray(), array('preview_url' => $preview_url));
return $this->successResponse($newsletter->asArray(), ['preview_url' => $preview_url]);
}
function setStatus($data = array()) {
function setStatus($data = []) {
$status = (isset($data['status']) ? $data['status'] : null);
if (!$status) {
return $this->badRequest(array(
APIError::BAD_REQUEST => WPFunctions::get()->__('You need to specify a status.', 'mailpoet')
));
return $this->badRequest([
APIError::BAD_REQUEST => WPFunctions::get()->__('You need to specify a status.', 'mailpoet'),
]);
}
$id = (isset($data['id'])) ? (int)$data['id'] : false;
$newsletter = Newsletter::findOneWithOptions($id);
if ($newsletter === false) {
return $this->errorResponse(array(
APIError::NOT_FOUND => WPFunctions::get()->__('This newsletter does not exist.', 'mailpoet')
));
return $this->errorResponse([
APIError::NOT_FOUND => WPFunctions::get()->__('This newsletter does not exist.', 'mailpoet'),
]);
}
$newsletter->setStatus($status);
@@ -247,7 +247,7 @@ class Newsletters extends APIEndpoint {
);
}
function restore($data = array()) {
function restore($data = []) {
$id = (isset($data['id']) ? (int)$data['id'] : false);
$newsletter = Newsletter::findOne($id);
if ($newsletter instanceof Newsletter) {
@@ -258,16 +258,16 @@ class Newsletters extends APIEndpoint {
return $this->successResponse(
$newsletter->asArray(),
array('count' => 1)
['count' => 1]
);
} else {
return $this->errorResponse(array(
APIError::NOT_FOUND => WPFunctions::get()->__('This newsletter does not exist.', 'mailpoet')
));
return $this->errorResponse([
APIError::NOT_FOUND => WPFunctions::get()->__('This newsletter does not exist.', 'mailpoet'),
]);
}
}
function trash($data = array()) {
function trash($data = []) {
$id = (isset($data['id']) ? (int)$data['id'] : false);
$newsletter = Newsletter::findOne($id);
if ($newsletter instanceof Newsletter) {
@@ -277,36 +277,36 @@ class Newsletters extends APIEndpoint {
if(!$newsletter instanceof Newsletter) return $this->errorResponse();
return $this->successResponse(
$newsletter->asArray(),
array('count' => 1)
['count' => 1]
);
} else {
return $this->errorResponse(array(
APIError::NOT_FOUND => WPFunctions::get()->__('This newsletter does not exist.', 'mailpoet')
));
return $this->errorResponse([
APIError::NOT_FOUND => WPFunctions::get()->__('This newsletter does not exist.', 'mailpoet'),
]);
}
}
function delete($data = array()) {
function delete($data = []) {
$id = (isset($data['id']) ? (int)$data['id'] : false);
$newsletter = Newsletter::findOne($id);
if ($newsletter instanceof Newsletter) {
$newsletter->delete();
return $this->successResponse(null, array('count' => 1));
return $this->successResponse(null, ['count' => 1]);
} else {
return $this->errorResponse(array(
APIError::NOT_FOUND => WPFunctions::get()->__('This newsletter does not exist.', 'mailpoet')
));
return $this->errorResponse([
APIError::NOT_FOUND => WPFunctions::get()->__('This newsletter does not exist.', 'mailpoet'),
]);
}
}
function duplicate($data = array()) {
function duplicate($data = []) {
$id = (isset($data['id']) ? (int)$data['id'] : false);
$newsletter = Newsletter::findOne($id);
if ($newsletter instanceof Newsletter) {
$data = array(
'subject' => sprintf(__('Copy of %s', 'mailpoet'), $newsletter->subject)
);
$data = [
'subject' => sprintf(__('Copy of %s', 'mailpoet'), $newsletter->subject),
];
$duplicate = $newsletter->duplicate($data);
$errors = $duplicate->getErrors();
@@ -318,21 +318,21 @@ class Newsletters extends APIEndpoint {
if(!$duplicate instanceof Newsletter) return $this->errorResponse();
return $this->successResponse(
$duplicate->asArray(),
array('count' => 1)
['count' => 1]
);
}
} else {
return $this->errorResponse(array(
APIError::NOT_FOUND => WPFunctions::get()->__('This newsletter does not exist.', 'mailpoet')
));
return $this->errorResponse([
APIError::NOT_FOUND => WPFunctions::get()->__('This newsletter does not exist.', 'mailpoet'),
]);
}
}
function showPreview($data = array()) {
function showPreview($data = []) {
if (empty($data['body'])) {
return $this->badRequest(array(
APIError::BAD_REQUEST => WPFunctions::get()->__('Newsletter data is missing.', 'mailpoet')
));
return $this->badRequest([
APIError::BAD_REQUEST => WPFunctions::get()->__('Newsletter data is missing.', 'mailpoet'),
]);
}
$id = (isset($data['id'])) ? (int)$data['id'] : false;
@@ -354,20 +354,20 @@ class Newsletters extends APIEndpoint {
if(!$newsletter instanceof Newsletter) return $this->errorResponse();
return $this->successResponse(
$newsletter->asArray(),
array('preview_url' => $preview_url)
['preview_url' => $preview_url]
);
} else {
return $this->errorResponse(array(
APIError::NOT_FOUND => WPFunctions::get()->__('This newsletter does not exist.', 'mailpoet')
));
return $this->errorResponse([
APIError::NOT_FOUND => WPFunctions::get()->__('This newsletter does not exist.', 'mailpoet'),
]);
}
}
function sendPreview($data = array()) {
function sendPreview($data = []) {
if (empty($data['subscriber'])) {
return $this->badRequest(array(
APIError::BAD_REQUEST => WPFunctions::get()->__('Please specify receiver information.', 'mailpoet')
));
return $this->badRequest([
APIError::BAD_REQUEST => WPFunctions::get()->__('Please specify receiver information.', 'mailpoet'),
]);
}
$id = (isset($data['id'])) ? (int)$data['id'] : false;
@@ -378,7 +378,7 @@ class Newsletters extends APIEndpoint {
$rendered_newsletter = $renderer->render();
$divider = '***MailPoet***';
$data_for_shortcodes = array_merge(
array($newsletter->subject),
[$newsletter->subject],
$rendered_newsletter
);
@@ -409,7 +409,7 @@ class Newsletters extends APIEndpoint {
$sender = false,
$reply_to = false
);
$extra_params = array('unsubscribe_url' => WPFunctions::get()->homeUrl());
$extra_params = ['unsubscribe_url' => WPFunctions::get()->homeUrl()];
$result = $mailer->send($rendered_newsletter, $data['subscriber'], $extra_params);
if ($result['response'] === false) {
@@ -417,7 +417,7 @@ class Newsletters extends APIEndpoint {
WPFunctions::get()->__('The email could not be sent: %s', 'mailpoet'),
$result['error']->getMessage()
);
return $this->errorResponse(array(APIError::BAD_REQUEST => $error));
return $this->errorResponse([APIError::BAD_REQUEST => $error]);
} else {
$newsletter = Newsletter::findOne($newsletter->id);
if(!$newsletter instanceof Newsletter) return $this->errorResponse();
@@ -427,21 +427,21 @@ class Newsletters extends APIEndpoint {
);
}
} catch (\Exception $e) {
return $this->errorResponse(array(
$e->getCode() => $e->getMessage()
));
return $this->errorResponse([
$e->getCode() => $e->getMessage(),
]);
}
} else {
return $this->errorResponse(array(
APIError::NOT_FOUND => WPFunctions::get()->__('This newsletter does not exist.', 'mailpoet')
));
return $this->errorResponse([
APIError::NOT_FOUND => WPFunctions::get()->__('This newsletter does not exist.', 'mailpoet'),
]);
}
}
function listing($data = array()) {
function listing($data = []) {
$listing_data = $this->listing_handler->get('\MailPoet\Models\Newsletter', $data);
$data = array();
$data = [];
foreach ($listing_data['items'] as $newsletter) {
$queue = false;
@@ -485,30 +485,30 @@ class Newsletters extends APIEndpoint {
$data[] = $this->wp->applyFilters('mailpoet_api_newsletters_listing_item', $newsletter->asArray());
}
return $this->successResponse($data, array(
return $this->successResponse($data, [
'count' => $listing_data['count'],
'filters' => $listing_data['filters'],
'groups' => $listing_data['groups'],
'mta_log' => $this->settings->get('mta_log'),
'mta_method' => $this->settings->get('mta.method'),
'cron_accessible' => CronHelper::isDaemonAccessible(),
'current_time' => $this->wp->currentTime('mysql')
));
'current_time' => $this->wp->currentTime('mysql'),
]);
}
function bulkAction($data = array()) {
function bulkAction($data = []) {
try {
$meta = $this->bulk_action->apply('\MailPoet\Models\Newsletter', $data);
return $this->successResponse(null, $meta);
} catch (\Exception $e) {
return $this->errorResponse(array(
$e->getCode() => $e->getMessage()
));
return $this->errorResponse([
$e->getCode() => $e->getMessage(),
]);
}
}
function create($data = array()) {
$options = array();
function create($data = []) {
$options = [];
if (isset($data['options'])) {
$options = $data['options'];
unset($data['options']);
@@ -526,7 +526,7 @@ class Newsletters extends APIEndpoint {
if ($template instanceof NewsletterTemplate) {
$newsletter->body = $template->body;
} else {
$newsletter->body = array();
$newsletter->body = [];
}
}

View File

@@ -14,9 +14,9 @@ use MailPoet\WP\Functions as WPFunctions;
if (!defined('ABSPATH')) exit;
class Segments extends APIEndpoint {
public $permissions = array(
'global' => AccessControl::PERMISSION_MANAGE_SEGMENTS
);
public $permissions = [
'global' => AccessControl::PERMISSION_MANAGE_SEGMENTS,
];
/** @var Listing\BulkActionController */
private $bulk_action;
@@ -37,22 +37,22 @@ class Segments extends APIEndpoint {
$this->woo_commerce_sync = $woo_commerce;
}
function get($data = array()) {
function get($data = []) {
$id = (isset($data['id']) ? (int)$data['id'] : false);
$segment = Segment::findOne($id);
if ($segment instanceof Segment) {
return $this->successResponse($segment->asArray());
} else {
return $this->errorResponse(array(
APIError::NOT_FOUND => WPFunctions::get()->__('This list does not exist.', 'mailpoet')
));
return $this->errorResponse([
APIError::NOT_FOUND => WPFunctions::get()->__('This list does not exist.', 'mailpoet'),
]);
}
}
function listing($data = array()) {
function listing($data = []) {
$listing_data = $this->listing_handler->get('\MailPoet\Models\Segment', $data);
$data = array();
$data = [];
foreach ($listing_data['items'] as $segment) {
$segment->subscribers_url = WPFunctions::get()->adminUrl(
'admin.php?page=mailpoet-subscribers#/filter[segment='.$segment->id.']'
@@ -63,14 +63,14 @@ class Segments extends APIEndpoint {
->asArray();
}
return $this->successResponse($data, array(
return $this->successResponse($data, [
'count' => $listing_data['count'],
'filters' => $listing_data['filters'],
'groups' => $listing_data['groups']
));
'groups' => $listing_data['groups'],
]);
}
function save($data = array()) {
function save($data = []) {
$segment = Segment::createOrUpdate($data);
$errors = $segment->getErrors();
@@ -85,7 +85,7 @@ class Segments extends APIEndpoint {
}
}
function restore($data = array()) {
function restore($data = []) {
$id = (isset($data['id']) ? (int)$data['id'] : false);
$segment = Segment::findOne($id);
if ($segment instanceof Segment) {
@@ -94,16 +94,16 @@ class Segments extends APIEndpoint {
if(!$segment instanceof Segment) return $this->errorResponse();
return $this->successResponse(
$segment->asArray(),
array('count' => 1)
['count' => 1]
);
} else {
return $this->errorResponse(array(
APIError::NOT_FOUND => WPFunctions::get()->__('This list does not exist.', 'mailpoet')
));
return $this->errorResponse([
APIError::NOT_FOUND => WPFunctions::get()->__('This list does not exist.', 'mailpoet'),
]);
}
}
function trash($data = array()) {
function trash($data = []) {
$id = (isset($data['id']) ? (int)$data['id'] : false);
$segment = Segment::findOne($id);
if ($segment instanceof Segment) {
@@ -112,36 +112,36 @@ class Segments extends APIEndpoint {
if(!$segment instanceof Segment) return $this->errorResponse();
return $this->successResponse(
$segment->asArray(),
array('count' => 1)
['count' => 1]
);
} else {
return $this->errorResponse(array(
APIError::NOT_FOUND => WPFunctions::get()->__('This list does not exist.', 'mailpoet')
));
return $this->errorResponse([
APIError::NOT_FOUND => WPFunctions::get()->__('This list does not exist.', 'mailpoet'),
]);
}
}
function delete($data = array()) {
function delete($data = []) {
$id = (isset($data['id']) ? (int)$data['id'] : false);
$segment = Segment::findOne($id);
if ($segment instanceof Segment) {
$segment->delete();
return $this->successResponse(null, array('count' => 1));
return $this->successResponse(null, ['count' => 1]);
} else {
return $this->errorResponse(array(
APIError::NOT_FOUND => WPFunctions::get()->__('This list does not exist.', 'mailpoet')
));
return $this->errorResponse([
APIError::NOT_FOUND => WPFunctions::get()->__('This list does not exist.', 'mailpoet'),
]);
}
}
function duplicate($data = array()) {
function duplicate($data = []) {
$id = (isset($data['id']) ? (int)$data['id'] : false);
$segment = Segment::findOne($id);
if ($segment instanceof Segment) {
$data = array(
'name' => sprintf(__('Copy of %s', 'mailpoet'), $segment->name)
);
$data = [
'name' => sprintf(__('Copy of %s', 'mailpoet'), $segment->name),
];
$duplicate = $segment->duplicate($data);
$errors = $duplicate->getErrors();
@@ -152,13 +152,13 @@ class Segments extends APIEndpoint {
if(!$duplicate instanceof Segment) return $this->errorResponse();
return $this->successResponse(
$duplicate->asArray(),
array('count' => 1)
['count' => 1]
);
}
} else {
return $this->errorResponse(array(
APIError::NOT_FOUND => WPFunctions::get()->__('This list does not exist.', 'mailpoet')
));
return $this->errorResponse([
APIError::NOT_FOUND => WPFunctions::get()->__('This list does not exist.', 'mailpoet'),
]);
}
}
@@ -170,22 +170,22 @@ class Segments extends APIEndpoint {
WP::synchronizeUsers();
}
} catch (\Exception $e) {
return $this->errorResponse(array(
$e->getCode() => $e->getMessage()
));
return $this->errorResponse([
$e->getCode() => $e->getMessage(),
]);
}
return $this->successResponse(null);
}
function bulkAction($data = array()) {
function bulkAction($data = []) {
try {
$meta = $this->bulk_action->apply('\MailPoet\Models\Segment', $data);
return $this->successResponse(null, $meta);
} catch (\Exception $e) {
return $this->errorResponse(array(
$e->getCode() => $e->getMessage()
));
return $this->errorResponse([
$e->getCode() => $e->getMessage(),
]);
}
}
}

View File

@@ -15,11 +15,11 @@ use MailPoet\WP\Functions as WPFunctions;
if (!defined('ABSPATH')) exit;
class SendingQueue extends APIEndpoint {
public $permissions = array(
'global' => AccessControl::PERMISSION_MANAGE_EMAILS
);
public $permissions = [
'global' => AccessControl::PERMISSION_MANAGE_EMAILS,
];
function add($data = array()) {
function add($data = []) {
$newsletter_id = (isset($data['newsletter_id'])
? (int)$data['newsletter_id']
: false
@@ -29,18 +29,18 @@ class SendingQueue extends APIEndpoint {
$newsletter = Newsletter::findOneWithOptions($newsletter_id);
if (!$newsletter instanceof Newsletter) {
return $this->errorResponse(array(
APIError::NOT_FOUND => WPFunctions::get()->__('This newsletter does not exist.', 'mailpoet')
));
return $this->errorResponse([
APIError::NOT_FOUND => WPFunctions::get()->__('This newsletter does not exist.', 'mailpoet'),
]);
}
// check that the sending method has been configured properly
try {
new \MailPoet\Mailer\Mailer(false);
} catch (\Exception $e) {
return $this->errorResponse(array(
$e->getCode() => $e->getMessage()
));
return $this->errorResponse([
$e->getCode() => $e->getMessage(),
]);
}
// add newsletter to the sending queue
@@ -50,9 +50,9 @@ class SendingQueue extends APIEndpoint {
->findOne();
if (!empty($queue)) {
return $this->errorResponse(array(
APIError::NOT_FOUND => WPFunctions::get()->__('This newsletter is already being sent.', 'mailpoet')
));
return $this->errorResponse([
APIError::NOT_FOUND => WPFunctions::get()->__('This newsletter is already being sent.', 'mailpoet'),
]);
}
$scheduled_queue = SendingQueueModel::joinWithTasks()
@@ -78,9 +78,9 @@ class SendingQueue extends APIEndpoint {
$finder = new SubscribersFinder();
$subscribers_count = $finder->addSubscribersToTaskFromSegments($queue->task(), $segments);
if (!$subscribers_count) {
return $this->errorResponse(array(
APIError::UNKNOWN => WPFunctions::get()->__('There are no subscribers in that list!', 'mailpoet')
));
return $this->errorResponse([
APIError::UNKNOWN => WPFunctions::get()->__('There are no subscribers in that list!', 'mailpoet'),
]);
}
$queue->updateCount();
$queue->status = null;
@@ -101,7 +101,7 @@ class SendingQueue extends APIEndpoint {
}
}
function pause($data = array()) {
function pause($data = []) {
$newsletter_id = (isset($data['newsletter_id'])
? (int)$data['newsletter_id']
: false
@@ -112,9 +112,9 @@ class SendingQueue extends APIEndpoint {
$queue = $newsletter->getQueue();
if ($queue === false) {
return $this->errorResponse(array(
APIError::UNKNOWN => WPFunctions::get()->__('This newsletter has not been sent yet.', 'mailpoet')
));
return $this->errorResponse([
APIError::UNKNOWN => WPFunctions::get()->__('This newsletter has not been sent yet.', 'mailpoet'),
]);
} else {
$queue->pause();
return $this->successResponse(
@@ -122,13 +122,13 @@ class SendingQueue extends APIEndpoint {
);
}
} else {
return $this->errorResponse(array(
APIError::NOT_FOUND => WPFunctions::get()->__('This newsletter does not exist.', 'mailpoet')
));
return $this->errorResponse([
APIError::NOT_FOUND => WPFunctions::get()->__('This newsletter does not exist.', 'mailpoet'),
]);
}
}
function resume($data = array()) {
function resume($data = []) {
$newsletter_id = (isset($data['newsletter_id'])
? (int)$data['newsletter_id']
: false
@@ -138,9 +138,9 @@ class SendingQueue extends APIEndpoint {
$queue = $newsletter->getQueue();
if ($queue === false) {
return $this->errorResponse(array(
APIError::UNKNOWN => WPFunctions::get()->__('This newsletter has not been sent yet.', 'mailpoet')
));
return $this->errorResponse([
APIError::UNKNOWN => WPFunctions::get()->__('This newsletter has not been sent yet.', 'mailpoet'),
]);
} else {
$queue->resume();
return $this->successResponse(
@@ -148,9 +148,9 @@ class SendingQueue extends APIEndpoint {
);
}
} else {
return $this->errorResponse(array(
APIError::NOT_FOUND => WPFunctions::get()->__('This newsletter does not exist.', 'mailpoet')
));
return $this->errorResponse([
APIError::NOT_FOUND => WPFunctions::get()->__('This newsletter does not exist.', 'mailpoet'),
]);
}
}
}

View File

@@ -16,31 +16,31 @@ if (!defined('ABSPATH')) exit;
class Services extends APIEndpoint {
public $bridge;
public $date_time;
public $permissions = array(
'global' => AccessControl::PERMISSION_MANAGE_SETTINGS
);
public $permissions = [
'global' => AccessControl::PERMISSION_MANAGE_SETTINGS,
];
function __construct() {
$this->bridge = new Bridge();
$this->date_time = new DateTime();
}
function checkMSSKey($data = array()) {
function checkMSSKey($data = []) {
$key = isset($data['key']) ? trim($data['key']) : null;
if (!$key) {
return $this->badRequest(array(
APIError::BAD_REQUEST => WPFunctions::get()->__('Please specify a key.', 'mailpoet')
));
return $this->badRequest([
APIError::BAD_REQUEST => WPFunctions::get()->__('Please specify a key.', 'mailpoet'),
]);
}
try {
$result = $this->bridge->checkMSSKey($key);
$this->bridge->storeMSSKeyAndState($key, $result);
} catch (\Exception $e) {
return $this->errorResponse(array(
$e->getCode() => $e->getMessage()
));
return $this->errorResponse([
$e->getCode() => $e->getMessage(),
]);
}
$state = !empty($result['state']) ? $result['state'] : null;
@@ -60,7 +60,7 @@ class Services extends APIEndpoint {
}
if ($success_message) {
return $this->successResponse(array('message' => $success_message));
return $this->successResponse(['message' => $success_message]);
}
switch ($state) {
@@ -84,25 +84,25 @@ class Services extends APIEndpoint {
break;
}
return $this->errorResponse(array(APIError::BAD_REQUEST => $error));
return $this->errorResponse([APIError::BAD_REQUEST => $error]);
}
function checkPremiumKey($data = array()) {
function checkPremiumKey($data = []) {
$key = isset($data['key']) ? trim($data['key']) : null;
if (!$key) {
return $this->badRequest(array(
APIError::BAD_REQUEST => WPFunctions::get()->__('Please specify a key.', 'mailpoet')
));
return $this->badRequest([
APIError::BAD_REQUEST => WPFunctions::get()->__('Please specify a key.', 'mailpoet'),
]);
}
try {
$result = $this->bridge->checkPremiumKey($key);
$this->bridge->storePremiumKeyAndState($key, $result);
} catch (\Exception $e) {
return $this->errorResponse(array(
$e->getCode() => $e->getMessage()
));
return $this->errorResponse([
$e->getCode() => $e->getMessage(),
]);
}
$state = !empty($result['state']) ? $result['state'] : null;
@@ -123,7 +123,7 @@ class Services extends APIEndpoint {
if ($success_message) {
return $this->successResponse(
array('message' => $success_message),
['message' => $success_message],
Installer::getPremiumStatus()
);
}
@@ -144,7 +144,7 @@ class Services extends APIEndpoint {
break;
}
return $this->errorResponse(array(APIError::BAD_REQUEST => $error));
return $this->errorResponse([APIError::BAD_REQUEST => $error]);
}
private function getErrorDescriptionByCode($code) {

View File

@@ -20,9 +20,9 @@ class Settings extends APIEndpoint {
/** @var SettingsController */
private $settings;
public $permissions = array(
'global' => AccessControl::PERMISSION_MANAGE_SETTINGS
);
public $permissions = [
'global' => AccessControl::PERMISSION_MANAGE_SETTINGS,
];
function __construct(SettingsController $settings) {
$this->settings = $settings;
@@ -32,13 +32,13 @@ class Settings extends APIEndpoint {
return $this->successResponse($this->settings->getAll());
}
function set($settings = array()) {
function set($settings = []) {
if (empty($settings)) {
return $this->badRequest(
array(
[
APIError::BAD_REQUEST =>
WPFunctions::get()->__('You have not specified any settings to be saved.', 'mailpoet')
));
WPFunctions::get()->__('You have not specified any settings to be saved.', 'mailpoet'),
]);
} else {
$original_inactivation_interval = (int)$this->settings->get('deactivate_subscriber_after_inactive_days');
// Will be uncommented on task [MAILPOET-1998]

View File

@@ -12,9 +12,9 @@ if (!defined('ABSPATH')) exit;
class Setup extends APIEndpoint {
private $wp;
public $permissions = array(
'global' => AccessControl::PERMISSION_MANAGE_SETTINGS
);
public $permissions = [
'global' => AccessControl::PERMISSION_MANAGE_SETTINGS,
];
function __construct(WPFunctions $wp) {
$this->wp = $wp;
@@ -28,9 +28,9 @@ class Setup extends APIEndpoint {
$this->wp->doAction('mailpoet_setup_reset');
return $this->successResponse();
} catch (\Exception $e) {
return $this->errorResponse(array(
$e->getCode() => $e->getMessage()
));
return $this->errorResponse([
$e->getCode() => $e->getMessage(),
]);
}
}
}

View File

@@ -26,10 +26,10 @@ if (!defined('ABSPATH')) exit;
class Subscribers extends APIEndpoint {
const SUBSCRIPTION_LIMIT_COOLDOWN = 60;
public $permissions = array(
public $permissions = [
'global' => AccessControl::PERMISSION_MANAGE_SUBSCRIBERS,
'methods' => array('subscribe' => AccessControl::NO_ACCESS_RESTRICTION)
);
'methods' => ['subscribe' => AccessControl::NO_ACCESS_RESTRICTION],
];
/** @var Listing\BulkActionController */
private $bulk_action_controller;
@@ -70,13 +70,13 @@ class Subscribers extends APIEndpoint {
$this->settings = $settings;
}
function get($data = array()) {
function get($data = []) {
$id = (isset($data['id']) ? (int)$data['id'] : false);
$subscriber = Subscriber::findOne($id);
if ($subscriber === false) {
return $this->errorResponse(array(
APIError::NOT_FOUND => WPFunctions::get()->__('This subscriber does not exist.', 'mailpoet')
));
return $this->errorResponse([
APIError::NOT_FOUND => WPFunctions::get()->__('This subscriber does not exist.', 'mailpoet'),
]);
} else {
return $this->successResponse(
$subscriber
@@ -87,7 +87,7 @@ class Subscribers extends APIEndpoint {
}
}
function listing($data = array()) {
function listing($data = []) {
if (!isset($data['filter']['segment'])) {
$listing_data = $this->listing_handler->get('\MailPoet\Models\Subscriber', $data);
@@ -95,7 +95,7 @@ class Subscribers extends APIEndpoint {
$listing_data = $this->subscribers_listings->getListingsInSegment($data);
}
$result = array();
$result = [];
foreach ($listing_data['items'] as $subscriber) {
$subscriber_result = $subscriber
->withSubscriptions()
@@ -111,11 +111,11 @@ class Subscribers extends APIEndpoint {
$listing_data['filters']['segment']
);
return $this->successResponse($result, array(
return $this->successResponse($result, [
'count' => $listing_data['count'],
'filters' => $listing_data['filters'],
'groups' => $listing_data['groups']
));
'groups' => $listing_data['groups'],
]);
}
private function preferUnsubscribedStatusFromSegment(array $subscriber, $segment_id) {
@@ -135,7 +135,7 @@ class Subscribers extends APIEndpoint {
}
}
function subscribe($data = array()) {
function subscribe($data = []) {
$form_id = (isset($data['form_id']) ? (int)$data['form_id'] : false);
$form = Form::findOne($form_id);
unset($data['form_id']);
@@ -143,40 +143,40 @@ class Subscribers extends APIEndpoint {
$recaptcha = $this->settings->get('re_captcha');
if (!$form instanceof Form) {
return $this->badRequest(array(
APIError::BAD_REQUEST => WPFunctions::get()->__('Please specify a valid form ID.', 'mailpoet')
));
return $this->badRequest([
APIError::BAD_REQUEST => WPFunctions::get()->__('Please specify a valid form ID.', 'mailpoet'),
]);
}
if (!empty($data['email'])) {
return $this->badRequest(array(
APIError::BAD_REQUEST => WPFunctions::get()->__('Please leave the first field empty.', 'mailpoet')
));
return $this->badRequest([
APIError::BAD_REQUEST => WPFunctions::get()->__('Please leave the first field empty.', 'mailpoet'),
]);
}
if (!empty($recaptcha['enabled']) && empty($data['recaptcha'])) {
return $this->badRequest(array(
APIError::BAD_REQUEST => WPFunctions::get()->__('Please check the CAPTCHA.', 'mailpoet')
));
return $this->badRequest([
APIError::BAD_REQUEST => WPFunctions::get()->__('Please check the CAPTCHA.', 'mailpoet'),
]);
}
if (!empty($recaptcha['enabled'])) {
$res = empty($data['recaptcha']) ? $data['recaptcha-no-js'] : $data['recaptcha'];
$res = WPFunctions::get()->wpRemotePost('https://www.google.com/recaptcha/api/siteverify', array(
'body' => array(
$res = WPFunctions::get()->wpRemotePost('https://www.google.com/recaptcha/api/siteverify', [
'body' => [
'secret' => $recaptcha['secret_token'],
'response' => $res
)
));
'response' => $res,
],
]);
if (is_wp_error($res)) {
return $this->badRequest(array(
APIError::BAD_REQUEST => WPFunctions::get()->__('Error while validating the CAPTCHA.', 'mailpoet')
));
return $this->badRequest([
APIError::BAD_REQUEST => WPFunctions::get()->__('Error while validating the CAPTCHA.', 'mailpoet'),
]);
}
$res = json_decode(wp_remote_retrieve_body($res));
if (empty($res->success)) {
return $this->badRequest(array(
APIError::BAD_REQUEST => WPFunctions::get()->__('Error while validating the CAPTCHA.', 'mailpoet')
));
return $this->badRequest([
APIError::BAD_REQUEST => WPFunctions::get()->__('Error while validating the CAPTCHA.', 'mailpoet'),
]);
}
}
@@ -190,15 +190,15 @@ class Subscribers extends APIEndpoint {
$segment_ids = (!empty($data['segments'])
? (array)$data['segments']
: array()
: []
);
$segment_ids = $form->filterSegments($segment_ids);
unset($data['segments']);
if (empty($segment_ids)) {
return $this->badRequest(array(
APIError::BAD_REQUEST => WPFunctions::get()->__('Please select a list.', 'mailpoet')
));
return $this->badRequest([
APIError::BAD_REQUEST => WPFunctions::get()->__('Please select a list.', 'mailpoet'),
]);
}
// only accept fields defined in the form
@@ -218,7 +218,7 @@ class Subscribers extends APIEndpoint {
if ($errors !== false) {
return $this->badRequest($errors);
} else {
$meta = array();
$meta = [];
if ($form !== false) {
// record form statistics
@@ -237,7 +237,7 @@ class Subscribers extends APIEndpoint {
}
return $this->successResponse(
array(),
[],
$meta
);
}
@@ -248,9 +248,9 @@ class Subscribers extends APIEndpoint {
return $obfuscator->deobfuscateFormPayload($data);
}
function save($data = array()) {
function save($data = []) {
if (empty($data['segments'])) {
$data['segments'] = array();
$data['segments'] = [];
}
$new_segments = $this->findNewSegments($data);
$subscriber = Subscriber::createOrUpdate($data);
@@ -285,7 +285,7 @@ class Subscribers extends APIEndpoint {
return array_diff($data['segments'], $old_segment_ids);
}
function restore($data = array()) {
function restore($data = []) {
$id = (isset($data['id']) ? (int)$data['id'] : false);
$subscriber = Subscriber::findOne($id);
if ($subscriber instanceof Subscriber) {
@@ -294,16 +294,16 @@ class Subscribers extends APIEndpoint {
if(!$subscriber instanceof Subscriber) return $this->errorResponse();
return $this->successResponse(
$subscriber->asArray(),
array('count' => 1)
['count' => 1]
);
} else {
return $this->errorResponse(array(
APIError::NOT_FOUND => WPFunctions::get()->__('This subscriber does not exist.', 'mailpoet')
));
return $this->errorResponse([
APIError::NOT_FOUND => WPFunctions::get()->__('This subscriber does not exist.', 'mailpoet'),
]);
}
}
function trash($data = array()) {
function trash($data = []) {
$id = (isset($data['id']) ? (int)$data['id'] : false);
$subscriber = Subscriber::findOne($id);
if ($subscriber instanceof Subscriber) {
@@ -312,29 +312,29 @@ class Subscribers extends APIEndpoint {
if(!$subscriber instanceof Subscriber) return $this->errorResponse();
return $this->successResponse(
$subscriber->asArray(),
array('count' => 1)
['count' => 1]
);
} else {
return $this->errorResponse(array(
APIError::NOT_FOUND => WPFunctions::get()->__('This subscriber does not exist.', 'mailpoet')
));
return $this->errorResponse([
APIError::NOT_FOUND => WPFunctions::get()->__('This subscriber does not exist.', 'mailpoet'),
]);
}
}
function delete($data = array()) {
function delete($data = []) {
$id = (isset($data['id']) ? (int)$data['id'] : false);
$subscriber = Subscriber::findOne($id);
if ($subscriber instanceof Subscriber) {
$subscriber->delete();
return $this->successResponse(null, array('count' => 1));
return $this->successResponse(null, ['count' => 1]);
} else {
return $this->errorResponse(array(
APIError::NOT_FOUND => WPFunctions::get()->__('This subscriber does not exist.', 'mailpoet')
));
return $this->errorResponse([
APIError::NOT_FOUND => WPFunctions::get()->__('This subscriber does not exist.', 'mailpoet'),
]);
}
}
function bulkAction($data = array()) {
function bulkAction($data = []) {
try {
if (!isset($data['listing']['filter']['segment'])) {
return $this->successResponse(
@@ -346,9 +346,9 @@ class Subscribers extends APIEndpoint {
return $this->successResponse(null, $bulk_action->apply());
}
} catch (\Exception $e) {
return $this->errorResponse(array(
$e->getCode() => $e->getMessage()
));
return $this->errorResponse([
$e->getCode() => $e->getMessage(),
]);
}
}
}

View File

@@ -16,9 +16,9 @@ class UserFlags extends APIEndpoint {
/** @var UserFlagsController */
private $user_flags;
public $permissions = array(
'global' => AccessControl::ALL_ROLES_ACCESS
);
public $permissions = [
'global' => AccessControl::ALL_ROLES_ACCESS,
];
function __construct(UserFlagsController $user_flags) {
$this->user_flags = $user_flags;
@@ -27,10 +27,10 @@ class UserFlags extends APIEndpoint {
function set(array $flags = []) {
if (empty($flags)) {
return $this->badRequest(
array(
[
APIError::BAD_REQUEST =>
WPFunctions::get()->__('You have not specified any user flags to be saved.', 'mailpoet')
));
WPFunctions::get()->__('You have not specified any user flags to be saved.', 'mailpoet'),
]);
} else {
foreach ($flags as $name => $value) {
$this->user_flags->set($name, $value);