Change coding style in email editor package to WordPress
This commit contains automated changes made with phpcbf command from CodeSniffer package. [MAILPOET-6240]
This commit is contained in:
@@ -13,116 +13,123 @@ use WP_Theme_JSON;
|
||||
* See register_post_type for details about EmailPostType args.
|
||||
*/
|
||||
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;
|
||||
private Templates $templates;
|
||||
private Template_Preview $templatePreview;
|
||||
private Patterns $patterns;
|
||||
private Settings_Controller $settingsController;
|
||||
private Email_Api_Controller $emailApiController;
|
||||
private Templates $templates;
|
||||
private Template_Preview $templatePreview;
|
||||
private Patterns $patterns;
|
||||
private Settings_Controller $settingsController;
|
||||
|
||||
public function __construct(
|
||||
Email_Api_Controller $emailApiController,
|
||||
Templates $templates,
|
||||
Template_Preview $templatePreview,
|
||||
Patterns $patterns,
|
||||
Settings_Controller $settingsController
|
||||
) {
|
||||
$this->emailApiController = $emailApiController;
|
||||
$this->templates = $templates;
|
||||
$this->templatePreview = $templatePreview;
|
||||
$this->patterns = $patterns;
|
||||
$this->settingsController = $settingsController;
|
||||
}
|
||||
public function __construct(
|
||||
Email_Api_Controller $emailApiController,
|
||||
Templates $templates,
|
||||
Template_Preview $templatePreview,
|
||||
Patterns $patterns,
|
||||
Settings_Controller $settingsController
|
||||
) {
|
||||
$this->emailApiController = $emailApiController;
|
||||
$this->templates = $templates;
|
||||
$this->templatePreview = $templatePreview;
|
||||
$this->patterns = $patterns;
|
||||
$this->settingsController = $settingsController;
|
||||
}
|
||||
|
||||
public function initialize(): void {
|
||||
do_action('mailpoet_email_editor_initialized');
|
||||
add_filter('mailpoet_email_editor_rendering_theme_styles', [$this, 'extendEmailThemeStyles'], 10, 2);
|
||||
$this->registerBlockTemplates();
|
||||
$this->registerBlockPatterns();
|
||||
$this->registerEmailPostTypes();
|
||||
$this->registerEmailPostSendStatus();
|
||||
$isEditorPage = apply_filters('mailpoet_is_email_editor_page', false);
|
||||
if ($isEditorPage) {
|
||||
$this->extendEmailPostApi();
|
||||
$this->settingsController->init();
|
||||
}
|
||||
}
|
||||
public function initialize(): void {
|
||||
do_action( 'mailpoet_email_editor_initialized' );
|
||||
add_filter( 'mailpoet_email_editor_rendering_theme_styles', array( $this, 'extendEmailThemeStyles' ), 10, 2 );
|
||||
$this->registerBlockTemplates();
|
||||
$this->registerBlockPatterns();
|
||||
$this->registerEmailPostTypes();
|
||||
$this->registerEmailPostSendStatus();
|
||||
$isEditorPage = apply_filters( 'mailpoet_is_email_editor_page', false );
|
||||
if ( $isEditorPage ) {
|
||||
$this->extendEmailPostApi();
|
||||
$this->settingsController->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
|
||||
if (strstr(wp_unslash($_SERVER['REQUEST_URI'] ?? ''), 'site-editor.php') === false) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
|
||||
$this->templates->initialize();
|
||||
$this->templatePreview->initialize();
|
||||
}
|
||||
}
|
||||
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
|
||||
if ( strstr( wp_unslash( $_SERVER['REQUEST_URI'] ?? '' ), 'site-editor.php' ) === false ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
|
||||
$this->templates->initialize();
|
||||
$this->templatePreview->initialize();
|
||||
}
|
||||
}
|
||||
|
||||
private function registerBlockPatterns(): void {
|
||||
$this->patterns->initialize();
|
||||
}
|
||||
private function registerBlockPatterns(): void {
|
||||
$this->patterns->initialize();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
private function registerEmailPostTypes(): void {
|
||||
foreach ($this->getPostTypes() as $postType) {
|
||||
register_post_type(
|
||||
$postType['name'],
|
||||
array_merge($this->getDefaultEmailPostArgs(), $postType['args'])
|
||||
);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
private function registerEmailPostTypes(): void {
|
||||
foreach ( $this->getPostTypes() as $postType ) {
|
||||
register_post_type(
|
||||
$postType['name'],
|
||||
array_merge( $this->getDefaultEmailPostArgs(), $postType['args'] )
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @phpstan-return EmailPostType[]
|
||||
*/
|
||||
private function getPostTypes(): array {
|
||||
$postTypes = [];
|
||||
return apply_filters('mailpoet_email_editor_post_types', $postTypes);
|
||||
}
|
||||
/**
|
||||
* @phpstan-return EmailPostType[]
|
||||
*/
|
||||
private function getPostTypes(): array {
|
||||
$postTypes = array();
|
||||
return apply_filters( 'mailpoet_email_editor_post_types', $postTypes );
|
||||
}
|
||||
|
||||
private function getDefaultEmailPostArgs(): array {
|
||||
return [
|
||||
'public' => false,
|
||||
'hierarchical' => false,
|
||||
'show_ui' => true,
|
||||
'show_in_menu' => false,
|
||||
'show_in_nav_menus' => false,
|
||||
'supports' => ['editor', 'title', 'custom-fields'], // 'custom-fields' is required for loading meta fields via API
|
||||
'has_archive' => true,
|
||||
'show_in_rest' => true, // Important to enable Gutenberg editor
|
||||
];
|
||||
}
|
||||
private function getDefaultEmailPostArgs(): array {
|
||||
return array(
|
||||
'public' => false,
|
||||
'hierarchical' => false,
|
||||
'show_ui' => true,
|
||||
'show_in_menu' => false,
|
||||
'show_in_nav_menus' => false,
|
||||
'supports' => array( 'editor', 'title', 'custom-fields' ), // 'custom-fields' is required for loading meta fields via API
|
||||
'has_archive' => true,
|
||||
'show_in_rest' => true, // Important to enable Gutenberg editor
|
||||
);
|
||||
}
|
||||
|
||||
private function registerEmailPostSendStatus(): void {
|
||||
register_post_status('sent', [
|
||||
'public' => false,
|
||||
'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
|
||||
'show_in_admin_all_list' => false,
|
||||
'show_in_admin_status_list' => false,
|
||||
]);
|
||||
}
|
||||
private function registerEmailPostSendStatus(): void {
|
||||
register_post_status(
|
||||
'sent',
|
||||
array(
|
||||
'public' => false,
|
||||
'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
|
||||
'show_in_admin_all_list' => false,
|
||||
'show_in_admin_status_list' => false,
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public function extendEmailPostApi() {
|
||||
$emailPostTypes = array_column($this->getPostTypes(), 'name');
|
||||
register_rest_field($emailPostTypes, 'email_data', [
|
||||
'get_callback' => [$this->emailApiController, 'getEmailData'],
|
||||
'update_callback' => [$this->emailApiController, 'saveEmailData'],
|
||||
'schema' => $this->emailApiController->getEmailDataSchema(),
|
||||
]);
|
||||
}
|
||||
public function extendEmailPostApi() {
|
||||
$emailPostTypes = array_column( $this->getPostTypes(), 'name' );
|
||||
register_rest_field(
|
||||
$emailPostTypes,
|
||||
'email_data',
|
||||
array(
|
||||
'get_callback' => array( $this->emailApiController, 'getEmailData' ),
|
||||
'update_callback' => array( $this->emailApiController, 'saveEmailData' ),
|
||||
'schema' => $this->emailApiController->getEmailDataSchema(),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public function getEmailThemeDataSchema(): array {
|
||||
return (new Email_Styles_Schema())->getSchema();
|
||||
}
|
||||
public function getEmailThemeDataSchema(): array {
|
||||
return ( new Email_Styles_Schema() )->getSchema();
|
||||
}
|
||||
|
||||
public function extendEmailThemeStyles(WP_Theme_JSON $theme, WP_Post $post): WP_Theme_JSON {
|
||||
$emailTheme = get_post_meta($post->ID, Email_Editor::MAILPOET_EMAIL_META_THEME_TYPE, true);
|
||||
if ($emailTheme && is_array($emailTheme)) {
|
||||
$theme->merge(new WP_Theme_JSON($emailTheme));
|
||||
}
|
||||
return $theme;
|
||||
}
|
||||
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 );
|
||||
if ( $emailTheme && is_array( $emailTheme ) ) {
|
||||
$theme->merge( new WP_Theme_JSON( $emailTheme ) );
|
||||
}
|
||||
return $theme;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user