: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
// Stop here if it's JSON (that's all we need).
if ( 'json' === $linktypes[ $atts['type'] ] ) {
// JSON is preferred to XML.
if ( ! empty( $providers['json'] ) ) {
return $providers['json'];
} elseif ( ! empty( $providers['xml'] ) ) {
return $providers['xml'];
* Connects to an oEmbed provider and returns the result.
* @param string $provider The URL to the oEmbed provider.
* @param string $url The URL to the content that is desired to be embedded.
* @param string|array $args Optional. Additional arguments for retrieving embed HTML.
* See wp_oembed_get() for accepted arguments. Default empty.
* @return object|false The result in the form of an object on success, false on failure.
public function fetch( $provider, $url, $args = '' ) {
$args = wp_parse_args( $args, wp_embed_defaults( $url ) );
$provider = add_query_arg( 'maxwidth', (int) $args['width'], $provider );
$provider = add_query_arg( 'maxheight', (int) $args['height'], $provider );
$provider = add_query_arg( 'url', urlencode( $url ), $provider );
$provider = add_query_arg( 'dnt', 1, $provider );
* Filters the oEmbed URL to be fetched.
* @since 4.9.0 The `dnt` (Do Not Track) query parameter was added to all oEmbed provider URLs.
* @param string $provider URL of the oEmbed provider.
* @param string $url URL of the content to be embedded.
* @param array $args Optional. Additional arguments for retrieving embed HTML.
* See wp_oembed_get() for accepted arguments. Default empty.
$provider = apply_filters( 'oembed_fetch_url', $provider, $url, $args );
foreach ( array( 'json', 'xml' ) as $format ) {
$result = $this->_fetch_with_format( $provider, $format );
if ( is_wp_error( $result ) && 'not-implemented' === $result->get_error_code() ) {
return ( $result && ! is_wp_error( $result ) ) ? $result : false;
* Fetches result from an oEmbed provider for a specific format and complete provider URL
* @param string $provider_url_with_args URL to the provider with full arguments list (url, maxheight, etc.)
* @param string $format Format to use.
* @return object|false|WP_Error The result in the form of an object on success, false on failure.
private function _fetch_with_format( $provider_url_with_args, $format ) {
$provider_url_with_args = add_query_arg( 'format', $format, $provider_url_with_args );
/** This filter is documented in wp-includes/class-wp-oembed.php */
$args = apply_filters( 'oembed_remote_get_args', array(), $provider_url_with_args );
$response = wp_safe_remote_get( $provider_url_with_args, $args );
if ( 501 === wp_remote_retrieve_response_code( $response ) ) {
return new WP_Error( 'not-implemented' );
$body = wp_remote_retrieve_body( $response );
$parse_method = "_parse_$format";
return $this->$parse_method( $body );
* Parses a json response body.
* @param string $response_body
private function _parse_json( $response_body ) {
$data = json_decode( trim( $response_body ) );
return ( $data && is_object( $data ) ) ? $data : false;
* Parses an XML response body.
* @param string $response_body
private function _parse_xml( $response_body ) {
if ( ! function_exists( 'libxml_disable_entity_loader' ) ) {
if ( PHP_VERSION_ID < 80000 ) {
* This function has been deprecated in PHP 8.0 because in libxml 2.9.0, external entity loading
* is disabled by default, so this function is no longer needed to protect against XXE attacks.
$loader = libxml_disable_entity_loader( true );
$errors = libxml_use_internal_errors( true );
$return = $this->_parse_xml_body( $response_body );
libxml_use_internal_errors( $errors );
if ( PHP_VERSION_ID < 80000 && isset( $loader ) ) {
// phpcs:ignore PHPCompatibility.FunctionUse.RemovedFunctions.libxml_disable_entity_loaderDeprecated
libxml_disable_entity_loader( $loader );
* Serves as a helper function for parsing an XML response body.
* @param string $response_body
private function _parse_xml_body( $response_body ) {
if ( ! function_exists( 'simplexml_import_dom' ) || ! class_exists( 'DOMDocument', false ) ) {
$dom = new DOMDocument();
$success = $dom->loadXML( $response_body );
if ( isset( $dom->doctype ) ) {
foreach ( $dom->childNodes as $child ) {
if ( XML_DOCUMENT_TYPE_NODE === $child->nodeType ) {
$xml = simplexml_import_dom( $dom );
$return = new stdClass();
foreach ( $xml as $key => $value ) {
$return->$key = (string) $value;
* Converts a data object from WP_oEmbed::fetch() and returns the HTML.
* @param object $data A data object result from an oEmbed provider.
* @param string $url The URL to the content that is desired to be embedded.
* @return string|false The HTML needed to embed on success, false on failure.
public function data2html( $data, $url ) {
if ( ! is_object( $data ) || empty( $data->type ) ) {
if ( empty( $data->url ) || empty( $data->width ) || empty( $data->height ) ) {
if ( ! is_string( $data->url ) || ! is_numeric( $data->width ) || ! is_numeric( $data->height ) ) {
$title = ! empty( $data->title ) && is_string( $data->title ) ? $data->title : '';
$return = '<a href="' . esc_url( $url ) . '"><img src="' . esc_url( $data->url ) . '" alt="' . esc_attr( $title ) . '" width="' . esc_attr( $data->width ) . '" height="' . esc_attr( $data->height ) . '" /></a>';
if ( ! empty( $data->html ) && is_string( $data->html ) ) {
if ( ! empty( $data->title ) && is_string( $data->title ) ) {
$return = '<a href="' . esc_url( $url ) . '">' . esc_html( $data->title ) . '</a>';
* Filters the returned oEmbed HTML.
* Use this filter to add support for custom data types, or to filter the result.
* @param string $return The returned oEmbed HTML.
* @param object $data A data object result from an oEmbed provider.
* @param string $url The URL of the content to be embedded.
return apply_filters( 'oembed_dataparse', $return, $data, $url );
* Strips any new lines from the HTML.
* @since 2.9.0 as strip_scribd_newlines()
* @param string $html Existing HTML.
* @param object $data Data object from WP_oEmbed::data2html()
* @param string $url The original URL passed to oEmbed.
* @return string Possibly modified $html
public function _strip_newlines( $html, $data, $url ) {
if ( ! str_contains( $html, "\n" ) ) {
$search = array( "\t", "\n", "\r", ' ' );
$replace = array( '__TAB__', '__NL__', '__CR__', '__SPACE__' );
$tokenized = str_replace( $search, $replace, $html );
preg_match_all( '#(<pre[^>]*>.+?</pre>)#i', $tokenized, $matches, PREG_SET_ORDER );
foreach ( $matches as $i => $match ) {
$tag_html = str_replace( $replace, $search, $match[0] );
$tag_token = $token . $i;
$found[ $tag_token ] = $tag_html;
$html = str_replace( $tag_html, $tag_token, $html, $count );
$replaced = str_replace( $replace, $search, $html );
$stripped = str_replace( array( "\r\n", "\n" ), '', $replaced );
$pre = array_values( $found );
$tokens = array_keys( $found );
return str_replace( $tokens, $pre, $stripped );