Migrate email editor email-editor to WP Coding Standard

[MAILPOET-6240]
This commit is contained in:
Jan Lysý
2024-10-16 19:09:14 +02:00
committed by Jan Lysý
parent 37b3cfbbc2
commit 062f504e14

View File

@@ -1,5 +1,11 @@
<?php declare(strict_types = 1); <?php
/**
* This file is part of the MailPoet plugin.
*
* @package MailPoet\EmailEditor
*/
declare(strict_types = 1);
namespace MailPoet\EmailEditor\Engine; namespace MailPoet\EmailEditor\Engine;
use MailPoet\EmailEditor\Engine\Patterns\Patterns; use MailPoet\EmailEditor\Engine\Patterns\Patterns;
@@ -9,126 +15,209 @@ use WP_Post;
use WP_Theme_JSON; use WP_Theme_JSON;
/** /**
* Email editor class.
*
* @phpstan-type EmailPostType array{name: string, args: array, meta: array{key: string, args: array}[]} * @phpstan-type EmailPostType array{name: string, args: array, meta: array{key: string, args: array}[]}
* See register_post_type for details about EmailPostType args. * See register_post_type for details about EmailPostType args.
*/ */
class Email_Editor { class Email_Editor {
public const MAILPOET_EMAIL_META_THEME_TYPE = 'mailpoet_email_theme'; public const MAILPOET_EMAIL_META_THEME_TYPE = 'mailpoet_email_theme';
private Email_Api_Controller $emailApiController; /**
* Property for the email API controller.
*
* @var Email_Api_Controller Email API controller.
*/
private Email_Api_Controller $email_api_controller;
/**
* Property for the templates.
*
* @var Templates Templates.
*/
private Templates $templates; private Templates $templates;
private Template_Preview $templatePreview; /**
* Property for the template preview.
*
* @var Template_Preview Template preview.
*/
private Template_Preview $template_preview;
/**
* Property for the patterns.
*
* @var Patterns Patterns.
*/
private Patterns $patterns; private Patterns $patterns;
private Settings_Controller $settingsController; /**
* Property for the settings controller.
*
* @var Settings_Controller Settings controller.
*/
private Settings_Controller $settings_controller;
/**
* Constructor.
*
* @param Email_Api_Controller $email_api_controller Email API controller.
* @param Templates $templates Templates.
* @param Template_Preview $template_preview Template preview.
* @param Patterns $patterns Patterns.
* @param Settings_Controller $settings_controller Settings controller.
*/
public function __construct( public function __construct(
Email_Api_Controller $emailApiController, Email_Api_Controller $email_api_controller,
Templates $templates, Templates $templates,
Template_Preview $templatePreview, Template_Preview $template_preview,
Patterns $patterns, Patterns $patterns,
Settings_Controller $settingsController Settings_Controller $settings_controller
) { ) {
$this->emailApiController = $emailApiController; $this->email_api_controller = $email_api_controller;
$this->templates = $templates; $this->templates = $templates;
$this->templatePreview = $templatePreview; $this->template_preview = $template_preview;
$this->patterns = $patterns; $this->patterns = $patterns;
$this->settingsController = $settingsController; $this->settings_controller = $settings_controller;
} }
/**
* Initialize the email editor.
*
* @return void
*/
public function initialize(): void { public function initialize(): void {
do_action( 'mailpoet_email_editor_initialized' ); do_action( 'mailpoet_email_editor_initialized' );
add_filter( 'mailpoet_email_editor_rendering_theme_styles', array( $this, 'extendEmailThemeStyles' ), 10, 2 ); add_filter( 'mailpoet_email_editor_rendering_theme_styles', array( $this, 'extend_email_theme_styles' ), 10, 2 );
$this->registerBlockTemplates(); $this->register_block_templates();
$this->registerBlockPatterns(); $this->register_block_patterns();
$this->registerEmailPostTypes(); $this->register_wmail_post_types();
$this->registerEmailPostSendStatus(); $this->register_email_post_send_status();
$isEditorPage = apply_filters( 'mailpoet_is_email_editor_page', false ); $is_editor_page = apply_filters( 'mailpoet_is_email_editor_page', false );
if ( $isEditorPage ) { if ( $is_editor_page ) {
$this->extendEmailPostApi(); $this->extend_email_post_api();
$this->settingsController->init(); $this->settings_controller->init();
} }
} }
private function registerBlockTemplates(): void { /**
// Since we cannot currently disable blocks in the editor for specific templates, disable templates when viewing site editor. @see https://github.com/WordPress/gutenberg/issues/41062 * Register block templates.
if ( strstr( wp_unslash( $_SERVER['REQUEST_URI'] ?? '' ), 'site-editor.php' ) === false ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized *
* @return void
*/
private function register_block_templates(): void {
// Since we cannot currently disable blocks in the editor for specific templates, disable templates when viewing site editor. @see https://github.com/WordPress/gutenberg/issues/41062.
if ( strstr( sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ?? '' ) ), 'site-editor.php' ) === false ) {
$this->templates->initialize(); $this->templates->initialize();
$this->templatePreview->initialize(); $this->template_preview->initialize();
} }
} }
private function registerBlockPatterns(): void { /**
* Register block patterns.
*
* @return void
*/
private function register_block_patterns(): void {
$this->patterns->initialize(); $this->patterns->initialize();
} }
/** /**
* Register all custom post types that should be edited via the email editor * Register all custom post types that should be edited via the email editor
* The post types are added via mailpoet_email_editor_post_types filter. * The post types are added via mailpoet_email_editor_post_types filter.
*
* @return void
*/ */
private function registerEmailPostTypes(): void { private function register_wmail_post_types(): void {
foreach ( $this->getPostTypes() as $postType ) { foreach ( $this->get_post_types() as $post_type ) {
register_post_type( register_post_type(
$postType['name'], $post_type['name'],
array_merge( $this->getDefaultEmailPostArgs(), $postType['args'] ) array_merge( $this->get_default_email_post_args(), $post_type['args'] )
); );
} }
} }
/** /**
* Returns the email post types.
*
* @return array
* @phpstan-return EmailPostType[] * @phpstan-return EmailPostType[]
*/ */
private function getPostTypes(): array { private function get_post_types(): array {
$postTypes = array(); $post_types = array();
return apply_filters( 'mailpoet_email_editor_post_types', $postTypes ); return apply_filters( 'mailpoet_email_editor_post_types', $post_types );
} }
private function getDefaultEmailPostArgs(): array { /**
* Returns the default arguments for email post types.
*
* @return array
*/
private function get_default_email_post_args(): array {
return array( return array(
'public' => false, 'public' => false,
'hierarchical' => false, 'hierarchical' => false,
'show_ui' => true, 'show_ui' => true,
'show_in_menu' => false, 'show_in_menu' => false,
'show_in_nav_menus' => false, 'show_in_nav_menus' => false,
'supports' => array( 'editor', 'title', 'custom-fields' ), // 'custom-fields' is required for loading meta fields via API 'supports' => array( 'editor', 'title', 'custom-fields' ), // 'custom-fields' is required for loading meta fields via API.
'has_archive' => true, 'has_archive' => true,
'show_in_rest' => true, // Important to enable Gutenberg editor 'show_in_rest' => true, // Important to enable Gutenberg editor.
); );
} }
private function registerEmailPostSendStatus(): void { /**
* Register the 'sent' post status for emails.
*
* @return void
*/
private function register_email_post_send_status(): void {
register_post_status( register_post_status(
'sent', 'sent',
array( array(
'public' => false, 'public' => false,
'exclude_from_search' => true, 'exclude_from_search' => true,
'internal' => true, // for now, we hide it, if we use the status in the listings we may flip this and following values 'internal' => true, // for now, we hide it, if we use the status in the listings we may flip this and following values.
'show_in_admin_all_list' => false, 'show_in_admin_all_list' => false,
'show_in_admin_status_list' => false, 'show_in_admin_status_list' => false,
) )
); );
} }
public function extendEmailPostApi() { /**
$emailPostTypes = array_column( $this->getPostTypes(), 'name' ); * Extends the email post types with email specific data.
*
* @return void
*/
public function extend_email_post_api() {
$email_post_types = array_column( $this->get_post_types(), 'name' );
register_rest_field( register_rest_field(
$emailPostTypes, $email_post_types,
'email_data', 'email_data',
array( array(
'get_callback' => array( $this->emailApiController, 'get_email_data'), 'get_callback' => array( $this->email_api_controller, 'get_email_data' ),
'update_callback' => array( $this->emailApiController, 'save_email_data'), 'update_callback' => array( $this->email_api_controller, 'save_email_data' ),
'schema' => $this->emailApiController->get_email_data_schema(), 'schema' => $this->email_api_controller->get_email_data_schema(),
) )
); );
} }
public function getEmailThemeDataSchema(): array { /**
* Returns the schema for email theme data.
*
* @return array
*/
public function get_email_theme_data_schema(): array {
return ( new Email_Styles_Schema() )->get_schema(); return ( new Email_Styles_Schema() )->get_schema();
} }
public function extendEmailThemeStyles( WP_Theme_JSON $theme, WP_Post $post ): WP_Theme_JSON { /**
$emailTheme = get_post_meta( $post->ID, self::MAILPOET_EMAIL_META_THEME_TYPE, true ); * Extends the email theme styles with the email specific styles.
if ( $emailTheme && is_array( $emailTheme ) ) { *
$theme->merge( new WP_Theme_JSON( $emailTheme ) ); * @param WP_Theme_JSON $theme Email theme styles.
* @param WP_Post $post Email post object.
* @return WP_Theme_JSON
*/
public function extend_email_theme_styles( WP_Theme_JSON $theme, WP_Post $post ): WP_Theme_JSON {
$email_theme = get_post_meta( $post->ID, self::MAILPOET_EMAIL_META_THEME_TYPE, true );
if ( $email_theme && is_array( $email_theme ) ) {
$theme->merge( new WP_Theme_JSON( $email_theme ) );
} }
return $theme; return $theme;
} }