class.url.php
49 lines
| 1 | <?php |
| 2 | |
| 3 | // Exit if accessed directly |
| 4 | if ( ! defined( 'ABSPATH' ) ) { |
| 5 | exit; |
| 6 | } |
| 7 | |
| 8 | /** |
| 9 | * Class WRIO_Webp_Hash_Src holds hash data about type, source src and normalized src which is mainly used to compare |
| 10 | * or replace data. |
| 11 | * |
| 12 | * @version 1.0 |
| 13 | */ |
| 14 | class WRIO_Url { |
| 15 | /** |
| 16 | * Check whether URI is valid or not. |
| 17 | * |
| 18 | * @param string $src Image path. |
| 19 | * @param bool $decode Whether to decode src. |
| 20 | * |
| 21 | * @return null|string NULL on failure to get valid uri. |
| 22 | */ |
| 23 | public static function normalize( $src, $decode = true ) { |
| 24 | $url_parts = wp_parse_url( $src ); |
| 25 | |
| 26 | // Unsupported scheme. |
| 27 | if ( isset( $url_parts['scheme'] ) && 'http' !== $url_parts['scheme'] && 'https' !== $url_parts['scheme'] ) { |
| 28 | return null; |
| 29 | } |
| 30 | |
| 31 | // This is a relative path, try to get the URL. |
| 32 | if ( ! isset( $url_parts['host'] ) && ! isset( $url_parts['scheme'] ) ) { |
| 33 | $src = site_url( $src ); |
| 34 | } |
| 35 | |
| 36 | $content_url = content_url(); |
| 37 | $content_url_http = str_replace( 'https://', 'http://', $content_url ); |
| 38 | if ( false === strpos( $src, $content_url ) && false === strpos( $src, $content_url_http ) ) { |
| 39 | return null; |
| 40 | } |
| 41 | |
| 42 | if ( $decode ) { |
| 43 | return urldecode( $src ); |
| 44 | } |
| 45 | |
| 46 | return $src; |
| 47 | } |
| 48 | } |
| 49 |