BlockHooksTrait.php
1 month ago
BlockTemplateUtils.php
2 months ago
BlocksSharedState.php
3 months ago
BlocksWpQuery.php
2 years ago
CartCheckoutUtils.php
1 month ago
MiniCartUtils.php
1 year ago
ProductAvailabilityUtils.php
11 months ago
ProductDataUtils.php
1 year ago
ProductGalleryUtils.php
1 month ago
StyleAttributesUtils.php
1 year ago
Utils.php
4 days ago
Utils.php
53 lines
| 1 | <?php |
| 2 | namespace Automattic\WooCommerce\Blocks\Utils; |
| 3 | |
| 4 | use Automattic\WooCommerce\Proxies\LegacyProxy; |
| 5 | |
| 6 | /** |
| 7 | * Utils class |
| 8 | */ |
| 9 | class Utils { |
| 10 | |
| 11 | /** |
| 12 | * Compare the current WordPress version with a given version. It's a wrapper around `version-compare` |
| 13 | * that additionally takes into account the suffix (like `-RC1`). |
| 14 | * For example: version 6.3 is considered lower than 6.3-RC2, so you can do |
| 15 | * wp_version_compare( '6.3', '>=' ) and that will return true for 6.3-RC2. |
| 16 | * |
| 17 | * @param string $version The version to compare against. |
| 18 | * @param string|null $operator Optional. The comparison operator. Defaults to null. |
| 19 | * @return bool|int Returns true if the current WordPress version satisfies the comparison, false otherwise. |
| 20 | */ |
| 21 | public static function wp_version_compare( $version, $operator = null ) { |
| 22 | $current_wp_version = get_bloginfo( 'version' ); |
| 23 | if ( preg_match( '/^([0-9]+\.[0-9]+)/', $current_wp_version, $matches ) ) { |
| 24 | $current_wp_version = (float) $matches[1]; |
| 25 | } |
| 26 | |
| 27 | // Replace non-alphanumeric characters with a dot. |
| 28 | $current_wp_version = preg_replace( '/[^0-9a-zA-Z\.]+/i', '.', $current_wp_version ); |
| 29 | $version = preg_replace( '/[^0-9a-zA-Z\.]+/i', '.', $version ); |
| 30 | |
| 31 | return version_compare( $current_wp_version, $version, $operator ); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Resolve a (possibly relative) script src to an absolute URL the same way |
| 36 | * WordPress core does in WP_Scripts::do_item(): a relative src is resolved |
| 37 | * against the scripts base URL (the site URL), unless it already points at |
| 38 | * the content directory. This keeps the resulting URL consistent with what |
| 39 | * WordPress itself would emit, including on non-default directory layouts |
| 40 | * (e.g. a custom WP_CONTENT_DIR/WP_CONTENT_URL). |
| 41 | * |
| 42 | * @param string $src The script src, which may be relative or absolute. |
| 43 | * @return string The absolute script URL. |
| 44 | */ |
| 45 | public static function get_absolute_script_url( $src ) { |
| 46 | $wp_scripts = wc_get_container()->get( LegacyProxy::class )->call_function( 'wp_scripts' ); |
| 47 | if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $wp_scripts->content_url && 0 === strpos( $src, $wp_scripts->content_url ) ) ) { |
| 48 | $src = $wp_scripts->base_url . $src; |
| 49 | } |
| 50 | return $src; |
| 51 | } |
| 52 | } |
| 53 |