Back

89.23% Statements 58/65
64.17% Branches 43/67
80% Functions 12/15
89.23% Lines 58/65

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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555                                                                1x   1x                                                                                             1x   2x                       2x       2x                           2x 6x                 2x 6x                   2x 6x               2x 8x   8x     2x 6x     2x 12x 8x     4x     2x 1x   1x       1x                     1x 1x   1x 1x   1x 1x           1x     2x 1x   1x   1x 1x           2x       2x 18x 18x                             6x 6x 6x 6x 6x       6x                     6x 66x     66x   6x   6x               6x 6x 6x 6x     6x 6x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
import { getRedirectUrl } from '@automattic/jetpack-components';
import {
	FacebookLinkPreview,
	TwitterLinkPreview,
	GoogleSearchPreview,
} from '@automattic/social-previews';
import { ToggleControl } from '@wordpress/components';
import { __, _x, _n, sprintf } from '@wordpress/i18n';
import { Stack, Text } from '@wordpress/ui';
import clsx from 'clsx';
import { Component } from 'react';
import { connect } from 'react-redux';
import { SocialLogo } from 'social-logos';
import Button from 'components/button';
import FoldableCard from 'components/foldable-card';
import { FormLabel, FormTextarea, FormFieldset } from 'components/forms';
import { withModuleSettingsFormHelpers } from 'components/module-settings/with-module-settings-form-helpers';
import { ModuleToggle } from 'components/module-toggle';
import SimpleNotice from 'components/notice';
import SettingsCard from 'components/settings-card';
import SettingsGroup from 'components/settings-group';
import analytics from 'lib/analytics';
import { FEATURE_ADVANCED_SEO } from 'lib/plans/constants';
import {
	getSiteIcon,
	isSeoEnhancerAvailable,
	getSiteRepresentativeImage,
} from 'state/initial-state';
import { siteHasFeature } from 'state/site';
import { isFetchingPluginsData, isPluginActive } from 'state/site/plugins';
import CustomSeoTitles from './seo/custom-seo-titles.jsx';
 
const OPEN_GRAPH_SETTINGS_OPTION = 'jetpack_social_open_graph_settings';
 
export const conflictingSeoPluginsList = [
	{
		name: 'Yoast SEO',
		slug: 'wordpress-seo/wp-seo.php',
	},
	{
		name: 'Yoast SEO Premium',
		slug: 'wordpress-seo-premium/wp-seo-premium.php',
	},
	{
		name: 'All In One SEO Pack',
		slug: 'all-in-one-seo-pack/all_in_one_seo_pack.php',
	},
	{
		name: 'All in One SEO Pack Pro',
		slug: 'all-in-one-seo-pack-pro/all_in_one_seo_pack.php',
	},
	{
		name: 'SEOPress',
		slug: 'wp-seopress/seopress.php',
	},
	{
		name: 'SEOPress Pro',
		slug: 'wp-seopress-pro/seopress-pro.php',
	},
	{
		name: 'Rank Math',
		slug: 'seo-by-rank-math/rank-math.php',
	},
	{
		name: 'SEOKEY',
		slug: 'seo-key/seo-key.php',
	},
	{
		name: 'SEOKEY Pro',
		slug: 'seo-key-pro/seo-key.php',
	},
	{
		name: 'Slim SEO',
		slug: 'slim-seo/slim-seo.php',
	},
	{
		name: 'The SEO Framework',
		slug: 'autodescription/autodescription.php',
	},
];
 
