Remove code related to template themes

[MAILPOET-6335]
This commit is contained in:
Rostislav Wolny
2024-12-10 17:16:47 +01:00
committed by Aschepikov
parent 1e1bec4ce0
commit ebe7151269
7 changed files with 6 additions and 203 deletions

View File

@ -1,55 +0,0 @@
import { useCallback } from '@wordpress/element';
import { useSelect, dispatch } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
import { store as editorStore } from '@wordpress/editor';
import { EmailTheme, storeName } from '../store';
export function useEmailTheme() {
const { templateTheme, templateId } = useSelect( ( select ) => {
const currentPostType = select( editorStore ).getCurrentPostType();
let templateThemeData: EmailTheme = {};
let tId = null;
let tContent = '';
// Edit email post mode
if ( currentPostType === 'mailpoet_email' ) {
const template = select( storeName ).getEditedPostTemplate();
templateThemeData = template?.mailpoet_email_theme || {};
tId = template?.id;
tContent = template?.content;
} else {
// @ts-expect-error TS2322: Type 'string' has no properties in common with type 'EmailTheme'.
templateThemeData =
select( editorStore ).getCurrentPostAttribute(
// @ts-expect-error Expected 0 arguments, but got 1.
'mailpoet_email_theme'
) || {};
}
return {
templateTheme: templateThemeData,
templateId: tId,
templateContent: tContent,
};
}, [] );
const updateTemplateTheme = useCallback(
( newTheme ) => {
if ( ! templateId ) {
return;
}
void dispatch( coreStore ).editEntityRecord(
'postType',
'wp_template',
templateId as string,
{
mailpoet_email_theme: newTheme,
}
);
},
[ templateId ]
);
return {
templateTheme,
updateTemplateTheme,
};
}

View File

