wppaste
WordPress

wp_validate_site_data( WP_Error $errors, array $data, WP_Site|null $old_site = null )

Since
5.1.0
Source
wp-includes/ms-site.php:575
Validates data for a site prior to inserting or updating in the database.

Compatibility

WordPress
since 5.1.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

$errorsWP_Error
Error object, passed by reference. Will contain validation errors if any occurred.
$dataarray
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

Performance profile

How much work a call to wp_validate_site_data() 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
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

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

Instructions
14–103

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • hookthird-party callbacksapply_filters()one call below wp_validate_site_data()

Further down the call graph this can also reach query, option, cache, serialize 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 · 27 distinct outcomes

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

WhenInstructionsCalls it makes
!empty($data)14–28none
!empty($data)21–39__(), ->add()
!empty($data) && empty($errors) && !domain_exists()27–40domain_exists()
always28–46__(), ->add(), __(), ->add()
!empty($data) && empty($errors) && domain_exists()34–47domain_exists(), __(), ->add()
!empty($data) && empty($errors) && !domain_exists()34–51__(), ->add(), domain_exists()
empty($data)35–53__(), ->add(), __(), ->add(), __(), ->add()
!empty($data) && empty($errors) && domain_exists()41–58__(), ->add(), domain_exists(), __(), ->add()
empty($errors) && !domain_exists()41–58__(), ->add(), __(), ->add(), domain_exists()
empty($data) && empty($data[$date_field])47–60__(), ->add(), __(), ->add(), __(), ->add(), __(), ->add()
empty($errors) && domain_exists()48–65__(), ->add(), __(), ->add(), domain_exists(), __(), ->add()
empty($data) && empty($errors) && !domain_exists()48–65__(), ->add(), __(), ->add(), __(), ->add(), domain_exists()
15 further outcomes, up to 103 instructions
!empty($data) && !empty($data[$date_field])50–63wp_checkdate(), __(), ->add()
empty($data) && empty($errors) && domain_exists()55–72__(), ->add(), __(), ->add(), __(), ->add(), domain_exists(), __(), ->add()
!empty($data[$date_field])57–70__(), ->add(), wp_checkdate(), __(), ->add()
empty($data) && empty($data[$date_field]) && empty($errors) && !domain_exists()60–72__(), ->add(), __(), ->add(), __(), ->add(), __(), ->add(), domain_exists()
!empty($data) && !empty($data[$date_field]) && empty($errors) && !domain_exists()63–75wp_checkdate(), __(), ->add(), domain_exists()
!empty($data[$date_field])64–77__(), ->add(), __(), ->add(), wp_checkdate(), __(), ->add()
empty($data) && empty($data[$date_field]) && empty($errors) && domain_exists()67–79__(), ->add(), __(), ->add(), __(), ->add(), __(), ->add(), domain_exists(), __(), ->add()
!empty($data) && !empty($data[$date_field]) && empty($errors) && domain_exists()70–82wp_checkdate(), __(), ->add(), domain_exists(), __(), ->add()
!empty($data[$date_field]) && empty($errors) && !domain_exists()70–82__(), ->add(), wp_checkdate(), __(), ->add(), domain_exists()
empty($data) && !empty($data[$date_field])71–84__(), ->add(), __(), ->add(), __(), ->add(), wp_checkdate(), __(), ->add()
!empty($data[$date_field]) && empty($errors) && domain_exists()77–89__(), ->add(), wp_checkdate(), __(), ->add(), domain_exists(), __(), ->add()
!empty($data[$date_field]) && empty($errors) && !domain_exists()77–89__(), ->add(), __(), ->add(), wp_checkdate(), __(), ->add(), domain_exists()
!empty($data[$date_field]) && empty($errors) && domain_exists()84–96__(), ->add(), __(), ->add(), wp_checkdate(), __(), ->add(), domain_exists(), __(), ->add()
empty($data) && !empty($data[$date_field]) && empty($errors) && !domain_exists()84–96__(), ->add(), __(), ->add(), __(), ->add(), wp_checkdate(), __(), ->add(), domain_exists()
empty($data) && !empty($data[$date_field]) && empty($errors) && domain_exists()91–103__(), ->add(), __(), ->add(), __(), ->add(), wp_checkdate(), __(), ->add(), domain_exists(), __(), ->add()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev11314–10314
8.511314–10314
8.411314–103149 fewer instructions than PHP 8.3
8.312214–11214
8.212214–11214
8.112214–11214
7.412214–11214

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.

Uses · 3

  • __()Retrieves the translation of $text.
  • wp_checkdate()Tests if the supplied date is valid for the Gregorian calendar.
  • domain_exists()Checks whether a site name is already taken.

Source code

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

Introduced in 5.1.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.8.8 tag, from src/wp-includes/ms-site.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.