Skip to content

Commit

Permalink
Update class-block-converter.php
Browse files Browse the repository at this point in the history
Rewrite URL parsing to include pieces we want, extract scheme from home option
  • Loading branch information
kevinfodness authored Nov 3, 2023
1 parent 0c9f332 commit ea3f153
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions src/class-block-converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ protected function p( DOMNode $node ): ?Block {
/**
* Create ul blocks.
*
* @param DOMNode $node The node.
* @param DOMNode $node The node.
* @return Block
*/
protected function ul( DOMNode $node ): Block {
Expand Down Expand Up @@ -318,16 +318,27 @@ public function remove_image_args( $url ): string {
// Split url.
$url_parts = wp_parse_url( $url );

if ( isset( $url_parts['query'] ) ) {
$url = str_replace( '?' . $url_parts['query'], '', $url );
}
if ( isset( $url_parts['fragment'] ) ) {
$url = str_replace( '#' . $url_parts['fragment'], '', $url );
// Negotiate scheme.
$scheme = $url_parts['scheme'] ?? '';
if ( empty( $scheme ) ) {
$home = get_option( 'home' );
if ( ! empty( $home ) && is_string( $home ) ) {
$home_url_parts = wp_parse_url( $home );
$scheme = $home_url_parts['scheme'] ?? '';
}
}
if ( str_starts_with( $url, '//' ) ) {
$url = preg_replace( '#^//#', 'https://', $url );

// Negotiate other properties.
$host = $url_parts['host'] ?? '';
$port = ! empty( $url_parts['port'] ) ? ':' . $url_parts['port'] : '';
$path = $url_parts['path'] ?? '';

// Ensure we have enough parts to construct a valid URL.
if ( empty( $scheme ) || empty( $host ) || empty( $path ) ) {
return '';
}
return $url;

return sprintf( '%s://%s%s%s', $scheme, $host, $port, $path );
}

/**
Expand Down

0 comments on commit ea3f153

Please sign in to comment.