export const SEO = withModuleSettingsFormHelpers(
	class extends Component {
		constants = {
			frontPageMetaMaxLength: 300,
			frontPageMetaSuggestedLength: 159,
			moduleOptionsArray: [
				'advanced_seo_front_page_description',
				'advanced_seo_title_formats',
				OPEN_GRAPH_SETTINGS_OPTION,
				'ai_seo_enhancer_enabled',
			],
			siteIconPreviewSize: 512,
		};
 
		state = {
			defaultSocialImageUrl: undefined,
		};
 
		toggleSeoEnhancer = () => {
			const isEnabled = this.props.getOptionValue( 'ai_seo_enhancer_enabled' );
 
			analytics.tracks.recordEvent( 'jetpack_wpa_settings_toggle', {
				module: 'seo-tools',
				setting: 'ai_seo_enhancer_enabled',
				toggled: ! isEnabled ? 'on' : 'off',
			} );
 
			this.props.updateOptions( {
				ai_seo_enhancer_enabled: ! isEnabled,
			} );
		};
 
		SocialPreviewGoogle = siteData => (
			<GoogleSearchPreview
				siteIcon={ siteData.siteIcon }
				siteTitle={ siteData.title }
				title={ siteData.title }
				url={ siteData.url }
				description={ siteData.frontPageMetaDescription }
			/>
		);
 
		SocialPreviewFacebook = siteData => (
			<FacebookLinkPreview
				title={ siteData.title }
				url={ siteData.url }
				type="website"
				imageMode="landscape"
				description={ siteData.frontPageMetaDescription }
				image={ siteData.socialImage }
			/>
		);
 
		SocialPreviewTwitter = siteData => (
			<TwitterLinkPreview
				title={ siteData.title }
				url={ siteData.url }
				description={ siteData.frontPageMetaDescription }
				image={ siteData.socialImage }
			/>
		);
 
		getOpenGraphSettings = () => {
			const settings = this.props.getOptionValue( OPEN_GRAPH_SETTINGS_OPTION );
 
			return settings && 'object' === typeof settings ? settings : { default_image_id: 0 };
		};
 
		getDefaultSocialImageId = () => {
			return Number( this.getOpenGraphSettings().default_image_id || 0 );
		};
 
		getDefaultSocialImagePreview = () => {
			if ( undefined !== this.state.defaultSocialImageUrl ) {
				return this.state.defaultSocialImageUrl;
			}
 
			return this.props.siteRepresentativeImage;
		};
 
		selectDefaultSocialImage = event => {
			event.preventDefault();
 
			Iif ( ! window.wp?.media ) {
				return;
			}
 
			const frame = window.wp.media( {
				title: __( 'Select default social image', 'jetpack' ),
				button: {
					text: __( 'Use this image', 'jetpack' ),
				},
				library: {
					type: 'image',
				},
				multiple: false,
			} );
 
			frame.on( 'select', () => {
				const attachment = frame.state().get( 'selection' ).first().toJSON();
				const imageUrl =
					attachment.sizes?.large?.url || attachment.sizes?.full?.url || attachment.url;
				const settings = this.getOpenGraphSettings();
 
				this.setState( { defaultSocialImageUrl: imageUrl } );
				this.props.updateFormStateOptionValue( OPEN_GRAPH_SETTINGS_OPTION, {
					...settings,
					default_image_id: Number( attachment.id || 0 ),
				} );
			} );
 
			frame.open();
		};
 
		removeDefaultSocialImage = event => {
			event.preventDefault();
 
			const settings = this.getOpenGraphSettings();
 
			this.setState( { defaultSocialImageUrl: '' } );
			this.props.updateFormStateOptionValue( OPEN_GRAPH_SETTINGS_OPTION, {
				...settings,
				default_image_id: 0,
			} );
		};
 
		updateCustomSeoTitleInputState = newCustomSeoTitles => {
			this.props.updateFormStateOptionValue( 'advanced_seo_title_formats', newCustomSeoTitles );
		};
 
		saveButton = props => {
			const isSaving = this.props.isSavingAnyOption( this.constants.moduleOptionsArray );
			return (
				<Button primary rna compact type="submit" disabled={ isSaving || ! props.isDirty() }>
					{ isSaving
						? _x( 'Saving…', 'Button caption', 'jetpack' )
						: _x(
								'Save settings',
								'Button caption',
								'jetpack',
								/* dummy arg to avoid bad minification */ 0
						  ) }
				</Button>
			);
		};
 
		render() {
			const seo = this.props.getModule( 'seo-tools' ),
				hasSeoTools = !! seo.module,
				isSeoActive = this.props.getOptionValue( seo.module ),
				customSeoTitles = this.props.getOptionValue( 'advanced_seo_title_formats' ),
				frontPageMetaDescription = this.props.getOptionValue(
					'advanced_seo_front_page_description'
				);
 
			const siteData = {
				title: this.props.siteData.name || '',
				tagline: this.props.siteData.description || '',
				url: this.props.siteData.URL || '',
				siteIcon: this.props.siteIcon || '',
				socialImage: this.getDefaultSocialImagePreview(),
				frontPageMetaDescription: frontPageMetaDescription
					? frontPageMetaDescription
					: this.props.siteData.description || '',
			};
 
			const conflictingSeoPlugins = conflictingSeoPluginsList.reduce( ( acc, plugin ) => {
				Iif ( isPluginActive( this.props.state, plugin.slug ) ) {
					acc.push( plugin );
				}
				return acc;
			}, [] );
			const hasConflictingSeoPlugin = conflictingSeoPlugins.length > 0;
 
			const frontPageMetaCharCountClasses = clsx( {
				'jp-seo-front-page-description-count': true,
				'jp-seo-front-page-description-count-max':
					frontPageMetaDescription.length >= this.constants.frontPageMetaMaxLength,
				'jp-seo-front-page-description-count-warn':
					frontPageMetaDescription.length > this.constants.frontPageMetaSuggestedLength &&
					frontPageMetaDescription.length < this.constants.frontPageMetaMaxLength,
			} );
			const defaultSocialImageId = this.getDefaultSocialImageId();
			const defaultSocialImagePreview = this.getDefaultSocialImagePreview();
			const selectDefaultSocialImageLabel = __( 'Select image', 'jetpack' );
			const replaceDefaultSocialImageLabel = __( 'Replace image', 'jetpack' );
 
			// Destructure feature out to ensure our explicit prop takes precedence
			const { feature: _ignoredFeature, ...restProps } = this.props;
			return (
				<SettingsCard
					{ ...restProps }
					header={ _x( 'Search engine optimization', 'Settings header', 'jetpack' ) }
					feature={ FEATURE_ADVANCED_SEO }
					module={ seo.module }
					saveDisabled={ this.props.isSavingAnyOption( this.constants.moduleOptionsArray ) }
					hideButton={ hasConflictingSeoPlugin || ! hasSeoTools }
				>
					{ hasSeoTools && (
						<SettingsGroup
							hasChild
							disableInOfflineMode
							module={ seo }
							support={ {
								text: __(
									'Allows you to optimize your site and its content for better results in search engines.',
									'jetpack'
								),
								link: getRedirectUrl( 'jetpack-support-seo-tools' ),
							} }
						>
							{ hasConflictingSeoPlugin && (
								<SimpleNotice showDismiss={ false }>
									{ sprintf(
										/* translators: %s is the name of conflicting SEO plugin */
										__( 'Your SEO settings are managed by the following plugin: %s', 'jetpack' ),
										conflictingSeoPlugins[ 0 ].name
									) }
								</SimpleNotice>
							) }
							<p>
								{ __(
									'Take control of the way search engines represent your site. With Jetpack’s SEO tools you can preview how your content will look on popular search engines and change items like your site name and tagline in seconds.',
									'jetpack'
								) }
							</p>
							<ModuleToggle
								slug="seo-tools"
								activated={ isSeoActive }
								disabled={
									this.props.isSavingAnyOption( [
										seo.module,
										...this.constants.moduleOptionsArray,
									] ) || hasConflictingSeoPlugin
								}
								toggleModule={ this.props.toggleModuleNow }
							>
								<span className="jp-form-toggle-explanation">
									{ __( 'Customize your SEO settings', 'jetpack' ) }
								</span>
							</ModuleToggle>
							{ this.props.seoEnhancerAvailable && this.props.hasSeoEnhancer && (
								<FormFieldset>
									<ToggleControl
										__nextHasNoMarginBottom={ true }
										id="seo-enhancer"
										disabled={
											! this.props.getOptionValue( 'seo-tools' ) || ! this.props.hasSeoEnhancer
										}
										checked={
											this.props.hasSeoEnhancer &&
											this.props.getOptionValue( 'ai_seo_enhancer_enabled' )
										}
										onChange={ this.toggleSeoEnhancer }
										label={
											<span className="jp-form-toggle-explanation">
												{ __(
													'Automatically generate SEO title, SEO description, and image alt text for new posts',
													'jetpack'
												) }
											</span>
										}
									/>
								</FormFieldset>
							) }
						</SettingsGroup>
					) }
					<SettingsGroup
						module={ { module: 'canonical-urls' } }
						support={ {
							text: __(
								'Adds canonical URL tags to archive pages to prevent duplicate content in search engines.',
								'jetpack'
							),
							link: getRedirectUrl( 'jetpack-support-canonical-urls' ),
						} }
					>
						<ModuleToggle
							slug="canonical-urls"
							activated={ this.props.getOptionValue( 'canonical-urls' ) }
							disabled={
								this.props.isSavingAnyOption( [
									'canonical-urls',
									...this.constants.moduleOptionsArray,
								] ) || hasConflictingSeoPlugin
							}
							toggleModule={ this.props.toggleModuleNow }
						>
							<span className="jp-form-toggle-explanation">
								{ __( 'Add canonical URLs to archive pages', 'jetpack' ) }
							</span>
						</ModuleToggle>
						<p className="jp-form-setting-explanation">
							{ __(
								'Adds a rel="canonical" link to archive pages, helping search engines identify the preferred URL and avoid indexing duplicate content.',
								'jetpack'
							) }
						</p>
					</SettingsGroup>
					{ isSeoActive &&
						! isFetchingPluginsData( this.props.state ) &&
						! hasConflictingSeoPlugin && (
							<>
								<FoldableCard
									header={ __(
										'Expand to customize the page title structures of your site.',
										'jetpack'
									) }
									clickableHeader={ true }
									className="jp-seo-custom-titles-card"
								>
									<SettingsGroup>
										<p>
											{ __(
												'You can set the structure of page titles for different sections of your site. Doing this will change the way your site title is displayed in search engines, social media sites, and browser tabs.',
												'jetpack'
											) }
										</p>
										<CustomSeoTitles
											customSeoTitles={ customSeoTitles }
											updateCustomSeoTitleInputState={ this.updateCustomSeoTitleInputState }
											siteData={ siteData }
										/>
										{
											<div className={ 'jp-seo-custom-titles-save-button' }>
												{ this.saveButton( this.props ) }
											</div>
										}
									</SettingsGroup>
								</FoldableCard>
								<FoldableCard
									header={ __( 'Expand to edit your front page meta description.', 'jetpack' ) }
									clickableHeader={ true }
									className="jp-seo-front-page-description-card"
								>
									<SettingsGroup>
										<p style={ { clear: 'both' } }>
											{ __(
												'Craft a description of your Website: up to 160 characters that will be used in search engine results for your front page, and when your website is shared on social media sites.',
												'jetpack'
											) }
										</p>
										<FormLabel htmlFor="jp-seo-front-page-description">
											<span className="jp-form-label-wide">
												{ __( 'Front Page Meta Description', 'jetpack' ) }
											</span>
										</FormLabel>
										<div className="jp-seo-front-page-description-container">
											<FormTextarea
												name="advanced_seo_front_page_description"
												id="jp-seo-front-page-description"
												className="jp-form-textarea-wide"
												maxLength={ this.constants.frontPageMetaMaxLength }
												value={ frontPageMetaDescription }
												onChange={ this.props.onOptionChange }
											/>
											<div className={ frontPageMetaCharCountClasses }>
												{ sprintf(
													/* translators: %d: the number of characters */
													_n(
														'%d character',
														'%d characters',
														frontPageMetaDescription.length,
														'jetpack'
													),
													frontPageMetaDescription.length
												) }
												{ frontPageMetaDescription.length >=
													this.constants.frontPageMetaMaxLength &&
													' - ' + __( 'Maximum characters reached.', 'jetpack' ) }
											</div>
										</div>
										{
											<div className={ 'jp-seo-front-page-description-save-button' }>
												{ this.saveButton( this.props ) }
											</div>
										}
									</SettingsGroup>
								</FoldableCard>
								<FoldableCard
									header={ __( 'Expand to choose a default social image and preview.', 'jetpack' ) }
									clickableHeader={ true }
									className="jp-seo-social-previews"
								>
									<div className="jp-seo-default-social-image">
										<p className="jp-form-setting-explanation">
											{ __(
												"Choose an image for social previews when a post doesn't have its own image, and preview how your homepage will look on Google, Facebook, and X.",
												'jetpack'
											) }
										</p>
										{ !! defaultSocialImageId && !! defaultSocialImagePreview && (
											<div className="jp-seo-default-social-image-preview">
												<img
													src={ defaultSocialImagePreview }
													alt={ __( 'Default social image preview', 'jetpack' ) }
													width={ 550 }
													height={ 288 }
												/>
											</div>
										) }
										<Stack direction="row" gap="sm" wrap="wrap">
											<Button
												id="jp-seo-default-social-image-button"
												rna
												compact
												type="button"
												onClick={ this.selectDefaultSocialImage }
												disabled={ ! window.wp?.media }
											>
												{ defaultSocialImageId
													? replaceDefaultSocialImageLabel
													: selectDefaultSocialImageLabel }
											</Button>
											{ !! defaultSocialImageId && (
												<Button rna compact type="button" onClick={ this.removeDefaultSocialImage }>
													{ __( 'Remove image', 'jetpack' ) }
												</Button>
											) }
										</Stack>
										<Text
											variant="body-sm"
											render={ <p className="jp-form-setting-explanation" /> }
										>
											{ __( 'Recommended size is 1200x630px and < 600 KB.', 'jetpack' ) }
										</Text>
										<div className="jp-seo-default-social-image-save-button">
											{ this.saveButton( this.props ) }
										</div>
									</div>
									<hr />
									<div className="jp-seo-social-previews-container">
										<SocialLogo icon="google" size={ 24 } />
										<span className="jp-seo-social-previews-label">
											{ __( 'Google search', 'jetpack' ) }
										</span>
									</div>
									{ this.SocialPreviewGoogle( siteData ) }
 
									<hr />
									<div className="jp-seo-social-previews-container">
										<SocialLogo icon="facebook" size={ 24 } />
										<span className="jp-seo-social-previews-label">
											{ __( 'Facebook', 'jetpack' ) }
										</span>
									</div>
									{ this.SocialPreviewFacebook( siteData ) }
 
									<hr />
									<div className="jp-seo-social-previews-container">
										<SocialLogo icon="x" size={ 24 } />
										<span className="jp-seo-social-previews-label">{ __( 'X', 'jetpack' ) }</span>
									</div>
									{ this.SocialPreviewTwitter( siteData ) }
								</FoldableCard>
							</>
						) }
				</SettingsCard>
			);
		}
	}
);
 
export default connect( state => {
	return {
		siteData: state.jetpack.siteData.data,
		siteIcon: getSiteIcon( state ),
		siteRepresentativeImage: getSiteRepresentativeImage( state ),
		seoEnhancerAvailable: isSeoEnhancerAvailable( state ),
		state,
		hasSeoEnhancer: siteHasFeature( state, 'ai-seo-enhancer' ),
	};
} )( SEO );