wp_normalize_site_data

函数
wp_normalize_site_data ( $data )
参数
  • (array) $data Associative array of site data passed to the respective function. See {@see} for the possibly included data.
    Required:
返回值
  • (array) Normalized site data.
定义位置
相关方法
wp_validate_site_datawp_normalize_pathwp_prepare_site_datawp_initialize_sitenormalize_whitespace
引入
5.1.0
弃用
-

wp_normalize_site_data: 这个函数对网站数据进行规范化处理,以确保其一致和安全使用。它对网站标题、标语和其他设置进行净化,以删除任何潜在的有害代码或字符。

在数据库中插入或更新之前,对一个站点的数据进行规范化处理。

function wp_normalize_site_data( $data ) {
	// Sanitize domain if passed.
	if ( array_key_exists( 'domain', $data ) ) {
		$data['domain'] = trim( $data['domain'] );
		$data['domain'] = preg_replace( '/s+/', '', sanitize_user( $data['domain'], true ) );
		if ( is_subdomain_install() ) {
			$data['domain'] = str_replace( '@', '', $data['domain'] );
		}
	}

	// Sanitize path if passed.
	if ( array_key_exists( 'path', $data ) ) {
		$data['path'] = trailingslashit( '/' . trim( $data['path'], '/' ) );
	}

	// Sanitize network ID if passed.
	if ( array_key_exists( 'network_id', $data ) ) {
		$data['network_id'] = (int) $data['network_id'];
	}

	// Sanitize status fields if passed.
	$status_fields = array( 'public', 'archived', 'mature', 'spam', 'deleted' );
	foreach ( $status_fields as $status_field ) {
		if ( array_key_exists( $status_field, $data ) ) {
			$data[ $status_field ] = (int) $data[ $status_field ];
		}
	}

	// Strip date fields if empty.
	$date_fields = array( 'registered', 'last_updated' );
	foreach ( $date_fields as $date_field ) {
		if ( ! array_key_exists( $date_field, $data ) ) {
			continue;
		}

		if ( empty( $data[ $date_field ] ) || '0000-00-00 00:00:00' === $data[ $date_field ] ) {
			unset( $data[ $date_field ] );
		}
	}

	return $data;
}

常见问题

FAQs
查看更多 >