Fetch template content for validation properly

When editing the template, the content property might be a function or empty, and
we need to build content by serializing blocks.
[MAILPOET-6393]
This commit is contained in:
Rostislav Wolny
2025-01-08 12:29:57 +01:00
committed by Aschepikov
parent 79ec5a5ffd
commit afb183bd71
2 changed files with 23 additions and 15 deletions

View File

@ -38,7 +38,7 @@ export const useContentValidation = (): ContentValidationData => {
editedContent:
mapSelect( emailEditorStore ).getEditedEmailContent(),
editedTemplateContent:
mapSelect( emailEditorStore ).getCurrentTemplate()?.content,
mapSelect( emailEditorStore ).getCurrentTemplateContent(),
postTemplateId:
mapSelect( emailEditorStore ).getCurrentTemplate()?.id,
} )

View File

@ -9,6 +9,19 @@ import { storeName } from './constants';
import { State, Feature, EmailTemplate, EmailEditorPostType } from './types';
import { Post } from '@wordpress/core-data/build-types/entity-types/post';
function getContentFromEntity( entity ): string {
if ( entity?.content && typeof entity.content === 'function' ) {
return entity.content( entity ) as string;
}
if ( entity?.blocks ) {
return serialize( entity.blocks );
}
if ( entity?.content ) {
return entity.content as string;
}
return '';
}
export const isFeatureActive = createRegistrySelector(
( select ) =>
( _, feature: Feature ): boolean =>
@ -126,15 +139,7 @@ export const getEditedEmailContent = createRegistrySelector(
| undefined;
if ( record ) {
if ( record?.content && typeof record.content === 'function' ) {
return record.content( record ) as string;
}
if ( record?.blocks ) {
return serialize( record.blocks );
}
if ( record?.content ) {
return record.content as string;
}
return getContentFromEntity( record );
}
return '';
}
@ -199,11 +204,6 @@ export const getEditedPostTemplate = createRegistrySelector(
}
);
export const getTemplateContent = () => {
const template = getEditedPostTemplate();
return template?.content || '';
};
export const getCurrentTemplate = createRegistrySelector( ( select ) => () => {
const isEditingTemplate =
select( editorStore ).getCurrentPostType() === 'wp_template';
@ -221,6 +221,14 @@ export const getCurrentTemplate = createRegistrySelector( ( select ) => () => {
return getEditedPostTemplate();
} );
export const getCurrentTemplateContent = () => {
const template = getCurrentTemplate();
if ( template ) {
return getContentFromEntity( template );
}
return '';
};
export const getGlobalEmailStylesPost = createRegistrySelector(
( select ) => () => {
const postId = select( storeName ).getGlobalStylesPostId();