class-arr.php
2 years ago
class-attachment.php
2 years ago
class-conditional.php
2 years ago
class-db.php
2 years ago
class-html.php
2 years ago
class-param.php
2 years ago
class-str.php
2 years ago
class-url.php
2 years ago
class-wordpress.php
2 years ago
index.php
2 years ago
class-url.php
185 lines
| 1 | <?php |
| 2 | /** |
| 3 | * The URL helpers. |
| 4 | * |
| 5 | * @since 1.0.0 |
| 6 | * @package MyThemeShop |
| 7 | * @subpackage MyThemeShop\Helpers |
| 8 | * @author MyThemeShop <admin@mythemeshop.com> |
| 9 | */ |
| 10 | |
| 11 | namespace MyThemeShop\Helpers; |
| 12 | |
| 13 | use MyThemeShop\Helpers\Str; |
| 14 | use MyThemeShop\Helpers\Param; |
| 15 | |
| 16 | /** |
| 17 | * Url class. |
| 18 | */ |
| 19 | class Url { |
| 20 | |
| 21 | /** |
| 22 | * Simple check for validating a URL, it must start with http:// or https://. |
| 23 | * and pass FILTER_VALIDATE_URL validation. |
| 24 | * |
| 25 | * @param string $url to check. |
| 26 | * |
| 27 | * @return bool |
| 28 | */ |
| 29 | public static function is_url( $url ) { |
| 30 | if ( ! is_string( $url ) ) { |
| 31 | return false; |
| 32 | } |
| 33 | |
| 34 | // Must start with http:// or https://. |
| 35 | if ( 0 !== strpos( $url, 'http://' ) && 0 !== strpos( $url, 'https://' ) && 0 !== strpos( $url, '//' ) ) { |
| 36 | return false; |
| 37 | } |
| 38 | |
| 39 | // Check for scheme first, if it's missing then add it. |
| 40 | if ( 0 === strpos( $url, '//' ) ) { |
| 41 | $url = 'http:' . $url; |
| 42 | } |
| 43 | |
| 44 | // Must pass validation. |
| 45 | return false !== filter_var( trailingslashit( $url ), FILTER_VALIDATE_URL ) ? true : false; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Check whether a url is relative. |
| 50 | * |
| 51 | * @param string $url URL string to check. |
| 52 | * |
| 53 | * @return bool |
| 54 | */ |
| 55 | public static function is_relative( $url ) { |
| 56 | return ( 0 !== strpos( $url, 'http' ) && 0 !== strpos( $url, '//' ) ); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Checks whether a url is external. |
| 61 | * |
| 62 | * @param string $url URL string to check. This should be a absolute URL. |
| 63 | * @param string $domain If wants to use some other domain not home_url(). |
| 64 | * |
| 65 | * @return bool |
| 66 | */ |
| 67 | public static function is_external( $url, $domain = false ) { |
| 68 | if ( empty( $url ) || '#' === $url[0] ) { // Link to current page. |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | if ( self::is_affiliate( $url ) ) { |
| 73 | return true; |
| 74 | } |
| 75 | |
| 76 | if ( '/' === $url[0] ) { // Link to current page or relative link. |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | $domain = self::get_domain( $domain ? $domain : home_url() ); |
| 81 | if ( Str::contains( $domain, $url ) ) { |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Checks whether a link is an affiliate link. |
| 90 | * |
| 91 | * @param string $url URL string to check. This should be a absolute URL. |
| 92 | * |
| 93 | * @return bool |
| 94 | */ |
| 95 | public static function is_affiliate( $url ) { |
| 96 | if ( empty( $url ) ) { |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Filter: 'wp_helpers_is_affiliate_link' - Allows developer to consider a link as affiliate. |
| 102 | * |
| 103 | * @param bool $value Default false. |
| 104 | * @param string $url URL. |
| 105 | */ |
| 106 | return apply_filters( 'wp_helpers_is_affiliate_link', false, $url ); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Get current url. |
| 111 | * |
| 112 | * @codeCoverageIgnore |
| 113 | * |
| 114 | * @return string |
| 115 | */ |
| 116 | public static function get_current_url() { |
| 117 | return self::get_scheme() . '://' . self::get_host() . self::get_port() . Param::server( 'REQUEST_URI' ); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Get url scheme. |
| 122 | * |
| 123 | * @return string |
| 124 | */ |
| 125 | public static function get_scheme() { |
| 126 | return is_ssl() ? 'https' : 'http'; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Some setups like HTTP_HOST, some like SERVER_NAME, it's complicated. |
| 131 | * |
| 132 | * @link http://stackoverflow.com/questions/2297403/http-host-vs-server-name |
| 133 | * |
| 134 | * @codeCoverageIgnore |
| 135 | * |
| 136 | * @return string the HTTP_HOST or SERVER_NAME |
| 137 | */ |
| 138 | public static function get_host() { |
| 139 | $host = Param::server( 'HTTP_HOST' ); |
| 140 | if ( false !== $host ) { |
| 141 | return $host; |
| 142 | } |
| 143 | |
| 144 | $name = Param::server( 'SERVER_NAME' ); |
| 145 | if ( false !== $name ) { |
| 146 | return $name; |
| 147 | } |
| 148 | |
| 149 | return ''; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Get current request port. |
| 154 | * |
| 155 | * @return string |
| 156 | */ |
| 157 | public static function get_port() { |
| 158 | $port = Param::server( 'SERVER_PORT' ); |
| 159 | $has_port = $port && ! in_array( $port, [ '80', '443' ], true ); |
| 160 | return $has_port ? ':' . $port : ''; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Get parent domain |
| 165 | * |
| 166 | * @param string $url Url to parse. |
| 167 | * |
| 168 | * @return string |
| 169 | */ |
| 170 | public static function get_domain( $url ) { |
| 171 | $pieces = wp_parse_url( $url ); |
| 172 | $domain = isset( $pieces['host'] ) ? $pieces['host'] : ''; |
| 173 | |
| 174 | if ( Str::contains( 'localhost', $domain ) ) { |
| 175 | return 'localhost'; |
| 176 | } |
| 177 | |
| 178 | if ( preg_match( '/(?P<domain>[a-zÀ-ž0-9][a-zÀ-ž0-9\-]{1,63}\.[a-zÀ-ž\.]{2,15})$/ui', $domain, $regs ) ) { |
| 179 | return $regs['domain']; |
| 180 | } |
| 181 | |
| 182 | return false; |
| 183 | } |
| 184 | } |
| 185 |