Merge pull request #1628 from mailpoet/mp-api-di

Introduce DI to plugin API [MAILPOET-1637]
This commit is contained in:
M. Shull
2018-11-15 07:55:11 -05:00
committed by GitHub
11 changed files with 210 additions and 107 deletions

View File

@@ -2,6 +2,7 @@
namespace MailPoet\API\JSON;
use MailPoet\Config\AccessControl;
use MailPoet\Dependencies\Symfony\Component\DependencyInjection\Container;
use MailPoet\Models\Setting;
use MailPoet\Util\Helpers;
use MailPoet\Util\Security;
@@ -20,10 +21,16 @@ class API {
private $_available_api_versions = array(
'v1'
);
/** @var Container */
private $container;
/** @var AccessControl */
private $access_control;
const CURRENT_VERSION = 'v1';
function __construct(AccessControl $access_control) {
function __construct(Container $container, AccessControl $access_control) {
$this->container = $container;
$this->access_control = $access_control;
foreach($this->_available_api_versions as $available_api_version) {
$this->addEndpointNamespace(
@@ -135,7 +142,7 @@ class API {
throw new \Exception(__('Invalid API endpoint.', 'mailpoet'));
}
$endpoint = new $this->_request_endpoint_class();
$endpoint = $this->container->get($this->_request_endpoint_class);
if(!method_exists($endpoint, $this->_request_method)) {
throw new \Exception(__('Invalid API endpoint method.', 'mailpoet'));

View File

@@ -10,13 +10,14 @@ use MailPoet\WP\Posts as WPPosts;
if(!defined('ABSPATH')) exit;
class AutomatedLatestContent extends APIEndpoint {
/** @var \MailPoet\Newsletter\AutomatedLatestContent */
public $ALC;
public $permissions = array(
'global' => AccessControl::PERMISSION_MANAGE_EMAILS
);
function __construct() {
$this->ALC = new \MailPoet\Newsletter\AutomatedLatestContent();
function __construct(\MailPoet\Newsletter\AutomatedLatestContent $alc) {
$this->ALC = $alc;
}
function getPostTypes() {
@@ -74,14 +75,12 @@ class AutomatedLatestContent extends APIEndpoint {
}
function getBulkTransformedPosts($data = array()) {
$alc = new \MailPoet\Newsletter\AutomatedLatestContent();
$used_posts = array();
$rendered_posts = array();
foreach($data['blocks'] as $block) {
$posts = $alc->getPosts($block, $used_posts);
$rendered_posts[] = $alc->transformPosts($block, $posts);
$posts = $this->ALC->getPosts($block, $used_posts);
$rendered_posts[] = $this->ALC->transformPosts($block, $posts);
foreach($posts as $post) {
$used_posts[] = $post->ID;
@@ -90,4 +89,4 @@ class AutomatedLatestContent extends APIEndpoint {
return $this->successResponse($rendered_posts);
}
}
}