WP_Font_Face::validate_font_face_declarations( array $font_face ): array|false

In this article

This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness.

Validates each font-face declaration (property and value pairing).

Parameters

$font_facearrayrequired
Font face property and value pairings to validate.

Return

array|false Validated font-face on success, or false on failure.

Source

private function validate_font_face_declarations( array $font_face ) {
	$font_face = wp_parse_args( $font_face, $this->font_face_property_defaults );

	// Check the font-family.
	if ( empty( $font_face['font-family'] ) || ! is_string( $font_face['font-family'] ) ) {
		// @todo replace with `wp_trigger_error()`.
		_doing_it_wrong(
			__METHOD__,
			__( 'Font font-family must be a non-empty string.' ),
			'6.4.0'
		);
		return false;
	}

	// Make sure that local fonts have 'src' defined.
	if ( empty( $font_face['src'] ) || ( ! is_string( $font_face['src'] ) && ! is_array( $font_face['src'] ) ) ) {
		// @todo replace with `wp_trigger_error()`.
		_doing_it_wrong(
			__METHOD__,
			__( 'Font src must be a non-empty string or an array of strings.' ),
			'6.4.0'
		);
		return false;
	}

	// Validate the 'src' property.
	foreach ( (array) $font_face['src'] as $src ) {
		if ( empty( $src ) || ! is_string( $src ) ) {
			// @todo replace with `wp_trigger_error()`.
			_doing_it_wrong(
				__METHOD__,
				__( 'Each font src must be a non-empty string.' ),
				'6.4.0'
			);
			return false;
		}
	}

	// Check the font-weight.
	if ( ! is_string( $font_face['font-weight'] ) && ! is_int( $font_face['font-weight'] ) ) {
		// @todo replace with `wp_trigger_error()`.
		_doing_it_wrong(
			__METHOD__,
			__( 'Font font-weight must be a properly formatted string or integer.' ),
			'6.4.0'
		);
		return false;
	}

	// Check the font-display.
	if ( ! in_array( $font_face['font-display'], $this->valid_font_display, true ) ) {
		$font_face['font-display'] = $this->font_face_property_defaults['font-display'];
	}

	// Remove invalid properties.
	foreach ( $font_face as $property => $value ) {
		if ( ! in_array( $property, $this->valid_font_face_properties, true ) ) {
			unset( $font_face[ $property ] );
		}
	}

	return $font_face;
}

Changelog

VersionDescription
6.4.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.