Add base route for sending preview email

MAILPOET-6092
This commit is contained in:
Oluwaseun Olorunsola
2024-11-13 14:36:40 +01:00
committed by Rostislav Wolný
parent 27d8093f86
commit 87676ccdb6
3 changed files with 27 additions and 14 deletions

View File

@ -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 {

View File

@ -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.
*

View File

@ -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.
*