Back

62.5% Statements 20/32
66.66% Branches 34/51
46.66% Functions 7/15
62.5% Lines 20/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193                                                            1x     8x   8x       8x         8x             8x       8x                                                             8x                     8x       8x                       8x 2x       6x       6x   6x     6x                                                                                   4x                   30x             4x   4x        
import { __ } from '@wordpress/i18n';
import { Component } from 'react';
import { connect } from 'react-redux';
import Card from 'components/card';
import QuerySite from 'components/data/query-site';
import {
	isMovedOut,
	isSettingsConsolidationEnabled,
	isSwitchedInFeatures,
} from 'lib/settings-consolidation';
import {
	isOfflineMode,
	isUnavailableInOfflineMode,
	isCurrentUserLinked,
	getConnectUrl,
} from 'state/connection';
import { getActiveFeatureDetails } from 'state/feature-check';
import { userCanManageModules, userCanEditPosts } from 'state/initial-state';
import { isModuleActivated, getModuleOverride, getModule } from 'state/modules';
import { getSearchTerm, isModuleFound } from 'state/search';
import { getSettings } from 'state/settings';
import { CarouselMoved } from './carousel-moved';
import Composing from './composing';
import CustomContentTypes from './custom-content-types';
import PostByEmail from './post-by-email';
import ThemeEnhancements from './theme-enhancements';
import Widgets from './widgets';
import WritingMedia from './writing-media';
 
export class Writing extends Component {
	static displayName = 'WritingSettings';
 
	constructor( props ) {
		super( props );
 
		const customContentTypeStatusInitialState = window?.CUSTOM_CONTENT_TYPE__INITIAL_STATE?.active
			? window.CUSTOM_CONTENT_TYPE__INITIAL_STATE.active
			: false;
 
		const customContentTypeOverrideStatusInitialState = window?.CUSTOM_CONTENT_TYPE__INITIAL_STATE
			?.over_ride
			? window.CUSTOM_CONTENT_TYPE__INITIAL_STATE.over_ride
			: false;
 
		this.state = {
			customContentTypeIsActive: customContentTypeStatusInitialState,
			customContentTypeIsOverridden: customContentTypeOverrideStatusInitialState,
			customContentKeywords: [],
		};
 
		// Call async initialization directly
		this.initializeCustomContentTypes();
	}
 
	initializeCustomContentTypes() {
		this.props
			.getActiveFeatureDetails()
			.then( response => {
				if ( response && response[ 'custom-content-types' ].active !== undefined ) {
					this.setState( {
						customContentTypeIsActive: response[ 'custom-content-types' ].active,
						customContentTypeIsOverridden: response[ 'custom-content-types' ].over_ride,
						customContentKeywords: [
							...response[ 'custom-content-types' ].additional_search_queries
								.split( ',' )
								.map( keyword => keyword.trim().toLowerCase() ),
							...response[ 'custom-content-types' ].description
								.toLowerCase()
								.split( ' ' )
								.map( word => word.trim() ),
						],
					} );
				} else {
					this.setState( { customContentTypeIsActive: false } );
				}
			} )
			.catch( error => {
				// eslint-disable-next-line no-console
				console.error( 'Error fetching custom content type status:', error );
 
				// Update state to reflect that the route doesn't exist or an error occurred
				this.setState( { customContentTypeIsActive: false } );
			} );
	}
 
	render() {
		const commonProps = {
			settings: this.props.settings,
			getModule: this.props.module,
			isOfflineMode: this.props.isOfflineMode,
			isUnavailableInOfflineMode: this.props.isUnavailableInOfflineMode,
			isLinked: this.props.isLinked,
			getModuleOverride: this.props.getModuleOverride,
			customContentTypeIsActive: this.state.customContentTypeIsActive || false,
			customContentTypeIsOverridden: this.state.customContentTypeIsOverridden || false,
		};
 
		Iif ( ! this.props.searchTerm && ! this.props.active ) {
			return null;
		}
 
		const found = [
			'carousel',
			'copy-post',
			'latex',
			'markdown',
			'shortcodes',
			'post-by-email',
			'infinite-scroll',
			'widgets',
			'widget-visibility',
			'blocks',
		].some( this.props.isModuleFound );
		if ( ! found && ! this.state.customContentTypeIsActive ) {
			return null;
		}
 
		const shouldRenderCustomContent =
			( this.state.customContentTypeIsActive && this.props.searchTerm === '' ) ||
			( this.state.customContentTypeIsActive &&
				this.state.customContentKeywords.includes( this.props.searchTerm?.toLowerCase() ) );
 
		const showComposing = this.props.userCanManageModules || this.props.userCanEditPosts,
			showPostByEmail =
				this.props.userCanManageModules ||
				( this.props.userCanEditPosts && this.props.isModuleActivated( 'post-by-email' ) );
 
		return (
			<div>
				<QuerySite />
				<h1 className="screen-reader-text">{ __( 'Jetpack Writing Settings', 'jetpack' ) }</h1>
				{ this.props.searchTerm && (
					<h2 className="jp-settings__section-title">{ __( 'Writing', 'jetpack' ) }</h2>
				) }
				{ this.props.isModuleFound( 'carousel' ) &&
					( isSettingsConsolidationEnabled() ? (
						this.props.searchTerm && <CarouselMoved getModule={ this.props.module } />
					) : (
						<WritingMedia { ...commonProps } />
					) ) }
				{ showComposing && (
					<Composing { ...commonProps } userCanManageModules={ this.props.userCanManageModules } />
				) }
				{ shouldRenderCustomContent && <CustomContentTypes { ...commonProps } /> }
				<ThemeEnhancements { ...commonProps } />
				<Widgets { ...commonProps } />
				{ this.props.isModuleFound( 'post-by-email' ) && showPostByEmail && (
					<PostByEmail
						{ ...commonProps }
						connectUrl={ this.props.connectUrl }
						isLinked={ this.props.isLinked }
						userCanManageModules={ this.props.userCanManageModules }
					/>
				) }
				{ ! showComposing && ! showPostByEmail && (
					<Card>
						{ __(
							'Writing tools available to you will be shown here when an administrator enables them.',
							'jetpack'
						) }
					</Card>
				) }
			</div>
		);
	}
}
 
export default connect(
	state => {
		return {
			module: module_name => getModule( state, module_name ),
			settings: getSettings( state ),
			isOfflineMode: isOfflineMode( state ),
			isUnavailableInOfflineMode: module_name => isUnavailableInOfflineMode( state, module_name ),
			userCanEditPosts: userCanEditPosts( state ),
			isModuleActivated: module_name => isModuleActivated( state, module_name ),
			isLinked: isCurrentUserLinked( state ),
			userCanManageModules: userCanManageModules( state ),
			isModuleFound: module_name =>
				isModuleFound( state, module_name ) &&
				! isMovedOut( module_name, state ) &&
				( ! isSwitchedInFeatures( state, module_name ) || !! getSearchTerm( state ) ),
			connectUrl: getConnectUrl( state ),
			getModuleOverride: module_name => getModuleOverride( state, module_name ),
		};
	},
	dispatch => ( {
		getActiveFeatureDetails: () => {
			return dispatch( getActiveFeatureDetails( 'custom-content-types' ) );
		},
	} )
)( Writing );