WC_Tracks_Event::sanitize_property_valuesprivate staticWC 1.0

Sanitize property values to ensure they can be safely serialized.

Converts array values to appropriate formats to prevent http_build_query() from creating bracket notation (e.g., prop[0], prop[1]) which violates the property name regex.

Метод класса: WC_Tracks_Event{}

Хуков нет.

Возвращает

Объект|Массив. Sanitized properties in the same type as input.

Использование

$result = WC_Tracks_Event::sanitize_property_values( $properties );
$properties(объект|массив) (обязательный)
Event properties as object or array.

Код WC_Tracks_Event::sanitize_property_values() WC 10.7.0

private static function sanitize_property_values( $properties ) {
	$is_object = is_object( $properties );
	$props     = $is_object ? get_object_vars( $properties ) : $properties;

	foreach ( $props as $key => $value ) {
		if ( ! is_array( $value ) ) {
			continue;
		}

		if ( ! $value ) {
			// Empty array becomes empty string.
			$props[ $key ] = '';
			continue;
		}

		// Check if array is indexed (not associative) and contains only scalar values.
		$is_indexed_array = array_keys( $value ) === range( 0, count( $value ) - 1 );
		$has_scalar_only  = ! array_filter(
			$value,
			function ( $item ) {
				return is_array( $item ) || is_object( $item );
			}
		);

		if ( $is_indexed_array && $has_scalar_only ) {
			// Indexed arrays with scalar values: join as comma string.
			$props[ $key ] = implode( ',', array_map( 'strval', $value ) );
			continue;
		}

		// Associative arrays or nested arrays become JSON strings.
		$encoded       = wp_json_encode( $value, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES );
		$props[ $key ] = ( false === $encoded ) ? '' : $encoded;
	}

	return $is_object ? (object) $props : $props;
}