2019-11-07 18:31:02 +00:00
|
|
|
/**
|
2019-12-10 19:01:21 +00:00
|
|
|
* External dependencies
|
2019-11-07 18:31:02 +00:00
|
|
|
*/
|
2019-12-30 10:11:50 +00:00
|
|
|
import { decodeEntities } from '@wordpress/html-entities';
|
2019-12-10 19:01:21 +00:00
|
|
|
import { without } from 'lodash';
|
2019-11-07 18:31:02 +00:00
|
|
|
|
2019-12-10 19:01:21 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2019-11-07 18:31:02 +00:00
|
|
|
import { getSetting } from '@woocommerce/wc-admin-settings';
|
|
|
|
|
2019-08-26 05:49:04 +00:00
|
|
|
/**
|
|
|
|
* Gets the country code from a country:state value string.
|
|
|
|
*
|
|
|
|
* @param {string} countryState Country state string, e.g. US:GA.
|
|
|
|
* @return {string} Country string.
|
|
|
|
*/
|
|
|
|
|
|
|
|
export function getCountryCode( countryState ) {
|
|
|
|
if ( ! countryState ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return countryState.split( ':' )[ 0 ];
|
|
|
|
}
|
2019-11-07 18:31:02 +00:00
|
|
|
|
2019-12-10 19:01:21 +00:00
|
|
|
export function getCurrencyRegion( countryState ) {
|
|
|
|
let region = getCountryCode( countryState );
|
2020-02-14 02:23:21 +00:00
|
|
|
const euCountries = without(
|
|
|
|
getSetting( 'onboarding', { euCountries: [] } ).euCountries,
|
|
|
|
'GB'
|
|
|
|
);
|
2019-12-10 19:01:21 +00:00
|
|
|
if ( euCountries.includes( region ) ) {
|
|
|
|
region = 'EU';
|
|
|
|
}
|
|
|
|
|
|
|
|
return region;
|
|
|
|
}
|
|
|
|
|
2019-12-20 12:58:38 +00:00
|
|
|
/**
|
|
|
|
* Gets the product IDs for items based on the product types and theme selected in the onboarding profiler.
|
|
|
|
*
|
2020-02-14 02:23:21 +00:00
|
|
|
* @param {Object} profileItems Onboarding profile.
|
|
|
|
* @param {boolean} includeInstalledItems Include installed items in returned product IDs.
|
2020-05-25 00:26:08 +00:00
|
|
|
* @param {Array} installedPlugins Installed plugins.
|
2020-02-14 02:23:21 +00:00
|
|
|
* @return {Array} Product Ids.
|
2019-12-20 12:58:38 +00:00
|
|
|
*/
|
2020-02-14 02:23:21 +00:00
|
|
|
export function getProductIdsForCart(
|
|
|
|
profileItems,
|
2020-05-25 00:26:08 +00:00
|
|
|
includeInstalledItems = false,
|
|
|
|
installedPlugins
|
2020-08-03 19:24:57 +00:00
|
|
|
) {
|
|
|
|
const productList = getProductList(
|
|
|
|
profileItems,
|
|
|
|
includeInstalledItems,
|
|
|
|
installedPlugins
|
|
|
|
);
|
|
|
|
const productIds = productList.map(
|
|
|
|
( product ) => product.id || product.product
|
|
|
|
);
|
|
|
|
return productIds;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the labeled/categorized product names and types for items based on the product types and theme selected in the onboarding profiler.
|
|
|
|
*
|
|
|
|
* @param {Object} profileItems Onboarding profile.
|
|
|
|
* @param {Array} installedPlugins Installed plugins.
|
|
|
|
* @return {Array} Objects with labeled/categorized product names and types.
|
|
|
|
*/
|
|
|
|
export function getCategorizedOnboardingProducts(
|
|
|
|
profileItems,
|
|
|
|
installedPlugins
|
|
|
|
) {
|
|
|
|
const productList = {};
|
|
|
|
productList.products = getProductList(
|
|
|
|
profileItems,
|
|
|
|
true,
|
|
|
|
installedPlugins
|
|
|
|
);
|
|
|
|
productList.remainingProducts = getProductList(
|
|
|
|
profileItems,
|
|
|
|
false,
|
|
|
|
installedPlugins
|
|
|
|
);
|
|
|
|
|
|
|
|
const uniqueItemsList = [
|
|
|
|
...new Set( [
|
|
|
|
...productList.products,
|
|
|
|
...productList.remainingProducts,
|
|
|
|
] ),
|
|
|
|
];
|
|
|
|
|
|
|
|
productList.uniqueItemsList = uniqueItemsList.map( ( product ) => {
|
|
|
|
let cleanedProduct;
|
|
|
|
if ( product.label ) {
|
|
|
|
cleanedProduct = { type: 'extension', name: product.label };
|
|
|
|
} else {
|
|
|
|
cleanedProduct = { type: 'theme', name: product.title };
|
|
|
|
}
|
|
|
|
return cleanedProduct;
|
|
|
|
} );
|
|
|
|
|
|
|
|
return productList;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a product list for items based on the product types and theme selected in the onboarding profiler.
|
|
|
|
*
|
|
|
|
* @param {Object} profileItems Onboarding profile.
|
|
|
|
* @param {boolean} includeInstalledItems Include installed items in returned product list.
|
|
|
|
* @param {Array} installedPlugins Installed plugins.
|
|
|
|
* @return {Array} Products.
|
|
|
|
*/
|
|
|
|
export function getProductList(
|
|
|
|
profileItems,
|
|
|
|
includeInstalledItems = false,
|
|
|
|
installedPlugins
|
2020-02-14 02:23:21 +00:00
|
|
|
) {
|
2019-12-20 12:58:38 +00:00
|
|
|
const onboarding = getSetting( 'onboarding', {} );
|
2020-08-03 19:24:57 +00:00
|
|
|
const productList = [];
|
2019-12-20 12:58:38 +00:00
|
|
|
|
2020-01-06 23:10:37 +00:00
|
|
|
// The population of onboarding.productTypes only happens if the task list should be shown
|
|
|
|
// so bail early if it isn't present.
|
|
|
|
if ( ! onboarding.productTypes ) {
|
2020-08-03 19:24:57 +00:00
|
|
|
return productList;
|
2020-01-06 23:10:37 +00:00
|
|
|
}
|
|
|
|
|
2020-05-13 00:29:41 +00:00
|
|
|
const productTypes = profileItems.product_types || [];
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
productTypes.forEach( ( productType ) => {
|
2019-12-20 12:58:38 +00:00
|
|
|
if (
|
|
|
|
onboarding.productTypes[ productType ] &&
|
2019-12-31 08:50:45 +00:00
|
|
|
onboarding.productTypes[ productType ].product &&
|
|
|
|
( includeInstalledItems ||
|
2020-05-25 00:26:08 +00:00
|
|
|
! installedPlugins.includes(
|
2020-02-14 02:23:21 +00:00
|
|
|
onboarding.productTypes[ productType ].slug
|
|
|
|
) )
|
2019-12-20 12:58:38 +00:00
|
|
|
) {
|
2020-08-03 19:24:57 +00:00
|
|
|
productList.push( onboarding.productTypes[ productType ] );
|
2019-12-20 12:58:38 +00:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
const theme = onboarding.themes.find(
|
|
|
|
( themeData ) => themeData.slug === profileItems.theme
|
|
|
|
);
|
2019-12-20 12:58:38 +00:00
|
|
|
|
2019-12-31 08:50:45 +00:00
|
|
|
if (
|
|
|
|
theme &&
|
|
|
|
theme.id &&
|
|
|
|
getPriceValue( theme.price ) > 0 &&
|
|
|
|
( includeInstalledItems || ! theme.is_installed )
|
|
|
|
) {
|
2020-08-03 19:24:57 +00:00
|
|
|
productList.push( theme );
|
2019-12-20 12:58:38 +00:00
|
|
|
}
|
|
|
|
|
2020-08-03 19:24:57 +00:00
|
|
|
return productList;
|
2019-12-20 12:58:38 +00:00
|
|
|
}
|
|
|
|
|
2019-12-30 10:11:50 +00:00
|
|
|
/**
|
|
|
|
* Get the value of a price from a string, removing any non-numeric characters.
|
|
|
|
*
|
|
|
|
* @param {string} string Price string.
|
|
|
|
* @return {number} Number value.
|
|
|
|
*/
|
|
|
|
export function getPriceValue( string ) {
|
|
|
|
return Number( decodeEntities( string ).replace( /[^0-9.-]+/g, '' ) );
|
|
|
|
}
|
|
|
|
|
2019-11-07 18:31:02 +00:00
|
|
|
/**
|
|
|
|
* Returns if the onboarding feature of WooCommerce Admin should be enabled.
|
|
|
|
*
|
|
|
|
* While we preform an a/b test of onboarding, the feature will be enabled within the plugin build,
|
|
|
|
* but only if the user recieved the test/opted in.
|
|
|
|
*
|
2020-02-14 02:23:21 +00:00
|
|
|
* @return {boolean} True if the onboarding is enabled.
|
2019-11-07 18:31:02 +00:00
|
|
|
*/
|
|
|
|
export function isOnboardingEnabled() {
|
|
|
|
if ( ! window.wcAdminFeatures.onboarding ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return getSetting( 'onboardingEnabled', false );
|
|
|
|
}
|
2020-08-05 22:02:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if a URL is a WC admin url.
|
|
|
|
*
|
|
|
|
* @param {*} url - the url to test
|
|
|
|
* @return {boolean} true if the url is a wc-admin URL
|
|
|
|
*/
|
|
|
|
export function isWCAdmin( url ) {
|
|
|
|
return /admin.php\?page=wc-admin/.test( url );
|
|
|
|
}
|