Add API endpoint returning registered personalization tags
[MAILPOET-6354]
This commit is contained in:
@ -8,6 +8,8 @@
|
|||||||
declare(strict_types = 1);
|
declare(strict_types = 1);
|
||||||
namespace MailPoet\EmailEditor\Engine;
|
namespace MailPoet\EmailEditor\Engine;
|
||||||
|
|
||||||
|
use MailPoet\EmailEditor\Engine\PersonalizationTags\Personalization_Tag;
|
||||||
|
use MailPoet\EmailEditor\Engine\PersonalizationTags\Personalization_Tags_Registry;
|
||||||
use MailPoet\EmailEditor\Validator\Builder;
|
use MailPoet\EmailEditor\Validator\Builder;
|
||||||
use WP_Post;
|
use WP_Post;
|
||||||
use WP_REST_Request;
|
use WP_REST_Request;
|
||||||
@ -17,6 +19,21 @@ use WP_REST_Response;
|
|||||||
* Class for email API controller.
|
* Class for email API controller.
|
||||||
*/
|
*/
|
||||||
class Email_Api_Controller {
|
class Email_Api_Controller {
|
||||||
|
/**
|
||||||
|
* Personalization tags registry to get all personalization tags.
|
||||||
|
*
|
||||||
|
* @var Personalization_Tags_Registry
|
||||||
|
*/
|
||||||
|
private Personalization_Tags_Registry $personalization_tags_registry;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Email_Api_Controller constructor with all dependencies.
|
||||||
|
*
|
||||||
|
* @param Personalization_Tags_Registry $personalization_tags_registry Personalization tags registry.
|
||||||
|
*/
|
||||||
|
public function __construct( Personalization_Tags_Registry $personalization_tags_registry ) {
|
||||||
|
$this->personalization_tags_registry = $personalization_tags_registry;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns email specific data.
|
* Returns email specific data.
|
||||||
@ -68,6 +85,34 @@ class Email_Api_Controller {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns all registered personalization tags.
|
||||||
|
*
|
||||||
|
* @return WP_REST_Response
|
||||||
|
*/
|
||||||
|
public function get_personalization_tags(): WP_REST_Response {
|
||||||
|
$tags = $this->personalization_tags_registry->get_all();
|
||||||
|
return new WP_REST_Response(
|
||||||
|
array(
|
||||||
|
'success' => true,
|
||||||
|
'result' => array_values(
|
||||||
|
array_map(
|
||||||
|
function ( Personalization_Tag $tag ) {
|
||||||
|
return array(
|
||||||
|
'name' => $tag->get_name(),
|
||||||
|
'token' => $tag->get_token(),
|
||||||
|
'category' => $tag->get_category(),
|
||||||
|
'attributes' => $tag->get_attributes(),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
$tags
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
200
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the schema for email data.
|
* Returns the schema for email data.
|
||||||
*
|
*
|
||||||
|
@ -239,6 +239,17 @@ class Email_Editor {
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
register_rest_route(
|
||||||
|
'mailpoet-email-editor/v1',
|
||||||
|
'/get_personalization_tags',
|
||||||
|
array(
|
||||||
|
'methods' => 'GET',
|
||||||
|
'callback' => array( $this->email_api_controller, 'get_personalization_tags' ),
|
||||||
|
'permission_callback' => function () {
|
||||||
|
return current_user_can( 'edit_posts' );
|
||||||
|
},
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -153,12 +153,6 @@ abstract class MailPoetTest extends \Codeception\TestCase\Test { // phpcs:ignore
|
|||||||
return new Initializer();
|
return new Initializer();
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
$container->set(
|
|
||||||
Email_Api_Controller::class,
|
|
||||||
function () {
|
|
||||||
return new Email_Api_Controller();
|
|
||||||
}
|
|
||||||
);
|
|
||||||
$container->set(
|
$container->set(
|
||||||
BlockTypesController::class,
|
BlockTypesController::class,
|
||||||
function () {
|
function () {
|
||||||
@ -298,6 +292,14 @@ abstract class MailPoetTest extends \Codeception\TestCase\Test { // phpcs:ignore
|
|||||||
return new Personalization_Tags_Registry();
|
return new Personalization_Tags_Registry();
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
$container->set(
|
||||||
|
Email_Api_Controller::class,
|
||||||
|
function ( $container ) {
|
||||||
|
return new Email_Api_Controller(
|
||||||
|
$container->get( Personalization_Tags_Registry::class ),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
);
|
||||||
$container->set(
|
$container->set(
|
||||||
Email_Editor::class,
|
Email_Editor::class,
|
||||||
function ( $container ) {
|
function ( $container ) {
|
||||||
|
Reference in New Issue
Block a user