diff --git a/projects/plugins/jetpack/changelog/update-newsletter-audience-paywall-notice b/projects/plugins/jetpack/changelog/update-newsletter-audience-paywall-notice new file mode 100644 index 000000000000..6303922fea5a --- /dev/null +++ b/projects/plugins/jetpack/changelog/update-newsletter-audience-paywall-notice @@ -0,0 +1,4 @@ +Significance: patch +Type: enhancement + +Newsletter: explain why the Everyone audience is unavailable when a post has a paywall block, instead of linking to the block. diff --git a/projects/plugins/jetpack/extensions/blocks/paywall/edit.jsx b/projects/plugins/jetpack/extensions/blocks/paywall/edit.jsx index 3b382ba8150d..18c701892617 100644 --- a/projects/plugins/jetpack/extensions/blocks/paywall/edit.jsx +++ b/projects/plugins/jetpack/extensions/blocks/paywall/edit.jsx @@ -154,6 +154,9 @@ function PaywallEdit() { stripeConnectUrl={ stripeConnectUrl } hasTierPlans={ hasTierPlans } postHasPaywallBlock={ true } + // The block card above this panel already explains the paywall, so the + // notice would only repeat the heading it sits under. + explainPaywallConstraint={ false } /> diff --git a/projects/plugins/jetpack/extensions/blocks/subscriptions/panel.jsx b/projects/plugins/jetpack/extensions/blocks/subscriptions/panel.jsx index a131c88bc9d0..cee8bb46a295 100644 --- a/projects/plugins/jetpack/extensions/blocks/subscriptions/panel.jsx +++ b/projects/plugins/jetpack/extensions/blocks/subscriptions/panel.jsx @@ -174,7 +174,7 @@ function NewsletterPrePublishSettingsPanel( { accessLevel, showPreviewModal } ) { __( 'Audience:', 'jetpack' ) } { accessLevel && ( - { accessOptions[ accessLevel ].panelHeading } + { accessOptions[ accessLevel ].label } ) } diff --git a/projects/plugins/jetpack/extensions/shared/memberships/constants.js b/projects/plugins/jetpack/extensions/shared/memberships/constants.js index 69f7f90af846..e843ffdffe88 100644 --- a/projects/plugins/jetpack/extensions/shared/memberships/constants.js +++ b/projects/plugins/jetpack/extensions/shared/memberships/constants.js @@ -7,16 +7,13 @@ export const accessOptions = { everybody: { key: 'everybody', label: __( 'Everyone', 'jetpack' ), - panelHeading: __( 'Everyone', 'jetpack' ), }, subscribers: { key: 'subscribers', label: __( 'Subscribers', 'jetpack' ), - panelHeading: __( 'Subscribers', 'jetpack' ), }, paid_subscribers: { key: 'paid_subscribers', label: __( 'Paid subscribers', 'jetpack' ), - panelHeading: __( 'Paid subscribers', 'jetpack' ), }, }; diff --git a/projects/plugins/jetpack/extensions/shared/memberships/settings.jsx b/projects/plugins/jetpack/extensions/shared/memberships/settings.jsx index e32b01f9955b..b6f8d07058de 100644 --- a/projects/plugins/jetpack/extensions/shared/memberships/settings.jsx +++ b/projects/plugins/jetpack/extensions/shared/memberships/settings.jsx @@ -1,7 +1,6 @@ -import { getBlockIconComponent } from '@automattic/jetpack-shared-extension-utils'; import { formatNumberCompact } from '@automattic/number-formatters'; import { - Button, + BaseControl, Flex, FlexBlock, RadioControl, @@ -9,13 +8,13 @@ import { __experimentalToggleGroupControl as ToggleGroupControl, // eslint-disable-line @wordpress/no-unsafe-wp-apis __experimentalToggleGroupControlOption as ToggleGroupControlOption, // eslint-disable-line @wordpress/no-unsafe-wp-apis } from '@wordpress/components'; -import { useInstanceId, useViewportMatch } from '@wordpress/compose'; +import { useInstanceId } from '@wordpress/compose'; import { useEntityId, useEntityProp, store as coreDataStore } from '@wordpress/core-data'; import { useDispatch, useSelect } from '@wordpress/data'; import { PostVisibilityCheck, store as editorStore } from '@wordpress/editor'; import { __ } from '@wordpress/i18n'; -import { Icon } from '@wordpress/icons'; -import { Link as ExternalLink } from '@wordpress/ui'; +import { Link as ExternalLink, Notice } from '@wordpress/ui'; +import clsx from 'clsx'; import paywallBlockMetadata from '../../blocks/paywall/block.json'; import { store as membershipProductsStore } from '../../store/membership-products'; import './settings.scss'; @@ -27,8 +26,6 @@ import { } from './constants'; import { getPaidPlanLink, getShowMisconfigurationWarning, MisconfigurationWarning } from './utils'; -const paywallIcon = getBlockIconComponent( paywallBlockMetadata ); - export function Link( { href, children } ) { return ( @@ -166,11 +163,73 @@ function TierSelector() { ); } +/** + * A single audience option, rendered with Gutenberg's own RadioControl classes so it + * looks identical to a stock radio group. + * + * The group is built by hand rather than with RadioControl because an unavailable + * option has to stay visible in its natural position — "Everyone" sits above the + * options that remain selectable, which is a place RadioControl gives no way to reach. + * + * Unavailable options use aria-disabled rather than the native disabled attribute: a + * disabled input leaves the tab order and is skipped by screen readers, which would + * hide the option from exactly the people its explanation is there to inform. They are + * also left out of the shared group name, so arrow-key navigation — which selects as it + * moves — cannot land on one and choose it. + * + * @param {object} props - Component props. + * @param {string} props.id - Unique id tying the input to its label. + * @param {string} props.groupName - Shared name for the selectable options. + * @param {string} props.value - Access level key this option sets. + * @param {string} props.label - Visible label. + * @param {boolean} props.checked - Whether this option is the current value. + * @param {boolean} props.disabled - Whether this option cannot be chosen. + * @param {string} [props.describedBy] - Id of the element explaining why it is unavailable. + * @param {Function} props.onChange - Called with the new access level key. + * @return {import('react').ReactElement} The option. + */ +function AccessOption( { id, groupName, value, label, checked, disabled, describedBy, onChange } ) { + return ( +
+ onChange( event.target.value ) } + onClick={ disabled ? event => event.preventDefault() : undefined } + onKeyDown={ + disabled + ? event => { + if ( event.key === ' ' ) { + event.preventDefault(); + } + } + : undefined + } + /> + +
+ ); +} + export function NewsletterAccessRadioButtons( { accessLevel, hasTierPlans, stripeConnectUrl, postHasPaywallBlock: postHasPaywallBlock = false, + explainPaywallConstraint = true, } ) { const isStripeConnected = stripeConnectUrl === null; const { totalSubscribers, paidSubscribers } = useSelect( select => @@ -186,6 +245,22 @@ export function NewsletterAccessRadioButtons( { // paid before Stripe was disconnected does not end up with nothing selected. const showPaidAsDisabled = ! isPaidAvailable && ! isPaidSelected; + // A paywall block splits the post, so "the whole post is public" stops being an + // option it can express. The option stays visible and disabled — the same treatment + // paid subscribers gets — with a notice saying why, rather than the panel silently + // changing shape. The saved-value guard is the same as above: the paywall block + // moves the post off "everybody" when it is inserted, but until that lands we must + // not leave the group with nothing selected. + const isEverybodySelected = accessLevel === accessOptions.everybody.key; + const showEverybodyAsDisabled = !! postHasPaywallBlock && ! isEverybodySelected; + + // The paywall block's own inspector opts out: it sits directly under Gutenberg's + // block card, which already says what a paywall does, so the notice repeats the + // heading above it. Both the notice and the option's aria-describedby derive from + // this one flag — gating only the notice would leave the option pointing at an id + // that is no longer in the DOM, which reads as no description at all. + const showPaywallNotice = showEverybodyAsDisabled && explainPaywallConstraint; + const setAccess = useSetAccess(); // The count beside each option is the size of the audience that can read it, which // is what distinguishes the options from one another. postHasPaywallBlock is @@ -203,78 +278,71 @@ export function NewsletterAccessRadioButtons( { paidSubscribers, } ); - const paidOptionLabel = `${ accessOptions.paid_subscribers.label } (${ formatNumberCompact( - paidSubscribersReach - ) })`; - const disabledPaidOptionId = useInstanceId( - NewsletterAccessRadioButtons, - 'jetpack-newsletter-access-paid-subscribers-disabled' - ); - const setupLinkId = `${ disabledPaidOptionId }-setup-link`; + const instanceId = useInstanceId( NewsletterAccessRadioButtons, 'jetpack-newsletter-access' ); + const groupName = `${ instanceId }-group`; + const paywallNoticeId = `${ instanceId }-paywall-notice`; + const setupLinkId = `${ instanceId }-paid-setup-link`; + + const options = [ + { + value: accessOptions.everybody.key, + label: accessOptions.everybody.label, + disabled: showEverybodyAsDisabled, + describedBy: showPaywallNotice ? paywallNoticeId : undefined, + }, + { + value: accessOptions.subscribers.key, + label: `${ accessOptions.subscribers.label } (${ formatNumberCompact( subscribersReach ) })`, + }, + { + value: accessOptions.paid_subscribers.key, + label: `${ accessOptions.paid_subscribers.label } (${ formatNumberCompact( + paidSubscribersReach + ) })`, + disabled: showPaidAsDisabled, + describedBy: showPaidAsDisabled ? setupLinkId : undefined, + }, + ]; return (
- - { showPaidAsDisabled && ( - <> -
- { /* - * aria-disabled rather than disabled: a disabled input leaves the tab - * order and is skipped by screen readers, which would hide the paid - * option from exactly the people the setup link is there to inform. - * It stays focusable and announced, but cannot be selected. - */ } - event.preventDefault() } - onKeyDown={ event => { - if ( event.key === ' ' ) { - event.preventDefault(); - } - } } - id={ disabledPaidOptionId } + { showPaywallNotice && ( + // icon={ null } matches the mockup, which shows the notice without the + // intent icon @wordpress/ui would otherwise render for "info". + + { __( 'Paywall active', 'jetpack' ) } + + { __( + 'Choose who can read the full post. Everyone can still read the content above the paywall.', + 'jetpack' + ) } + + + ) } +
+ + { __( 'Who can read this post?', 'jetpack' ) } + +
+ { options.map( option => ( + - -
- - { __( 'Turn on paid subscribers', 'jetpack' ) } - - + ) ) } +
+ + { showPaidAsDisabled && ( + + { __( 'Turn on paid subscribers', 'jetpack' ) } + ) } { isPaidSelected && isPaidAvailable && }

@@ -285,7 +353,7 @@ export function NewsletterAccessRadioButtons( { } export function NewsletterAccessDocumentSettings( { accessLevel } ) { - const { hasTierPlans, stripeConnectUrl, isLoading, foundPaywallBlock } = useSelect( select => { + const { hasTierPlans, stripeConnectUrl, isLoading, postHasPaywallBlock } = useSelect( select => { const { getNewsletterTierProducts, getConnectUrl, isApiStateLoading } = select( 'jetpack/membership-products' ); @@ -295,15 +363,11 @@ export function NewsletterAccessDocumentSettings( { accessLevel } ) { isLoading: isApiStateLoading(), stripeConnectUrl: getConnectUrl(), hasTierPlans: getNewsletterTierProducts()?.length !== 0, - foundPaywallBlock: getBlocks().find( block => block.name === paywallBlockMetadata.name ), + postHasPaywallBlock: getBlocks().some( block => block.name === paywallBlockMetadata.name ), }; } ); const postVisibility = useSelect( select => select( editorStore ).getEditedPostVisibility() ); - const { selectBlock } = useDispatch( 'core/block-editor' ); - const { closeGeneralSidebar } = useDispatch( 'core/edit-post' ); - - const isMobileViewport = useViewportMatch( 'medium', '<' ); if ( isLoading ) { return ( @@ -321,54 +385,22 @@ export function NewsletterAccessDocumentSettings( { accessLevel } ) { return ( ( - <> - { foundPaywallBlock && ( - <> -

- - - -
-

{ __( 'Paywall', 'jetpack' ) }

- - { __( - 'The content below the paywall block is exclusive to the selected audience.', - 'jetpack' - ) }{ ' ' } - - -
-
- - ) } - - { showMisconfigurationWarning && } - - { canEdit && ( - - ) } - - { /* Display the uneditable access level when the user doesn't have edit privileges*/ } - { ! canEdit && { accessLabel } } - - - + + { showMisconfigurationWarning && } + + { canEdit && ( + + ) } + + { /* Display the uneditable access level when the user doesn't have edit privileges*/ } + { ! canEdit && { accessLabel } } + + ) } /> ); diff --git a/projects/plugins/jetpack/extensions/shared/memberships/settings.scss b/projects/plugins/jetpack/extensions/shared/memberships/settings.scss index 8f0233f3c581..de80a4d9b84a 100644 --- a/projects/plugins/jetpack/extensions/shared/memberships/settings.scss +++ b/projects/plugins/jetpack/extensions/shared/memberships/settings.scss @@ -39,6 +39,14 @@ } } + // The group is built by hand rather than by RadioControl (see AccessOption), + // so it supplies the spacing RadioControl would get from its own VStack. + .components-radio-control__group-wrapper { + display: flex; + flex-direction: column; + gap: 12px; + } + // Two tiers of spacing: the options sit close together, while the legend, the // setup link and the description are each set apart from the group by a wider // gap. Gutenberg gives the legend 8px of its own, so it needs half as much. diff --git a/projects/plugins/jetpack/extensions/shared/memberships/test/settings-test.jsx b/projects/plugins/jetpack/extensions/shared/memberships/test/settings-test.jsx index 0924554e395e..906d45009f72 100644 --- a/projects/plugins/jetpack/extensions/shared/memberships/test/settings-test.jsx +++ b/projects/plugins/jetpack/extensions/shared/memberships/test/settings-test.jsx @@ -8,6 +8,7 @@ import { Link, getAccessDescription, getReachForAccessLevelKey, + NewsletterAccessDocumentSettings, NewsletterAccessRadioButtons, NewsletterEmailDocumentSettings, } from '../settings'; @@ -334,11 +335,130 @@ describe( 'NewsletterAccessRadioButtons', () => { } ); } ); - test( 'omits Everyone when the post has a paywall block', () => { - renderPanel( { postHasPaywallBlock: true, accessLevel: 'subscribers' } ); + describe( 'when the post has a paywall block', () => { + const renderPaywalled = ( props = {}, selectOptions = {} ) => + renderPanel( + { postHasPaywallBlock: true, accessLevel: 'subscribers', ...props }, + selectOptions + ); + + // Removing the option instead would make the panel silently change shape. Keeping + // it visible and disabled matches how paid subscribers behaves when it is unavailable. + test( 'keeps Everyone visible but not selectable', () => { + renderPaywalled(); + + const everyone = screen.getByRole( 'radio', { name: 'Everyone' } ); + expect( everyone ).toHaveAttribute( 'aria-disabled', 'true' ); + expect( everyone ).not.toBeChecked(); + } ); + + test( 'does not save the everybody level when the disabled option is clicked', async () => { + renderPaywalled(); + + await userEvent.click( screen.getByRole( 'radio', { name: 'Everyone' } ) ); + expect( mockSetPostMeta ).not.toHaveBeenCalled(); + } ); + + // The notice only helps a screen reader user if the option points at it, so the + // copy is asserted through the description as it is actually computed from + // aria-describedby rather than by reading the notice on its own. + test( 'explains why Everyone is unavailable, in a way the option points at', () => { + renderPaywalled(); + + expect( screen.getByText( 'Paywall active' ) ).toBeInTheDocument(); + + const everyone = screen.getByRole( 'radio', { name: 'Everyone' } ); + expect( everyone ).toHaveAccessibleDescription( /^Paywall active/ ); + expect( everyone ).toHaveAccessibleDescription( + /Choose who can read the full post\. Everyone can still read the content above the paywall\.$/ + ); + } ); + + // A native `disabled` attribute would drop the option out of the tab order and hide + // it from screen readers, taking the explanation with it. + test( 'keeps the disabled option focusable', () => { + renderPaywalled(); + + const everyone = screen.getByRole( 'radio', { name: 'Everyone' } ); + expect( everyone ).toBeEnabled(); + + everyone.focus(); + expect( everyone ).toHaveFocus(); + } ); + + // Arrow keys select as they move within a native radio group, so an unselectable + // option must not share the group's name or it could be chosen by keyboard. + test( 'leaves the disabled option out of the keyboard group', () => { + renderPaywalled(); - expect( screen.queryByRole( 'radio', { name: 'Everyone' } ) ).not.toBeInTheDocument(); - expect( screen.getByRole( 'radio', { name: /^Subscribers/ } ) ).toBeInTheDocument(); + expect( screen.getByRole( 'radio', { name: 'Everyone' } ) ).not.toHaveAttribute( 'name' ); + expect( screen.getByRole( 'radio', { name: /^Subscribers/ } ) ).toHaveAttribute( 'name' ); + } ); + + test( 'still lets the remaining audiences be chosen', async () => { + renderPaywalled(); + + await userEvent.click( screen.getByRole( 'radio', { name: /^Paid subscribers/ } ) ); + expect( mockSetPostMeta ).toHaveBeenCalledWith( + expect.objectContaining( { + [ META_NAME_FOR_POST_LEVEL_ACCESS_SETTINGS ]: 'paid_subscribers', + } ) + ); + } ); + + // Inserting a paywall block moves the post off "everybody". Until that lands, the + // option has to stay selectable or the group would show nothing selected at all. + test( 'leaves Everyone selectable while it is still the saved value', () => { + renderPaywalled( { accessLevel: 'everybody' } ); + + const everyone = screen.getByRole( 'radio', { name: 'Everyone' } ); + expect( everyone ).toBeChecked(); + expect( everyone ).not.toHaveAttribute( 'aria-disabled' ); + expect( screen.queryByText( 'Paywall active' ) ).not.toBeInTheDocument(); + } ); + + // The paywall block's own inspector opts out, because Gutenberg's block card + // directly above it already explains the paywall. + describe( 'when the caller opts out of the explanation', () => { + const renderOptedOut = () => renderPaywalled( { explainPaywallConstraint: false } ); + + test( 'hides the notice but keeps Everyone visible and disabled', () => { + renderOptedOut(); + + expect( screen.queryByText( 'Paywall active' ) ).not.toBeInTheDocument(); + expect( screen.getByRole( 'radio', { name: 'Everyone' } ) ).toHaveAttribute( + 'aria-disabled', + 'true' + ); + } ); + + // Gating the notice without gating aria-describedby would leave the option + // pointing at an id that is no longer rendered, which assistive tech reports as + // no description at all — worse than deliberately having none. + test( 'leaves no dangling description reference behind', () => { + renderOptedOut(); + + const everyone = screen.getByRole( 'radio', { name: 'Everyone' } ); + expect( everyone ).not.toHaveAttribute( 'aria-describedby' ); + expect( everyone ).toHaveAccessibleDescription( '' ); + } ); + + test( 'still refuses to save the everybody level', async () => { + renderOptedOut(); + + await userEvent.click( screen.getByRole( 'radio', { name: 'Everyone' } ) ); + expect( mockSetPostMeta ).not.toHaveBeenCalled(); + } ); + } ); + } ); + + test( 'offers Everyone without explanation when there is no paywall block', () => { + renderPanel( { accessLevel: 'everybody' } ); + + const everyone = screen.getByRole( 'radio', { name: 'Everyone' } ); + expect( everyone ).toBeChecked(); + expect( everyone ).not.toHaveAttribute( 'aria-disabled' ); + expect( screen.queryByText( 'Paywall active' ) ).not.toBeInTheDocument(); } ); // The counts report how many people can read each level. Switching the paid count to @@ -355,6 +475,77 @@ describe( 'NewsletterAccessRadioButtons', () => { } ); } ); +describe( 'NewsletterAccessDocumentSettings', () => { + const PAYWALL_BLOCK = { name: 'jetpack/paywall', clientId: 'paywall-1' }; + + const createMockSelect = + ( { blocks = [] } = {} ) => + store => { + if ( store === 'jetpack/membership-products' ) { + return { + isApiStateLoading: () => false, + getConnectUrl: () => null, + getNewsletterTierProducts: () => [], + }; + } + if ( store === 'core/block-editor' ) { + return { getBlocks: () => blocks }; + } + if ( store === membershipProductsStore ) { + return { + getSubscriberCounts: () => ( { totalSubscribers: 10, paidSubscribers: 2 } ), + getNewsletterTierProducts: () => [], + }; + } + if ( store === editorStore ) { + return { + getCurrentPostType: () => 'post', + getEditedPostVisibility: () => 'public', + }; + } + return {}; + }; + + const renderSettings = ( { blocks = [], accessLevel = 'subscribers' } = {} ) => { + mockUseSelect.mockImplementation( selector => selector( createMockSelect( { blocks } ) ) ); + return render( ); + }; + + beforeEach( () => { + jest.clearAllMocks(); + mockUseEntityProp.mockReturnValue( [ {}, jest.fn() ] ); + jest.spyOn( wpData, 'useSelect' ).mockImplementation( selector => mockUseSelect( selector ) ); + } ); + + afterEach( () => { + jest.restoreAllMocks(); + } ); + + // The card used to sit above the radio group with an "Edit the block." button that + // selected the paywall block without switching the sidebar to the Block tab, so it + // looked inert. The notice explains the constraint instead. Asserting the notice is + // present is what makes the card's absence meaningful: it proves the paywall block + // was detected in this render, rather than the card being missing for some other reason. + test( 'explains the paywall in the panel instead of linking out to the block', () => { + renderSettings( { blocks: [ PAYWALL_BLOCK ] } ); + + expect( screen.getByText( 'Paywall active' ) ).toBeInTheDocument(); + expect( screen.queryByRole( 'button', { name: /edit the block/i } ) ).not.toBeInTheDocument(); + expect( + screen.queryByText( /content below the paywall block is exclusive/i ) + ).not.toBeInTheDocument(); + } ); + + test( 'leaves Everyone selectable when the post has no paywall block', () => { + renderSettings( { blocks: [ { name: 'core/paragraph', clientId: 'p-1' } ] } ); + + expect( screen.getByRole( 'radio', { name: 'Everyone' } ) ).not.toHaveAttribute( + 'aria-disabled' + ); + expect( screen.queryByText( 'Paywall active' ) ).not.toBeInTheDocument(); + } ); +} ); + describe( 'NewsletterEmailDocumentSettings', () => { const createMockSelect = postEmailSentState => store => { if ( store === editorStore ) {