Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Email preview: fix sending preview if content in the editor is different from latest in the database #34419

Merged
merged 4 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: bugfix

Fix sending email preview if content in the editor is different from latest in the database.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
TextControl,
Icon,
} from '@wordpress/components';
import { useSelect } from '@wordpress/data';
import { useSelect, useDispatch } from '@wordpress/data';
import { store as editorStore } from '@wordpress/editor';
import { useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import './email-preview.scss';
Expand All @@ -23,15 +24,24 @@ export default function EmailPreview( { isModalOpen, closeModal } ) {
const [ emailSending, setEmailSending ] = useState( false );
const [ errorMessage, setErrorMessage ] = useState( false );
const postId = useSelect( select => select( 'core/editor' ).getCurrentPostId() );
const { __unstableSaveForPreview } = useDispatch( editorStore );
const [ isSmall ] = useBreakpointMatch( 'sm' );
const { tracks } = useAnalytics();

const sendEmailPreview = () => {
const sendEmailPreview = async () => {
tracks.recordEvent( 'jetpack_send_email_preview', {
post_id: postId,
} );

setEmailSending( true );

// Save post revision so that we send what they see in the editor, and not what previous draft/revision might've saved
// Introduced at GB 16.3 at https://github.com/WordPress/gutenberg/pull/44971
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Automattic/jetpack-crew in case you had advice on how to handle Gutenberg functions and backwards compatibility with older WPs in Jetpack; this isn't really issue for .com since we run the latest.

Method introduced in WordPress/gutenberg#44971
It's here, and it's contents are stable so in theory I could just copy all that code here instead.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jetpack needs to support back to the version of the Gutenberg packages that is bundled in the previous WordPress release, which is currently 6.3.

I guess the case here is that 6.3 (and maybe 6.4 too?) doesn't have any version of the function at all, and you're wanting to copy-paste the implementation into Jetpack so the change you're making here still happens in 6.3? Be sure to include a comment indicating that the back-compat code can be removed when we drop support for the old version. Something along the lines of // @todo Remove the `if` check and `else` branch once WP 6.4 is the minimum supported version is fine, you don't have to get too fancy. Just be sure the comment clearly mentions an appropriate WordPress version so we can find it easily via a grep when we're making the PRs like #34210.

Or if it's ok that WP 6.3 just doesn't do whatever the new function does, that's fine with us too. Again though a comment mentioning the WP version when the if check can be removed would be useful.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

// @todo Remove the `if` check once WP 6.4 is the minimum supported version
if ( typeof __unstableSaveForPreview === 'function' ) {
await __unstableSaveForPreview();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

}

apiFetch( {
path: '/wpcom/v2/send-email-preview/',
method: 'POST',
Expand Down