2021-02-12 19:32:42 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
2022-01-06 12:53:30 +00:00
|
|
|
import { getSetting } from '@woocommerce/settings';
|
2021-02-12 19:32:42 +00:00
|
|
|
|
|
|
|
// Remove mutable data from settings object to prevent access. Data stores should be used instead.
|
|
|
|
const mutableSources = [ 'wcAdminSettings', 'preloadSettings' ];
|
2022-01-06 12:53:30 +00:00
|
|
|
const adminSettings = getSetting( 'admin', {} );
|
|
|
|
const ADMIN_SETTINGS_SOURCE = Object.keys( adminSettings ).reduce(
|
|
|
|
( source, key ) => {
|
|
|
|
if ( ! mutableSources.includes( key ) ) {
|
|
|
|
source[ key ] = adminSettings[ key ];
|
|
|
|
}
|
|
|
|
return source;
|
|
|
|
},
|
|
|
|
{}
|
|
|
|
);
|
2021-02-12 19:32:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves a setting value from the setting state.
|
|
|
|
*
|
2022-03-18 11:45:14 +00:00
|
|
|
* @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.
|
2021-02-12 19:32:42 +00:00
|
|
|
*
|
|
|
|
* @return {*} The value present in the settings state for the given
|
|
|
|
* name.
|
|
|
|
*/
|
2022-01-06 12:53:30 +00:00
|
|
|
export function getAdminSetting(
|
|
|
|
name,
|
|
|
|
fallback = false,
|
|
|
|
filter = ( val ) => val
|
|
|
|
) {
|
2021-02-12 19:32:42 +00:00
|
|
|
if ( mutableSources.includes( name ) ) {
|
|
|
|
throw new Error(
|
|
|
|
__( 'Mutable settings should be accessed via data store.' )
|
|
|
|
);
|
|
|
|
}
|
2022-01-06 12:53:30 +00:00
|
|
|
const value = ADMIN_SETTINGS_SOURCE.hasOwnProperty( name )
|
|
|
|
? ADMIN_SETTINGS_SOURCE[ name ]
|
|
|
|
: fallback;
|
2021-02-12 19:32:42 +00:00
|
|
|
return filter( value, fallback );
|
|
|
|
}
|
|
|
|
|
2022-01-06 12:53:30 +00:00
|
|
|
export const ADMIN_URL = getSetting( 'adminUrl' );
|
|
|
|
export const COUNTRIES = getSetting( 'countries' );
|
|
|
|
export const CURRENCY = getSetting( 'currency' );
|
|
|
|
export const LOCALE = getSetting( 'locale' );
|
|
|
|
export const SITE_TITLE = getSetting( 'siteTitle' );
|
|
|
|
export const WC_ASSET_URL = getSetting( 'wcAssetUrl' );
|
|
|
|
export const ORDER_STATUSES = getAdminSetting( 'orderStatuses' );
|
|
|
|
|
2021-02-12 19:32:42 +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
|
|
|
|
*
|
2022-03-18 11:45:14 +00:00
|
|
|
* @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)
|
2021-02-12 19:32:42 +00:00
|
|
|
*/
|
2022-01-06 12:53:30 +00:00
|
|
|
export function setAdminSetting( name, value, filter = ( val ) => val ) {
|
2021-02-12 19:32:42 +00:00
|
|
|
if ( mutableSources.includes( name ) ) {
|
|
|
|
throw new Error(
|
|
|
|
__( 'Mutable settings should be mutated via data store.' )
|
|
|
|
);
|
|
|
|
}
|
2022-01-06 12:53:30 +00:00
|
|
|
ADMIN_SETTINGS_SOURCE[ name ] = filter( value );
|
2021-05-17 14:51:02 +00:00
|
|
|
}
|