Skip to content
Closed
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,16 @@ export function currentThemeSupports( state, feature ) {
return get( state.jetpack.initialState.themeData, [ 'support', feature ], false );
}

/**
* Check that a theme is a block-enabled theme.
*
* @param {Object} state - Global state tree.
* @returns {boolean} Is the theme a block-enabled theme.
*/
export function isBlockTheme( state ) {
return get( state.jetpack.initialState.themeData, 'isBlockTheme', false );
}

/**
* Check if backups UI should be displayed.
*
Expand Down
19 changes: 3 additions & 16 deletions projects/plugins/jetpack/_inc/client/traffic/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ import { GoogleAnalytics } from './google-analytics';
import { Ads } from './ads';
import { SiteStats } from './site-stats';
import Shortlinks from './shortlinks';
import { RelatedPosts } from './related-posts';
import RelatedPosts from './related-posts';
import { VerificationServices } from './verification-services';
import Sitemaps from './sitemaps';
import { getLastPostUrl, isAtomicSite } from 'state/initial-state';
import { isAtomicSite } from 'state/initial-state';

export class Traffic extends React.Component {
static displayName = 'TrafficSettings';
Expand Down Expand Up @@ -94,19 +94,7 @@ export class Traffic extends React.Component {
} ) }
/>
) }
{ foundRelated && (
<RelatedPosts
{ ...commonProps }
configureUrl={
this.props.siteAdminUrl +
'customize.php?autofocus[section]=jetpack_relatedposts' +
'&return=' +
encodeURIComponent( this.props.siteAdminUrl + 'admin.php?page=jetpack#/traffic' ) +
'&url=' +
encodeURIComponent( this.props.lastPostUrl )
}
/>
) }
{ foundRelated && <RelatedPosts { ...commonProps } /> }
{ foundSeo && (
<SEO
{ ...commonProps }
Expand Down Expand Up @@ -142,7 +130,6 @@ export default connect( state => {
isUnavailableInOfflineMode: module_name => isUnavailableInOfflineMode( state, module_name ),
isModuleFound: module_name => isModuleFound( state, module_name ),
isSiteConnected: isSiteConnected( state ),
lastPostUrl: getLastPostUrl( state ),
getModuleOverride: module_name => getModuleOverride( state, module_name ),
isAtomicSite: isAtomicSite( state ),
hasConnectedOwner: hasConnectedOwner( state ),
Expand Down
284 changes: 177 additions & 107 deletions projects/plugins/jetpack/_inc/client/traffic/related-posts.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/**
* External dependencies
*/
import React from 'react';
import React, { Component } from 'react';
import { connect } from 'react-redux';

/**
* WordPress dependencies
Expand All @@ -21,22 +22,24 @@ import { ModuleToggle } from 'components/module-toggle';
import { withModuleSettingsFormHelpers } from 'components/module-settings/with-module-settings-form-helpers';
import SettingsCard from 'components/settings-card';
import SettingsGroup from 'components/settings-group';
import { isBlockTheme, getLastPostUrl } from 'state/initial-state';

class RelatedPostsComponent extends React.Component {
class RelatedPosts extends Component {
/**
* Get options for initial state.
*
* @returns {{show_headline: Boolean, show_thumbnails: Boolean}} Initial state object.
* @returns {{show_headline: boolean, show_thumbnails: boolean, append_to_posts: boolean}} Initial state object.
*/
state = {
show_headline: this.props.getOptionValue( 'show_headline', 'related-posts' ),
show_thumbnails: this.props.getOptionValue( 'show_thumbnails', 'related-posts' ),
append_to_posts: this.props.getOptionValue( 'append_to_posts', 'related-posts' ),
};

