函数文档

wp_validate_site_data()

💡 云策文档标注

概述

wp_validate_site_data() 函数用于在将站点数据插入或更新到数据库前进行验证。它检查必需字段是否为空、日期是否有效,并确保站点域、路径和网络ID的唯一性。

关键要点

  • 验证站点数据,包括域名、路径、网络ID和日期字段(注册日期和最后更新日期)。
  • 使用 WP_Error 对象收集验证错误,通过引用传递。
  • 对于新站点或更改的域/路径/网络ID,调用 domain_exists() 检查唯一性。
  • 支持可选参数 $old_site,用于更新现有站点时的比较。

代码示例

function wp_validate_site_data( $errors, $data, $old_site = null ) {
    // A domain must always be present.
    if ( empty( $data['domain'] ) ) {
        $errors->add( 'site_empty_domain', __( 'Site domain must not be empty.' ) );
    }

    // A path must always be present.
    if ( empty( $data['path'] ) ) {
        $errors->add( 'site_empty_path', __( 'Site path must not be empty.' ) );
    }

    // A network ID must always be present.
    if ( empty( $data['network_id'] ) ) {
        $errors->add( 'site_empty_network_id', __( 'Site network ID must be provided.' ) );
    }

    // Both registration and last updated dates must always be present and valid.
    $date_fields = array( 'registered', 'last_updated' );
    foreach ( $date_fields as $date_field ) {
        if ( empty( $data[ $date_field ] ) ) {
            $errors->add( 'site_empty_' . $date_field, __( 'Both registration and last updated dates must be provided.' ) );
            break;
        }

        // Allow '0000-00-00 00:00:00', although it be stripped out at this point.
        if ( '0000-00-00 00:00:00' !== $data[ $date_field ] ) {
            $month      = substr( $data[ $date_field ], 5, 2 );
            $day        = substr( $data[ $date_field ], 8, 2 );
            $year       = substr( $data[ $date_field ], 0, 4 );
            $valid_date = wp_checkdate( $month, $day, $year, $data[ $date_field ] );
            if ( ! $valid_date ) {
                $errors->add( 'site_invalid_' . $date_field, __( 'Both registration and last updated dates must be valid dates.' ) );
                break;
            }
        }
    }

    if ( ! empty( $errors->errors ) ) {
        return;
    }

    // If a new site, or domain/path/network ID have changed, ensure uniqueness.
    if ( ! $old_site
        || $data['domain'] !== $old_site->domain
        || $data['path'] !== $old_site->path
        || $data['network_id'] !== $old_site->network_id
    ) {
        if ( domain_exists( $data['domain'], $data['path'], $data['network_id'] ) ) {
            $errors->add( 'site_taken', __( 'Sorry, that site already exists!' ) );
        }
    }
}

注意事项

  • 验证失败时,错误信息通过 WP_Error 对象返回,不会抛出异常。
  • 日期字段允许 '0000-00-00 00:00:00' 作为特殊值,但通常会被剥离。
  • 函数在 WordPress 5.1.0 版本中引入。

📄 原文内容

Validates data for a site prior to inserting or updating in the database.

Parameters

$errorsWP_Errorrequired
Error object, passed by reference. Will contain validation errors if any occurred.
$dataarrayrequired
Associative array of complete site data. See wp_insert_site() for the included data.
$old_siteWP_Site|nulloptional
The old site object if the data belongs to a site being updated, or null if it is a new site being inserted.

Default:null

Source

function wp_validate_site_data( $errors, $data, $old_site = null ) {
	// A domain must always be present.
	if ( empty( $data['domain'] ) ) {
		$errors->add( 'site_empty_domain', __( 'Site domain must not be empty.' ) );
	}

	// A path must always be present.
	if ( empty( $data['path'] ) ) {
		$errors->add( 'site_empty_path', __( 'Site path must not be empty.' ) );
	}

	// A network ID must always be present.
	if ( empty( $data['network_id'] ) ) {
		$errors->add( 'site_empty_network_id', __( 'Site network ID must be provided.' ) );
	}

	// Both registration and last updated dates must always be present and valid.
	$date_fields = array( 'registered', 'last_updated' );
	foreach ( $date_fields as $date_field ) {
		if ( empty( $data[ $date_field ] ) ) {
			$errors->add( 'site_empty_' . $date_field, __( 'Both registration and last updated dates must be provided.' ) );
			break;
		}

		// Allow '0000-00-00 00:00:00', although it be stripped out at this point.
		if ( '0000-00-00 00:00:00' !== $data[ $date_field ] ) {
			$month      = substr( $data[ $date_field ], 5, 2 );
			$day        = substr( $data[ $date_field ], 8, 2 );
			$year       = substr( $data[ $date_field ], 0, 4 );
			$valid_date = wp_checkdate( $month, $day, $year, $data[ $date_field ] );
			if ( ! $valid_date ) {
				$errors->add( 'site_invalid_' . $date_field, __( 'Both registration and last updated dates must be valid dates.' ) );
				break;
			}
		}
	}

	if ( ! empty( $errors->errors ) ) {
		return;
	}

	// If a new site, or domain/path/network ID have changed, ensure uniqueness.
	if ( ! $old_site
		|| $data['domain'] !== $old_site->domain
		|| $data['path'] !== $old_site->path
		|| $data['network_id'] !== $old_site->network_id
	) {
		if ( domain_exists( $data['domain'], $data['path'], $data['network_id'] ) ) {
			$errors->add( 'site_taken', __( 'Sorry, that site already exists!' ) );
		}
	}
}

Changelog

Version Description
5.1.0 Introduced.