函数文档

ms_allowed_http_request_hosts()

💡 云策文档标注

概述

ms_allowed_http_request_hosts() 是一个 WordPress 多站点安装中的函数,用于安全地将域名添加到允许的 HTTP 请求主机列表中。它通过 'http_request_host_is_external' 过滤器实现,检查主机是否为多站点网络的一部分。

关键要点

  • 函数附加到 'http_request_host_is_external' 过滤器,用于控制外部主机请求的允许状态。
  • 接受两个参数:$is_external(布尔值,必需)和 $host(字符串,必需),返回布尔值表示主机是否被允许。
  • 通过全局 $wpdb 查询数据库,检查 $host 是否存在于 $wpdb->blogs 表中,以确定其是否为多站点网络中的域名。
  • 使用静态变量 $queried 缓存查询结果,提高性能。
  • 如果 $is_external 为 true,则直接返回 true;如果 $host 与主网络域名匹配,也返回 true。

代码示例

function ms_allowed_http_request_hosts( $is_external, $host ) {
    global $wpdb;
    static $queried = array();
    if ( $is_external ) {
        return $is_external;
    }
    if ( get_network()->domain === $host ) {
        return true;
    }
    if ( isset( $queried[ $host ] ) ) {
        return $queried[ $host ];
    }
    $queried[ $host ] = (bool) $wpdb->get_var( $wpdb->prepare( "SELECT domain FROM $wpdb->blogs WHERE domain = %s LIMIT 1", $host ) );
    return $queried[ $host ];
}

📄 原文内容

Adds any domain in a multisite installation for safe HTTP requests to the allowed list.

Description

Attached to the ‘http_request_host_is_external’ filter.

Parameters

$is_externalboolrequired
$hoststringrequired

Return

bool

Source

function ms_allowed_http_request_hosts( $is_external, $host ) {
	global $wpdb;
	static $queried = array();
	if ( $is_external ) {
		return $is_external;
	}
	if ( get_network()->domain === $host ) {
		return true;
	}
	if ( isset( $queried[ $host ] ) ) {
		return $queried[ $host ];
	}
	$queried[ $host ] = (bool) $wpdb->get_var( $wpdb->prepare( "SELECT domain FROM $wpdb->blogs WHERE domain = %s LIMIT 1", $host ) );
	return $queried[ $host ];
}

Changelog

Version Description
3.6.0 Introduced.