Skip to content

Commit 7627128

Browse files
committed
WIP: Fix fetching relative stylesheet URLs
1 parent 506847f commit 7627128

File tree

2 files changed

+78
-10
lines changed

2 files changed

+78
-10
lines changed

includes/sanitizers/class-amp-form-sanitizer.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ public function sanitize() {
112112
/**
113113
* Get the action URL for the form element.
114114
*
115+
* @todo De-duplicate with AMP_Style_Sanitizer::normalize_stylesheet_url().
116+
*
115117
* @param string $action_url Action URL.
116118
* @return string Action URL.
117119
*/
@@ -140,28 +142,25 @@ protected function get_action_url( $action_url ) {
140142
return $action_url;
141143
}
142144

143-
// Make URL protocol relative.
144-
$parsed_url['scheme'] = '//';
145-
146145
// Set an empty path if none is defined but there is a host.
147146
if ( ! isset( $parsed_url['path'] ) && isset( $parsed_url['host'] ) ) {
148147
$parsed_url['path'] = '';
149148
}
150149

151150
if ( ! isset( $parsed_url['host'] ) ) {
152-
$parsed_url['host'] = $_SERVER['HTTP_HOST'];
151+
$parsed_url['host'] = $_SERVER['HTTP_HOST']; // @todo Use home_url() instead?
153152
}
154153

155154
if ( ! isset( $parsed_url['path'] ) ) {
156155
// If there is action URL path, use the one from the request.
157-
$parsed_url['path'] = trailingslashit( wp_unslash( $_SERVER['REQUEST_URI'] ) );
156+
$parsed_url['path'] = trailingslashit( wp_unslash( $_SERVER['REQUEST_URI'] ) ); // @todo This is wrong because it includes the path.
158157
} elseif ( '' !== $parsed_url['path'] && '/' !== $parsed_url['path'][0] ) {
159158
// If the path is relative, append it to the current request path.
160-
$parsed_url['path'] = trailingslashit( wp_unslash( $_SERVER['REQUEST_URI'] ) ) . trailingslashit( $parsed_url['path'] );
159+
$parsed_url['path'] = trailingslashit( wp_unslash( $_SERVER['REQUEST_URI'] ) ) . trailingslashit( $parsed_url['path'] ); // @todo This is wrong because it includes the path.
161160
}
162161

163162
// Rebuild the URL.
164-
$action_url = $parsed_url['scheme'];
163+
$action_url = '//';
165164
if ( isset( $parsed_url['user'] ) ) {
166165
$action_url .= $parsed_url['user'];
167166
if ( isset( $parsed_url['pass'] ) ) {

includes/sanitizers/class-amp-style-sanitizer.php

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,6 +1358,75 @@ private function get_stylesheet_from_url( $stylesheet_url ) {
13581358
return $this->fetch_external_stylesheet( $stylesheet_url );
13591359
}
13601360

1361+
/**
1362+
* Get the action URL for the form element.
1363+
*
1364+
* @todo De-duplicate with \AMP_Form_Sanitizer::get_action_url().
1365+
*
1366+
* @param string $stylesheet_url Stylesheet URL.
1367+
* @return string|WP_Error Stylesheet URL.
1368+
*/
1369+
protected function normalize_stylesheet_url( $stylesheet_url ) {
1370+
if ( ! $stylesheet_url ) {
1371+
return new WP_Error( 'empty_stylesheet_url', __( 'Empty stylesheet URL', 'amp' ) );
1372+
}
1373+
1374+
$parsed_url = wp_parse_url( $stylesheet_url );
1375+
if ( ! $parsed_url ) {
1376+
return new WP_Error( 'stylesheet_url_parse_error', __( 'Stylesheet URL parse error', 'amp' ) );
1377+
}
1378+
1379+
// If a scheme was provided, there's nothing to do.
1380+
if ( ! empty( $parsed_url['scheme'] ) ) {
1381+
return $stylesheet_url;
1382+
}
1383+
1384+
$parsed_home_url = wp_parse_url( home_url() );
1385+
1386+
// Supply the same scheme as the site.
1387+
$parsed_url['scheme'] = $parsed_home_url['scheme'];
1388+
1389+
// Set an empty path if none is defined but there is a host.
1390+
if ( ! isset( $parsed_url['path'] ) && isset( $parsed_url['host'] ) ) {
1391+
$parsed_url['path'] = '';
1392+
}
1393+
1394+
if ( ! isset( $parsed_url['host'] ) ) {
1395+
$parsed_url['host'] = $parsed_home_url['host'];
1396+
}
1397+
1398+
if ( ! isset( $parsed_url['path'] ) ) {
1399+
// If there is action URL path, use the one from the request.
1400+
$parsed_url['path'] = trailingslashit( wp_unslash( $_SERVER['REQUEST_URI'] ) ); // @todo This is wrong because it includes the path.
1401+
} elseif ( '' !== $parsed_url['path'] && '/' !== $parsed_url['path'][0] ) {
1402+
// If the path is relative, append it to the current request path.
1403+
$parsed_url['path'] = trailingslashit( wp_unslash( $_SERVER['REQUEST_URI'] ) ) . trailingslashit( $parsed_url['path'] ); // @todo This is wrong because it includes the path.
1404+
}
1405+
1406+
// Rebuild the URL.
1407+
$stylesheet_url = $parsed_url['scheme'] . '://';
1408+
if ( isset( $parsed_url['user'] ) ) {
1409+
$stylesheet_url .= $parsed_url['user'];
1410+
if ( isset( $parsed_url['pass'] ) ) {
1411+
$stylesheet_url .= ':' . $parsed_url['pass'];
1412+
}
1413+
$stylesheet_url .= '@';
1414+
}
1415+
$stylesheet_url .= $parsed_url['host'];
1416+
if ( isset( $parsed_url['port'] ) ) {
1417+
$stylesheet_url .= ':' . $parsed_url['port'];
1418+
}
1419+
$stylesheet_url .= $parsed_url['path'];
1420+
if ( isset( $parsed_url['query'] ) ) {
1421+
$stylesheet_url .= '?' . $parsed_url['query'];
1422+
}
1423+
if ( isset( $parsed_url['fragment'] ) ) {
1424+
$stylesheet_url .= '#' . $parsed_url['fragment'];
1425+
}
1426+
1427+
return esc_url_raw( $stylesheet_url );
1428+
}
1429+
13611430
/**
13621431
* Fetch external stylesheet.
13631432
*
@@ -1368,9 +1437,9 @@ private function get_stylesheet_from_url( $stylesheet_url ) {
13681437
*/
13691438
private function fetch_external_stylesheet( $url ) {
13701439

1371-
// Prepend schemeless stylesheet URL with the same URL scheme as the current site.
1372-
if ( '//' === substr( $url, 0, 2 ) ) {
1373-
$url = wp_parse_url( home_url(), PHP_URL_SCHEME ) . ':' . $url;
1440+
$url = $this->normalize_stylesheet_url( $url );
1441+
if ( is_wp_error( $url ) ) {
1442+
return $url;
13741443
}
13751444

13761445
$cache_key = md5( $url );

0 commit comments

Comments
 (0)