t_last()`.
* @return array|false {
* Extension details.
*
* @type string $slug The extension slug. This is the plugin or theme's directory.
* @type string $type The extension type. Either 'plugin' or 'theme'.
* }
*/
protected function get_extension_for_error( $error ) {
global $wp_theme_directories;
if ( ! isset( $error['file'] ) ) {
return false;
}
if ( ! defined( 'WP_PLUGIN_DIR' ) ) {
return false;
}
$error_file = wp_normalize_path( $error['file'] );
$wp_plugin_dir = wp_normalize_path( WP_PLUGIN_DIR );
if ( str_starts_with( $error_file, $wp_plugin_dir ) ) {
$path = str_replace( $wp_plugin_dir . '/', '', $error_file );
$parts = explode( '/', $path );
return array(
'type' => 'plugin',
'slug' => $parts[0],
);
}
if ( empty( $wp_theme_directories ) ) {
return false;
}
foreach ( $wp_theme_directories as $theme_directory ) {
$theme_directory = wp_normalize_path( $theme_directory );
if ( str_starts_with( $error_file, $theme_directory ) ) {
$path = str_replace( $theme_directory . '/', '', $error_file );
$parts = explode( '/', $path );
return array(
'type' => 'theme',
'slug' => $parts[0],
);
}
}
return false;
}
/**
* Checks whether the given extension a network activated plugin.
*
* @since 5.2.0
*
* @param array $extension Extension data.
* @return bool True if network plugin, false otherwise.
*/
protected function is_network_plugin( $extension ) {
if ( 'plugin' !== $extension['type'] ) {
return false;
}
if ( ! is_multisite() ) {
return false;
}
$network_plugins = wp_get_active_network_plugins();
foreach ( $network_plugins as $plugin ) {
if ( str_starts_with( $plugin, $extension['slug'] . '/' ) ) {
return true;
}
}
return false;
}
/**
* Stores the given error so that the extension causing it is paused.
*
* @since 5.2.0
*
* @param array $error Error details from `error_get_last()`.
* @return bool True if the error was stored successfully, false otherwise.
*/
protected function store_error( $error ) {
$extension = $this->get_extension_for_error( $error );
if ( ! $extension ) {
return false;
}
switch ( $extension['type'] ) {
case 'plugin':
return wp_paused_plugins()->set( $extension['slug'], $error );
case 'theme':
return wp_paused_themes()->set( $extension['slug'], $error );
default:
return false;
}
}
/**
* Redirects the current request to allow recovering multiple errors in one go.
*
* The redirection will only happen when on a protected endpoint.
*
* It must be ensured that this method is only called when an error actually occurred and will not occur on the
* next request again. Otherwise it will create a redirect loop.
*
* @since 5.2.0
*/
protected function redirect_protected() {
// Pluggable is usually loaded after plugins, so we manually include it here for redirection functionality.
if ( ! function_exists( 'wp_safe_redirect' ) ) {
require_once ABSPATH . WPINC . '/pluggable.php';
}
$scheme = is_ssl() ? 'https://' : 'http://';
$url = "{$scheme}{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
wp_safe_redirect( $url );
exit;
}
}
* @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() ) {
continue;
}
return ( $result && ! is_wp_error( $result ) ) ? $result : false;
}
return false;
}
/**
* Fetches result from an oEmbed provider for a specific format and complete provider URL
*
* @since 3.0.0
*
* @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 );
if ( ! $body ) {
return false;
}
$parse_method = "_parse_$format";
return $this->$parse_method( $body );
}
/**
* Parses a json response body.
*
* @since 3.0.0
*
* @param string $response_body
* @return object|false
*/
private function _parse_json( $response_body ) {
$data = json_decode( trim( $response_body ) );
return ( $data && is_object( $data ) ) ? $data : false;
}
/**
* Parses an XML response body.
*
* @since 3.0.0
*
* @param string $response_body
* @return object|false
*/
private function _parse_xml( $response_body ) {
if ( ! function_exists( 'libxml_disable_entity_loader' ) ) {
return false;
}
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 );
}
return $return;
}
/**
* Serves as a helper function for parsing an XML response body.
*
* @since 3.6.0
*
* @param string $response_body
* @return stdClass|false
*/
private function _parse_xml_body( $response_body ) {
if ( ! function_exists( 'simplexml_import_dom' ) || ! class_exists( 'DOMDocument', false ) ) {
return false;
}
$dom = new DOMDocument();
$success = $dom->loadXML( $response_body );
if ( ! $success ) {
return false;
}
if ( isset( $dom->doctype ) ) {
return false;
}
foreach ( $dom->childNodes as $child ) {
if ( XML_DOCUMENT_TYPE_NODE === $child->nodeType ) {
return false;
}
}
$xml = simplexml_import_dom( $dom );
if ( ! $xml ) {
return false;
}
$return = new stdClass();
foreach ( $xml as $key => $value ) {
$return->$key = (string) $value;
}
return $return;
}
/**
* Converts a data object from WP_oEmbed::fetch() and returns the HTML.
*
* @since 2.9.0
*
* @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 ) ) {
return false;
}
$return = false;
switch ( $data->type ) {
case 'photo':
if ( empty( $data->url ) || empty( $data->width ) || empty( $data->height ) ) {
break;
}
if ( ! is_string( $data->url ) || ! is_numeric( $data->width ) || ! is_numeric( $data->height ) ) {
break;
}
$title = ! empty( $data->title ) && is_string( $data->title ) ? $data->title : '';
$return = '';
break;
case 'video':
case 'rich':
if ( ! empty( $data->html ) && is_string( $data->html ) ) {
$return = $data->html;
}
break;
case 'link':
if ( ! empty( $data->title ) && is_string( $data->title ) ) {
$return = '' . esc_html( $data->title ) . '';
}
break;
default:
$return = false;
}
/**
* Filters the returned oEmbed HTML.
*
* Use this filter to add support for custom data types, or to filter the result.
*
* @since 2.9.0
*
* @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()
* @since 3.0.0
*
* @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" ) ) {
return $html;
}
$count = 1;
$found = array();
$token = '__PRE__';
$search = array( "\t", "\n", "\r", ' ' );
$replace = array( '__TAB__', '__NL__', '__CR__', '__SPACE__' );
$tokenized = str_replace( $search, $replace, $html );
preg_match_all( '#(
]*>.+?)#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 ); } } be created if it does not exist yet. * * @since 6.5.0 * * @return WP_Block_Bindings_Registry The main instance. */ public static function get_instance() { if ( null === self::$instance ) { self::$instance = new self(); } return self::$instance; } }