Skip to content
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: changed

Add Send newsletter by default toggle to Newsletter settings section.
4 changes: 2 additions & 2 deletions projects/packages/newsletter/src/settings/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
GlobalNotices,
useGlobalNotices,
} from '@automattic/jetpack-components';
import { getSiteType, isSimpleSite } from '@automattic/jetpack-script-data';
import { getSiteType } from '@automattic/jetpack-script-data';
import { Notice, Disabled, Spinner } from '@wordpress/components';
import { createRoot, useCallback, useEffect, useState, useMemo } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
Expand Down Expand Up @@ -391,7 +391,7 @@ function NewsletterSettingsApp(): JSX.Element | null {
<Container horizontalSpacing={ 3 }>
<Col>
<Stack gap="md" direction="column" className="newsletter-settings">
{ ! isSimpleSite() && <NewsletterSection data={ data } onChange={ handleAutoSave } /> }
<NewsletterSection data={ data } onChange={ handleAutoSave } />

<Disabled isDisabled={ ! data.subscriptions }>
<Stack gap="md" direction="column">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
*/
import analytics from '@automattic/jetpack-analytics';
import { getRedirectUrl } from '@automattic/jetpack-components';
import { getSiteType } from '@automattic/jetpack-script-data';
import { getSiteType, isSimpleSite } from '@automattic/jetpack-script-data';
import {
Card,
CardHeader,
CardBody,
CardFooter,
ExternalLink,
ToggleControl,
__experimentalHeading as Heading, // eslint-disable-line @wordpress/no-unsafe-wp-apis
} from '@wordpress/components';
import { DataForm, type Field } from '@wordpress/dataviews/wp';
Expand Down Expand Up @@ -63,17 +64,54 @@ export function NewsletterSection( { data, onChange }: NewsletterSectionProps ):
}, [ siteType ] );

const fields: Field< NewsletterSettings >[] = [
...( ! isSimpleSite()
? [
{
id: 'subscriptions',
label: __(
'Let visitors subscribe to this site and receive emails when you publish a post',
'jetpack-newsletter'
),
type: 'boolean' as const,
Edit: 'toggle' as const,
},
]
: [] ),
{
id: 'subscriptions',
label: __(
'Let visitors subscribe to this site and receive emails when you publish a post',
id: 'wpcom_newsletter_send_default',
label: __( 'Email new posts to subscribers by default', 'jetpack-newsletter' ),
type: 'boolean' as const,
Edit( { field, onChange: onChangeField, data: formData } ) {
const handleToggle = useCallback( () => {
onChangeField(
field.setValue( {
item: formData,
value: ! field.getValue( { item: formData } ),
} )
);
}, [ onChangeField, field, formData ] );
return (
<ToggleControl
label={ field.label }
help={ field.description }
checked={ !! field.getValue( { item: formData } ) }
onChange={ handleToggle }
disabled={ ! isSimpleSite() && ! formData.subscriptions }
/>
);
},
description: __(
'When on, the newsletter option will be pre-selected each time you publish. You can change it in the newsletter panel in the editor before publishing any post.',
'jetpack-newsletter'
),
type: 'boolean' as const,
Edit: 'toggle' as const,
},
];

const formFields = [
...( ! isSimpleSite() ? [ 'subscriptions' ] : [] ),
'wpcom_newsletter_send_default',
];

return (
<Card>
<CardHeader>
Expand All @@ -88,7 +126,7 @@ export function NewsletterSection( { data, onChange }: NewsletterSectionProps ):
type: 'regular',
labelPosition: 'top',
},
fields: [ 'subscriptions' ],
fields: formFields,
} }
onChange={ handleChange }
/>
Expand Down
1 change: 1 addition & 0 deletions projects/packages/newsletter/src/settings/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface NewsletterSettings {
jetpack_post_date_in_email: boolean;
jetpack_subscriptions_reply_to: 'comment' | 'author' | 'no-reply';
jetpack_subscriptions_from_name: string;
wpcom_newsletter_send_default: boolean;
wpcom_newsletter_categories_enabled: boolean;
wpcom_newsletter_categories: string[];
subscription_options?: {
Expand Down
48 changes: 47 additions & 1 deletion projects/plugins/jetpack/_inc/client/newsletter/newsletter.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getRedirectUrl } from '@automattic/jetpack-components';
import { getRedirectUrl, ToggleControl } from '@automattic/jetpack-components';
import { __ } from '@wordpress/i18n';
import { useCallback } from 'react';
import { connect } from 'react-redux';
Expand All @@ -19,6 +19,8 @@ import { isWpAdminSubscriberManagementEnabled, getSiteAdminUrl } from 'state/ini
import { getModule } from 'state/modules';
import { SUBSCRIPTIONS_MODULE_NAME } from './constants';

const NEWSLETTER_SEND_DEFAULT_OPTION = 'wpcom_newsletter_send_default';

const trackViewSubsClick = () => {
analytics.tracks.recordJetpackClick( 'manage-subscribers' );
};
Expand All @@ -41,7 +43,9 @@ function Newsletter( props ) {
siteHasConnectedUser,
wpAdminSubscriberManagementEnabled,
siteAdminUrl,
newsletterSendDefault,
updateOptions,
updateFormStateAndSaveOptionValue,
getOptionValue,
refreshSettings,
} = props;
Expand Down Expand Up @@ -71,6 +75,14 @@ function Newsletter( props ) {
);
};

