khaihihi
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { getSetting } from '@woocommerce/settings';
|
||||
|
||||
export const ENABLE_REVIEW_RATING = getSetting( 'enableReviewRating', true );
|
||||
export const SHOW_AVATARS = getSetting( 'showAvatars', true );
|
||||
export const MAX_COLUMNS = getSetting( 'max_columns', 6 );
|
||||
export const MIN_COLUMNS = getSetting( 'min_columns', 1 );
|
||||
export const DEFAULT_COLUMNS = getSetting( 'default_columns', 3 );
|
||||
export const MAX_ROWS = getSetting( 'max_rows', 6 );
|
||||
export const MIN_ROWS = getSetting( 'min_rows', 1 );
|
||||
export const DEFAULT_ROWS = getSetting( 'default_rows', 2 );
|
||||
export const MIN_HEIGHT = getSetting( 'min_height', 500 );
|
||||
export const DEFAULT_HEIGHT = getSetting( 'default_height', 500 );
|
||||
export const PLACEHOLDER_IMG_SRC = getSetting( 'placeholderImgSrc', '' );
|
||||
export const THUMBNAIL_SIZE = getSetting( 'thumbnail_size', 300 );
|
||||
export const IS_LARGE_CATALOG = getSetting( 'isLargeCatalog' );
|
||||
export const LIMIT_TAGS = getSetting( 'limitTags' );
|
||||
export const HAS_PRODUCTS = getSetting( 'hasProducts', true );
|
||||
export const HAS_TAGS = getSetting( 'hasTags', true );
|
||||
export const HOME_URL = getSetting( 'homeUrl', '' );
|
||||
export const PRODUCT_COUNT = getSetting( 'productCount', 0 );
|
||||
export const ATTRIBUTES = getSetting( 'attributes', [] );
|
||||
export const WC_BLOCKS_ASSET_URL = getSetting( 'wcBlocksAssetUrl', '' );
|
||||
@@ -0,0 +1,6 @@
|
||||
const NAMESPACE = '/wc/blocks';
|
||||
export const ENDPOINTS = {
|
||||
root: NAMESPACE,
|
||||
products: `${ NAMESPACE }/products`,
|
||||
categories: `${ NAMESPACE }/products/categories`,
|
||||
};
|
||||
@@ -0,0 +1,2 @@
|
||||
export * from './constants';
|
||||
export { ENDPOINTS } from './endpoints';
|
||||
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import { allSettings } from './settings-init';
|
||||
|
||||
export const ADMIN_URL = allSettings.adminUrl;
|
||||
export const COUNTRIES = allSettings.countries;
|
||||
export const CURRENCY = allSettings.currency;
|
||||
export const LOCALE = allSettings.locale;
|
||||
export const ORDER_STATUSES = allSettings.orderStatuses;
|
||||
export const SITE_TITLE = allSettings.siteTitle;
|
||||
export const WC_ASSET_URL = allSettings.wcAssetUrl;
|
||||
export const DEFAULT_DATE_RANGE = allSettings.defaultDateRange;
|
||||
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import { allSettings } from './settings-init';
|
||||
|
||||
/**
|
||||
* Retrieves a setting value from the setting state.
|
||||
*
|
||||
* @export
|
||||
* @param {string} name The identifier for the setting.
|
||||
* @param {mixed} [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.
|
||||
* @returns {mixed}
|
||||
*/
|
||||
export function getSetting( name, fallback = false, filter = ( val ) => val ) {
|
||||
const value = allSettings.hasOwnProperty( name )
|
||||
? allSettings[ name ]
|
||||
: fallback;
|
||||
return filter( value, fallback );
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import { getSetting } from './get-setting';
|
||||
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import compareVersions from 'compare-versions';
|
||||
|
||||
export * from './default-constants';
|
||||
export { setSetting } from './set-setting';
|
||||
|
||||
/**
|
||||
* Note: this attempts to coerce the wpVersion to a semver for comparison
|
||||
* This will result in dropping any beta/rc values.
|
||||
*
|
||||
* `5.3-beta1-4252` would get converted to `5.3.0-rc.4252`
|
||||
* `5.3-beta1` would get converted to `5.3.0-rc`.
|
||||
* `5.3` would not be touched.
|
||||
*
|
||||
* For the purpose of these comparisons all pre-release versions are normalized
|
||||
* to `rc`.
|
||||
*/
|
||||
export const compareWithWpVersion = ( version, operator ) => {
|
||||
let replacement = getSetting( 'wpVersion', '' ).replace(
|
||||
/-[a-zA-Z0-9]*[\-]*/,
|
||||
'.0-rc.'
|
||||
);
|
||||
replacement = replacement.endsWith( '.' )
|
||||
? replacement.substring( 0, replacement.length - 1 )
|
||||
: replacement;
|
||||
return compareVersions.compare( version, replacement, operator );
|
||||
};
|
||||
|
||||
export { compareVersions, getSetting };
|
||||
|
||||
/**
|
||||
* 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 const getAdminLink = ( path ) => getSetting( 'adminUrl' ) + path;
|
||||
@@ -0,0 +1,19 @@
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import { allSettings } from './settings-init';
|
||||
|
||||
/**
|
||||
* Sets a value to a property on the settings state.
|
||||
*
|
||||
* @export
|
||||
* @param {string} name The setting property key for the
|
||||
* setting being mutated.
|
||||
* @param {mixed} 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)
|
||||
*/
|
||||
export function setSetting( name, value, filter = ( val ) => val ) {
|
||||
allSettings[ name ] = filter( value );
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
const defaults = {
|
||||
adminUrl: '',
|
||||
countries: [],
|
||||
currency: {
|
||||
code: 'USD',
|
||||
precision: 2,
|
||||
symbol: '$',
|
||||
symbolPosition: 'left',
|
||||
decimalSeparator: '.',
|
||||
priceFormat: '%1$s%2$s',
|
||||
thousandSeparator: ',',
|
||||
},
|
||||
defaultDateRange: 'period=month&compare=previous_year',
|
||||
locale: {
|
||||
siteLocale: 'en_US',
|
||||
userLocale: 'en_US',
|
||||
weekdaysShort: [ 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' ],
|
||||
},
|
||||
orderStatuses: [],
|
||||
siteTitle: '',
|
||||
wcAssetUrl: '',
|
||||
};
|
||||
|
||||
const globalSharedSettings = typeof wcSettings === 'object' ? wcSettings : {};
|
||||
|
||||
// Use defaults or global settings, depending on what is set.
|
||||
const allSettings = {
|
||||
...defaults,
|
||||
...globalSharedSettings,
|
||||
};
|
||||
|
||||
allSettings.currency = {
|
||||
...defaults.currency,
|
||||
...allSettings.currency,
|
||||
};
|
||||
|
||||
allSettings.locale = {
|
||||
...defaults.locale,
|
||||
...allSettings.locale,
|
||||
};
|
||||
|
||||
export { allSettings };
|
||||
@@ -0,0 +1,24 @@
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import { compareWithWpVersion, setSetting } from '..';
|
||||
|
||||
describe( 'compareWithWpVersion', () => {
|
||||
it.each`
|
||||
version | operator | result
|
||||
${'5.3-beta1'} | ${'>'} | ${true}
|
||||
${'5.3'} | ${'='} | ${true}
|
||||
${'5.3-beta12-235'} | ${'>'} | ${true}
|
||||
${'5.3-rc1'} | ${'<'} | ${false}
|
||||
${'5.3-rc12-235'} | ${'>'} | ${true}
|
||||
${'5.3.1'} | ${'<'} | ${true}
|
||||
${'5.4-beta1'} | ${'<'} | ${true}
|
||||
`(
|
||||
'should return $result when $version is the current wpVersion ' +
|
||||
'and `5.3` is the version compared using `$operator`',
|
||||
( { version, operator, result } ) => {
|
||||
setSetting( 'wpVersion', version );
|
||||
expect( compareWithWpVersion( '5.3', operator ) ).toBe( result );
|
||||
}
|
||||
);
|
||||
} );
|
||||
@@ -0,0 +1,17 @@
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import { getSetting } from '../get-setting';
|
||||
import { ADMIN_URL } from '../default-constants';
|
||||
|
||||
describe( 'getSetting', () => {
|
||||
it( 'returns provided default for non available setting', () => {
|
||||
expect( getSetting( 'nada', 'really nada' ) ).toBe( 'really nada' );
|
||||
} );
|
||||
it( 'returns expected value for existing setting', () => {
|
||||
expect( getSetting( 'adminUrl', 'not this' ) ).toEqual( ADMIN_URL );
|
||||
} );
|
||||
it( 'filters value via provided filter callback', () => {
|
||||
expect( getSetting( 'some value', 'default', () => 42 ) ).toBe( 42 );
|
||||
} );
|
||||
} );
|
||||
@@ -0,0 +1,20 @@
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import { setSetting } from '../set-setting';
|
||||
import { getSetting } from '../get-setting';
|
||||
|
||||
describe( 'setSetting', () => {
|
||||
it( 'should add a new value to the settings state for value not present', () => {
|
||||
setSetting( 'aSetting', 42 );
|
||||
expect( getSetting( 'aSetting' ) ).toBe( 42 );
|
||||
} );
|
||||
it( 'should replace existing value', () => {
|
||||
setSetting( 'adminUrl', 'not original' );
|
||||
expect( getSetting( 'adminUrl' ) ).toBe( 'not original' );
|
||||
} );
|
||||
it( 'should save the value run through the provided filter', () => {
|
||||
setSetting( 'aSetting', 'who', () => 42 );
|
||||
expect( getSetting( 'aSetting' ) ).toBe( 42 );
|
||||
} );
|
||||
} );
|
||||
Reference in New Issue
Block a user