2019-09-23 21:47:08 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2020-03-25 03:20:17 +00:00
|
|
|
import { __ } from '@wordpress/i18n';
|
2019-09-23 21:47:08 +00:00
|
|
|
|
2020-03-25 03:20:17 +00:00
|
|
|
// Remove mutable data from settings object to prevent access. Data stores should be used instead.
|
|
|
|
const mutableSources = [ 'wcAdminSettings', 'preloadSettings' ];
|
|
|
|
const settings = typeof wcSettings === 'object' ? wcSettings : {};
|
|
|
|
const SOURCE = Object.keys( settings ).reduce( ( source, key ) => {
|
|
|
|
if ( ! mutableSources.includes( key ) ) {
|
|
|
|
source[ key ] = settings[ key ];
|
|
|
|
}
|
|
|
|
return source;
|
|
|
|
}, {} );
|
|
|
|
|
|
|
|
export const ADMIN_URL = SOURCE.adminUrl;
|
|
|
|
export const COUNTRIES = SOURCE.countries;
|
|
|
|
export const CURRENCY = SOURCE.currency;
|
|
|
|
export const LOCALE = SOURCE.locale;
|
|
|
|
export const ORDER_STATUSES = SOURCE.orderStatuses;
|
|
|
|
export const SITE_TITLE = SOURCE.siteTitle;
|
|
|
|
export const WC_ASSET_URL = SOURCE.wcAssetUrl;
|
2019-09-23 21:47:08 +00:00
|
|
|
|
|
|
|
/**
|
2020-03-25 03:20:17 +00:00
|
|
|
* Retrieves a setting value from the setting state.
|
|
|
|
*
|
|
|
|
* @export
|
|
|
|
* @param {string} name The identifier for the setting.
|
|
|
|
* @param {*} [fallback=false] The value to use as a fallback
|
|
|
|
* if the setting is not in the
|
|
|
|
* state.
|
|
|
|
* @param {Function} [filter=( val ) => val] A callback for filtering the
|
|
|
|
* value before it's returned.
|
|
|
|
* Receives both the found value
|
|
|
|
* (if it exists for the key) and
|
|
|
|
* the provided fallback arg.
|
|
|
|
*
|
|
|
|
* @return {*} The value present in the settings state for the given
|
|
|
|
* name.
|
2019-09-23 21:47:08 +00:00
|
|
|
*/
|
2020-07-28 02:32:58 +00:00
|
|
|
export function getSetting( name, fallback = false, filter = ( val ) => val ) {
|
2020-03-25 03:20:17 +00:00
|
|
|
if ( mutableSources.includes( name ) ) {
|
2020-07-28 02:32:58 +00:00
|
|
|
throw new Error(
|
|
|
|
__( 'Mutable settings should be accessed via data store.' )
|
|
|
|
);
|
2020-03-25 03:20:17 +00:00
|
|
|
}
|
|
|
|
const value = SOURCE.hasOwnProperty( name ) ? SOURCE[ name ] : fallback;
|
|
|
|
return filter( value, fallback );
|
|
|
|
}
|
2019-09-23 21:47:08 +00:00
|
|
|
|
2020-03-25 03:20:17 +00:00
|
|
|
/**
|
|
|
|
* Sets a value to a property on the settings state.
|
|
|
|
*
|
|
|
|
* NOTE: This feature is to be removed in favour of data stores when a full migration
|
|
|
|
* is complete.
|
|
|
|
*
|
|
|
|
* @deprecated
|
|
|
|
*
|
|
|
|
* @export
|
|
|
|
* @param {string} name The setting property key for the
|
|
|
|
* setting being mutated.
|
|
|
|
* @param {*} value The value to set.
|
|
|
|
* @param {Function} [filter=( val ) => val] Allows for providing a callback
|
|
|
|
* to sanitize the setting (eg.
|
|
|
|
* ensure it's a number)
|
|
|
|
*/
|
2020-07-28 02:32:58 +00:00
|
|
|
export function setSetting( name, value, filter = ( val ) => val ) {
|
2020-03-25 03:20:17 +00:00
|
|
|
if ( mutableSources.includes( name ) ) {
|
2020-07-28 02:32:58 +00:00
|
|
|
throw new Error(
|
|
|
|
__( 'Mutable settings should be mutated via data store.' )
|
|
|
|
);
|
2020-03-25 03:20:17 +00:00
|
|
|
}
|
|
|
|
SOURCE[ name ] = filter( value );
|
|
|
|
}
|
2019-11-23 00:30:46 +00:00
|
|
|
|
2020-03-25 03:20:17 +00:00
|
|
|
/**
|
|
|
|
* Returns a string with the site's wp-admin URL appended. JS version of `admin_url`.
|
|
|
|
*
|
|
|
|
* @param {string} path Relative path.
|
|
|
|
* @return {string} Full admin URL.
|
|
|
|
*/
|
|
|
|
export function getAdminLink( path ) {
|
|
|
|
return ( ADMIN_URL || '' ) + path;
|
|
|
|
}
|