2019-10-11 15:29:27 +00:00
|
|
|
/**
|
2020-06-26 12:01:35 +00:00
|
|
|
* External dependencies
|
2019-10-11 15:29:27 +00:00
|
|
|
*/
|
2020-06-26 12:01:35 +00:00
|
|
|
import compareVersions from 'compare-versions';
|
2019-10-11 15:29:27 +00:00
|
|
|
|
|
|
|
/**
|
2020-06-26 12:01:35 +00:00
|
|
|
* Internal dependencies
|
2019-10-11 15:29:27 +00:00
|
|
|
*/
|
2021-04-22 11:37:27 +00:00
|
|
|
import { allSettings } from './settings-init';
|
2019-10-11 15:29:27 +00:00
|
|
|
|
2021-04-22 11:37:27 +00:00
|
|
|
/**
|
|
|
|
* Retrieves a setting value from the setting state.
|
|
|
|
*
|
2021-05-16 18:00:06 +00:00
|
|
|
* If a setting with key `name` does not exist or is undefined,
|
|
|
|
* the `fallback` will be returned instead. An optional `filter`
|
|
|
|
* callback can be passed to format the returned value.
|
2021-04-22 11:37:27 +00:00
|
|
|
*/
|
2021-06-17 07:50:52 +00:00
|
|
|
export const getSetting = < T >(
|
2021-04-22 11:37:27 +00:00
|
|
|
name: string,
|
|
|
|
fallback: unknown = false,
|
2021-05-16 18:00:06 +00:00
|
|
|
filter = ( val: unknown, fb: unknown ) =>
|
|
|
|
typeof val !== 'undefined' ? val : fb
|
2021-06-17 07:50:52 +00:00
|
|
|
): T => {
|
2021-04-22 11:37:27 +00:00
|
|
|
const value = name in allSettings ? allSettings[ name ] : fallback;
|
2021-06-17 07:50:52 +00:00
|
|
|
return filter( value, fallback ) as T;
|
2021-04-22 11:37:27 +00:00
|
|
|
};
|
2019-10-11 15:29:27 +00:00
|
|
|
|
2021-12-03 09:45:06 +00:00
|
|
|
export const getSettingWithCoercion = < T >(
|
|
|
|
name: string,
|
|
|
|
fallback: T,
|
|
|
|
typeguard: ( val: unknown, fb: unknown ) => val is T
|
|
|
|
): T => {
|
|
|
|
const value = name in allSettings ? allSettings[ name ] : fallback;
|
|
|
|
return typeguard( value, fallback ) ? value : fallback;
|
|
|
|
};
|
|
|
|
|
2019-10-11 15:29:27 +00:00
|
|
|
/**
|
|
|
|
* 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`.
|
2019-12-10 17:17:46 +00:00
|
|
|
*
|
2022-04-08 13:47:19 +00:00
|
|
|
* @param {string} setting Setting name (e.g. wpVersion or wcVersion).
|
|
|
|
* @param {string} version Version to compare.
|
2021-04-22 11:37:27 +00:00
|
|
|
* @param {compareVersions.CompareOperator} operator Comparison operator.
|
2019-10-11 15:29:27 +00:00
|
|
|
*/
|
2020-10-15 01:13:49 +00:00
|
|
|
const compareVersionSettingIgnorePrerelease = (
|
2021-04-22 11:37:27 +00:00
|
|
|
setting: string,
|
|
|
|
version: string,
|
|
|
|
operator: compareVersions.CompareOperator
|
|
|
|
): boolean => {
|
|
|
|
const settingValue = getSetting( setting, '' ) as string;
|
|
|
|
let replacement = settingValue.replace( /-[a-zA-Z0-9]*[\-]*/, '.0-rc.' );
|
2019-10-11 15:29:27 +00:00
|
|
|
replacement = replacement.endsWith( '.' )
|
|
|
|
? replacement.substring( 0, replacement.length - 1 )
|
|
|
|
: replacement;
|
2020-12-15 15:10:24 +00:00
|
|
|
return compareVersions.compare( replacement, version, operator );
|
2019-10-11 15:29:27 +00:00
|
|
|
};
|
|
|
|
|
2020-12-15 15:10:24 +00:00
|
|
|
/**
|
|
|
|
* Compare the current WP version with the provided `version` param using the
|
|
|
|
* `operator`.
|
|
|
|
*
|
|
|
|
* For example `isWpVersion( '5.6', '<=' )` returns true if the site WP version
|
|
|
|
* is smaller or equal than `5.6` .
|
|
|
|
*/
|
2021-04-22 11:37:27 +00:00
|
|
|
export const isWpVersion = (
|
|
|
|
version: string,
|
|
|
|
operator: compareVersions.CompareOperator = '='
|
|
|
|
): boolean => {
|
2020-10-15 01:13:49 +00:00
|
|
|
return compareVersionSettingIgnorePrerelease(
|
|
|
|
'wpVersion',
|
2020-12-15 15:10:24 +00:00
|
|
|
version,
|
2020-10-15 01:13:49 +00:00
|
|
|
operator
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-12-15 15:10:24 +00:00
|
|
|
/**
|
|
|
|
* Compare the current WC version with the provided `version` param using the
|
|
|
|
* `operator`.
|
|
|
|
*
|
|
|
|
* For example `isWcVersion( '4.9.0', '<=' )` returns true if the site WC version
|
|
|
|
* is smaller or equal than `4.9`.
|
|
|
|
*/
|
2021-04-22 11:37:27 +00:00
|
|
|
export const isWcVersion = (
|
|
|
|
version: string,
|
|
|
|
operator: compareVersions.CompareOperator = '='
|
|
|
|
): boolean => {
|
2020-10-15 01:13:49 +00:00
|
|
|
return compareVersionSettingIgnorePrerelease(
|
|
|
|
'wcVersion',
|
2020-12-15 15:10:24 +00:00
|
|
|
version,
|
2020-10-15 01:13:49 +00:00
|
|
|
operator
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2019-11-28 16:33:29 +00:00
|
|
|
/**
|
|
|
|
* Returns a string with the site's wp-admin URL appended. JS version of `admin_url`.
|
|
|
|
*
|
2019-12-10 17:17:46 +00:00
|
|
|
* @param {string} path Relative path.
|
|
|
|
* @return {string} Full admin URL.
|
2019-11-28 16:33:29 +00:00
|
|
|
*/
|
2021-04-22 11:37:27 +00:00
|
|
|
export const getAdminLink = ( path: string ): string =>
|
|
|
|
getSetting( 'adminUrl' ) + path;
|