WP_Duotone::get_filter_svg( string $filter_id, array $colors ): string
- Since
- 6.3.0
- Source
wp-includes/class-wp-duotone.php:668
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
- Scaling
- Scales with input
- Instructions
- 47–56
- Plugin surface
- None
- Called by
- 2
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 98.
Nothing here hands control to plugin code.
2 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
do_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 47–56 | ob_start(), esc_attr(), esc_attr(), esc_attr(), esc_attr(), esc_attr(), ob_get_clean() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 98 | 47–56 | 4 | |
| 8.5 | 98 | 47–56 | 4 | |
| 8.4 | 98 | 47–56 | 4 | 20 fewer instructions than PHP 8.3 |
| 8.3 | 118 | 59–76 | 4 | |
| 8.2 | 118 | 59–76 | 4 | |
| 8.1 | 118 | 59–76 | 4 | |
| 7.4 | 118 | 59–76 | 4 |
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
- __()Retrieves the translation of $text.
- _doing_it_wrong()Marks something as being incorrectly called.
- esc_attr()Escaping for HTML attributes.
- WP_Duotone::colord_parse()Tries to convert an incoming string into RGBA values.
Used by · 2
- WP_Duotone::get_filter_svg_from_preset()Gets the SVG for the duotone filter definition from a preset.
- WP_Duotone::get_svg_definitions()Get the SVGs for the duotone filters.
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.
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.