const handleEmailPostToSubsDefaultToggle = useCallback( () => {
const newValue = ! newsletterSendDefault;
updateFormStateAndSaveOptionValue( NEWSLETTER_SEND_DEFAULT_OPTION, newValue );
analytics.tracks.recordEvent( 'jetpack_set_wpcom_newsletter_send_default', {
enabled: newValue,
} );
}, [ newsletterSendDefault, updateFormStateAndSaveOptionValue ] );

const toggleModule = useCallback(
module => {
const status = getOptionValue( module );
Expand Down Expand Up @@ -129,6 +141,39 @@ function Newsletter( props ) {
</ModuleToggle>
</SettingsGroup>

<SettingsGroup
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't think it's a big deal but this SettingsGroup repeats the Privacy information link

Image

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks @dsas I think that is the same across all the support help popovers. I noticed the same but it was consistent across all of them and assumed it was by design.

hasChild
disableInOfflineMode
disableInSiteConnectionMode={ ! siteHasConnectedUser }
module={ subscriptions }
support={ {
text: __(
'When on, the newsletter option will be pre-selected each time you publish. You can change it in the newsletter panel in the editor before publishing any post.',
'jetpack'
),
link: getRedirectUrl( 'jetpack-support-subscriptions', {
anchor: 'post-email-vs-post-only-options',
} ),
} }
>
<ToggleControl
checked={ isSubscriptionsActive && !! newsletterSendDefault }
disabled={
! isSubscriptionsActive ||
! siteHasConnectedUser ||
unavailableInOfflineMode ||
isSavingAnyOption( [ SUBSCRIPTIONS_MODULE_NAME, NEWSLETTER_SEND_DEFAULT_OPTION ] )
}
toggling={ isSavingAnyOption( [ NEWSLETTER_SEND_DEFAULT_OPTION ] ) }
onChange={ handleEmailPostToSubsDefaultToggle }
label={
<span className="jp-form-toggle-explanation">
{ __( 'Email new posts to subscribers by default', 'jetpack' ) }
</span>
}
/>
</SettingsGroup>

{ getSubClickableCard() }
</SettingsCard>
);
Expand All @@ -145,6 +190,7 @@ export default withModuleSettingsFormHelpers(
siteHasConnectedUser: hasConnectedOwner( state ),
wpAdminSubscriberManagementEnabled: isWpAdminSubscriberManagementEnabled( state ),
siteAdminUrl: getSiteAdminUrl( state ),
newsletterSendDefault: ownProps.getOptionValue( NEWSLETTER_SEND_DEFAULT_OPTION ),
};
} )( Newsletter )
);
36 changes: 36 additions & 0 deletions projects/plugins/jetpack/_inc/client/newsletter/test/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ describe( 'Newsletter', () => {
wpAdminSubscriberManagementEnabled: true,
siteAdminUrl: 'https://example.org/wp-admin/',
getOptionValue: () => true,
newsletterSendDefault: true,
updateOptions: jest.fn().mockResolvedValue( {} ),
updateFormStateAndSaveOptionValue: jest.fn(),
refreshSettings: jest.fn(),
};

Expand Down Expand Up @@ -116,4 +118,38 @@ describe( 'Newsletter', () => {
expect( screen.getByRole( 'link', { name: 'Manage all subscribers' } ) ).toBeInTheDocument();
} );
} );

describe( 'Email new posts to subscribers by default toggle', () => {
it( 'renders the toggle', () => {
render( <Newsletter { ...defaultProps } />, { initialState } );
expect( screen.getByText( 'Email new posts to subscribers by default' ) ).toBeInTheDocument();
} );

it( 'is checked when newsletterSendDefault is true', () => {
render( <Newsletter { ...defaultProps } />, { initialState } );
const toggle = screen.getByLabelText( 'Email new posts to subscribers by default' );
expect( toggle ).toBeChecked();
} );

it( 'is unchecked when newsletterSendDefault is false', () => {
const propsWithSendDefaultFalse = {
...defaultProps,
getOptionValue: option => ( option === 'wpcom_newsletter_send_default' ? false : true ),
};
render( <Newsletter { ...propsWithSendDefaultFalse } />, { initialState } );
const toggle = screen.getByLabelText( 'Email new posts to subscribers by default' );
expect( toggle ).not.toBeChecked();
} );

it( 'is disabled and unchecked when subscriptions are inactive', () => {
const propsWithSubscriptionsInactive = {
...defaultProps,
getOptionValue: option => ( option === 'subscriptions' ? false : true ),
};
render( <Newsletter { ...propsWithSubscriptionsInactive } />, { initialState } );
const toggle = screen.getByLabelText( 'Email new posts to subscribers by default' );
expect( toggle ).not.toBeChecked();
expect( toggle ).toBeDisabled();
} );
} );
} );
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: other

Newsletter: Add Send newsletter by default toggle to settings.
Loading