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 mailpoet/package.json --cwd mailpoet
|
||||||
npx lint-staged -c package.json
|
npx lint-staged -c package.json
|
||||||
npx lint-staged -c packages/js/email-editor/package.json --cwd packages/js/email-editor
|
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": {
|
"scripts": {
|
||||||
"unit-test": "vendor/bin/codecept run unit",
|
"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",
|
"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.
|
* @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.
|
* Email_Api_Controller constructor.
|
||||||
|
*
|
||||||
|
* @param Send_Preview_Email $send_preview_email send_preview_email.
|
||||||
*/
|
*/
|
||||||
public function __construct(
|
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.
|
// 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 {
|
public function send_preview_email_data( WP_REST_Request $request ): WP_REST_Response {
|
||||||
$data = $request->get_params();
|
$data = $request->get_params();
|
||||||
try {
|
try {
|
||||||
$result = $this->send_Preview_Email->sendPreviewEmail($data);
|
$result = $this->send_preview_email->send_preview_email( $data );
|
||||||
return new WP_REST_Response(['success' => true, 'result' => $result], 200);
|
return new WP_REST_Response(
|
||||||
|
array(
|
||||||
|
'success' => true,
|
||||||
|
'result' => $result,
|
||||||
|
),
|
||||||
|
200
|
||||||
|
);
|
||||||
} catch ( \Exception $exception ) {
|
} 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->extend_email_post_api();
|
||||||
$this->settings_controller->init();
|
$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() {
|
public function register_email_editor_api_routes() {
|
||||||
register_rest_route('mailpoet-email-editor/v1', '/send_preview_email', [
|
register_rest_route(
|
||||||
'methods' => 'POST',
|
'mailpoet-email-editor/v1',
|
||||||
'callback' => array( $this->email_api_controller, 'send_preview_email_data' ),
|
'/send_preview_email',
|
||||||
'permission_callback' => function() {
|
array(
|
||||||
return current_user_can('edit_posts');
|
'methods' => 'POST',
|
||||||
}
|
'callback' => array( $this->email_api_controller, 'send_preview_email_data' ),
|
||||||
]);
|
'permission_callback' => function () {
|
||||||
|
return current_user_can( 'edit_posts' );
|
||||||
|
},
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -11,79 +11,124 @@ namespace MailPoet\EmailEditor\Integrations\Utils;
|
|||||||
|
|
||||||
use MailPoet\EmailEditor\Engine\Renderer\Renderer;
|
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 {
|
class Send_Preview_Email {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Instance of the Renderer class used for rendering the editor emails.
|
||||||
|
*
|
||||||
|
* @var Renderer $renderer
|
||||||
|
*/
|
||||||
private Renderer $renderer;
|
private Renderer $renderer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send_Preview_Email constructor.
|
* Send_Preview_Email constructor.
|
||||||
|
*
|
||||||
|
* @param Renderer $renderer renderer instance.
|
||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
Renderer $renderer
|
Renderer $renderer
|
||||||
) {
|
) {
|
||||||
$this->renderer = $renderer;
|
$this->renderer = $renderer;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sends preview email
|
* Sends a preview email.
|
||||||
* @throws \Exception
|
*
|
||||||
|
* @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 {
|
public function send_preview_email( array $data ): bool {
|
||||||
$this->validateData($data);
|
$this->validate_data( $data );
|
||||||
|
|
||||||
$email = $data['email'];
|
$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');
|
$subject = isset( $post->post_title ) ? $post->post_title : __( 'Email Preview', 'mailpoet' );
|
||||||
$language = get_bloginfo('language');
|
$language = get_bloginfo( 'language' );
|
||||||
|
|
||||||
$renderedData = $this->renderer->render(
|
$rendered_data = $this->renderer->render(
|
||||||
$post,
|
$post,
|
||||||
$subject,
|
$subject,
|
||||||
__('Preview', 'mailpoet'),
|
__( 'Preview', 'mailpoet' ),
|
||||||
$language
|
$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 );
|
$result = wp_mail( $to, $subject, $body );
|
||||||
|
|
||||||
// Reset content-type to avoid conflicts
|
// Reset content-type to avoid conflicts.
|
||||||
remove_filter( 'wp_mail_content_type', [$this, 'set_mail_content_type'] );
|
remove_filter( 'wp_mail_content_type', array( $this, 'set_mail_content_type' ) );
|
||||||
|
|
||||||
return $result;
|
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';
|
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'] ) ) {
|
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']) ) {
|
if ( ! is_email( $data['email'] ) ) {
|
||||||
throw new \InvalidArgumentException(__('Invalid email', 'mailpoet'));
|
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 {
|
private function fetch_post( $post_id ): \WP_Post {
|
||||||
$post = get_post(intval($postId));
|
$post = get_post( intval( $post_id ) );
|
||||||
if (!$post instanceof \WP_Post) {
|
if ( ! $post instanceof \WP_Post ) {
|
||||||
throw new \Exception(__('Invalid post', 'mailpoet'));
|
throw new \Exception( esc_html__( 'Invalid post', 'mailpoet' ) );
|
||||||
}
|
}
|
||||||
return $post;
|
return $post;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user