PluginProbe ʕ •ᴥ•ʔ
Smush – Image Optimization, Compression, Lazy Load, WebP & CDN / 4.1.0
Smush – Image Optimization, Compression, Lazy Load, WebP & CDN v4.1.0
4.1.0 4.0.3 4.0.2 2.8.1 2.9.1 3.0.0 3.0.1 3.0.2 3.1.1 3.10.1 3.10.2 3.10.3 3.11.1 3.12.3 3.12.4 3.12.5 3.12.6 3.13.0 3.13.1 3.13.2 3.14.0 3.14.1 3.14.2 3.15.0 3.15.1 3.15.2 3.15.3 3.15.4 3.15.5 3.16.2 3.16.4 3.16.5 3.16.6 3.17.0 3.17.1 3.18.0 3.18.1 3.2.0.1 3.2.1 3.2.2.1 3.2.4 3.20.0 3.21.1 3.22.1 3.22.3 3.23.0 3.23.1 3.23.2 3.23.3 3.23.4 3.24.0 3.24.0-beta.2 3.3.0 3.3.1 3.3.2 3.4.1 3.4.2 3.6.1 3.6.3 3.7.0 3.7.1 3.7.2 3.7.3 3.8.2 3.8.3 3.8.4 3.8.5 3.8.7 3.8.8 3.9.0 3.9.1 3.9.11 3.9.2 3.9.4 3.9.5 3.9.8 3.9.9 trunk 1.0.0 1.0.1 1.0.2 1.1 1.1.1 1.1.2 1.1.3 1.2 1.2.1 1.2.10 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.1 1.3.2 1.3.3 1.3.4 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.5.1 1.6.5.2 1.6.5.3 1.6.5.4 1.7 1.7.1 1.7.1.1 2.0 2.0.1 2.0.3 2.0.4 2.0.5 2.0.6 2.0.6.2 2.0.6.3 2.0.6.5 2.0.7 2.0.7.1 2.1 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2 2.2.1 2.2.2 2.3 2.3.1 2.4 2.4.2 2.4.3 2.4.4 2.4.5 2.5.2 2.5.3 2.6.1 2.6.2 2.6.3 2.7 2.7.1 2.7.4 2.7.4.1 2.7.5 2.7.6 2.7.8 2.7.8.1 2.7.9.1 2.8.0 2.8.0.1
wp-smushit / core / parser / class-image-url.php
wp-smushit / core / parser Last commit date
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