Import.js
412 lines
| 1 | /* global tiobDash */ |
| 2 | /* eslint-disable no-console */ |
| 3 | import { __ } from '@wordpress/i18n'; |
| 4 | import { useEffect, useState } from '@wordpress/element'; |
| 5 | import { withDispatch, withSelect } from '@wordpress/data'; |
| 6 | import { compose } from '@wordpress/compose'; |
| 7 | import { Icon } from '@wordpress/components'; |
| 8 | |
| 9 | import { |
| 10 | activateTheme, |
| 11 | cleanupImport, |
| 12 | importContent, |
| 13 | importMods, |
| 14 | importWidgets, |
| 15 | installPlugins, |
| 16 | installTheme, |
| 17 | } from '../../utils/site-import'; |
| 18 | |
| 19 | import ImportProgress from '../ImportProgress'; |
| 20 | import ImportError from '../ImportError'; |
| 21 | import ImportForm from '../ImportForm'; |
| 22 | |
| 23 | const Import = ( { |
| 24 | general, |
| 25 | pluginOptions, |
| 26 | themeData, |
| 27 | error, |
| 28 | setError, |
| 29 | importData, |
| 30 | editor, |
| 31 | setThemeAction, |
| 32 | importing, |
| 33 | setImporting, |
| 34 | } ) => { |
| 35 | const [ currentStep, setCurrentStep ] = useState( null ); |
| 36 | const [ actionsDone, setActionsDone ] = useState( 0 ); |
| 37 | |
| 38 | function runImportCleanup() { |
| 39 | console.clear(); |
| 40 | if ( ! general.cleanup ) { |
| 41 | console.log( '[S] Cleanup.' ); |
| 42 | runImport(); |
| 43 | return false; |
| 44 | } |
| 45 | setCurrentStep( 'cleanup' ); |
| 46 | console.log( '[P] Cleanup.' ); |
| 47 | cleanupImport( {} ) |
| 48 | .then( ( response ) => { |
| 49 | if ( ! response.success ) { |
| 50 | handleError( response, 'cleanup' ); |
| 51 | return false; |
| 52 | } |
| 53 | console.log( '[D] Cleanup.' ); |
| 54 | setActionsDone( ( prevActionsDone ) => prevActionsDone + 1 ); |
| 55 | runImport(); |
| 56 | } ) |
| 57 | .catch( ( incomingError ) => |
| 58 | handleError( incomingError, 'cleanup' ) |
| 59 | ); |
| 60 | } |
| 61 | |
| 62 | function runImport() { |
| 63 | // console.clear(); |
| 64 | if ( ! themeData ) { |
| 65 | console.log( '[S] Theme.' ); |
| 66 | runImportPlugins(); |
| 67 | return false; |
| 68 | } |
| 69 | if ( themeData.action === 'install' ) { |
| 70 | setCurrentStep( 'theme_install' ); |
| 71 | console.log( '[P] Theme Install.' ); |
| 72 | handleThemeInstall(); |
| 73 | return false; |
| 74 | } |
| 75 | setCurrentStep( 'theme_install' ); |
| 76 | console.log( '[P] Theme Activate.' ); |
| 77 | handleActivate(); |
| 78 | } |
| 79 | |
| 80 | function handleThemeInstall() { |
| 81 | const callbackSuccess = () => { |
| 82 | setThemeAction( { ...themeData, action: 'activate' } ); |
| 83 | console.log( '[D] Theme Install.' ); |
| 84 | handleActivate(); |
| 85 | }; |
| 86 | |
| 87 | const callbackError = ( err ) => { |
| 88 | setThemeAction( { ...themeData, action: 'activate' } ); |
| 89 | handleError( |
| 90 | err.errorMessage || |
| 91 | __( |
| 92 | 'Could not install theme.', |
| 93 | 'templates-patterns-collection' |
| 94 | ), |
| 95 | 'theme_install' |
| 96 | ); |
| 97 | }; |
| 98 | |
| 99 | installTheme( 'neve', callbackSuccess, callbackError ); |
| 100 | } |
| 101 | |
| 102 | function handleActivate() { |
| 103 | const callbackSuccess = () => { |
| 104 | console.log( '[D] Theme Activate.' ); |
| 105 | setActionsDone( ( prevActionsDone ) => prevActionsDone + 1 ); |
| 106 | setThemeAction( false ); |
| 107 | runImportPlugins(); |
| 108 | }; |
| 109 | |
| 110 | const callbackError = () => { |
| 111 | handleError( |
| 112 | __( |
| 113 | 'Could not activate theme.', |
| 114 | 'templates-patterns-collection' |
| 115 | ), |
| 116 | 'theme_install' |
| 117 | ); |
| 118 | }; |
| 119 | |
| 120 | activateTheme( themeData, callbackSuccess, callbackError ); |
| 121 | } |
| 122 | |
| 123 | function runImportPlugins() { |
| 124 | if ( |
| 125 | ! pluginOptions && |
| 126 | ! general.performanceAddon |
| 127 | ) { |
| 128 | console.log( '[S] Plugins.' ); |
| 129 | runImportContent(); |
| 130 | return false; |
| 131 | } |
| 132 | setCurrentStep( 'plugins' ); |
| 133 | console.log( '[P] Plugins.' ); |
| 134 | installPlugins( pluginOptions ) |
| 135 | .then( ( response ) => { |
| 136 | if ( ! response.success ) { |
| 137 | handleError( response, 'plugins' ); |
| 138 | return false; |
| 139 | } |
| 140 | console.log( '[D] Plugins.' ); |
| 141 | setActionsDone( ( prevActionsDone ) => prevActionsDone + 1 ); |
| 142 | try { |
| 143 | runImportContent(); |
| 144 | } catch ( incomingError ) { |
| 145 | handleError( incomingError, 'content' ); |
| 146 | } |
| 147 | } ) |
| 148 | .catch( ( incomingError ) => |
| 149 | handleError( incomingError, 'plugins' ) |
| 150 | ); |
| 151 | } |
| 152 | |
| 153 | function runImportContent() { |
| 154 | if ( ! general.content ) { |
| 155 | console.log( '[S] Content.' ); |
| 156 | runImportCustomizer(); |
| 157 | return false; |
| 158 | } |
| 159 | |
| 160 | if ( ! importData || ! importData.content_file ) { |
| 161 | handleError( { data: 'ti__ob_missing_import_data' }, 'content' ); |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | setCurrentStep( 'content' ); |
| 166 | console.log( '[P] Content.' ); |
| 167 | importContent( { |
| 168 | contentFile: importData.content_file, |
| 169 | source: 'remote', |
| 170 | frontPage: importData.front_page, |
| 171 | shopPages: importData.shop_pages, |
| 172 | paymentForms: importData.payment_forms, |
| 173 | masteriyoData: importData.masteriyo_data, |
| 174 | demoSlug: importData.slug, |
| 175 | editor, |
| 176 | } ) |
| 177 | .then( ( response ) => { |
| 178 | if ( ! response.success ) { |
| 179 | handleError( response, 'content' ); |
| 180 | return false; |
| 181 | } |
| 182 | console.log( '[D] Content.' ); |
| 183 | setActionsDone( ( prevActionsDone ) => prevActionsDone + 1 ); |
| 184 | runImportCustomizer(); |
| 185 | } ) |
| 186 | .catch( ( incomingError ) => |
| 187 | handleError( incomingError, 'content' ) |
| 188 | ); |
| 189 | } |
| 190 | |
| 191 | function runImportCustomizer() { |
| 192 | if ( ! general.customizer ) { |
| 193 | console.log( '[S] Customizer.' ); |
| 194 | runImportWidgets(); |
| 195 | return false; |
| 196 | } |
| 197 | setCurrentStep( 'customizer' ); |
| 198 | console.log( '[P] Customizer.' ); |
| 199 | importMods( { |
| 200 | source_url: importData.url, |
| 201 | theme_mods: importData.theme_mods, |
| 202 | wp_options: importData.wp_options, |
| 203 | } ) |
| 204 | .then( ( response ) => { |
| 205 | if ( ! response.success ) { |
| 206 | handleError( response, 'customizer' ); |
| 207 | return false; |
| 208 | } |
| 209 | console.log( '[D] Customizer.' ); |
| 210 | setActionsDone( ( prevActionsDone ) => prevActionsDone + 1 ); |
| 211 | runImportWidgets(); |
| 212 | } ) |
| 213 | .catch( ( incomingError ) => |
| 214 | handleError( incomingError, 'customizer' ) |
| 215 | ); |
| 216 | } |
| 217 | |
| 218 | function runImportWidgets() { |
| 219 | if ( ! general.widgets ) { |
| 220 | console.log( '[S] Widgets.' ); |
| 221 | runPerformanceAddonInstall(); |
| 222 | return false; |
| 223 | } |
| 224 | setCurrentStep( 'widgets' ); |
| 225 | console.log( '[P] Widgets.' ); |
| 226 | importWidgets( { |
| 227 | widgets: importData.widgets, |
| 228 | source_url: importData.url, |
| 229 | } ) |
| 230 | .then( ( response ) => { |
| 231 | if ( ! response.success ) { |
| 232 | handleError( response, 'widgets' ); |
| 233 | return false; |
| 234 | } |
| 235 | console.log( '[D] Widgets.' ); |
| 236 | setActionsDone( ( prevActionsDone ) => prevActionsDone + 1 ); |
| 237 | runPerformanceAddonInstall(); |
| 238 | } ) |
| 239 | .catch( ( incomingError ) => |
| 240 | handleError( incomingError, 'widgets' ) |
| 241 | ); |
| 242 | } |
| 243 | |
| 244 | function runPerformanceAddonInstall() { |
| 245 | if ( ! general.performanceAddon ) { |
| 246 | console.log( '[S] Performance Addon.' ); |
| 247 | importDone(); |
| 248 | return false; |
| 249 | } |
| 250 | setCurrentStep( 'performanceAddon' ); |
| 251 | console.log( '[P] Performance Addon.' ); |
| 252 | |
| 253 | installPlugins( { 'optimole-wp': true } ) |
| 254 | .then( ( response ) => { |
| 255 | if ( ! response.success ) { |
| 256 | handleError( response, 'performanceAddon' ); |
| 257 | return false; |
| 258 | } |
| 259 | console.log( '[D] Performance Addon.' ); |
| 260 | setActionsDone( ( prevActionsDone ) => prevActionsDone + 1 ); |
| 261 | importDone(); |
| 262 | } ) |
| 263 | .catch( ( incomingError ) => |
| 264 | handleError( incomingError, 'performanceAddon' ) |
| 265 | ); |
| 266 | } |
| 267 | |
| 268 | function importDone() { |
| 269 | setCurrentStep( 'done' ); |
| 270 | tiobDash.cleanupAllowed = 'yes'; |
| 271 | setImporting( false ); |
| 272 | } |
| 273 | |
| 274 | function handleError( incomingError, step ) { |
| 275 | setImporting( false ); |
| 276 | setCurrentStep( null ); |
| 277 | |
| 278 | const map = { |
| 279 | cleanup: __( |
| 280 | 'Something went wrong while cleaning the previous import.', |
| 281 | 'templates-patterns-collection' |
| 282 | ), |
| 283 | plugins: __( |
| 284 | 'Something went wrong while installing the necessary plugins.', |
| 285 | 'templates-patterns-collection' |
| 286 | ), |
| 287 | content: __( |
| 288 | 'Something went wrong while importing the website content.', |
| 289 | 'templates-patterns-collection' |
| 290 | ), |
| 291 | customizer: __( |
| 292 | 'Something went wrong while updating the customizer settings.', |
| 293 | 'templates-patterns-collection' |
| 294 | ), |
| 295 | widgets: __( |
| 296 | 'Something went wrong while importing the widgets.', |
| 297 | 'templates-patterns-collection' |
| 298 | ), |
| 299 | performanceAddon: __( |
| 300 | 'Something went wrong while installing the performance addon.', |
| 301 | 'templates-patterns-collection' |
| 302 | ), |
| 303 | }; |
| 304 | |
| 305 | setError( |
| 306 | incomingError.data |
| 307 | ? { message: map[ step ], code: incomingError.data } |
| 308 | : { message: map[ step ] } |
| 309 | ); |
| 310 | } |
| 311 | |
| 312 | useEffect( () => { |
| 313 | setImporting( true ); |
| 314 | runImportCleanup(); |
| 315 | }, [] ); |
| 316 | |
| 317 | if ( error ) { |
| 318 | return ( |
| 319 | <div className="ob-container narrow center"> |
| 320 | <ImportError |
| 321 | message={ error.message || null } |
| 322 | code={ error.code || null } |
| 323 | /> |
| 324 | <hr /> |
| 325 | </div> |
| 326 | ); |
| 327 | } |
| 328 | |
| 329 | return ( |
| 330 | <div className="ob-container narrow center"> |
| 331 | { 'done' !== currentStep && importing ? ( |
| 332 | <> |
| 333 | <div className="ob-importing-header-wrap"> |
| 334 | <h1> |
| 335 | { __( |
| 336 | 'We are importing your new site…', |
| 337 | 'templates-patterns-collection' |
| 338 | ) } |
| 339 | </h1> |
| 340 | <p> |
| 341 | { __( |
| 342 | 'Sit tight as we import a website based on your preferences.', |
| 343 | 'templates-patterns-collection' |
| 344 | ) } |
| 345 | </p> |
| 346 | </div> |
| 347 | <ImportProgress |
| 348 | actionsDone={ actionsDone } |
| 349 | currentStep={ currentStep } |
| 350 | actionsNb={ |
| 351 | Object.values( general ).filter( |
| 352 | ( value ) => value === true |
| 353 | ).length |
| 354 | } |
| 355 | /> |
| 356 | </> |
| 357 | ) : ( |
| 358 | <> |
| 359 | { 'done' === currentStep && ! importing && ( |
| 360 | <div className="ob-import-done"> |
| 361 | <Icon icon="yes-alt" /> |
| 362 | <h1> |
| 363 | { __( |
| 364 | 'Import complete.', |
| 365 | 'templates-patterns-collection' |
| 366 | ) } |
| 367 | <br /> |
| 368 | { __( |
| 369 | 'Enjoy your site!', |
| 370 | 'templates-patterns-collection' |
| 371 | ) } |
| 372 | </h1> |
| 373 | <ImportForm /> |
| 374 | </div> |
| 375 | ) } |
| 376 | </> |
| 377 | ) } |
| 378 | </div> |
| 379 | ); |
| 380 | }; |
| 381 | |
| 382 | export default compose( |
| 383 | withSelect( ( select ) => { |
| 384 | const { |
| 385 | getCurrentEditor, |
| 386 | getCurrentSite, |
| 387 | getThemeAction, |
| 388 | getError, |
| 389 | getPluginOptions, |
| 390 | getImportData, |
| 391 | } = select( 'ti-onboarding' ); |
| 392 | return { |
| 393 | error: getError(), |
| 394 | editor: getCurrentEditor(), |
| 395 | siteData: getCurrentSite(), |
| 396 | themeData: getThemeAction() || false, |
| 397 | pluginOptions: getPluginOptions(), |
| 398 | importData: getImportData(), |
| 399 | }; |
| 400 | } ), |
| 401 | withDispatch( ( dispatch ) => { |
| 402 | const { setImportModalStatus, setThemeAction, setError } = |
| 403 | dispatch( 'ti-onboarding' ); |
| 404 | |
| 405 | return { |
| 406 | setModal: ( status ) => setImportModalStatus( status ), |
| 407 | setThemeAction: ( status ) => setThemeAction( status ), |
| 408 | setError, |
| 409 | }; |
| 410 | } ) |
| 411 | )( Import ); |
| 412 |