step-content.jsx 11.8 KB
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
/**
 * External dependencies
 */
import React from 'react';

/**
 * WordPress dependencies
 */
const { __, sprintf } = wp.i18n;

export default ({
	currentStep,
	serverType,
	rulesMethod,
	setRulesMethod,
	setServerType,
	rulesError,
	smushData,
	makeRequest,
}) => {
	const stepsHeading = {
		1: {
			title: __('Choose Server Type', 'wp-smushit'),
			description: __(
				'Choose your server type. If you don’t know this, please contact your hosting provider.',
				'wp-smushit'
			),
		},
		2: {
			title: __('Add Rules', 'wp-smushit'),
			description:
				'apache' === serverType
					? __(
							'Smush can automatically apply WebP conversion rules for Apache servers by writing to your .htaccess file. Alternatively, switch to Manual to apply these rules yourself.',
							'wp-smushit'
					  )
					: __(
							'The following configurations are for NGINX servers. If you do not have access to your NGINX config files you will need to contact your hosting provider to make these changes.',
							'wp-smushit'
					  ),
		},
		3: {
			title: __('Finish Setup', 'wp-smushit'),
			description: __(
				'The rules have been applied successfully.',
				'wp-smushit'
			),
		},
	};

	const getTopNotice = () => {
		if (1 === currentStep && smushData.isS3Enabled) {
			return (
				<div className="sui-notice sui-notice-warning">
					<div className="sui-notice-content">
						<div className="sui-notice-message">
							<span
								className="sui-notice-icon sui-icon-info sui-md"
								aria-hidden="true"
							></span>
							<p>
								{__(
									'We noticed the Amazon S3 Integration is enabled. Offloaded images will not be served in WebP format, but Smush will create local WebP copies of all images. If this is undesirable, you can quit the setup.',
									'wp-smushit'
								)}
							</p>
						</div>
					</div>
				</div>
			);
		}

		if (2 === currentStep) {
			return (
				<div
					role="alert"
					className="sui-notice sui-notice-warning"
					aria-live="assertive"
					style={rulesError ? { display: 'block' } : {}}
				>
					{rulesError && (
						<div className="sui-notice-content">
							<div className="sui-notice-message">
								<span
									className="sui-notice-icon sui-icon-info sui-md"
									aria-hidden="true"
								></span>
								<p
									dangerouslySetInnerHTML={{
										__html: rulesError,
									}}
								/>
							</div>
						</div>
					)}
				</div>
			);
		}

		if (smushData.isWpmudevHost) {
			const message = !smushData.isWhitelabel
				? __(
						'Since your site is hosted with WPMU DEV, we already have done the configurations steps for you. The only step for you would be to create WebP images below.',
						'wp-smushit'
				  )
				: __(
						'WebP conversion is active and working well. Your hosting has automatically pre-configured the conversion for you. The only step for you would be to create WebP images below.',
						'wp-smushit'
				  );

			return (
				<div className="sui-notice sui-notice-info">
					<div className="sui-notice-content">
						<div className="sui-notice-message">
							<span
								className="sui-notice-icon sui-icon-info sui-md"
								aria-hidden="true"
							></span>
							<p>{message}</p>
						</div>
					</div>
				</div>
			);
		}
	};

	const getStepContent = () => {
		if (1 === currentStep) {
			return (
				<React.Fragment>
					<div className="sui-box-selectors">
						<ul>
							<li>
								<label
									htmlFor="smush-wizard-server-type-apache"
									className="sui-box-selector"
								>
									<input
										id="smush-wizard-server-type-apache"
										type="radio"
										value="apache"
										checked={'apache' === serverType}
										onChange={(e) =>
											setServerType(e.currentTarget.value)
										}
									/>
									<span>{__('Apache', 'wp-smushit')}</span>
								</label>
							</li>

							<li>
								<label
									htmlFor="smush-wizard-server-type-nginx"
									className="sui-box-selector"
								>
									<input
										id="smush-wizard-server-type-nginx"
										type="radio"
										value="nginx"
										checked={'nginx' === serverType}
										onChange={(e) =>
											setServerType(e.currentTarget.value)
										}
									/>
									<span>{__('NGINX', 'wp-smushit')}</span>
								</label>
							</li>
						</ul>
					</div>

					<div className="sui-notice" style={{ textAlign: 'left' }}>
						<div className="sui-notice-content">
							<div className="sui-notice-message">
								<span
									className="sui-notice-icon sui-icon-info sui-md"
									aria-hidden="true"
								></span>
								<p>
									{sprintf(
										/* translators: server type */
										__(
											"We've automatically detected your server type is %s. If this is incorrect, manually select your server type to generate the relevant rules and instructions.",
											'wp-smushit'
										),
										'nginx' === smushData.detectedServer
											? 'NGINX'
											: 'Apache / LiteSpeed'
									)}
								</p>
							</div>
						</div>
					</div>
				</React.Fragment>
			);
		}

		if (2 === currentStep) {
			if ('nginx' === serverType) {
				return (
					<div className="smush-wizard-rules-wrapper">
						<ol className="sui-description">
							<li>
								{__(
									'Insert the following in the server context of your configuration file (usually found in /etc/nginx/sites-available). The server context refers to the part of the configuration that starts with server { and ends with the matching }.',
									'wp-smushit'
								)}
							</li>
							<li>
								{__(
									'Copy the generated code found below and paste it inside your http or server blocks.',
									'wp-smushit'
								)}
							</li>
						</ol>

						<pre
							className="sui-code-snippet"
							style={{ marginLeft: '12px' }}
						>
							{smushData.nginxRules}
						</pre>
						<ol className="sui-description" start="3">
							<li>{__('Reload NGINX.', 'wp-smushit')}</li>
						</ol>

						<p className="sui-description">
							{__('Still having trouble?', 'wp_smushit')}{' '}
							<a
								href={smushData.urls.support}
								target="_blank"
								rel="noreferrer"
							>
								{__('Get Support.', 'wp_smushit')}
							</a>
						</p>
					</div>
				);
			}

			// TODO: The non-selected button isn't focusable this way. Why arrows don't workkkkkkk?
			return (
				<div className="sui-side-tabs sui-tabs">
					<div role="tablist" className="sui-tabs-menu">
						<button
							type="button"
							role="tab"
							id="smush-tab-automatic"
							className={
								'sui-tab-item' +
								('automatic' === rulesMethod ? ' active' : '')
							}
							aria-controls="smush-tab-content-automatic"
							aria-selected={'automatic' === rulesMethod}
							onClick={() => setRulesMethod('automatic')}
							tabIndex={'automatic' === rulesMethod ? '0' : '-1'}
						>
							{__('Automatic', 'wp-smushit')}
						</button>

						<button
							type="button"
							role="tab"
							id="smush-tab-manual"
							className={
								'sui-tab-item' +
								('manual' === rulesMethod ? ' active' : '')
							}
							aria-controls="smush-tab-content-manual"
							aria-selected={'manual' === rulesMethod}
							onClick={() => setRulesMethod('manual')}
							tabIndex={'manual' === rulesMethod ? '0' : '-1'}
						>
							{__('Manual', 'wp-smushit')}
						</button>
					</div>

					<div className="sui-tabs-content">
						<div
							role="tabpanel"
							tabIndex="0"
							id="smush-tab-content-automatic"
							className={
								'sui-tab-content' +
								('automatic' === rulesMethod ? ' active' : '')
							}
							aria-labelledby="smush-tab-automatic"
							hidden={'automatic' !== rulesMethod}
						>
							<p
								className="sui-description"
								style={{ marginTop: '30px' }}
							>
								{__(
									'Please note: Some servers have both Apache and NGINX software which may not begin serving WebP images after applying the .htaccess rules. If errors occur after applying the rules, we recommend adding NGINX rules manually.',
									'wp-smushit'
								)}
							</p>
						</div>

						<div
							role="tabpanel"
							tabIndex="0"
							id="smush-tab-content-manual"
							className={
								'sui-tab-content' +
								('manual' === rulesMethod ? ' active' : '')
							}
							aria-labelledby="smush-tab-manual"
							hidden={'manual' !== rulesMethod}
						>
							<p className="sui-description">
								{__(
									'If you are unable to get the automated method working, follow these steps:',
									'wp-smushit'
								)}
							</p>

							<div className="smush-wizard-rules-wrapper">
								<ol className="sui-description">
									<li>
										{__(
											'Copy the generated code below and paste it at the top of your .htaccess file (before any existing code) in the root directory.',
											'wp-smushit'
										)}
									</li>
								</ol>

								<pre
									className="sui-code-snippet"
									style={{ marginLeft: '12px' }}
								>
									{smushData.apacheRules}
								</pre>
								<ol className="sui-description" start="2">
									<li>
										{__(
											"Next, click Check Status button below to see if it's working.",
											'wp-smushit'
										)}
									</li>
								</ol>

								<h5
									className="sui-settings-label"
									style={{
										marginTop: '30px',
										fontSize: '13px',
										color: '#333333',
									}}
								>
									{__('Troubleshooting', 'wp-smushit')}
								</h5>

								<p className="sui-description">
									{__(
										'If .htaccess does not work, and you have access to vhosts.conf or httpd.conf, try this:',
										'wp-smushit'
									)}
								</p>

								<ol className="sui-description">
									<li>
										{__(
											'Look for your site in the file and find the line that starts with <Directory> - add the code above that line and into that section and save the file.',
											'wp-smushit'
										)}
									</li>
									<li>
										{__('Reload Apache.', 'wp-smushit')}
									</li>
									<li>
										{__(
											"If you don't know where those files are, or you aren't able to reload Apache, you would need to consult with your hosting provider or a system administrator who has access to change the configuration of your server.",
											'wp-smushit'
										)}
									</li>
								</ol>

								<p className="sui-description">
									{__('Still having trouble?', 'wp_smushit')}{' '}
									<a
										href={smushData.urls.support}
										target="_blank"
										rel="noreferrer"
									>
										{__('Get Support.', 'wp_smushit')}
									</a>
								</p>
							</div>
						</div>
					</div>
				</div>
			);
		}

		const hideWizard = (e) => {
			e.preventDefault();
			makeRequest('smush_toggle_webp_wizard').then(() => {
				location.href = smushData.urls.bulkPage;
			});
		};

		return (
			<React.Fragment>
				<p style={{ marginBottom: 0 }}><b>{__('Convert Images to WebP', 'wp-smushit')}</b></p>
				<p className="sui-description" dangerouslySetInnerHTML={ { __html: smushData.thirdStepMsg } } />
				{!smushData.isMultisite && (
					<p>
						<a href={smushData.urls.bulkPage} onClick={hideWizard}>
							{__('Convert now', 'wp-smushit')}
						</a>
					</p>
				)}
			</React.Fragment>
		);
	};

	const stepIndicatorText = sprintf(
		/* translators: currentStep/totalSteps indicator */
		__('Step %s', 'wp-smushit'),
		currentStep + '/3'
	);

	return (
		<div
			className={`smush-wizard-steps-content-wrapper smush-wizard-step-${currentStep}`}
		>
			{getTopNotice()}
			<div className="smush-wizard-steps-content">
				<span className="smush-step-indicator">
					{stepIndicatorText}
				</span>
				<h2>{stepsHeading[currentStep].title}</h2>
				<p className="sui-description">
					{stepsHeading[currentStep].description}
				</p>
				{getStepContent()}
			</div>
		</div>
	);
};