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
|
|
|
*/
|
2020-06-26 12:01:35 +00:00
|
|
|
import { getSetting } from './get-setting';
|
2021-04-08 12:31:12 +00:00
|
|
|
import '../../filters/exclude-draft-status-from-analytics';
|
2019-10-11 15:29:27 +00:00
|
|
|
|
2019-09-23 18:07:13 +00:00
|
|
|
export * from './default-constants';
|
2021-04-08 12:31:12 +00:00
|
|
|
export * from './default-address-fields';
|
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
|
|
|
*
|
2020-10-15 01:13:49 +00:00
|
|
|
* @param {string} setting Setting name (e.g. wpVersion or wcVersion).
|
2020-12-15 15:10:24 +00:00
|
|
|
* @param {string} version Version to compare.
|
2019-12-10 17:17:46 +00:00
|
|
|
* @param {string} operator Comparison operator.
|
2019-10-11 15:29:27 +00:00
|
|
|
*/
|
2020-10-15 01:13:49 +00:00
|
|
|
const compareVersionSettingIgnorePrerelease = (
|
|
|
|
setting,
|
2020-12-15 15:10:24 +00:00
|
|
|
version,
|
2020-10-15 01:13:49 +00:00
|
|
|
operator
|
|
|
|
) => {
|
|
|
|
let replacement = getSetting( setting, '' ).replace(
|
2019-10-11 15:29:27 +00:00
|
|
|
/-[a-zA-Z0-9]*[\-]*/,
|
|
|
|
'.0-rc.'
|
|
|
|
);
|
|
|
|
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` .
|
|
|
|
*
|
|
|
|
* @param {string} version Version to use to compare against the current wpVersion.
|
|
|
|
* @param {string} [operator='='] Operator to use in the comparison.
|
|
|
|
*/
|
|
|
|
export const isWpVersion = ( version, operator = '=' ) => {
|
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`.
|
|
|
|
*
|
|
|
|
* @param {string} version Version to use to compare against the current wcVersion.
|
|
|
|
* @param {string} [operator='='] Operator to use in the comparison.
|
|
|
|
*/
|
|
|
|
export const isWcVersion = ( version, operator = '=' ) => {
|
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-10-11 15:29:27 +00:00
|
|
|
export { compareVersions, getSetting };
|
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
|
|
|
*/
|
|
|
|
export const getAdminLink = ( path ) => getSetting( 'adminUrl' ) + path;
|