WP_Theme_JSON::update_button_width_declarations( array $feature_declarations, array $settings ): array
- Since
- 7.1.0
- Source
wp-includes/class-wp-theme-json.php:3434
Description
When a percentage width is set on the Button block via Global Styles, the resulting CSS needs to account for block gap spacing so that buttons tile correctly on a row (e.g. 4 buttons at 25% width all fit on one row).
This mirrors the dynamic calc() formula applied at the block instance level in the button block's stylesheet (style.scss).
Compatibility
- WordPress
- since 7.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 1 of the 5 tracked releases, added in 7.1.0, and compiles on PHP 7.4 through 8.6-dev.
Parameters
$feature_declarationsarray- The feature declarations keyed by selector.
$settingsarray- The theme.json settings.
Return value
array- The updated feature declarations.
Performance profile
How much work a call to WP_Theme_JSON::update_button_width_declarations() 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
- 5–10
- Plugin surface
- None
- Called by
- 0
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 87.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
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_Theme_JSON::update_button_width_declarations() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 5–10 | none |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 87 | 5–10 | 23 | |
| 8.5 | 87 | 5–10 | 23 | |
| 8.4 | 87 | 5–10 | 23 | 5 fewer instructions than PHP 8.3 |
| 8.3 | 92 | 5–10 | 23 | |
| 8.2 | 92 | 5–10 | 23 | |
| 8.1 | 92 | 5–10 | 23 | |
| 7.4 | 92 | 5–10 | 23 |
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 · 2
- str_ends_with()Polyfill for `str_ends_with()` function added in PHP 8.0.
- str_starts_with()Polyfill for `str_starts_with()` function added in PHP 8.0.
Source code
private static function update_button_width_declarations( $feature_declarations, $settings ) { if ( ! isset( $feature_declarations['.wp-block-button'] ) ) { return $feature_declarations; } foreach ( $feature_declarations['.wp-block-button'] as &$declaration ) { if ( 'width' !== $declaration['name'] || ! isset( $declaration['value'] ) ) { continue; } $value = $declaration['value']; $percentage = null; // Case 1: Direct percentage value e.g. "25%". if ( is_string( $value ) && str_ends_with( $value, '%' ) ) { $percentage = (float) $value; } // Case 2: Preset CSS var e.g. "var(--wp--preset--dimension--50)". if ( null === $percentage && is_string( $value ) && str_starts_with( $value, 'var(--wp--preset--dimension--' ) ) { // Extract the slug from the var name. $slug = substr( $value, strlen( 'var(--wp--preset--dimension--' ), -1 ); /* * Look up the preset size across all origins. * Check block-level settings first (core/button), then top-level settings. */ $dimension_sizes = ( $settings['blocks']['core/button']['dimensions']['dimensionSizes'] ?? array() ) + ( $settings['dimensions']['dimensionSizes'] ?? array() ); foreach ( $dimension_sizes as $origin_sizes ) { if ( ! is_array( $origin_sizes ) ) { continue; } foreach ( $origin_sizes as $preset ) { if ( isset( $preset['slug'] ) && $slug === $preset['slug'] && isset( $preset['size'] ) ) { $size = $preset['size']; if ( is_string( $size ) && str_ends_with( $size, '%' ) ) { $percentage = (float) $size; } break 2; } } } } if ( null === $percentage ) { continue; } /* * Apply the same calc() formula as the block instance level (style.scss). * The numeric percentage value is used as a unitless number: * - Multiplied by 1% to get the percentage width. * - Divided by 100 to calculate the gap adjustment proportion. */ $declaration['value'] = sprintf( 'calc(%s * 1%% - (var(--wp--style--block-gap, 0.5em) * (1 - %s / 100)))', $percentage, $percentage ); } unset( $declaration ); return $feature_declarations; }Changelog
Introduced in 7.1.0.
Signature, return type and hooks compared across 1 parsed release.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/class-wp-theme-json.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.