wppaste
WordPress

WP_Http::block_request( string $uri ): bool

Since
2.8.0
Source
wp-includes/class-wp-http.php:894
Determines whether an HTTP API request to the given URL should be blocked.

Description

Those who are behind a proxy and want to prevent access to certain hosts may do so. This will prevent plugins from working and core functionality, if you don't include api.wordpress.org.

You block external URL requests by defining WP_HTTP_BLOCK_EXTERNAL as true in your wp-config.php file and this will only allow localhost and your site to make requests. The constant WP_ACCESSIBLE_HOSTS will allow additional hosts to go through for requests. The format of the WP_ACCESSIBLE_HOSTS constant is a comma separated list of hostnames to allow, wildcard domains are supported, eg *.wordpress.org will allow for all subdomains of wordpress.org to be contacted.

Compatibility

WordPress
since 2.8.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$uristring
URI of url.

Return value

bool
True to block, false to allow.

Performance profile

How much work a call to WP_Http::block_request() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reads stored settings via get_option(), cached per request but not free on a cold cache.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
4–56

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 77.

Plugin surface
1 hook

Third-party callbacks on 'block_local_requests' run inside this call, and their cost is not bounded by anything here.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • optionoption read or writeget_option()called directly
  • hookthird-party callbacksapply_filters()called directly
  • regexregular expression over the whole inputpreg_split()called directly
  • cacheobject cachewp_cache_get()one call below WP_Http::block_request()
  • serializeserialisationmaybe_unserialize()one call below WP_Http::block_request()

Further down the call graph this can also reach query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 5 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Http::block_request() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always4–6none
defined('WP_HTTP_BLOCK_EXTERNAL')11parse_url()
defined('WP_HTTP_BLOCK_EXTERNAL')25–39parse_url(), get_option(), parse_url()
defined('WP_HTTP_BLOCK_EXTERNAL')25–31parse_url(), get_option(), parse_url(), apply_filters()
defined('WP_HTTP_BLOCK_EXTERNAL') && defined('WP_ACCESSIBLE_HOSTS') && $accessible_hosts === null43–56parse_url(), get_option(), parse_url(), preg_split()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev774–5612
8.5774–5612
8.4774–561215 fewer instructions than PHP 8.3
8.3924–6512
8.2924–6512
8.1924–6512
7.4924–6512

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 1

One hook fires while WP_Http::block_request() runs, in this order:

  1. apply_filters( block_local_requests )filterline 918 (+24 into the body)

    Filters whether to block local HTTP API requests.

Uses · 3

  • get_option()Retrieves an option value based on an option name.
  • apply_filters()Calls the callback functions that have been added to a filter hook.
  • str_contains()Polyfill for `str_contains()` function added in PHP 8.0.

Used by · 1

Source code

	public function block_request( $uri ) {		// We don't need to block requests, because nothing is blocked.		if ( ! defined( 'WP_HTTP_BLOCK_EXTERNAL' ) || ! WP_HTTP_BLOCK_EXTERNAL ) {			return false;		} 		$check = parse_url( $uri );		if ( ! $check ) {			return true;		} 		$home = parse_url( get_option( 'siteurl' ) ); 		// Don't block requests back to ourselves by default.		if ( 'localhost' === $check['host'] || ( isset( $home['host'] ) && $home['host'] === $check['host'] ) ) {			/**			 * Filters whether to block local HTTP API requests.			 *			 * A local request is one to `localhost` or to the same host as the site itself.			 *			 * @since 2.8.0			 *			 * @param bool $block Whether to block local requests. Default false.			 */			return apply_filters( 'block_local_requests', false );		} 		if ( ! defined( 'WP_ACCESSIBLE_HOSTS' ) ) {			return true;		} 		static $accessible_hosts = null;		static $wildcard_regex   = array();		if ( null === $accessible_hosts ) {			$accessible_hosts = preg_split( '|,\s*|', WP_ACCESSIBLE_HOSTS ); 			if ( str_contains( WP_ACCESSIBLE_HOSTS, '*' ) ) {				$wildcard_regex = array();				foreach ( $accessible_hosts as $host ) {					$wildcard_regex[] = str_replace( '\*', '.+', preg_quote( $host, '/' ) );				}				$wildcard_regex = '/^(' . implode( '|', $wildcard_regex ) . ')$/i';			}		} 		if ( ! empty( $wildcard_regex ) ) {			return ! preg_match( $wildcard_regex, $check['host'] );		} else {			return ! in_array( $check['host'], $accessible_hosts, true ); // Inverse logic, if it's in the array, then don't block it.		}	}

Changelog

Introduced in 2.8.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.9.7 tag, from src/wp-includes/class-wp-http.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.