@ -1,6 +1,6 @@
<?php
/**
* This file is part of the MailPoet plugin.
* This file is part of the MailPoet Email Editor package.
*
* @package MailPoet\EmailEditor
*/
@ -11,7 +11,6 @@ namespace MailPoet\EmailEditor\Engine\Templates;
use MailPoet\EmailEditor\Engine\Settings_Controller;
use MailPoet\EmailEditor\Engine\Theme_Controller;
use MailPoet\EmailEditor\Validator\Builder;
use WP_Theme_JSON;
/**
* Template_Preview class.
@ -29,28 +28,19 @@ class Template_Preview {
* @var Settings_Controller
*/
private Settings_Controller $settings_controller;
/**
* Provides the templates.
*
* @var Templates
*/
private Templates $templates;
/**
* Template_Preview constructor.
*
* @param Theme_Controller $theme_controller Theme controller.
* @param Settings_Controller $settings_controller Theme controller.
* @param Templates $templates Templates.
*/
public function __construct(
Theme_Controller $theme_controller,
Settings_Controller $settings_controller,
Templates $templates
Settings_Controller $settings_controller
) {
$this->theme_controller = $theme_controller;
$this->settings_controller = $settings_controller;
$this->templates = $templates;
}
/**
@ -71,15 +61,9 @@ class Template_Preview {
/**
* Generates CSS for preview of email theme
* They are applied in the preview BLockPreview in template selection
*
* @param array $template Template data.
*/
public function get_email_theme_preview_css( $template ): string {
$editor_theme = clone $this->theme_controller->get_theme();
$template_theme = $this->templates->get_block_template_theme( $template['id'], $template['wp_id'] );
if ( is_array( $template_theme ) ) {
$editor_theme->merge( new WP_Theme_JSON( $template_theme, 'custom' ) );
}
public function get_email_theme_preview_css(): string {
$editor_theme = clone $this->theme_controller->get_theme();
$editor_settings = $this->settings_controller->get_settings();
$additional_css = '';
foreach ( $editor_settings['styles'] as $style ) {

View File

@ -8,15 +8,13 @@
declare(strict_types = 1);
namespace MailPoet\EmailEditor\Engine\Templates;
use MailPoet\EmailEditor\Engine\Email_Styles_Schema;
use WP_Block_Template;
/**
* Templates class.
*/
class Templates {
const MAILPOET_EMAIL_META_THEME_TYPE = 'mailpoet_email_theme';
const MAILPOET_TEMPLATE_EMPTY_THEME = array( 'version' => 3 ); // The version 3 is important to merge themes correctly.
const MAILPOET_TEMPLATE_EMPTY_THEME = array( 'version' => 3 ); // The version 3 is important to merge themes correctly.
/**
* Provides the utils.
@ -48,12 +46,6 @@ class Templates {
* @var array $templates
*/
private array $templates = array();
/**
* The theme JSON.
*
* @var array $theme_json
*/
private array $theme_json = array();
/**
* Templates constructor.
@ -76,7 +68,6 @@ class Templates {
add_filter( 'get_block_template', array( $this, 'add_block_template_details' ), 10, 1 );
add_filter( 'rest_pre_insert_wp_template', array( $this, 'force_post_content' ), 9, 1 );
$this->initialize_templates();
$this->initialize_api();
}
/**
@ -90,39 +81,6 @@ class Templates {
return $templates[ $template_id ] ?? null;
}
/**
* Get a predefined or user defined theme for a block template.
*
* @param string $template_id The template ID.
* @param int|null $template_wp_id The template WP ID.
* @return array
*/
public function get_block_template_theme( $template_id, $template_wp_id = null ) {
// First check if there is a user updated theme saved.
$theme = $this->get_custom_template_theme( $template_wp_id );
if ( $theme ) {
return $theme;
}
// If there is no user edited theme, look for default template themes in files.
['prefix' => $template_prefix, 'slug' => $template_slug] = $this->utils->get_template_id_parts( $template_id );
if ( $this->plugin_slug !== $template_prefix ) {
return self::MAILPOET_TEMPLATE_EMPTY_THEME;
}
if ( ! isset( $this->theme_json[ $template_slug ] ) ) {
$json_file = $this->template_directory . $template_slug . '.json';
if ( file_exists( $json_file ) ) {
$this->theme_json[ $template_slug ] = json_decode( (string) file_get_contents( $json_file ), true );
}
}
return $this->theme_json[ $template_slug ] ?? self::MAILPOET_TEMPLATE_EMPTY_THEME;
}
/**
* Get block template from file.
*
@ -255,37 +213,6 @@ class Templates {
);
}
/**
* Initialize the API.
*/
private function initialize_api(): void {
register_post_meta(
'wp_template',
self::MAILPOET_EMAIL_META_THEME_TYPE,
array(
'show_in_rest' => array(
'schema' => ( new Email_Styles_Schema() )->get_schema(),
),
'single' => true,
'type' => 'object',
'default' => self::MAILPOET_TEMPLATE_EMPTY_THEME,
)
);
register_rest_field(
'wp_template',
self::MAILPOET_EMAIL_META_THEME_TYPE,
array(
'get_callback' => function ( $item ) {
return $this->get_block_template_theme( $item['id'], $item['wp_id'] );
},
'update_callback' => function ( $value, $template ) {
return update_post_meta( $template->wp_id, self::MAILPOET_EMAIL_META_THEME_TYPE, $value );
},
'schema' => ( new Email_Styles_Schema() )->get_schema(),
)
);
}
/**
* Gets block templates indexed by ID.
*
@ -377,21 +304,4 @@ class Templates {
$custom_templates
);
}
/**
* Get the custom theme for a template.
*
* @param int|null $template_wp_id The template ID.
* @return array|null
*/
private function get_custom_template_theme( ?int $template_wp_id ): ?array {
if ( ! $template_wp_id ) {
return null;
}
$theme = get_post_meta( $template_wp_id, self::MAILPOET_EMAIL_META_THEME_TYPE, true );
if ( is_array( $theme ) && isset( $theme['styles'] ) ) {
return $theme;
}
return null;
}
}

View File

@ -1,14 +0,0 @@
{
"version": 3,
"styles": {
"spacing": {
"blockGap": "10px",
"padding": {
"top": "var:preset|spacing|20",
"right": "var:preset|spacing|10",
"bottom": "var:preset|spacing|20",
"left": "var:preset|spacing|10"
}
}
}
}

View File

@ -33,13 +33,7 @@ class Template_Preview_Test extends \MailPoetTest {
* @return void
*/
public function testItCanGetThemePreviewCss(): void {
$template_id = 'mailpoet/mailpoet//simple-light';
$result = $this->template_preview->get_email_theme_preview_css(
array(
'id' => $template_id,
'wp_id' => null,
)
);
$result = $this->template_preview->get_email_theme_preview_css();
verify( $result )->stringContainsString( 'Styles for the email editor.' );
verify( $result )->stringContainsString( 'is-layout-email-flex' );

View File

@ -45,21 +45,6 @@ class Templates_Test extends \MailPoetTest {
verify( $template->description )->equals( 'A general template for emails.' );
}
/**
* Test is can fetch template theme
*
* @return void
*/
public function testItCanFetchTemplateTheme(): void {
$template_id = 'mailpoet/mailpoet//simple-light';
$theme = $this->templates->get_block_template_theme( $template_id );
verify( $theme )->arrayCount( 2 );
verify( $theme['styles'] )->isArray();
verify( $theme['styles']['spacing'] )->isArray();
verify( $theme['styles']['spacing']['blockGap'] )->equals( '10px' );
}
/**
* Test it can add block templates
*

View File

@ -209,7 +209,6 @@ abstract class MailPoetTest extends \Codeception\TestCase\Test { // phpcs:ignore
return new Template_Preview(
$container->get( Theme_Controller::class ),
$container->get( Settings_Controller::class ),
$container->get( Templates::class ),
);
}
);