wppaste
WordPress

WP_Duotone::get_filter_svg( string $filter_id, array $colors ): string

Since
6.3.0
Source
wp-includes/class-wp-duotone.php:668
Gets the SVG for the duotone filter definition.

Description

Whitespace is removed when SCRIPT_DEBUG is not enabled.

Compatibility

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

$filter_idstring
The ID of the filter.
$colorsarray
An array of color strings.

Return value

string
An SVG with a duotone filter definition.

Performance profile

How much work a call to WP_Duotone::get_filter_svg() 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
47–56

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
2

2 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksdo_action()one call below WP_Duotone::get_filter_svg()

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

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

WhenInstructionsCalls it makes
always47–56ob_start(), esc_attr(), esc_attr(), esc_attr(), esc_attr(), esc_attr(), ob_get_clean()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev9847–564
8.59847–564
8.49847–56420 fewer instructions than PHP 8.3
8.311859–764
8.211859–764
8.111859–764
7.411859–764

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 · 4

Used by · 2

Source code

	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;	}

Changelog

Introduced in 6.3.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 7.1.0 tag, from src/wp-includes/class-wp-duotone.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.