Back

0% Statements 0/27
0% Branches 0/40
0% Functions 0/9
0% Lines 0/27

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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219                                                                                                                                                                                                                                                                                                                                                                                                                                                     
import { getRedirectUrl } from '@automattic/jetpack-components';
import { ToggleControl } from '@wordpress/components';
import { createInterpolateElement } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { Component } from 'react';
import { connect } from 'react-redux';
import CompactCard from 'components/card/compact';
import { withModuleSettingsFormHelpers } from 'components/module-settings/with-module-settings-form-helpers';
import SettingsCard from 'components/settings-card';
import SettingsGroup from 'components/settings-group';
import { getModule } from 'state/modules';
import { updateSettings } from 'state/settings';
 
export class CustomContentTypes extends Component {
	state = {
		testimonial: this.props.getOptionValue( 'jetpack_testimonial' ) || false,
		portfolio: this.props.getOptionValue( 'jetpack_portfolio' ) || false,
	};
 
	updateCPTs = type => {
		const deactivate = 'testimonial' === type ? ! this.state.testimonial : ! this.state.portfolio;
 
		if ( type === 'portfolio' ) {
			this.props.updateSettings( { jetpack_portfolio: deactivate } );
		} else {
			this.props.updateSettings( { jetpack_testimonial: deactivate } );
		}
 
		this.setState( {
			[ type ]: ! this.state[ type ],
		} );
	};
 
	linkIfActiveCPT = type => {
		return this.props.getSettingCurrentValue( `jetpack_${ type }` ) ? (
			<a href={ `${ this.props.siteAdminUrl }edit.php?post_type=jetpack-${ type }` } />
		) : (
			<span />
		);
	};
 
	handleTestimonialToggleChange = () => {
		this.updateCPTs( 'testimonial' );
	};
 
	handlePortfolioToggleChange = () => {
		this.updateCPTs( 'portfolio' );
	};
 
