diff --git a/packages/js/email-editor/src/store/actions.ts b/packages/js/email-editor/src/store/actions.ts index 13aec8378b..1c8469137a 100644 --- a/packages/js/email-editor/src/store/actions.ts +++ b/packages/js/email-editor/src/store/actions.ts @@ -146,21 +146,16 @@ export function* requestSendingNewsletterPreview( } as Partial< State[ 'preview' ] >, } as const; try { - const url = window.MailPoetEmailEditor.json_api_root; - const token = window.MailPoetEmailEditor.api_token; - const method = 'POST'; - const body = new FormData(); - body.append( 'token', token ); - body.append( 'action', 'mailpoet' ); - body.append( 'api_version', window.MailPoetEmailEditor.api_version ); - body.append( 'endpoint', 'newsletters' ); - body.append( 'method', 'sendPreview' ); - body.append( 'data[subscriber]', email ); - body.append( 'data[id]', newsletterId.toString() ); + const postId = select( storeName ).getEmailPostId(); + yield apiFetch( { - url, - method, - body, + path: '/mailpoet-email-editor/v1/send_preview_email', + method: 'POST', + data: { + newsletterId, + email, + postId, + }, } ); yield { 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 c9202b7778..4e86bbf5d2 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 @@ -10,6 +10,8 @@ namespace MailPoet\EmailEditor\Engine; use MailPoet\EmailEditor\Validator\Builder; use WP_Post; +use WP_REST_Request; +use WP_REST_Response; /** * Class for email API controller. @@ -35,6 +37,11 @@ class Email_Api_Controller { // Here comes code saving of Email specific data that will be passed on 'email_data' attribute. } + 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); + } + /** * Returns the schema for email data. * diff --git a/packages/php/email-editor/src/Engine/class-email-editor.php b/packages/php/email-editor/src/Engine/class-email-editor.php index ddbed8e2ef..cc5c6fdb9f 100644 --- a/packages/php/email-editor/src/Engine/class-email-editor.php +++ b/packages/php/email-editor/src/Engine/class-email-editor.php @@ -94,6 +94,7 @@ class Email_Editor { $this->extend_email_post_api(); $this->settings_controller->init(); } + add_action( 'rest_api_init', [ $this, 'register_email_editor_api_routes' ] ); } /** @@ -198,6 +199,16 @@ class Email_Editor { ); } + public function register_email_editor_api_routes() { + register_rest_route('mailpoet-email-editor/v1', '/send_preview_email', [ + 'methods' => 'POST', + 'callback' => array( $this->email_api_controller, 'send_preview_email_data' ), + 'permission_callback' => function() { + return current_user_can('edit_posts'); + } + ]); + } + /** * Returns the schema for email theme data. *