/**
* Update state so preview is updated instantly and toggle options.
*
* @param {string} optionName Slug of option to update.
* @param {string} optionName - Slug of option to update.
*/
updateOptions = optionName => {
this.setState(
Expand All @@ -55,11 +58,163 @@ class RelatedPostsComponent extends React.Component {
this.updateOptions( 'show_thumbnails' );
};

trackConfigureClick = () => {
analytics.tracks.recordJetpackClick( 'configure-related-posts' );
handleAppendPostsToggleChange = () => {
this.updateOptions( 'append_to_posts' );
};

trackConfigureClick = type => {
analytics.tracks.recordJetpackClick( {
target: 'configure-related-posts',
type, // Classic for classic themes (customizer link), // blockTheme for block themes (site editor link).
} );
};

configureLink() {
const { siteAdminUrl, isBlockEnabledTheme, lastPostUrl } = this.props;

// For block themes, folks will want to add the Related Posts block directly in the site editor.
if ( isBlockEnabledTheme ) {
return (
<Card
compact
className="jp-settings-card__configure-link"
onClick={ this.trackConfigureClick( 'blockTheme' ) }
href={ `${ siteAdminUrl }site-editor.php` }
>
{ __( 'Add a Related Posts Block to your site.', 'jetpack' ) }
</Card>
);
}

const configureUrl =
siteAdminUrl +
'customize.php?autofocus[section]=jetpack_relatedposts' +
'&return=' +
encodeURIComponent( siteAdminUrl + 'admin.php?page=jetpack#/traffic' ) +
'&url=' +
encodeURIComponent( lastPostUrl );

return (
<Card
compact
className="jp-settings-card__configure-link"
onClick={ this.trackConfigureClick( 'classic' ) }
href={ configureUrl }
>
{ __( 'Configure related posts in the Customizer', 'jetpack' ) }
</Card>
);
}

classicRelatedPostsSettings() {
return (
<>
<CompactFormToggle
checked={ this.state.append_to_posts }
disabled={ this.props.isSavingAnyOption( [ 'related-posts', 'append_to_posts' ] ) }
onChange={ this.handleAppendPostsToggleChange }
>
<span className="jp-form-toggle-explanation">
{ __( 'Automatically display related posts at the bottom of each post.', 'jetpack' ) }
</span>
</CompactFormToggle>
{ this.state.append_to_posts && (
<>
<CompactFormToggle
checked={ this.state.show_headline }
disabled={ this.props.isSavingAnyOption( [
'related-posts',
'show_headline',
'append_posts',
] ) }
onChange={ this.handleShowHeadlineToggleChange }
>
<span className="jp-form-toggle-explanation">
{ __( 'Highlight related content with a heading', 'jetpack' ) }
</span>
</CompactFormToggle>
<CompactFormToggle
checked={ this.state.show_thumbnails }
disabled={ this.props.isSavingAnyOption( [
'related-posts',
'show_thumbnails',
'append_posts',
] ) }
onChange={ this.handleShowThumbnailsToggleChange }
>
<span className="jp-form-toggle-explanation">
{ __( 'Show a thumbnail image where available', 'jetpack' ) }
</span>
</CompactFormToggle>
{ this.relatedPostsPreview() }
</>
) }
</>
);
}

relatedPostsPreview() {
const fakePosts = [
{
url: 'cat-blog.png',
text: __( 'Big iPhone/iPad Update Now Available', 'jetpack' ),
context: _x(
'In "Mobile"',
'It refers to the category where a post was found. Used in an example preview.',
'jetpack'
),
},
{
url: 'devices.jpg',
text: __( 'The WordPress for Android App Gets a Big Facelift', 'jetpack' ),
context: _x(
'In "Mobile"',
'It refers to the category where a post was found. Used in an example preview.',
'jetpack'
),
},
{
url: 'mobile-wedding.jpg',
text: __( 'Upgrade Focus: VideoPress For Weddings', 'jetpack' ),
context: _x(
'In "Upgrade"',
'It refers to the category where a post was found. Used in an example preview.',
'jetpack'
),
},
];

return (
<div>
<FormLabel className="jp-form-label-wide">
{ _x( 'Preview', 'A header for a preview area in the configuration screen.', 'jetpack' ) }
</FormLabel>
<Card className="jp-related-posts-preview">
{ this.state.show_headline && (
<div className="jp-related-posts-preview__title">{ __( 'Related', 'jetpack' ) }</div>
) }
{ fakePosts.map( ( item, index ) => (
<div key={ `preview_${ index }` } className="jp-related-posts-preview__item">
{ this.state.show_thumbnails && (
<img
src={ `https://jetpackme.files.wordpress.com/2019/03/${ item.url }` }
alt={ item.text }
/>
) }
<h4 className="jp-related-posts-preview__post-title">
<a href="#/traffic">{ item.text }</a>
</h4>
<p className="jp-related-posts-preview__post-context">{ item.context }</p>
</div>
) ) }
</Card>
</div>
);
}

render() {
const { isBlockEnabledTheme } = this.props;

const isRelatedPostsActive = this.props.getOptionValue( 'related-posts' ),
unavailableInOfflineMode = this.props.isUnavailableInOfflineMode( 'related-posts' );
return (
Expand All @@ -79,7 +234,7 @@ class RelatedPostsComponent extends React.Component {
<p>
{ createInterpolateElement(
__(
'Keep your visitors engaged with related content at the bottom of each post. These settings won’t apply to <a>related posts added using the block editor</a>.',
'Keep your visitors engaged with related content at the bottom of each post, or anywhere within your content thanks to the <a>Related Posts Block</a>.',
'jetpack'
),
{
Expand All @@ -101,111 +256,26 @@ class RelatedPostsComponent extends React.Component {
toggleModule={ this.props.toggleModuleNow }
>
<span className="jp-form-toggle-explanation">
{ __( 'Show related content after posts', 'jetpack' ) }
{ __( 'Show related content for each one of your posts.', 'jetpack' ) }

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.

Might it make sense to note that this feature doesn't work for block themes as part of this explanation?

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.

The general toggle still applies to block themes, so I think it's okay there. What doesn't work with block themes is hidden from the UI when you use such a theme.

</span>
</ModuleToggle>
<FormFieldset>
<CompactFormToggle
checked={ this.state.show_headline }
disabled={
! isRelatedPostsActive ||
unavailableInOfflineMode ||
this.props.isSavingAnyOption( [ 'related-posts', 'show_headline' ] )
}
onChange={ this.handleShowHeadlineToggleChange }
>
<span className="jp-form-toggle-explanation">
{ __( 'Highlight related content with a heading', 'jetpack' ) }
</span>
</CompactFormToggle>
<CompactFormToggle
checked={ this.state.show_thumbnails }
disabled={
! isRelatedPostsActive ||
unavailableInOfflineMode ||
this.props.isSavingAnyOption( [ 'related-posts', 'show_thumbnails' ] )
}
onChange={ this.handleShowThumbnailsToggleChange }
>
<span className="jp-form-toggle-explanation">
{ __( 'Show a thumbnail image where available', 'jetpack' ) }
</span>
</CompactFormToggle>
{ isRelatedPostsActive && (
<div>
<FormLabel className="jp-form-label-wide">
{ _x(
'Preview',
'A header for a preview area in the configuration screen.',
'jetpack'
) }
</FormLabel>
<Card className="jp-related-posts-preview">
{ this.state.show_headline && (
<div className="jp-related-posts-preview__title">
{ __( 'Related', 'jetpack' ) }
</div>
) }
{ [
{
url: 'cat-blog.png',
text: __( 'Big iPhone/iPad Update Now Available', 'jetpack' ),
context: _x(
'In "Mobile"',
'It refers to the category where a post was found. Used in an example preview.',
'jetpack'
),
},
{
url: 'devices.jpg',
text: __( 'The WordPress for Android App Gets a Big Facelift', 'jetpack' ),
context: _x(
'In "Mobile"',
'It refers to the category where a post was found. Used in an example preview.',
'jetpack'
),
},
{
url: 'mobile-wedding.jpg',
text: __( 'Upgrade Focus: VideoPress For Weddings', 'jetpack' ),
context: _x(
'In "Upgrade"',
'It refers to the category where a post was found. Used in an example preview.',
'jetpack'
),
},
].map( ( item, index ) => (
<div key={ `preview_${ index }` } className="jp-related-posts-preview__item">
{ this.state.show_thumbnails && (
<img
src={ `https://jetpackme.files.wordpress.com/2019/03/${ item.url }` }
alt={ item.text }
/>
) }
<h4 className="jp-related-posts-preview__post-title">
<a href="#/traffic">{ item.text }</a>
</h4>
<p className="jp-related-posts-preview__post-context">{ item.context }</p>
</div>
) ) }
</Card>
</div>
) }
</FormFieldset>
{ isRelatedPostsActive && (
<FormFieldset>
{ ! isBlockEnabledTheme &&
! unavailableInOfflineMode &&
this.classicRelatedPostsSettings() }
</FormFieldset>
) }
</SettingsGroup>
{ ! this.props.isUnavailableInOfflineMode( 'related-posts' ) && isRelatedPostsActive && (
<Card
compact
className="jp-settings-card__configure-link"
onClick={ this.trackConfigureClick }
href={ this.props.configureUrl }
>
{ __( 'Configure related posts in the Customizer', 'jetpack' ) }
</Card>
) }
{ ! unavailableInOfflineMode && isRelatedPostsActive && this.configureLink() }
</SettingsCard>
);
}
}

export const RelatedPosts = withModuleSettingsFormHelpers( RelatedPostsComponent );
export default connect( state => {
return {
isBlockEnabledTheme: isBlockTheme( state ),
lastPostUrl: getLastPostUrl( state ),
};
} )( withModuleSettingsFormHelpers( RelatedPosts ) );
Loading