Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consistently use wp_parse_url(); deprecate AMP_WP_Utils #995

Merged
merged 9 commits into from
Mar 19, 2018
2 changes: 1 addition & 1 deletion includes/embeds/class-amp-dailymotion-embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public function render( $args ) {
* @return integer Video ID.
*/
private function get_video_id_from_url( $url ) {
$parsed_url = AMP_WP_Utils::parse_url( $url );
$parsed_url = wp_parse_url( $url );
parse_str( $parsed_url['path'], $path );
$tok = explode( '/', $parsed_url['path'] );
$tok = explode( '_', $tok[2] );
Expand Down
2 changes: 1 addition & 1 deletion includes/embeds/class-amp-soundcloud-embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ private function render_embed_fallback( $url ) {
* @return string Track ID or empty string if none matched.
*/
private function get_track_id_from_url( $url ) {
$parsed_url = AMP_WP_Utils::parse_url( $url );
$parsed_url = wp_parse_url( $url );
if ( ! preg_match( '#tracks/(?P<track_id>[^/]+)#', $parsed_url['path'], $matches ) ) {
return '';
}
Expand Down
2 changes: 1 addition & 1 deletion includes/embeds/class-amp-vimeo-embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public function render( $args ) {
* @return integer Video ID.
*/
private function get_video_id_from_url( $url ) {
$parsed_url = parse_url( $url );
$parsed_url = wp_parse_url( $url );
parse_str( $parsed_url['path'], $path );

$video_id = '';
Expand Down
2 changes: 1 addition & 1 deletion includes/embeds/class-amp-youtube-embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public function render( $args ) {
*/
private function get_video_id_from_url( $url ) {
$video_id = false;
$parsed_url = AMP_WP_Utils::parse_url( $url );
$parsed_url = wp_parse_url( $url );

if ( self::SHORT_URL_HOST === substr( $parsed_url['host'], -strlen( self::SHORT_URL_HOST ) ) ) {
/* youtu.be/{id} */
Expand Down
2 changes: 1 addition & 1 deletion includes/sanitizers/class-amp-img-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ private function adjust_and_replace_node( $node ) {
*/
private function is_gif_url( $url ) {
$ext = self::$anim_extension;
$path = AMP_WP_Utils::parse_url( $url, PHP_URL_PATH );
$path = wp_parse_url( $url, PHP_URL_PATH );
return substr( $path, -strlen( $ext ) ) === $ext;
}
}
10 changes: 5 additions & 5 deletions includes/sanitizers/class-amp-tag-and-attribute-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -1216,7 +1216,7 @@ private function check_attr_spec_rule_allowed_protocol( $node, $attr_name, $attr
* This seems to be an acceptable check since the AMP validator
* will allow a URL with no protocol to pass validation.
*/
$url_scheme = AMP_WP_Utils::parse_url( $url, PHP_URL_SCHEME );
$url_scheme = wp_parse_url( $url, PHP_URL_SCHEME );
if ( $url_scheme ) {
if ( ! in_array( strtolower( $url_scheme ), $attr_spec_rule[ AMP_Rule_Spec::VALUE_URL ][ AMP_Rule_Spec::ALLOWED_PROTOCOL ], true ) ) {
return AMP_Rule_Spec::FAIL;
Expand All @@ -1233,7 +1233,7 @@ private function check_attr_spec_rule_allowed_protocol( $node, $attr_name, $attr
* This seems to be an acceptable check since the AMP validator
* will allow a URL with no protocol to pass validation.
*/
$url_scheme = AMP_WP_Utils::parse_url( $url, PHP_URL_SCHEME );
$url_scheme = wp_parse_url( $url, PHP_URL_SCHEME );
if ( $url_scheme ) {
if ( ! in_array( strtolower( $url_scheme ), $attr_spec_rule[ AMP_Rule_Spec::VALUE_URL ][ AMP_Rule_Spec::ALLOWED_PROTOCOL ], true ) ) {
return AMP_Rule_Spec::FAIL;
Expand Down Expand Up @@ -1266,7 +1266,7 @@ private function check_attr_spec_rule_disallowed_relative( $node, $attr_name, $a
if ( $node->hasAttribute( $attr_name ) ) {
$urls_to_test = preg_split( '/\s*,\s*/', $node->getAttribute( $attr_name ) );
foreach ( $urls_to_test as $url ) {
$parsed_url = AMP_WP_Utils::parse_url( $url );
$parsed_url = wp_parse_url( $url );

/*
* The JS AMP validator seems to consider 'relative' to mean
Expand All @@ -1285,7 +1285,7 @@ private function check_attr_spec_rule_disallowed_relative( $node, $attr_name, $a
if ( $node->hasAttribute( $alternative_name ) ) {
$urls_to_test = preg_split( '/\s*,\s*/', $node->getAttribute( $alternative_name ) );
foreach ( $urls_to_test as $url ) {
$parsed_url = AMP_WP_Utils::parse_url( $url );
$parsed_url = wp_parse_url( $url );
if ( empty( $parsed_url['scheme'] ) ) {
return AMP_Rule_Spec::FAIL;
}
Expand Down Expand Up @@ -1338,7 +1338,7 @@ private function check_attr_spec_rule_disallowed_empty( $node, $attr_name, $attr
private function check_attr_spec_rule_disallowed_domain( $node, $attr_name, $attr_spec_rule ) {
if ( isset( $attr_spec_rule[ AMP_Rule_Spec::DISALLOWED_DOMAIN ] ) && $node->hasAttribute( $attr_name ) ) {
$attr_value = $node->getAttribute( $attr_name );
$url_domain = AMP_WP_Utils::parse_url( $attr_value, PHP_URL_HOST );
$url_domain = wp_parse_url( $attr_value, PHP_URL_HOST );
if ( ! empty( $url_domain ) ) {
foreach ( $attr_spec_rule[ AMP_Rule_Spec::DISALLOWED_DOMAIN ] as $disallowed_domain ) {
if ( strtolower( $url_domain ) === strtolower( $disallowed_domain ) ) {
Expand Down
2 changes: 1 addition & 1 deletion includes/utils/class-amp-image-dimension-extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static function normalize_url( $url ) {
return set_url_scheme( $url, 'http' );
}

$parsed = AMP_WP_Utils::parse_url( $url );
$parsed = wp_parse_url( $url );
if ( ! isset( $parsed['host'] ) ) {
$path = '';
if ( isset( $parsed['path'] ) ) {
Expand Down
47 changes: 44 additions & 3 deletions includes/utils/class-amp-wp-utils.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,39 @@
<?php
/**
* Class AMP_WP_Utils
*
* @package AMP
*/

/**
* Class AMP_WP_Utils
*
* @since 0.5
*
* @deprecated 0.7 As WordPress 4.7 is our minimum supported version.
*/
class AMP_WP_Utils {
/**
* wp_parse_url in < WordPress 4.7 does not respect the component arg, so we're adding this helper so we can use it.
* The core function wp_parse_url in < WordPress 4.7 does not respect the component arg. This helper lets us use it.
*
* This can be removed once 4.8 is out and we bump up our min supported WP version.
* Don't use.
*
* @deprecated 0.7 wp_parse_url() is now used instead.
*
* @param string $url The raw URL. Can be false if the URL failed to parse.
* @param int $component The specific component to retrieve. Use one of the PHP
* predefined constants to specify which one.
* Defaults to -1 (= return all parts as an array).
* @return mixed False on parse failure; Array of URL components on success;
* When a specific component has been requested: null if the component
* doesn't exist in the given URL; a string or - in the case of
* PHP_URL_PORT - integer when it does. See parse_url()'s return values.
*/
public static function parse_url( $url, $component = -1 ) {
_deprecated_function( __METHOD__, '0.7', 'wp_parse_url' );
$parsed = wp_parse_url( $url, $component );

// Because < 4.7 always returned a full array regardless of component
// Because < 4.7 always returned a full array regardless of component.
if ( -1 !== $component && is_array( $parsed ) ) {
return self::_get_component_from_parsed_url_array( $parsed, $component );
}
Expand All @@ -21,6 +45,17 @@ public static function parse_url( $url, $component = -1 ) {
* Included for 4.6 back-compat
*
* Copied from https://developer.wordpress.org/reference/functions/_get_component_from_parsed_url_array/
*
* @deprecated 0.7
*
* @param array|false $url_parts The parsed URL. Can be false if the URL failed to parse.
* @param int $component The specific component to retrieve. Use one of the PHP
* predefined constants to specify which one.
* Defaults to -1 (= return all parts as an array).
* @return mixed False on parse failure; Array of URL components on success;
* When a specific component has been requested: null if the component
* doesn't exist in the given URL; a string or - in the case of
* PHP_URL_PORT - integer when it does. See parse_url()'s return values.
*/
protected static function _get_component_from_parsed_url_array( $url_parts, $component = -1 ) {
if ( -1 === $component ) {
Expand All @@ -39,6 +74,12 @@ protected static function _get_component_from_parsed_url_array( $url_parts, $com
* Included for 4.6 back-compat
*
* Copied from https://developer.wordpress.org/reference/functions/_wp_translate_php_url_constant_to_key/
*
* @param int $constant The specific component to retrieve. Use one of the PHP
* predefined constants to specify which one.
* @return mixed False if component not found. string or integer if found.
*
* @deprecated 0.7
*/
protected static function _wp_translate_php_url_constant_to_key( $constant ) {
$translation = array(
Expand Down
2 changes: 1 addition & 1 deletion jetpack-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function jetpack_amp_build_stats_pixel_url() {
$blog = Jetpack_Options::get_option( 'id' );
$tz = get_option( 'gmt_offset' );
$v = 'ext';
$blog_url = AMP_WP_Utils::parse_url( site_url() );
$blog_url = wp_parse_url( site_url() );
$srv = $blog_url['host'];
$j = sprintf( '%s:%s', JETPACK__API_VERSION, JETPACK__VERSION );
$post = $wp_the_query->get_queried_object_id();
Expand Down
69 changes: 0 additions & 69 deletions tests/test-amp-wp-utils.php

This file was deleted.

4 changes: 2 additions & 2 deletions wpcom-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,12 @@ function wpcom_amp_extract_image_dimensions_from_querystring( $dimensions ) {
continue;
}

$host = parse_url( $url, PHP_URL_HOST );
$host = wp_parse_url( $url, PHP_URL_HOST );
if ( ! wp_endswith( $host, '.wp.com' ) || ! wp_endswith( $host, '.files.wordpress.com' ) ) {
continue;
}

parse_str( parse_url( $url, PHP_URL_QUERY ), $query );
parse_str( wp_parse_url( $url, PHP_URL_QUERY ), $query );
$w = isset( $query['w'] ) ? absint( $query['w'] ) : false;
$h = isset( $query['h'] ) ? absint( $query['h'] ) : false;

Expand Down