	render() {
		if ( ! this.props.customContentTypeIsActive ) {
			return null;
		}
 
		const disabledByOverride = this.props.customContentTypeIsOverridden;
 
		const woa_theme_supports_jetpack_portfolio =
			typeof jetpack_portfolio_theme_supports !== 'undefined'
				? jetpack_portfolio_theme_supports // eslint-disable-line no-undef
				: false;
		const portfolioText = woa_theme_supports_jetpack_portfolio
			? createInterpolateElement(
					__(
						'Use <portfolioLink>portfolios</portfolioLink> on your site to showcase your best work. If your theme doesn’t support Jetpack Portfolios, you can still use a simple shortcode to display them on your site. This feature is enabled by default in your theme settings.',
						'jetpack'
					),
					{
						portfolioLink: this.linkIfActiveCPT( 'portfolio' ),
					}
			  )
			: createInterpolateElement(
					__(
						'Use <portfolioLink>portfolios</portfolioLink> on your site to showcase your best work. If your theme doesn’t support Jetpack Portfolios, you can still use a simple shortcode to display them on your site.',
						'jetpack'
					),
					{
						portfolioLink: this.linkIfActiveCPT( 'portfolio' ),
					}
			  );
 
		const siteShouldDisplayTestimonials = window?.CUSTOM_CONTENT_TYPE__INITIAL_STATE
			?.should_show_testimonials
			? window.CUSTOM_CONTENT_TYPE__INITIAL_STATE.should_show_testimonials
			: false;
 
		const siteShouldDisplayPortfolios = window?.CUSTOM_CONTENT_TYPE__INITIAL_STATE
			?.should_show_portfolios
			? window.CUSTOM_CONTENT_TYPE__INITIAL_STATE.should_show_portfolios
			: false;
 
		const woa_theme_supports_jetpack_testimonial =
			typeof jetpack_testimonial_theme_supports !== 'undefined'
				? jetpack_testimonial_theme_supports // eslint-disable-line no-undef
				: false;
		const testimonialText = woa_theme_supports_jetpack_testimonial
			? createInterpolateElement(
					__(
						'Use <testimonialLink>testimonials</testimonialLink> on your site to showcase your best work. If your theme doesn’t support Jetpack Testimonials, you can still use a simple shortcode to display them on your site. This feature is enabled by default in your theme settings.',
						'jetpack'
					),
					{
						testimonialLink: this.linkIfActiveCPT( 'testimonial' ),
					}
			  )
			: createInterpolateElement(
					__(
						'Use <testimonialLink>testimonials</testimonialLink> on your site to showcase your best work. If your theme doesn’t support Jetpack Testimonials, you can still use a simple shortcode to display them on your site.',
						'jetpack'
					),
					{
						testimonialLink: this.linkIfActiveCPT( 'testimonial' ),
					}
			  );
 
		return (
			<SettingsCard
				{ ...this.props }
				module="custom-content-types"
				header="Custom content types"
				hideButton
			>
				{ siteShouldDisplayTestimonials && (
					<SettingsGroup
						hasChild
						module={ { module: 'custom-content-types' } }
						support={ {
							link: getRedirectUrl( 'jetpack-support-custom-content-types' ),
						} }
					>
						<p> { testimonialText } </p>
						<ToggleControl
							__nextHasNoMarginBottom={ true }
							checked={
								this.props.getOptionValue( 'jetpack_testimonial' )
									? this.props.getOptionValue( 'jetpack_testimonial' )
									: false
							}
							disabled={ disabledByOverride || woa_theme_supports_jetpack_testimonial }
							onChange={ this.handleTestimonialToggleChange }
							label={
								<span className="jp-form-toggle-explanation">
									{ __( 'Testimonials', 'jetpack' ) }
								</span>
							}
							help={
								<span className="jp-form-setting-explanation jp-form-shortcode-setting-explanation">
									{ __( 'Testimonials shortcode: [testimonials]', 'jetpack' ) }
								</span>
							}
						/>
					</SettingsGroup>
				) }
				{ this.props.getOptionValue( 'jetpack_testimonial' ) && (
					<CompactCard
						className="jp-settings-card__configure-link"
						href={ `${ this.props.siteAdminUrl }post-new.php?post_type=jetpack-testimonial` }
					>
						{ __( 'Add a testimonial', 'jetpack' ) }
					</CompactCard>
				) }
				{ siteShouldDisplayPortfolios && (
					<SettingsGroup
						hasChild
						module={ { module: 'custom-content-types' } }
						support={ {
							link: getRedirectUrl( 'jetpack-support-custom-content-types' ),
						} }
					>
						<p>{ portfolioText }</p>
						<ToggleControl
							__nextHasNoMarginBottom={ true }
							checked={
								this.props.getOptionValue( 'jetpack_portfolio' )
									? this.props.getOptionValue( 'jetpack_portfolio' )
									: false
							}
							disabled={ disabledByOverride || woa_theme_supports_jetpack_portfolio }
							onChange={ this.handlePortfolioToggleChange }
							label={
								<span className="jp-form-toggle-explanation">
									{ __( 'Portfolios', 'jetpack' ) }
								</span>
							}
							help={
								<span className="jp-form-setting-explanation jp-form-shortcode-setting-explanation">
									{ __( 'Portfolios shortcode: [portfolio]', 'jetpack' ) }
								</span>
							}
						/>
					</SettingsGroup>
				) }
				{ this.props.getOptionValue( 'jetpack_portfolio' ) && (
					<CompactCard
						className="jp-settings-card__configure-link"
						href={ `${ this.props.siteAdminUrl }post-new.php?post_type=jetpack-portfolio` }
					>
						{ __( 'Add a portfolio item', 'jetpack' ) }
					</CompactCard>
				) }
			</SettingsCard>
		);
	}
}
 
export default withModuleSettingsFormHelpers(
	connect(
		state => {
			return {
				module: module_name => getModule( state, module_name ),
			};
		},
		dispatch => ( {
			updateSettings: module_option => {
				return dispatch( updateSettings( module_option ) );
			},
		} )
	)( CustomContentTypes )
);