From 5f568de4306d310a2683922c729df1dcbdb3f2e4 Mon Sep 17 00:00:00 2001 From: efuller Date: Wed, 26 Jun 2024 08:52:35 -0400 Subject: [PATCH 1/5] Reset single send form if selected template does not have a post block --- hooks/useNewsletterMeta/index.ts | 16 ++++++++++++++++ plugins/newsletter-from-post/email-settings.tsx | 9 +++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/hooks/useNewsletterMeta/index.ts b/hooks/useNewsletterMeta/index.ts index d4e41c1e..1dd01ad0 100644 --- a/hooks/useNewsletterMeta/index.ts +++ b/hooks/useNewsletterMeta/index.ts @@ -1,4 +1,5 @@ import { usePostMeta } from '@alleyinteractive/block-editor-tools'; +import { useCallback } from '@wordpress/element'; export interface NewsletterMeta { type: string; @@ -29,6 +30,20 @@ function useNewsletterMeta() { nb_newsletter_sent_breaking_post_id: sentBreakingPostId = [], } = meta; + const resetMeta = useCallback(() => { + setMeta({ + nb_breaking_email_type: '', + nb_breaking_template: 0, + nb_breaking_from_name: '', + nb_breaking_header_img: 0, + nb_breaking_content: '', + nb_breaking_subject: '', + nb_breaking_preview: '', + nb_breaking_list: [], + nb_breaking_should_send: false, + }); + }, [setMeta]); + return { meta: { type, @@ -42,6 +57,7 @@ function useNewsletterMeta() { content, sentBreakingPostId, }, + resetMeta, setMeta, }; } diff --git a/plugins/newsletter-from-post/email-settings.tsx b/plugins/newsletter-from-post/email-settings.tsx index 77160da6..a19d3017 100644 --- a/plugins/newsletter-from-post/email-settings.tsx +++ b/plugins/newsletter-from-post/email-settings.tsx @@ -33,7 +33,7 @@ interface Window { function EmailSettings() { const [fetched, setFetched] = useState(false); - const { meta, setMeta } = useNewsletterMeta(); + const { meta, resetMeta, setMeta } = useNewsletterMeta(); const { emailListOptions, selectedEmailList } = useEmailLists(); const manualSubject = meta.subject !== ''; const manualPreview = meta.preview !== ''; @@ -76,13 +76,18 @@ function EmailSettings() { const blocks = parse(html); const postIndex = blocks.findIndex((block) => block.name === 'wp-newsletter-builder/post'); + if (postIndex === -1) { + resetMeta(); + return; + } + blocks[postIndex] = createBlock('wp-newsletter-builder/post', { ...blocks[postIndex].attributes, postId, }, blocks[postIndex].innerBlocks); setMeta({ nb_breaking_content: serialize(blocks) }); - }, [postId, setMeta]); + }, [postId, resetMeta, setMeta]); const areRequiredFieldsSet = meta.type === '' || meta.template === '' From 21c713c80f5958f6871379a22c977812191808be Mon Sep 17 00:00:00 2001 From: efuller Date: Wed, 26 Jun 2024 10:17:39 -0400 Subject: [PATCH 2/5] Add in a guard to only allow valid templates A valid template much have at least one single post block in it. --- hooks/useNewsletterMeta/index.ts | 3 --- .../components/invalid-template.tsx | 17 +++++++++++++++++ plugins/newsletter-from-post/email-settings.tsx | 10 +++++++++- 3 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 plugins/newsletter-from-post/components/invalid-template.tsx diff --git a/hooks/useNewsletterMeta/index.ts b/hooks/useNewsletterMeta/index.ts index 1dd01ad0..8bc3e1a0 100644 --- a/hooks/useNewsletterMeta/index.ts +++ b/hooks/useNewsletterMeta/index.ts @@ -32,10 +32,7 @@ function useNewsletterMeta() { const resetMeta = useCallback(() => { setMeta({ - nb_breaking_email_type: '', nb_breaking_template: 0, - nb_breaking_from_name: '', - nb_breaking_header_img: 0, nb_breaking_content: '', nb_breaking_subject: '', nb_breaking_preview: '', diff --git a/plugins/newsletter-from-post/components/invalid-template.tsx b/plugins/newsletter-from-post/components/invalid-template.tsx new file mode 100644 index 00000000..281b140b --- /dev/null +++ b/plugins/newsletter-from-post/components/invalid-template.tsx @@ -0,0 +1,17 @@ +import { __ } from '@wordpress/i18n'; + +interface InvalidTemplateProps { + invalid: boolean; +} + +function InvalidTemplate({ invalid }: InvalidTemplateProps) { + return ( + <> + {invalid ? ( +

{__('Invalid Template: Template must have at least one Newsletter Single Post Block.', 'wp-newsletter-builder')}

+ ) : null} + + ); +} + +export default InvalidTemplate; diff --git a/plugins/newsletter-from-post/email-settings.tsx b/plugins/newsletter-from-post/email-settings.tsx index a19d3017..17d81f0d 100644 --- a/plugins/newsletter-from-post/email-settings.tsx +++ b/plugins/newsletter-from-post/email-settings.tsx @@ -16,8 +16,9 @@ import NewsletterSpinner from '@/components/newsletterSpinner'; import useEmailLists, { Option } from '@/hooks/useEmailLists'; import useNewsletterMeta from '@/hooks/useNewsletterMeta'; -import RequiredFields from './components/required-fields'; import EmailTypeSelector from '../../components/emailTypeSelector'; +import InvalidTemplate from './components/invalid-template'; +import RequiredFields from './components/required-fields'; interface CoreEditor { getEditedPostAttribute: (attribute: string) => string; @@ -33,6 +34,7 @@ interface Window { function EmailSettings() { const [fetched, setFetched] = useState(false); + const [invalidTemplate, setInvalidTemplate] = useState(false); const { meta, resetMeta, setMeta } = useNewsletterMeta(); const { emailListOptions, selectedEmailList } = useEmailLists(); const manualSubject = meta.subject !== ''; @@ -78,6 +80,7 @@ function EmailSettings() { if (postIndex === -1) { resetMeta(); + setInvalidTemplate(true); return; } @@ -86,6 +89,7 @@ function EmailSettings() { postId, }, blocks[postIndex].innerBlocks); + setInvalidTemplate(false); setMeta({ nb_breaking_content: serialize(blocks) }); }, [postId, resetMeta, setMeta]); @@ -186,7 +190,11 @@ function EmailSettings() { onChange={(value) => { setMeta({ nb_breaking_should_send: value }); }} disabled={areRequiredFieldsSet} /> + +
+

{__('Newsletter Validation', 'wp-newsletter-builder')}

+
From bd8e455f6baae2386cc3b992fb467e38287f5cf3 Mon Sep 17 00:00:00 2001 From: efuller Date: Wed, 26 Jun 2024 11:27:53 -0400 Subject: [PATCH 3/5] Format typeHandler function --- plugins/newsletter-from-post/email-settings.tsx | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/plugins/newsletter-from-post/email-settings.tsx b/plugins/newsletter-from-post/email-settings.tsx index 17d81f0d..970a7f08 100644 --- a/plugins/newsletter-from-post/email-settings.tsx +++ b/plugins/newsletter-from-post/email-settings.tsx @@ -129,7 +129,12 @@ function EmailSettings() { > { setMeta({ nb_breaking_email_type: newType }); }} + typeHandler={ + (newType) => { + setMeta({ nb_breaking_email_type: newType }); + setInvalidTemplate(false); + } + } imageHandler={(newImage) => { setMeta({ nb_breaking_header_img: newImage }); }} typeValue={meta.type} templateHandler={(newTemplate) => { setMeta({ nb_breaking_template: newTemplate }); }} From 8d81e9b01ca8458f9ff4c34b2ebae6ef6604da50 Mon Sep 17 00:00:00 2001 From: efuller Date: Wed, 26 Jun 2024 11:30:52 -0400 Subject: [PATCH 4/5] Rename resetMeta to resetTemplate --- hooks/useNewsletterMeta/index.ts | 4 ++-- plugins/newsletter-from-post/email-settings.tsx | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/hooks/useNewsletterMeta/index.ts b/hooks/useNewsletterMeta/index.ts index 8bc3e1a0..e714beee 100644 --- a/hooks/useNewsletterMeta/index.ts +++ b/hooks/useNewsletterMeta/index.ts @@ -30,7 +30,7 @@ function useNewsletterMeta() { nb_newsletter_sent_breaking_post_id: sentBreakingPostId = [], } = meta; - const resetMeta = useCallback(() => { + const resetTemplate = useCallback(() => { setMeta({ nb_breaking_template: 0, nb_breaking_content: '', @@ -54,7 +54,7 @@ function useNewsletterMeta() { content, sentBreakingPostId, }, - resetMeta, + resetTemplate, setMeta, }; } diff --git a/plugins/newsletter-from-post/email-settings.tsx b/plugins/newsletter-from-post/email-settings.tsx index 970a7f08..2a171f37 100644 --- a/plugins/newsletter-from-post/email-settings.tsx +++ b/plugins/newsletter-from-post/email-settings.tsx @@ -35,7 +35,7 @@ interface Window { function EmailSettings() { const [fetched, setFetched] = useState(false); const [invalidTemplate, setInvalidTemplate] = useState(false); - const { meta, resetMeta, setMeta } = useNewsletterMeta(); + const { meta, resetTemplate, setMeta } = useNewsletterMeta(); const { emailListOptions, selectedEmailList } = useEmailLists(); const manualSubject = meta.subject !== ''; const manualPreview = meta.preview !== ''; @@ -79,7 +79,7 @@ function EmailSettings() { const postIndex = blocks.findIndex((block) => block.name === 'wp-newsletter-builder/post'); if (postIndex === -1) { - resetMeta(); + resetTemplate(); setInvalidTemplate(true); return; } @@ -91,7 +91,7 @@ function EmailSettings() { setInvalidTemplate(false); setMeta({ nb_breaking_content: serialize(blocks) }); - }, [postId, resetMeta, setMeta]); + }, [postId, resetTemplate, setMeta]); const areRequiredFieldsSet = meta.type === '' || meta.template === '' From 017e82482f0096f36ac0106e702e895b4efd1da5 Mon Sep 17 00:00:00 2001 From: efuller Date: Wed, 26 Jun 2024 12:12:41 -0400 Subject: [PATCH 5/5] Version bump --- README.md | 2 +- plugin.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 60bb24dc..7ba5622b 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Contributors: alleyinteractive Tags: alleyinteractive, wp-newsletter-builder -Stable tag: 0.3.31 +Stable tag: 0.3.32 Requires at least: 6.2 diff --git a/plugin.php b/plugin.php index c20e3edf..b278a050 100644 --- a/plugin.php +++ b/plugin.php @@ -3,7 +3,7 @@ * Plugin Name: Newsletter Builder * Plugin URI: https://github.com/alleyinteractive/wp-newsletter-builder * Description: Interface to manage email newsletters - * Version: 0.3.31 + * Version: 0.3.32 * Author: Alley Interactive * Author URI: https://github.com/alleyinteractive/wp-newsletter-builder * Requires at least: 6.2