Refactor listing handler to reusable service
[MAILPOET-1689]
This commit is contained in:
@ -39,12 +39,8 @@ class Forms extends APIEndpoint {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function listing($data = array()) {
|
function listing($data = array()) {
|
||||||
$listing = new Listing\Handler(
|
$listing = new Listing\Handler();
|
||||||
'\MailPoet\Models\Form',
|
$listing_data = $listing->get('\MailPoet\Models\Form', $data);
|
||||||
$data
|
|
||||||
);
|
|
||||||
|
|
||||||
$listing_data = $listing->get();
|
|
||||||
|
|
||||||
$data = array();
|
$data = array();
|
||||||
foreach($listing_data['items'] as $form) {
|
foreach($listing_data['items'] as $form) {
|
||||||
|
@ -383,11 +383,8 @@ class Newsletters extends APIEndpoint {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function listing($data = array()) {
|
function listing($data = array()) {
|
||||||
$listing = new Listing\Handler(
|
$listing = new Listing\Handler();
|
||||||
'\MailPoet\Models\Newsletter',
|
$listing_data = $listing->get('\MailPoet\Models\Newsletter', $data);
|
||||||
$data
|
|
||||||
);
|
|
||||||
$listing_data = $listing->get();
|
|
||||||
|
|
||||||
$data = array();
|
$data = array();
|
||||||
foreach($listing_data['items'] as $newsletter) {
|
foreach($listing_data['items'] as $newsletter) {
|
||||||
|
@ -36,12 +36,8 @@ class Segments extends APIEndpoint {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function listing($data = array()) {
|
function listing($data = array()) {
|
||||||
$listing = new Listing\Handler(
|
$listing = new Listing\Handler();
|
||||||
'\MailPoet\Models\Segment',
|
$listing_data = $listing->get('\MailPoet\Models\Segment', $data);
|
||||||
$data
|
|
||||||
);
|
|
||||||
|
|
||||||
$listing_data = $listing->get();
|
|
||||||
|
|
||||||
$data = array();
|
$data = array();
|
||||||
foreach($listing_data['items'] as $segment) {
|
foreach($listing_data['items'] as $segment) {
|
||||||
|
@ -56,9 +56,9 @@ class Subscribers extends APIEndpoint {
|
|||||||
function listing($data = array()) {
|
function listing($data = array()) {
|
||||||
|
|
||||||
if(!isset($data['filter']['segment'])) {
|
if(!isset($data['filter']['segment'])) {
|
||||||
$listing = new Listing\Handler('\MailPoet\Models\Subscriber', $data);
|
$listing = new Listing\Handler();
|
||||||
|
|
||||||
$listing_data = $listing->get();
|
$listing_data = $listing->get('\MailPoet\Models\Subscriber', $data);
|
||||||
} else {
|
} else {
|
||||||
$listings = new SubscribersListings();
|
$listings = new SubscribersListings();
|
||||||
$listing_data = $listings->getListingsInSegment($data);
|
$listing_data = $listings->getListingsInSegment($data);
|
||||||
|
@ -14,14 +14,11 @@ class BulkActionController {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
$listing_handler = new Handler(
|
$listing_handler = new Handler();
|
||||||
$model_class,
|
|
||||||
$data['listing']
|
|
||||||
);
|
|
||||||
|
|
||||||
return call_user_func_array(
|
return call_user_func_array(
|
||||||
array($model_class, $bulk_action_method),
|
array($model_class, $bulk_action_method),
|
||||||
array($listing_handler->getSelection(), $data)
|
array($listing_handler->getSelection($model_class, $data['listing']), $data)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,15 +6,120 @@ if(!defined('ABSPATH')) exit;
|
|||||||
class Handler {
|
class Handler {
|
||||||
const DEFAULT_LIMIT_PER_PAGE = 20;
|
const DEFAULT_LIMIT_PER_PAGE = 20;
|
||||||
|
|
||||||
private $data = array();
|
function getSelection($model_class, array $data) {
|
||||||
private $model = null;
|
$data = $this->processData($data);
|
||||||
private $model_class = null;
|
$table_name = $model_class::$_table;
|
||||||
|
$model = \Model::factory($model_class);
|
||||||
|
|
||||||
function __construct($model_class, $data = array()) {
|
if(method_exists($model_class, 'listingQuery')) {
|
||||||
$this->table_name = $model_class::$_table;
|
$custom_query = call_user_func_array(
|
||||||
$this->model_class = $model_class;
|
[$model_class, 'listingQuery'],
|
||||||
$this->model = \Model::factory($this->model_class);
|
[$data]
|
||||||
|
);
|
||||||
|
if(!empty($data['selection'])) {
|
||||||
|
$custom_query->whereIn($table_name.'.id', $data['selection']);
|
||||||
|
}
|
||||||
|
return $custom_query;
|
||||||
|
} else {
|
||||||
|
$model = $this->setFilter($model, $data);
|
||||||
|
$this->setGroup($model, $data);
|
||||||
|
$this->setSearch($model, $data);
|
||||||
|
|
||||||
|
if(!empty($data['selection'])) {
|
||||||
|
$model->whereIn($table_name.'.id', $data['selection']);
|
||||||
|
}
|
||||||
|
return $model;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function get($model_class, array $data) {
|
||||||
|
$data = $this->processData($data);
|
||||||
|
$table_name = $model_class::$_table;
|
||||||
|
$model = \Model::factory($model_class);
|
||||||
|
// get groups
|
||||||
|
$groups = array();
|
||||||
|
if(method_exists($model_class, 'groups')) {
|
||||||
|
$groups = call_user_func_array(
|
||||||
|
array($model_class, 'groups'),
|
||||||
|
array($data)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// get filters
|
||||||
|
$filters = array();
|
||||||
|
if(method_exists($model_class, 'filters')) {
|
||||||
|
$filters = call_user_func_array(
|
||||||
|
array($model_class, 'filters'),
|
||||||
|
array($data)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// get items and total count
|
||||||
|
if(method_exists($model_class, 'listingQuery')) {
|
||||||
|
$custom_query = call_user_func_array(
|
||||||
|
array($model_class, 'listingQuery'),
|
||||||
|
array($data)
|
||||||
|
);
|
||||||
|
|
||||||
|
$count = $custom_query->count();
|
||||||
|
|
||||||
|
$items = $custom_query
|
||||||
|
->offset($data['offset'])
|
||||||
|
->limit($data['limit'])
|
||||||
|
->{'order_by_'.$data['sort_order']}(
|
||||||
|
$table_name.'.'.$data['sort_by']
|
||||||
|
)
|
||||||
|
->findMany();
|
||||||
|
} else {
|
||||||
|
$model = $this->setFilter($model, $data);
|
||||||
|
$this->setGroup($model, $data);
|
||||||
|
$this->setSearch($model, $data);
|
||||||
|
$this->setOrder($model, $data, $table_name);
|
||||||
|
|
||||||
|
$count = $model->count();
|
||||||
|
|
||||||
|
$items = $model
|
||||||
|
->offset($data['offset'])
|
||||||
|
->limit($data['limit'])
|
||||||
|
->findMany();
|
||||||
|
}
|
||||||
|
|
||||||
|
return array(
|
||||||
|
'count' => $count,
|
||||||
|
'filters' => $filters,
|
||||||
|
'groups' => $groups,
|
||||||
|
'items' => $items
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function setSearch(\ORMWrapper $model, array $data) {
|
||||||
|
if(empty($data['search'])) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
return $model->filter('search', $data['search']);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function setOrder(\ORMWrapper $model, array $data, $table_name) {
|
||||||
|
return $model
|
||||||
|
->{'order_by_'.$data['sort_order']}(
|
||||||
|
$table_name.'.'.$data['sort_by']);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function setGroup(\ORMWrapper $model, array $data) {
|
||||||
|
if($data['group'] === null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$model->filter('groupBy', $data['group']);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function setFilter(\ORMWrapper $model, array $data) {
|
||||||
|
if($data['filter'] === null) {
|
||||||
|
return $model;
|
||||||
|
}
|
||||||
|
return $model->filter('filterBy', $data['filter']);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function processData(array $data) {
|
||||||
// check if sort order was specified or default to "asc"
|
// check if sort order was specified or default to "asc"
|
||||||
$sort_order = (!empty($data['sort_order'])) ? $data['sort_order'] : 'asc';
|
$sort_order = (!empty($data['sort_order'])) ? $data['sort_order'] : 'asc';
|
||||||
// constrain sort order value to either be "asc" or "desc"
|
// constrain sort order value to either be "asc" or "desc"
|
||||||
@ -29,9 +134,9 @@ class Handler {
|
|||||||
$sort_by = 'id';
|
$sort_by = 'id';
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->data = array(
|
$data = [
|
||||||
// extra parameters
|
// extra parameters
|
||||||
'params' => (isset($data['params']) ? $data['params'] : array()),
|
'params' => (isset($data['params']) ? $data['params'] : []),
|
||||||
// pagination
|
// pagination
|
||||||
'offset' => (isset($data['offset']) ? (int)$data['offset'] : 0),
|
'offset' => (isset($data['offset']) ? (int)$data['offset'] : 0),
|
||||||
'limit' => (isset($data['limit'])
|
'limit' => (isset($data['limit'])
|
||||||
@ -49,112 +154,8 @@ class Handler {
|
|||||||
'filter' => (isset($data['filter']) ? $data['filter'] : null),
|
'filter' => (isset($data['filter']) ? $data['filter'] : null),
|
||||||
// selection
|
// selection
|
||||||
'selection' => (isset($data['selection']) ? $data['selection'] : null)
|
'selection' => (isset($data['selection']) ? $data['selection'] : null)
|
||||||
);
|
];
|
||||||
}
|
|
||||||
|
|
||||||
private function setSearch() {
|
return $data;
|
||||||
if(empty($this->data['search'])) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
return $this->model->filter('search', $this->data['search']);
|
|
||||||
}
|
|
||||||
|
|
||||||
private function setOrder() {
|
|
||||||
return $this->model
|
|
||||||
->{'order_by_'.$this->data['sort_order']}(
|
|
||||||
$this->table_name.'.'.$this->data['sort_by']);
|
|
||||||
}
|
|
||||||
|
|
||||||
private function setGroup() {
|
|
||||||
if($this->data['group'] === null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
return $this->model->filter('groupBy', $this->data['group']);
|
|
||||||
}
|
|
||||||
|
|
||||||
private function setFilter() {
|
|
||||||
if($this->data['filter'] === null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
$this->model = $this->model->filter('filterBy', $this->data['filter']);
|
|
||||||
}
|
|
||||||
|
|
||||||
function getSelection() {
|
|
||||||
if(method_exists($this->model_class, 'listingQuery')) {
|
|
||||||
$custom_query = call_user_func_array(
|
|
||||||
array($this->model_class, 'listingQuery'),
|
|
||||||
array($this->data)
|
|
||||||
);
|
|
||||||
if(!empty($this->data['selection'])) {
|
|
||||||
$custom_query->whereIn($this->table_name.'.id', $this->data['selection']);
|
|
||||||
}
|
|
||||||
return $custom_query;
|
|
||||||
} else {
|
|
||||||
$this->setFilter();
|
|
||||||
$this->setGroup();
|
|
||||||
$this->setSearch();
|
|
||||||
|
|
||||||
if(!empty($this->data['selection'])) {
|
|
||||||
$this->model->whereIn($this->table_name.'.id', $this->data['selection']);
|
|
||||||
}
|
|
||||||
return $this->model;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function get() {
|
|
||||||
// get groups
|
|
||||||
$groups = array();
|
|
||||||
if(method_exists($this->model_class, 'groups')) {
|
|
||||||
$groups = call_user_func_array(
|
|
||||||
array($this->model_class, 'groups'),
|
|
||||||
array($this->data)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// get filters
|
|
||||||
$filters = array();
|
|
||||||
if(method_exists($this->model_class, 'filters')) {
|
|
||||||
$filters = call_user_func_array(
|
|
||||||
array($this->model_class, 'filters'),
|
|
||||||
array($this->data)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// get items and total count
|
|
||||||
if(method_exists($this->model_class, 'listingQuery')) {
|
|
||||||
$custom_query = call_user_func_array(
|
|
||||||
array($this->model_class, 'listingQuery'),
|
|
||||||
array($this->data)
|
|
||||||
);
|
|
||||||
|
|
||||||
$count = $custom_query->count();
|
|
||||||
|
|
||||||
$items = $custom_query
|
|
||||||
->offset($this->data['offset'])
|
|
||||||
->limit($this->data['limit'])
|
|
||||||
->{'order_by_'.$this->data['sort_order']}(
|
|
||||||
$this->table_name.'.'.$this->data['sort_by']
|
|
||||||
)
|
|
||||||
->findMany();
|
|
||||||
} else {
|
|
||||||
$this->setFilter();
|
|
||||||
$this->setGroup();
|
|
||||||
$this->setSearch();
|
|
||||||
$this->setOrder();
|
|
||||||
|
|
||||||
$count = $this->model->count();
|
|
||||||
|
|
||||||
$items = $this->model
|
|
||||||
->offset($this->data['offset'])
|
|
||||||
->limit($this->data['limit'])
|
|
||||||
->findMany();
|
|
||||||
}
|
|
||||||
|
|
||||||
return array(
|
|
||||||
'count' => $count,
|
|
||||||
'filters' => $filters,
|
|
||||||
'groups' => $groups,
|
|
||||||
'items' => $items
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace MailPoet\Segments;
|
namespace MailPoet\Segments;
|
||||||
|
|
||||||
|
use MailPoet\Listing\Handler;
|
||||||
use MailPoet\Models\Segment;
|
use MailPoet\Models\Segment;
|
||||||
use MailPoet\WP\Hooks;
|
use MailPoet\WP\Hooks;
|
||||||
|
|
||||||
@ -36,7 +37,7 @@ 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\BulkActionController();
|
$bulk_action = new \MailPoet\Listing\BulkActionController(new Handler());
|
||||||
return $bulk_action->apply('\MailPoet\Models\Subscriber', $this->data);
|
return $bulk_action->apply('\MailPoet\Models\Subscriber', $this->data);
|
||||||
} 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());
|
||||||
|
@ -19,9 +19,9 @@ class SubscribersListings {
|
|||||||
|
|
||||||
private function getListings($data, Segment $segment = null) {
|
private function getListings($data, Segment $segment = null) {
|
||||||
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) {
|
||||||
$listing = new Handler('\MailPoet\Models\Subscriber', $data);
|
$listing = new Handler();
|
||||||
|
|
||||||
return $listing_data = $listing->get();
|
return $listing_data = $listing->get('\MailPoet\Models\Subscriber', $data);
|
||||||
}
|
}
|
||||||
$handlers = Hooks::applyFilters('mailpoet_get_subscribers_listings_in_segment_handlers', array());
|
$handlers = Hooks::applyFilters('mailpoet_get_subscribers_listings_in_segment_handlers', array());
|
||||||
foreach($handlers as $handler) {
|
foreach($handlers as $handler) {
|
||||||
|
Reference in New Issue
Block a user