class-composite-element.php
3 days ago
class-element-attribute.php
3 days ago
class-element-css-property.php
3 days ago
class-element.php
3 days ago
class-image-url.php
3 days ago
class-page-parser.php
3 days ago
class-page.php
3 days ago
class-parser.php
3 days ago
class-placeholder-replacement.php
3 days ago
class-replaceable.php
3 days ago
class-rest-content.php
3 days ago
class-style.php
3 days ago
class-value.php
3 days ago
class-image-url.php
164 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Smush\Core\Parser; |
| 4 | |
| 5 | class Image_URL { |
| 6 | /** |
| 7 | * @var Value |
| 8 | */ |
| 9 | private $url; |
| 10 | |
| 11 | private $ext; |
| 12 | |
| 13 | private $base_url; |
| 14 | |
| 15 | private $absolute_url; |
| 16 | |
| 17 | private $scheme; |
| 18 | |
| 19 | public function __construct( $url, $ext, $base_url ) { |
| 20 | $this->url = new Value( $url ); |
| 21 | $this->ext = $ext; |
| 22 | $this->base_url = $base_url; |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * @return string |
| 27 | */ |
| 28 | public function get_url() { |
| 29 | return $this->url->get(); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * @param $url |
| 34 | * |
| 35 | * @return bool |
| 36 | */ |
| 37 | public function set_url( $url ) { |
| 38 | /** |
| 39 | * If the new value matches the absolute URL then there is no need to update. |
| 40 | * The url class {@see Value::set()} also internally checks if the value is the same as before. |
| 41 | */ |
| 42 | $current_absolute_url = $this->get_absolute_url(); |
| 43 | if ( $url === $current_absolute_url ) { |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | return $this->url->set( $url ); |
| 48 | } |
| 49 | |
| 50 | public function get_base_url() { |
| 51 | return $this->base_url; |
| 52 | } |
| 53 | |
| 54 | public function get_previous_url() { |
| 55 | return $this->url->get_previous(); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @return mixed |
| 60 | */ |
| 61 | public function get_ext() { |
| 62 | return $this->ext; |
| 63 | } |
| 64 | |
| 65 | public function has_updates() { |
| 66 | return $this->url->has_updates(); |
| 67 | } |
| 68 | |
| 69 | public function get_scheme() { |
| 70 | if ( is_null( $this->scheme ) ) { |
| 71 | $this->scheme = $this->prepare_scheme(); |
| 72 | } |
| 73 | return $this->scheme; |
| 74 | } |
| 75 | |
| 76 | private function prepare_scheme() { |
| 77 | $url_parts = wp_parse_url( $this->get_absolute_url() ); |
| 78 | |
| 79 | return $url_parts |
| 80 | ? $url_parts['scheme'] |
| 81 | : ''; |
| 82 | } |
| 83 | |
| 84 | public function get_absolute_url() { |
| 85 | if ( empty( $this->get_base_url() ) ) { |
| 86 | // If a base URL is not provided we don't try to make an absolute URL |
| 87 | return $this->get_url(); |
| 88 | } |
| 89 | |
| 90 | if ( is_null( $this->absolute_url ) ) { |
| 91 | $this->absolute_url = $this->prepare_absolute_url(); |
| 92 | } |
| 93 | |
| 94 | return $this->absolute_url; |
| 95 | } |
| 96 | |
| 97 | private function prepare_absolute_url() { |
| 98 | if ( $this->is_scheme_missing_from_original() ) { |
| 99 | $scheme = is_ssl() ? 'https:' : 'http:'; |
| 100 | $full_url = $scheme . $this->url->get(); |
| 101 | } else if ( $this->is_original_url_absolute() ) { |
| 102 | $full_url = $this->url->get(); |
| 103 | } else if ( $this->original_url_starts_with_slash() ) { |
| 104 | $full_url = $this->make_url_relative_to_host(); |
| 105 | } else { |
| 106 | $full_url = $this->make_url_relative_to_base(); |
| 107 | } |
| 108 | |
| 109 | return $this->resolve_relative_url( $full_url ); |
| 110 | } |
| 111 | |
| 112 | private function is_original_url_absolute() { |
| 113 | $scheme = parse_url( $this->url->get(), PHP_URL_SCHEME ); |
| 114 | |
| 115 | return ! empty( $scheme ); |
| 116 | } |
| 117 | |
| 118 | private function is_scheme_missing_from_original() { |
| 119 | return str_starts_with( $this->url->get(), '//' ); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * @param $full_url |
| 124 | * |
| 125 | * @return string |
| 126 | */ |
| 127 | private function resolve_relative_url( $full_url ) { |
| 128 | $path = parse_url( $full_url, PHP_URL_PATH ); |
| 129 | $resolved_path = str_replace( '/./', '/', $path ); |
| 130 | |
| 131 | // TODO: in the following regex [a-zA-Z0-9-_.] is too narrow, what about non-english characters? |
| 132 | $pattern = '@/[a-zA-Z0-9-_.]*/\.{2}/@i'; |
| 133 | while ( preg_match( $pattern, $resolved_path ) ) { |
| 134 | $resolved_path = preg_replace( $pattern, '/', $resolved_path ); |
| 135 | } |
| 136 | |
| 137 | return str_replace( $path, $resolved_path, $full_url ); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * @return bool |
| 142 | */ |
| 143 | private function original_url_starts_with_slash() { |
| 144 | return str_starts_with( $this->url->get(), '/' ); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * @return string |
| 149 | */ |
| 150 | private function make_url_relative_to_host() { |
| 151 | $scheme = parse_url( $this->base_url, PHP_URL_SCHEME ); |
| 152 | $host = parse_url( $this->base_url, PHP_URL_HOST ); |
| 153 | |
| 154 | return trailingslashit( "$scheme://$host" ) . ltrim( $this->url->get(), '/' ); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * @return string |
| 159 | */ |
| 160 | private function make_url_relative_to_base() { |
| 161 | return trailingslashit( $this->base_url ) . ltrim( $this->url->get(), '/' ); |
| 162 | } |
| 163 | } |
| 164 |