wppaste
WordPress

WP_Theme_JSON::compute_spacing_sizes( array $spacing_scale ): array

Since
6.6.0
Source
wp-includes/class-wp-theme-json.php:5458
Generates a set of spacing sizes by starting with a medium size and applying an operator with an increment value to generate the rest of the sizes outward from the medium size. The medium slug is '50' with the rest of the slugs being 10 apart. The generated names use t-shirt sizing.

Description

Example:

$spacing_scale = array(
 'steps' => 4,
 'mediumStep' => 16,
 'unit' => 'px',
 'operator' => '+',
 'increment' => 2,
);
$spacing_sizes = static::compute_spacing_sizes( $spacing_scale );
// -> array(
// array( 'name' => 'Small', 'slug' => '40', 'size' => '14px' ),
// array( 'name' => 'Medium', 'slug' => '50', 'size' => '16px' ),
// array( 'name' => 'Large', 'slug' => '60', 'size' => '18px' ),
// array( 'name' => 'X-Large', 'slug' => '70', 'size' => '20px' ),
// )

Compatibility

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

$spacing_scalearray
The spacing scale values. All are required.
  • $stepsint

    The number of steps in the scale. (up to 10 steps are supported.) @type float $mediumStep The middle value that gets the slug '50'. (For even number of steps, this becomes the first middle value.) @type string $unit The CSS unit to use for the sizes.
  • $operatorstring

    The mathematical operator to apply to generate the other sizes. Either '+' or '*'.
  • $incrementfloat

    The value used with the operator to generate the other sizes.

Return value

array
The spacing sizes presets or an empty array if some spacing scale values are missing or invalid.

Performance profile

How much work a call to WP_Theme_JSON::compute_spacing_sizes() 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
4–103

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • hookthird-party callbacksapply_filters()one call below WP_Theme_JSON::compute_spacing_sizes()

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 · 3 distinct outcomes

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

WhenInstructionsCalls it makes
always4–30none
isset($spacing_scale) && !$above_midpoint_count78–99round(), array_reverse(), __()
isset($spacing_scale) && !$above_midpoint_count82–103sanitize_title(), round(), array_reverse(), __()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev1984–102295 fewer instructions than PHP 8.5
8.52034–10329
8.42034–103296 fewer instructions than PHP 8.3
8.32094–10929
8.22094–10929
8.12094–109293 fewer instructions than PHP 7.4
7.42124–10929

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

  • sanitize_title()Sanitizes a string into a slug, which can be used in URLs or HTML attributes.
  • __()Retrieves the translation of $text.

Source code

	private static function compute_spacing_sizes( $spacing_scale ) {		/*		 * This condition is intentionally missing some checks on ranges for the values in order to		 * keep backwards compatibility with the previous implementation.		 */		if (			! isset( $spacing_scale['steps'] ) ||			! is_numeric( $spacing_scale['steps'] ) ||			0 === $spacing_scale['steps'] ||			! isset( $spacing_scale['mediumStep'] ) ||			! is_numeric( $spacing_scale['mediumStep'] ) ||			! isset( $spacing_scale['unit'] ) ||			! isset( $spacing_scale['operator'] ) ||			( '+' !== $spacing_scale['operator'] && '*' !== $spacing_scale['operator'] ) ||			! isset( $spacing_scale['increment'] ) ||			! is_numeric( $spacing_scale['increment'] )		) {			return array();		} 		$unit            = '%' === $spacing_scale['unit'] ? '%' : sanitize_title( $spacing_scale['unit'] );		$current_step    = $spacing_scale['mediumStep'];		$steps_mid_point = round( $spacing_scale['steps'] / 2, 0 );		$x_small_count   = null;		$below_sizes     = array();		$slug            = 40;		$remainder       = 0; 		for ( $below_midpoint_count = $steps_mid_point - 1; $spacing_scale['steps'] > 1 && $slug > 0 && $below_midpoint_count > 0; $below_midpoint_count-- ) {			if ( '+' === $spacing_scale['operator'] ) {				$current_step -= $spacing_scale['increment'];			} elseif ( $spacing_scale['increment'] > 1 ) {				$current_step /= $spacing_scale['increment'];			} else {				$current_step *= $spacing_scale['increment'];			} 			if ( $current_step <= 0 ) {				$remainder = $below_midpoint_count;				break;			} 			$below_sizes[] = array(				/* translators: %s: Digit to indicate multiple of sizing, eg. 2X-Small. */				'name' => $below_midpoint_count === $steps_mid_point - 1 ? __( 'Small' ) : sprintf( __( '%sX-Small' ), (string) $x_small_count ),				'slug' => (string) $slug,				'size' => round( $current_step, 2 ) . $unit,			); 			if ( $below_midpoint_count === $steps_mid_point - 2 ) {				$x_small_count = 2;			} 			if ( $below_midpoint_count < $steps_mid_point - 2 ) {				++$x_small_count;			} 			$slug -= 10;		} 		$below_sizes = array_reverse( $below_sizes ); 		$below_sizes[] = array(			'name' => __( 'Medium' ),			'slug' => '50',			'size' => $spacing_scale['mediumStep'] . $unit,		); 		$current_step  = $spacing_scale['mediumStep'];		$x_large_count = null;		$above_sizes   = array();		$slug          = 60;		$steps_above   = ( $spacing_scale['steps'] - $steps_mid_point ) + $remainder; 		for ( $above_midpoint_count = 0; $above_midpoint_count < $steps_above; $above_midpoint_count++ ) {			$current_step = '+' === $spacing_scale['operator']				? $current_step + $spacing_scale['increment']				: ( $spacing_scale['increment'] >= 1 ? $current_step * $spacing_scale['increment'] : $current_step / $spacing_scale['increment'] ); 			$above_sizes[] = array(

Changelog

Introduced in 6.6.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-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.