BlockHooksTrait.php
2 months ago
BlockTemplateUtils.php
2 weeks ago
BlocksSharedState.php
5 months ago
BlocksWpQuery.php
2 years ago
CartCheckoutUtils.php
2 months ago
MiniCartUtils.php
1 year ago
ProductAvailabilityUtils.php
1 year ago
ProductDataUtils.php
1 year ago
ProductGalleryUtils.php
1 week ago
StyleAttributesUtils.php
1 year ago
Utils.php
1 week ago
Utils.php
89 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 | /** |
| 54 | * Get the current page URL using the request path relative to home. |
| 55 | * |
| 56 | * @internal This function is used internally by WooCommerce blocks to get the current page URL. It is not intended for external use. |
| 57 | * |
| 58 | * @return string The current page URL. |
| 59 | * |
| 60 | * @since 11.1.0 |
| 61 | */ |
| 62 | public static function get_current_page_url() { |
| 63 | global $wp, $wp_rewrite; |
| 64 | |
| 65 | $request_path = is_object( $wp ) && isset( $wp->request ) && is_string( $wp->request ) ? $wp->request : ''; |
| 66 | |
| 67 | // PATHINFO permalinks keep index.php in the public URL; $wp->request does not. |
| 68 | if ( |
| 69 | '' !== $request_path && |
| 70 | $wp_rewrite instanceof \WP_Rewrite && |
| 71 | $wp_rewrite->using_index_permalinks() |
| 72 | ) { |
| 73 | $index = is_string( $wp_rewrite->index ) && '' !== $wp_rewrite->index ? $wp_rewrite->index : 'index.php'; |
| 74 | if ( $index !== $request_path && ! str_starts_with( $request_path, $index . '/' ) ) { |
| 75 | $request_path = $index . '/' . $request_path; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | $url = home_url( user_trailingslashit( $request_path ) ); |
| 80 | |
| 81 | if ( isset( $_SERVER['QUERY_STRING'] ) && is_string( $_SERVER['QUERY_STRING'] ) && '' !== $_SERVER['QUERY_STRING'] ) { |
| 82 | // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Preserving the raw query string encoding and delimiters. |
| 83 | $url .= '?' . wp_unslash( $_SERVER['QUERY_STRING'] ); |
| 84 | } |
| 85 | |
| 86 | return $url; |
| 87 | } |
| 88 | } |
| 89 |