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,66 @@
import { getRedirectUrl } from '@automattic/jetpack-components';
import { __ } from '@wordpress/i18n';
import { Link, Stack, SwitchControl, Text } from '@wordpress/ui';
import { useId } from 'react';
import { PanelModuleToggle } from './panel-module-toggle';
import styles from './styles.module.scss';
import { useModuleOption } from './use-module-option';
import type { ModuleSettingsProps } from './registry';

/**
* The Markdown options.
*
* @param props - Component props.
* @param props.module - The module.
* @param props.disabled - Whether the site cannot use the module.
* @return The settings.
*/
export function MarkdownSettings( { module: $module, disabled }: ModuleSettingsProps ) {
const posts = useModuleOption( $module, 'wpcom_publish_posts_with_markdown' );
const comments = useModuleOption( $module, 'wpcom_publish_comments_with_markdown' );
const optionsDisabled = ! $module.activated || disabled;
const labelId = useId();

return (
<Stack direction="column" gap="lg">
<Text variant="body-md">
{ __(
'Use Markdown syntax to compose content with links, lists, and other styles. This setting enables Markdown in the Classic Editor as well as within a Classic Editor block.',
'jetpack-my-jetpack'
) }
</Text>
<Link
href={ getRedirectUrl( 'jetpack-support-markdown' ) }
target="_blank"
rel="noopener noreferrer"
>
{ __( 'Learn more about Markdown', 'jetpack-my-jetpack' ) }
</Link>

<Stack
direction="row"
align="center"
justify="space-between"
className={ styles[ 'settings-panel__toggle' ] }
>
<Text variant="body-md" id={ labelId }>
{ __( 'Enable Markdown', 'jetpack-my-jetpack' ) }
</Text>
<PanelModuleToggle module={ $module } labelledby={ labelId } disabled={ disabled } />

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.

[suggestion] One row of the coupling table in the description doesn't match the server: "Turning the module on keeps the stored option values (they also survive off and on)". This switch POSTs jetpack/v4/module/markdown/active. register_endpoints() loads the module endpoints file on every REST request (

add_action( 'rest_api_init', array( 'Jetpack_Core_Json_Api_Endpoints', 'register_endpoints' ) );
,
require_once JETPACK__PLUGIN_DIR . '_inc/lib/core-api/class.jetpack-core-api-module-endpoints.php';
), and that file hooks jetpack_activate_module_markdown to set wpcom_publish_posts_with_markdown to true (
* Actions performed only when Markdown is activated through the endpoint call.
*
* @since 4.7.0
*/
function jetpack_do_after_markdown_activation() {
// When Markdown is activated, enable support for post editing automatically.
update_option( 'wpcom_publish_posts_with_markdown', true );
}
add_action( 'jetpack_activate_module_markdown', 'jetpack_do_after_markdown_activation' );
). So every activation turns the posts option on, as on the old screen. Only the comments option survives off and on. The modules store refetches after the switch, so the panel shows the new value. Only the table, and what testers expect, need fixing.

Related, and older than this PR: Jetpack::plugin_upgrade() turns the posts option back on at every Jetpack version change, whether or not the module is on (

// Make sure Markdown for posts gets turned back on.
if ( ! get_option( 'wpcom_publish_posts_with_markdown' ) ) {
update_option( 'wpcom_publish_posts_with_markdown', true );
}
). With this panel, "module on, posts off" becomes the normal way to stop Markdown in posts, so that choice reverts at the next update. Probably a follow-up issue rather than this PR.

Generated by Claude.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Right, thanks. I fixed the table row in the description: activation sets the posts option, and only the comments option survives off and on. Filed the plugin_upgrade() reset as a follow-up: JETPACK-3729.

</Stack>

<SwitchControl
label={ __( 'Write posts and pages with Markdown', 'jetpack-my-jetpack' ) }
checked={ posts.value }
disabled={ optionsDisabled || posts.isSaving }
onCheckedChange={ posts.setValue }
/>
<SwitchControl
label={ __( 'Let readers use Markdown in comments', 'jetpack-my-jetpack' ) }
checked={ comments.value }
disabled={ optionsDisabled || comments.isSaving }
onCheckedChange={ comments.setValue }
/>
</Stack>
);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CarouselSettings } from './carousel';
import { GoogleAnalyticsSettings } from './google-analytics';
import { InfiniteScrollSettings } from './infinite-scroll';
import { MarkdownSettings } from './markdown';
import { PostByEmailSettings } from './post-by-email';
import { RelatedPostsSettings } from './related-posts';
import { SeoSettings } from './seo';
Expand All @@ -23,6 +24,7 @@ export type ModuleSettingsProps = {
export const moduleSettings: Record< string, ComponentType< ModuleSettingsProps > > = {
carousel: CarouselSettings,
'infinite-scroll': InfiniteScrollSettings,
markdown: MarkdownSettings,
'post-by-email': PostByEmailSettings,
'related-posts': RelatedPostsSettings,
'seo-tools': SeoSettings,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import apiFetch from '@wordpress/api-fetch';
import { MarkdownSettings } from '../markdown';
import type { MyJetpackModule } from '../../../types';

jest.mock( '@wordpress/api-fetch', () => jest.fn() );
jest.mock( '@automattic/jetpack-shared-stores', () => ( { store: {} } ) );
jest.mock( '@wordpress/notices', () => ( { store: 'core/notices' } ) );
jest.mock( '@wordpress/data', () => ( {
useDispatch: () => ( {
fetchModules: jest.fn(),
createErrorNotice: jest.fn(),
createSuccessNotice: jest.fn(),
} ),
} ) );
jest.mock( '../../../hooks/use-analytics', () => ( {
__esModule: true,
default: () => ( { recordEvent: jest.fn() } ),
} ) );
jest.mock( '../../module-toggle', () => ( {
ModuleToggle: ( { labelledby, disabled }: { labelledby?: string; disabled?: boolean } ) => (
<input type="checkbox" aria-labelledby={ labelledby } disabled={ disabled } />
),
} ) );

const build = ( overrides: Partial< MyJetpackModule > = {} ) =>
( {
module: 'markdown',
name: 'Markdown',
activated: true,
override: false,
options: {
wpcom_publish_posts_with_markdown: { current_value: true },
wpcom_publish_comments_with_markdown: { current_value: false },
},
...overrides,
} ) as unknown as MyJetpackModule;

const postsSwitch = () => screen.getByRole( 'switch', { name: /posts and pages/ } );
const commentsSwitch = () => screen.getByRole( 'switch', { name: /comments/ } );

beforeEach( () => {
jest.clearAllMocks();
( apiFetch as unknown as jest.Mock ).mockResolvedValue( {} );
} );

describe( 'MarkdownSettings', () => {
it( 'shows each option with its stored value', () => {
render( <MarkdownSettings module={ build() } disabled={ false } /> );

expect( postsSwitch() ).toBeChecked();
expect( commentsSwitch() ).not.toBeChecked();
} );

it( 'saves the comments option on its own', async () => {
render( <MarkdownSettings module={ build() } disabled={ false } /> );
await userEvent.click( commentsSwitch() );

expect( apiFetch ).toHaveBeenCalledWith( {
path: '/jetpack/v4/settings',
method: 'POST',
data: { wpcom_publish_comments_with_markdown: true },
} );
} );

it.each( [
[ 'the module is off', { activated: false }, false ],
[ 'the site cannot use it', {}, true ],
] )( 'locks both options when %s', ( _name, overrides, disabled ) => {
render( <MarkdownSettings module={ build( overrides ) } disabled={ disabled } /> );

expect( postsSwitch() ).toHaveAttribute( 'aria-disabled', 'true' );
expect( commentsSwitch() ).toHaveAttribute( 'aria-disabled', 'true' );
} );

it( 'locks the module switch when the site cannot use it', () => {
render( <MarkdownSettings module={ build() } disabled /> );

expect( screen.getByRole( 'checkbox', { name: 'Enable Markdown' } ) ).toBeDisabled();
} );
} );
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Settings panel: Add the Markdown settings, behind the settings consolidation flag.


2 changes: 1 addition & 1 deletion projects/packages/my-jetpack/src/class-initializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ class Initializer {
/**
* Modules whose Jetpack settings link opens in the Features panel. A subset of `moduleSettings` in `settings-panel/registry.ts`; see there for which modules stay out.
*/
public const SETTINGS_PANEL_MODULES = array( 'carousel', 'infinite-scroll', 'post-by-email', 'related-posts' );
public const SETTINGS_PANEL_MODULES = array( 'carousel', 'infinite-scroll', 'markdown', 'post-by-email', 'related-posts' );

/**
* Holds info/data about the site (from the /sites/%d endpoint)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ import { useLocation, useNavigate } from 'react-router';
import QuerySitePlugins from 'components/data/query-site-plugins';
import Search from 'components/search';
import analytics from 'lib/analytics';
import {
getAdsSettingsUrl,
isSettingsConsolidationEnabled,
isSwitchedInFeatures as _isSwitchedInFeatures,
} from 'lib/settings-consolidation';
import { getAdsSettingsUrl, isSettingsConsolidationEnabled } from 'lib/settings-consolidation';
import {
userCanManageModules as _userCanManageModules,
userIsSubscriber as _userIsSubscriber,
Expand Down Expand Up @@ -56,7 +52,6 @@ const SettingsNavTabs = props => {
hasPerformanceFeature,
hasModules,
isModuleActive,
isSwitchedInFeatures,
searchTerm,
searchForTerm,
} = props;
Expand Down Expand Up @@ -113,15 +108,11 @@ const SettingsNavTabs = props => {
if ( ! isSettingsConsolidationEnabled() && hasPerformanceFeature ) {
visibleTabs.push( '/performance' );
}
// A module Features switches no longer fills this tab.
// Once settings move out, every Writing card has a new home and leaves only a search pointer here.
if (
hasModules(
[ 'markdown', 'post-by-email', 'infinite-scroll', 'copy-post' ].filter(
module => ! isSwitchedInFeatures( module )
)
) ||
// Once settings move out, custom content types leave only a search pointer here.
( ! isSettingsConsolidationEnabled() && window.CUSTOM_CONTENT_TYPE__INITIAL_STATE?.active )
! isSettingsConsolidationEnabled() &&
( hasModules( [ 'markdown', 'post-by-email', 'infinite-scroll', 'copy-post' ] ) ||
window.CUSTOM_CONTENT_TYPE__INITIAL_STATE?.active )
) {
visibleTabs.push( '/writing' );
}
Expand Down Expand Up @@ -231,7 +222,6 @@ export default connect(
hasPerformanceFeature: hasAnyPerformanceFeature( state ),
hasModules: modules => hasAnyOfTheseModules( state, modules ),
isModuleActive: module => isModuleActivated( state, module ),
isSwitchedInFeatures: module => _isSwitchedInFeatures( state, module ),
searchTerm: getSearchTerm( state ),
} ),
dispatch => ( {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ describe( 'SettingsNavTabs', () => {
[ 'shows', 'Sharing', 'Like buttons', 'likes', false, 1 ],
[ 'shows', 'Writing', 'Copy Post', 'copy-post', false, 1 ],
[ 'hides', 'Writing', 'Copy Post', 'copy-post', true, 0 ],
[ 'shows', 'Writing', 'Markdown', 'markdown', false, 1 ],
[ 'hides', 'Writing', 'Markdown', 'markdown', true, 0 ],
[ 'hides', 'Writing', 'Post by Email', 'post-by-email', true, 0 ],
[ 'hides', 'Writing', 'Infinite Scroll', 'infinite-scroll', true, 0 ],
[ 'shows', 'Writing', 'custom content types', null, false, 1 ],
[ 'hides', 'Writing', 'custom content types', null, true, 0 ],
[ 'shows', 'Reader', 'the Reader', 'wpcom-reader', false, 1 ],
Expand Down
10 changes: 9 additions & 1 deletion projects/plugins/jetpack/_inc/client/discussion/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { __ } from '@wordpress/i18n';
import { Component } from 'react';
import { connect } from 'react-redux';
import QuerySite from 'components/data/query-site';
import { isSettingsConsolidationEnabled } from 'lib/settings-consolidation';
import {
isOfflineMode,
isUnavailableInOfflineMode,
Expand All @@ -12,6 +13,7 @@ import {
isOdysseyStatsEnabled,
getSiteAdminUrl,
isCommentBlocksAvailable,
userCanManageModules,
} from 'state/initial-state';
import { getModule, getModuleOverride } from 'state/modules';
import { isModuleFound as _isModuleFound } from 'state/search';
Expand Down Expand Up @@ -68,7 +70,13 @@ export default connect( state => {
settings: getSettings( state ),
isOfflineMode: isOfflineMode( state ),
isUnavailableInOfflineMode: module_name => isUnavailableInOfflineMode( state, module_name ),
isModuleFound: module_name => _isModuleFound( state, module_name ),
// Markdown's settings live in its My Jetpack panel, as in the Writing tab.
isModuleFound: module_name =>
! (
'markdown' === module_name &&
isSettingsConsolidationEnabled() &&
userCanManageModules( state )
) && _isModuleFound( state, module_name ),
connectUrl: getConnectUrl( state ),
isLinked: isCurrentUserLinked( state ),
getModuleOverride: module_name => getModuleOverride( state, module_name ),
Expand Down
8 changes: 7 additions & 1 deletion projects/plugins/jetpack/_inc/client/main.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,14 @@ export class Main extends Component {
break;
}
// Falls through.
case '/settings':
case '/writing':
// Only admins get here, and they find every Writing setting in Features once settings move out.
if ( '/writing' === route && isSettingsConsolidationEnabled() ) {
window.location.replace( getAdminUrl( 'admin.php?page=my-jetpack#/features' ) );
break;
}
// Falls through.
case '/settings':
case '/discussion':
case '/traffic':
case '/privacy':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ describe( 'Main routes that leave Settings once settings move out', () => {
[ '/performance', 'admin.php?page=my-jetpack#/features?search=cdn' ],
[ '/sharing', 'admin.php?page=my-jetpack#/features?search=sharing' ],
[ '/reader', 'admin.php?page=my-jetpack#/features?search=reader' ],
[ '/writing', 'admin.php?page=my-jetpack#/features' ],
] )( 'sends %s to %s with the settings flag on', ( route, destination ) => {
isSettingsConsolidationEnabled.mockReturnValue( true );
renderAt( route, false );
Expand All @@ -87,7 +88,7 @@ describe( 'Main routes that leave Settings once settings move out', () => {
expect( screen.queryByTestId( 'searchable-settings' ) ).not.toBeInTheDocument();
} );

it.each( [ '/performance', '/sharing', '/reader' ] )(
it.each( [ '/performance', '/sharing', '/reader', '/writing' ] )(
'keeps %s on Settings with the settings flag off',
route => {
renderAt( route, false );
Expand Down
18 changes: 13 additions & 5 deletions projects/plugins/jetpack/_inc/client/writing/composing.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ import { ModuleToggle } from 'components/module-toggle';
import { FeaturesPointer } from 'components/pointer-card';
import SettingsCard from 'components/settings-card';
import SettingsGroup from 'components/settings-group';
import { isSwitchedInFeatures as _isSwitchedInFeatures } from 'lib/settings-consolidation';
import { showMyJetpack } from 'state/initial-state';
import {
isSettingsConsolidationEnabled,
isSwitchedInFeatures as _isSwitchedInFeatures,
} from 'lib/settings-consolidation';
import { showMyJetpack, userCanManageModules } from 'state/initial-state';
import { getModule } from 'state/modules';
import { isModuleFound as _isModuleFound, getSearchTerm } from 'state/search';
import { MarkdownMoved } from './markdown-moved';

// Card order.
const COMPOSING_MODULES = [ 'copy-post', 'markdown', 'latex', 'shortcodes', 'blocks' ];
Expand Down Expand Up @@ -49,11 +53,12 @@ export class Composing extends Component {
};

render() {
const { isModuleFound, isSwitchedInFeatures } = this.props,
const { isMarkdownMoved, isModuleFound, isSwitchedInFeatures } = this.props,
isFound = module => ! isSwitchedInFeatures( module ) && isModuleFound( module ),
foundCopyPost = isFound( 'copy-post' ),
foundLatex = isFound( 'latex' ),
foundMarkdown = isFound( 'markdown' ),
foundMarkdown = ! isMarkdownMoved && isFound( 'markdown' ),
pointsToMarkdown = isMarkdownMoved && !! this.props.searchTerm && isModuleFound( 'markdown' ),
foundShortcodes = isFound( 'shortcodes' ),
foundBlocks = isFound( 'blocks' ),
foundMyJetpack = this.props.isMyJetpackReachable,
Expand All @@ -64,7 +69,7 @@ export class Composing extends Component {
)
: [];

if ( ! foundCard && ! pointedModules.length ) {
if ( ! foundCard && ! pointedModules.length && ! pointsToMarkdown ) {
return null;
}

Expand Down Expand Up @@ -262,6 +267,7 @@ export class Composing extends Component {
/>
);
} ) }
{ pointsToMarkdown && <MarkdownMoved getModule={ this.props.module } /> }
</>
);
}
Expand All @@ -272,6 +278,8 @@ export default connect( state => {
module: module_name => getModule( state, module_name ),
isModuleFound: module_name => _isModuleFound( state, module_name ),
isSwitchedInFeatures: module_name => _isSwitchedInFeatures( state, module_name ),
// The panel needs Features, which users who cannot manage modules lack.
isMarkdownMoved: isSettingsConsolidationEnabled() && userCanManageModules( state ),
Comment on lines +281 to +282

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.

[suggestion] This gate is written twice, here and in discussion/index.jsx (L73-L79). The comment repeats the reason already in the isSwitchedInFeatures() docblock (

* Users who cannot manage modules have no Features, so they keep the toggle.
*
* @param {object} state - Global state tree.
* @param {string} module - Module slug.
* @return {boolean} True when Features switches the module instead.
*/
export function isSwitchedInFeatures( state, module ) {
if ( ! isSettingsConsolidationEnabled() || ! userCanManageModules( state ) ) {
), and Post by Email does the same check inline (
( isSettingsConsolidationEnabled() && this.props.userCanManageModules ? (
). A helper next to isSwitchedInFeatures(), say isInSettingsPanel( state, module ), would hold the gate and its reason in one place. The Comments panel (JETPACK-3720) needs it next, so it can wait for that PR.

Generated by Claude.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in #54903 (d368df3): isInSettingsPanel( state ) next to isSwitchedInFeatures(), used by Composing, Discussion and Post by Email. It takes no module, so it needs no JS copy of SETTINGS_PANEL_MODULES.

isMyJetpackReachable: showMyJetpack( state ),
searchTerm: getSearchTerm( state ),
};
Expand Down
19 changes: 19 additions & 0 deletions projects/plugins/jetpack/_inc/client/writing/markdown-moved.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { __ } from '@wordpress/i18n';
import PointerCard from 'components/pointer-card';

/**
* Search result that sends the Markdown settings to the panel in Features.
*
* @param {object} props - Component props.
* @param {Function} props.getModule - Reads a module by slug.
* @return {import('react').ReactNode} The card.
*/
export const MarkdownMoved = ( { getModule } ) => (
<PointerCard
header={ __( 'Markdown', 'jetpack' ) }
href={ getModule( 'markdown' )?.configure_url }
linkText={ __( 'Open the Markdown settings', 'jetpack' ) }
>
<p>{ __( 'The Markdown settings are now in Features, in My Jetpack.', 'jetpack' ) }</p>
</PointerCard>
);
Loading
Loading