ACF\AI\GEO

GEO::add_types_to_nested_objectsprivate staticACF 6.8.0

Add "@type" to nested objects based on schema.org property ranges

Examines each property in the data and if it expects an object type (like Person, Organization, etc.), automatically adds the appropriate @type.

For example, if 'author' contains { 'name': 'John' }, it becomes: { '@type': 'Person', 'name': 'John' }

Method of the class: GEO{}

No Hooks.

Returns

Array. The data with @type added to nested objects.

Usage

$result = GEO::add_types_to_nested_objects( $data );
$data(array) (required)
The data array to process.

Changelog

Since 6.8.0 Introduced.

GEO::add_types_to_nested_objects() code ACF 6.8.8

private static function add_types_to_nested_objects( $data ) {
	foreach ( $data as $property => $value ) {
		// Skip if value is not an array (can't be a nested object).
		if ( ! is_array( $value ) ) {
			continue;
		}

		// Skip if already has @type.
		if ( isset( $value['@type'] ) ) {
			continue;
		}

		// Check if this is a sequential array (list) vs associative array (object).
		// Sequential arrays are for properties that accept multiple values.
		// We only add @type to associative arrays (objects).
		$is_list = array_keys( $value ) === range( 0, count( $value ) - 1 );

		if ( $is_list ) {
			// This is a list/array, not a single object. Skip adding @type.
			continue;
		}

		// Check if this property expects an object type.
		if ( Schema::property_expects_object( $property ) ) {
			// Get the preferred object type for this property.
			$object_type = Schema::get_preferred_object_type( $property );

			if ( $object_type ) {
				// Add @type at the beginning of the array.
				$data[ $property ] = array_merge(
					array( '@type' => $object_type ),
					$value
				);
			}
		}
	}

	return $data;
}