2019-05-28 14:45:52 +00:00
|
|
|
/** @format */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 )
|
|
|
|
.catch( error => {
|
|
|
|
return { [ resourceName ]: { error: String( error.message ) } };
|
|
|
|
} ),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
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 ],
|
|
|
|
} )
|
|
|
|
.then( profileItemToResource.bind( null, data[ resourceName ] ) )
|
|
|
|
.catch( error => {
|
|
|
|
return { [ resourceName ]: { error } };
|
|
|
|
} ),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
function profileItemsToResources( items ) {
|
|
|
|
const resourceName = 'onboarding-profile';
|
|
|
|
|
|
|
|
const itemKeys = Object.keys( items );
|
|
|
|
|
|
|
|
const resources = {};
|
|
|
|
itemKeys.forEach( key => {
|
|
|
|
const item = items[ key ];
|
|
|
|
resources[ getResourceName( resourceName, key ) ] = { data: item };
|
|
|
|
} );
|
|
|
|
|
|
|
|
return {
|
|
|
|
[ resourceName ]: {
|
|
|
|
data: itemKeys,
|
|
|
|
},
|
|
|
|
...resources,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function profileItemToResource( items ) {
|
|
|
|
const resourceName = 'onboarding-profile';
|
|
|
|
|
|
|
|
const resources = {};
|
|
|
|
Object.keys( items ).forEach( key => {
|
|
|
|
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 )
|
|
|
|
.catch( error => {
|
|
|
|
return { [ resourceName ]: { error: String( error.message ) } };
|
|
|
|
} ),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
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( ',' ),
|
|
|
|
},
|
|
|
|
} )
|
|
|
|
.then( response => activatePluginToResource( response, plugins ) )
|
|
|
|
.catch( error => {
|
|
|
|
const resources = { [ resourceName ]: { error } };
|
|
|
|
Object.keys( plugins ).forEach( key => {
|
|
|
|
const pluginError = { ...error };
|
|
|
|
const item = plugins[ key ];
|
|
|
|
pluginError.message = getPluginErrorMessage( 'activate', item );
|
|
|
|
resources[ getResourceName( resourceName, item ) ] = { error: pluginError };
|
|
|
|
} );
|
|
|
|
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 },
|
|
|
|
[ 'active-plugins' ]: { data: response.active },
|
|
|
|
};
|
|
|
|
Object.keys( items ).forEach( key => {
|
|
|
|
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,
|
|
|
|
} )
|
|
|
|
.then( response => {
|
|
|
|
return { [ resourceName ]: { data: response } };
|
|
|
|
} )
|
|
|
|
.catch( error => {
|
|
|
|
return { [ resourceName ]: { error: String( error.message ) } };
|
|
|
|
} ),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2019-08-21 06:34:21 +00:00
|
|
|
function readJetpackConnectUrl( resourceNames, fetch ) {
|
2019-10-16 22:00:47 +00:00
|
|
|
const filteredNames = resourceNames.filter( name => {
|
|
|
|
return name.startsWith( 'jetpack-connect-url' );
|
|
|
|
} );
|
2019-08-21 06:34:21 +00:00
|
|
|
|
2019-10-16 22:00:47 +00:00
|
|
|
return filteredNames.map( async resourceName => {
|
|
|
|
const query = getResourceIdentifier( resourceName );
|
|
|
|
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,
|
|
|
|
} )
|
|
|
|
.then( response => {
|
|
|
|
return { [ resourceName ]: { data: response.connectAction } };
|
2019-08-21 06:34:21 +00:00
|
|
|
} )
|
2019-10-16 22:00:47 +00:00
|
|
|
.catch( error => {
|
|
|
|
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(
|
2019-08-21 06:34:21 +00:00
|
|
|
__( 'There was an error installing %s. Please try again.', 'woocommerce-admin' ),
|
|
|
|
pluginName
|
2019-08-23 12:56:57 +00:00
|
|
|
);
|
|
|
|
case 'connect':
|
|
|
|
return sprintf(
|
|
|
|
__( 'There was an error connecting to %s. Please try again.', 'woocommerce-admin' ),
|
|
|
|
pluginName
|
|
|
|
);
|
|
|
|
case 'activate':
|
|
|
|
default:
|
|
|
|
return sprintf(
|
2019-08-21 06:34:21 +00:00
|
|
|
__( 'There was an error activating %s. Please try again.', 'woocommerce-admin' ),
|
|
|
|
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 ];
|
|
|
|
|
2019-08-23 12:56:57 +00:00
|
|
|
return plugins.map( async plugin => {
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
} )
|
|
|
|
.then( response => {
|
2020-01-17 01:46:11 +00:00
|
|
|
const { installedPlugins = [] } = getSetting( 'onboarding', {} );
|
|
|
|
setSetting( 'onboarding', {
|
|
|
|
...getSetting( 'onboarding', {} ),
|
|
|
|
installedPlugins: uniq( [ ...installedPlugins, ...plugins ] ),
|
|
|
|
} );
|
|
|
|
|
2019-08-23 12:56:57 +00:00
|
|
|
return {
|
|
|
|
[ resourceName ]: { data: plugins },
|
|
|
|
[ getResourceName( resourceName, plugin ) ]: { data: response },
|
|
|
|
};
|
|
|
|
} )
|
|
|
|
.catch( error => {
|
|
|
|
error.message = getPluginErrorMessage( 'install', pluginNames[ plugin ] || plugin );
|
|
|
|
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,
|
|
|
|
};
|