Add Send_Preview_Email class for MailPoet email editor
MAILPOET-6092
This commit is contained in:
committed by
Rostislav Wolný
parent
87676ccdb6
commit
ee967015dc
@ -364,6 +364,7 @@ class ContainerConfigurator implements IContainerConfigurator {
|
||||
$container->autowire(\MailPoet\EmailEditor\Integrations\MailPoet\EmailApiController::class)->setPublic(true);
|
||||
$container->autowire(\MailPoet\EmailEditor\Integrations\MailPoet\Blocks\BlockTypesController::class)->setPublic(true);
|
||||
$container->autowire(\MailPoet\EmailEditor\Integrations\MailPoet\Blocks\BlockTypes\PoweredByMailpoet::class)->setPublic(true);
|
||||
$container->autowire(\MailPoet\EmailEditor\Integrations\Utils\Send_Preview_Email::class)->setPublic(true);
|
||||
// Features
|
||||
$container->autowire(\MailPoet\Features\FeaturesController::class)->setPublic(true);
|
||||
$container->autowire(\MailPoet\Features\FeatureFlagsController::class)->setPublic(true);
|
||||
|
@ -8,6 +8,7 @@
|
||||
declare(strict_types = 1);
|
||||
namespace MailPoet\EmailEditor\Engine;
|
||||
|
||||
use MailPoet\EmailEditor\Integrations\Utils\Send_Preview_Email;
|
||||
use MailPoet\EmailEditor\Validator\Builder;
|
||||
use WP_Post;
|
||||
use WP_REST_Request;
|
||||
@ -17,6 +18,23 @@ use WP_REST_Response;
|
||||
* Class for email API controller.
|
||||
*/
|
||||
class Email_Api_Controller {
|
||||
|
||||
/**
|
||||
* Property for the send preview email controller.
|
||||
*
|
||||
* @var Send_Preview_Email Send Preview controller.
|
||||
*/
|
||||
private Send_Preview_Email $send_Preview_Email;
|
||||
|
||||
/**
|
||||
* Email_Api_Controller constructor.
|
||||
*/
|
||||
public function __construct(
|
||||
Send_Preview_Email $send_Preview_Email
|
||||
) {
|
||||
$this->send_Preview_Email = $send_Preview_Email;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns email specific data.
|
||||
*
|
||||
@ -39,7 +57,12 @@ class Email_Api_Controller {
|
||||
|
||||
public function send_preview_email_data( WP_REST_Request $request ): WP_REST_Response {
|
||||
$data = $request->get_params();
|
||||
return new WP_REST_Response(['success' => true, 'data' => $data], 200);
|
||||
try {
|
||||
$result = $this->send_Preview_Email->sendPreviewEmail($data);
|
||||
return new WP_REST_Response(['success' => true, 'result' => $result], 200);
|
||||
} catch ( \Exception $exception ) {
|
||||
return new WP_REST_Response(['error' => $exception->getMessage()], 400);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -0,0 +1,75 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of the MailPoet plugin.
|
||||
*
|
||||
* @package MailPoet\EmailEditor
|
||||
*/
|
||||
|
||||
declare( strict_types = 1 );
|
||||
|
||||
namespace MailPoet\EmailEditor\Integrations\Utils;
|
||||
|
||||
use MailPoet\EmailEditor\Engine\Renderer\Renderer;
|
||||
|
||||
class Send_Preview_Email {
|
||||
|
||||
private Renderer $renderer;
|
||||
|
||||
/**
|
||||
* Send_Preview_Email constructor.
|
||||
*/
|
||||
public function __construct(
|
||||
Renderer $renderer
|
||||
) {
|
||||
$this->renderer = $renderer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends preview email
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function sendPreviewEmail(array $data): bool {
|
||||
$this->validateData($data);
|
||||
|
||||
$email = $data['email'];
|
||||
$postId = $data['postId'];
|
||||
|
||||
$post = $this->fetchPost($postId);
|
||||
|
||||
$subject = $post->post_title;
|
||||
$language = get_bloginfo('language');
|
||||
|
||||
$renderedData = $this->renderer->render(
|
||||
$post,
|
||||
$subject,
|
||||
__('Preview', 'mailpoet'),
|
||||
$language
|
||||
);
|
||||
|
||||
$emailHtmlContent = $renderedData['html'];
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private function validateData( array $data ) {
|
||||
if ( empty( $data['email'] ) || empty( $data['postId'] ) ) {
|
||||
throw new \InvalidArgumentException(__('Missing required data', 'mailpoet'));
|
||||
}
|
||||
|
||||
if ( ! is_email( $data['email']) ) {
|
||||
throw new \InvalidArgumentException(__('Invalid email', 'mailpoet'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
private function fetchPost( $postId ): \WP_Post {
|
||||
$post = get_post(intval($postId));
|
||||
if (!$post instanceof \WP_Post) {
|
||||
throw new \Exception(__('Invalid post', 'mailpoet'));
|
||||
}
|
||||
return $post;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user