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-05-28 14:45:52 +00:00
|
|
|
import apiFetch from '@wordpress/api-fetch';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { getResourceName } from '../utils';
|
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 };
|
|
|
|
} );
|
|
|
|
|
|
|
|
return resources;
|
|
|
|
}
|
|
|
|
|
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';
|
|
|
|
|
|
|
|
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 ) {
|
|
|
|
const resourceName = 'jetpack-connect-url';
|
|
|
|
|
|
|
|
if ( resourceNames.includes( resourceName ) ) {
|
2019-09-02 03:45:56 +00:00
|
|
|
const url = WC_ADMIN_NAMESPACE + '/onboarding/plugins/connect-jetpack';
|
2019-08-21 06:34:21 +00:00
|
|
|
|
|
|
|
return [
|
|
|
|
fetch( {
|
|
|
|
path: url,
|
|
|
|
} )
|
|
|
|
.then( response => {
|
|
|
|
return { [ resourceName ]: { data: response.connectAction } };
|
|
|
|
} )
|
|
|
|
.catch( error => {
|
2019-08-23 12:56:57 +00:00
|
|
|
error.message = getPluginErrorMessage( 'connect', 'jetpack' );
|
2019-08-21 06:34:21 +00:00
|
|
|
return { [ resourceName ]: { error } };
|
|
|
|
} ),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
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 => {
|
|
|
|
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,
|
|
|
|
};
|