WP_Duotone::get_filter_svg()private staticWP 6.3.0

Gets the SVG for the duotone filter definition.

Whitespace is removed when SCRIPT_DEBUG is not enabled.

Method of the class: WP_Duotone{}

No Hooks.

Return

String. An SVG with a duotone filter definition.

Usage

$result = WP_Duotone::get_filter_svg( $filter_id, $colors );
$filter_id(string) (required)
The ID of the filter.
$colors(array) (required)
An array of color strings.

Changelog

Since 6.3.0 Introduced.

WP_Duotone::get_filter_svg() code WP 6.5.3

<?php
private static function get_filter_svg( $filter_id, $colors ) {
	$duotone_values = array(
		'r' => array(),
		'g' => array(),
		'b' => array(),
		'a' => array(),
	);

	foreach ( $colors as $color_str ) {
		$color = self::colord_parse( $color_str );

		if ( null === $color ) {
			$error_message = sprintf(
				/* translators: 1: Duotone colors, 2: theme.json, 3: settings.color.duotone */
				__( '"%1$s" in %2$s %3$s is not a hex or rgb string.' ),
				$color_str,
				'theme.json',
				'settings.color.duotone'
			);
			_doing_it_wrong( __METHOD__, $error_message, '6.3.0' );
		} else {
			$duotone_values['r'][] = $color['r'] / 255;
			$duotone_values['g'][] = $color['g'] / 255;
			$duotone_values['b'][] = $color['b'] / 255;
			$duotone_values['a'][] = $color['a'];
		}
	}

	ob_start();

	?>

	<svg
		xmlns="http://www.w3.org/2000/svg"
		viewBox="0 0 0 0"
		width="0"
		height="0"
		focusable="false"
		role="none"
		style="visibility: hidden; position: absolute; left: -9999px; overflow: hidden;"
	>
		<defs>
			<filter id="<?php echo esc_attr( $filter_id ); ?>">
				<feColorMatrix
					color-interpolation-filters="sRGB"
					type="matrix"
					values="
						.299 .587 .114 0 0
						.299 .587 .114 0 0
						.299 .587 .114 0 0
						.299 .587 .114 0 0
					"
				/>
				<feComponentTransfer color-interpolation-filters="sRGB" >
					<feFuncR type="table" tableValues="<?php echo esc_attr( implode( ' ', $duotone_values['r'] ) ); ?>" />
					<feFuncG type="table" tableValues="<?php echo esc_attr( implode( ' ', $duotone_values['g'] ) ); ?>" />
					<feFuncB type="table" tableValues="<?php echo esc_attr( implode( ' ', $duotone_values['b'] ) ); ?>" />
					<feFuncA type="table" tableValues="<?php echo esc_attr( implode( ' ', $duotone_values['a'] ) ); ?>" />
				</feComponentTransfer>
				<feComposite in2="SourceGraphic" operator="in" />
			</filter>
		</defs>
	</svg>

	<?php

	$svg = ob_get_clean();

	if ( ! SCRIPT_DEBUG ) {
		// Clean up the whitespace.
		$svg = preg_replace( "/[\r\n\t ]+/", ' ', $svg );
		$svg = str_replace( '> <', '><', $svg );
		$svg = trim( $svg );
	}

	return $svg;
}