diff --git a/mailpoet/lib/DI/ContainerConfigurator.php b/mailpoet/lib/DI/ContainerConfigurator.php index cf4816f581..7333ca4336 100644 --- a/mailpoet/lib/DI/ContainerConfigurator.php +++ b/mailpoet/lib/DI/ContainerConfigurator.php @@ -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); diff --git a/packages/php/email-editor/src/Engine/class-email-api-controller.php b/packages/php/email-editor/src/Engine/class-email-api-controller.php index 4e86bbf5d2..ad47628abe 100644 --- a/packages/php/email-editor/src/Engine/class-email-api-controller.php +++ b/packages/php/email-editor/src/Engine/class-email-api-controller.php @@ -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); + } } /** diff --git a/packages/php/email-editor/src/Integrations/Utils/class-send-preview-email.php b/packages/php/email-editor/src/Integrations/Utils/class-send-preview-email.php new file mode 100644 index 0000000000..281577028e --- /dev/null +++ b/packages/php/email-editor/src/Integrations/Utils/class-send-preview-email.php @@ -0,0 +1,75 @@ +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; + } +}