Refactor bulk action into reusable controller
[MAILPOET-1689]
This commit is contained in:
@ -280,11 +280,8 @@ class Forms extends APIEndpoint {
|
|||||||
|
|
||||||
function bulkAction($data = array()) {
|
function bulkAction($data = array()) {
|
||||||
try {
|
try {
|
||||||
$bulk_action = new Listing\BulkAction(
|
$bulk_action = new Listing\BulkActionController();
|
||||||
'\MailPoet\Models\Form',
|
$meta = $bulk_action->apply('\MailPoet\Models\Form', $data);
|
||||||
$data
|
|
||||||
);
|
|
||||||
$meta = $bulk_action->apply();
|
|
||||||
return $this->successResponse(null, $meta);
|
return $this->successResponse(null, $meta);
|
||||||
} catch(\Exception $e) {
|
} catch(\Exception $e) {
|
||||||
return $this->errorResponse(array(
|
return $this->errorResponse(array(
|
||||||
|
@ -438,11 +438,8 @@ class Newsletters extends APIEndpoint {
|
|||||||
|
|
||||||
function bulkAction($data = array()) {
|
function bulkAction($data = array()) {
|
||||||
try {
|
try {
|
||||||
$bulk_action = new Listing\BulkAction(
|
$bulk_action = new Listing\BulkActionController();
|
||||||
'\MailPoet\Models\Newsletter',
|
$meta = $bulk_action->apply('\MailPoet\Models\Newsletter', $data);
|
||||||
$data
|
|
||||||
);
|
|
||||||
$meta = $bulk_action->apply();
|
|
||||||
return $this->successResponse(null, $meta);
|
return $this->successResponse(null, $meta);
|
||||||
} catch(\Exception $e) {
|
} catch(\Exception $e) {
|
||||||
return $this->errorResponse(array(
|
return $this->errorResponse(array(
|
||||||
|
@ -152,11 +152,8 @@ class Segments extends APIEndpoint {
|
|||||||
|
|
||||||
function bulkAction($data = array()) {
|
function bulkAction($data = array()) {
|
||||||
try {
|
try {
|
||||||
$bulk_action = new Listing\BulkAction(
|
$bulk_action = new Listing\BulkActionController();
|
||||||
'\MailPoet\Models\Segment',
|
$meta = $bulk_action->apply('\MailPoet\Models\Segment', $data);
|
||||||
$data
|
|
||||||
);
|
|
||||||
$meta = $bulk_action->apply();
|
|
||||||
return $this->successResponse(null, $meta);
|
return $this->successResponse(null, $meta);
|
||||||
} catch(\Exception $e) {
|
} catch(\Exception $e) {
|
||||||
return $this->errorResponse(array(
|
return $this->errorResponse(array(
|
||||||
|
@ -262,11 +262,15 @@ class Subscribers extends APIEndpoint {
|
|||||||
function bulkAction($data = array()) {
|
function bulkAction($data = array()) {
|
||||||
try {
|
try {
|
||||||
if(!isset($data['listing']['filter']['segment'])) {
|
if(!isset($data['listing']['filter']['segment'])) {
|
||||||
$bulk_action = new Listing\BulkAction('\MailPoet\Models\Subscriber', $data);
|
$bulk_action = new Listing\BulkActionController();
|
||||||
|
return $this->successResponse(
|
||||||
|
null,
|
||||||
|
$bulk_action->apply('\MailPoet\Models\Subscriber', $data)
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
$bulk_action = new BulkAction($data);
|
$bulk_action = new BulkAction($data);
|
||||||
|
return $this->successResponse(null, $bulk_action->apply());
|
||||||
}
|
}
|
||||||
return $this->successResponse(null, $bulk_action->apply());
|
|
||||||
} catch(\Exception $e) {
|
} catch(\Exception $e) {
|
||||||
return $this->errorResponse(array(
|
return $this->errorResponse(array(
|
||||||
$e->getCode() => $e->getMessage()
|
$e->getCode() => $e->getMessage()
|
||||||
|
@ -1,38 +0,0 @@
|
|||||||
<?php
|
|
||||||
namespace MailPoet\Listing;
|
|
||||||
|
|
||||||
if(!defined('ABSPATH')) exit;
|
|
||||||
|
|
||||||
class BulkAction {
|
|
||||||
private $listing = null;
|
|
||||||
private $action = null;
|
|
||||||
private $data = null;
|
|
||||||
private $model_class = null;
|
|
||||||
|
|
||||||
function __construct($model_class, $data) {
|
|
||||||
$this->action = $data['action'];
|
|
||||||
unset($data['action']);
|
|
||||||
$this->data = $data;
|
|
||||||
$this->model_class = $model_class;
|
|
||||||
$this->listing = new Handler(
|
|
||||||
$model_class,
|
|
||||||
$this->data['listing']
|
|
||||||
);
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
function apply() {
|
|
||||||
$bulk_action_method = 'bulk'.ucfirst($this->action);
|
|
||||||
|
|
||||||
if(!method_exists($this->model_class, $bulk_action_method)) {
|
|
||||||
throw new \Exception(
|
|
||||||
$this->model_class. ' has no method "'.$bulk_action_method.'"'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return call_user_func_array(
|
|
||||||
array($this->model_class, $bulk_action_method),
|
|
||||||
array($this->listing->getSelection(), $this->data)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
27
lib/Listing/BulkActionController.php
Normal file
27
lib/Listing/BulkActionController.php
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
<?php
|
||||||
|
namespace MailPoet\Listing;
|
||||||
|
|
||||||
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
|
class BulkActionController {
|
||||||
|
function apply($model_class, array $data) {
|
||||||
|
$bulk_action_method = 'bulk'.ucfirst($data['action']);
|
||||||
|
unset($data['action']);
|
||||||
|
|
||||||
|
if(!method_exists($model_class, $bulk_action_method)) {
|
||||||
|
throw new \Exception(
|
||||||
|
$model_class. ' has no method "'.$bulk_action_method.'"'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$listing_handler = new Handler(
|
||||||
|
$model_class,
|
||||||
|
$data['listing']
|
||||||
|
);
|
||||||
|
|
||||||
|
return call_user_func_array(
|
||||||
|
array($model_class, $bulk_action_method),
|
||||||
|
array($listing_handler->getSelection(), $data)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@ -36,12 +36,8 @@ class BulkAction {
|
|||||||
*/
|
*/
|
||||||
private function applySegment($segment) {
|
private function applySegment($segment) {
|
||||||
if(!$segment || $segment['type'] === Segment::TYPE_DEFAULT || $segment['type'] === Segment::TYPE_WP_USERS) {
|
if(!$segment || $segment['type'] === Segment::TYPE_DEFAULT || $segment['type'] === Segment::TYPE_WP_USERS) {
|
||||||
$bulk_action = new \MailPoet\Listing\BulkAction(
|
$bulk_action = new \MailPoet\Listing\BulkActionController();
|
||||||
'\MailPoet\Models\Subscriber',
|
return $bulk_action->apply('\MailPoet\Models\Subscriber', $this->data);
|
||||||
$this->data
|
|
||||||
);
|
|
||||||
|
|
||||||
return $bulk_action->apply();
|
|
||||||
} else {
|
} else {
|
||||||
$handlers = Hooks::applyFilters('mailpoet_subscribers_in_segment_apply_bulk_action_handlers', array());
|
$handlers = Hooks::applyFilters('mailpoet_subscribers_in_segment_apply_bulk_action_handlers', array());
|
||||||
foreach($handlers as $handler) {
|
foreach($handlers as $handler) {
|
||||||
@ -54,4 +50,4 @@ class BulkAction {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user