Skip to content

Not enough information given when fetch_external_stylesheet failure happened #2449

@pentatonicfunk

Description

@pentatonicfunk

Version : 1.1.1

When fetching external stylesheet failure that indicated by WP_Error on wp_remote_get, there is no code and message displayed on Validations Page.

Example case : when Invalid SSL being used on external stylesheet url.

This is caused by wp_remote_retrieve_response_code and wp_remote_retrieve_response_message are empty

https://github.com/ampproject/amp-wp/blob/1.1.1/includes/sanitizers/class-amp-style-sanitizer.php#L1111

private function fetch_external_stylesheet( $url ) {
		$cache_key = md5( $url );
		$contents  = get_transient( $cache_key );
		if ( false === $contents ) {
			$r = wp_remote_get( $url );
                        // TODO: Need to handle WP_Error
			if ( 200 !== wp_remote_retrieve_response_code( $r ) ) {
				$contents = new WP_Error(
					wp_remote_retrieve_response_code( $r ),
					wp_remote_retrieve_response_message( $r )
				);
			} elseif ( ! preg_match( '#^text/css#', wp_remote_retrieve_header( $r, 'content-type' ) ) ) {
				$contents = new WP_Error(
					'no_css_content_type',
					__( 'Response did not contain the expected text/css content type.', 'amp' )
				);
			} else {
				$contents = wp_remote_retrieve_body( $r );
			}
			set_transient( $cache_key, $contents, MONTH_IN_SECONDS );
		}
		return $contents;
	}

Possible Solution : Add WP_Error handler after wp_remote_get to catch code and message, that will be usable on later stage

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions