wppaste
WordPress

_wp_prepare_json_schema_for_client_with_allowed_keywords( array $schema, array $allowed_keywords ): array<string,

Since
7.1.0
Source
wp-includes/json-schema.php:106
Prepares a JSON Schema for clients using a given keyword lookup.

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

$schemaarray
$allowed_keywordsarray

Return value

array<string,
mixed> The prepared schema.

Performance profile

How much work a call to _wp_prepare_json_schema_for_client_with_allowed_keywords() 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
22–70

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

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 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_prepare_json_schema_for_client_with_allowed_keywords() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always22–55array_intersect_key()
isset($schema) && wp_is_numeric_array()34–65array_intersect_key(), wp_is_numeric_array()
isset($schema) && !wp_is_numeric_array()40–70array_intersect_key(), wp_is_numeric_array(), _wp_prepare_json_schema_for_client_with_allowed_keywords()

This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev19022–7048
8.519022–7048
8.419022–7048
8.319022–7048
8.219022–7048
8.119022–70481 fewer instruction than PHP 7.4
7.419123–7148

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

Used by · 2

Source code

function _wp_prepare_json_schema_for_client_with_allowed_keywords( array $schema, array $allowed_keywords ): array {	if ( isset( $schema['type'] ) && 'object' === $schema['type'] && isset( $schema['default'] ) ) {		$default = $schema['default'];		if ( is_array( $default ) && empty( $default ) ) {			$schema['default'] = (object) $default;		}	} 	$schema = array_intersect_key( $schema, $allowed_keywords ); 	/*	 * Collect draft-03 per-property `required: true` flags into a draft-04	 * `required` array of property names on the parent object schema.	 *	 * This mirrors rest_validate_object_value_from_schema(), where a draft-04	 * `required` array takes precedence: when one is present, per-property	 * booleans are ignored during validation. They are therefore left out of	 * the array here as well (but still stripped from the output) so the	 * published schema describes exactly what gets enforced.	 */	if ( isset( $schema['properties'] ) && is_array( $schema['properties'] ) ) {		$has_required_array = isset( $schema['required'] ) && is_array( $schema['required'] );		$required           = array();		foreach ( $schema['properties'] as $property => &$property_schema ) {			if ( is_array( $property_schema ) && ! wp_is_numeric_array( $property_schema ) && isset( $property_schema['required'] ) && is_bool( $property_schema['required'] ) ) {				if ( ! $has_required_array && true === $property_schema['required'] ) {					$required[] = (string) $property;				}				unset( $property_schema['required'] );			}		}		unset( $property_schema ); 		/*		 * Property keys are unique, so the collected list needs no deduplication.		 * When a draft-04 array is already present, leave it untouched.		 */		if ( ! $has_required_array && count( $required ) > 0 ) {			$schema['required'] = $required;		}	} 	/*	 * A boolean `required` outside of an object's property list has no draft-04	 * equivalent, so drop it rather than emit an invalid keyword.	 */	if ( isset( $schema['required'] ) && is_bool( $schema['required'] ) ) {		unset( $schema['required'] );	} 	/*	 * Sub-schema maps: keys are user-defined, values are sub-schemas.	 * Note: 'dependencies' values can also be property-dependency arrays	 * (numeric arrays of strings) which are skipped via wp_is_numeric_array().	 */	foreach ( array( 'properties', 'patternProperties', 'definitions', 'dependencies' ) as $keyword ) {		if ( isset( $schema[ $keyword ] ) && is_array( $schema[ $keyword ] ) ) {			foreach ( $schema[ $keyword ] as $key => $child_schema ) {				if ( is_array( $child_schema ) && ! wp_is_numeric_array( $child_schema ) ) {					$schema[ $keyword ][ $key ] = _wp_prepare_json_schema_for_client_with_allowed_keywords( $child_schema, $allowed_keywords );				}			}		}	} 	// Single sub-schema keywords.	foreach ( array( 'not', 'additionalProperties', 'additionalItems' ) as $keyword ) {		if ( isset( $schema[ $keyword ] ) && is_array( $schema[ $keyword ] ) && ! wp_is_numeric_array( $schema[ $keyword ] ) ) {			$schema[ $keyword ] = _wp_prepare_json_schema_for_client_with_allowed_keywords( $schema[ $keyword ], $allowed_keywords );		}	} 	// Items: single schema or tuple array of schemas.	if ( isset( $schema['items'] ) && is_array( $schema['items'] ) ) {		if ( ! wp_is_numeric_array( $schema['items'] ) ) {			$schema['items'] = _wp_prepare_json_schema_for_client_with_allowed_keywords( $schema['items'], $allowed_keywords );		} else {			foreach ( $schema['items'] as $index => $item_schema ) {				if ( is_array( $item_schema ) && ! wp_is_numeric_array( $item_schema ) ) {					$schema['items'][ $index ] = _wp_prepare_json_schema_for_client_with_allowed_keywords( $item_schema, $allowed_keywords );

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/json-schema.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.