wp_validate_site_data

函式
wp_validate_site_data ( $errors, $data, $old_site = null )
引數
  • (WP_Error) $errors Error object, passed by reference. Will contain validation errors if any occurred.
    Required:
  • (array) $data Associative array of complete site data. See {@see} for the included data.
    Required:
  • (WP_Site|null) $old_site The old site object if the data belongs to a site being updated, or null if it is a new site being inserted.
    Required:
    Default: null
定義位置
相關方法
wp_normalize_site_datawp_validate_redirectwp_prepare_site_datawp_validate_boolean_wp_translate_postdata
引入
5.1.0
棄用
-

wp_validate_site_data是一個WordPress的函式,用於驗證一個新網站的資料: 這個函式與WordPress的多站點功能一起使用,在新站點建立之前驗證它的資料: 該函式將新站點的資料作為其輸入,並返回驗證的資料。

在資料庫中插入或更新之前,驗證一個站點的資料。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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!' ) );
}
}
}
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!' ) ); } } }
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!' ) );
		}
	}
}

常見問題

FAQs
檢視更多 >