11/*global tiobDash*/
22/* eslint-disable no-console */
33import {
4+ activateTheme ,
45 cleanupImport ,
56 importContent ,
67 importMods ,
78 importWidgets ,
89 installPlugins ,
910 installTheme ,
10- activateTheme ,
1111} from '../utils/site-import' ;
1212import { get } from '../utils/rest' ;
1313import { trailingSlashIt } from '../utils/common' ;
@@ -17,19 +17,24 @@ import classnames from 'classnames';
1717import ImportModalError from './ImportModalError' ;
1818import ImportModalMock from './ImportModalMock' ;
1919import CustomTooltip from './CustomTooltip' ;
20- import { useState , useEffect } from '@wordpress/element' ;
21- import { withSelect , withDispatch } from '@wordpress/data' ;
20+
21+ import {
22+ createInterpolateElement ,
23+ useEffect ,
24+ useState ,
25+ } from '@wordpress/element' ;
26+ import { withDispatch , withSelect } from '@wordpress/data' ;
2227import { __ , sprintf } from '@wordpress/i18n' ;
2328import { compose } from '@wordpress/compose' ;
2429
2530import {
2631 Button ,
2732 Icon ,
28- ToggleControl ,
2933 Modal ,
3034 Panel ,
3135 PanelBody ,
3236 PanelRow ,
37+ ToggleControl ,
3338} from '@wordpress/components' ;
3439
3540const ImportModal = ( {
@@ -45,9 +50,14 @@ const ImportModal = ( {
4550 customizer : true ,
4651 widgets : true ,
4752 cleanup : false ,
53+ performanceAddon : true ,
4854 theme_install : themeData !== false ,
4955 } ) ;
56+
5057 const [ themeInstallProgress , setThemeInstallProgress ] = useState ( false ) ;
58+ const [ performanceAddonProgress , setPerformanceAddonProgress ] = useState (
59+ false
60+ ) ;
5161 const [ cleanupProgress , setCleanupProgress ] = useState ( false ) ;
5262 const [ pluginsProgress , setPluginsProgress ] = useState ( false ) ;
5363 const [ contentProgress , setContentProgress ] = useState ( false ) ;
@@ -56,7 +66,7 @@ const ImportModal = ( {
5666 const [ frontPageID , setFrontPageID ] = useState ( null ) ;
5767 const [ currentStep , setCurrentStep ] = useState ( null ) ;
5868 const [ importing , setImporting ] = useState ( false ) ;
59- const [ pluginOptions , setPluginOptions ] = useState ( null ) ;
69+ const [ pluginOptions , setPluginOptions ] = useState ( { } ) ;
6070 const [ error , setError ] = useState ( null ) ;
6171 const [ importData , setImportData ] = useState ( null ) ;
6272 const [ fetching , setFetching ] = useState ( true ) ;
@@ -211,6 +221,44 @@ const ImportModal = ( {
211221 'templates-patterns-collection'
212222 ) ,
213223 } ,
224+ performanceAddon : {
225+ title : __ (
226+ 'Enable performance features for my site' ,
227+ 'templates-patterns-collection'
228+ ) ,
229+ icon : 'dashboard' ,
230+ tooltip : createInterpolateElement (
231+ __ (
232+ sprintf (
233+ // translators: %s is Optimole plugin name.
234+ 'Optimize and speed up your site with our trusted addon, <a><span>%s</span><icon/></a>. It’s free.' ,
235+ 'Optimole'
236+ ) ,
237+ 'templates-patterns-collection'
238+ ) ,
239+ {
240+ a : (
241+ < a
242+ href = "https://wordpress.org/plugins/optimole-wp/"
243+ target = { '_blank' }
244+ style = { {
245+ textDecoration : 'none' ,
246+ display : 'inline-flex' ,
247+ alignItems : 'center' ,
248+ } }
249+ />
250+ ) ,
251+ icon : (
252+ < Icon
253+ size = { 10 }
254+ icon = "external"
255+ style = { { marginLeft : 0 } }
256+ />
257+ ) ,
258+ span : < div /> ,
259+ }
260+ ) ,
261+ } ,
214262 } ;
215263
216264 if ( isCleanupAllowed === 'yes' ) {
@@ -255,7 +303,11 @@ const ImportModal = ( {
255303 < Icon icon = { icon } />
256304 < span > { title } </ span >
257305 { tooltip && (
258- < CustomTooltip > { tooltip } </ CustomTooltip >
306+ < CustomTooltip
307+ toLeft = { id === 'performanceAddon' }
308+ >
309+ { tooltip }
310+ </ CustomTooltip >
259311 ) }
260312 { id !== 'theme_install' && (
261313 < div className = "toggle-wrapper" >
@@ -285,6 +337,10 @@ const ImportModal = ( {
285337 ...( importData . mandatory_plugins || { } ) ,
286338 } ;
287339
340+ if ( Object . keys ( allPlugins ) . length < 1 ) {
341+ return null ;
342+ }
343+
288344 const toggleOpen = ( ) => {
289345 setPluginsOpened ( ! pluginsOpened ) ;
290346 } ;
@@ -417,7 +473,7 @@ const ImportModal = ( {
417473
418474 function runImportPlugins ( ) {
419475 // console.clear();
420- if ( ! pluginOptions ) {
476+ if ( ! pluginOptions && ! general . performanceAddon ) {
421477 console . log ( '[S] Plugins.' ) ;
422478 runImportContent ( ) ;
423479 return false ;
@@ -502,7 +558,7 @@ const ImportModal = ( {
502558 function runImportWidgets ( ) {
503559 if ( ! general . widgets ) {
504560 console . log ( '[S] Widgets.' ) ;
505- importDone ( ) ;
561+ runPerformanceAddonInstall ( ) ;
506562 return false ;
507563 }
508564 setCurrentStep ( 'widgets' ) ;
@@ -515,12 +571,35 @@ const ImportModal = ( {
515571 }
516572 console . log ( '[D] Widgets.' ) ;
517573 setWidgetsProgress ( 'done' ) ;
518- importDone ( ) ;
574+ runPerformanceAddonInstall ( ) ;
519575 } )
520576 . catch ( ( incomingError ) =>
521577 handleError ( incomingError , 'widgets' )
522578 ) ;
523579 }
580+ function runPerformanceAddonInstall ( ) {
581+ if ( ! general . performanceAddon ) {
582+ console . log ( '[S] Performance Addon.' ) ;
583+ importDone ( ) ;
584+ return false ;
585+ }
586+ setCurrentStep ( 'performanceAddon' ) ;
587+ console . log ( '[P] Performance Addon.' ) ;
588+
589+ installPlugins ( { 'optimole-wp' : true } )
590+ . then ( ( response ) => {
591+ if ( ! response . success ) {
592+ handleError ( response , 'performanceAddon' ) ;
593+ return false ;
594+ }
595+ console . log ( '[D] Performance Addon.' ) ;
596+ setPerformanceAddonProgress ( 'done' ) ;
597+ importDone ( ) ;
598+ } )
599+ . catch ( ( incomingError ) =>
600+ handleError ( incomingError , 'performanceAddon' )
601+ ) ;
602+ }
524603
525604 function importDone ( ) {
526605 setCurrentStep ( 'done' ) ;
@@ -566,6 +645,10 @@ const ImportModal = ( {
566645 'Something went wrong while importing the widgets.' ,
567646 'templates-patterns-collection'
568647 ) ,
648+ performanceAddon : __ (
649+ 'Something went wrong while installing the performance addon.' ,
650+ 'templates-patterns-collection'
651+ ) ,
569652 } ;
570653
571654 switch ( step ) {
@@ -584,6 +667,9 @@ const ImportModal = ( {
584667 case 'widgets' :
585668 setWidgetsProgress ( 'error' ) ;
586669 break ;
670+ case 'performanceAddon' :
671+ setPerformanceAddonProgress ( 'error' ) ;
672+ break ;
587673 }
588674 setError (
589675 incomingError . data
@@ -660,6 +746,7 @@ const ImportModal = ( {
660746 content : contentProgress ,
661747 customizer : customizerProgress ,
662748 widgets : widgetsProgress ,
749+ performanceAddon : performanceAddonProgress ,
663750 } }
664751 currentStep = { currentStep }
665752 willDo = { general }
0 commit comments