Fix code lint style errors
MAILPOET-6092
This commit is contained in:
committed by
Rostislav Wolný
parent
a9c2c3ca1d
commit
d83c179f5c
@@ -5,3 +5,4 @@
|
||||
npx lint-staged -c mailpoet/package.json --cwd mailpoet
|
||||
npx lint-staged -c package.json
|
||||
npx lint-staged -c packages/js/email-editor/package.json --cwd packages/js/email-editor
|
||||
cd packages/php/email-editor && ../../../mailpoet/tools/vendor/composer.phar code-style
|
||||
|
@@ -29,6 +29,7 @@
|
||||
"scripts": {
|
||||
"unit-test": "vendor/bin/codecept run unit",
|
||||
"integration-test": "cd ../../../tests_env/docker && COMPOSE_HTTP_TIMEOUT=200 docker compose run -e SKIP_DEPS=1 -e SKIP_PLUGINS=1 -e PACKAGE_NAME=email-editor codeception_integration",
|
||||
"code-style": "../../../mailpoet/tasks/code_sniffer/vendor/bin/phpcs -ps"
|
||||
"code-style": "../../../mailpoet/tasks/code_sniffer/vendor/bin/phpcs -ps",
|
||||
"code-style-fix": "../../../mailpoet/tasks/code_sniffer/vendor/bin/phpcbf -p"
|
||||
}
|
||||
}
|
||||
|
@@ -24,15 +24,17 @@ class Email_Api_Controller {
|
||||
*
|
||||
* @var Send_Preview_Email Send Preview controller.
|
||||
*/
|
||||
private Send_Preview_Email $send_Preview_Email;
|
||||
private Send_Preview_Email $send_preview_email;
|
||||
|
||||
/**
|
||||
* Email_Api_Controller constructor.
|
||||
*
|
||||
* @param Send_Preview_Email $send_preview_email send_preview_email.
|
||||
*/
|
||||
public function __construct(
|
||||
Send_Preview_Email $send_Preview_Email
|
||||
Send_Preview_Email $send_preview_email
|
||||
) {
|
||||
$this->send_Preview_Email = $send_Preview_Email;
|
||||
$this->send_preview_email = $send_preview_email;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -55,13 +57,25 @@ class Email_Api_Controller {
|
||||
// Here comes code saving of Email specific data that will be passed on 'email_data' attribute.
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends preview email
|
||||
*
|
||||
* @param WP_REST_Request $request route request.
|
||||
* @return WP_REST_Response
|
||||
*/
|
||||
public function send_preview_email_data( WP_REST_Request $request ): WP_REST_Response {
|
||||
$data = $request->get_params();
|
||||
try {
|
||||
$result = $this->send_Preview_Email->sendPreviewEmail($data);
|
||||
return new WP_REST_Response(['success' => true, 'result' => $result], 200);
|
||||
$result = $this->send_preview_email->send_preview_email( $data );
|
||||
return new WP_REST_Response(
|
||||
array(
|
||||
'success' => true,
|
||||
'result' => $result,
|
||||
),
|
||||
200
|
||||
);
|
||||
} catch ( \Exception $exception ) {
|
||||
return new WP_REST_Response(['error' => $exception->getMessage()], 400);
|
||||
return new WP_REST_Response( array( 'error' => $exception->getMessage() ), 400 );
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -94,7 +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' ] );
|
||||
add_action( 'rest_api_init', array( $this, 'register_email_editor_api_routes' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -199,14 +199,23 @@ class Email_Editor {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers the API route endpoint for the email editor
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register_email_editor_api_routes() {
|
||||
register_rest_route('mailpoet-email-editor/v1', '/send_preview_email', [
|
||||
register_rest_route(
|
||||
'mailpoet-email-editor/v1',
|
||||
'/send_preview_email',
|
||||
array(
|
||||
'methods' => 'POST',
|
||||
'callback' => array( $this->email_api_controller, 'send_preview_email_data' ),
|
||||
'permission_callback' => function() {
|
||||
return current_user_can('edit_posts');
|
||||
}
|
||||
]);
|
||||
'permission_callback' => function () {
|
||||
return current_user_can( 'edit_posts' );
|
||||
},
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -11,12 +11,27 @@ namespace MailPoet\EmailEditor\Integrations\Utils;
|
||||
|
||||
use MailPoet\EmailEditor\Engine\Renderer\Renderer;
|
||||
|
||||
/**
|
||||
* Class Send_Preview_Email
|
||||
*
|
||||
* This class is responsible for handling the functionality to send preview emails.
|
||||
* It is part of the email editor integrations utilities.
|
||||
*
|
||||
* @package MailPoet\EmailEditor\Integrations\Utils
|
||||
*/
|
||||
class Send_Preview_Email {
|
||||
|
||||
/**
|
||||
* Instance of the Renderer class used for rendering the editor emails.
|
||||
*
|
||||
* @var Renderer $renderer
|
||||
*/
|
||||
private Renderer $renderer;
|
||||
|
||||
/**
|
||||
* Send_Preview_Email constructor.
|
||||
*
|
||||
* @param Renderer $renderer renderer instance.
|
||||
*/
|
||||
public function __construct(
|
||||
Renderer $renderer
|
||||
@@ -25,65 +40,95 @@ class Send_Preview_Email {
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends preview email
|
||||
* @throws \Exception
|
||||
* Sends a preview email.
|
||||
*
|
||||
* @param array $data The data required to send the preview email.
|
||||
* @return bool Returns true if the preview email was sent successfully, false otherwise.
|
||||
* @throws \Exception If the data is invalid.
|
||||
*/
|
||||
public function sendPreviewEmail(array $data): bool {
|
||||
$this->validateData($data);
|
||||
public function send_preview_email( array $data ): bool {
|
||||
$this->validate_data( $data );
|
||||
|
||||
$email = $data['email'];
|
||||
$postId = $data['postId'];
|
||||
$post_id = $data['postId'];
|
||||
|
||||
$post = $this->fetchPost($postId);
|
||||
$post = $this->fetch_post( $post_id );
|
||||
|
||||
$subject = $post->post_title ?: __('Email Preview', 'mailpoet');
|
||||
$language = get_bloginfo('language');
|
||||
$subject = isset( $post->post_title ) ? $post->post_title : __( 'Email Preview', 'mailpoet' );
|
||||
$language = get_bloginfo( 'language' );
|
||||
|
||||
$renderedData = $this->renderer->render(
|
||||
$rendered_data = $this->renderer->render(
|
||||
$post,
|
||||
$subject,
|
||||
__('Preview', 'mailpoet'),
|
||||
__( 'Preview', 'mailpoet' ),
|
||||
$language
|
||||
);
|
||||
|
||||
$emailHtmlContent = $renderedData['html'];
|
||||
$email_html_content = $rendered_data['html'];
|
||||
|
||||
return $this->sendEmail($email, $subject, $emailHtmlContent);
|
||||
return $this->send_email( $email, $subject, $email_html_content );
|
||||
}
|
||||
|
||||
public function sendEmail($to, $subject, $body) {
|
||||
add_filter( 'wp_mail_content_type', [$this, 'set_mail_content_type'] );
|
||||
/**
|
||||
* Sends an email preview.
|
||||
*
|
||||
* @param string $to The recipient email address.
|
||||
* @param string $subject The subject of the email.
|
||||
* @param string $body The body content of the email.
|
||||
* @return bool Returns true if the email was sent successfully, false otherwise.
|
||||
*/
|
||||
public function send_email( string $to, string $subject, string $body ): bool {
|
||||
add_filter( 'wp_mail_content_type', array( $this, 'set_mail_content_type' ) );
|
||||
|
||||
$result = wp_mail( $to, $subject, $body );
|
||||
|
||||
// Reset content-type to avoid conflicts
|
||||
remove_filter( 'wp_mail_content_type', [$this, 'set_mail_content_type'] );
|
||||
// Reset content-type to avoid conflicts.
|
||||
remove_filter( 'wp_mail_content_type', array( $this, 'set_mail_content_type' ) );
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function set_mail_content_type( $content_type ): string {
|
||||
|
||||
/**
|
||||
* Sets the mail content type. Used by $this->send_email.
|
||||
*
|
||||
* @param string $content_type The content type to be set for the mail.
|
||||
* @return string The content type that was set.
|
||||
*/
|
||||
public function set_mail_content_type( string $content_type ): string { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found
|
||||
return 'text/html';
|
||||
}
|
||||
|
||||
private function validateData( array $data ) {
|
||||
/**
|
||||
* Validates the provided data array.
|
||||
*
|
||||
* @param array $data The data array to be validated.
|
||||
*
|
||||
* @return void
|
||||
* @throws \InvalidArgumentException If the data is invalid.
|
||||
*/
|
||||
private function validate_data( array $data ) {
|
||||
if ( empty( $data['email'] ) || empty( $data['postId'] ) ) {
|
||||
throw new \InvalidArgumentException(__('Missing required data', 'mailpoet'));
|
||||
throw new \InvalidArgumentException( esc_html__( 'Missing required data', 'mailpoet' ) );
|
||||
}
|
||||
|
||||
if ( ! is_email( $data['email']) ) {
|
||||
throw new \InvalidArgumentException(__('Invalid email', 'mailpoet'));
|
||||
if ( ! is_email( $data['email'] ) ) {
|
||||
throw new \InvalidArgumentException( esc_html__( 'Invalid email', 'mailpoet' ) );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Fetches a post_id post object based on the provided post ID.
|
||||
*
|
||||
* @throws \Exception
|
||||
* @param int $post_id The ID of the post to fetch.
|
||||
* @return \WP_Post The WordPress post object.
|
||||
* @throws \Exception If the post is invalid.
|
||||
*/
|
||||
private function fetchPost( $postId ): \WP_Post {
|
||||
$post = get_post(intval($postId));
|
||||
if (!$post instanceof \WP_Post) {
|
||||
throw new \Exception(__('Invalid post', 'mailpoet'));
|
||||
private function fetch_post( $post_id ): \WP_Post {
|
||||
$post = get_post( intval( $post_id ) );
|
||||
if ( ! $post instanceof \WP_Post ) {
|
||||
throw new \Exception( esc_html__( 'Invalid post', 'mailpoet' ) );
|
||||
}
|
||||
return $post;
|
||||
}
|
||||
|
Reference in New Issue
Block a user