Add bulk action factory to be able to use custom classes for actions [MAILPOET-1922]
This commit is contained in:
@ -71,6 +71,7 @@ class ContainerConfigurator implements IContainerConfigurator {
|
|||||||
$container->autowire(\MailPoet\Cron\CronTrigger::class)->setPublic(true);
|
$container->autowire(\MailPoet\Cron\CronTrigger::class)->setPublic(true);
|
||||||
// Listing
|
// Listing
|
||||||
$container->autowire(\MailPoet\Listing\BulkActionController::class)->setPublic(true);
|
$container->autowire(\MailPoet\Listing\BulkActionController::class)->setPublic(true);
|
||||||
|
$container->autowire(\MailPoet\Listing\BulkActionFactory::class)->setPublic(true);
|
||||||
$container->autowire(\MailPoet\Listing\Handler::class)->setPublic(true);
|
$container->autowire(\MailPoet\Listing\Handler::class)->setPublic(true);
|
||||||
// Router
|
// Router
|
||||||
$container->autowire(\MailPoet\Router\Endpoints\CronDaemon::class)->setPublic(true);
|
$container->autowire(\MailPoet\Router\Endpoints\CronDaemon::class)->setPublic(true);
|
||||||
|
@ -4,10 +4,14 @@ namespace MailPoet\Listing;
|
|||||||
if (!defined('ABSPATH')) exit;
|
if (!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
class BulkActionController {
|
class BulkActionController {
|
||||||
|
/** @var BulkActionFactory */
|
||||||
|
private $factory;
|
||||||
|
|
||||||
/** @var Handler */
|
/** @var Handler */
|
||||||
private $handler;
|
private $handler;
|
||||||
|
|
||||||
function __construct(Handler $handler) {
|
function __construct(BulkActionFactory $factory, Handler $handler) {
|
||||||
|
$this->factory = $factory;
|
||||||
$this->handler = $handler;
|
$this->handler = $handler;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -15,14 +19,10 @@ class BulkActionController {
|
|||||||
$bulk_action_method = 'bulk'.ucfirst($data['action']);
|
$bulk_action_method = 'bulk'.ucfirst($data['action']);
|
||||||
unset($data['action']);
|
unset($data['action']);
|
||||||
|
|
||||||
if (!method_exists($model_class, $bulk_action_method)) {
|
$action_class = $this->factory->getActionClass($model_class, $bulk_action_method);
|
||||||
throw new \Exception(
|
|
||||||
$model_class. ' has no method "'.$bulk_action_method.'"'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return call_user_func_array(
|
return call_user_func_array(
|
||||||
array($model_class, $bulk_action_method),
|
array($action_class, $bulk_action_method),
|
||||||
array($this->handler->getSelection($model_class, $data['listing']), $data)
|
array($this->handler->getSelection($model_class, $data['listing']), $data)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
31
lib/Listing/BulkActionFactory.php
Normal file
31
lib/Listing/BulkActionFactory.php
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
<?php
|
||||||
|
namespace MailPoet\Listing;
|
||||||
|
|
||||||
|
if (!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
|
class BulkActionFactory {
|
||||||
|
/** @var array */
|
||||||
|
private $actions = [];
|
||||||
|
|
||||||
|
function registerAction($model_class, $bulk_action_method, $action_class) {
|
||||||
|
$this->ensureMethodExists($action_class, $bulk_action_method);
|
||||||
|
$this->actions[$model_class][$bulk_action_method] = $action_class;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getActionClass($model_class, $bulk_action_method) {
|
||||||
|
$resulting_class = $model_class;
|
||||||
|
if (!empty($this->actions[$model_class][$bulk_action_method])) {
|
||||||
|
$resulting_class = $this->actions[$model_class][$bulk_action_method];
|
||||||
|
}
|
||||||
|
$this->ensureMethodExists($resulting_class, $bulk_action_method);
|
||||||
|
return $resulting_class;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function ensureMethodExists($action_class, $bulk_action_method) {
|
||||||
|
if (!method_exists($action_class, $bulk_action_method)) {
|
||||||
|
throw new \Exception(
|
||||||
|
(is_object($action_class) ? get_class($action_class) : $action_class).' has no method "'.$bulk_action_method.'"'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace MailPoet\Segments;
|
namespace MailPoet\Segments;
|
||||||
|
|
||||||
|
use MailPoet\DI\ContainerWrapper;
|
||||||
use MailPoet\Listing\Handler;
|
use MailPoet\Listing\Handler;
|
||||||
use MailPoet\Models\Segment;
|
use MailPoet\Models\Segment;
|
||||||
use MailPoet\WP\Functions as WPFunctions;
|
use MailPoet\WP\Functions as WPFunctions;
|
||||||
@ -41,7 +42,10 @@ class BulkAction {
|
|||||||
if (!$segment
|
if (!$segment
|
||||||
|| in_array($segment['type'], [Segment::TYPE_DEFAULT, Segment::TYPE_WP_USERS, Segment::TYPE_WC_USERS], true)
|
|| in_array($segment['type'], [Segment::TYPE_DEFAULT, Segment::TYPE_WP_USERS, Segment::TYPE_WC_USERS], true)
|
||||||
) {
|
) {
|
||||||
$bulk_action = new \MailPoet\Listing\BulkActionController(new Handler());
|
$bulk_action = new \MailPoet\Listing\BulkActionController(
|
||||||
|
ContainerWrapper::getInstance()->get(\MailPoet\Listing\BulkActionFactory::class),
|
||||||
|
new Handler()
|
||||||
|
);
|
||||||
return $bulk_action->apply('\MailPoet\Models\Subscriber', $this->data);
|
return $bulk_action->apply('\MailPoet\Models\Subscriber', $this->data);
|
||||||
} else {
|
} else {
|
||||||
$handlers = $this->wp->applyFilters('mailpoet_subscribers_in_segment_apply_bulk_action_handlers', array());
|
$handlers = $this->wp->applyFilters('mailpoet_subscribers_in_segment_apply_bulk_action_handlers', array());
|
||||||
|
Reference in New Issue
Block a user