2019-05-28 14:45:52 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2019-08-21 06:34:21 +00:00
|
|
|
import { __, sprintf } from '@wordpress/i18n';
|
2019-10-16 22:00:47 +00:00
|
|
|
import { addQueryArgs } from '@wordpress/url';
|
2019-05-28 14:45:52 +00:00
|
|
|
import apiFetch from '@wordpress/api-fetch';
|
2020-01-17 01:46:11 +00:00
|
|
|
import { uniq } from 'lodash';
|
2019-05-28 14:45:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2019-10-16 22:00:47 +00:00
|
|
|
import { getResourceIdentifier, getResourceName } from '../utils';
|
2020-01-17 01:46:11 +00:00
|
|
|
import { getSetting, setSetting } from '@woocommerce/wc-admin-settings';
|
2019-09-02 03:45:56 +00:00
|
|
|
import { JETPACK_NAMESPACE, WC_ADMIN_NAMESPACE } from '../constants';
|
|
|
|
import { pluginNames } from './constants';
|
2019-05-28 14:45:52 +00:00
|
|
|
|
|
|
|
function read( resourceNames, fetch = apiFetch ) {
|
2019-08-21 06:34:21 +00:00
|
|
|
return [
|
2019-08-23 12:56:57 +00:00
|
|
|
...readActivePlugins( resourceNames, fetch ),
|
2019-08-21 06:34:21 +00:00
|
|
|
...readProfileItems( resourceNames, fetch ),
|
2019-08-29 16:41:04 +00:00
|
|
|
...readJetpackStatus( resourceNames, fetch ),
|
2019-08-21 06:34:21 +00:00
|
|
|
...readJetpackConnectUrl( resourceNames, fetch ),
|
|
|
|
];
|
2019-05-28 14:45:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function update( resourceNames, data, fetch = apiFetch ) {
|
2019-08-21 06:34:21 +00:00
|
|
|
return [
|
|
|
|
...activatePlugins( resourceNames, data, fetch ),
|
|
|
|
...installPlugins( resourceNames, data, fetch ),
|
|
|
|
...updateProfileItems( resourceNames, data, fetch ),
|
|
|
|
];
|
2019-05-28 14:45:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function readProfileItems( resourceNames, fetch ) {
|
|
|
|
const resourceName = 'onboarding-profile';
|
|
|
|
|
|
|
|
if ( resourceNames.includes( resourceName ) ) {
|
2019-09-02 03:45:56 +00:00
|
|
|
const url = WC_ADMIN_NAMESPACE + '/onboarding/profile';
|
2019-05-28 14:45:52 +00:00
|
|
|
|
|
|
|
return [
|
|
|
|
fetch( { path: url } )
|
|
|
|
.then( profileItemsToResources )
|
2020-02-14 02:23:21 +00:00
|
|
|
.catch( ( error ) => {
|
|
|
|
return {
|
|
|
|
[ resourceName ]: { error: String( error.message ) },
|
|
|
|
};
|
2019-05-28 14:45:52 +00:00
|
|
|
} ),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateProfileItems( resourceNames, data, fetch ) {
|
|
|
|
const resourceName = 'onboarding-profile';
|
|
|
|
|
|
|
|
if ( resourceNames.includes( resourceName ) ) {
|
2019-09-02 03:45:56 +00:00
|
|
|
const url = WC_ADMIN_NAMESPACE + '/onboarding/profile';
|
2019-05-28 14:45:52 +00:00
|
|
|
|
|
|
|
return [
|
|
|
|
fetch( {
|
|
|
|
path: url,
|
|
|
|
method: 'POST',
|
|
|
|
data: data[ resourceName ],
|
|
|
|
} )
|
2020-02-14 02:23:21 +00:00
|
|
|
.then(
|
|
|
|
profileItemToResource.bind( null, data[ resourceName ] )
|
|
|
|
)
|
|
|
|
.catch( ( error ) => {
|
2019-05-28 14:45:52 +00:00
|
|
|
return { [ resourceName ]: { error } };
|
|
|
|
} ),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
function profileItemsToResources( items ) {
|
|
|
|
const resourceName = 'onboarding-profile';
|
|
|
|
|
|
|
|
const itemKeys = Object.keys( items );
|
|
|
|
|
|
|
|
const resources = {};
|
2020-02-14 02:23:21 +00:00
|
|
|
itemKeys.forEach( ( key ) => {
|
2019-05-28 14:45:52 +00:00
|
|
|
const item = items[ key ];
|
|
|
|
resources[ getResourceName( resourceName, key ) ] = { data: item };
|
|
|
|
} );
|
|
|
|
|
|
|
|
return {
|
|
|
|
[ resourceName ]: {
|
|
|
|
data: itemKeys,
|
|
|
|
},
|
|
|
|
...resources,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function profileItemToResource( items ) {
|
|
|
|
const resourceName = 'onboarding-profile';
|
|
|
|
|
|
|
|
const resources = {};
|
2020-02-14 02:23:21 +00:00
|
|
|
Object.keys( items ).forEach( ( key ) => {
|
2019-05-28 14:45:52 +00:00
|
|
|
const item = items[ key ];
|
|
|
|
resources[ getResourceName( resourceName, key ) ] = { data: item };
|
|
|
|
} );
|
|
|
|
|
2019-11-01 02:04:54 +00:00
|
|
|
return {
|
|
|
|
[ resourceName ]: {
|
|
|
|
lastReceived: Date.now(),
|
|
|
|
},
|
|
|
|
...resources,
|
|
|
|
};
|
2019-05-28 14:45:52 +00:00
|
|
|
}
|
|
|
|
|
2019-08-23 12:56:57 +00:00
|
|
|
function readActivePlugins( resourceNames, fetch ) {
|
|
|
|
const resourceName = 'active-plugins';
|
|
|
|
if ( resourceNames.includes( resourceName ) ) {
|
2019-09-02 03:45:56 +00:00
|
|
|
const url = WC_ADMIN_NAMESPACE + '/onboarding/plugins/active';
|
2019-08-23 12:56:57 +00:00
|
|
|
|
|
|
|
return [
|
|
|
|
fetch( { path: url } )
|
|
|
|
.then( activePluginsToResources )
|
2020-02-14 02:23:21 +00:00
|
|
|
.catch( ( error ) => {
|
|
|
|
return {
|
|
|
|
[ resourceName ]: { error: String( error.message ) },
|
|
|
|
};
|
2019-08-23 12:56:57 +00:00
|
|
|
} ),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
function activePluginsToResources( items ) {
|
|
|
|
const { plugins } = items;
|
|
|
|
const resourceName = 'active-plugins';
|
|
|
|
return {
|
|
|
|
[ resourceName ]: {
|
|
|
|
data: plugins,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function activatePlugins( resourceNames, data, fetch ) {
|
|
|
|
const resourceName = 'plugin-activate';
|
|
|
|
if ( resourceNames.includes( resourceName ) ) {
|
|
|
|
const plugins = data[ resourceName ];
|
2019-09-02 03:45:56 +00:00
|
|
|
const url = WC_ADMIN_NAMESPACE + '/onboarding/plugins/activate';
|
2019-08-23 12:56:57 +00:00
|
|
|
return [
|
|
|
|
fetch( {
|
|
|
|
path: url,
|
|
|
|
method: 'POST',
|
|
|
|
data: {
|
|
|
|
plugins: plugins.join( ',' ),
|
|
|
|
},
|
|
|
|
} )
|
2020-02-14 02:23:21 +00:00
|
|
|
.then( ( response ) =>
|
|
|
|
activatePluginToResource( response, plugins )
|
|
|
|
)
|
|
|
|
.catch( ( error ) => {
|
2019-08-23 12:56:57 +00:00
|
|
|
const resources = { [ resourceName ]: { error } };
|
2020-02-14 02:23:21 +00:00
|
|
|
Object.keys( plugins ).forEach( ( key ) => {
|
2019-08-23 12:56:57 +00:00
|
|
|
const pluginError = { ...error };
|
|
|
|
const item = plugins[ key ];
|
2020-02-14 02:23:21 +00:00
|
|
|
pluginError.message = getPluginErrorMessage(
|
|
|
|
'activate',
|
|
|
|
item
|
|
|
|
);
|
|
|
|
resources[ getResourceName( resourceName, item ) ] = {
|
|
|
|
error: pluginError,
|
|
|
|
};
|
2019-08-23 12:56:57 +00:00
|
|
|
} );
|
|
|
|
return resources;
|
|
|
|
} ),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
function activatePluginToResource( response, items ) {
|
|
|
|
const resourceName = 'plugin-activate';
|
2020-01-17 01:46:11 +00:00
|
|
|
const { activePlugins = [] } = getSetting( 'onboarding', {} );
|
|
|
|
setSetting( 'onboarding', {
|
|
|
|
...getSetting( 'onboarding', {} ),
|
|
|
|
activePlugins: uniq( [ ...activePlugins, ...items ] ),
|
|
|
|
} );
|
2019-08-23 12:56:57 +00:00
|
|
|
|
|
|
|
const resources = {
|
|
|
|
[ resourceName ]: { data: items },
|
2020-02-14 02:23:21 +00:00
|
|
|
'active-plugins': { data: response.active },
|
2019-08-23 12:56:57 +00:00
|
|
|
};
|
2020-02-14 02:23:21 +00:00
|
|
|
Object.keys( items ).forEach( ( key ) => {
|
2019-08-23 12:56:57 +00:00
|
|
|
const item = items[ key ];
|
|
|
|
resources[ getResourceName( resourceName, item ) ] = { data: item };
|
|
|
|
} );
|
|
|
|
return resources;
|
|
|
|
}
|
|
|
|
|
2019-08-29 16:41:04 +00:00
|
|
|
function readJetpackStatus( resourceNames, fetch ) {
|
|
|
|
const resourceName = 'jetpack-status';
|
|
|
|
|
|
|
|
if ( resourceNames.includes( resourceName ) ) {
|
|
|
|
const url = JETPACK_NAMESPACE + '/connection';
|
|
|
|
|
|
|
|
return [
|
|
|
|
fetch( {
|
|
|
|
path: url,
|
|
|
|
} )
|
2020-02-14 02:23:21 +00:00
|
|
|
.then( ( response ) => {
|
2019-08-29 16:41:04 +00:00
|
|
|
return { [ resourceName ]: { data: response } };
|
|
|
|
} )
|
2020-02-14 02:23:21 +00:00
|
|
|
.catch( ( error ) => {
|
|
|
|
return {
|
|
|
|
[ resourceName ]: { error: String( error.message ) },
|
|
|
|
};
|
2019-08-29 16:41:04 +00:00
|
|
|
} ),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2019-08-21 06:34:21 +00:00
|
|
|
function readJetpackConnectUrl( resourceNames, fetch ) {
|
2020-02-14 02:23:21 +00:00
|
|
|
const filteredNames = resourceNames.filter( ( name ) => {
|
2019-10-16 22:00:47 +00:00
|
|
|
return name.startsWith( 'jetpack-connect-url' );
|
|
|
|
} );
|
2019-08-21 06:34:21 +00:00
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
return filteredNames.map( async ( resourceName ) => {
|
2019-10-16 22:00:47 +00:00
|
|
|
const query = getResourceIdentifier( resourceName );
|
2020-02-14 02:23:21 +00:00
|
|
|
const url = addQueryArgs(
|
|
|
|
WC_ADMIN_NAMESPACE + '/onboarding/plugins/connect-jetpack',
|
|
|
|
query
|
|
|
|
);
|
2019-08-21 06:34:21 +00:00
|
|
|
|
2019-10-16 22:00:47 +00:00
|
|
|
return fetch( {
|
|
|
|
path: url,
|
|
|
|
} )
|
2020-02-14 02:23:21 +00:00
|
|
|
.then( ( response ) => {
|
2019-10-16 22:00:47 +00:00
|
|
|
return { [ resourceName ]: { data: response.connectAction } };
|
2019-08-21 06:34:21 +00:00
|
|
|
} )
|
2020-02-14 02:23:21 +00:00
|
|
|
.catch( ( error ) => {
|
2019-10-16 22:00:47 +00:00
|
|
|
error.message = getPluginErrorMessage( 'connect', 'jetpack' );
|
|
|
|
return { [ resourceName ]: { error } };
|
|
|
|
} );
|
|
|
|
} );
|
2019-08-21 06:34:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getPluginErrorMessage( action, plugin ) {
|
|
|
|
const pluginName = pluginNames[ plugin ] || plugin;
|
2019-08-23 12:56:57 +00:00
|
|
|
switch ( action ) {
|
|
|
|
case 'install':
|
|
|
|
return sprintf(
|
2020-02-14 02:23:21 +00:00
|
|
|
__(
|
|
|
|
'There was an error installing %s. Please try again.',
|
|
|
|
'woocommerce-admin'
|
|
|
|
),
|
2019-08-21 06:34:21 +00:00
|
|
|
pluginName
|
2019-08-23 12:56:57 +00:00
|
|
|
);
|
|
|
|
case 'connect':
|
|
|
|
return sprintf(
|
2020-02-14 02:23:21 +00:00
|
|
|
__(
|
|
|
|
'There was an error connecting to %s. Please try again.',
|
|
|
|
'woocommerce-admin'
|
|
|
|
),
|
2019-08-23 12:56:57 +00:00
|
|
|
pluginName
|
|
|
|
);
|
|
|
|
case 'activate':
|
|
|
|
default:
|
|
|
|
return sprintf(
|
2020-02-14 02:23:21 +00:00
|
|
|
__(
|
|
|
|
'There was an error activating %s. Please try again.',
|
|
|
|
'woocommerce-admin'
|
|
|
|
),
|
2019-08-21 06:34:21 +00:00
|
|
|
pluginName
|
|
|
|
);
|
2019-08-23 12:56:57 +00:00
|
|
|
}
|
2019-08-21 06:34:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function installPlugins( resourceNames, data, fetch ) {
|
|
|
|
const resourceName = 'plugin-install';
|
|
|
|
if ( resourceNames.includes( resourceName ) ) {
|
|
|
|
const plugins = data[ resourceName ];
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
return plugins.map( async ( plugin ) => {
|
2019-08-23 12:56:57 +00:00
|
|
|
return fetch( {
|
2019-09-02 03:45:56 +00:00
|
|
|
path: `${ WC_ADMIN_NAMESPACE }/onboarding/plugins/install`,
|
2019-08-23 12:56:57 +00:00
|
|
|
method: 'POST',
|
|
|
|
data: {
|
|
|
|
plugin,
|
|
|
|
},
|
|
|
|
} )
|
2020-02-14 02:23:21 +00:00
|
|
|
.then( ( response ) => {
|
|
|
|
const { installedPlugins = [] } = getSetting(
|
|
|
|
'onboarding',
|
|
|
|
{}
|
|
|
|
);
|
2020-01-17 01:46:11 +00:00
|
|
|
setSetting( 'onboarding', {
|
|
|
|
...getSetting( 'onboarding', {} ),
|
2020-02-14 02:23:21 +00:00
|
|
|
installedPlugins: uniq( [
|
|
|
|
...installedPlugins,
|
|
|
|
...plugins,
|
|
|
|
] ),
|
2020-01-17 01:46:11 +00:00
|
|
|
} );
|
|
|
|
|
2019-08-23 12:56:57 +00:00
|
|
|
return {
|
|
|
|
[ resourceName ]: { data: plugins },
|
2020-02-14 02:23:21 +00:00
|
|
|
[ getResourceName( resourceName, plugin ) ]: {
|
|
|
|
data: response,
|
|
|
|
},
|
2019-08-23 12:56:57 +00:00
|
|
|
};
|
|
|
|
} )
|
2020-02-14 02:23:21 +00:00
|
|
|
.catch( ( error ) => {
|
|
|
|
error.message = getPluginErrorMessage(
|
|
|
|
'install',
|
|
|
|
pluginNames[ plugin ] || plugin
|
|
|
|
);
|
2019-08-23 12:56:57 +00:00
|
|
|
return {
|
|
|
|
[ resourceName ]: { data: plugins },
|
|
|
|
[ getResourceName( resourceName, plugin ) ]: { error },
|
|
|
|
};
|
|
|
|
} );
|
|
|
|
} );
|
2019-08-21 06:34:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2019-05-28 14:45:52 +00:00
|
|
|
export default {
|
|
|
|
read,
|
|
|
|
update,
|
|
|
|
};
|